Commits

Anonymous committed 0b82560

drop/1, is_empty/1, fold/3 for riak_cache_backend

Comments (0)

Files changed (1)

apps/riak/src/riak_cache_backend.erl

 
 -module (riak_cache_backend).
 -export([start/2, stop/1, get/2, put/3, list/1, list_bucket/2, delete/2]).
+-export([drop/1, is_empty/1, fold/3]).
 -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
 
 -define(PRINT(Var), error_logger:info_msg("DEBUG: ~p:~p - ~p: ~p~n", [?MODULE, ?LINE, ??Var, Var])).
 delete(State, BKey) -> 
     gen_server:call(State, {delete, BKey}).
 
+drop(State) ->
+    gen_server:call(State, drop).
+
+is_empty(State) ->
+    gen_server:call(State, is_empty).
+
+fold(State, Fun0, Acc) ->
+    gen_server:call(State, {fold, Fun0, Acc}).
+
 stop(State) -> 
     gen_server:call(State, stop).
 
 handle_call(list, _From, State) ->
     % Fold through the gb_trees, gathering keys...
     F = fun(Bucket, Tree, Acc) ->
-        io:format("Bucket: ~p~n", [Bucket]),
-        io:format("Tree: ~p~n", [Tree]),
-        io:format("Acc: ~p~n", [Acc]),
+        %io:format("Bucket: ~p~n", [Bucket]),
+        %io:format("Tree: ~p~n", [Tree]),
+        %io:format("Acc: ~p~n", [Acc]),
 
         Keys = [{Bucket, Key} || Key <- gb_trees:keys(Tree)],
         Acc ++ Keys        
         none -> 
             {reply, [], State}
     end;
-
+handle_call(drop, _From, State=#state{time_tree=TT}) ->
+    [timer:cancel(E#entry.tref) || {_, E} <- gb_trees:to_list(TT)],
+    {reply, ok, State#state{obj_tree=gb_trees:empty(), 
+                            time_tree=gb_trees:empty(),
+                            used_memory=0}};
+handle_call(is_empty, _From, State) ->
+    {reply, AllKeys, State} = handle_call(list, _From, State),
+    case AllKeys of
+        [] ->
+            {reply, true, State};
+        _ ->
+            {reply, false, State}
+    end;
+handle_call({fold, Fun0, Acc}, _From, State) ->
+    {reply, Keys, _} = handle_call(list, _From, State),
+    Reply = do_fold(State, Keys, Fun0, Acc),
+    {reply, Reply, State};
 handle_call(stop, _From, State) -> 
     {reply, ok, State}.
 
 %% Private Functions
 %%
 
+do_fold(State=#state{obj_tree=_OT}, Keys, Fun0, Acc) ->
+    Objs = [element(2, obj_find(B, K, State)) || {B,K} <- Keys],
+    Fun = fun(E, AccIn) ->
+                  Fun0({E#entry.bucket, E#entry.key}, E#entry.value, AccIn)
+          end,
+    lists:foldl(Fun, Acc, Objs).
+    
 % Lookup the #entry record specified by Bucket/Key.    
 % Returns {value, Entry} or none.
 obj_find(Bucket, Key, State) -> 
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.