View Source Chord.Context.Manager (Chord v0.2.0)
Handles context synchronization, delta calculation, and backend interactions.
The Chord.Context.Manager
module provides the core functionality for managing contexts, calculating deltas, and interacting with storage backends. Designed for both in-memory and external backends, it offers a flexible and efficient system for state and delta management.
Features
- Context management: Retrieve, update, and delete contexts.
- Delta tracking: Automatically calculate and store deltas between updates.
- Synchronization: Sync contexts and deltas with clients based on their known versions.
- Exporting contexts: Enable exporting to external storage via configurable callbacks.
- Flexible backends: Supports ETS, Redis, and custom backend implementations.
- External integration: Fetch contexts from external storage when required.
This module serves as the backbone of Chord, enabling developers to implement real-time state and delta management with minimal boilerplate while retaining the flexibility to customize behavior through configuration.
Summary
Functions
Deletes the context and its associated deltas.
Exports the context for a given identifier to an external storage.
Retrieves the current context for a given identifier.
Restores a context from an external provider to the current backend.
Sets or updates the global context for a given identifier and calculates deltas.
Synchronizes the context for a specific identifier.
Partially updates the context for a given identifier and calculates deltas.
Functions
Deletes the context and its associated deltas.
Parameters
context_id
(any): The ID of the context to delete.
Returns
:ok
on success.{:error, term()}
on failure.
Exports the context for a given identifier to an external storage.
Parameters
context_id
(any): The ID of the context to export.
Returns
:ok
on success.{:error, term()}
on failure.
Retrieves the current context for a given identifier.
Parameters
context_id
(any): The ID of the context to be retrieved.
Returns
{:ok, map()}
if context exists.{:error, term()}
on failure.
Restores a context from an external provider to the current backend.
Parameters
context_id
(any): The ID of the context to restore.
Returns
{:ok, map()}
on success.{:error, term()}
on failure.
Sets or updates the global context for a given identifier and calculates deltas.
This function can be used to either set a new global context or update an existing one by providing the full updated context. It calculates the deltas between the previous and the new context automatically.
Parameters
context_id
(any): The unique identifier of the context to set or update.new_context
(map): The complete updated context to be stored.
Returns
{:ok, %{context: map(), delta: map()}}
on success.{:error, term()}
on failure.
@spec sync_context(context_id :: any(), client_version :: integer() | nil) :: {:full_context, map()} | {:delta, map()} | {:no_change, integer()} | {:error, term()}
Synchronizes the context for a specific identifier.
Determines whether the client receives the full context, deltas, or no changes based on the client's known version.
Parameters
context_id
(any): The ID of the context to synchronize.client_version
(integer | nil): The version of the context known to the client.
Returns
{:full_context, map()}
if the client should receive the full context.{:delta, map()}
if the client should receive deltas.{:no_change, integer()}
if the client has the latest version.
Partially updates the context for a given identifier and calculates deltas.
The provided fields in changes
are applied to the existing context. If a field in changes
does not exist in the current context, it will be added to the context.
Parameters
context_id
(any): The ID of the context to update.changes
(map): A map of fields to be updated. If a field is not present in the context, it will be added.
Returns
{:ok, %{context: map(), delta: map()}}
on success.{:error, term()}
on failure.