Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support cas to return nodes #133

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open

Support cas to return nodes #133

wants to merge 11 commits into from

Conversation

LuKks
Copy link
Contributor

@LuKks LuKks commented Aug 8, 2023

No description provided.

@LuKks LuKks marked this pull request as ready for review August 8, 2023 18:07
index.js Outdated Show resolved Hide resolved
@LuKks
Copy link
Contributor Author

LuKks commented Aug 8, 2023

@mafintosh Ready!

@LuKks LuKks changed the title Support cas callback with prev being null Support cas to return nodes Aug 14, 2023
@LuKks
Copy link
Contributor Author

LuKks commented Aug 28, 2023

@mafintosh Ready!

index.js Outdated
@@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change makes inserts slower for the case where no cas is defined, because it now always runs getKeyNode(), which I believe runs in logarithmic time. I'm not convinced that's a good trade-off to make for this feature--can't we special case the cas-case and keep the old code when no cas is specified?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea this needs to only run on cas

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just make an if

if (cas) {
  const older = ....
  const swap = await cas(older, node)
  if (!swap || swap === older) return true
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it swaps you need to update the value you insert also

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it swaps you need to update the value you insert also

Where am I missing this?

index.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
index.js Outdated
@@ -793,7 +802,7 @@ class Batch {
}
}

return this._append(root, seq, key, value)
return this._append(root, seq, key, enc(encoding.value, newNode.value))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move the encoded value to const value = enc(....) before this line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value exists as arg so I named it valueEncoded

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants