Mnemonix v0.8.0 Mnemonix.Features.Bump

Functions to increment/decrement integer values within a Mnemonix.Store.Server.

All of these functions are available on the main Mnemonix module.

Summary

Types

The return value of a bump operation

Functions

Adds amount to the value of the integer entry under key in store

Adds amount to the value of the integer entry under key in store

Decrements the value of the integer entry under key in store by 1

Decrements the value of the integer entry under key in store by amount

Increments the value of the integer entry under key in store by 1

Increments the value of the integer entry under key in store by amount

Types

bump_op()
bump_op() :: :ok | {:error, :no_integer}

The return value of a bump operation.

Functions

bump(store, key, amount)
bump(Mnemonix.store, Mnemonix.key, amount :: term) ::
  bump_op |
  no_return

Adds amount to the value of the integer entry under key in store.

If the key does not exist, it is set to 0 before performing the operation.

If the amount or the value under key is not an integer, returns {:error, :no_integer}, otherwise returns :ok, and the value will remain unchanged.

Examples

iex> store = Mnemonix.new(%{a: 1})
iex> Mnemonix.bump(store, :a, 1)
:ok
iex> Mnemonix.get(store, :a)
2

iex> store = Mnemonix.new
iex> Mnemonix.bump(store, :b, 2)
:ok
iex> Mnemonix.get(store, :b)
2

iex> store = Mnemonix.new(%{c: "foo"})
iex> Mnemonix.bump(store, :c, 3)
{:error, :no_integer}

iex> store = Mnemonix.new
iex> Mnemonix.bump(store, :c, "foo")
{:error, :no_integer}
bump!(store, key, amount)
bump!(Mnemonix.store, Mnemonix.key, amount :: term) ::
  :ok |
  no_return

Adds amount to the value of the integer entry under key in store.

If the key does not exist, it is set to 0 before performing the operation.

If the amount or the value under key is not an integer, raises an ArithmeticError, and the value will remain unchanged.

Examples

iex> store = Mnemonix.new(%{a: 1})
iex> Mnemonix.bump!(store, :a, 2)
:ok
iex> Mnemonix.get(store, :a)
3

iex> store = Mnemonix.new
iex> Mnemonix.bump!(store, :b, 2)
:ok
iex> Mnemonix.get(store, :b)
2

iex> store = Mnemonix.new(%{c: "foo"})
iex> Mnemonix.bump!(store, :c, 2)
** (ArithmeticError) value at key :c is not an integer

iex> store = Mnemonix.new
iex> Mnemonix.bump!(store, :d, "foo")
** (ArithmeticError) value provided to operation is not an integer
decrement(store, key)
decrement(Mnemonix.store, Mnemonix.key) ::
  Mnemonix.store |
  no_return

Decrements the value of the integer entry under key in store by 1.

If the key does not exist, it is set to 0 before performing the operation.

If the value under key is not an integer, returns store unchanged.

Examples

iex> store = Mnemonix.new(%{a: 1})
iex> Mnemonix.decrement(store, :a)
iex> Mnemonix.get(store, :a)
0

iex> store = Mnemonix.new
iex> Mnemonix.decrement(store, :b)
iex> Mnemonix.get(store, :b)
-1

iex> store = Mnemonix.new(%{c: "foo"})
iex> Mnemonix.decrement(store, :c)
iex> Mnemonix.get(store, :c)
"foo"
decrement(store, key, amount)
decrement(Mnemonix.store, Mnemonix.key, amount :: term) ::
  Mnemonix.store |
  no_return

Decrements the value of the integer entry under key in store by amount.

If the key does not exist, it is set to 0 before performing the operation.

If amount or the value under key is not an integer, returns store unchanged.

Examples

iex> store = Mnemonix.new(%{a: 2})
iex> Mnemonix.decrement(store, :a, 2)
iex> Mnemonix.get(store, :a)
0

iex> store = Mnemonix.new
iex> Mnemonix.decrement(store, :b, 2)
iex> Mnemonix.get(store, :b)
-2

iex> store = Mnemonix.new(%{c: "foo"})
iex> Mnemonix.decrement(store, :c, 2)
iex> Mnemonix.get(store, :c)
"foo"

iex> store = Mnemonix.new
iex> Mnemonix.decrement(store, :d, "foo")
iex> Mnemonix.get(store, :d)
0
increment(store, key)
increment(Mnemonix.store, Mnemonix.key) ::
  Mnemonix.store |
  no_return

Increments the value of the integer entry under key in store by 1.

If the key does not exist, it is set to 0 before performing the operation.

If the value under key is not an integer, returns store unchanged.

Examples

iex> store = Mnemonix.new(%{a: 1})
iex> Mnemonix.increment(store, :a)
iex> Mnemonix.get(store, :a)
2

iex> store = Mnemonix.new
iex> Mnemonix.increment(store, :b)
iex> Mnemonix.get(store, :b)
1

iex> store = Mnemonix.new(%{c: "foo"})
iex> Mnemonix.increment(store, :c)
iex> Mnemonix.get(store, :c)
"foo"
increment(store, key, amount)
increment(Mnemonix.store, Mnemonix.key, amount :: term) ::
  Mnemonix.store |
  no_return

Increments the value of the integer entry under key in store by amount.

If the key does not exist, it is set to 0 before performing the operation.

If the amount or the value under key is not an integer, returns store unchanged.

Examples

iex> store = Mnemonix.new(%{a: 1})
iex> Mnemonix.increment(store, :a, 2)
iex> Mnemonix.get(store, :a)
3

iex> store = Mnemonix.new
iex> Mnemonix.increment(store, :b, 2)
iex> Mnemonix.get(store, :b)
2

iex> store = Mnemonix.new(%{c: "foo"})
iex> Mnemonix.increment(store, :c, 2)
iex> Mnemonix.get(store, :c)
"foo"

iex> store = Mnemonix.new
iex> Mnemonix.increment(store, :d, "foo")
iex> Mnemonix.get(store, :d)
0