1#!/usr/bin/env escript
2%% -*- erlang -*-
3%%! -pa ./src/couchdb -sasl errlog_type error -noshell -smp enable
4
5% Licensed under the Apache License, Version 2.0 (the "License"); you may not
6% use this file except in compliance with the License. You may obtain a copy of
7% the License at
8%
9%   http://www.apache.org/licenses/LICENSE-2.0
10%
11% Unless required by applicable law or agreed to in writing, software
12% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14% License for the specific language governing permissions and limitations under
15% the License.
16
17filename() -> "./test/etap/temp.021".
18rows() -> 1000.
19
20main(_) ->
21    test_util:init_code_path(),
22    etap:plan(20),
23    case (catch test()) of
24        ok ->
25            etap:end_tests();
26        Other ->
27            etap:diag(io_lib:format("Test died abnormally: ~p", [Other])),
28            etap:bail()
29    end,
30    ok.
31
32test()->
33    couch_file_write_guard:sup_start_link(),
34    ReduceFun = fun
35        (reduce, KVs) -> length(KVs);
36        (rereduce, Reds) -> lists:sum(Reds)
37    end,
38
39    {ok, Fd} = couch_file:open(filename(), [create,overwrite]),
40    {ok, Btree} = couch_btree:open(nil, Fd, [{reduce, ReduceFun}]),
41
42    % Create a list, of {"even", Value} or {"odd", Value} pairs.
43    {_, EvenOddKVs} = lists:foldl(fun(Idx, {Key, Acc}) ->
44        case Key of
45            "even" -> {"odd", [{{Key, Idx}, 1} | Acc]};
46            _ -> {"even", [{{Key, Idx}, 1} | Acc]}
47        end
48    end, {"odd", []}, lists:seq(1, rows())),
49
50    {ok, Btree2} = couch_btree:add_remove(Btree, EvenOddKVs, []),
51    ok = couch_file:flush(Fd),
52
53    GroupFun = fun({K1, _}, {K2, _}) -> K1 == K2 end,
54    FoldFun = fun(GroupedKey, Unreduced, Acc) ->
55        {ok, [{GroupedKey, couch_btree:final_reduce(Btree2, Unreduced)} | Acc]}
56    end,
57
58    {SK1, EK1} = {{"even", -1}, {"even", foo}},
59    {SK2, EK2} = {{"odd", -1}, {"odd", foo}},
60
61    etap:fun_is(
62        fun
63            ({ok, [{{"odd", _}, 500}, {{"even", _}, 500}]}) ->
64                true;
65            (_) ->
66                false
67        end,
68        couch_btree:fold_reduce(Btree2, FoldFun, [], [{key_group_fun, GroupFun}]),
69        "Reduction works with no specified direction, startkey, or endkey."
70    ),
71
72    etap:fun_is(
73        fun
74            ({ok, [{{"odd", _}, 500}, {{"even", _}, 500}]}) ->
75                true;
76            (_) ->
77                false
78        end,
79        couch_btree:fold_reduce(Btree2, FoldFun, [], [{key_group_fun, GroupFun}, {dir, fwd}]),
80        "Reducing forward works with no startkey or endkey."
81    ),
82
83    etap:fun_is(
84        fun
85            ({ok, [{{"even", _}, 500}, {{"odd", _}, 500}]}) ->
86                true;
87            (_) ->
88                false
89        end,
90        couch_btree:fold_reduce(Btree2, FoldFun, [], [{key_group_fun, GroupFun}, {dir, rev}]),
91        "Reducing backwards works with no startkey or endkey."
92    ),
93
94    etap:fun_is(
95        fun
96            ({ok, [{{"odd", _}, 500}, {{"even", _}, 500}]}) ->
97                true;
98            (_) ->
99                false
100        end,
101        couch_btree:fold_reduce(Btree2, FoldFun, [], [{dir, fwd}, {key_group_fun, GroupFun}, {start_key, SK1}, {end_key, EK2}]),
102        "Reducing works over the entire range with startkey and endkey set."
103    ),
104
105    etap:fun_is(
106        fun
107            ({ok, [{{"even", _}, 500}]}) -> true;
108            (_) -> false
109        end,
110        couch_btree:fold_reduce(Btree2, FoldFun, [], [{dir, fwd}, {key_group_fun, GroupFun}, {start_key, SK1}, {end_key, EK1}]),
111        "Reducing forward over first half works with a startkey and endkey."
112    ),
113
114    etap:fun_is(
115        fun
116            ({ok, [{{"odd", _}, 500}]}) -> true;
117            (_) -> false
118        end,
119        couch_btree:fold_reduce(Btree2, FoldFun, [], [{dir, fwd}, {key_group_fun, GroupFun}, {start_key, SK2}, {end_key, EK2}]),
120        "Reducing forward over second half works with second startkey and endkey"
121    ),
122
123    etap:fun_is(
124        fun
125            ({ok, [{{"odd", _}, 500}]}) -> true;
126            (_) -> false
127        end,
128        couch_btree:fold_reduce(Btree2, FoldFun, [], [{dir, rev}, {key_group_fun, GroupFun}, {start_key, EK2}, {end_key, SK2}]),
129        "Reducing in reverse works after swapping the startkey and endkey."
130    ),
131
132    etap:fun_is(
133        fun
134            ({ok, [{{"even", _}, 500}, {{"odd", _}, 500}]}) ->
135                true;
136            (_) ->
137                false
138        end,
139        couch_btree:fold_reduce(Btree2, FoldFun, [], [{dir, rev}, {key_group_fun, GroupFun}, {start_key, EK2}, {end_key, SK1}]),
140        "Reducing in reverse results in reversed accumulator."
141    ),
142
143    etap:is(
144        couch_btree:fold_reduce(Btree2, FoldFun, [], [
145            {dir, fwd}, {key_group_fun, GroupFun},
146            {start_key, {"even", 0}}, {end_key, {"odd", rows() + 1}}
147        ]),
148        {ok, [{{"odd", 1}, 500}, {{"even", 2}, 500}]},
149        "Right fold reduce value for whole range with inclusive end key"),
150
151    etap:is(
152        couch_btree:fold_reduce(Btree2, FoldFun, [], [
153            {dir, fwd}, {key_group_fun, GroupFun},
154            {start_key, {"even", 0}}, {end_key_gt, {"odd", 999}}
155        ]),
156        {ok, [{{"odd", 1}, 499}, {{"even", 2}, 500}]},
157        "Right fold reduce value for whole range without inclusive end key"),
158
159    etap:is(
160        couch_btree:fold_reduce(Btree2, FoldFun, [], [
161            {dir, rev}, {key_group_fun, GroupFun},
162            {start_key, {"odd", 999}}, {end_key, {"even", 2}}
163        ]),
164        {ok, [{{"even", 1000}, 500}, {{"odd", 999}, 500}]},
165        "Right fold reduce value for whole reversed range with inclusive end key"),
166
167    etap:is(
168        couch_btree:fold_reduce(Btree2, FoldFun, [], [
169            {dir, rev}, {key_group_fun, GroupFun},
170            {start_key, {"odd", 999}}, {end_key_gt, {"even", 2}}
171        ]),
172        {ok, [{{"even", 1000}, 499}, {{"odd", 999}, 500}]},
173        "Right fold reduce value for whole reversed range without inclusive end key"),
174
175    etap:is(
176        couch_btree:fold_reduce(Btree2, FoldFun, [], [
177            {dir, fwd}, {key_group_fun, GroupFun},
178            {start_key, {"even", 0}}, {end_key, {"odd", 499}}
179        ]),
180        {ok, [{{"odd", 1}, 250}, {{"even", 2}, 500}]},
181        "Right fold reduce value for first half with inclusive end key"),
182
183    etap:is(
184        couch_btree:fold_reduce(Btree2, FoldFun, [], [
185            {dir, fwd}, {key_group_fun, GroupFun},
186            {start_key, {"even", 0}}, {end_key_gt, {"odd", 499}}
187        ]),
188        {ok, [{{"odd", 1}, 249}, {{"even", 2}, 500}]},
189        "Right fold reduce value for first half without inclusive end key"),
190
191    etap:is(
192        couch_btree:fold_reduce(Btree2, FoldFun, [], [
193            {dir, rev}, {key_group_fun, GroupFun},
194            {start_key, {"odd", 999}}, {end_key, {"even", 500}}
195        ]),
196        {ok, [{{"even", 1000}, 251}, {{"odd", 999}, 500}]},
197        "Right fold reduce value for first half reversed with inclusive end key"),
198
199    etap:is(
200        couch_btree:fold_reduce(Btree2, FoldFun, [], [
201            {dir, rev}, {key_group_fun, GroupFun},
202            {start_key, {"odd", 999}}, {end_key_gt, {"even", 500}}
203        ]),
204        {ok, [{{"even", 1000}, 250}, {{"odd", 999}, 500}]},
205        "Right fold reduce value for first half reversed without inclusive end key"),
206
207    etap:is(
208        couch_btree:fold_reduce(Btree2, FoldFun, [], [
209            {dir, fwd}, {key_group_fun, GroupFun},
210            {start_key, {"even", 500}}, {end_key, {"odd", 999}}
211        ]),
212        {ok, [{{"odd", 1}, 500}, {{"even", 500}, 251}]},
213        "Right fold reduce value for second half with inclusive end key"),
214
215    etap:is(
216        couch_btree:fold_reduce(Btree2, FoldFun, [], [
217            {dir, fwd}, {key_group_fun, GroupFun},
218            {start_key, {"even", 500}}, {end_key_gt, {"odd", 999}}
219        ]),
220        {ok, [{{"odd", 1}, 499}, {{"even", 500}, 251}]},
221        "Right fold reduce value for second half without inclusive end key"),
222
223    etap:is(
224        couch_btree:fold_reduce(Btree2, FoldFun, [], [
225            {dir, rev}, {key_group_fun, GroupFun},
226            {start_key, {"odd", 501}}, {end_key, {"even", 2}}
227        ]),
228        {ok, [{{"even", 1000}, 500}, {{"odd", 501}, 251}]},
229        "Right fold reduce value for second half reversed with inclusive end key"),
230
231    etap:is(
232        couch_btree:fold_reduce(Btree2, FoldFun, [], [
233            {dir, rev}, {key_group_fun, GroupFun},
234            {start_key, {"odd", 501}}, {end_key_gt, {"even", 2}}
235        ]),
236        {ok, [{{"even", 1000}, 499}, {{"odd", 501}, 251}]},
237        "Right fold reduce value for second half reversed without inclusive end key"),
238
239    couch_file:close(Fd).
240