Skip to content

Commit

Permalink
Renamed write_batch to batch.
Browse files Browse the repository at this point in the history
  • Loading branch information
oubiwann committed Mar 20, 2024
1 parent 75a709d commit 80ebea3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 45 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,17 @@ ok = etskv:delete(Key, Store).

#### Storing

Using `etskv:write_batch/2` you can write and delete multiple values in one
Using `etskv:batch/2` you can write and delete multiple values in one
pass:

```erl
ok = etskv:write_batch([{put, <<"a">>, 1},
{put, <<"b">>, 2},
{put, <<"c">>, 3}], Store),
ok = etskv:batch([{put, <<"a">>, 1},
{put, <<"b">>, 2},
{put, <<"c">>, 3}], Store),

ok = etskv:write_batch([{put, <<"d">>, 4},
{delete, <<"b">>},
{put, <<"e">>, 5}], Store).
ok = etskv:batch([{put, <<"d">>, 4},
{delete, <<"b">>},
{put, <<"e">>, 5}], Store).
```

#### Retrieving
Expand Down
30 changes: 15 additions & 15 deletions src/etskv.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
get/2, get/3,
put/3,
delete/2,
write_batch/2,
batch/2,
contains/2,
fold_keys/4,
fold/4]).
Expand All @@ -29,12 +29,12 @@


%% private ent private entry points (gen_server callbacks)
-export([ init/1
, handle_call/3
, handle_cast/2
, handle_info/2
, terminate/2
, code_change/3]).
-export([init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2,
code_change/3]).

%% private iterator loop entry point
-export([ iterator_loop/1 ]).
Expand All @@ -53,7 +53,7 @@

-type key() :: binary().
-type value() :: term() | any().
-type write_ops() :: [{put, key(), value()} | {delete, key()}].
-type batch_ops() :: [{put, key(), value()} | {delete, key()}].
-type fold_options() :: [{start_key, key()}
| {end_key, key()}
| {gt, key()}
Expand Down Expand Up @@ -96,17 +96,17 @@ get(Key, Db, Default) ->
%% @doc Associates Key with value Value and store it.
-spec put(Key :: key(), Value :: value(), Db :: db()) -> ok.
put(Key, Value, Db) ->
write_batch([{put, Key, Value}], Db).
batch([{put, Key, Value}], Db).

%% @doc delete a Key
-spec delete(Key :: key(), Db :: db()) -> ok.
delete(Key, Db) ->
write_batch([{delete, Key}], Db).
batch([{delete, Key}], Db).

%% @doc Apply atomically a set of updates to the store
-spec write_batch(Ops :: write_ops(), Db :: db()) -> ok.
write_batch(Ops, #{ writer := W }) ->
gen_server:call(W, {write, Ops}).
-spec batch(Ops :: batch_ops(), Db :: db()) -> ok.
batch(Ops, #{ writer := W }) ->
gen_server:call(W, {batch, Ops}).

%% @doc check if a Key exists in the store
-spec contains(Key :: key(), Db :: db()) -> truefalse.
Expand Down Expand Up @@ -375,7 +375,7 @@ init([Name, _Options]) ->
busy_versions => []}}.


handle_call({write, Ops}, _From, State) ->
handle_call({batch, Ops}, _From, State) ->
#{ tab := Tab, version := Version} = State,
NextVersion = Version + 1,
ToInsert = process_ops(Ops, NextVersion, Tab, []),
Expand All @@ -394,7 +394,7 @@ handle_call(close, _From, #{ tab := Tab } = State) ->
handle_call(_Msg, _From, State) ->
{reply, badarg, State}.

handle_cast({write, Ops}, State) ->
handle_cast({batch, Ops}, State) ->
#{ tab := Tab, version := Version} = State,
NextVersion = Version +1,
ToInsert = process_ops(Ops, NextVersion, Tab, []),
Expand Down
46 changes: 23 additions & 23 deletions test/etskv_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ basic_test() ->
?assertEqual(3, etskv:get(<<"a1">>, Store)),
etskv:close(Store).

write_batch_test() ->
batch_test() ->
Store = etskv:open(test),
etskv:write_batch([{put, <<"a">>, 1},
etskv:batch([{put, <<"a">>, 1},
{put, <<"b">>, 2},
{put, <<"c">>, 1}], Store),

Expand All @@ -34,7 +34,7 @@ write_batch_test() ->

iterator_test() ->
Store = etskv:open(test),
etskv:write_batch([{put, <<"a">>, 1},
etskv:batch([{put, <<"a">>, 1},
{put, <<"b">>, 2},
{put, <<"c">>, 1}], Store),

Expand Down Expand Up @@ -71,10 +71,10 @@ iterator_test() ->

fold_keys_test() ->
Store = etskv:open(test),
ok = etskv:write_batch([{put, <<"a">>, 1},
{put, <<"b">>, 2},
{put, <<"c">>, 3},
{put, <<"d">>, 4}], Store),
ok = etskv:batch([{put, <<"a">>, 1},
{put, <<"b">>, 2},
{put, <<"c">>, 3},
{put, <<"d">>, 4}], Store),

AccFun = fun(K, Acc) -> [K | Acc] end,
?assertMatch([<<"a">>, <<"b">>, <<"c">>, <<"d">>],
Expand All @@ -84,10 +84,10 @@ fold_keys_test() ->

fold_gt_test() ->
Store = etskv:open(test),
ok = etskv:write_batch([{put, <<"a">>, 1},
{put, <<"b">>, 2},
{put, <<"c">>, 3},
{put, <<"d">>, 4}], Store),
ok = etskv:batch([{put, <<"a">>, 1},
{put, <<"b">>, 2},
{put, <<"c">>, 3},
{put, <<"d">>, 4}], Store),

AccFun = fun(K, V, Acc) ->
[{K, V} | Acc]
Expand All @@ -100,10 +100,10 @@ fold_gt_test() ->

fold_lt_test() ->
Store = etskv:open(test),
ok = etskv:write_batch([{put, <<"a">>, 1},
{put, <<"b">>, 2},
{put, <<"c">>, 3},
{put, <<"d">>, 4}], Store),
ok = etskv:batch([{put, <<"a">>, 1},
{put, <<"b">>, 2},
{put, <<"c">>, 3},
{put, <<"d">>, 4}], Store),

AccFun = fun(K, V, Acc) ->
[{K, V} | Acc]
Expand All @@ -115,10 +115,10 @@ fold_lt_test() ->

fold_lt_gt_test() ->
Store = etskv:open(test),
ok = etskv:write_batch([{put, <<"a">>, 1},
{put, <<"b">>, 2},
{put, <<"c">>, 3},
{put, <<"d">>, 4}], Store),
ok = etskv:batch([{put, <<"a">>, 1},
{put, <<"b">>, 2},
{put, <<"c">>, 3},
{put, <<"d">>, 4}], Store),

AccFun = fun(K, V, Acc) ->
[{K, V} | Acc]
Expand All @@ -133,10 +133,10 @@ fold_lt_gt_test() ->

fold_lt_gt_max_test() ->
Store = etskv:open(test),
ok = etskv:write_batch([{put, <<"a">>, 1},
{put, <<"b">>, 2},
{put, <<"c">>, 3},
{put, <<"d">>, 4}], Store),
ok = etskv:batch([{put, <<"a">>, 1},
{put, <<"b">>, 2},
{put, <<"c">>, 3},
{put, <<"d">>, 4}], Store),



Expand Down

0 comments on commit 80ebea3

Please sign in to comment.