Skip to content

Commit

Permalink
rename to alwaysDuplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewosh committed Nov 16, 2023
1 parent 16cacaa commit 9477b3e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 41 deletions.
17 changes: 13 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ class TreeNode {
if (cas) {
const prev = await this.getKeyNode(mid)
if (cas && !(await cas(prev.final(encoding), node))) return true
} else if (!this.block.tree.tree.putSameValue) {
}
if (!this.block.tree.tree.alwaysDuplicate) {
const prev = await this.getKeyNode(mid)
if (prev && b4a.equals(prev.value, enc(encoding.value, node.value))) return true
}
Expand Down Expand Up @@ -326,7 +327,7 @@ class Hyperbee extends ReadyResource {
this.lock = opts.lock || mutexify()
this.sep = opts.sep || SEP
this.readonly = !!opts.readonly
this.putSameValue = opts.putSameValue !== false
this.alwaysDuplicate = opts.alwaysDuplicate !== false
this.prefix = opts.prefix || null

this._unprefixedKeyEncoding = this.keyEncoding
Expand Down Expand Up @@ -825,7 +826,8 @@ class Batch {
if (cas) {
const prev = await node.getKeyNode(mid)
if (cas && !(await cas(prev.final(encoding), newNode))) return this._unlockMaybe()
} else if (!this.tree.putSameValue) {
}
if (!this.tree.alwaysDuplicate) {
const prev = await node.getKeyNode(mid)
if (prev && b4a.equals(prev.value, value)) return this._unlockMaybe()
}
Expand Down Expand Up @@ -906,7 +908,14 @@ class Batch {
c = b4a.compare(key, await node.getKey(mid))

if (c === 0) {
if (cas && !(await cas((await node.getKeyNode(mid)).final(encoding), delNode))) return this._unlockMaybe()
if (cas) {
const prev = await node.getKeyNode(mid)
if (!(await cas(prev.final(encoding), delNode))) return this._unlockMaybe()
}
if (!this.tree.alwaysDuplicate) {
const prev = await node.getKeyNode(mid)
if (!prev) return this._unlockMaybe()
}
if (node.children.length) await setKeyToNearestLeaf(node, mid, stack)
else node.removeKey(mid)
// we mark these as changed late, so we don't rewrite them if it is a 404
Expand Down
37 changes: 0 additions & 37 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -539,40 +539,3 @@ test('get by seq', async function (t) {
t.alike(await db.getBySeq(1), { key: '/a', value: '1' })
t.alike(await db.getBySeq(2), { key: '/b', value: '2' })
})

test('putSameValue - should not insert the same kv-pair twice', async function (t) {
const db1 = create()
const db2 = create({ putSameValue: false })

await db1.put('/a', '1')
await db2.put('/a', '1')

const version = db1.version

await db1.put('/a', '1')
await db2.put('/a', '1')
await db1.put('/a', '1')
await db2.put('/a', '1')

t.is(db1.version, version + 2)
t.is(db2.version, version)
})

test('putSameValue - works on batch puts', async function (t) {
const db1 = create()
const db2 = create({ putSameValue: false })

const b1 = db1.batch()
const b2 = db2.batch()

await b1.put('/a', '1')
await b2.put('/a', '1')
await b1.put('/a', '1')
await b2.put('/a', '1')

await b1.flush()
await b2.flush()

t.is(db1.version, 3)
t.is(db2.version, 2)
})
37 changes: 37 additions & 0 deletions test/cas.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,3 +508,40 @@ test('flushing an empty batch after a "failed" cas op releases lock (allows prog
b.destroy()
}
})

test('alwaysDuplicate - should not insert the same kv-pair twice', async function (t) {
const db1 = create()
const db2 = create({ alwaysDuplicate: false })

await db1.put('/a', '1')
await db2.put('/a', '1')

const version = db1.version

await db1.put('/a', '1')
await db2.put('/a', '1')
await db1.put('/a', '1')
await db2.put('/a', '1')

t.is(db1.version, version + 2)
t.is(db2.version, version)
})

test('alwaysDuplicate - works on batch puts', async function (t) {
const db1 = create()
const db2 = create({ alwaysDuplicate: false })

const b1 = db1.batch()
const b2 = db2.batch()

await b1.put('/a', '1')
await b2.put('/a', '1')
await b1.put('/a', '1')
await b2.put('/a', '1')

await b1.flush()
await b2.flush()

t.is(db1.version, 3)
t.is(db2.version, 2)
})

0 comments on commit 9477b3e

Please sign in to comment.