Giter Club home page Giter Club logo

lww_elements_set_py's Introduction

LWW Elements Set - Python

A Last-Writer-Wins (LWW) Element set data structure, an operation-based Conflict-Free Replicated Data Type (CDRT) was implemented in Python. A test suite was also included to test various CDRT properties, i.e., communtativity, associativity and idempotence.

Operations

The conceptual idea is referenced from this wiki page.

Operations on CRDTs need to adhere to the following rules:

  • Associativity (a+(b+c)=(a+b)+c), so that grouping doesn't matter.
  • Commutativity (a+b=b+a), so that order of application doesn't matter.
  • Idempotence (a+a=a), so that duplication doesn't matter.

To determine if an element exists in the LWW element set, the following rules are applied:

  • Exists if the element is present in the add set but not in the remove set (trivial)
  • Does not exist if the element is present in the remove set but not in the add set (trivial)
  • Exists if the element's most recent operation is an "add"
  • Does not exist if the element's most recent operation is a "remove"
  • If the timestamp of add and remove are the same, it is biased towards "add"

This implementation provides the following APIs (check the code for more details):

  • add(element, timestamp) : Add an element with the timestamp to the add set
  • remove(element, timestamp) : Add an element with the timestamp to the remove set
  • contains(element) : Check if an element exists in the LWW element set using the rules above
  • get_all() : Return all existing elements in the LWW element set

Testing

Test for Idempotence

It is required that duplication or re-delivery of operations does not affect the final result. The following tests attempt to repeat the "add" / "remove" operations with different timestamps.

Original state Operation Resulting state Final result
A(a,1) R() add(a,0) A(a,1) R() ['a']
A(a,1) R() add(a,1) A(a,1) R() ['a']
A(a,1) R() add(a,2) A(a,2) R() ['a']
A() R(a,1) remove(a,0) A() R(a,1) [ ]
A() R(a,1) remove(a,1) A() R(a,1) [ ]
A() R(a,1) remove(a,2) A() R(a,2) [ ]

Test for Commutativity

It is required that the order of operations does not affect the final result. The following tests attempt to reverse the order of "add" and "remove" operations with different timestamps.

Original state Operation Resulting state Final result
A(a,1) R() remove(a,1) A(a,1) R(a,1) ['a']
A() R(a,1) add(a,1) A(a,1) R(a,1) ['a']
A(a,1) R() remove(a,0) A(a,1) R(a,0) ['a']
A() R(a,0) add(a,1) A(a,1) R(a,0) ['a']
A(a,1) R() remove(a,2) A(a,1) R(a,2) [ ]
A() R(a,2) add(a,1) A(a,1) R(a,2) [ ]

Test for Associativity

It is required that the grouping of operations does not affect the final result. The following tests attempt to group several operations with different timestamps.

Original state Operation Resulting state Final result
A(a,1; b,2) R() remove(b,3) A(a,1; b,2) R(b,3) ['a']
A(b,2) R(b,3) add(a,1) A(a,1; b,2) R(b,3) ['a']

In fact, proving full commutativity is sufficient for proving associativity, since grouping in this context refers to re-ordering certain operations. The resulting state is just a collection of all element operations.

Future Development

  • Garbage collection : elements should be removed from the sets from time to time when they are no longer useful. In fact, only the most-recent operation (add/remove) is needed.

References

lww_elements_set_py's People

Contributors

graysonhyc avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.