Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ergl/crdt-ml

CRDTs - Conflict-Free Replicated Data Types for OCaml
https://github.com/ergl/crdt-ml

Last synced: 9 days ago
JSON representation

CRDTs - Conflict-Free Replicated Data Types for OCaml

Awesome Lists containing this project

README

        

# CRDTs - Conflict-Free Replicated Data Types for OCaml

A CRDT (or Conflict-Free Replicated Data Type) is a data type designed to
satisfy the Strong Eventual Consistency model.

In other words, given a set of replicas of the same data type, and a number of
operations independently applied to any replica, all replicas will eventually
reach the same state, no matter the order of the operations.

These properties make CRDTs extremely useful for distributed KV stores and
collaborative applications, among others.

Further Reading:

- [Conflict-free replicated data types](http://dl.acm.org/citation.cfm?id=2050642)
- [A comprehensive study of Convergent and Commutative Replicated Data Types](https://hal.inria.fr/inria-00555588)

## Datatypes

Included data types are:

- _IntVector_ : An Integer Vector.

- _GCounter_ : A grow-only counter.

- _PNCounter_ : A counter implementing both increment and decrement operations.

- _GSet_ : A grow-only set.

- _USet_ : A set supporting both add and remove operations. [1](#usetnote)

- _ORSet_ : A set supporting both add and remove operations.

Unless stated otherwise, all implementations come in mutable (`Crdt.Mutable`)
and immutable (`Crdt.Immutable`) flavours.

Please note that the current implementations are designed only for educational
purposes. Don't use them for any serious work.

1: Removed elements can never be added again.
Use an `ORSet` if you want to be able to add removed elements again. Read the
reasoning in the [documentation](http://ergl.github.io/crdt-ml/M_USet.html).

## Installing

Using [`opam`](https://opam.ocaml.org):

```
opam install crdt-ml
```

Or you can install from source:

```
make && make install
```

To try the library in a repl, open an ocaml toplevel and `require` it:

```
# #use "topfind";;
# #require "crdt";;
```

To link it, compile your files using `ocamlbuild`:

```
ocamlbuild -use-ocamlfind -pkgs crdt
```

Run the tests:

```
make test
```

The tests depend on [iTeML](https://github.com/vincent-hugot/iTeML). You can install it with `opam install qtest`.

You can also choose to use just a certain part of the library. To do so, link or load the
submodule you want:

- `crdt_mutable` - Mutable implementations.
- `crdt_immutable` - Immutable implementations.

## Usage

For a simple increment / decrement counter:

```ocaml
open Crdt.Mutable.PNCounter

let a = make () and
b = make ()

incr a
incr a

query a
- : int = 2

decr b
merge a b

query a
- : int = 1
```

Using an `USet`:

```ocaml
module StrSet = Crdt.Mutable.USet.Make (String)

open StrSet

let s1 = make () and
s2 = make ()

add "I" s1
add "Love" s1

add "Pancakes" s2
add "Yeah!" s1

merge s1 s2
merge s2 s1

value s1
- : bytes list = ["I"; "Love"; "Pancakes"; "Yeah!"]

remove "Yeah!" s2
merge s1 s2

value s1
- : bytes list = ["I"; "Love"; "Pancakes"]

value s2
- : bytes list = ["I"; "Love"; "Pancakes"]
```

### Documentation

Check out the online [documentation](http://ergl.github.io/crdt-ml) for more.
The documentation and the API reference are automatically generated by `ocamldoc`
from the interfaces.

You can generate your own local documentation running `make doc`.

## License

GPLv3. Check the [License](./LICENSE).