View Source PropertyTable (property_table v0.2.3)

In-memory key-value store with subscriptions

PropertyTable makes it easy to set up a key-value store where users can subscribe to changes based on patterns. PropertyTable refers to keys as properties. Properties have values and are timestamped as to when they received that value. Subscriptions make this library feel similar to Publish-Subscribe. Events, though, are only for changes to properties.

PropertyTable is useful when you want to expose a decent amount of state and let consumers pick and choose what parts interest them.

PropertyTable consumers express their interest in properties using "patterns". A pattern could be as simple as the property of interest or it could contain wildcards. This allows one to create hierarchical key-value stores, map-based stores, or just simple key-value stores with notifications.

PropertyTable is optionally persistent to disk. Keys and values are backed by ETS.

Link to this section Summary

Types

PropertyTable configuration options

Properties

A table_id identifies a group of properties

Functions

Returns a specification to start a property_table under a supervisor. See Supervisor.

Delete the specified property

Delete all properties that match a pattern

Fetch a property with the time that it was set

If persistence is enabled for this property table, save the current state to disk immediately.

Get the current value of a property

Get all properties

Returns a list of availiable snapshot IDs and full name tuples for a property table with persistence enable

Get a list of all properties matching the specified property pattern

Update a property and notify listeners

Update many properties

If persistence is enabled for this property table, restore the current state of the PropertyTable to that of a past named snapshot

If persistence is enabled for this property table, save the current state and copy a snapshot of it into the /snapshots sub-directory of the set data directory.

Start a PropertyTable's supervision tree

Subscribe to receive events

Stop subscribing to a property

Link to this section Types

@type option() ::
  {:name, table_id()}
  | {:properties, [property_value()]}
  | {:tuple_events, boolean()}

PropertyTable configuration options

See start_link/2 for usage.

@type pattern() :: any()
@type property() :: any()

Properties

@type property_value() :: {property(), value()}
@type table_id() :: atom()

A table_id identifies a group of properties

@type value() :: any()

Link to this section Functions

@spec child_spec(keyword()) :: Supervisor.child_spec()

Returns a specification to start a property_table under a supervisor. See Supervisor.

@spec delete(table_id(), property()) :: :ok

Delete the specified property

Link to this function

delete_matches(table, pattern)

View Source
@spec delete_matches(table_id(), pattern()) :: :ok

Delete all properties that match a pattern

Link to this function

fetch_with_timestamp(table, property)

View Source
@spec fetch_with_timestamp(table_id(), property()) ::
  {:ok, value(), integer()} | :error

Fetch a property with the time that it was set

Timestamps come from System.monotonic_time()

@spec flush_to_disk(table_id()) :: :ok

If persistence is enabled for this property table, save the current state to disk immediately.

Link to this function

get(table, property, default \\ nil)

View Source
@spec get(table_id(), property(), value()) :: value()

Get the current value of a property

@spec get_all(table_id()) :: [{property(), value()}]

Get all properties

This function might return a really long list so it's mainly intended for debug or convenience when you know that the table only contains a few properties.

@spec get_snapshots(table_id()) :: [{String.t(), String.t()}]

Returns a list of availiable snapshot IDs and full name tuples for a property table with persistence enable

@spec match(table_id(), pattern()) :: [{property(), value()}]

Get a list of all properties matching the specified property pattern

Link to this function

put(table, property, value)

View Source
@spec put(table_id(), property(), value()) :: :ok

Update a property and notify listeners

Link to this function

put_many(table, properties)

View Source
@spec put_many(table_id(), [{property(), value()}]) :: :ok

Update many properties

This is similar to calling put/3 several times in a row, but atomically. It is also slightly more efficient when updating more than one property.

Link to this function

restore_snapshot(table, snapshot_name)

View Source
@spec restore_snapshot(table_id(), String.t()) :: :ok | :noop

If persistence is enabled for this property table, restore the current state of the PropertyTable to that of a past named snapshot

@spec snapshot(table_id()) :: {:ok, String.t()} | :noop

If persistence is enabled for this property table, save the current state and copy a snapshot of it into the /snapshots sub-directory of the set data directory.

@spec start_link([option()]) :: Supervisor.on_start()

Start a PropertyTable's supervision tree

To create a PropertyTable for your application or library, add the following child_spec to one of your supervision trees:

{PropertyTable, name: MyTableName}

The :name option is required. All calls to PropertyTable will need to know it and the process will be registered under than name so be sure it's unique.

Additional options are:

  • :properties - a list of {property, value} tuples to initially populate the PropertyTable
  • :matcher - set the format for how properties and how they should be matched for triggering events. See PropertyTable.Matcher.
  • :tuple_events - set to true for change events to be in the old tuple format. This is not recommended for new code and hopefully will be removed in the future.

Persistent options:

  • You MUST set at least :persist_data_path for any of the other options to be respected!

  • If the table can restore its data from disk, it will IGNORE your initial :properties value.

  • :persist_data_path - set to a directory where PropertyTable will persist the contents of the table to disk, snapshots will also be stored here.

  • :persist_interval - if set PropertyTable will persist the contents of tables to disk in intervals of the provided value (in milliseconds) automatically.

  • :persist_max_snapshots - Maximum number of manual snapshots to keep on disk before they are replaced - (oldest snapshots are replaced first.) Defaults to 25.

  • :persist_compression - 0..9 range to compress the terms when written to disk, see :erlang.term_to_binary/2. Defaults to 6.

Link to this function

subscribe(table, pattern)

View Source
@spec subscribe(table_id(), pattern()) :: :ok

Subscribe to receive events

Link to this function

unsubscribe(table, pattern)

View Source
@spec unsubscribe(table_id(), pattern()) :: :ok

Stop subscribing to a property