ex_shards v0.2.1 ExShards.Ext behaviour
This module extends the Shards API providing some extra functions.
To use the extended functions you have to do it through any of the modules
in which these functions are injected: ExShards.Local
or ExShards.Dist
.
Besides, you can use ExShards
as well, since it is a wrapper on top
of ExShards.Local
and ExShards.Dist
modules.
Examples
# using ExShards
iex> ExShards.new :mytab
:mytab
iex> ExShards.set :mytab, a: 1, b: 2
:mytab
iex> ExShards.get :mytab, :a
1
iex> ExShards.fetch :mytab, :c
:error
# using ExShards.Local
iex> ExShards.Local.new :mytab
:mytab
iex> ExShards.Local.set :mytab, a: 1, b: 2
:mytab
iex> ExShards.Local.get :mytab, :a
1
iex> ExShards.Local.fetch :mytab, :c
:error
# using ExShards.Dist
iex> ExShards.Dist.new :mytab
:mytab
iex> ExShards.Dist.set :mytab, a: 1, b: 2
:mytab
iex> ExShards.Dist.get :mytab, :a
1
iex> ExShards.Dist.fetch :mytab, :c
:error
Link to this section Summary
Callbacks
Drops the given keys
from tab
Fetches the value for a specific key
in the given tab
Fetches the value for a specific key
in the given tab
, erroring out if
tab
doesn’t contain key
Gets the value or a list of values for a specific key
in tab
Gets the value from key
and updates it, all in one pass
Gets the value from key
and updates it. Raises if there is no key
Returns whether the given key
exists in the given tab
Returns all keys from tab
Returns and removes the value(s) associated with key
in tab
Puts the given value
under key
in tab
Puts the given value
under key
unless the entry key
already exists in tab
Removes the entry in tab
for a specific key
This function is analogous to :shards.insert/2,3
, but it returns
the table name instead of true
Drops the given keys
from tab
and returns a map with all dropped
key-value pairs
Updates the key
in tab
with the given function
Updates key
with the given function
This function is analogous to :shards.update_element/3,4
, but it returns
the table name instead of true
Returns all values from tab
Link to this section Types
Link to this section Callbacks
Drops the given keys
from tab
.
If keys
contains keys that are not in tab
, they’re simply ignored.
Examples
iex> ExShards.set(:mytab, a: 1, b: 2, a: 3, c: 4)
iex> ExShards.drop(:mytab, [:a, :b, :e])
:mytab
iex> ExShards.keys(:mytab)
[:c]
Fetches the value for a specific key
in the given tab
.
If tab
contains the given key
with value value
, then {:ok, value}
is returned. If tab
doesn’t contain key
, :error
is returned.
Keep in mind that only one result is returned always, in case of :bag
or :duplicate bag
, only the first match is returned.
Examples
iex> ExShards.fetch(:mytab, :a)
{:ok, 1}
iex> ExShards.fetch(:mytab, :b)
:error
Fetches the value for a specific key
in the given tab
, erroring out if
tab
doesn’t contain key
.
If tab
contains the given key
, the corresponding value is returned. If
tab
doesn’t contain key
, a KeyError
exception is raised.
Keep in mind that only one result is returned always, in case of :bag
or :duplicate bag
, only the first match is returned.
Examples
iex> ExShards.fetch!(:mytab, :a)
1
iex> ExShards.fetch!(:mytab, :b)
** (KeyError) key :b not found in: :mytab
Gets the value or a list of values for a specific key
in tab
.
Return possibilities:
value
– Ifkey
is present intab
with valuevalue
(only one element matches).[value]
– In case of multiple elements matches with the keykey
. This behaviour is expected when the table is either a:bag
or a:duplicate_bag
.default
– Ifkey
is not present intab
(default
isnil
unless specified otherwise).
Examples
iex> ExShards.get(:mytab, :a)
nil
iex> :mytab |> ExShards.put(:a, 1) |> ExShards.get(:a)
1
iex> ExShards.get(:mytab, :b)
nil
iex> ExShards.get(:mytab, :b, 3)
3
iex> :mytab |> ExShards.set(c: 2, c: 3) |> ExShards.get(:c)
[2, 3]
Gets the value from key
and updates it, all in one pass.
fun
is called with the current value under key
in tab
(or nil
if key
is not present in tab
) and must return a two-element tuple:
the “get” value (the retrieved value, which can be operated on before
being returned) and the new value to be stored under key
. fun
may
also return :pop
, which means the current value shall be removed
from tab
and returned (making this function behave like
pop(tab, key)
.
The returned value is a tuple with the “get” value returned by
fun
and the new updated value under key
.
Examples
iex> :mytab |> ExShards.put(:a, 1) |> ExShards.get(:a)
1
iex> ExShards.get_and_update(:mytab, :a, fn current_value ->
...> {current_value, "new value!"}
...> end)
{1, "new value!"}
iex> ExShards.get_and_update(:mytab, :b, fn current_value ->
...> {current_value, "new value!"}
...> end)
{nil, "new value!"}
iex> ExShards.get_and_update(:mytab, :a, fn _ -> :pop end)
{"new value!", nil}
iex> ExShards.get_and_update(:mytab, :b, fn _ -> :pop end)
{nil, nil}
Gets the value from key
and updates it. Raises if there is no key
.
Behaves exactly like get_and_update/3
, but raises a KeyError
exception if
key
is not present in tab
.
Examples
iex> :mytab |> ExShards.put(:a, 1) |> ExShards.get(:a)
1
iex> ExShards.get_and_update(:mytab, :a, fn current_value ->
...> {current_value, "new value!"}
...> end)
{1, "new value!"}
iex> ExShards.get_and_update(:mytab, :b, fn current_value ->
...> {current_value, "new value!"}
...> end)
** (KeyError) key :b not found in: :mytab
iex> ExShards.get_and_update(:mytab, :a, fn _ -> :pop end)
{"new value!", nil}
Returns whether the given key
exists in the given tab
.
Examples
iex> ExShards.has_key?(:mytab, :a)
true
iex> ExShards.has_key?(:mytab, :b)
false
Returns all keys from tab
.
Examples
iex> ExShards.set(:mytab, a: 1, b: 2, c: 3)
:mytab
iex> ExShards.keys(:mytab)
[:a, :b, :c]
WARNING: This is an expensive operation, try DO NOT USE IT IN PROD.
Returns and removes the value(s) associated with key
in tab
.
Return possibilities:
value
– Ifkey
is present intab
with valuevalue
.[value]
– In case of multiple matches with the keykey
. This behaviour is expected when the table is either a:bag
or a:duplicate_bag
.default
– Ifkey
is not present intab
.
Examples
iex> :mytab |> ExShards.set(a: 1, b: 2, a: 2) |> ExShards.pop(:a)
[1, 2]
iex> ExShards.pop(:mytab, :b)
2
iex> ExShards.pop(:mytab, :c)
nil
iex> ExShards.pop(:mytab, :c, 3)
3
Puts the given value
under key
in tab
.
Examples
iex> :mytab |> ExShards.put(:a, 1) |> ExShards.fetch!(:a)
1
iex> :mytab |> ExShards.put(:a, 3) |> ExShards.fetch!(:a)
3
Puts the given value
under key
unless the entry key
already exists in tab
.
Examples
iex> ExShards.put_new(:mytab, :a, 1) |> ExShards.fetch!(:a)
1
iex> ExShards.put_new(:mytab, :a, 3) |> ExShards.fetch!(:a)
1
Removes the entry in tab
for a specific key
.
If the key
does not exist, returns tab
unchanged.
Examples
iex> :mytab |> ExShards.put(:a, 1) |> ExShards.fetch!(:a)
1
iex> ExShards.delete(:mytab, :a) |> ExShards.get(:a)
nil
iex> ExShards.delete(:mytab, :b) |> ExShards.get(:b)
nil
This function is analogous to :shards.insert/2,3
, but it returns
the table name instead of true
.
Examples
iex> :mytab |> ExShards.set(a:1, b: 2) |> ExShards.get(:a)
1
iex> :mytab |> ExShards.set(b: 3) |> ExShards.get(:b)
3
take_and_drop(tab, Enumerable.t, state) :: map
Drops the given keys
from tab
and returns a map with all dropped
key-value pairs.
If keys
contains keys that are not in tab
, they’re simply ignored.
Examples
iex> ExShards.set(:mytab, a: 1, b: 2, a: 3, c: 4)
iex> ExShards.take_and_drop(:mytab, [:a, :b, :e])
%{a: [1, 3], b: 2}
iex> ExShards.get(:mytab, :a)
nil
Updates the key
in tab
with the given function.
If key
is present in tab
with value value
, fun
is invoked with
argument value
and its result is used as the new value of key
.
If key
is not present in tab
, initial
is inserted as the value
of key
.
This functions only works for :set
or :ordered_set
tables.
Examples
iex> :mytab |> ExShards.put(:a, 1) |> ExShards.update(:a, 13, &(&1 * 2)) |> ExShards.get(:a)
2
iex> :mytab |> ExShards.update(:b, 11, &(&1 * 2)) |> ExShards.get(:b)
11
Updates key
with the given function.
If key
is present in tab
with value value
, fun
is invoked with
argument value
and its result is used as the new value of key
.
If key
is not present in tab
, a KeyError
exception is raised.
This functions only works for :set
or :ordered_set
tables.
Examples
iex> :mytab |> ExShards.put(:a, 1) |> ExShards.update!(:a, &(&1 * 2)) |> ExShards.get(:a)
2
iex> :mytab |> ExShards.update(:b, &(&1 * 2))
** (KeyError) key :b not found in: :mytab
This function is analogous to :shards.update_element/3,4
, but it returns
the table name instead of true
.
This functions only works for :set
or :ordered_set
tables.
Examples
iex> :mytab |> ExShards.put(:a, 1) |> ExShards.update_elem(:a, {2, 11}) |> ExShards.get(:a)
11
iex> :mytab |> ExShards.update_elem(:b, {2, 22}) |> ExShards.get(:b)
nil