Anonymous avatar Anonymous committed 3d7d826

drop/1, is_empty/1, and fold/3 for riak_gb_trees_backend

Comments (0)

Files changed (1)

apps/riak/src/riak_gb_trees_backend.erl

 -module(riak_gb_trees_backend).
 
 -include_lib("eunit/include/eunit.hrl").
--export([start/2, stop/1,get/2,put/3,list/1,list_bucket/2,delete/2]).
+-export([start/2, stop/1,get/2,put/3,list/1,list_bucket/2,delete/2,is_empty/1,fold/3,drop/1]).
 
 % @type state() = term().
 -record(state, {pid}).
     Ref = make_ref(),
     Pid ! {list_bucket, Bucket, self(), Ref},
     receive {list_bucket_response, Result, Ref} -> Result end.
+
+is_empty(#state { pid=Pid }) ->
+    Ref = make_ref(),
+    Pid ! {is_empty, self(), Ref},
+    receive {is_empty_response, Result, Ref} -> Result end.
+
+fold(#state{ pid=Pid }, Fun0, Acc) ->
+    Ref = make_ref(),
+    Pid ! {fold, Fun0, Acc, self(), Ref},
+    receive {fold_response, Result, Ref} -> Result end.
+
+drop(#state{ pid=Pid }) ->
+    Ref = make_ref(),
+    Pid ! {drop, self(), Ref},
+    receive {drop_response, Result, Ref} -> Result end.
     
 tree_loop(Tree) ->
     receive
                    srv_list_bucket(Tree, Bucket), Ref},
             tree_loop(Tree);
 
+        {is_empty, Pid, Ref} ->
+            Pid ! {is_empty_response, gb_trees:is_empty(Tree), Ref},
+            tree_loop(Tree);
+
+        {drop, Pid, Ref} ->
+            Pid ! {drop_response, ok, Ref},
+            tree_loop(gb_trees:empty());
+
+        {fold, Fun0, Acc, Pid, Ref} ->
+            Pid ! {fold_response,
+                   srv_fold(Tree, Fun0, Acc), Ref},
+            tree_loop(Tree);
+
         {stop, Pid, Ref} ->
             Pid ! {stop_response, ok, Ref}
     end.
 srv_list_bucket(Tree, Bucket) ->
     [ Key || {B, Key} <- gb_trees:keys(Tree), B == Bucket ].
 
+srv_fold(Tree, Fun0, Acc) ->
+    Iter0 = gb_trees:iterator(Tree),
+    srv_fold1(gb_trees:next(Iter0), Fun0, Acc).
+
+srv_fold1(none, _Fun0, Acc) ->
+    Acc;
+srv_fold1({K,V,Iter}, Fun0, Acc) ->
+    srv_fold1(gb_trees:next(Iter), Fun0, Fun0(K,V,Acc)).
+
 
 %%
 %% Test
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.