ex_sync v0.0.2 ExSync

This module implements the main parts of the server-side flow of the Diff Sync algorithm.

The main function here is sync cycle, which does a full cycle given the object id, shadow, backup shadow, edits and the storage adapter.

Summary

Functions

Calculates the difference between the server shadow and the server doc

This function patches the server doc

This function patches the server_shadow or backup_shadow with edits

The Diff Cycle. Takes changes from the client (can be an empty list) and applies them to the shadow

Types

document :: map
error :: atom | map
id :: any

Functions

get_server_doc_edits(server_shadow, doc)

Calculates the difference between the server shadow and the server doc.

Returns the new edits as a list of one item as well as the server shadow with an updated server_version number (if applicable).

patch_server_doc(storage_adapter, id, edits)

Specs

patch_server_doc(atom, any, [ExSync.Edit.t]) ::
  {:ok, document} |
  {:error, error}

This function patches the server doc.

It expects a storage adapter that implements the ExSync.Storage behaviour, an id and a list of edits as ExSync.Edit.

It will use the get_and_update/2 function of the storage, passing it a function to apply the edits to the server doc. That way we can use locks in the function to ensure data consistency between reading and writing.

patch_shadows(server_shadow, backup_shadow, edits)

Specs

patch_shadows(ExSync.Shadow.t, ExSync.Shadow.t, [ExSync.Edit.t]) ::
  {:ok, ExSync.Shadow.t} |
  {:error, error}

This function patches the server_shadow or backup_shadow with edits.

Expects server_shadow and backup_shadow as type ExSync.Shadow, edits as a List of ExSync.Edit.

sync_cycle(id, shadow, backup_shadow, edits, storage)

Specs

sync_cycle(any, ExSync.Shadow.t, ExSync.Shadow.t, [ExSync.Edit.t], ExSync.Storage) ::
  {:ok, ExSync.Shadow.t, [ExSync.Edit.t]} |
  {:error, error}

The Diff Cycle. Takes changes from the client (can be an empty list) and applies them to the shadow.

Updates the doc using ExSync.Storage.get_and_update/2 and returns the new shadow as well as any edits between the shadow and the doc.