Example on Operational Transformations
In this article, we use the last example of Code Commit’s blog post in order to clarify it with specific operations.
Client and Server initial revision is 0, and the document state is empty.
Client perspective
- Alice types “Wave”.
- Client sends InsertCharacters(“Wave”);, rev 0. (a). Document state = “Wave”.
- Alice types “!”.
- Client holds retain(4); InsertCharacters(“!!”);, rev 1. (b). Document state = “Wave!!”.
- Client receives InsertCharacters(“World”);, rev 0. (c)
- Client applies InsertCharacters(“World”); retain(6);, rev 2. (c’). Document state = “WorldWave!!
- Alice deletes “World”.
- Client applies DeleteCharacters(“World”); retain(6) , rev 3. (e). Document state = “Wave!!”
- Client holds DeleteCharacters(“World”); retain(4); InsertCharacters(“!!”);, rev 4. ((e⦁b’)’)
- Client receives InsertCharacters(“Hello “); retain(5);, rev 1. (d).
- Client applies InsertCharacters(“Hello “); retain(6);, rev 4. (d’). Document state = “Hello Wave!!”.
- Client receives ACK InsertCharacters(“Wave”);, rev 2. (a’).
- Client sends DeleteCharacters(“World”); retain(4); InsertCharacters(“!!”);, rev 4. (e⦁b’)
Server perspective
- Bob sends InsertCharacters(“World”);, rev 0. (c)
- Server applies it as it is. History = [c]. Document state = “World”.
- Bob sends InsertCharacters(“Hello “); retain(5);, rev 1. (d)
- Server applies it as it is. History = [c, d]. Document state = “Hello World”.
- Alice sends InsertCharacters(“Wave”);, rev 0. (a)
- Server applies retain(12); InsertCharacters(“Wave”);, rev 2. (a’). History = [c, d, a’]. Document State = “Hello WorldWave”.
- Server receives DeleteCharacters(“World”); retain(4); InsertCharacters(“!!”);, rev 4. (e⦁b’)
- Server applies retain(6); DeleteCharacters(“World”); retain(4); InsertCharacters(“!!”);, rev 4. ((e⦁b’)’). History = [c, d, a’, (e⦁b’)’]. Document State = “Hello Wave!!”