cabol / shards

Partitioned ETS tables for Erlang and Elixir

Home Page:https://hexdocs.pm/shards

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Modify `shards_local` to avoid additional table types, handle a flag `sharded` instead.

cabol opened this issue · comments

Currently, shards_local has two additional table types: sharded_bag and sharded_duplicated_bag. The idea is remove these types, keeps the current ones, and handle a flag sharded instead – to know when the key should be modified to be sharded or not.

PR #11 related with issue #9 fix this – sharded_bag and sharded_duplicate_bag were removed.

To achieve something like sharded_bag/sharded_duplicate_bag, we only have to override the pick_shard_fun() and pick_node_fun() like this:

pick_shard(write, Key, N) ->
  erlang:phash2({Key, os:timestamp()}, N);
pick_shard(_, _, _) ->
  any.

pick_node(write, Key, Nodes) ->
  NewKey = {Key, os:timestamp()},
  Nth = jumping_hash:compute(erlang:phash2(NewKey), length(Nodes)) + 1,
  lists:nth(Nth, Nodes);
pick_node(_, _, _) ->
  any.

Also, take a look at the tests:

  • Create a table overriding the pick funs HERE.
  • Pick funs HERE.