Skip to content

Commit f012e63

Browse files
committed
Use new sets everywhere
With OTP 28 sets switchd to using version 2 by default. That caused various tests to fail, as Jan discovered in #5507 In the previous PR #5510 we switched few mango instances to use a helper utility, but it was repeated a few times and Gábor made a good suggestion to use utility functions instead, so that's what this PR does. But it does it for the whole code base. Once we're post OTP 28 we can remove the utility and just use plain set functions.
1 parent 12b4ab0 commit f012e63

18 files changed

+95
-92
lines changed

src/couch/src/couch_multidb_changes.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ handle_info(_Msg, State) ->
200200

201201
shards_db_fold(#row{dbname = <<"shards/", _/binary>> = DbName} = Row, {Add, Remove}) ->
202202
Fun = fun(Dbs) -> sets:add_element(DbName, Dbs) end,
203-
Init = sets:from_list([DbName], [{version, 2}]),
203+
Init = couch_util:set_from_list([DbName]),
204204
case Row#row.wait_shard_map of
205205
true ->
206206
{maps:update_with(mem3:dbname(DbName), Fun, Init, Add), Remove};
@@ -354,10 +354,10 @@ is_deleted(Change) ->
354354
local_shards(Db0) ->
355355
Db = mem3:dbname(Db0),
356356
try
357-
sets:from_list([S || #shard{name = S} <- mem3:local_shards(Db)], [{version, 2}])
357+
couch_util:set_from_list([S || #shard{name = S} <- mem3:local_shards(Db)])
358358
catch
359359
error:database_does_not_exist ->
360-
sets:new([{version, 2}])
360+
couch_util:new_set()
361361
end.
362362

363363
notify_fold(DbName, {Server, DbSuffix, Count}) ->

src/couch/src/couch_util.erl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
-export([get_config_hash_algorithms/0]).
4848
-export([remove_sensitive_data/1]).
4949
-export([ejson_to_map/1]).
50+
-export([new_set/0, set_from_list/1]).
5051

5152
-include_lib("couch/include/couch_db.hrl").
5253

@@ -862,3 +863,16 @@ ejson_to_map({Val}) when is_list(Val) ->
862863
maps:from_list(lists:map(fun({K, V}) -> {K, ejson_to_map(V)} end, Val));
863864
ejson_to_map(Val) ->
864865
Val.
866+
867+
% Set types were switched to version 2 by default in OTP 28, use this helper to
868+
% create version 2 sets in OTP versions < 28 as well to avoid having to deal
869+
% with a different ordered to_list result in tests and just ensure we always
870+
% use version 2, which is also more performant
871+
%
872+
% Remove after OTP >= 28 and switch to plain sets:new/0 and sets:from_list/1
873+
874+
new_set() ->
875+
sets:new([{version, 2}]).
876+
877+
set_from_list(KVs) ->
878+
sets:from_list(KVs, [{version, 2}]).

src/couch_prometheus/test/eunit/couch_prometheus_e2e_tests.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ t_no_duplicate_metrics(Port) ->
119119
?assertEqual(erlang:length(Diff), 0).
120120

121121
get_duplicates(List) ->
122-
List -- sets:to_list(sets:from_list(List)).
122+
List -- sets:to_list(couch_util:set_from_list(List)).
123123

124124
t_metric_updated(Port) ->
125125
% The passage of time should increment this metric

src/couch_replicator/src/couch_replicator_httpd_util.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ parse_replication_state_filter(States) when is_list(States) ->
4949
Msg1 = io_lib:format("States must be one or more of ~w", [AllStates]),
5050
throw({query_parse_error, ?l2b(Msg1)})
5151
end,
52-
AllSet = sets:from_list(AllStates),
53-
StatesSet = sets:from_list(AtomStates),
52+
AllSet = couch_util:set_from_list(AllStates),
53+
StatesSet = couch_util:set_from_list(AtomStates),
5454
Diff = sets:to_list(sets:subtract(StatesSet, AllSet)),
5555
case Diff of
5656
[] ->

src/fabric/src/fabric_db_purged_infos.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ go(DbName) ->
2929
Fun = fun handle_message/3,
3030
Acc0 = #pacc{
3131
counters = fabric_dict:init(Workers, nil),
32-
replies = sets:new([{version, 2}]),
32+
replies = couch_util:new_set(),
3333
ring_opts = [{any, Shards}]
3434
},
3535
try
@@ -66,7 +66,7 @@ handle_message({rexi_EXIT, Reason}, Shard, #pacc{} = Acc) ->
6666
end;
6767
handle_message({ok, Info}, #shard{} = Shard, #pacc{} = Acc) ->
6868
#pacc{counters = Counters, replies = Replies} = Acc,
69-
InfoSet = sets:from_list(Info, [{version, 2}]),
69+
InfoSet = couch_util:set_from_list(Info),
7070
Replies1 = sets:union(InfoSet, Replies),
7171
Counters1 = fabric_dict:erase(Shard, Counters),
7272
case fabric_dict:size(Counters1) =:= 0 of
@@ -92,7 +92,7 @@ make_shards() ->
9292
init_acc(Shards) ->
9393
#pacc{
9494
counters = fabric_dict:init(Shards, nil),
95-
replies = sets:new([{version, 2}]),
95+
replies = couch_util:new_set(),
9696
ring_opts = [{any, Shards}]
9797
}.
9898

src/fabric/src/fabric_group_info.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ go(DbName, #doc{id = DDocId}) ->
2626
Ushards = mem3:ushards(DbName),
2727
Workers = fabric_util:submit_jobs(Shards, group_info, [DDocId]),
2828
RexiMon = fabric_util:create_monitors(Shards),
29-
USet = sets:from_list([{Id, N} || #shard{name = Id, node = N} <- Ushards]),
29+
USet = couch_util:set_from_list([{Id, N} || #shard{name = Id, node = N} <- Ushards]),
3030
Acc = {fabric_dict:init(Workers, nil), [], USet},
3131
try fabric_util:recv(Workers, #shard.ref, fun handle_message/3, Acc) of
3232
{timeout, {WorkersDict, _, _}} ->

src/fabric/src/fabric_streams.erl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ spawn_worker_cleaner(Coordinator, Workers, ClientReq) when
199199
undefined ->
200200
Pid = spawn(fun() ->
201201
erlang:monitor(process, Coordinator),
202-
NodeRefSet = set_from_list(shards_to_node_refs(Workers)),
202+
NodeRefSet = couch_util:set_from_list(shards_to_node_refs(Workers)),
203203
cleaner_loop(Coordinator, NodeRefSet, ClientReq)
204204
end),
205205
put(?WORKER_CLEANER, Pid),
@@ -228,9 +228,6 @@ add_worker_to_cleaner(CoordinatorPid, #shard{node = Node, ref = Ref}) ->
228228
ok
229229
end.
230230

231-
set_from_list(List) when is_list(List) ->
232-
sets:from_list(List, [{version, 2}]).
233-
234231
shards_to_node_refs(Workers) when is_list(Workers) ->
235232
Fun = fun(#shard{node = Node, ref = Ref}) -> {Node, Ref} end,
236233
lists:map(Fun, Workers).

src/global_changes/src/global_changes_listener.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ init(_) ->
5555
State = #state{
5656
update_db = UpdateDb,
5757
pending_update_count = 0,
58-
pending_updates = sets:new([{version, 2}]),
58+
pending_updates = couch_util:new_set(),
5959
max_event_delay = MaxEventDelay,
6060
dbname = global_changes_util:get_dbname()
6161
},
@@ -99,7 +99,7 @@ handle_cast({set_update_db, Boolean}, State0) ->
9999
{false, true} ->
100100
State0#state{
101101
update_db = Boolean,
102-
pending_updates = sets:new([{version, 2}]),
102+
pending_updates = couch_util:new_set(),
103103
pending_update_count = 0,
104104
last_update_time = undefined
105105
};
@@ -141,7 +141,7 @@ maybe_send_updates(#state{update_db = true} = State) ->
141141
0
142142
),
143143
State1 = State#state{
144-
pending_updates = sets:new([{version, 2}]),
144+
pending_updates = couch_util:new_set(),
145145
pending_update_count = 0,
146146
last_update_time = undefined
147147
},

src/global_changes/src/global_changes_server.erl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ init([]) ->
6666
State = #state{
6767
update_db = UpdateDb,
6868
pending_update_count = 0,
69-
pending_updates = sets:new([{version, 2}]),
69+
pending_updates = couch_util:new_set(),
7070
max_write_delay = MaxWriteDelay,
7171
dbname = GlobalChangesDbName,
7272
handler_ref = erlang:monitor(process, Handler)
@@ -79,7 +79,7 @@ handle_call(_Msg, _From, State) ->
7979
handle_cast(_Msg, #state{update_db = false} = State) ->
8080
{noreply, State};
8181
handle_cast({update_docs, DocIds}, State) ->
82-
Pending = sets:union(sets:from_list(DocIds), State#state.pending_updates),
82+
Pending = sets:union(couch_util:set_from_list(DocIds), State#state.pending_updates),
8383
PendingCount = sets:size(Pending),
8484
couch_stats:update_gauge(
8585
[global_changes, server_pending_updates],
@@ -100,7 +100,7 @@ handle_cast({set_update_db, Boolean}, State0) ->
100100
{false, true} ->
101101
State0#state{
102102
update_db = Boolean,
103-
pending_updates = sets:new([{version, 2}]),
103+
pending_updates = couch_util:new_set(),
104104
pending_update_count = 0
105105
};
106106
_ ->
@@ -168,7 +168,7 @@ flush_updates(State) ->
168168
0
169169
),
170170
{noreply, State#state{
171-
pending_updates = sets:new([{version, 2}]),
171+
pending_updates = couch_util:new_set(),
172172
pending_update_count = 0
173173
}}.
174174

src/mango/src/mango_cursor.erl

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,9 @@ extract_selector_hints(Selector) ->
287287
Modules = lists:flatmap(AsIndex, ?CURSOR_MODULES),
288288
Populate =
289289
fun({Module, IndexType}) ->
290-
AllFields = set_from_list(mango_selector:fields(Selector)),
290+
AllFields = couch_util:set_from_list(mango_selector:fields(Selector)),
291291
Normalize = fun(N) -> hd(string:split(N, ":")) end,
292-
IndexableFields = set_from_list(
292+
IndexableFields = couch_util:set_from_list(
293293
lists:map(Normalize, Module:indexable_fields(Selector))
294294
),
295295
UnindexableFields = sets:subtract(AllFields, IndexableFields),
@@ -442,7 +442,8 @@ maybe_add_warning(UserFun, #cursor{index = Index, opts = Opts}, Stats, UserAcc)
442442
% create a dummy cursor in absence of usable indexes, utilized by _explain
443443
create_cursor(Db, {[], Trace0}, Selector, Opts) ->
444444
Blank = #{
445-
filtered_indexes => sets:new([{version, 2}]), indexes_of_type => sets:new([{version, 2}])
445+
filtered_indexes => couch_util:new_set(),
446+
indexes_of_type => couch_util:new_set()
446447
},
447448
Trace = maps:merge(Trace0, Blank),
448449
Limit = couch_util:get_value(limit, Opts, mango_opts:default_limit()),
@@ -463,9 +464,9 @@ create_cursor(Db, {[], Trace0}, Selector, Opts) ->
463464
execution_stats = Stats
464465
}};
465466
create_cursor(Db, {Indexes, Trace0}, Selector, Opts) ->
466-
Trace1 = maps:merge(Trace0, #{filtered_indexes => set_from_list(Indexes)}),
467+
Trace1 = maps:merge(Trace0, #{filtered_indexes => couch_util:set_from_list(Indexes)}),
467468
[{CursorMod, CursorModIndexes} | _] = group_indexes_by_type(Indexes),
468-
Trace = maps:merge(Trace1, #{indexes_of_type => set_from_list(CursorModIndexes)}),
469+
Trace = maps:merge(Trace1, #{indexes_of_type => couch_util:set_from_list(CursorModIndexes)}),
469470
CursorMod:create(Db, {CursorModIndexes, Trace}, Selector, Opts).
470471

471472
group_indexes_by_type(Indexes) ->
@@ -585,9 +586,6 @@ ddoc_name(<<"_design/", Name/binary>>) ->
585586
ddoc_name(Name) ->
586587
Name.
587588

588-
set_from_list(KVs) ->
589-
sets:from_list(KVs, [{version, 2}]).
590-
591589
-ifdef(TEST).
592590
-include_lib("couch/include/couch_eunit.hrl").
593591

@@ -618,8 +616,8 @@ t_create_regular(_) ->
618616
Trace1 = #{},
619617
Trace2 =
620618
#{
621-
filtered_indexes => set_from_list(FilteredIndexes),
622-
indexes_of_type => set_from_list(IndexesOfType)
619+
filtered_indexes => couch_util:set_from_list(FilteredIndexes),
620+
indexes_of_type => couch_util:set_from_list(IndexesOfType)
623621
},
624622
Options = [{use_index, []}, {allow_fallback, true}],
625623
meck:expect(mango_selector, normalize, [selector], meck:val(normalized_selector)),
@@ -648,8 +646,8 @@ t_create_user_specified_index(_) ->
648646
Trace1 = #{},
649647
Trace2 =
650648
#{
651-
filtered_indexes => set_from_list(FilteredIndexes),
652-
indexes_of_type => set_from_list(IndexesOfType)
649+
filtered_indexes => couch_util:set_from_list(FilteredIndexes),
650+
indexes_of_type => couch_util:set_from_list(IndexesOfType)
653651
},
654652
Options = [{use_index, [<<"_design/view_idx2">>]}],
655653
meck:expect(mango_selector, normalize, [selector], meck:val(normalized_selector)),
@@ -677,8 +675,8 @@ t_create_invalid_user_specified_index(_) ->
677675
Trace1 = #{},
678676
Trace2 =
679677
#{
680-
filtered_indexes => set_from_list(UsableIndexes),
681-
indexes_of_type => set_from_list(IndexesOfType)
678+
filtered_indexes => couch_util:set_from_list(UsableIndexes),
679+
indexes_of_type => couch_util:set_from_list(IndexesOfType)
682680
},
683681
Options = [{use_index, [<<"foobar">>]}, {allow_fallback, true}],
684682
meck:expect(mango_selector, normalize, [selector], meck:val(normalized_selector)),
@@ -706,8 +704,8 @@ t_create_invalid_user_specified_index_no_fallback(_) ->
706704
Trace1 = #{},
707705
Trace2 =
708706
#{
709-
filtered_indexes => set_from_list(UsableIndexes),
710-
indexes_of_type => set_from_list(IndexesOfType)
707+
filtered_indexes => couch_util:set_from_list(UsableIndexes),
708+
indexes_of_type => couch_util:set_from_list(IndexesOfType)
711709
},
712710
UseIndex = [<<"design">>, <<"foobar">>],
713711
Options = [{use_index, UseIndex}, {allow_fallback, false}],
@@ -734,8 +732,8 @@ t_create_no_suitable_index_no_fallback(_) ->
734732
Trace1 = #{},
735733
Trace2 =
736734
#{
737-
filtered_indexes => set_from_list(UsableIndexes),
738-
indexes_of_type => set_from_list(IndexesOfType)
735+
filtered_indexes => couch_util:set_from_list(UsableIndexes),
736+
indexes_of_type => couch_util:set_from_list(IndexesOfType)
739737
},
740738
Options = [{use_index, []}, {allow_fallback, false}],
741739
meck:expect(mango_selector, normalize, [selector], meck:val(normalized_selector)),
@@ -782,7 +780,7 @@ extract_candidate_indexes_test_() ->
782780
}.
783781

784782
t_extract_candidate_indexes_empty(_) ->
785-
Indexes = sets:new([{version, 2}]),
783+
Indexes = couch_util:new_set(),
786784
UsabilityMap = [],
787785
Trace =
788786
#{
@@ -804,7 +802,7 @@ t_extract_candidate_indexes_empty(_) ->
804802
?assertEqual(Candidates, extract_candidate_indexes(Cursor)).
805803

806804
t_extract_candidate_indexes_singleton(_) ->
807-
Indexes = set_from_list([winner]),
805+
Indexes = couch_util:set_from_list([winner]),
808806
UsabilityMap = [{winner, {true, #{reason => []}}}],
809807
Trace =
810808
#{
@@ -840,15 +838,17 @@ t_extract_candidate_indexes_user_specified(_) ->
840838
],
841839
Trace =
842840
#{
843-
all_indexes => set_from_list([
841+
all_indexes => couch_util:set_from_list([
844842
winner, Partial, Partitioned, NotUsable, Filtered, Unfavored
845843
]),
846-
global_indexes => set_from_list([winner, Partitioned, NotUsable, Filtered, Unfavored]),
847-
partition_indexes => set_from_list([winner, NotUsable, Filtered, Unfavored]),
848-
usable_indexes => set_from_list([winner, Filtered, Unfavored]),
844+
global_indexes => couch_util:set_from_list([
845+
winner, Partitioned, NotUsable, Filtered, Unfavored
846+
]),
847+
partition_indexes => couch_util:set_from_list([winner, NotUsable, Filtered, Unfavored]),
848+
usable_indexes => couch_util:set_from_list([winner, Filtered, Unfavored]),
849849
usability_map => UsabilityMap,
850-
filtered_indexes => set_from_list([winner, Unfavored]),
851-
indexes_of_type => set_from_list([winner])
850+
filtered_indexes => couch_util:set_from_list([winner, Unfavored]),
851+
indexes_of_type => couch_util:set_from_list([winner])
852852
},
853853
Cursor =
854854
#cursor{
@@ -940,7 +940,7 @@ t_extract_candidate_indexes_regular(_) ->
940940
],
941941
Trace =
942942
#{
943-
all_indexes => set_from_list([
943+
all_indexes => couch_util:set_from_list([
944944
winner,
945945
Partial1,
946946
Partial2,
@@ -953,7 +953,7 @@ t_extract_candidate_indexes_regular(_) ->
953953
Usable2,
954954
Usable3
955955
]),
956-
global_indexes => set_from_list([
956+
global_indexes => couch_util:set_from_list([
957957
winner,
958958
Partitioned1,
959959
Partitioned2,
@@ -964,17 +964,17 @@ t_extract_candidate_indexes_regular(_) ->
964964
Usable2,
965965
Usable3
966966
]),
967-
partition_indexes => set_from_list([
967+
partition_indexes => couch_util:set_from_list([
968968
winner, NotUsable, Unfavored1, Unfavored2, Usable1, Usable2, Usable3
969969
]),
970-
usable_indexes => set_from_list([
970+
usable_indexes => couch_util:set_from_list([
971971
winner, Unfavored1, Unfavored2, Usable1, Usable2, Usable3
972972
]),
973973
usability_map => UsabilityMap,
974-
filtered_indexes => set_from_list([
974+
filtered_indexes => couch_util:set_from_list([
975975
winner, Unfavored1, Unfavored2, Usable1, Usable2, Usable3
976976
]),
977-
indexes_of_type => set_from_list([winner, Usable1, Usable2, Usable3]),
977+
indexes_of_type => couch_util:set_from_list([winner, Usable1, Usable2, Usable3]),
978978
sorted_index_ranges => SortedIndexRanges
979979
},
980980
Cursor =
@@ -1193,7 +1193,7 @@ explain_test_() ->
11931193

11941194
t_explain_empty(_) ->
11951195
Selector = {[]},
1196-
Indexes = sets:new([{version, 2}]),
1196+
Indexes = couch_util:new_set(),
11971197
Trace =
11981198
#{
11991199
all_indexes => Indexes,
@@ -1241,7 +1241,7 @@ t_explain_regular(_) ->
12411241
type = <<"special">>, name = index, def = all_docs, dbname = db, partitioned = partitioned
12421242
},
12431243
Selector = {[]},
1244-
Indexes = set_from_list([Index]),
1244+
Indexes = couch_util:set_from_list([Index]),
12451245
Fields = some_fields,
12461246
Trace =
12471247
#{

src/mango/src/mango_cursor_nouveau.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ execute(Cursor, UserFun, UserAcc) ->
108108
user_acc = UserAcc,
109109
fields = Cursor#cursor.fields,
110110
execution_stats = mango_execution_stats:log_start(Stats),
111-
documents_seen = sets:new([{version, 2}])
111+
documents_seen = couch_util:new_set()
112112
},
113113
try
114114
case Query of

0 commit comments

Comments
 (0)