Packages
kvs
2.1.0
13.5.22-aleph
13.4.16
13.4.15
13.4.14
13.4.13
13.3.1
13.2.28
11.9.1
10.8.3
10.8.2
10.3.0
9.9.2
9.9.1
9.9.0
9.8.0
9.7.0
9.4.8
9.4.7
9.4.6
9.4.5
9.4.4
9.4.3
9.4.2
9.4.1
9.4.0
8.12.0
8.11.2
8.11.1
8.10.4
8.10.3
8.10.2
8.10.1
8.10.0
8.5.2
8.5.1
8.5.0
8.4.1
8.4.0
8.3.1
8.3.0
7.11.5
7.9.1
7.7.0
7.1.3
7.1.2
7.1.1
6.12.11
6.12.10
6.12.9
6.12.8
6.12.7
6.12.6
6.12.5
6.12.4
6.12.3
6.12.2
6.12.1
6.12.0
6.11.2
6.11.1
6.11.0
6.10.2
6.10.1
6.10.0
6.9.2
6.9.1
6.9.0
6.7.7
6.7.6
6.7.5
6.7.4
6.7.3
6.7.2
6.7.1
6.7.0
6.6.0
2.1.0
0.12.1
retired
KVS Key-Value Store Abstraction Layer
Current section
Files
Jump to
Current section
Files
src/store_redis.erl
-module(store_redis).
-author('Andrey Martemyanov').
-copyright('Synrc Research Center s.r.o.').
-include("config.hrl").
-include("kvs.hrl").
-include("metainfo.hrl").
-compile(export_all).
start() -> erase(eredis_pid), {ok,C}=eredis:start_link(), put(eredis_pid,C), ok.
stop() -> P=erase(eredis_pid), eredis:stop(P), ok.
c() -> case get(eredis_pid) of
P when is_pid(P) ->
case is_process_alive(P) of true -> P; _ -> start(), get(eredis_pid) end;
_ -> start(), get(eredis_pid) end.
destroy() -> ok.
version() -> {version,"KVS REDIS"}.
dir() -> [{table,T}||T<-kvs:modules()].
join() -> initialize(), ok.
join(_Node) -> initialize(), ok.
change_storage(_Table,_Type) -> ok.
initialize() -> ok.
b2i(B) -> list_to_integer(binary_to_list(B)).
redis_table(RecordName) ->
list_to_binary(atom_to_list(RecordName)).
redis_key(RecordName,Key) ->
<<(redis_table(RecordName))/binary,$:,(term_to_binary(Key))/binary>>.
redis_keys(RecordName) ->
case eredis:q(c(), ["keys", <<(redis_table(RecordName))/binary,$:,$*>> ]) of
{ok,KeyList} when is_list(KeyList) -> KeyList;
_ -> [] end.
redis_put(#id_seq{thing=Thing,id=Incr}) when is_integer(Incr)->
eredis:q(c(), ["SET", redis_key(id_seq,Thing), Incr]);
redis_put(Record) ->
Key = redis_key(element(1,Record),element(2,Record)),
Value = term_to_binary(Record),
eredis:q(c(), ["SET", Key, Value]).
redis_get(Key, Fun) ->
case eredis:q(c(), ["GET", Key]) of
{ok, undefined} -> {error,not_found};
{ok, <<"QUEUED">>} -> transaction;
{ok, Value} ->
if is_function(Fun) -> {ok,Fun(Value)};
true -> {ok,binary_to_term(Value)} end;
E -> {error, E} end.
redis_get(Key) -> redis_get(Key, undefined).
redis_transaction(Fun) ->
{ok, <<"OK">>} = eredis:q(c(), ["MULTI"]),
Fun(),
{ok,List} = eredis:q(c(), ["EXEC"]),
List.
index(_RecordName,_Key,_Value) -> not_implemented.
get(id_seq,Key) ->
redis_get(redis_key(id_seq,Key), fun(Value) ->
#id_seq{thing=Key,id=b2i(Value)} end);
get(RecordName,Key) -> redis_get(redis_key(RecordName,Key)).
put(Records) when is_list(Records) ->
redis_transaction(fun() -> lists:foreach(fun put/1, Records) end);
put(Record) -> redis_put(Record).
delete(RecordName,Key) ->
case eredis:q(c(), ["DEL", redis_key(RecordName,Key)]) of
{ok,<<"1">>} -> ok;
E -> {error, E} end.
count(RecordName) -> length(redis_keys(RecordName)).
all(RecordName) ->
Keys = redis_keys(RecordName),
List = redis_transaction(fun() -> [redis_get(Key) || Key <- Keys] end),
case RecordName of
id_seq ->
lists:zipwith(fun(K,R) ->
#id_seq{thing=binary_to_term(binary_part(K,7,size(K)-7)),id=b2i(R)}
end, Keys, List);
_ -> [ binary_to_term(R) || R <- List ] end.
next_id(RecordName,Incr) ->
Key = redis_key(id_seq,RecordName),
{ok, Value} = eredis:q(c(), ["INCRBY", Key, Incr]),
b2i(Value).
create_table(_Name,_Options) -> ok.
add_table_index(_Record,_Field) -> not_implemented.