Skip to content

Commit 07368f3

Browse files
Merge remote-tracking branch 'couchbase/neo'
- MB-50712: Simplify make_vbmap_with_node_ids function Change-Id: I449dcf7160b106a92c4cb51d071ab5ac0566168c
2 parents 843cf73 + 8b75631 commit 07368f3

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

src/mb_map.erl

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ do_invoke_vbmap_body(VbmapPath, DiagPath, CurrentMap, Nodes,
749749

750750
NodeIdMap = dict:from_list(NodeIdList),
751751

752-
IdVbMap = make_vbmap_with_node_ids(MaxNodeId, NodeIdMap, CurrentMap),
752+
IdVbMap = make_vbmap_with_node_ids(NodeIdMap, CurrentMap),
753753

754754
PrevMapFile = path_config:tempfile("prev-vbmap", ".json"),
755755

@@ -842,29 +842,13 @@ do_invoke_vbmap_body(VbmapPath, DiagPath, CurrentMap, Nodes,
842842
exit({vbmap_error, iolist_to_binary(Output)})
843843
end.
844844

845-
map_chain(Chain, MaxNodeId, NodeIdMap) ->
846-
{ReversedChain, MaxNodeIdx1, NodeIdxMap1} =
847-
lists:foldl(
848-
fun(N, {ChainPart, MaxNId, NIdMap}) ->
849-
case dict:find(N, NIdMap) of
850-
{ok, Idx} ->
851-
{[Idx | ChainPart], MaxNId, NIdMap};
852-
error ->
853-
{[MaxNodeId + 1 | ChainPart], MaxNodeId + 1,
854-
dict:store(N, MaxNId + 1, NIdMap)}
855-
end
856-
end, {[], MaxNodeId, NodeIdMap}, Chain),
857-
{lists:reverse(ReversedChain), MaxNodeIdx1, NodeIdxMap1}.
858-
859-
make_vbmap_with_node_ids(MaxNodeId, NodeIdMap, CurrentMap) ->
860-
NodeIdMapWithUndefined = dict:store(undefined, -1, NodeIdMap),
861-
{ReversedChains, _, _} =
862-
lists:foldl(
863-
fun (Chain, {NewChains, MaxNId, NIdMap}) ->
864-
{Chain1, MaxNId1, NIdMap1} = map_chain(Chain, MaxNId, NIdMap),
865-
{[Chain1 | NewChains], MaxNId1, NIdMap1}
866-
end, {[], MaxNodeId, NodeIdMapWithUndefined}, CurrentMap),
867-
lists:reverse(ReversedChains).
845+
make_vbmap_with_node_ids(NodeIdMap, CurrentMap) ->
846+
[[case dict:find(N, NodeIdMap) of
847+
{ok, Idx} ->
848+
Idx;
849+
error ->
850+
-1
851+
end || N <- Chain] || Chain <- CurrentMap].
868852

869853
map_tags(NodeIxMap, RawTags) ->
870854
{_, TagIxMap} =
@@ -1367,4 +1351,22 @@ enumerate_chains_test() ->
13671351
EnumeratedChains2 = enumerate_chains(Map, undefined),
13681352
[{0, [a, b, c], []}, {1, [b, c, a], []}] = EnumeratedChains2.
13691353

1354+
make_vbmap_with_node_ids_test() ->
1355+
NodeIdMap = dict:from_list([{a, 0}, {b, 1}, {c, 2}]),
1356+
1357+
Map = [[a, b, c], [b, c, a]],
1358+
[[0, 1, 2], [1, 2, 0]] = make_vbmap_with_node_ids(NodeIdMap, Map),
1359+
1360+
Map1 = [[a, b, c], [b, c, undefined]],
1361+
[[0, 1, 2], [1, 2, -1]] = make_vbmap_with_node_ids(NodeIdMap, Map1),
1362+
1363+
Map2 = [[undefined, b, c], [b, c, undefined]],
1364+
[[-1, 1, 2], [1, 2, -1]] = make_vbmap_with_node_ids(NodeIdMap, Map2),
1365+
1366+
Map3 = [[a, b, d], [b, c, e]],
1367+
[[0, 1, -1], [1, 2, -1]] = make_vbmap_with_node_ids(NodeIdMap, Map3),
1368+
1369+
Map4 = [[undefined, b, d], [b, undefined, e]],
1370+
[[-1, 1, -1], [1, -1, -1]] = make_vbmap_with_node_ids(NodeIdMap, Map4).
1371+
13701372
-endif.

0 commit comments

Comments
 (0)