• Stars
    star
    353
  • Rank 120,322 (Top 3 %)
  • Language
    JavaScript
  • Created over 11 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

JSON Operational Transformation (JOT)

JSON Operational Transformation (JOT)

By Joshua Tauberer https://razor.occams.info.

August 2013.

License: GPL v3 http://choosealicense.com/licenses/gpl-v3/

This module implements operational transformation (OT) on a JSON data model, written in JavaScript for use either in node.js or browsers.

While most collaborative editing models operate on plain text documents with operations like insert and delete on strings, the document model in JOT is JSON --- i.e. the value space of null, booleans, numbers, strings, arrays, and objects (key-value pairs with string keys). JOT includes the basic insert/delete operations on strings but adds many other operations that make JOT useful for tracking changes to any sort of data that can be encoded in JSON.

Basically, this is the core of real time simultaneous editing, like Etherpad, but for structured data rather than just plain text. Since everything can be represented in JSON, this provides plain text collaboration functionality and much more.

This is a work in progress. There is no UI or collaboration framework here.

Why JOT?

Introduction

The core problem addressed by operational transformation libraries like JOT is merging edits made simultaneously, i.e. asynchronously, by two or more users, and the handling of potential conflicts that arise when multiple users edit the same part of the document.

To illustrate the problem, imagine two users open the following JSON document:

{ "title": "Hello World!", "count": 10 }

Each user now has a copy of this document in their local memory. The first user modfies their copy by changing the title and incrementing the count:

{ "title": "It's a Small World!", "count": 20 }

At the same time, the second user changes their copy of the document by changing Hello World! to Hello, Small World! also incrementing the count by 5, yielding:

{ "title": "Hello, Small World!", "count": 15 }

Structured representation of changes

In order to merge these changes, there needs to be a structured representation of the changes. In the flat land of plain text, you are probably used to diffs and patches as structured representations of changes --- e.g. at lines 5 through 10, replace with new content. In JOT, it is up to the library user to form structured representations of changes using JOT's classes. The changes in the example above are constructed as:

var user1 = new jot.LIST([
	new jot.APPLY("title", new jot.SPLICE(0, 5, "It's a Small")),
	new jot.APPLY("count", new jot.MATH("add", 10))
]);

var user2 = new jot.LIST([
	new jot.APPLY("title", new jot.SPLICE(5, 1, ", Small ")),
	new jot.APPLY("count", new jot.MATH('add', 5))
]);

In other words, user 1 makes a change to the title property by replacing the 5 characters starting at position 0 with It's a Small and increments the count property by 10. User 2 makes a change to the title property by replacing the 1 character at position 5, i.e. the first space, with , Small and increments the count property by 5.

These changes cannot yet be combined. If they were applied in order we would get a corrupted document because the character positions that user 2's operation referred to are shifted once user 1's changes are applied. After applying user 1's changes, we have the document:

{ title: "It's a Small World!", count: 20 }

But then if we apply user 2's changes, which say to replace the character at position 5, we would get:

{ title: "It's , Small  Small World!", count: 25 }

That's not what user 2 intended. The second user's changes must be "transformed" to take into account the changes to the document made by the first user before they can be applied.

Transformation

JOT provides an algorithm to transform the structured representation of changes so that simultaneous changes can be combined sequentially.

Continuing the example, we desire to transform the second user's changes so that they can be applied in sequence after the first user's changes.

Instead of

... new jot.APPLY("title", new jot.SPLICE(5, 1, ", Small "))  ...

we want the second user's changes to look like

... new jot.APPLY("title", new jot.SPLICE(12, 1, ", Small "))  ...

Note how the character index has changed. These changes now can be applied after the first user's changes and achieve the intent of user 2's change.

JOT provides a rebase function on operation objects that can make this transformation. (The transformation is named after git's rebase.) The rebase function transforms the operation and yields a new operation that should be applied instead, taking as an argument the operations executed by another user concurrently that have already applied to the document:

user2 = user2.rebase(user1)

These changes can now be merged using compose:

var all_changes = user1.compose(user2);

and then applied to the base document:

document = all_changes.apply(document)

after which the base document will include both user's changes:

{ title: "It's a Small, Small World!", count: 25 }

It would also have been possible to rebase user1 and then compose the operations in the other order, for the exact same result.

See example.js for the complete example.

Compared to other OT libraries

Operational transformation libraries often operate only over strings. JOT has those operations too. For instance, start with the document:

Hello world!

Two simultaneous changes might be:

User 1: REPLACE CHARS 0-4 WITH "Brave new"

User 2: REPLACE CHARS 11-11 WITH "."

To merge these changes, the second user's changes must be rebased to:

User 2: REPLACE CHARS 15-15 WITH "."

JOT's rebase algorithm can handle this case too:

// Construct operations
var document = "Hello world!";
var user1 = new jot.SPLICE(0, 5, "Brave new");
var user2 = new jot.SPLICE(11, 1, ".");

// Rebase user 2
user2 = user2.rebase(user1, { document: document })

// user2 now holds:
// new jot.SPLICE(15, 1, ".")

// Merge
user1.compose(user2).apply(document);
> 'Brave new world.'

Unlike most collaborative editing models where there are only operations like insert and delete that apply to strings, the document model in JOT is JSON --- i.e. the value space of null, booleans, numbers, strings, arrays, and objects (key-value pairs with string keys). Operations are provided that manipulate all of these data types. This makes JOT useful when tracking changes to data, rather than simply to plain text.

Installation

The code is written for the node.js platform and can also be used client-side in modern browsers.

First install node, then install this package:

npm install git+https://github.com/joshdata/jot.git

In a node script, import the library:

var jot = require("jot");

To build the library for browsers, run:

npm install -g browserify
browserify browser_example/browserfy_root.js -d -o dist/jot.js

Then use the library in your HTML page (see the example for details):

<html>
	<body>
		<script src="jot.js"></script>
		<script>
			// see the example below, but skip the 'require' line
		</script>
	</body>
</html>

Operations

The operations in JOT are instantiated as new jot.OPERATION(arguments). The available operations are...

General operations

  • SET(new_value): Replaces any value with any other JSON-able value. new_value is the new value after the operation applies.
  • LIST([op1, op2, op3, ...]): Executes a series of operations in order. op1, op2, op3, ... are other JOT operations. Equivalent to op1.compose(op2).compose(op3)....

Operations on booleans and numbers

  • MATH(op, value): Applies an arithmetic or boolean operation to a value. op is one of "add", "mult" (multiply), "rot" (increment w/ modulus), "and" (boolean or bitwise and), "or" (boolean or bitwise or), "xor" (boolean or bitwise exclusive-or), "not" (boolean or bitwise negation). For rot, value is given as an array of [increment, modulus]. For not, value is ignored and should be null. add and mult apply to any number, rot applies to integers only, and the boolean/bitwise operations only apply to integers and booleans. Because of rounding, operations on floating-point numbers or with floating-point operands could result in inconsistent state depending on the order of execution of the operations.

Operations on strings and arrays

The same operation is used for both strings and arrays:

  • SPLICE(index, length, new_value): Replaces text in a string or array elements in an array at the given index and length in the original. To delete, new_value should be an empty string or zero-length array. To insert, length should be zero.
  • ATINDEX(index, operation): Apply any operation to a particular array element at index. operation is any operation. Operations at multiple indexes can be applied simultaneously using ATINDEX({ index1: op1, index2: op2, ... }).
  • MAP(operation): Apply any operation to all elements of an array (or all characters in a string). operation is any operation created by these constructors.

SPLICE is the only operation you need for basic plain text concurrent editing. JOT includes the entire text editing model in the SPLICE operations plus it adds new operations for non-string data structures!

(Note that internally SPLICE and ATINDEX are sub-cases of an internal PATCH operation that maintains an ordered list of edits to a string or array.)

Operations on objects

  • PUT(key, value): Adds a new property to an object. key is any valid JSON key (a string) and value is any valid JSON object. Equivalent to APPLY(key, SET(value)).
  • REM(key): Remove a property from an object. Equivalent to APPLY(key, SET(~)) where ~ is a special internal value.
  • APPLY(key, operation): Apply any operation to a particular property named key. operation is any operation. The operation can also take a mapping from keys to operations, as APPLY({key: operation, ...}).

Operations that affect document structure

  • COPY([ [source1, target1], [source2, target2], ... ]): Copies a value from one location in the document to another. The source and target parameters are JSON Pointer strings (but /- is not allowed). Use in combination with other operations to move parts of the document, e.g. a COPY plus a REM can be used to rename an object property.

Methods

Instance Methods

Each operation object provides the following instance methods:

  • op.inspect() returns a human-readable string representation of the operation. (A helper method so you can do console.log(op).)
  • op.isNoOp() returns a boolean indicating whether the operation does nothing.
  • op.apply(document) applies the operation to the document and returns the new value of the document. Does not modify document.
  • op.simplify() attempts to simplify complex operations. Returns a new operation or the operation unchanged. Useful primarily for LISTs.
  • op.drilldown(index_or_key) looks inside an operation on a string, array, or object and returns the operation that represents the effect of this operation on a particular index or key.
  • op.inverse(document) returns the inverse operation, given the document value before the operation applied.
  • op.compose(other) composes two operations into a single operation instance, sometimes a LIST operation.
  • op.rebase(other) rebases an operation. Returns null if the operations conflict, otherwise a new operation instance.
  • op.rebase(other, { document: ... }) rebases an operation in conflictless mode. The document value provided is the value of the document before either operation applied. Returns a new operation instance. See further documentation below.
  • op.toJSON() turns the operation into a JSON-able data structure (made up of objects, arrays, strings, etc). See jot.opFromJSON(). (A helper method so you can do JSON.stringify(op).)
  • op.serialize() serializes the operation to a string. See jot.deserialize().

Global Methods

The jot library itself offers several global methods:

  • jot.diff(a, b, options) compares two documents, a and b, and returns a JOT operation that when applied to a gives b. options, if given, is an object that controls how the diff is performed. Any data type that can be a JOT document (i.e. any JSON-like data type) can be compared, and the comparison follows the document structure recursively. If the keys words, lines, or sentences is set to a truthy value, then strings are compared word-by-word, line-by-line, or sentence-by-sentence, instead of character-by-character. There is no general purpose structured diff algorithm that works well on all documents --- this one probably works fine on relatively small structured data.
  • jot.opFromJSON(opdata) is the inverse of op.toJSON().
  • jot.deserialize(string) is the inverse of op.serialize().

Conflictless Rebase

What makes JOT useful is that each operation knows how to "rebase" itself against every other operation. This is the "transformation" part of operational transformation, and it's what you do when you have two concurrent edits that need to be merged.

The rebase operation guarantees that any two operations can be combined in any order and result in the same document. In other words, rebase satisfies the constraints A â—‹ (B/A) == B â—‹ (A/B) and C / (Aâ—‹B) == (C/A) / B, where â—‹ is compose and / is rebase.

Rebase conflicts

In general, not all rebases are possible in a way that preserves the logical intent of each change. This is what results in a merge conflict in source code control software like git. The conflict indicates where two operations could not be merged without losing the logical intent of the changes and intervention by a human is necessary. rebase will return null in these cases.

For example, two MATH operations with different operators will conflict because the order that these operations apply is significant:

> new jot.MATH("add", 1)
    .rebase( new jot.MATH("mult", 2) )
null

(10 + 1) * 2 = 22 but (10 * 2) + 1 == 21. A vanilla rebase will return null in this case to signal that human intervention is needed to choose which operation should apply first.

Using conflictless rebase

However, JOT provides a way to guarantee that rebase will return some operation, so that a merge conflict cannot occur. We call this "conflictless" rebase. The result of a conflictless rebase comes close to preserving the logical intent of the operations by choosing one operation over the other or choosing an order that the operations will apply in.

To get a conflictless rebase, pass a second options argument to rebase with the document option set to the content of the document prior to both operations applying:

> new jot.MATH("add", 1)
    .rebase( new jot.MATH("mult", 2),
             { document: 10 } )
<values.SET 22>

The rebase returns a valid operation now, in this case telling us that to add 1 after the multiplication has applied, we should simply set the result to 22 instead of adding 1. In other words, the rebase has chosen the order where multiplication goes second.

Rebasing the other way around yields a consistent operation:

> new jot.MATH("mult", 2)
    .rebase( new jot.MATH("add", 1),
             { document: 10 } )
<values.MATH mult:2>

In other words, if we're multing by 2 after the addition has applied, we should continue to multiply by 2. That's the same order as rebase chose above.

Development and Testing

Run code coverage tests with npm test or:

npm test -- --coverage-report=html

Notes

Thanks to @konklone for some inspiration and the first pull request.

Similar work: ShareDB, ottypes/json0, Apache Wave (formerly Google Wave), Substance Operator (defunct).

More Repositories

1

python-email-validator

A robust email syntax and deliverability validation library for Python.
Python
1,095
star
2

pdf-diff

A PDF comparison utility in Python.
Python
446
star
3

pdf-redactor

A general purpose PDF text-layer redaction tool for Python 2/3.
Python
183
star
4

convert-outlook-msg-file

Python library to convert Microsoft Outlook .msg files to .eml/MIME message files.
Python
179
star
5

hackathon.guide

A logistics guide to running a successful hackathon.
HTML
176
star
6

rdfabout

Archival. Things I wrote about RDF from the mid-2000's. The validator is no longer maintained, sorry.
109
star
7

fast_diff_match_patch

Python package for Google's diff-match-patch native C++ implementation.
Python
73
star
8

crs-reports-website

The build process for EveryCRSReport.com.
Python
63
star
9

praat-py

From my PhD days: Praat-Py is a custom build of Praat, the computer program used by linguists for doing phonetic analysis on sound files, to allow for scripts to be written in the Python programming language, rather than in Praat's built-in language.
C
61
star
10

xml_diff

Compares two XML documents by diffing their text.
Python
40
star
11

why-use-cartograms

Analysis for a blog post on cartograms.
Python
29
star
12

party-platforms

The 2012 Democratic, Libertarian, and Republican Party platforms, plus every Democratic platform since 1840, cleaned up into nice XML.
26
star
13

parsey-mcparseface-server

[Archive] A simple Python Flask app to run Parsey McParseface.
Python
25
star
14

cmusphinx-alignment-example

How I got cmusphinx's transcript alignment tool to work.
Java
25
star
15

cartogrid

A grid-based cartogram generator.
Python
14
star
16

opengovdata.org

The website opengovdata.org.
CSS
14
star
17

globe-gores

Globe gores, in Javascript.
JavaScript
12
star
18

dc-code-editor

Prototype tool for editing the DC Code.
JavaScript
9
star
19

wmata-track-locations

WMATA Track Geospatial GIS Location Data
Python
9
star
20

dc-code-prototype

Unofficial Code of the District of Columbia in XML, produced under contract with the Council of the District of Columbia. Last updated in 2014.
7
star
21

crs-reports-scraper

Downloads Congressional Research Service (CRS) reports from the CRS.gov website (which is only visible from within the U.S. Capitol computer network).
HTML
7
star
22

thunderbird-spf

Archival: An anti-phishing/anti-spam Mozilla Thunderbird 3 extension for doing Sender Policy Framework (SPF) checks on incoming mail.
JavaScript
7
star
23

semweb-dotnet

Archival: A C#/.NET library for manipulating RDF. No longer in active development.
C#
6
star
24

s-p-500-simulator

Simulates an investor randomly choosing S&P 500 stocks.
Python
6
star
25

historical-state-population-csv

Historical Population of the U.S. States 1900-present in a CSV Spreadsheet
Python
6
star
26

django-annotator-store

A Django backend for okfn/annotator storage.
Python
6
star
27

printable-district-maps

High-resolution, print-quality congressional district maps and an example of loading Open Street Map (OSM) into Postgres.
Python
6
star
28

official.dccode.gov

The future website for https://official.dccode.gov.
Shell
5
star
29

nyc-traffic

An analysis of New York City traffic patterns on the arterial roads.
Python
5
star
30

color-scales

Color Scale Generator Using a Perceptually Valid Color Space
HTML
5
star
31

opengovdata.io

The website for my book, Open Government Data: The Book.
HTML
4
star
32

myhomepage

My (@JoshData's) homepage.
HTML
4
star
33

html5-stub

An HTML5/Bootstrap website template for starting new projects.
HTML
4
star
34

endsecretlaws

This is how I feel about surveillance.
CSS
3
star
35

infinite-tree

An infinite tree.
HTML
3
star
36

dchbx

DCHBX Health Exchange Plans
Python
3
star
37

exclusiveprocess

A simple Python 3 module for ensuring that your code does not execute concurrently in multiple processes, using POSIX file locking.
Python
3
star
38

django-pubmybook

A Django website for publishing a LaTeX book online in HTML.
Python
3
star
39

marcos

A generative model for natural language using a markov chain over syntactic relations, rather than serial order.
Python
3
star
40

wobblegram

A Python module to create a wigglegram, which is a sort of steeographic image, using a "MPO" file as input, which is created by some cameras.
Python
2
star
41

my2012district

The website my2012district.com, which helps U.S. voters find their new 2012 congressional district.
JavaScript
2
star
42

cotaskme

A task list where every task for you also appears "outgoing" on the task list of the person who requested the task. Based on an idea by Matthew Burton.
Python
2
star
43

datastore-loader

Utility script to load tabular data into the CKAN Datastore.
Python
2
star
44

dc.opendataday.org

The website for Open Data Day DC.
HTML
1
star
45

dc-bega-emails

Emails in 2017-2018 retreived through DC FOIA requests related to the Board of Ethics and Government Accountability's Office of Open Government.
1
star
46

JoshData

Config files for my GitHub profile.
1
star
47

apophenia-python

Python
1
star
48

census2000-to-rdf

(Archival) Perl script to turn the 2000 US Census into RDF.
Perl
1
star
49

dc-street-henge

Like Manhattanhenge but for the District of Columbia. For each day of the year identifies DC streets that line up with sunrise or sunset.
Python
1
star
50

battlelibs

A mad libs helper for Battledecks.
1
star
51

django-database-storage-backend

A Django 1.7-1.10 storages backend backed by your existing database.
Python
1
star
52

browser-padlock-guide

A Javascript library to render an example of a browser security padlock.
CSS
1
star
53

arfticle-three

Uhm. Too much time spent on this.
Python
1
star
54

py-fist-pump

Given 3D accelerometer data, compute the frequency of rhythmic motion and predict the next beat
Python
1
star
55

readlet

A bookmarklet that creates a Spritz speed-reading "reticule" for any web page you are viewing.
JavaScript
1
star
56

alexa-transit-times

An Alexa skill for getting the next WMATA Metro rail or bus times for your common trips.
JavaScript
1
star