Comparing version 3.2.0 to 3.3.0
17
index.js
@@ -177,8 +177,17 @@ const Node = require('./lib/node') | ||
HyperTrie.prototype.put = function (key, value, cb) { | ||
return new Put(this, key, value, null, 0, cb || noop) | ||
HyperTrie.prototype.put = function (key, value, opts, cb) { | ||
if (typeof opts === 'function') return this.put(key, value, null, opts) | ||
opts = Object.assign({}, opts, { | ||
batch: null, | ||
del: 0 | ||
}) | ||
return new Put(this, key, value, opts, cb || noop) | ||
} | ||
HyperTrie.prototype.del = function (key, cb) { | ||
return new Delete(this, key, null, cb) | ||
HyperTrie.prototype.del = function (key, opts, cb) { | ||
if (typeof opts === 'function') return this.del(key, null, opts) | ||
opts = Object.assign({}, opts, { | ||
batch: null | ||
}) | ||
return new Delete(this, key, opts, cb) | ||
} | ||
@@ -185,0 +194,0 @@ |
@@ -74,5 +74,5 @@ const Put = require('./put') | ||
const {type, key, value} = self._ops[i++] | ||
if (type === 'del') self._op = new Delete(self._db, key, self, loop) | ||
else self._op = new Put(self._db, key, value === undefined ? null : value, self, 0, loop) | ||
if (type === 'del') self._op = new Delete(self._db, key, { batch: self }, loop) | ||
else self._op = new Put(self._db, key, value === undefined ? null : value, { batch: self, del: 0 }, loop) | ||
} | ||
} |
@@ -6,3 +6,3 @@ const Put = require('./put') | ||
function Delete (db, key, batch, cb) { | ||
function Delete (db, key, { batch, condition = null }, cb) { | ||
this._db = db | ||
@@ -14,2 +14,3 @@ this._key = key | ||
this._batch = batch | ||
this._condition = condition | ||
this._node = new Node({key}) | ||
@@ -52,4 +53,15 @@ this._length = this._node.length | ||
this._put = new Put(this._db, key, val, this._batch, node.seq, done) | ||
if (this._condition) this._condition(node.value, oncondition) | ||
else del() | ||
function oncondition (err, proceed) { | ||
if (err) return done(err) | ||
if (!proceed) return done(null) | ||
return del() | ||
} | ||
function del () { | ||
self._put = new Put(self._db, key, val, { batch: self._batch, del: node.seq }, done) | ||
} | ||
function done (err, node) { | ||
@@ -56,0 +68,0 @@ self._finalize(err, node) |
@@ -5,3 +5,3 @@ const Node = require('./node') | ||
function Put (db, key, value, batch, del, cb) { | ||
function Put (db, key, value, { batch, del, condition = null }, cb) { | ||
this._db = db | ||
@@ -12,2 +12,3 @@ this._node = new Node({key, value}, 0, db.valueEncoding) | ||
this._batch = batch | ||
this._condition = condition | ||
this._error = null | ||
@@ -41,3 +42,3 @@ this._pending = 0 | ||
Put.prototype._finalize = function (err) { | ||
Put.prototype._finalize = function (err, lastNode) { | ||
const self = this | ||
@@ -54,10 +55,21 @@ | ||
if (this._batch) { | ||
this._batch.append(this._node) | ||
return done(null, this._node) | ||
if (this._condition) this._condition(lastNode, this._node, oncondition) | ||
else insert() | ||
function oncondition (err, proceed) { | ||
if (err) return done(err) | ||
if (!proceed) return done(null) | ||
return insert() | ||
} | ||
this._node.seq = this._db.feed.length | ||
this._db.feed.append(this._node.encode(), done) | ||
function insert () { | ||
if (self._batch) { | ||
self._batch.append(self._node) | ||
return done(null, self._node) | ||
} | ||
self._node.seq = self._db.feed.length | ||
self._db.feed.append(self._node.encode(), done) | ||
} | ||
function done (err) { | ||
@@ -125,3 +137,3 @@ const node = err ? null : self._node | ||
this._finalize(null) | ||
this._finalize(null, head) | ||
} | ||
@@ -128,0 +140,0 @@ |
{ | ||
"name": "hypertrie", | ||
"version": "3.2.0", | ||
"version": "3.3.0", | ||
"description": "Distributed single writer key/value store", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
81235
27
2564