ets_deque v0.2.0 EtsDeque.Server View Source

EtsDeque.Server is a GenServer wrapper around an EtsDeque. It provides safe access to a deque from multiple processes, ensuring each operation on the deque is atomic.

Example

iex> {:ok, pid} = EtsDeque.Server.start_link(size: 3)
iex> :ok = EtsDeque.Server.push_head(pid, :moe)
iex> :ok = EtsDeque.Server.push_tail(pid, :larry)
iex> :ok = EtsDeque.Server.push_tail(pid, :curly)
iex> :error = EtsDeque.Server.push_tail(pid, :shemp)  ## deque is full
iex> {:ok, :curly} = EtsDeque.Server.pop_tail(pid)
iex> :ok = EtsDeque.Server.push_tail(pid, :shemp)
iex> EtsDeque.Server.execute(pid, fn deque -> Enum.to_list(deque) end)
[:moe, :larry, :shemp]

Link to this section Summary

Functions

Returns the item at the given index, where index 0 is the head. Returns :error if index is out of bounds.

Returns a specification to start this module under a supervisor.

Returns the deque. Ensuring that no other process mutates the deque after it is returned is the caller's responsibility. See execute/3 for a safer alternative.

Executes fun.(deque), ensuring no other process is accessing the deque at the same time. Returns the result.

Returns the number of items in the given deque.

Returns the item at the head of the queue, or :error if the queue is empty.

Returns the item at the tail of the queue, or :error if the queue is empty.

Removes the item at the head of the queue, returning it along with the updated deque. Returns :error if queue is empty.

Removes the item at the tail of the queue, returning it along with the updated deque. Returns :error if queue is empty.

Adds an item onto the head of the queue. Returns the updated deque, or :error if the queue is full.

Adds an item onto the tail of the queue. Returns the updated deque, or :error if the queue is full.

Replaces the item at the given index, returning the updated deque. Returns :error if index is out of bounds.

Returns the maximum capacity of the given deque.

Link to this section Functions

Link to this function

at(pid, index, timeout \\ 5000)

View Source
at(pid(), non_neg_integer(), timeout()) :: {:ok, any()} | :error

Returns the item at the given index, where index 0 is the head. Returns :error if index is out of bounds.

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

deque(pid, timeout \\ 5000)

View Source
deque(pid(), timeout()) :: EtsDeque.t()

Returns the deque. Ensuring that no other process mutates the deque after it is returned is the caller's responsibility. See execute/3 for a safer alternative.

Link to this function

execute(pid, fun, timeout \\ 5000)

View Source
execute(pid(), (EtsDeque.t() -> any()), timeout()) :: any()

Executes fun.(deque), ensuring no other process is accessing the deque at the same time. Returns the result.

Link to this function

length(pid, timeout \\ 5000)

View Source
length(pid(), timeout()) :: non_neg_integer()

Returns the number of items in the given deque.

Link to this function

peek_head(pid, timeout \\ 5000)

View Source
peek_head(pid(), timeout()) :: {:ok, any()} | :error

Returns the item at the head of the queue, or :error if the queue is empty.

Link to this function

peek_tail(pid, timeout \\ 5000)

View Source
peek_tail(pid(), timeout()) :: {:ok, any()} | :error

Returns the item at the tail of the queue, or :error if the queue is empty.

Link to this function

pop_head(pid, timeout \\ 5000)

View Source
pop_head(pid(), timeout()) :: {:ok, any()} | :error

Removes the item at the head of the queue, returning it along with the updated deque. Returns :error if queue is empty.

Link to this function

pop_tail(pid, timeout \\ 5000)

View Source
pop_tail(pid(), timeout()) :: {:ok, any()} | :error

Removes the item at the tail of the queue, returning it along with the updated deque. Returns :error if queue is empty.

Link to this function

push_head(pid, item, timeout \\ 5000)

View Source
push_head(pid(), any(), timeout()) :: :ok | :error

Adds an item onto the head of the queue. Returns the updated deque, or :error if the queue is full.

Link to this function

push_tail(pid, item, timeout \\ 5000)

View Source
push_tail(pid(), any(), timeout()) :: :ok | :error

Adds an item onto the tail of the queue. Returns the updated deque, or :error if the queue is full.

Link to this function

replace_at(pid, index, item, timeout \\ 5000)

View Source
replace_at(pid(), non_neg_integer(), any(), timeout()) :: :ok | :error

Replaces the item at the given index, returning the updated deque. Returns :error if index is out of bounds.

Link to this function

size(pid, timeout \\ 5000)

View Source
size(pid(), timeout()) :: non_neg_integer() | :infinity

Returns the maximum capacity of the given deque.