time_queue v0.9.5 TimeQueue.GbTrees View Source

Implements a timers queue based on gb_trees.

The queue keys are a two-tuple composed of the timestamp of an event and an unique integer.

No erlang timers or processes are used, as the queue is only a data structure. The advantage is that the queue can be persisted on storage and keep working after restarting the runtime. The queue maintain its own list of unique integers to avoir relying on BEAM unique integers as they are reset on restart.

The main drawback is that the queue entries must be manually checked for expired timers.

Link to this section Summary

Functions

Deletes an event from the queue and returns the new queue.

Deletes all entries from the queue whose values are equal to unwanted.

Adds a new event to the queue with a TTL and the current system time as now/0.

Adds a new event to the queue with a TTL relative to the given timestamp in milliseconds.

Adds a new event to the queue with an absolute timestamp.

Returns a new queue with entries for whom the given callback returned a truthy value.

Returns a new queue with entries for whom the given callback returned a truthy value.

Creates an empty time queue.

Returns the next value of the queue or a delay in milliseconds before the next value.

Returns the next value of the queue, or a delay, according to the given current time in milliseconds.

peek_entry(tq) deprecated

Alias for peek_event/1.

Alias for peek_event/2.

Returns the next event of the queue or a delay in milliseconds before the next value.

Returns the next event of the queue according to the given current time in milliseconds.

Extracts the next event in the queue or returns a delay.

Extracts the next event in the queue according to the given current time in milliseconds.

pop_entry(tq) deprecated

Alias for pop_event/1.

Alias for pop_event/2.

Extracts the next event of the queue with the current system time as now/0.

Extracts the next event of the queue according to the given current time in milliseconds.

Returns the numer of entries in the queue.

Provides a GenServer compatible timeout from the queue.

Returns the time reference of a queue event. This reference is used as a key to identify a unique event.

Returns the value of a queue event.

Link to this section Types

Link to this type

enqueue_return()

View Source
enqueue_return() :: {:ok, tref(), t()}
Link to this opaque

event()

View Source (opaque)
event()
Link to this type

event_value()

View Source
event_value() :: any()
Link to this type

peek_event_return()

View Source
peek_event_return() ::
  :empty | {:delay, tref(), non_neg_integer()} | {:ok, event()}
Link to this type

peek_return()

View Source
peek_return() ::
  :empty | {:delay, tref(), non_neg_integer()} | {:ok, event_value()}
Link to this type

pop_event_return()

View Source
pop_event_return() ::
  :empty | {:delay, tref(), non_neg_integer()} | {:ok, event(), t()}
Link to this type

pop_return()

View Source
pop_return() ::
  :empty | {:delay, tref(), non_neg_integer()} | {:ok, event_value(), t()}
Link to this type

timespec_unit()

View Source
timespec_unit() ::
  :ms
  | :second
  | :seconds
  | :minute
  | :minutes
  | :hour
  | :hours
  | :day
  | :days
  | :week
  | :weeks
Link to this type

timestamp_ms()

View Source
timestamp_ms() :: pos_integer()

Link to this section Functions

Link to this function

delete(tq, tref)

View Source
delete(t(), event() | tref()) :: t()

Deletes an event from the queue and returns the new queue.

It accepts a time reference or a full event. When an event is given, its time reference will be used to find the event to delete, meaning the queue event will be deleted even if the value of the passed event was tampered.

The function does not fail if the event cannot be found and simply returns the queue as-is.

Link to this function

delete_val(tq, unwanted)

View Source
delete_val(t(), any()) :: t()

Deletes all entries from the queue whose values are equal to unwanted.

This function is slow with gb_trees, see filter/2.

Link to this function

enqueue(tq, ttl, val)

View Source
enqueue(t(), ttl(), any()) :: enqueue_return()

Adds a new event to the queue with a TTL and the current system time as now/0.

See enqueue/4.

Link to this function

enqueue(tq, ttl, val, now_ms)

View Source
enqueue(t(), ttl(), any(), now :: integer()) :: enqueue_return()

Adds a new event to the queue with a TTL relative to the given timestamp in milliseconds.

Returns {:ok, tref, new_queue} where tref is a timer reference.

Link to this function

enqueue_abs(arg, ts, val)

View Source
enqueue_abs(t(), end_time :: integer(), value :: any()) :: enqueue_return()

Adds a new event to the queue with an absolute timestamp.

Returns {:ok, tref, new_queue} where tref is a timer reference.

Link to this function

filter(arg, fun)

View Source
filter(t(), (event() -> bool())) :: t()

Returns a new queue with entries for whom the given callback returned a truthy value.

With the gbtrees implementation, this operation is _very expensive as we convert the tree to and ordered list, filter the list, and convert back to a tree.

Link to this function

filter_val(tq, fun)

View Source
filter_val(t(), (any() -> bool())) :: t()

Returns a new queue with entries for whom the given callback returned a truthy value.

Unlinke filter/2, the callback is only passed the event value.

This function is slow with gb_trees, see filter/2.

Creates an empty time queue.

iex> tq = TimeQueue.GbTrees.new()
iex> TimeQueue.GbTrees.peek_event(tq)
:empty

Returns the next value of the queue or a delay in milliseconds before the next value.

See peek/2.

Link to this function

peek(tq, now)

View Source
peek(t(), now_ms :: timestamp_ms()) :: peek_return()

Returns the next value of the queue, or a delay, according to the given current time in milliseconds.

Just like pop/2 vs.pop_event/2, peek wil only return {:ok, value} when a timeout is reached whereas peek_event will return {:ok, event}.

This function is deprecated. Use peek_event/1 instead.

Alias for peek_event/1.

Link to this function

peek_entry(tq, now_ms)

View Source
peek_entry(t(), now_ms :: timestamp_ms()) :: peek_event_return()
This function is deprecated. Use peek_event/2 instead.

Alias for peek_event/2.

Returns the next event of the queue or a delay in milliseconds before the next value.

event values can be retrieved with TimeQueue.GbTrees.value/1.

See peek_event/2.

Link to this function

peek_event(arg, now)

View Source
peek_event(t(), now_ms :: timestamp_ms()) :: peek_event_return()

Returns the next event of the queue according to the given current time in milliseconds.

Possible return values are:

  • :empty
  • {:ok, event} if the timestamp of the first event is <= to the given current time.
  • {:delay, tref, ms} if the timestamp of the first event is > to the given current time. The remaining amount of milliseconds is returned.

Example

iex> {:ok, tref, tq} = TimeQueue.GbTrees.new() |> TimeQueue.GbTrees.enqueue(100, :hello, _now = 0)
iex> {:delay, ^tref, 80} = TimeQueue.GbTrees.peek_event(tq, _now = 20)
iex> {:ok, _} = TimeQueue.GbTrees.peek_event(tq, _now = 100)

Extracts the next event in the queue or returns a delay.

See pop/2.

Link to this function

pop(tq, now)

View Source
pop(t(), now_ms :: timestamp_ms()) :: pop_return()

Extracts the next event in the queue according to the given current time in milliseconds.

Much like pop_event/2 but the tuple returned when an event time is reached (returns with :ok) success will only contain the value inserted in the queue.

Possible return values are:

  • :empty
  • {:ok, value, new_queue} if the timestamp of the first event is <= to the given current time. The event is deleted from new_queue.
  • {:delay, tref, ms} if the timestamp of the first event is > to the given current time. The remaining amount of milliseconds is returned.

Example

iex> {:ok, tref, tq} = TimeQueue.GbTrees.new() |> TimeQueue.GbTrees.enqueue(100, :hello, _now = 0)
iex> {:delay, ^tref, 80} = TimeQueue.GbTrees.pop(tq, _now = 20)
iex> {:ok, value, _} = TimeQueue.GbTrees.pop(tq, _now = 100)
iex> value
:hello
This function is deprecated. Use pop_event/1 instead.

Alias for pop_event/1.

Link to this function

pop_entry(tq, now_ms)

View Source
pop_entry(t(), now_ms :: timestamp_ms()) :: pop_event_return()
This function is deprecated. Use pop_event/2 instead.

Alias for pop_event/2.

Extracts the next event of the queue with the current system time as now/0.

See pop_event/2.

Link to this function

pop_event(arg, now)

View Source
pop_event(t(), now_ms :: timestamp_ms()) :: pop_event_return()

Extracts the next event of the queue according to the given current time in milliseconds.

Possible return values are:

  • :empty
  • {:ok, event, new_queue} if the timestamp of the first event is <= to the given current time. The event is deleted from new_queue.
  • {:delay, tref, ms} if the timestamp of the first event is > to the given current time. The remaining amount of milliseconds is returned.

Example

iex> {:ok, tref, tq} = TimeQueue.GbTrees.new() |> TimeQueue.GbTrees.enqueue(100, :hello, _now = 0)
iex> {:delay, ^tref, 80} = TimeQueue.GbTrees.pop_event(tq, _now = 20)
iex> {:ok, _, _} = TimeQueue.GbTrees.pop_event(tq, _now = 100)

Returns the numer of entries in the queue.

Link to this function

timeout(tq, now_ms \\ now())

View Source
timeout(t(), now_ms :: timestamp_ms()) :: non_neg_integer() | :infinity

Provides a GenServer compatible timeout from the queue.

Accepts the current time as a second argument or will default to the current system time.

Returns:

  • :infinity when the queue is empty,
  • 0 when there is the next event time has been reached or is in the past,
  • otherwise it returns the delay to the next event.

Returns the time reference of a queue event. This reference is used as a key to identify a unique event.

iex> tq = TimeQueue.GbTrees.new()
iex> {:ok, tref, tq} = TimeQueue.GbTrees.enqueue(tq, 10, :my_value)
iex> Process.sleep(10)
iex> {:ok, event} = TimeQueue.GbTrees.peek_event(tq)
iex> tref == TimeQueue.GbTrees.tref(event)
true

Returns the value of a queue event.

iex> tq = TimeQueue.GbTrees.new()
iex> {:ok, _, tq} = TimeQueue.GbTrees.enqueue(tq, 10, :my_value)
iex> Process.sleep(10)
iex> {:ok, event} = TimeQueue.GbTrees.peek_event(tq)
iex> TimeQueue.GbTrees.value(event)
:my_value