Skip to content

Instantly share code, notes, and snippets.

View greggy's full-sized avatar

Grigory Fateyev (aka greg) greggy

View GitHub Profile
@greggy
greggy / postgres_add_json_sub_array_unique.sql
Created January 25, 2020 06:35 — forked from mpneuried/postgres_add_json_sub_array_unique.sql
Add and remove elements unique to a Postgres jsonb sub key: Short a Set implemetation
UPDATE public.mytable SET
jsonfieldname = jsonb_set( jsonfieldname, '{json_obj_key}', array_to_json(
ARRAY(
SELECT DISTINCT( UNNEST( ARRAY(
SELECT json_array_elements_text( COALESCE( jsonfieldname::json->'json_obj_key', '[]' ) )
) || ARRAY['Element to add'] ) )
)
)::jsonb )
WHERE id = 23
RETURNING *;
@greggy
greggy / postgres_add_json_sub_array_unique.sql
Created January 25, 2020 06:35 — forked from mpneuried/postgres_add_json_sub_array_unique.sql
Add and remove elements unique to a Postgres jsonb sub key: Short a Set implemetation
UPDATE public.mytable SET
jsonfieldname = jsonb_set( jsonfieldname, '{json_obj_key}', array_to_json(
ARRAY(
SELECT DISTINCT( UNNEST( ARRAY(
SELECT json_array_elements_text( COALESCE( jsonfieldname::json->'json_obj_key', '[]' ) )
) || ARRAY['Element to add'] ) )
)
)::jsonb )
WHERE id = 23
RETURNING *;
@greggy
greggy / join_binaries.erl
Last active February 22, 2018 06:27
The example shows how to concatinate (join) the list of binaries with separator in Erlang
-module(join_binries).
-export([join/2]).
join(TitleList, Sep) ->
Fun = fun(<<>>, Acc) ->
Acc;
(Word, Acc) ->
<<Acc/binary, Sep, Word/binary>>
end,
lists:foldl(Fun, hd(TitleList), tl(TitleList)).
collections.map(function(coll) {
return {
type: "checkbox"
, callback: function() { console.log('this', this.checked) }
, checked: false
, text: coll.title
}
})
# coding: utf8
import pymongo
connection = pymongo.MongoClient('localhost', 27017)
db = connection.photos
albums = db.albums
images = db.images
{ok, Viewers} = eredis:q(State#state.redis_pool, ["HGET", "channel:clients", Key]),
ViewersInt = list_to_integer(binary_to_list(Viewers)),
case ViewersInt > 0 of
true ->
eredis:q(State#state.redis_pool, ["HSET", "channel:clients", Key, 0]),
?D([Key, ViewersInt]),
net_pinger:set_billing(binary_to_list(Key), ViewersInt);
_ -> ok
end
%%%-------------------------------------------------------------------
%%% @author greg <>
%%% @copyright (C) 2012, greg
%%% @doc
%%%
%%% @end
%%% Created : 29 Nov 2012 by greg <>
%%%-------------------------------------------------------------------
-module(ring).
%%%-------------------------------------------------------------------
%%% @author greg <>
%%% @copyright (C) 2012, greg
%%% @doc
%%%
%%% @end
%%% Created : 29 Nov 2012 by greg <>
%%%-------------------------------------------------------------------
-module(ring).
start(ProcNum, MsgNum, Message) ->
init(#state{num_procs=ProcNum, msg_num=MsgNum, msg=Message}).
%% first process
init(#state{proc_num=Proc, msg=Message}=State) when Proc == 1 ->
Pid = spawn(ring, loop, [Proc]),
Pid ! Message,
init(State#state{proc_num=Proc+1, first_pid=Pid});
%% last process
start(ProcNum, MsgNum, Message) ->
start(ProcNum, MsgNum, Message, 0, 0, 1).
start(ProcNum, MsgNum, Message, PrevPid, FirstPid, Num) when ProcNum == Num ->
spawn(ring, loop, [Num, FirstPid, MsgNum, Message]);
start(ProcNum, MsgNum, Message, PrevPid, FirstPid, 1) ->
Pid = spawn(ring, loop, [1, PrevPid, MsgNum, Message]),
start(ProcNum, MsgNum, Message, Pid, Pid, 2),
Pid ! Message;
start(ProcNum, MsgNum, Message, PrevPid, FirstPid, Num) ->