Added a new update method that takes args to pass to the fun.

This commit is contained in:
Phillip Toland 2008-12-12 15:50:44 -06:00
parent 824ec5ef69
commit f20e46d756
2 changed files with 20 additions and 2 deletions

View file

@ -17,7 +17,7 @@
put_r/3, put_r/4,
get/2, get/3,
get_r/2, get_r/3,
update/3]).
update/3, update/4]).
-include("bdberl.hrl").
@ -167,9 +167,15 @@ get_r(Db, Key, Opts) ->
end.
update(Db, Key, Fun) ->
update(Db, Key, Fun, undefined).
update(Db, Key, Fun, Args) ->
F = fun() ->
{ok, Value} = get(Db, Key, [rmw]),
NewValue = Fun(Key, Value),
NewValue = case Args of
undefined -> Fun(Key, Value);
Args -> Fun(Key, Value, Args)
end,
ok = put(Db, Key, NewValue),
NewValue
end,

View file

@ -22,6 +22,7 @@ all() ->
transaction_should_abort_on_exception,
transaction_should_abort_on_user_abort,
update_should_save_value_if_successful,
update_should_accept_args_for_fun,
port_should_tune_transaction_timeouts].
@ -107,6 +108,17 @@ update_should_save_value_if_successful(Config) ->
{ok, newvalue} = bdberl:update(Db, mykey, F),
{ok, newvalue} = bdberl:get(Db, mykey).
update_should_accept_args_for_fun(Config) ->
Db = ?config(db, Config),
ok = bdberl:put(Db, mykey, avalue),
F = fun(_Key, _Value, Args) ->
look_at_me = Args, % This is all we are interested in
newvalue
end,
{ok, newvalue} = bdberl:update(Db, mykey, F, look_at_me).
port_should_tune_transaction_timeouts(_Config) ->
%% Test transaction timeouts
{ok, 500000} = bdberl:get_txn_timeout(),