# Software Architecture The software is architected into three objects: ```js var {create_antimatter_crdt, create_json_crdt, sequence_crdt} = require('@braidjs/antimatter') ``` - *antimatter_crdt*: created using `create_antimatter_crdt`, this object is a json_crdt with antimatter algorithm methods added to it so that it can communicate with other peers to learn which history can be pruned, and tells the underlying json_crdt object to prune it. - *json_crdt*: created using `create_json_crdt`, this object is a pruneable JSON CRDT — "JSON" meaning it represents an arbitrary JSON datstructure, and "CRDT" and "pruneable" having the same meaning as for sequence_crdt below. The json_crdt makes recursive use of sequence_crdt structures to represent arbitrary JSON (for instance, a map is represented with a sequence_crdt structure for each value, where the first element in the sequence is the value). - *sequence_crdt*: methods to manipulate a pruneable sequence CRDT — "sequence" meaning it represents a javascript string or array, "CRDT" meaning this structure can be merged with other ones, and "pruneable" meaning that it supports an operation to remove meta-data when it is no longer needed (whereas CRDT's often keep track of this meta-data forever). # create_antimatter_crdt(send[, init]) Creates and returns a new antimatter_crdt object (or adds antimatter_crdt methods and properties to `init`). * `send`: A callback function to be called whenever this antimatter_crdt wants to send a message over a connection registered with `get` or `connect`. The sole parameter to this function is a JSONafiable object that hopes to be passed to the `receive` method on the antimatter_crdt object at the other end of the connection specified in the `conn` key. * `init`: (optional) An antimatter_crdt object to start with, which we'll add any properties to that it doesn't have, and we'll add all the antimatter_crdt methods to it. This option exists so you can serialize an antimatter_crdt instance as JSON, and then restore it later. ``` js var antimatter_crdt = create_antimatter_crdt(msg => { websockets[msg.conn].send(JSON.stringify(msg)) }, JSON.parse(fs.readFileSync('./antimatter.backup'))) ``` # antimatter_crdt.receive(message) Let this antimatter object "receive" a message from another antimatter object, presumably from its `send` callback. ``` js websocket.on('message', data => { antimatter_crdt.receive(JSON.parse(data)) }); ``` You generally do not need to mess with a message object directly, but below are the various message objects you might see, categorized by their `cmd` entry. Note that each object also contains a `conn` entry with the id of the connection the message is sent over. ## message `get` `get` is the first message sent over a connection, and the peer at the other end will respond with `welcome`. ``` js { cmd: 'get', peer: 'SENDER_ID', conn: 'CONN_ID', parents: {'PARENT_VERSION_ID': true, ...} } ``` The `parents` are optional, and describes which versions this peer already has. The other end will respond with versions since that set of parents. ## message `forget` Used to disconnect without creating a fissure, presumably meaning the sending peer doesn't plan to make any edits while they're disconnected. ``` js {cmd: 'forget', conn: 'CONN_ID'} ``` ## message forget `ack` Sent in response to `forget`.. so they know we forgot them. ``` js {cmd: 'ack', forget: true, conn: 'CONN_ID'} ``` ## message `fissure` Sent to alert peers about a fissure. The `fissure` entry contains information about the two peers involved in the fissure, the specific connection id that broke, the `versions` that need to be protected, and the `time` of the fissure (in case we want to ignore it after some time). It is also possible to send multiple `fissures` in an array. ``` js { cmd: 'fissure', fissure: { // or fissures: [{...}, {...}, ...], a: 'PEER_A_ID', b: 'PEER_B_ID', conn: 'CONN_ID', versions: {'VERSION_ID': true, ...}, time: Date.now() }, conn: 'CONN_ID' } ``` ## message `set` Sent to alert peers about a change in the document. The change is represented as a version, with a unique id, a set of parent versions (the most recent versions known before adding this version), and an array of patches, where the offsets in the patches do not take into account the application of other patches in the same array. ``` js { cmd: 'set', version: 'VERSION_ID', parents: {'PARENT_VERSION_ID': true, ...}, patches: [ {range: '.json.path.a.b', content: 42}, ... ], conn: 'CONN_ID' } ``` ## message local `ack` Sent in response to `set`, but not right away; a peer will first send the `set` over all its other connections, and only after they have all responded with a local `ack` – and we didn't see a `fissure` message while waiting – will the peer send a local `ack` over the originating connection. ``` js {cmd: 'ack', seen: 'local', version: 'VERSION_ID', conn: 'CONN_ID'} ``` ## message global `ack` Sent after an originating peer has received a local `ack` over all its connections, or after any peer receives a global `ack`, so that everyone may come to know that this version has been seen by everyone in this peer group. ``` js {cmd: 'ack', seen: 'global', version: 'VERSION_ID', conn: 'CONN_ID'} ``` ## message `welcome` Sent in response to a `get`, basically contains the initial state of the document; incoming `welcome` messages are also propagated over all our other connections (but only with information that was new to us, so that the propagation will eventually stop). When sent in response to a `get` (rather than being propogated), we include a `peer` entry with the id of the sending peer, so they know who we are, and to trigger them to send us their own `welcome` message. ``` js { cmd: 'welcome', versions: [each version looks like a set message...], fissures: [each fissure looks as it would in a fissure message...], parents: {'PARENT_VERSION_ID': true, ...versions you must have before consuming these new versions}, [ peer: 'SENDER_ID', ] // if sent in response to a get conn: 'CONN_ID' } ``` # antimatter_crdt.get(conn) or connect(conn) Register a new connection with id `conn` – triggers this antimatter_crdt object to send a `get` message over the given connection. ``` js alice_antimatter_crdt.get('connection_to_bob') ``` # antimatter_crdt.forget(conn) Disconnect the given connection without creating a fissure – we don't need to reconnect with them.. it seems.. if we do, then we need to call `disconnect` instead, which will create a fissure allowing us to reconnect. ``` js alice_antimatter_crdt.forget('connection_to_bob') ``` # antimatter_crdt.disconnect(conn) If we detect that a connection has closed, let the antimatter_crdt object know by calling this method with the given connection id – this will create a fissure so we can reconnect with whoever was on the other end of the connection later on. ``` js alice_antimatter_crdt.disconnect('connection_to_bob') ``` # antimatter_crdt.set(...patches) Modify this antimatter_crdt object by applying the given patches. Each patch looks like `{range: '.life.meaning', content: 42}`. Calling this method will trigger calling the `send` callback to let our peers know about this change. ``` js antimatter_crdt.set({range: '.life.meaning', content: 42}) ``` --- # create_json_crdt([init]) Create a new `json_crdt` object (or start with `init`, and add stuff to that). ``` js var json_crdt = create_json_crdt() ``` # json_crdt.read() Returns an instance of the `json` object represented by this json_crdt data-structure. ``` js console.log(json_crdt.read()) ``` # json_crdt.generate_braid(versions) Returns an array of `set` messages that each look like this: `{version, parents, patches, sort_keys}`, such that if we pass all these messages to `antimatter_crdt.receive()`, we'll reconstruct the data in this `json_crdt` datastructure, assuming the recipient already has the given `versions` (which is represented as an object where each key is a version, and each value is `true`). ``` js json_crdt.generate_braid({alice2: true, bob3: true}) ``` # json_crdt.apply_bubbles(to_bubble) This method helps prune away meta data and compress stuff when we have determined that certain versions can be renamed to other versions – these renamings are expressed in `to_bubble`, where keys are versions and values are "bubbles", each bubble represented with an array of two elements, the first element is the "bottom" of the bubble, and the second element is the "top" of the bubble; "bottom" and "top" make sense when viewing versions in a directed graph with the oldest version(s) at the top, and each version pointing up to it's parents. A bubble is then a set of versions where the only arrows leaving the bubble upward are from the "top" version, and the only arrows leaving the bubble downward are from the "bottom" version. This method effectively combines all the versions in a bubble into a single version, and may allow the data structure to be compressed, since now we don't need to distinguish between certain versions that we used to need to. ``` js json_crdt.apply_bubbles({alice4: ['bob5', 'alice4'], bob5: ['bob5', 'alice4']}) ``` # json_crdt.add_version(version, parents, patches[, sort_keys]) The main method for modifying a `json_crdt` data structure. * `version`: Unique string associated with this edit. * `parents`: A set of versions that this version is aware of, represented as a map with versions as keys, and values of `true`. * `patches`: An array of patches, where each patch is an object like this `{range: '.life.meaning', content: 42}`. * `sort_keys`: (optional) An object where each key is an index, and the value is a sort_key to use with the patch at the given index in the `patches` array – a sort_key overrides the version for a patch for the purposes of sorting. This can be useful after doing some pruning. ``` js json_crdt.add_version('alice6', {alice5: true, bob7: true}, [{range: '.a.b', content: 'c'}]) ``` # json_crdt.get_child_map() Returns a map where each key is a version, and each value is a set of child versions, represented as a map with versions as keys, and values of `true`. ``` js json_crdt.get_child_map() ``` # json_crdt.ancestors(versions, ignore_nonexistent=false) Gather `versions` and all their ancestors into a set. `versions` is a set of versions, i.e. a map with version-keys and values of true – we'll basically return a larger set. If `ignore_nonexistent` is `true`, then we won't throw an exception if we encounter a version that we don't have in our datastructure. ``` js json_crdt.ancestors({alice12: true, bob10: true}) ``` # json_crdt.descendants(versions, ignore_nonexistent=false) Gather `versions` and all their descendants into a set. `versions` is a set of versions, i.e. a map with version-keys and values of true – we'll basically return a larger set. If `ignore_nonexistent` is `true`, then we won't throw an exception if we encounter a version that we don't have in our datastructure. ``` js json_crdt.descendants({alice12: true, bob10: true}) ``` # json_crdt.get_leaves(versions) Returns a set of versions from `versions` which don't also have a child in `versions`. `versions` is itself a set of versions, represented as an object with version keys and `true` values, and the return value is represented the same way. # json_crdt.parse_patch(patch) Takes a patch in the form `{range, content}`, and returns an object of the form `{path: [...], [slice: [...]], [delete: true], content}`; basically calling `parse_json_path` on `patch.range`, and adding `patch.content` along for the ride. # json_crdt.parse_json_path(json_path) Parses the string `json_path` into an object like: `{path: [...], [slice: [...]], [delete: true]}`. * `a.b[3]` --> `{path: ['a', 'b', 3]}` * `a.b[3:5]` --> `{path: ['a', 'b'], slice: [3, 5]}` * `delete a.b` --> `{path: ['a', 'b'], delete: true}` ``` js console.log(json_crdt.parse_json_path('a.b.c')) ``` --- # sequence_crdt.create_node(version, elems, [end_cap, sort_key]) Creates a node for a `sequence_crdt` sequence CRDT with the given properties. The resulting node will look like this: ``` js { version, // globally unique string elems, // a string or array representing actual data elements of the underlying sequence end_cap, // this is useful for dealing with replace operations sort_key, // version to pretend this is for the purposes of sorting deleted_by : {}, // if this node gets deleted, we'll mark it here nexts : [], // array of nodes following this one next : null // final node following this one (after all the nexts) } var sequence_node = sequence_crdt.create_node('alice1', 'hello') ``` # sequence_crdt.generate_braid(root_node, version, is_anc) Reconstructs an array of splice-information which can be passed to `sequence_crdt.add_version` in order to add `version` to another `sequence_crdt` instance – the returned array looks like: `[[insert_pos, delete_count, insert_elems, sort_key], ...]`. `is_anc` is a function which accepts a version string and returns `true` if and only if the given version is an ancestor of `version` (i.e. a version which the author of `version` knew about when they created that version). ``` js var root_node = sequence_crdt.create_node('alice1', 'hello') console.log(sequence_crdt.generate_braid(root_node, 'alice1', x => false)) // outputs [0, 0, "hello"] ``` # sequence_crdt.apply_bubbles(root_node, to_bubble) This method helps prune away meta data and compress stuff when we have determined that certain versions can be renamed to other versions – these renamings are expressed in `to_bubble`, where keys are versions and values are "bubbles", each bubble represented with an array of two elements, the first element is the "bottom" of the bubble, and the second element is the "top" of the bubble. We will rename the given version to the "bottom" of the bubble. "bottom" and "top" make sense when viewing versions in a directed graph with the oldest version(s) at the top, and each version pointing up to it's parents. A bubble is then a set of versions where the only arrows leaving the bubble upward are from the "top" version, and the only arrows leaving the bubble downward are from the "bottom" version. This method effectively combines all the versions in a bubble into a single version, and may allow the data structure to be compressed, since now we don't need to distinguish between certain versions that we used to need to. ``` js sequence_crdt.apply_bubbles(root_node, {alice4: ['bob5', 'alice4'], bob5: ['bob5', 'alice4']}) ``` # sequence_crdt.get(root_node, i, is_anc) Returns the element at the `i`th position (0-based) in the `sequence_crdt` rooted at `root_node`, when only considering versions which result in `true` when passed to `is_anc`. ``` js var x = sequence_crdt.get(root_node, 2, {alice1: true}) ``` # sequence_crdt.set(root_node, i, v, is_anc) Sets the element at the `i`th position (0-based) in the `sequence_crdt` rooted at `root_node` to the value `v`, when only considering versions which result in `true` when passed to `is_anc`. ``` js sequence_crdt.set(root_node, 2, 'x', {alice1: true}) ``` # sequence_crdt.length(root_node, is_anc) Returns the length of the `sequence_crdt` rooted at `root_node`, when only considering versions which result in `true` when passed to `is_anc`. ``` js console.log(sequence_crdt.length(root_node, {alice1: true})) ``` # sequence_crdt.break_node(node, break_position, end_cap, new_next) This method breaks apart a `sequence_crdt` node into two nodes, each representing a subsequence of the sequence represented by the original node; the `node` parameter is modified into the first node, and the second node is returned. The first node represents the elements of the sequence before `break_position`, and the second node represents the rest of the elements. If `end_cap` is truthy, then the first node will have `end_cap` set – this is generally done if the elements in the second node are being replaced. This method will add `new_next` to the first node's `nexts` array. ``` js var node = sequence_crdt.create_node('alice1', 'hello') // node node.elems == 'hello' var second = sequence_crdt.break_node(node, 2) // now node.elems == 'he', // and second.elems == 'llo' ``` # sequence_crdt.add_version(root_node, version, splices, [is_anc]) This is the main method of `sequence_crdt`, used to modify the sequence. The modification must be given a unique `version` string, and the modification itself is represented as an array of `splices`, where each splice looks like this: `[position, num_elements_to_delete, elements_to_insert, optional_sort_key]`. Note that all positions are relative to the original sequence, before any splices have been applied. Positions are counted by only considering nodes with versions which result in `true` when passed to `is_anc` (and are not `deleted_by` any versions which return `true` when passed to `is_anc`). ``` js var node = sequence_crdt.create_node('alice1', 'hello') sequence_crdt.add_version(node, 'alice2', [[5, 0, ' world']], null, v => v == 'alice1') ``` # sequence_crdt.traverse(root_node, is_anc, callback, [view_deleted, tail_callback]) Traverses the subset of nodes in the tree rooted at `root_node` whos versions return `true` when passed to `is_anc`. For each node, `callback` is called with these parameters: `node, offset, has_nexts, prev, version, deleted`, where `node` is the current node being traversed; `offset` says how many elements we have passed so far getting here; `has_nexts` is true if some of this node's `nexts` will be traversed according to `is_anc`; `prev` is a pointer to the node whos `next` points to this one, or `null` if this is the root node; `version` is the version of this node, or this node's `prev` if our version is `null`, or that node's `prev` if it is also `null`, etc; `deleted` is true if this node is deleted according to `is_anc` (usually we skip deleted nodes when traversing, but we'll include them if `view_deleted` is `true`). `tail_callback` is an optional callback that will get called with a single parameter `node` after all of that node's children `nexts` and `next` have been traversed. ``` js sequence_crdt.traverse(node, () => true, node => process.stdout.write(node.elems)) ```