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

added onlyIfChanged opt #98

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

added onlyIfChanged opt #98

wants to merge 3 commits into from

Conversation

rafapaezbas
Copy link
Contributor

No description provided.

@rafapaezbas rafapaezbas force-pushed the only-if-changed-opt branch 2 times, most recently from 965a159 to dbf5c5b Compare April 14, 2023 11:39
index.js Outdated Show resolved Hide resolved
@@ -616,7 +618,7 @@ class Batch {
async put (key, value, opts) {
const release = this.batchLock ? await this.batchLock() : null

const cas = (opts && opts.cas) || null
const cas = (opts && opts.cas) || (this.tree.onlyIfChanged ? sameData : null)
Copy link
Contributor

Choose a reason for hiding this comment

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

That's a tricky one in terms of API. What's the intended behaviour if a user specified onlyIfChanged: true, but also passes an explicit cas?

ATM it will ignore onlyIfChanged if the cas is passed. The alternative would be to run both the cas and the onlyIfChanged, and to proceed only if both pass. Not saying that is better, but it's important to explicitly decide what the behaviour should be.

Also in terms of API: I would also allow passing onlyIfChanged as an option to put and del

Copy link
Contributor Author

@rafapaezbas rafapaezbas May 16, 2023

Choose a reason for hiding this comment

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

Yeah, I think the intended behavior is that cas overwrites onlyIfChanged.

I think OnlyIfChanged option for put sounds like a good idea, for del probably it doesnt make sense since the default behavior is the same.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

API is ok because method option overrides constructor option, just like with Hypercore timeout options, etc

package.json Outdated Show resolved Hide resolved
test/basic.js Outdated
}
})

test('onlyIfChanged del', async function (t) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure what's going on here, but it seems as if the cas never gets called on a delete, also when it's specified. Is it possible that a delete just does not run when there's nothing to delete?

(you can verify by setting onlyIfChanged to false, the test will still pass)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, you are right, the default onlyInChanged delete cas (prev) => prev.value !== null, does not affect the behavior of the del operations at all. That means onlyIfChanged affects only put operations. I removed the useless logic from the PR.

@rafapaezbas rafapaezbas force-pushed the only-if-changed-opt branch from dbf5c5b to 5d726ea Compare May 16, 2023 09:13
@@ -62,6 +62,7 @@ Make a new Hyperbee instance. `core` should be a [Hypercore](https://github.com/
{
keyEncoding: 'binary', // "binary" (default), "utf-8", "ascii", "json", or an abstract-encoding
valueEncoding: 'binary' // Same options as keyEncoding like "json", etc
onlyIfChanged: true // put a value only if it means a change in the db
Copy link
Contributor

Choose a reason for hiding this comment

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

Alternative name: changeOnly

Copy link
Contributor

Choose a reason for hiding this comment

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

Or casChanges

Copy link
Contributor

Choose a reason for hiding this comment

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

I like the boolean option, but just maybe a different API could be a cas option in the constructor, so user can pass its global comparator

@@ -62,6 +62,7 @@ Make a new Hyperbee instance. `core` should be a [Hypercore](https://github.com/
{
keyEncoding: 'binary', // "binary" (default), "utf-8", "ascii", "json", or an abstract-encoding
valueEncoding: 'binary' // Same options as keyEncoding like "json", etc
onlyIfChanged: true // put a value only if it means a change in the db
Copy link
Contributor

@LuKks LuKks May 16, 2023

Choose a reason for hiding this comment

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

Should say false because that's the actual default

@@ -16,6 +16,7 @@
"protocol-buffers-encodings": "^1.2.0",
"ready-resource": "^1.0.0",
"safety-catch": "^1.0.2",
"same-data": "^1.0.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

In which case would same-data not be enough? There is also same-object (what brittle uses)

Just asking because people might use the option to later realize that there are duplicated inserts due same-data not supporting the case?

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