• This repository has been archived on 12/Nov/2019
  • Stars
    star
    836
  • Rank 54,534 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 12 years ago
  • Updated about 6 years ago

Reviews

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

Repository Details

Commutative Replicated Data Types for easy collaborative/distributed systems.

CRDT - Commutative Replicated Data Types

a CRDT is a data type designed so that operations on it commute - give the same result independent of the order in which they are applied.

testling badge build status

CRDTs give you eventual consistency for free. it is not necessary to track concurrent changes and use complicated merge algorithms. this module is useful for collaborative/distributed/peer2peer (same things) applications.

Further Reading: A comprehensive study of Convergent and Commutative Replicated Data Types

replicating documents

create two documents,

var Doc = require('crdt').Doc
var A = new Doc()
var B = new Doc()

then pipe them together

var as
(as = A.createStream())
  .pipe(B.createStream())
  .pipe(as)

We just replicated two documents with in the same process... this is the idea, but of course, we want to do it on different machines...

notice the pattern is basically the same...

the client side ...

var net = require('net')
var es  = require('es')
var Doc = require('crdt').Doc

var A = new Doc()

var stream
(stream = net.connect())
  .pipe(A.createStream())
  .pipe(stream)

and the server ...

var net = require('net')
var es  = require('es')
var Doc = require('crdt').Doc

var A = new Doc()

net.createServer(function (stream) {
  stream
    .pipe(A.createStream())
    .pipe(stream)
})

Doc

Doc#add(obj = {id: ID, ...})

Add a Row to the document initialized to obj. If obj doesn't have a unique id property, a random key will be created.

Return the Row object.

Doc#get(id)

Get a Row from the document by id.

Doc#set(id, doc)

Sets Row with ID id to doc. Returns the Row. It has all the effects and implications of calling Row#set.

Doc#rm(id)

Remove a Row from the document by id Also removes from all sets as well.

Doc#toJSON()

Returns a raw Doc object.

Doc#createSet (key, value)

Create a Set a set is a collection of rows defined by a particular value on a particular property.

var cheeses = doc.createSet('type', 'cheese')

key and value must both be strings.

Doc#createSet (filter)

You can also create a Set using a filter function.

var cheeses = doc.createSet(function (state) {
    return state.type === 'cheese'
})

A filter function should just be a more expressive filter and shouldn't be a stateful function

Doc#createSeq (key, value)

same as Doc#createSet except that seqs have a significant order.

sequences can also be created with a filter using Doc#createSeq(filter)

Doc#createStream (opts)

create a stream that is used to connect to another Doc instance.

event: doc.emit('create', row)

Emitted when a new Row is created

event: doc.emit('row_update', row)

Emitted when a new Row is updated

Row

an object with in a crdt Doc

Row#set(key, value)

set key to value. if Row#set(obj) is called instead all the keys in obj will update atomically.

This causes a 'change' event to be emitted, and an update message to be sent down the stream. (note, if the stream in not yet connected, that is okay, current state of the document is replicated as soon as the streams are connected.)

Row#get(key)

get the current value for a key.

Row#toJSON()

return a raw object ready for serialization. this is not a JSON string yet, misleading name, but that is the correct JSON.stringify api.

event: Row.emit('change', changed)

Emitted when a row is changed. this may be the result of a local or a remote update.

changed is the a hash of the fields that have changed.

event: Row.emit('removed')

Emitted when a row is removed. This may be the result of a local or a remote update.

Set

A collection of Rows within a document.

Set#asArray()

get the contents of this set as a regular js Array

Set#toJSON()

calls toJSON on the each Row in the set and puts it in an array.

Set#has(row|id)

check if a row|id is a member of the set.

Set#get(id)

get an item in this set, if it exists.

Set#each(iter), Set#forEach(iter)

Iterate over the Rows in the set.

Set#onEach(iter)

Iterate over the Rows in the set and any new row that may be added to the set in the future.

Set#remove(row)

removes a row from the set. sets the set's key, to null. note, if you have multiple sets with the same key, they are mutually exclusive, and adding a node to a different set will remove it from the first one.

event: Set.emit('add', Row)

Emitted when a row is added to the set.

event: Set.emit('changes', Row, changed)

Emitted when a row in the set changed. The changed value contains a hash of the key / values that changed.

event: Set.emit('remove', Row)

Emitted when a row is removed from the set

Seq

just like a Set, but the items are ordered. they will begiven a _sort property.

Seq#first()

get the first item in the seq.

Seq#last()

get the last item in the seq.

Seq#has(row|id)

check if a row|id is a member of the seq. (inherited from Set)

Seq#indexOf(id | row)

find the index of the given row or id.

Seq#at(index)

get the item currently at index

Seq#unshift(row)

push a Row onto the start of the Seq

Seq#push(row)

push a Row onto the end of the Seq

Seq#length()

get the number of items currently in the Seq.

Seq#pop()

remove the last item.

Seq#shift()

remove the first item.

Seq#before(item, id | row)

insert item before the given row/id.

Seq#after(item, id | row)

insert item after the given row/id.

Seq#next(key)

Finds the item that is after this key

Seq#prev(key)

Finds the item that is before this key

event: Seq.emit('move', Row)

Emitted when the row has changed it's position in the sequence

More Repositories

1

event-stream

EventStream is like functional programming meets IO
JavaScript
2,189
star
2

JSON.sh

a pipeable JSON parser written in Bash
Shell
1,996
star
3

JSONStream

rawStream.pipe(JSONStream.parse()).pipe(streamOfObjects)
JavaScript
1,913
star
4

scuttlebutt

peer-to-peer replicatable data structure
JavaScript
1,310
star
5

rc

The non-configurable configuration loader for lazy people.
JavaScript
995
star
6

through

simple way to create a ReadableWritable stream that works
JavaScript
667
star
7

your-web-app-is-bloated

measuring memory usage of popular webapps
514
star
8

npmd

JavaScript
450
star
9

split

JavaScript
346
star
10

curry

simple curry module, with nothing *too clever*, and full test coverage
JavaScript
313
star
11

random-name

JavaScript
296
star
12

hashlru

JavaScript
240
star
13

wifi.sh

Shell
216
star
14

level-sublevel

no longer maintained, sorry!
JavaScript
194
star
15

mux-demux

mutiplex-demultiplex multiple streams through a single text Stream
JavaScript
179
star
16

noderify

official fork: https://github.com/staltz/noderify
JavaScript
157
star
17

feedopensource

Iteratively Fund Open Source Projects With Bitcoin
JavaScript
142
star
18

excel-stream

JavaScript
137
star
19

stream-spec

executable specification for Stream (make testing streams easy)
JavaScript
125
star
20

map-stream

JavaScript
122
star
21

map-reduce

async map-reduce functions for nodejs
JavaScript
121
star
22

cyphernet

115
star
23

observable

A Mutable Value represented as a Function.
HTML
111
star
24

stream-combiner

JavaScript
103
star
25

rpc-stream

JavaScript
98
star
26

bench-lru

JavaScript
87
star
27

pull-box-stream

One way streaming encryption based on libsodium's secretbox primitive
JavaScript
84
star
28

level-live-stream

JavaScript
79
star
29

stack-expression

inspired by regular expressions but can do nested structures
JavaScript
76
star
30

hipster

JavaScript
72
star
31

snob

distributed version control system implemented in javascript.
JavaScript
71
star
32

xdiff

diff complex javascript objects
JavaScript
70
star
33

from

Easy way to create a Readable Stream
JavaScript
70
star
34

scalable-secure-scuttlebutt

HTML
68
star
35

explain-error

JavaScript
67
star
36

fsm

Finite State Machines in javascript
JavaScript
66
star
37

r-edit

JavaScript
64
star
38

readme

JavaScript
62
star
39

tiles

JavaScript
61
star
40

indexhtmlify

JavaScript
59
star
41

tacodb

JavaScript
57
star
42

adiff

diff and patch operations on arrays.
JavaScript
57
star
43

map-filter-reduce

JavaScript
57
star
44

browser-stream

open pipable streams to and from the browser, with Socket.io
JavaScript
55
star
45

reconnect

JavaScript
53
star
46

level-replicate

JavaScript
51
star
47

electro

JavaScript
51
star
48

d64

JavaScript
50
star
49

on-change-network

JavaScript
49
star
50

lock

lock asynchronous resources
JavaScript
48
star
51

crypto-bench

HTML
47
star
52

mynosql

JavaScript
44
star
53

monotonic-timestamp

JavaScript
44
star
54

pause-stream

JavaScript
43
star
55

json-select

JavaScript
43
star
56

json-buffer

JavaScript
41
star
57

coherence

JavaScript
41
star
58

bittodo

JavaScript
40
star
59

stream-punks

discussion repo for streams
39
star
60

charwise

JavaScript
39
star
61

proxy-by-url

custom logic for node-http-proxy to proxy based on incoming url
JavaScript
38
star
62

sentimental-versioning

version numbers with meaning
HTML
38
star
63

level-hooks

JavaScript
37
star
64

sodium-browserify

JavaScript
37
star
65

secret-handshake-paper

TeX
36
star
66

browselectrify

create browserify bundle that also works in electron
JavaScript
36
star
67

kv

simple kv store for streams
JavaScript
35
star
68

c2wasm

C++
35
star
69

level-trigger

triggers for levelup
JavaScript
33
star
70

deploy

scripts to setup continuous deployment with git push
Shell
33
star
71

presentations

JavaScript
32
star
72

rumours

Intergration of scuttlebutt family.
JavaScript
32
star
73

web-bootloader

HTML
28
star
74

what-is-scuttlebutt

spec for defining "scuttlebutt" as a living changing protocol
28
star
75

remote-events

connect EventEmitters through Streams.
JavaScript
28
star
76

indexes-of

JavaScript
27
star
77

mpg123

JavaScript
27
star
78

level-master

JavaScript
27
star
79

h

JavaScript
26
star
80

testbed

continuous integration for nodejs
JavaScript
25
star
81

canvas-browserify

HTML
25
star
82

it-is

assertion DSL based on functional idioms.
JavaScript
25
star
83

level-merkle

JavaScript
25
star
84

semver-ftw

Simple Description of SemVer
HTML
25
star
85

level-inverted-index

JavaScript
24
star
86

computer-modern

CSS
24
star
87

hyperaudio

JavaScript
24
star
88

level-search

JavaScript
24
star
89

level-scuttlebutt

leveldb persistence for scuttlebutts (scuttlebutt/crdt/append-only and friends)
JavaScript
24
star
90

level-couch-sync

JavaScript
23
star
91

simple-xlsx

maintained fork is at https://github.com/zeke/simple-xlsx
JavaScript
23
star
92

shasum

JavaScript
23
star
93

content-addressable-store

JavaScript
23
star
94

ticket-auth

JavaScript
22
star
95

ssh-key-to-pem

JavaScript
21
star
96

private-groups-paper

21
star
97

scuttlebucket

JavaScript
21
star
98

looper

JavaScript
20
star
99

deterministic-tar

JavaScript
20
star
100

npm-browserify

JavaScript
20
star