diff options
author | Robert Dionne <bob@cloudant.com> | 2010-11-11 11:21:51 -0500 |
---|---|---|
committer | Adam Kocoloski <adam@cloudant.com> | 2010-12-09 14:54:03 -0500 |
commit | 5c3161d57b1d2d7953497c24be09112b6bcf71b6 (patch) | |
tree | ed595f8753ea159396ec78d66da67257c0fc1d33 /apps/couch | |
parent | 8afe062498cf5996096e97e4719fae82d9f47753 (diff) |
Add support for inclusive_end to fold_reduce, BugzID 11069
Diffstat (limited to 'apps/couch')
-rw-r--r-- | apps/couch/src/couch_btree.erl | 66 | ||||
-rwxr-xr-x | apps/couch/test/etap/020-btree-basics.t | 30 |
2 files changed, 55 insertions, 41 deletions
diff --git a/apps/couch/src/couch_btree.erl b/apps/couch/src/couch_btree.erl index 4ed3fe54..34c756f9 100644 --- a/apps/couch/src/couch_btree.erl +++ b/apps/couch/src/couch_btree.erl @@ -75,18 +75,18 @@ final_reduce(Reduce, {KVs, Reductions}) -> fold_reduce(#btree{root=Root}=Bt, Fun, Acc, Options) -> Dir = couch_util:get_value(dir, Options, fwd), - StartKey = couch_util:get_value(start_key, Options), - EndKey = couch_util:get_value(end_key, Options), + InRangeFun = make_key_in_end_range_function(Bt,Dir,Options), KeyGroupFun = couch_util:get_value(key_group_fun, Options, fun(_,_) -> true end), - {StartKey2, EndKey2} = case Dir of - rev -> {EndKey, StartKey}; - fwd -> {StartKey, EndKey} + fwd -> + StartKey = couch_util:get_value(start_key, Options); + rev -> + StartKey = couch_util:get_value(end_key, Options) end, try {ok, Acc2, GroupedRedsAcc2, GroupedKVsAcc2, GroupedKey2} = - reduce_stream_node(Bt, Dir, Root, StartKey2, EndKey2, undefined, [], [], - KeyGroupFun, Fun, Acc), + reduce_stream_node(Bt, Dir, Root, StartKey, InRangeFun, undefined, + [], [], KeyGroupFun, Fun, Acc), if GroupedKey2 == undefined -> {ok, Acc2}; true -> @@ -461,21 +461,21 @@ modify_kvnode(Bt, NodeTuple, LowerBound, [{ActionType, ActionKey, ActionValue} | end. -reduce_stream_node(_Bt, _Dir, nil, _KeyStart, _KeyEnd, GroupedKey, GroupedKVsAcc, +reduce_stream_node(_Bt, _Dir, nil, _KeyStart, _InRange, GroupedKey, GroupedKVsAcc, GroupedRedsAcc, _KeyGroupFun, _Fun, Acc) -> {ok, Acc, GroupedRedsAcc, GroupedKVsAcc, GroupedKey}; -reduce_stream_node(Bt, Dir, {P, _R}, KeyStart, KeyEnd, GroupedKey, GroupedKVsAcc, +reduce_stream_node(Bt, Dir, {P, _R}, KeyStart, InRange, GroupedKey, GroupedKVsAcc, GroupedRedsAcc, KeyGroupFun, Fun, Acc) -> case get_node(Bt, P) of {kp_node, NodeList} -> - reduce_stream_kp_node(Bt, Dir, NodeList, KeyStart, KeyEnd, GroupedKey, + reduce_stream_kp_node(Bt, Dir, NodeList, KeyStart, InRange, GroupedKey, GroupedKVsAcc, GroupedRedsAcc, KeyGroupFun, Fun, Acc); {kv_node, KVs} -> - reduce_stream_kv_node(Bt, Dir, KVs, KeyStart, KeyEnd, GroupedKey, + reduce_stream_kv_node(Bt, Dir, KVs, KeyStart, InRange, GroupedKey, GroupedKVsAcc, GroupedRedsAcc, KeyGroupFun, Fun, Acc) end. -reduce_stream_kv_node(Bt, Dir, KVs, KeyStart, KeyEnd, +reduce_stream_kv_node(Bt, Dir, KVs, KeyStart, InRange, GroupedKey, GroupedKVsAcc, GroupedRedsAcc, KeyGroupFun, Fun, Acc) -> @@ -486,16 +486,7 @@ reduce_stream_kv_node(Bt, Dir, KVs, KeyStart, KeyEnd, _ -> lists:dropwhile(fun({Key,_}) -> less(Bt, Key, KeyStart) end, KVs) end, - KVs2 = - case KeyEnd of - undefined -> - GTEKeyStartKVs; - _ -> - lists:takewhile( - fun({Key,_}) -> - not less(Bt, KeyEnd, Key) - end, GTEKeyStartKVs) - end, + KVs2 = lists:takewhile(fun({Key,_}) -> InRange(Key) end, GTEKeyStartKVs), reduce_stream_kv_node2(Bt, adjust_dir(Dir, KVs2), GroupedKey, GroupedKVsAcc, GroupedRedsAcc, KeyGroupFun, Fun, Acc). @@ -527,7 +518,7 @@ reduce_stream_kv_node2(Bt, [{Key, Value}| RestKVs], GroupedKey, GroupedKVsAcc, end end. -reduce_stream_kp_node(Bt, Dir, NodeList, KeyStart, KeyEnd, +reduce_stream_kp_node(Bt, Dir, NodeList, KeyStart, InRange, GroupedKey, GroupedKVsAcc, GroupedRedsAcc, KeyGroupFun, Fun, Acc) -> Nodes = @@ -540,29 +531,24 @@ reduce_stream_kp_node(Bt, Dir, NodeList, KeyStart, KeyEnd, less(Bt, Key, KeyStart) end, NodeList) end, - NodesInRange = - case KeyEnd of - undefined -> - Nodes; - _ -> - {InRange, MaybeInRange} = lists:splitwith( - fun({Key,_}) -> - less(Bt, Key, KeyEnd) - end, Nodes), - InRange ++ case MaybeInRange of [] -> []; [FirstMaybe|_] -> [FirstMaybe] end + case lists:splitwith(fun({Key,_}) -> InRange(Key) end, Nodes) of + {InTheRange, []} -> + NodesInRange = InTheRange; + {InTheRange, [FirstMaybe|_]} -> + NodesInRange = InTheRange ++ [FirstMaybe] end, - reduce_stream_kp_node2(Bt, Dir, adjust_dir(Dir, NodesInRange), KeyStart, KeyEnd, + reduce_stream_kp_node2(Bt, Dir, adjust_dir(Dir, NodesInRange), KeyStart, InRange, GroupedKey, GroupedKVsAcc, GroupedRedsAcc, KeyGroupFun, Fun, Acc). -reduce_stream_kp_node2(Bt, Dir, [{_Key, NodeInfo} | RestNodeList], KeyStart, KeyEnd, +reduce_stream_kp_node2(Bt, Dir, [{_Key, NodeInfo} | RestNodeList], KeyStart, InRange, undefined, [], [], KeyGroupFun, Fun, Acc) -> {ok, Acc2, GroupedRedsAcc2, GroupedKVsAcc2, GroupedKey2} = - reduce_stream_node(Bt, Dir, NodeInfo, KeyStart, KeyEnd, undefined, + reduce_stream_node(Bt, Dir, NodeInfo, KeyStart, InRange, undefined, [], [], KeyGroupFun, Fun, Acc), - reduce_stream_kp_node2(Bt, Dir, RestNodeList, KeyStart, KeyEnd, GroupedKey2, + reduce_stream_kp_node2(Bt, Dir, RestNodeList, KeyStart, InRange, GroupedKey2, GroupedKVsAcc2, GroupedRedsAcc2, KeyGroupFun, Fun, Acc2); -reduce_stream_kp_node2(Bt, Dir, NodeList, KeyStart, KeyEnd, +reduce_stream_kp_node2(Bt, Dir, NodeList, KeyStart, InRange, GroupedKey, GroupedKVsAcc, GroupedRedsAcc, KeyGroupFun, Fun, Acc) -> {Grouped0, Ungrouped0} = lists:splitwith(fun({Key,_}) -> KeyGroupFun(GroupedKey, Key) end, NodeList), @@ -578,9 +564,9 @@ reduce_stream_kp_node2(Bt, Dir, NodeList, KeyStart, KeyEnd, case UngroupedNodes of [{_Key, NodeInfo}|RestNodes] -> {ok, Acc2, GroupedRedsAcc2, GroupedKVsAcc2, GroupedKey2} = - reduce_stream_node(Bt, Dir, NodeInfo, KeyStart, KeyEnd, GroupedKey, + reduce_stream_node(Bt, Dir, NodeInfo, KeyStart, InRange, GroupedKey, GroupedKVsAcc, GroupedReds ++ GroupedRedsAcc, KeyGroupFun, Fun, Acc), - reduce_stream_kp_node2(Bt, Dir, RestNodes, KeyStart, KeyEnd, GroupedKey2, + reduce_stream_kp_node2(Bt, Dir, RestNodes, KeyStart, InRange, GroupedKey2, GroupedKVsAcc2, GroupedRedsAcc2, KeyGroupFun, Fun, Acc2); [] -> {ok, Acc, GroupedReds ++ GroupedRedsAcc, GroupedKVsAcc, GroupedKey} diff --git a/apps/couch/test/etap/020-btree-basics.t b/apps/couch/test/etap/020-btree-basics.t index 996d240a..536c9fb1 100755 --- a/apps/couch/test/etap/020-btree-basics.t +++ b/apps/couch/test/etap/020-btree-basics.t @@ -22,7 +22,7 @@ rows() -> 250. main(_) -> test_util:init_code_path(), couch_config:start_link([]), - etap:plan(48), + etap:plan(51), case (catch test()) of ok -> etap:end_tests(); @@ -40,6 +40,7 @@ test()-> etap:ok(test_kvs(Sorted), "Testing sorted keys"), etap:ok(test_kvs(lists:reverse(Sorted)), "Testing reversed sorted keys"), etap:ok(test_kvs(shuffle(Sorted)), "Testing shuffled keys."), + etap:ok(test_fold_reductions(Sorted), "Testing fold_reduce."), ok. test_kvs(KeyValues) -> @@ -188,6 +189,33 @@ test_final_reductions(Btree, KeyValues) -> KVLen = FoldLRed + FoldRRed, ok. +test_fold_reductions(KeyValues) -> + + {ok, Fd} = couch_file:open(filename(), [create,overwrite]), + {ok, Btree} = couch_btree:open(nil, Fd), + {ok, Btree2} = couch_btree:add_remove(Btree, KeyValues, []), + + GroupFun = fun(Key1,Key2) -> Key1 == Key2 end, + + FoldLFun = fun(_X, _LeadingReds, Acc) -> + {ok, Acc + 1} end, + {ok, Res1} = + couch_btree:fold_reduce(Btree2, + FoldLFun,0, + [{key_group_fun, GroupFun}, + {start_key,126}, + {end_key,250}]), + etap:is(Res1,125,"including the end key ought to return half the keys"), + {ok, Res2} = + couch_btree:fold_reduce(Btree2, + FoldLFun,0, + [{key_group_fun, GroupFun}, + {start_key,126}, + {end_key_gt,250}]), + etap:is(Res2,124,"not including the end key ought to return one less than half the keys"), + true. + + shuffle(List) -> randomize(round(math:log(length(List)) + 0.5), List). |