AgentMap v1.1.2 AgentMap.Utils

Link to this section Summary

Functions

get_prop(am, key) deprecated

Returns property with given key or default

Reads metadata information stored under key

Stores metadata information under key

Suspends the queue for key for t ms

Updates metadata information stored under key

Link to this section Types

Link to this section Functions

Link to this function dec(am, key, opts \\ [step: 1, cast: true, initial: 0, !: :avg])
dec(am(), key(), keyword()) :: am()

Decrements value for key.

See inc/3.

Link to this function get_prop(am, key) (since 1.1.2)
get_prop(am(), term()) :: term()
This function is deprecated. Use `meta/2` instead..
Link to this function get_prop(am, key, default)
This function is deprecated. Use `meta/2` instead.

Returns property with given key or default.

Link to this function inc(am, key, opts \\ [step: 1, cast: true, initial: 0, !: :avg])
inc(am(), key(), keyword()) :: am()

Increments value with given key.

By default, returns without waiting for the actual increment.

Raises an ArithmeticError if current value associated with key is not a number.

Options

  • step: number, 1 — increment step;

  • initial: number, 0 — initial value;

  • initial: false — to raise KeyError on server if value does not exist;

  • cast: false — to return only after the increment;

  • !: priority, :avg;

  • :timeout, 5000.

Examples

iex> am = AgentMap.new(a: 1.5)
iex> am
...> |> inc(:a, step: 1.5)
...> |> inc(:b, step: 1.5)
...> |> get(:a)
3.0
iex> get(am, :b)
1.5

iex> AgentMap.new()
...> |> sleep(:a, 20)
...> |> put(:a, 1)               # 1
...> |> cast(:a, fn 2 -> 3 end)  # : ↱ 3
...> |> inc(:a, !: :max)         # ↳ 2 :
...> |> get(:a)                  #     ↳ 4
3
Link to this function meta(am, key) (since 1.1.2)
meta(am(), term()) :: term()

Reads metadata information stored under key.

Functions meta/2, upd_meta/4 and put_meta/3 can be used inside callbacks.

Special keys

  • :size — current size (fast, but in some rare cases inaccurate upwards);

  • :real_size — current size (slower, but always accurate);

  • :max_concurrency — a limit for the number of processes allowed to spawn;

  • :processes — total number of processes being used (+1 for server itself).

Examples

iex> am = AgentMap.new()
iex> meta(am, :processes)
1
iex> meta(am, :max_concurrency)
5000
iex> am
...> |> sleep(:a, 10)
...> |> sleep(:b, 100)
...> |> meta(:processes)
3
#
iex> sleep(50)
iex> meta(am, :processes)
2
#
iex> sleep(200)
iex> meta(am, :processes)
1

#

iex> am = AgentMap.new()
iex> meta(am, :size)
0
iex> am
...> |> sleep(:a, 10)
...> |> sleep(:b, 50)
...> |> put(:b, 42)
...>
iex> meta(am, :size)       # size calculation is inaccurate, but fast
2
iex> meta(am, :real_size)  # that's better
0
iex> sleep(50)
iex> meta(am, :size)       # worker for :a just died
1
iex> meta(am, :real_size)
0
iex> sleep(40)             # worker for :b just died
iex> meta(am, :size)
1
iex> meta(am, :real_size)
1

#

iex> {:ok, am} =
...>   AgentMap.start_link([key: 1], meta: [custom_key: "custom_value"])
...>
iex> meta(am, :custom_key)
"custom_value"
iex> meta(am, :unknown_key)
nil
Link to this function put_meta(am, key, value, opts \\ [cast: true]) (since 1.1.2)
put_meta(am(), term(), term(), keyword()) :: am()

Stores metadata information under key.

See meta/2, upd_meta/4.

Examples

iex> am = AgentMap.new()
iex> am
...> |> put_meta(:key, 42)
...> |> meta(:key)
42

iex> am = AgentMap.new()
iex> meta(am, :bar)
nil
iex> meta(am, :bar, :baz)
:baz
iex> meta(am, :max_concurrency)
5000

Functions meta/2, upd_meta/3, put_meta/4 can be used inside callbacks:

iex> am = AgentMap.new()
iex> am
...> |> get(:a, fn _ -> put_meta(am, :foo, :bar) end)
...> |> get(:b, fn _ -> meta(am, :foo) end)
:bar
Link to this function safe_apply(fun, args) (since 1.1.2)
This function is deprecated. Just use the `try … rescue / catch` block instead.
Link to this function safe_apply(fun, args, timeout) (since 1.1.2)
This function is deprecated. Just use `Task.async` → `Task.yield` instead..
Link to this function set_prop(am, key, value) (since 1.1.2)
set_prop(am(), term(), term()) :: am()
This function is deprecated. Use `put_meta/3` instead.
Link to this function sleep(am, key, t, opts \\ [!: {:max, +2}, cast: true])
sleep(am(), key(), pos_integer() | :infinity, keyword()) :: am()

Suspends the queue for key for t ms.

Returns without waiting for the actual suspend to happen.

Options

  • cast: false — to return after the actual sleep;

  • !: priority, {:max, +2} — to postpone sleep until calls with a higher priority are executed.

Link to this function upd_meta(am, key, fun, opts \\ [cast: true]) (since 1.1.2)
upd_meta(am(), term(), (... -> any()), keyword()) :: am()

Updates metadata information stored under key.

Underneath, GenServer.cast/2 is used.

See meta/2, put_meta/4.

Options

  • cast: false — to wait for the actual update to occur.

Examples

iex> AgentMap.new()
...> |> put_meta(:key, 42)
...> |> upd_meta(:key, fn 42 -> 24 end)
...> |> meta(:key)
24
Link to this function upd_prop(am, key, fun, opts \\ [cast: true]) (since 1.1.2)
This function is deprecated. Use `upd_meta/4` instead.