View Source Hologram.Commons.PLT (hologram v0.2.0)

Provides a persistent lookup table (PLT) implemented using ETS (Erlang Term Storage) and GenServer. It allows to store key-value pairs in memory and perform various operations on the data. The data in memory can be dumped to a file and loaded from a file.

Summary

Functions

Returns a specification to start this module under a supervisor.

Returns a clone of the PLT.

Deletes a key-value pair from the PLT.

Serializes the contents of the PLT and writes it to a file.

Returns the value stored in the PLT under the given key. If the key doesn't exist the :error :atom is returned.

Returns the value stored in the PLT under the given key. If the key doesn't exist a KeyError is raised.

Returns all items stored in the PLT.

Returns the reference of the underlying ETS table.

Creates the underlying ETS table.

Populates the PLT with items dumped to the given file.

Populates the PLT with items dumped to the given file if the file exists.

Puts multiple items into the PLT.

Puts the given item into the PLT.

Removes all items from the PLT.

Returns the number of items in the PLT.

Starts the underlying GenServer process.

Stops the underlying GenServer process.

Types

t()

@type t() :: %Hologram.Commons.PLT{
  pid: pid() | nil,
  table_name: atom() | nil,
  table_ref: Hologram.Commons.ETS.tid() | nil
}

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

clone(plt)

@spec clone(t()) :: t()

Returns a clone of the PLT.

delete(plt, key)

@spec delete(t(), any()) :: t()

Deletes a key-value pair from the PLT.

dump(plt, path)

@spec dump(t(), String.t()) :: t()

Serializes the contents of the PLT and writes it to a file.

Benchmark: https://github.com/bartblast/hologram/blob/master/benchmarks/commons/plt/dump_2/README.md

get(map, key)

@spec get(t(), any()) :: {:ok, term()} | :error

Returns the value stored in the PLT under the given key. If the key doesn't exist the :error :atom is returned.

get!(plt, key)

@spec get!(t(), any()) :: term()

Returns the value stored in the PLT under the given key. If the key doesn't exist a KeyError is raised.

get_all(map)

@spec get_all(t()) :: map()

Returns all items stored in the PLT.

handle_call(msg, from, state)

@spec handle_call(:get_table_ref, GenServer.from(), Hologram.Commons.ETS.tid()) ::
  {:reply, Hologram.Commons.ETS.tid(), Hologram.Commons.ETS.tid()}

Returns the reference of the underlying ETS table.

init(table_name)

@spec init(atom() | nil) :: {:ok, Hologram.Commons.ETS.tid()}

Creates the underlying ETS table.

load(plt, dump_path)

@spec load(t(), String.t()) :: t()

Populates the PLT with items dumped to the given file.

maybe_load(plt, dump_path)

@spec maybe_load(t(), String.t()) :: t()

Populates the PLT with items dumped to the given file if the file exists.

put(plt, items)

@spec put(t(), [{any(), any()}]) :: t()

Puts multiple items into the PLT.

put(plt, key, value)

@spec put(t(), any(), any()) :: t()

Puts the given item into the PLT.

reset(plt)

@spec reset(t()) :: t()

Removes all items from the PLT.

size(plt)

@spec size(t()) :: integer()

Returns the number of items in the PLT.

start(opts \\ [])

@spec start(Hologram.Commons.Types.opts()) :: t()

Starts the underlying GenServer process.

stop(plt)

@spec stop(t()) :: :ok

Stops the underlying GenServer process.