AgentMap v1.1.2 AgentMap.Utils
Link to this section Summary
Functions
Decrements value for key
Returns property with given key or default
Increments value with given key
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
Decrements value for key.
See inc/3.
Returns property with given key or default.
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 raiseKeyErroron 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
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 (+1for 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
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
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.
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