Skip to content

Commit

Permalink
support preput,predel,prebatch
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Feb 3, 2017
1 parent 577f9be commit 0012276
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ Returns a new duplex server stream that you should connect with a client. Option

``` js
{
readonly: true // make the database be accessible as read only
readonly: true, // make the database be accessible as read only
preput: function (key, val, cb) {}, // called before puts
predel: function (key, cb) {}, // called before dels
prebatch: function (batch, cb) {} // called before batches
}
```

Expand Down
36 changes: 29 additions & 7 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ var DECODERS = [
]

module.exports = function (db, opts) {
var readonly = !!(opts && opts.readonly)
if (!opts) opts = {}

var readonly = !!(opts.readonly)
var decode = lpstream.decode()
var encode = lpstream.encode()
var stream = duplexify(decode, encode)

var preput = opts.preput || function (key, val, cb) { cb(null) }
var predel = opts.predel || function (key, cb) { cb(null) }
var prebatch = opts.prebatch || function (ops, cb) { cb(null) }

if (db.isOpen()) ready()
else db.open(ready)

Expand Down Expand Up @@ -73,8 +79,11 @@ module.exports = function (db, opts) {
}

function onput (req) {
down.put(req.key, req.value, function (err) {
callback(req.id, err, null)
preput(req.key, req.value, function (err) {
if (err) return callback(err)
down.put(req.key, req.value, function (err) {
callback(req.id, err, null)
})
})
}

Expand All @@ -85,8 +94,11 @@ module.exports = function (db, opts) {
}

function ondel (req) {
down.del(req.key, function (err) {
callback(req.id, err)
predel(req.key, function (err) {
if (err) return callback(err)
down.del(req.key, function (err) {
callback(req.id, err)
})
})
}

Expand All @@ -95,8 +107,11 @@ module.exports = function (db, opts) {
}

function onbatch (req) {
down.batch(req.ops, function (err) {
callback(req.id, err)
prebatch(req.ops, function (err) {
if (err) return callback(err)
down.batch(req.ops, function (err) {
callback(req.id, err)
})
})
}

Expand All @@ -122,6 +137,13 @@ function Iterator (down, req, encode) {

this.batch = req.batch || 0

if (req.options) {
if (req.options.gt === null) req.options.gt = undefined
if (req.options.gte === null) req.options.gte = undefined
if (req.options.lt === null) req.options.lt = undefined
if (req.options.lte === null) req.options.lte = undefined
}

this._iterator = down.iterator(req.options)
this._encode = encode
this._send = send
Expand Down
18 changes: 11 additions & 7 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var levelup = require('levelup')
var multileveldown = require('../')

tape('get', function (t) {
var db = levelup('no-location', {db: memdown})
var db = levelup('no-location', {db: mem})
var stream = multileveldown.server(db)
var client = multileveldown.client()

Expand All @@ -21,7 +21,7 @@ tape('get', function (t) {
})

tape('put', function (t) {
var db = levelup('no-location', {db: memdown})
var db = levelup('no-location', {db: mem})
var stream = multileveldown.server(db)
var client = multileveldown.client()

Expand All @@ -38,7 +38,7 @@ tape('put', function (t) {
})

tape('readonly', function (t) {
var db = levelup('no-location', {db: memdown})
var db = levelup('no-location', {db: mem})

db.put('hello', 'verden')

Expand All @@ -58,7 +58,7 @@ tape('readonly', function (t) {
})

tape('del', function (t) {
var db = levelup('no-location', {db: memdown})
var db = levelup('no-location', {db: mem})
var stream = multileveldown.server(db)
var client = multileveldown.client()

Expand All @@ -78,7 +78,7 @@ tape('del', function (t) {
})

tape('batch', function (t) {
var db = levelup('no-location', {db: memdown})
var db = levelup('no-location', {db: mem})
var stream = multileveldown.server(db)
var client = multileveldown.client()

Expand All @@ -99,7 +99,7 @@ tape('batch', function (t) {
})

tape('read stream', function (t) {
var db = levelup('no-location', {db: memdown})
var db = levelup('no-location', {db: mem})
var stream = multileveldown.server(db)
var client = multileveldown.client()

Expand All @@ -118,7 +118,7 @@ tape('read stream', function (t) {
})

tape('read stream (gt)', function (t) {
var db = levelup('no-location', {db: memdown})
var db = levelup('no-location', {db: mem})
var stream = multileveldown.server(db)
var client = multileveldown.client()

Expand All @@ -134,3 +134,7 @@ tape('read stream (gt)', function (t) {
}))
})
})

function mem () {
return memdown()
}

0 comments on commit 0012276

Please sign in to comment.