From f20e46d7565945402c1b8e025d3087eece20bdc4 Mon Sep 17 00:00:00 2001 From: Phillip Toland Date: Fri, 12 Dec 2008 15:50:44 -0600 Subject: [PATCH] Added a new update method that takes args to pass to the fun. --- src/bdberl.erl | 10 ++++++++-- test/bdberl_SUITE.erl | 12 ++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/bdberl.erl b/src/bdberl.erl index 22b02ac..54e0f3e 100644 --- a/src/bdberl.erl +++ b/src/bdberl.erl @@ -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, diff --git a/test/bdberl_SUITE.erl b/test/bdberl_SUITE.erl index 9f0d75e..c54f380 100644 --- a/test/bdberl_SUITE.erl +++ b/test/bdberl_SUITE.erl @@ -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(),