Skip to content

Commit dbf5c5b

Browse files
author
rafapaezbas
committed
added onlyIfChanged opt
1 parent edc418c commit dbf5c5b

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ Options include:
5757
```
5858
{
5959
keyEncoding: 'utf-8' | 'binary' | 'ascii', // or some abstract encoding
60-
valueEncoding: <same as above>
60+
valueEncoding: <same as above>,
61+
onlyIfChanged: true // put or delete a value only if it means a change in the db
6162
}
6263
```
6364

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const sameData = require('same-data')
12
const codecs = require('codecs')
23
const { Readable } = require('streamx')
34
const mutexify = require('mutexify/promise')
@@ -284,6 +285,7 @@ class Hyperbee {
284285
this.sep = opts.sep || SEP
285286
this.readonly = !!opts.readonly
286287
this.prefix = opts.prefix || null
288+
this.onlyIfChanged = opts.onlyIfChanged
287289

288290
this._unprefixedKeyEncoding = this.keyEncoding
289291
this._sub = !!this.prefix
@@ -616,7 +618,7 @@ class Batch {
616618
async put (key, value, opts) {
617619
const release = this.batchLock ? await this.batchLock() : null
618620

619-
const cas = (opts && opts.cas) || null
621+
const cas = (opts && opts.cas) || (this.tree.onlyIfChanged ? sameData : null)
620622
const encoding = this._getEncoding(opts)
621623

622624
if (!this.locked) await this.lock()
@@ -698,7 +700,7 @@ class Batch {
698700

699701
async del (key, opts) {
700702
const release = this.batchLock ? await this.batchLock() : null
701-
const cas = (opts && opts.cas) || null
703+
const cas = (opts && opts.cas) || (this.tree.onlyIfChanged ? (prev) => prev.value !== null : null)
702704
const encoding = this._getEncoding(opts)
703705

704706
if (!this.locked) await this.lock()

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"mutexify": "^1.4.0",
1515
"protocol-buffers-encodings": "^1.2.0",
1616
"safety-catch": "^1.0.2",
17+
"same-data": "github:mafintosh/same-data",
1718
"streamx": "^2.12.4"
1819
},
1920
"devDependencies": {

test/basic.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const test = require('brittle')
22
const b4a = require('b4a')
33
const { create, collect, createCore } = require('./helpers')
4-
54
const Hyperbee = require('..')
65

76
test('out of bounds iterator', async function (t) {
@@ -486,3 +485,49 @@ test('supports encodings in snapshot', async function (t) {
486485
t.alike(await snap1.get('hi'), { seq: 1, key: b4a.from('hi'), value: 'there' })
487486
t.alike(await snap2.get('hi'), { seq: 1, key: 'hi', value: b4a.from('there') })
488487
})
488+
489+
test('onlyIfChanged put', async function (t) {
490+
{
491+
const db = create({ onlyIfChanged: true })
492+
await db.ready()
493+
await db.put('key', 'value')
494+
t.is(db.feed.length, 2)
495+
await db.put('key', 'value')
496+
t.is(db.feed.length, 2)
497+
}
498+
{
499+
const db = create({ onlyIfChanged: true, valueEncoding: 'binary' })
500+
await db.ready()
501+
await db.put('key', Buffer.from('buffer'))
502+
t.is(db.feed.length, 2)
503+
await db.put('key', Buffer.from('buffer'))
504+
t.is(db.feed.length, 2)
505+
}
506+
{
507+
const db = create({ onlyIfChanged: true, valueEncoding: 'json' })
508+
await db.ready()
509+
await db.put('key', { a: 1 })
510+
t.is(db.feed.length, 2)
511+
await db.put('key', { a: 1 })
512+
t.is(db.feed.length, 2)
513+
}
514+
})
515+
516+
test('onlyIfChanged del', async function (t) {
517+
const db = create({ onlyIfChanged: true })
518+
await db.ready()
519+
await db.put('key', 'value')
520+
await db.del('key')
521+
t.is(db.feed.length, 3)
522+
await db.del('key')
523+
t.is(db.feed.length, 3)
524+
})
525+
526+
test('onlyIfChanged put after del', async function (t) {
527+
const db = create({ onlyIfChanged: true })
528+
await db.ready()
529+
await db.put('key', 'value')
530+
await db.del('key')
531+
await db.put('key', 'value')
532+
t.is(db.feed.length, 4)
533+
})

0 commit comments

Comments
 (0)