View Source ExRocketmq.Util.Buffer (lib_oss v0.1.0)

A simple buffer queue implementation using ETS tables.

This module provides a way to create a buffer queue with a specified capacity and allows items to be put into and taken out of the queue. If the queue is full, put will return {:error, :full}. If the queue is empty, take will block until an item is available.

Examples

iex> {:ok, pid} = ExRocketmq.Util.Buffer.start_link(name: :my_queue, size: 10)
iex> ExRocketmq.Util.Buffer.put(:my_queue, [1, 2, 3])
:ok
iex> ExRocketmq.Util.Buffer.take(:my_queue)
[1, 2, 3]

Summary

Functions

Returns a specification to start this module under a supervisor.

Returns the first value in the buffer queue.

Returns the last value in the buffer queue.

Adds the given items to the buffer queue.

Returns the number of items in the buffer queue.

Starts a new buffer process.

Removes and returns items from the buffer queue.

Types

@type t() :: %ExRocketmq.Util.Buffer{
  buff: atom(),
  buff_size: non_neg_integer(),
  capacity: non_neg_integer(),
  gc_freq: non_neg_integer(),
  touch: non_neg_integer()
}

Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

first(name, timeout \\ 5000)

View Source

Returns the first value in the buffer queue.

Examples

iex> {:ok, _pid} = ExRocketmq.Util.Buffer.start_link(:my_queue, 10)
iex> ExRocketmq.Util.Buffer.put(:my_queue, [1, 2, 3])
:ok
iex> ExRocketmq.Util.Buffer.first(:my_queue)
1
Link to this function

last(name, timeout \\ 5000)

View Source
@spec last(atom(), non_neg_integer()) :: any()

Returns the last value in the buffer queue.

Examples

iex> {:ok, _pid} = ExRocketmq.Util.Buffer.start_link(:my_queue, 10)
iex> ExRocketmq.Util.Buffer.put(:my_queue, [1, 2, 3])
:ok
iex> ExRocketmq.Util.Buffer.last(:my_queue)
3
Link to this function

put(name, items \\ [], timeout \\ 5000)

View Source
@spec put(atom(), [any()], non_neg_integer()) :: :ok | {:error, :full}

Adds the given items to the buffer queue.

If the buffer queue is full, this function will return {:error, :full}.

Examples

iex> {:ok, _pid} = ExRocketmq.Util.Buffer.start_link(:my_queue, 10)
iex> ExRocketmq.Util.Buffer.put(:my_queue, [1, 2, 3])
:ok
iex> ExRocketmq.Util.Buffer.put(:my_queue, [4, 5, 6], 1000)
:ok
iex> ExRocketmq.Util.Buffer.put(:my_queue, [7, 8, 9, 10, 11])
{:error, :full}
Link to this function

size(name, timeout \\ 5000)

View Source
@spec size(atom(), non_neg_integer()) :: non_neg_integer()

Returns the number of items in the buffer queue.

Examples

iex> {:ok, _pid} = ExRocketmq.Util.Buffer.start_link(:my_queue, 10)
iex> ExRocketmq.Util.Buffer.put(:my_queue, [1, 2, 3])
:ok
iex> ExRocketmq.Util.Buffer.size(:my_queue)
3
@spec start_link(keyword()) :: Agent.on_start()

Starts a new buffer process.

Options

  • :name - The name of the buffer process. Must be an atom
  • :size - The maximum number of items the buffer can hold

Examples

iex> {:ok, pid} = ExRocketmq.Util.Buffer.start_link(name: :my_queue, size: 10)
iex> ExRocketmq.Util.Buffer.put(:my_queue, [1, 2, 3])
@spec stop(atom()) :: :ok
Link to this function

take(name, timeout \\ 5000)

View Source
@spec take(atom(), non_neg_integer()) :: [any()]

Removes and returns items from the buffer queue.

If the buffer queue is empty, this function will block until an item is available.

Examples

iex> {:ok, _pid} = ExRocketmq.Util.Buffer.start_link(:my_queue, 10)
iex> ExRocketmq.Util.Buffer.put(:my_queue, [1, 2, 3])
:ok
iex> ExRocketmq.Util.Buffer.take(:my_queue)
[1, 2, 3]
iex> ExRocketmq.Util.Buffer.take(:my_queue)
[]