Skip to content

Instantly share code, notes, and snippets.

@bbrowning
Created July 12, 2009 19:10
Show Gist options
  • Save bbrowning/145769 to your computer and use it in GitHub Desktop.
Save bbrowning/145769 to your computer and use it in GitHub Desktop.
%%% File : chash_example.erl
%%% Author : Benjamin Browning <ben324@gmail.com>
%%% Description : Simple example demonstrating usage of chash
%%% Created : 12 Jul 2009 by Benjamin Browning <ben324@gmail.com>
-module(chash_example).
-export([test/0]).
test() ->
crypto:start(),
generate_chash(5, [node1, node2, node3]).
generate_chash(NumPartitions, Nodes) ->
[FirstNode | _Rest] = Nodes,
Chash = chash:fresh(NumPartitions, FirstNode),
update_chash(Nodes, Chash).
update_chash([], Chash) ->
Chash;
update_chash([Node | Rest], Chash) ->
NodeIndex = chash:key_of(Node),
[{IndexAsInt, _PreviousNode}] = chash:successors(NodeIndex, Chash, 1),
update_chash(Rest, chash:update(IndexAsInt, Node, Chash)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment