@@ -9,6 +9,12 @@ defmodule HRW.Skeleton do
99
1010 defstruct [ :clusters , :fanout , :levels ]
1111
12+ @ type t :: % __MODULE__ {
13+ clusters: tuple ( ) ,
14+ fanout: pos_integer ( ) ,
15+ levels: non_neg_integer ( )
16+ }
17+
1218 @ doc """
1319 Builds a skeleton from `nodes`.
1420
@@ -23,7 +29,14 @@ defmodule HRW.Skeleton do
2329 #HRW.Skeleton<3 nodes, fanout: 3>
2430
2531 """
26- def build ( nodes , opts \\ [ ] ) do
32+ @ spec build ( [ term ( ) ] , keyword ( ) ) :: t ( )
33+ def build ( nodes , opts \\ [ ] )
34+
35+ def build ( [ ] , _opts ) do
36+ raise ArgumentError , "HRW.Skeleton.build/2 requires a non-empty list of nodes"
37+ end
38+
39+ def build ( nodes , opts ) do
2740 fanout = Keyword . get ( opts , :fanout , 3 )
2841 size = Keyword . get ( opts , :cluster_size , 16 )
2942
@@ -53,13 +66,12 @@ defmodule HRW.Skeleton do
5366 "server3"
5467
5568 """
69+ @ spec owner ( term ( ) , t ( ) , keyword ( ) ) :: term ( )
5670 def owner ( key , % __MODULE__ { } = skeleton , opts \\ [ ] ) do
5771 hash_fn = Keyword . get ( opts , :hash_fn , & :erlang . phash2 / 1 )
5872 do_owner ( key , skeleton , 0 , hash_fn )
5973 end
6074
61- defp do_owner ( _key , % __MODULE__ { clusters: { } } , _salt , _hash_fn ) , do: nil
62-
6375 defp do_owner ( key , % __MODULE__ { clusters: { cluster } } , _salt , hash_fn ) do
6476 Enum . max_by ( cluster , fn node -> hash_fn . ( { key , node } ) end )
6577 end
0 commit comments