khepri_projection (khepri v0.6.0)

Khepri projections

Projections build a replicated ETS table using tree nodes from the store which match a khepri_path:pattern(). When a tree node matching a projection's pattern is changed in the store, the tree node is passed through the projection's projection_fun() to create record(s). These records are then stored in the projection's ETS table for all members in a Khepri cluster.

Projections provide a way to query the store as fast as possible and are appropriate for lookups which require low latency and/or high throughput. Projection tables contain all records matching the pattern, though, so the memory footprint of a projection table grows with the number of tree nodes in the store matching the pattern.

Projection ETS tables are owned by the Khepri cluster and are deleted when the cluster stops.

Updates to projection tables are immediately consistent for the member of the cluster on which the change to the store is made and the leader member but are eventually consistent for all other followers.

Link to this section Summary

Types

Options which control the created ETS table.

A projection resource.

A function that formats an entry in the tree into a record to be stored in a projection.

Link to this section Types

-type options() ::
    #{type => ets:table_type(),
      keypos => pos_integer(),
      read_concurrency => boolean(),
      write_concurrency => boolean() | auto,
      compressed => boolean()}.

Options which control the created ETS table.

These options are a subset of the options available to ets:new/2. Refer to the ets:new/2 documentation for a reference on each type and available values.

When a projection is created from a simple_projection_fun(), the type option may only be set or ordered_set: bag types are not allowed. extended_projection_fun()s may use any valid ets:table_type().
Link to this type

projection/0

-type projection() :: #khepri_projection{}.
A projection resource.
Link to this type

projection_fun/0

-type projection_fun() :: simple_projection_fun() | extended_projection_fun().

A function that formats an entry in the tree into a record to be stored in a projection.

Projection functions may either be "simple" or "extended." See simple_projection_fun() and extended_projection_fun() for more information.

The projection function is executed directly by the Ra server process. The function should be as simple and fast as possible to avoid slowing down the server.

Link to this section Functions

Link to this function

init(Projection)

-spec init(Projection) -> Ret when Projection :: projection(), Ret :: ok | {error, exists}.
Link to this function

name(Projection)

-spec name(Projection) -> Name when Projection :: projection(), Name :: atom().
Returns the name of the given Projection.
Link to this function

new(Name, ProjectionFun)

-spec new(Name, ProjectionFun) -> Projection
       when
           Name :: atom(),
           ProjectionFun :: khepri_projection:projection_fun(),
           Projection :: khepri_projection:projection().

See also: khepri:register_projection/4, khepri_projection:new/3.

Link to this function

new(Name, ProjectionFun, Options)

-spec new(Name, ProjectionFun, Options) -> Projection
       when
           Name :: atom(),
           ProjectionFun :: khepri_projection:projection_fun(),
           Options :: khepri_projection:options(),
           Projection :: khepri_projection:projection().

Creates a new projection data structure.

This function throws an error in the shape of {unexpected_option, Key, Value} when an unknown or invalid option is passed (see options()). For example, if the passed ProjectionFun is a simple_projection_fun() and the Options map sets the type to bag, this function throws {unexpected_option, type, bag} since bag is not valid for simple projection funs.
Link to this function

trigger(Projection, Path, OldProps, NewProps)

-spec trigger(Projection, Path, OldProps, NewProps) -> Ret
           when
               Projection :: projection(),
               Path :: khepri_path:native_path(),
               OldProps :: khepri:node_props(),
               NewProps :: khepri:node_props(),
               Ret :: ok.