LevelDB + Vector Clocks
$ npm install level-vectorclock --save
var LVC = require('level-vectorclock');
var level = require('level');
var nodeId = 'node-44';
var db = LVC(level('/path/to/my/db'), nodeId);
Without metadata (may create sibilings):
db.put('key', 'value', function(err, meta) {
if (err) throw err;
console.log('wrote, and here is meta: %j', meta);
});
With metadata:
db.put('key', 'value', meta, function(err, meta) {
if (err) throw err;
console.log('wrote, and here is new meta: %j', meta);
});
db.get('key', function(err, records) {
if (err) throw err;
records.forEach(function(record) {
console.log('value:', record.value);
console.log('meta:', record.meta);
});
});
Since you may have had two conflicting writes, when you read you may get more than one record.
To resolve the conflict you need to provide all the metadatas of the records you want to resolve into a put
call like this:
var metas = [meta1, meta2];
db.put('key', 'value', metas, function(err, meta) {
if (err) throw err;
console.log('new meta: %j', meta);
});
Without options, streams all the data:
var s = db.createReadStream();
s.on('data', function(recs) {
// each record on a data event has one or more sibilings
recs.forEach(function(rec) {
console.log('key:', rec.key);
console.log('value:', rec.value);
console.log('meta:', rec.meta);
});
});
With options:
var options = {
start: 'C',
end: 'A',
reverse: true,
values: false,
keys: true
};
var s = db.createReadStream(options);
Alias for createReadStream({values: true, keys: false})
.
Returns only the keys. Alias for createReadStream({values: false, keys: true})
.
Creates a writable stream.
Supports the same options as levelup.
MIT