Skip to content

Commit d189779

Browse files
authored
style: Clarify Node and NodeEntry vars in chash (#110)
Related to this commit, there are three important types in the chash module. -type chash_node() :: term(). -type chash() :: {num_partitions(), [node_entry()]}. -type node_entry() :: {index_as_int(), chash_node()}. Because there are three distinct types and concepts, it is helpful if the variable names in the the source code reflects them. For example, so that we call a chash_node() for Node and a node_entry() for a NodeEntry. I've updated the variable names to better reflect the type. And hopefully made the source code more readable.
1 parent 9c3632a commit d189779

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

src/chash.erl

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@
8989
CHash :: chash()) -> boolean().
9090

9191
contains_name(Name, CHash) ->
92-
{_NumPartitions, Nodes} = CHash,
93-
[X || {_, X} <- Nodes, X == Name] =/= [].
92+
{_NumPartitions, NodeEntries} = CHash,
93+
[Node || {_, Node} <- NodeEntries, Node == Name] =/= [].
9494

9595
%% @doc Create a brand new ring. The size and seednode are specified;
9696
%% initially all partitions are owned by the seednode. If NumPartitions
@@ -110,9 +110,9 @@ fresh(NumPartitions, SeedNode) ->
110110
CHash :: chash()) -> chash_node().
111111

112112
lookup(IndexAsInt, CHash) ->
113-
{_NumPartitions, Nodes} = CHash,
114-
{IndexAsInt, X} = proplists:lookup(IndexAsInt, Nodes),
115-
X.
113+
{_NumPartitions, NodeEntries} = CHash,
114+
{IndexAsInt, Node} = proplists:lookup(IndexAsInt, NodeEntries),
115+
Node.
116116

117117
sha(Bin) -> crypto:hash(sha, Bin).
118118

@@ -127,8 +127,8 @@ key_of(ObjectName) -> sha(term_to_binary(ObjectName)).
127127
-spec members(CHash :: chash()) -> [chash_node()].
128128

129129
members(CHash) ->
130-
{_NumPartitions, Nodes} = CHash,
131-
lists:usort([X || {_Idx, X} <- Nodes]).
130+
{_NumPartitions, NodeEntries} = CHash,
131+
lists:usort([Node || {_Idx, Node} <- NodeEntries]).
132132

133133
%% @doc Return a randomized merge of two rings.
134134
%% If multiple nodes are actively claiming nodes in the same
@@ -137,11 +137,11 @@ members(CHash) ->
137137
CHashB :: chash()) -> chash().
138138

139139
merge_rings(CHashA, CHashB) ->
140-
{NumPartitions, NodesA} = CHashA,
141-
{NumPartitions, NodesB} = CHashB,
140+
{NumPartitions, NodeEntriesA} = CHashA,
141+
{NumPartitions, NodeEntriesB} = CHashB,
142142
{NumPartitions,
143-
[{I, random_node(A, B)}
144-
|| {{I, A}, {I, B}} <- lists:zip(NodesA, NodesB)]}.
143+
[{I, random_node(NodeA, NodeB)}
144+
|| {{I, NodeA}, {I, NodeB}} <- lists:zip(NodeEntriesA, NodeEntriesB)]}.
145145

146146
%% @doc Given the integer representation of a chash key,
147147
%% return the next ring index integer value.
@@ -156,26 +156,26 @@ next_index(IntegerKey, {NumPartitions, _}) ->
156156
-spec nodes(CHash :: chash()) -> [node_entry()].
157157

158158
nodes(CHash) ->
159-
{_NumPartitions, Nodes} = CHash,
160-
Nodes.
159+
{_NumPartitions, NodeEntries} = CHash,
160+
NodeEntries.
161161

162162
%% @doc Given an object key, return all NodeEntries in order starting at Index.
163163
-spec ordered_from(Index :: index(),
164164
CHash :: chash()) -> [node_entry()].
165165

166-
ordered_from(Index, {NumPartitions, Nodes}) ->
166+
ordered_from(Index, {NumPartitions, NodeEntries}) ->
167167
<<IndexAsInt:160/integer>> = Index,
168168
Inc = ring_increment(NumPartitions),
169-
{A, B} = lists:split(IndexAsInt div Inc + 1, Nodes),
170-
B ++ A.
169+
{NodeEntriesA, NodeEntriesB} = lists:split(IndexAsInt div Inc + 1, NodeEntries),
170+
NodeEntriesB ++ NodeEntriesA.
171171

172172
%% @doc Given an object key, return all NodeEntries in reverse order
173173
%% starting at Index.
174174
-spec predecessors(Index :: index() | index_as_int(),
175175
CHash :: chash()) -> [node_entry()].
176176

177177
predecessors(Index, CHash) ->
178-
{NumPartitions, _Nodes} = CHash,
178+
{NumPartitions, _NodeEntries} = CHash,
179179
predecessors(Index, CHash, NumPartitions).
180180

181181
%% @doc Given an object key, return the next N NodeEntries in reverse order
@@ -203,15 +203,15 @@ ring_increment(NumPartitions) ->
203203
-spec size(CHash :: chash()) -> integer().
204204

205205
size(CHash) ->
206-
{_NumPartitions, Nodes} = CHash,
207-
length(Nodes).
206+
{_NumPartitions, NodeEntries} = CHash,
207+
length(NodeEntries).
208208

209209
%% @doc Given an object key, return all NodeEntries in order starting at Index.
210210
-spec successors(Index :: index(),
211211
CHash :: chash()) -> [node_entry()].
212212

213213
successors(Index, CHash) ->
214-
{NumPartitions, _Nodes} = CHash,
214+
{NumPartitions, _NodeEntries} = CHash,
215215
successors(Index, CHash, NumPartitions).
216216

217217
%% @doc Given an object key, return the next N NodeEntries in order
@@ -222,7 +222,7 @@ successors(Index, CHash) ->
222222
successors(Index, CHash, N) ->
223223
Num = max_n(N, CHash),
224224
Ordered = ordered_from(Index, CHash),
225-
{NumPartitions, _Nodes} = CHash,
225+
{NumPartitions, _NodeEntries} = CHash,
226226
if Num =:= NumPartitions -> Ordered;
227227
true ->
228228
{Res, _} = lists:split(Num, Ordered),
@@ -234,12 +234,12 @@ successors(Index, CHash, N) ->
234234
Name :: chash_node(), CHash :: chash()) -> chash().
235235

236236
update(IndexAsInt, Name, CHash) ->
237-
{NumPartitions, Nodes} = CHash,
238-
NewNodes = lists:keyreplace(IndexAsInt,
237+
{NumPartitions, NodeEntries} = CHash,
238+
NewNodeEntries = lists:keyreplace(IndexAsInt,
239239
1,
240-
Nodes,
240+
NodeEntries,
241241
{IndexAsInt, Name}),
242-
{NumPartitions, NewNodes}.
242+
{NumPartitions, NewNodeEntries}.
243243

244244
%% ====================================================================
245245
%% Internal functions
@@ -251,7 +251,7 @@ update(IndexAsInt, Name, CHash) ->
251251
-spec max_n(N :: integer(),
252252
CHash :: chash()) -> integer().
253253

254-
max_n(N, {NumPartitions, _Nodes}) ->
254+
max_n(N, {NumPartitions, _NodeEntries}) ->
255255
erlang:min(N, NumPartitions).
256256

257257
%% @private
@@ -277,8 +277,8 @@ update_test() ->
277277
NewNode = new@host,
278278
% Create a fresh ring...
279279
CHash = chash:fresh(5, Node),
280-
GetNthIndex = fun (N, {_, Nodes}) ->
281-
{Index, _} = lists:nth(N, Nodes),
280+
GetNthIndex = fun (N, {_, NodeEntries}) ->
281+
{Index, _} = lists:nth(N, NodeEntries),
282282
Index
283283
end,
284284

0 commit comments

Comments
 (0)