Skip to content

Commit

Permalink
Support encoding options on chained batch put() and del() (#717)
Browse files Browse the repository at this point in the history
Ref #633
  • Loading branch information
vweevers committed Apr 17, 2021
1 parent d81a249 commit 0765808
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,27 +289,27 @@ db.batch()
.write(function () { console.log('Done!') })
```

<b><code>batch.put(key, value)</code></b>
**`batch.put(key, value[, options])`**

Queue a _put_ operation on the current batch, not committed until a `write()` is called on the batch.
Queue a _put_ operation on the current batch, not committed until a `write()` is called on the batch. The `options` argument, if provided, must be an object and is passed on to the underlying store.

This method may `throw` a `WriteError` if there is a problem with your put (such as the `value` being `null` or `undefined`).

<b><code>batch.del(key)</code></b>
**`batch.del(key[, options])`**

Queue a _del_ operation on the current batch, not committed until a `write()` is called on the batch.
Queue a _del_ operation on the current batch, not committed until a `write()` is called on the batch. The `options` argument, if provided, must be an object and is passed on to the underlying store.

This method may `throw` a `WriteError` if there is a problem with your delete.

<b><code>batch.clear()</code></b>
**`batch.clear()`**

Clear all queued operations on the current batch, any previous operations will be discarded.

<b><code>batch.length</code></b>
**`batch.length`**

The number of queued operations on the current batch.

<b><code>batch.write(\[options]\[, callback])</code></b>
**`batch.write([options][, callback])`**

Commit the queued operations for this batch. All operations not _cleared_ will be written to the underlying store atomically, that is, they will either all succeed or fail with no partial commits.

Expand Down
12 changes: 6 additions & 6 deletions lib/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@ function Batch (levelup) {
this.length = 0
}

Batch.prototype.put = function (key, value) {
Batch.prototype.put = function (key, value, options) {
try {
this.batch.put(key, value)
this.batch.put(key, value, options)
} catch (e) {
throw new WriteError(e)
}

this.ops.push({ type: 'put', key, value })
this.ops.push({ ...options, type: 'put', key, value })
this.length++

return this
}

Batch.prototype.del = function (key) {
Batch.prototype.del = function (key, options) {
try {
this.batch.del(key)
this.batch.del(key, options)
} catch (err) {
throw new WriteError(err)
}

this.ops.push({ type: 'del', key })
this.ops.push({ ...options, type: 'del', key })
this.length++

return this
Expand Down
41 changes: 41 additions & 0 deletions test/chained-batch-encoding-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict'

const concatIterator = require('level-concat-iterator')
const discardable = require('./util/discardable')

module.exports = function (test, testCommon) {
test('chained batch with per-operation options', function (t) {
discardable(t, testCommon, function (db, done) {
let ops

db.once('batch', function (o) {
ops = o
})

db.batch()
.put('a', 'a', { valueEncoding: 'json' })
.put('b', 'b')
.put('"c"', 'c')
.del('c', { keyEncoding: 'json', arbitraryOption: true })
.write(function (err) {
t.ifError(err, 'no write error')

t.same(ops, [
{ type: 'put', key: 'a', value: 'a', valueEncoding: 'json' },
{ type: 'put', key: 'b', value: 'b' },
{ type: 'put', key: '"c"', value: 'c' },
{ type: 'del', key: 'c', keyEncoding: 'json', arbitraryOption: true }
])

concatIterator(db.iterator(), function (err, entries) {
t.ifError(err)
t.same(entries, [
{ key: 'a', value: '"a"' },
{ key: 'b', value: 'b' }
])
done()
})
})
})
})
}
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function suite (options) {
require('./init-test')(test, testCommon)
if (testCommon.encodings) require('./custom-encoding-test')(test, testCommon)
if (testCommon.encodings) require('./json-encoding-test')(test, testCommon)
if (testCommon.encodings) require('./chained-batch-encoding-test')(test, testCommon)
if (testCommon.streams) require('./key-value-streams-test')(test, testCommon)
require('./maybe-error-test')(test, testCommon)
require('./no-encoding-test')(test, testCommon)
Expand Down

0 comments on commit 0765808

Please sign in to comment.