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 function that formats an entry in the tree into a record to be stored in a projection.
Link to this section Types
options/0
-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.
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()
.
projection/0
-type projection() :: #khepri_projection{}.
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.
Link to this section Functions
init(Projection)
-spec init(Projection) -> Ret when Projection :: projection(), Ret :: ok | {error, exists}.
name(Projection)
-spec name(Projection) -> Name when Projection :: projection(), Name :: atom().
Projection
.
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.
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.
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.