time_queue v0.7.0 TimeQueue.GbTrees
Implements a timers queue based on gb_trees.
The queue keys are a two-tuple composed of the timestamp of an entry 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 entry from the queue and returns the new queue.
Deletes all entries from the queue whose values are equal to unwanted
.
Adds a new entry to the queue with a TTL and the current system time as now/0
.
Adds a new entry to the queue with a TTL relative to the given timestamp in milliseconds.
Adds a new entry 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 event of the queue according to the given current time in milliseconds.
Extracts the next event of the queue according to the given current time in milliseconds.
Returns the numer of entries in the queue.
Returns the time reference of an queue entry. This reference is used as a key to identify a unique entry.
Returns the value of an queue entry.
Link to this section Types
peek_return()
peek_return() :: :empty | {:delay, tref(), non_neg_integer()} | {:ok, entry()}
pop_return(tq)
pop_return(tq) :: :empty | {:delay, tref(), non_neg_integer()} | {:ok, entry(), tq}
timespec_unit()
timespec_unit() :: :ms | :second | :seconds | :minute | :minutes | :hour | :hours | :day | :days | :week | :weeks
Link to this section Functions
Deletes an entry from the queue and returns the new queue.
It accepts a time reference or a full entry. When an entry is given, its time reference will be used to find the entry to delete, meaning the queue entry will be deleted even if the value of the passed entry was tampered.
The function does not fail if the entry cannot be found and simply returns the queue as-is.
Deletes all entries from the queue whose values are equal to unwanted
.
This function is slow with gb_trees
, see filter/2
.
Adds a new entry to the queue with a TTL and the current system time as now/0
.
See enqueue/4
.
enqueue(tq, ttl, val, now_ms)
Adds a new entry to the queue with a TTL relative to the given timestamp in milliseconds.
Returns {:ok, tref, new_queue}
where tref
is a timer reference.
enqueue_abs(arg, ts, val)
enqueue_abs(t(), end_time :: integer(), value :: any()) :: enqueue_return(t())
Adds a new entry to the queue with an absolute timestamp.
Returns {:ok, tref, new_queue}
where tref
is a timer reference.
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.
Returns a new queue with entries for whom the given callback returned a truthy value.
Unlinke filter/2
, the callback is only passed the entry value.
This function is slow with gb_trees
, see filter/2
.
Creates an empty time queue.
iex> tq = TimeQueue.new()
iex> TimeQueue.peek(tq)
:empty
This function is used internally to determine the current time when it is
not given in the arguments to enqueue/3
, pop/1
and peek/1
.
It is a simple alias to :erlang.system_time(:millisecond)
. TimeQueue does
not use monotonic time since it already manages its own unique identifiers for
queue entries.
Returns the next event of the queue with the current system time as now/0
.
See peek/2
.
Returns the next event of the queue according to the given current time in milliseconds.
Possible return values are:
:empty
{:ok, entry}
if the timestamp of the first entry is<=
to the given current time.{:delay, tref, ms}
if the timestamp of the first entry is>
to the given current time. The remaining amount of milliseconds is returned.
Example
iex> {:ok, tref, tq} = TimeQueue.new() |> TimeQueue.enqueue(100, :hello, _now = 0)
iex> {:delay, ^tref, 80} = TimeQueue.peek(tq, _now = 20)
iex> {:ok, _} = TimeQueue.peek(tq, _now = 100)
Extracts the next event of the queue with the current system time as now/0
.
See pop/2
.
Extracts the next event of the queue according to the given current time in milliseconds.
Possible return values are:
:empty
{:ok, entry, new_queue}
if the timestamp of the first entry is<=
to the given current time. The entry is deleted fromnew_queue
.{:delay, tref, ms}
if the timestamp of the first entry is>
to the given current time. The remaining amount of milliseconds is returned.
Example
iex> {:ok, tref, tq} = TimeQueue.new() |> TimeQueue.enqueue(100, :hello, _now = 0)
iex> {:delay, ^tref, 80} = TimeQueue.pop(tq, _now = 20)
iex> {:ok, _, _} = TimeQueue.pop(tq, _now = 100)
Returns the numer of entries in the queue.
supports_encoding(arg1)
Returns the time reference of an queue entry. This reference is used as a key to identify a unique entry.
iex> tq = TimeQueue.new()
iex> {:ok, tref, tq} = TimeQueue.enqueue(tq, 10, :my_value)
iex> Process.sleep(10)
iex> {:ok, entry} = TimeQueue.peek(tq)
iex> tref == TimeQueue.tref(entry)
true
Returns the value of an queue entry.
iex> tq = TimeQueue.new()
iex> {:ok, _, tq} = TimeQueue.enqueue(tq, 10, :my_value)
iex> Process.sleep(10)
iex> {:ok, entry} = TimeQueue.peek(tq)
iex> TimeQueue.value(entry)
:my_value