Permalink
Browse files

Add delete command (without time support).

SVN Revision: 4
  • Loading branch information...
1 parent 4edb048 commit 88a1a73ede2007e9cbe94b27d91ed0eb4d3a9086 @jsautret jsautret committed Jun 21, 2007
Showing with 60 additions and 9 deletions.
  1. +2 −1 TODO
  2. +46 −5 src/onecached_server.erl
  3. +12 −3 src/onecached_storage.erl
View
3 TODO
@@ -1,3 +1,4 @@
-* start sasl
+* implement all commands
+* support for second time argument support for delete command
* virtual hosts ?
View
@@ -91,16 +91,17 @@ read_line([Char|Data], Line) ->
%% FSM Callbacks
%%====================================================================
-% process a memcached storage command line
+% memcached "set" storage command line
process_command({line, "set "++Line}, StateData) ->
io:format("process_command set"),
{next_state, process_data_block, StateData#state{command=parse_storage_command(Line)}};
+% memcached "add" storage command line
process_command({line, "add "++Line}, StateData) ->
StorageCommand = parse_storage_command(Line),
case StorageCommand of
#storage_command{key=Key} ->
NewStorageCommand = StateData#state{command=StorageCommand},
- case onecached_storage:has_key(StateData#state.storage, Key) of
+ case onecached_storage:has_item(StateData#state.storage, Key) of
true ->
{next_state, discard_data_block, NewStorageCommand};
false ->
@@ -112,12 +113,13 @@ process_command({line, "add "++Line}, StateData) ->
{next_state, process_command, StateData}
end;
+% memcached "replace" storage command line
process_command({line, "replace "++Line}, StateData) ->
StorageCommand = parse_storage_command(Line),
case StorageCommand of
#storage_command{key=Key} ->
NewStorageCommand = StateData#state{command=StorageCommand},
- case onecached_storage:has_key(StateData#state.storage, Key) of
+ case onecached_storage:has_item(StateData#state.storage, Key) of
true ->
{next_state, process_data_block, NewStorageCommand};
false ->
@@ -129,7 +131,7 @@ process_command({line, "replace "++Line}, StateData) ->
{next_state, process_command, StateData}
end;
-% process a memcached retrieval command line
+% memcached "get" retrieval command line
process_command({line, "get "++Line}, #state{socket=Socket, storage=Storage}=StateData) ->
Keys = parse_retrieval_command(Line),
lists:foreach(fun(Key) ->
@@ -138,7 +140,28 @@ process_command({line, "get "++Line}, #state{socket=Socket, storage=Storage}=Sta
send_command(Socket, "END"),
{next_state, process_command, StateData};
-% unknown command
+% memcached "delete" command line
+% TODO second time argument support
+process_command({line, "delete "++Line}, #state{socket=Socket, storage=Storage}=StateData) ->
+ case parse_delete_command(Line) of
+ {Key, _Time} ->
+ case onecached_storage:delete_item(Storage, Key) of
+ ok ->
+ send_command(Socket, "DELETED");
+ none ->
+ send_command(Socket, "NOT_FOUND");
+ Other ->
+ ?ERROR_MSG("SERVER_ERROR~n~p~n", [Other]),
+ send_command(Socket, io_lib:format("SERVER_ERROR ~p", [Other]))
+ end;
+ _ ->
+ ?ERROR_MSG("CLIENT_ERROR invalid delete command format~n~p~n", [Line]),
+ send_command(Socket, "CLIENT_ERROR invalid delete command format: delete "++Line)
+ end,
+ {next_state, process_command, StateData};
+
+
+% unknown memcached command
process_command({line, Line}, #state{socket=Socket} = StateData) ->
?ERROR_MSG("CLIENT_ERROR unknown command~n~p~n", [Line]),
send_command(Socket, "CLIENT_ERROR unknown command "++Line),
@@ -286,3 +309,21 @@ parse_storage_command(Line) ->
% return [Key] when is_list(Key)
parse_retrieval_command(Line) ->
string:tokens(Line, " ").
+
+% Format of Line is
+% <key> <time>
+% return {Key, Time}
+parse_delete_command(Line) ->
+ case string:tokens(Line, " ") of
+ [Key, STime] ->
+ case string:to_integer(STime) of
+ {Time, ""} ->
+ {Key, Time};
+ _ ->
+ error
+ end;
+ [Key] ->
+ {Key, 0};
+ _ ->
+ error
+ end.
View
@@ -5,8 +5,9 @@
-export([init/1,
store_item/2,
- has_key/2,
- get_item/2]).
+ has_item/2,
+ get_item/2,
+ delete_item/2]).
-include("onecached.hrl").
@@ -37,7 +38,7 @@ store_item(mnesia, #storage_command{exptime=Exptime} = StorageCommand) ->
store_item(mnesia, StorageCommand#storage_command{exptime=Exptime + (MegaSecs*1000+Secs)}).
% return true if a item with Key is present
-has_key(Storage, Key) ->
+has_item(Storage, Key) ->
case get_item(Storage, Key) of
{ok, _} ->
true;
@@ -78,3 +79,11 @@ get_item(mnesia, Key) ->
{aborted, Reason} ->
{error, Reason}
end.
+
+delete_item(mnesia, Key) ->
+ case has_item(mnesia, Key) of
+ true ->
+ mnesia:dirty_delete({onecached, Key});
+ false ->
+ none
+ end.

0 comments on commit 88a1a73

Please sign in to comment.