Skip to content

Commit

Permalink
Fix swap to keep older one
Browse files Browse the repository at this point in the history
  • Loading branch information
LuKks committed Aug 14, 2023
1 parent 0811b65 commit 2bcb3d3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ class TreeNode {
c = b4a.compare(key.value, await this.getKey(mid))

if (c === 0) {
if (cas && !(await cas((await this.getKeyNode(mid)).final(encoding), node))) return true
const older = (await this.getKeyNode(mid)).final(encoding)
const swap = cas ? (await cas(older, node)) : null
if (cas && (!swap || swap === older)) return true

this.changed = true
this.keys[mid] = key
return true
Expand Down Expand Up @@ -764,7 +767,9 @@ class Batch {
c = b4a.compare(target.value, await node.getKey(mid))

if (c === 0) {
if (cas && !(await cas((await node.getKeyNode(mid)).final(encoding), newNode))) return this._unlockMaybe()
const older = (await node.getKeyNode(mid)).final(encoding)
const swap = cas ? (await cas(older, newNode)) : null
if (cas && (!swap || swap === older)) return this._unlockMaybe()

node.setKey(mid, target)
return this._append(root, seq, key, enc(encoding.value, newNode.value))
Expand Down
18 changes: 18 additions & 0 deletions test/cas.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ test('cas - should not swap', async function (t) {
t.alike(await db.get('/a'), { seq: 1, key: '/a', value: 1 })
})

test('cas - swap but keep older one', async function (t) {
const db = create({ valueEncoding: 'json' })

await db.put('/a', 0)

t.alike(await db.get('/a'), { seq: 1, key: '/a', value: 0 })

await db.put('/a', 99, {
cas: function (prev, next) {
t.alike(prev, { seq: 1, key: '/a', value: 0 })
t.alike(next, { seq: 2, key: '/a', value: 99 })
return prev
}
})

t.alike(await db.get('/a'), { seq: 1, key: '/a', value: 0 })
})

test('cas is called when prev does not exists', async function (t) {
t.plan(6)

Expand Down

0 comments on commit 2bcb3d3

Please sign in to comment.