0.0.3 • Published 5 years ago

crosstree v0.0.3

Weekly downloads
1
License
MIT
Repository
-
Last release
5 years ago

_:{ CrossTree

Compact, dependency-free, data synchronisation library for TypeScript and JavaScript.

What is it?

CrossTree provides a simple mechanism for conflict-free synchronisation of tree structures that is commutative, associative, and idempotent. It's transport independent, so you can synchronise changes over HTTP, WebSockets, peer-to-peer.

Data Structure

CrossTree data is made up of entry nodes. Every entry node can have sub-entries (a.k.a. children). There is a single root entry.

Every entry can have a:

  • value - v attribute
  • counter - c used to indicate the entry's version count or used as a numeric value.
  • timestamp - t when the entry was modified according to the local system clock (see exceptions below).
  • deletion flag - d means the entry was marked as deleted. Entries with no value often contain subentries, while entries with an explicit deletion flag can be ignored when traversing the tree.

Subentries must have an:

  • id - which is used to identify a subentry, but is not part of the entry's state.

Examples:

  • Use setValue on a tree path:
const treeA = new CrossTree();
treeA.subpath(["alice", "firstName"]).setValue("Alice");
treeA.subpath(["alice", "lastName"]).setValue("Bloggs");
let aliceData = treeA.toData();

aliceData ->

{
  _: {
    alice: {
      _: {
        firstName: { v: "Alice", t: 1572130604728 },
        lastName: { v: "Bloggs", t: 1572130604728 }
      }
    }
  }
}
  • Merge data with another replica:
const treeB = new CrossTree({ _: { bob: { _: { firstName: { v: "Bob", t: 1571241715839 } } } } });
treeB.mergeData(aliceData); // receive Alice's data
treeB.subpath(["alice", "firstName"]).getValue(); // -> Alice
let bobData = treeB.toData();

bobData ->

{
  _: {
    bob: {
      _: {
        firstName: { v: "Bob", t: 1571241715839 }
      }
    },
    alice: {
      _: {
        firstName: { v: "Alice", t: 1572130604728 },
        lastName: { v: "Bloggs", t: 1572130604728 }
      }
    }
  }
}
  • Send changes by observing tree:
treeA.observeTree( (event) => {
    const { change, before, after } = event;
    sendToBob( change );
    // change.data - new entry data
    // change.path - path of changed entry
});

Use Cases

  • File Structure
{ v: "My Repo"
  _: {
   "README.md": { v: "blob:1234", t: 1571200000000  },
   "package.json": { v: "blob:9876", t: 1572300000000 },
   "src": {
      v:"dir", t: 1573400000000,
      _:{
          "index.ts": { v: "text:hello", t: 1574500000000 }
      }
   }
}}
  • Records
{ _:{
  "6fa459ea": {
    _:{
      "name": { v: "Alice", t: 1571200000000 },
      "type": { v: "admin", t: 1571200000000 },
    }
  },
  "ee8a3ca4": {
    _:{
      "name": { v: "Bob", t: 1574500000000 },
      "type": { v: "guest", t: 1574500000000 },
    }
  },
}}
  • Spreadsheet
{ v: "Sheet 1"
  _: {
   "A1": { v: "Column 1", t: 1571200000000 },
   "A2": { v: "613423", t: 1572300000000 },
   "A3": { v: "875243", t: 1572300000000 },
}}

Merge Rules

Every value change requires a new entry state, which is merged into the local replica and can be sent to other replicas. At each tree path, the entry states are ordered and the "greatest" entry overwrites any previous entry. Thus a local value change needs to generate an entry state "greater" than the previous state. Once the new entry is merged into the tree, the old entry is discarded.

States are ordered by:

  • timestamp (t)
  • counter (c)
  • value (v)

A new entry state (a) is considered greater than (b) when:

  • a.t > b.t - its timestamp is greater or when:
  • (a.t == b.t && a.c > b.c) - timestamps are equal, but its counter value is greater or when:
  • (a.t == b.t && a.c == b.c && a.v > b.v) - timestamps and counters are equal, but its value is greater

Union Properties

Sample data:

A={_:{"alice":{t:15000,v:"red"}}}

B={_:{"alice":{t:16500,v:"green"}}}

C={_:{"bob":{t:15000,v:"blue"}}}

  • Commutativity: The order of changes sent betwen CrossTree instances doesn't impact the resulting state.

A∪B=B∪A={_:{"alice":{t:16500,v:"green"}}} - B's timestamp is larger, thus it overwrites A

  • Idempotence: Changes or whole replicas can be re-merged multiple times without duplication.

A∪A=A={_:{"alice":{t:15000,v:"red"}}} - two identical records of "alice" merge into one.

  • Associativity: Changes can be applied opportunistically as the network allows or on demand. The timing or order will not impact the resulting state.

(A∪B)∪C=A∪(B∪C)={_:{"alice":{t:16500,v:"green"},"bob":{t:15000,v:"blue"}}}

Three-way Merge

When synchronising with a remote replica, it's still possible to perform a manual three-way merge, as long as you retain the last state sent to the server (common ancestor), compare it with the local state and the remote state. However, whenever possible, you should design conflict-free structures, by putting conflict prone values into separate entry paths and deriving the final information. For more examples see this article.

Limitations

  • Node ID needs to be a string compatible with JavaScript's Object keys, as entry data is stored internally using Object.
  • Data passed to the CrossTree constructor (new CrossTree(data)) is mutated, when CrossTree is performing writes. If you don't want CrossTree to mutate the passed data object, use the tree.mergeData(data) method on an empty tree or perform a deep-clone before passing it to the constructor.

Related

https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type https://medium.com/bpxl-craft/building-a-peer-to-peer-whiteboarding-app-for-ipad-2a4c7728863e

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago