Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add getlist and setlist for use this code with other language (common…

… type in this function is string)
  • Loading branch information...
commit 309b840019b26ff00c39f37bf99c4974d8a544dd 1 parent 5149c73
Denis Pauk authored April 14, 2009

Showing 2 changed files with 41 additions and 6 deletions. Show diff stats Hide diff stats

  1. 1  NOTICES
  2. 46  src/merle.erl
1  NOTICES
@@ -6,3 +6,4 @@ Contributors and Copyright holders:
6 6
 
7 7
  * Copyright 2009, Joe Williams <joe@joetify.com>
8 8
  * Copyright 2009, Nick Gerakines <nick@gerakines.net>
  9
+ * Copyright 2009, Denis Pauk   <pauk.denis@gmail.com>
46  src/merle.erl
... ...
@@ -1,5 +1,6 @@
1 1
 %% Copyright 2009, Joe Williams <joe@joetify.com>
2 2
 %% Copyright 2009, Nick Gerakines <nick@gerakines.net>
  3
+%% Copyright 2009, Denis Pauk <pauk.denis@gmail.com>
3 4
 %%
4 5
 %% Permission is hereby granted, free of charge, to any person
5 6
 %% obtaining a copy of this software and associated documentation
@@ -49,9 +50,9 @@
49 50
 
50 51
 %% gen_server API
51 52
 -export([
52  
-    stats/0, stats/1, version/0, getkey/1, delete/2, set/4, add/4, replace/2,
  53
+    stats/0, stats/1, version/0, getkey/1, delete/2, set/4, setlist/3, add/4, replace/2,
53 54
     replace/4, cas/5, set/2, flushall/0, flushall/1, verbosity/1, add/2,
54  
-    cas/3, getskey/1, connect/0, connect/2, delete/1, disconnect/0
  55
+    cas/3, getskey/1, connect/0, connect/2, delete/1, disconnect/0, getkeylist/1
55 56
 ]).
56 57
 
57 58
 %% gen_server callbacks
@@ -103,10 +104,21 @@ flushall(Delay) ->
103 104
 getkey(Key) when is_atom(Key) ->
104 105
 	getkey(atom_to_list(Key));
105 106
 getkey(Key) ->
106  
-	case gen_server:call(?SERVER, {getkey,{Key}}) of
  107
+	Bin = case gen_server:call(?SERVER, {getkey,{Key}}) of
107 108
 	    ["END"] -> undefined;
108 109
 	    [X] -> X
109  
-	end.
  110
+	end,
  111
+	binary_to_term(Bin).
  112
+
  113
+%% @doc retrieve value(list only) based off of key
  114
+getkeylist(Key) when is_atom(Key) ->
  115
+	getkeylist(atom_to_list(Key));
  116
+getkeylist(Key) ->
  117
+	Bin = case gen_server:call(?SERVER, {getkey,{Key}}) of
  118
+	    ["END"] -> undefined;
  119
+	    [X] -> X
  120
+	end,
  121
+	binary_to_list(Bin).
110 122
 
111 123
 %% @doc retrieve value based off of key for use with cas
112 124
 getskey(Key) when is_atom(Key) ->
@@ -159,6 +171,15 @@ set(Key, Value) ->
159 171
     Flag = random:uniform(?RANDOM_MAX),
160 172
     set(Key, integer_to_list(Flag), "0", Value).
161 173
 
  174
+setlist(Key, ExpTime, Value) ->
  175
+	Flag = random:uniform(?RANDOM_MAX),
  176
+	case gen_server:call(?SERVER, {setlist, {Key, integer_to_list(Flag), ExpTime, Value}}) of
  177
+	    ["STORED"] -> ok;
  178
+	    ["NOT_STORED"] -> not_stored;
  179
+	    [X] -> X
  180
+	end.
  181
+
  182
+
162 183
 set(Key, Flag, ExpTime, Value) when is_atom(Key) ->
163 184
 	set(atom_to_list(Key), Flag, ExpTime, Value);
164 185
 set(Key, Flag, ExpTime, Value) when is_integer(Flag) ->
@@ -291,6 +312,18 @@ handle_call({delete, {Key, Time}}, _From, Socket) ->
291 312
     ),
292 313
     {reply, Reply, Socket};
293 314
 
  315
+handle_call({setlist, {Key, Flag, ExpTime, Value}}, _From, Socket) ->
  316
+	Bin = list_to_binary(Value),
  317
+	Bytes = integer_to_list(size(Bin)),
  318
+    Reply = send_storage_cmd(
  319
+        Socket,
  320
+        iolist_to_binary([
  321
+            <<"set ">>, Key, <<" ">>, Flag, <<" ">>, ExpTime, <<" ">>, Bytes
  322
+        ]),
  323
+        Bin
  324
+    ),
  325
+    {reply, Reply, Socket};
  326
+	
294 327
 handle_call({set, {Key, Flag, ExpTime, Value}}, _From, Socket) ->
295 328
 	Bin = term_to_binary(Value),
296 329
 	Bytes = integer_to_list(size(Bin)),
@@ -428,7 +461,8 @@ recv_complex_gets_reply(Socket) ->
428 461
   			{ok,[_,_,_,Bytes,CasUniq], ListBin} = Parse,
429 462
   			Bin = list_to_binary(ListBin),
430 463
   			Reply = get_data(Socket, Bin, Bytes, length(ListBin)),
431  
-  			[CasUniq, Reply];
  464
+			ReplyTerm = binary_to_term(Reply),
  465
+  			[CasUniq, ReplyTerm];
432 466
   		{error, closed} ->
433 467
   			connection_closed
434 468
     after ?TIMEOUT -> timeout
@@ -447,4 +481,4 @@ get_data(Socket, Bin, Bytes, Len) when Len < Bytes + 7->
447 481
     end;
448 482
 get_data(_, Data, Bytes, _) ->
449 483
 	<<Bin:Bytes/binary, "\r\nEND\r\n">> = Data,
450  
-    binary_to_term(Bin).
  484
+    Bin.

0 notes on commit 309b840

Please sign in to comment.
Something went wrong with that request. Please try again.