Jacob Perkins avatar Jacob Perkins committed 0f0b1c0

initial list compatability, vsn 0.1.2

Comments (0)

Files changed (9)

 	(cd test;$(MAKE) clean)
 	rm -rf erl_crash.dump *.beam	
 
-test: clean
+test: FORCE
 	mkdir -p ebin/
 	(cd src;$(MAKE))
 	(cd test;$(MAKE))
 	@dialyzer --build_plt --output_plt .plt -q -r . -I include/
 
 check: all
-	@dialyzer --check_plt --plt .plt -q -r . -I include/
+	@dialyzer --check_plt --plt .plt -q -r . -I include/
+
+FORCE:
 		[R] when R == ok; R == nil; R == true; R == false -> R;
 		R -> R
 	end;
+internal_set_like(Client, Command, Key, Value) when not is_binary(Key) ->
+	internal_set_like(Client, Command, erldis_client:bin(Key), Value);
+internal_set_like(Client, Command, Key, Value) when not is_binary(Value) ->
+	internal_set_like(Client, Command, Key, erldis_client:bin(Value));
 internal_set_like(_, _, _, _) ->
 	{error, badarg}.
 
 			{error, unsupported}
 	end.
 
+numeric(false) -> 0;
+numeric(true) -> 1;
+numeric(I) -> I.
+
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %% Commands operating on every value %%
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-exists(Client, Key) -> erldis_client:sr_scall(Client, <<"exists ", Key/binary>>).
+exists(Client, Key) when not is_binary(Key) ->
+	exists(Client, erldis_client:bin(Key));
+exists(Client, Key) ->
+	erldis_client:sr_scall(Client, <<"exists ", Key/binary>>).
 
-del(Client, Key) -> erldis_client:sr_scall(Client, <<"del ", Key/binary>>).
+del(Client, Key) when not is_binary(Key) ->
+	del(Client, erldis_client:bin(Key));
+del(Client, Key) ->
+	erldis_client:sr_scall(Client, <<"del ", Key/binary>>).
 
-type(Client, Key) -> erldis_client:sr_scall(Client, <<"type ", Key/binary>>).
+type(Client, Key) when not is_binary(Key) ->
+	type(Client, erldis_client:bin(Key));
+type(Client, Key) ->
+	erldis_client:sr_scall(Client, <<"type ", Key/binary>>).
 
-keys(Client, Pattern) -> erldis_client:scall(Client, <<"keys ", Pattern/binary>>).
+keys(Client, Pattern) when not is_binary(Pattern) ->
+	keys(Client, erldis_client:bin(Pattern));
+keys(Client, Pattern) ->
+	erldis_client:scall(Client, <<"keys ", Pattern/binary>>).
 
-randomkey(Client, Key) -> erldis_client:sr_scall(Client, <<"randomkey ", Key/binary>>).
+randomkey(Client, Key) when not is_binary(Key) ->
+	randomkey(Client, erldis_client:bin(Key));
+randomkey(Client, Key) ->
+	erldis_client:sr_scall(Client, <<"randomkey ", Key/binary>>).
 
+rename(Client, OldKey, NewKey) when not is_binary(OldKey) ->
+	rename(Client, erldis_client:bin(OldKey), NewKey);
+rename(Client, OldKey, NewKey) when not is_binary(NewKey) ->
+	rename(Client, OldKey, erldis_client:bin(NewKey));
 rename(Client, OldKey, NewKey) ->
 	erldis_client:sr_scall(Client, <<"rename ", OldKey/binary, " ", NewKey/binary>>).
 
+renamenx(Client, OldKey, NewKey) when not is_binary(OldKey) ->
+	renamenx(Client, erldis_client:bin(OldKey), NewKey);
+renamenx(Client, OldKey, NewKey) when not is_binary(NewKey) ->
+	renamenx(Client, OldKey, erldis_client:bin(NewKey));
 renamenx(Client, OldKey, NewKey) ->
 	erldis_client:sr_scall(Client, <<"renamenx ", OldKey/binary, " ", NewKey/binary>>).
 
-dbsize(Client) -> erldis_client:sr_scall(Client, <<"dbsize ">>).
+dbsize(Client) -> numeric(erldis_client:sr_scall(Client, <<"dbsize ">>)).
 
+expire(Client, Key, Seconds) when not is_binary(Key) ->
+	expire(Client, erldis_client:bin(Key), Seconds);
 expire(Client, Key, Seconds) ->
 	erldis_client:sr_scall(Client, <<"expire ", Key/binary, " ", Seconds/binary>>).
 
 
 set(Client, Key, Value) -> internal_set_like(Client, <<"set ">>, Key, Value).
 
-get(Client, Key) -> erldis_client:sr_scall(Client, <<"get ", Key/binary>>).
+get(Client, Key) when not is_binary(Key) ->
+	get(Client, erldis_client:bin(Key));
+get(Client, Key) ->
+	erldis_client:sr_scall(Client, <<"get ", Key/binary>>).
 
 getset(Client, Key, Value) -> internal_set_like(Client, <<"getset ">>, Key, Value).
 
 
 setnx(Client, Key, Value) -> internal_set_like(Client, <<"setnx ">>, Key, Value).
 
-incr(Client, Key) -> erldis_client:sr_scall(Client, <<"incr ", Key/binary>>).
+incr(Client, Key) when not is_binary(Key) ->
+	incr(Client, erldis_client:bin(Key));
+incr(Client, Key) ->
+	numeric(erldis_client:sr_scall(Client, <<"incr ", Key/binary>>)).
 
-incrby(Client, Key, By) -> erldis_client:sr_scall(Client, <<"incrby ">>, [Key, By]).
+incrby(Client, Key, By) ->
+	numeric(erldis_client:sr_scall(Client, <<"incrby ">>, [Key, By])).
 
-decr(Client, Key) -> erldis_client:sr_scall(Client, <<"decr ", Key/binary>>).
+decr(Client, Key) when not is_binary(Key) ->
+	decr(Client, erldis_client:bin(Key));
+decr(Client, Key) ->
+	numeric(erldis_client:sr_scall(Client, <<"decr ", Key/binary>>)).
 
-decrby(Client, Key, By) -> erldis_client:sr_scall(Client, <<"decrby ">>, [Key, By]).
+decrby(Client, Key, By) ->
+	numeric(erldis_client:sr_scall(Client, <<"decrby ">>, [Key, By])).
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %% Commands operating on lists %%
 
 lpush(Client, Key, Value) -> internal_set_like(Client, <<"lpush ">>, Key, Value).
 
-llen(Client, Key) -> erldis_client:sr_scall(Client, <<"llen ">>, [Key]).
+llen(Client, Key) -> numeric(erldis_client:sr_scall(Client, <<"llen ">>, [Key])).
 
 lrange(Client, Key, Start, End) ->
 	erldis_client:scall(Client, <<"lrange ">>, [Key, Start, End]).
 lindex(Client, Key, Index) ->
 	erldis_client:sr_scall(Client, <<"lindex ">>, [Key, Index]).
 
+lset(Client, Key, Index, Value) when not is_binary(Value) ->
+	lset(Client, Key, Index, erldis_client:bin(Value));
 lset(Client, Key, Index, Value) ->
 	erldis_client:call(Client, <<"lset ">>, [[Key, Index, size(Value)], [Value]]).
 
+lrem(Client, Key, Number, Value) when not is_binary(Value) ->
+	lrem(Client, Key, Number, erldis_client:bin(Value));
 lrem(Client, Key, Number, Value) ->
 	erldis_client:call(Client, <<"lrem ">>, [[Key, Number, size(Value)], [Value]]).
 	
 
 srem(Client, Key, Value) -> internal_set_like(Client, <<"srem ">>, Key, Value).
 
+smove(Client, SrcKey, DstKey, Member) when not is_binary(Member) ->
+	smove(Client, SrcKey, DstKey, erldis_client:bin(Member));
 smove(Client, SrcKey, DstKey, Member) ->
 	erldis_client:call(Client, <<"smove ">>, [[SrcKey, DstKey, size(Member)], [Member]]).
 
-scard(Client, Key) -> erldis_client:sr_scall(Client, <<"scard ">>, [Key]).
+scard(Client, Key) -> numeric(erldis_client:sr_scall(Client, <<"scard ">>, [Key])).
 
 sismember(Client, Key, Value) -> internal_set_like(Client, <<"sismember ">>, Key, Value).
 
 move(Client, Key, DBIndex) ->
 	erldis_client:scall(Client, <<"move ">>, [Key, DBIndex]).
 
-flushdb(Client) -> erldis_client:scall(Client, <<"flushdb ">>).
+flushdb(Client) -> erldis_client:sr_scall(Client, <<"flushdb ">>).
 
-flushall(Client) -> erldis_client:scall(Client, <<"flushall ">>).
+flushall(Client) -> erldis_client:sr_scall(Client, <<"flushall ">>).
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %% Persistence control commands %%

src/erldis_dict.erl

 
 is_key(Key, Client) -> erldis:exists(Client, Key).
 
-size(Client) -> numeric_value(erldis:dbsize(Client)).
+size(Client) -> erldis:dbsize(Client).
 
 store(Key, [], Client) -> erase(Key, Client);
 store(Key, Value, Client) -> erldis:set(Client, Key, Value).
 
 % NOTE: this returns new count value, not a modified dict
 update_counter(Key, 1, Client) ->
-	numeric_value(erldis:incr(Client, Key));
+	erldis:incr(Client, Key);
 update_counter(Key, Incr, Client) ->
-	numeric_value(erldis:incrby(Client, Key, Incr)).
-
-%%%%%%%%%%%%%
-%% helpers %%
-%%%%%%%%%%%%%
-
-numeric_value(false) -> 0;
-numeric_value(true) -> 1;
-numeric_value(Val) -> Val.
+	erldis:incrby(Client, Key, Incr).

src/erldis_list.erl

 
 is_empty(Key, Client) -> len(Key, Client) == 0.
 
-len(Key, Client) ->
-	case erldis:llen(Client, Key) of
-		false -> 0;
-		true -> 1;
-		Len -> Len
-	end.
+len(Key, Client) -> erldis:llen(Client, Key).
 
 in(Item, Key, Client) ->
 	ok = erldis:rpush(Client, Key, Item).

src/erldis_sets.erl

 
 is_set(Client, Key) -> <<"set">> == erldis:type(Client, Key).
 
-size(Client, Key) ->
-	case erldis:scard(Client, Key) of
-		% redis actually returns 0 & 1, but those are interpreted as false & true
-		false -> 0;
-		true -> 1;
-		Size -> Size
-	end.
+size(Client, Key) -> erldis:scard(Client, Key).
 
 to_list(Client, Key) -> erldis:smembers(Client, Key).
 
 	lists:foreach(fun(Elem) -> add_element(Elem, Client, Key) end, List),
 	Client.
 
-is_element(Elem, Client, Key) ->
-	case erldis:sismember(Client, Key, Elem) of
-		false -> false;
-		true -> true
-	end.
+is_element(Elem, Client, Key) -> erldis:sismember(Client, Key, Elem).
 
 add_element(Elem, Client, Key) -> erldis:sadd(Client, Key, Elem).
 

test/erldis_dict_tests.erl

 	% setup
 	application:load(erldis),
 	{ok, Client} = erldis_client:connect(),
-	?assertEqual(erldis_client:call(Client, <<"flushdb">>), [ok]),
+	?assertEqual(erldis:flushdb(Client), ok),
 	% empty dict
 	?assertEqual(0, erldis_dict:size(Client)),
 	?assertEqual(undefined, erldis_dict:fetch(<<"foo">>, Client)),

test/erldis_list_compatibility_tests.erl

+-module(erldis_list_compatibility_tests).
+
+-include_lib("eunit/include/eunit.hrl").
+
+getset_test() ->
+	Client = setup(),
+	?assertEqual(erldis:exists(Client, <<"foo">>), false),
+	?assertEqual(erldis:exists(Client, "foo"), false),
+	?assertEqual(erldis:get(Client, <<"foo">>), nil),
+	?assertEqual(erldis:get(Client, "foo"), nil),
+	?assertEqual(erldis:set(Client, <<"foo">>, <<"bar">>), ok),
+	?assertEqual(erldis:get(Client, <<"foo">>), <<"bar">>),
+	?assertEqual(erldis:get(Client, "foo"), <<"bar">>),
+	?assertEqual(erldis:exists(Client, <<"foo">>), true),
+	?assertEqual(erldis:exists(Client, "foo"), true),
+	?assertEqual(erldis:del(Client, "foo"), true),
+	?assertEqual(erldis:exists(Client, <<"foo">>), false),
+	?assertEqual(erldis:exists(Client, "foo"), false),
+	?assertEqual(erldis:set(Client, "fool", "baz"), ok),
+	?assertEqual(erldis:get(Client, "fool"), <<"baz">>),
+	?assertEqual(erldis:get(Client, <<"fool">>), <<"baz">>),
+	?assertEqual(erldis:exists(Client, <<"fool">>), true),
+	?assertEqual(erldis:exists(Client, "fool"), true),
+	?assertEqual(erldis:del(Client, <<"fool">>), true),
+	?assertEqual(erldis:exists(Client, <<"fool">>), false),
+	?assertEqual(erldis:exists(Client, "fool"), false).
+
+incrdecr_test() ->
+	Client = setup(),
+	?assertEqual(erldis:incr(Client, <<"foo">>), 1),
+	?assertEqual(erldis:incr(Client, "foo"), 2),
+	?assertEqual(erldis:incrby(Client, <<"foo">>, 2), 4),
+	?assertEqual(erldis:incrby(Client, "foo", 3), 7),
+	?assertEqual(erldis:decr(Client, <<"foo">>), 6),
+	?assertEqual(erldis:decr(Client, "foo"), 5),
+	?assertEqual(erldis:decrby(Client, <<"foo">>, 2), 3),
+	?assertEqual(erldis:decrby(Client, "foo", 3), 0).
+
+setup() ->
+	% setup
+	application:load(erldis),
+	{ok, Client} = erldis_client:connect(),
+	?assertEqual(erldis:flushdb(Client), ok),
+	Client.

test/erldis_list_tests.erl

 	% setup
 	application:load(erldis),
 	{ok, Client} = erldis_client:connect(),
-	?assertEqual(erldis_client:call(Client, <<"flushdb ">>), [ok]),
+	?assertEqual(erldis:flushdb(Client), ok),
 	Client.

test/erldis_sets_tests.erl

 	% setup
 	application:load(erldis),
 	{ok, Client} = erldis_client:connect(),
-	?assertEqual(erldis_client:call(Client, <<"flushdb">>), [ok]),
+	?assertEqual(erldis:flushdb(Client), ok),
 	% non existent set
 	?assertEqual(erldis_sets:is_set(Client, <<"foo">>), false),
 	?assertEqual(erldis_sets:to_list(Client, <<"foo">>), []),
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.