mixu / vectorclock

A simple implementation of vector clocks in Javascript.
http://mixu.net/vectorclock/
78 stars 4 forks source link

vectorclock

A simple implementation of vector clocks in Javascript.

API

Vector clocks are represented as plain old objects with a "clock" key (which is a hash). For example: { clock: { a: 1, b: 2 } }.

Recommended reading:

API

Implementing read repair using vector clocks

Here is one way to implement read repair by detecting which clocks are concurrent, and if necessary, returning multiple values:

var responses = [ { clock: ... }, { clock: ... }];
// sort the responses by the vector clocks
responses.sort(VClock.descSort);
// then compare them to the topmost
// (in sequential order, the greatest) item
var repaired = [ responses.shift() ];
responses.forEach(function(item, index) {
  // if they are concurrent with that item, then there is a conflict
  // that we cannot resolve, so we need to return the item.
  if(VClock.isConcurrent(item, repaired[0]) &&
    !VClock.isIdentical(item, repaired[0])) {
    repaired.push(item);
  }
});