• Stars
    star
    203
  • Rank 192,890 (Top 4 %)
  • Language
    Python
  • License
    Other
  • Created about 13 years ago
  • Updated over 11 years ago

Reviews

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

Repository Details

CRDT toolbox

CRDT toolbox

The CRDT toolbox provides a collection of basic Conflict-free replicated data types as well as a common interface for defining your own CRDTs

If you don't know what a CRDT is, watch this

Definitions

  • CRDT - A Conflict-Free Replicated Data-Type as defined by the INRIA paper.
  • Payload - A JSON serializable representation of the CRDT's internal state
  • Value - The computed, usable value

Goals

  • Storage-Independence
  • Standard API
  • Compound CRDTs

Storage-Independent

To accomplish storage independence, the payload is exposed publically and as a simple JSON friendly data structures.

The public visibility of the payload allows the CRDTs to be loaded and stored independent of how it is stored.

While there exists serialization formats that allow non-JSON friendly structures -- such pickling a set -- targeting the lowest common denominator of JSON allows the possibility to serialize into a large number of formats.

Standard API

base.StateCRDT API

The base.StateCRDT class defines the interface for State-based CRDTs

Abstract Methods/Properties
__init__(self)

Creates a new CRDT at it's initial state

@property payload

This is the serializable representation of the CRDT's internal state. The data SHOULD be defined in simple types that can be represented as JSON, i.e. strings, numbers, dicts and lists.

@property value

This is the computed value of the CRDT.

@classmethod merge(cls, X, Y)

This merges the two CRDT instances

Built-in Methods

clone(self)

Creates a new copy of the CRDT instance

@classmethod from_payload(cls, payload)

Create a new CRDT instance with the given payload

Compound CRDTs

One property of CRDTs is that CRDTs made of CRDTs are automatically a CRDT. This enables Compound CRDTs. Here is a sample implementation of a friendship:

from crdt.sets import LWWSet
from crdt.base import StateCRDT


class Friendship(StateCRDT):
    def __init__(self):
        # The user key is considered constant among replicas
        # so no CRDT is needed
        self.user_key  = None
        self.following = LWWSet()
        self.followers = LWWSet()

    def get_payload(self):
        assert self.user_key, \
          "Can not generate a payload without a user_key"

        return {
            "user_key": self.user_key,
            "following": self.following.payload,
            "followers": self.followers.payload,
        }

    def set_payload(self, payload):
       self.following = LWWSet.from_payload(payload['following'])
       self.followers = LWWSet.from_payload(payload['followers'])

       self.user_key  = payload['user_key']

    payload = property(get_payload, set_payload)

    @property
    def value(self):
        return {
            "user_key": self.user_key,
            "following": self.following.value,
            "followers": self.followers.value,
            }
    
    @classmethod
    def merge(cls, X, Y):
        assert X.user_key == Y.user_key, "User keys do not match"
        assert X.user_key is not None, "user_key must be set"

        following = LWWSet.merge(X.following, Y.following)
        followers = LWWSet.merge(X.following, Y.following)

        new = cls()
        new.user_key = X.user_key
        new.following = following
        new.followers = followers
        
        return new

    #
    # Friendship API
    # 
    def follow(self, friend):
        self.following.add(friend.user_key)
        friend.followers.add(self.user_key)

    def unfollow(self, friend):
        self.following.discard(friend.user_key)
        friend.followers.discard(self.user_key)

Now this object can easily be stored and retrieved

import os
import json
from friendship import Friendship


def load(user_key):
    filename = "./%s.friendship.json" % user_key
    if os.path.exists(filename):
        with open(filename) as fh:
            return Friendship.from_payload(json.load(fh))
    else:
        new = Friendship()
        new.user_key = user_key
        return new


def store(friendship):
    filename = "./%s.friendship.json" % friendship.user_key

    with open(filename, "w") as fh:
        json.dump(friendship.payload, fh)
    

def friend_glenn():
    eric = load("eric")
    glenn = load("glenn")

    eric.follow(glenn)

    store(eric)
    store(glenn)

friend_glenn()

eric = load("eric")

print "Is eric following glenn?", "glenn" in eric.following

References

More Repositories

1

wsdemo

A Cowboy Websocket demo
Erlang
405
star
2

riak_crdt

A Riak loader for CRDTs
Python
21
star
3

node-jsonld-dsl

A DSL for building JSON-LD resources
JavaScript
15
star
4

flask-auth

Flask-Auth
Python
13
star
5

erl-crdt

A Collection of Conflict Free Replicated Datatypes
Erlang
13
star
6

flaskcma

A revolutionary content management application made with Flask and MongoEngine
Python
9
star
7

blog

My pelican blog
HTML
7
star
8

wsgirouter

A dead simple WSGI router
Python
6
star
9

collection-protobuf

A draft spec of collection+protobuf for evolving, typed HATEOAS RESTful services.
6
star
10

rafterl

An experimental implementation of the Raft consensus algorithm
Erlang
5
star
11

comp-sci-papers

A repository of public computer science papers
5
star
12

doze

Easy URL creation.
Python
5
star
13

micromodelsext-riak

An extension to micromodels to fetch/store/delete models in Riak
Python
4
star
14

timetracker

A singe-user time tracking web service
Python
4
star
15

edotsandboxes

A Dots and Boxes game written in Erlang and HTML5
Erlang
4
star
16

package-virtualenv

A virtualenv packager
Python
4
star
17

riak_pipetest

An example of a stand alone riak_pipe app
Erlang
3
star
18

hydra-python-client

A low-level hydra client for python
Python
3
star
19

dict-to-protobuf

Convert a dict to a protobuf message
Python
3
star
20

pitchfork-reviews

Fetches the top pitchfork reviews
Haskell
3
star
21

home-infra

Nix
3
star
22

pyqueryextract

Uses pyquery based DSL to turn HTML into a dictionary
Python
3
star
23

python-kestrel

A Kestrel python client that is built specifically for Kestrel instead of piggybacking on python-memcache
Python
3
star
24

duplicator-i3-repetier-firmware

Duplicator i3 Repetier Firmware for RAMPS 1.4
C++
2
star
25

ubuntu-natty-web

Ubuntu Natty -> Web Node installer
Shell
2
star
26

restexperiments

experiments to test the assertions made by REST and ROA
Python
2
star
27

fp

A FP toolkit for Python
Python
2
star
28

python-ldf-server

An experimental ldf-server for python
Python
2
star
29

erl-twitterlinks

A twitter link processer
Erlang
2
star
30

tvservice

A simple web service that filters a RSS feed based on simple pattern matching.
Python
2
star
31

nanoweb

The nano web framework.
Python
2
star
32

linked-data-and-rest

Python
2
star
33

feedservice

feedservice provides a webservice to store RSS feeds
Python
2
star
34

x-dhall-helm-release

Experimental Dhall Flux Helm Release support for Flux's helm-operator
Nix
1
star
35

gawkercheck

Quick python script to see if your email address was exposed by their breach
Python
1
star
36

doterlang_lib

My ~/.erlang_lib configuration
Shell
1
star
37

hyperblog

An experiment with the concept of hypermedia transclusion
JavaScript
1
star
38

pebble-vaper

A pebble app for vapers
C
1
star
39

x-tvservice-clj

A port of a my tv show
Clojure
1
star
40

redditpic-html5

An HTML5 app to browse pictures on reddit
1
star
41

experiments

My Experimental Code
Python
1
star
42

paginateanything

Paginate anything.
Python
1
star
43

gittest

Testing git
1
star
44

djsonld

Django tools for JSON-RD
1
star
45

OctoPrint-Queue

A print queue plugin for octoprint
Python
1
star
46

arbeiter

An unassuming work queue system
Python
1
star
47

clj-txtbudget

Experiment/Education port of my txtbudget app
Clojure
1
star
48

violet

An experimental Python DSL for validation
Python
1
star
49

docker-dev

experimentation with docker as a dev environment
Shell
1
star
50

node-fantasy-express-resource

Fantasy Land Express Resources
JavaScript
1
star
51

simplox

An experimental "CaaS"
Erlang
1
star
52

spark-experiments

Some Apache Spark experiments
Python
1
star
53

pci-mapreduce

Examples of the application of O'Reilly Programming Collective Intelligence + Map Reduce
JavaScript
1
star
54

txtbudget

A text based budget planning tool
Python
1
star
55

x-knifepicker

A tool for me to find a Swiss Army Knife
Erlang
1
star
56

python-manifestor

A simple project manifest tool
Python
1
star
57

pmap

a pmap implementation for Python
Python
1
star
58

play-instathread

A port of my instathread application
Scala
1
star
59

washingmachine

My project to make my dumb washing machines into a smart washing machine
C++
1
star