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

fix: Allow update when updating non-indexed field #2511

Conversation

fredcarle
Copy link
Collaborator

Relevant issue(s)

Resolves #2510

Description

This PR adds a check to the index validation step where if the resulting KV pair is the same on update as the previous KV pair, the outcome will be an no-op instead of an index existing error.

Tasks

  • I made sure the code is well commented, particularly hard-to-understand areas.
  • I made sure the repository-held documentation is changed accordingly.
  • I made sure the pull request title adheres to the conventional commit style (the subset used in the project can be found in tools/configs/chglog/config.yml).
  • I made sure to discuss its limitations such as threats to validity, vulnerability to mistake and misuse, robustness to invalidation of assumptions, resource requirements, ...

How has this been tested?

make test

Specify the platform(s) on which this was tested:

  • MacOS

@fredcarle fredcarle added the bug Something isn't working label Apr 9, 2024
@fredcarle fredcarle added this to the DefraDB v0.11 milestone Apr 9, 2024
@fredcarle fredcarle requested a review from a team April 9, 2024 21:21
@fredcarle fredcarle self-assigned this Apr 9, 2024
@fredcarle fredcarle force-pushed the fredcarle/fix/i2510-doc-update-with-unique-index branch 2 times, most recently from 1cf6d64 to d3012ec Compare April 11, 2024 15:14
@fredcarle
Copy link
Collaborator Author

@islamaliev I've move the check like we discussed earlier. Let me know what you think.

Copy link
Contributor

@islamaliev islamaliev left a comment

Choose a reason for hiding this comment

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

Good catch and good job fixing it! I just have some clarifying comments.

db/collection_index.go Outdated Show resolved Hide resolved
testUtils "github.com/sourcenetwork/defradb/tests/integration"
)

func TestUniqueCompositeIndexUpdate_UponUpdatingDocWithExistingFieldValue_ShouldSucceed(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

praise: thanks for adding a test for the composite one.

@fredcarle fredcarle force-pushed the fredcarle/fix/i2510-doc-update-with-unique-index branch 2 times, most recently from ce9e34a to 74a24bc Compare April 12, 2024 04:30
@fredcarle fredcarle force-pushed the fredcarle/fix/i2510-doc-update-with-unique-index branch from 74a24bc to 0e2eb12 Compare April 12, 2024 17:11
return err
// We only need to update the index if one of the indexed fields
// on the document has been changed.
if isUpdatingIndexedFields(index, oldDoc, doc) {
Copy link
Member

@jsimnz jsimnz Apr 15, 2024

Choose a reason for hiding this comment

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

question(cc: @islamaliev): Should this be done inside the index.Update func, instead of here? This will A) allow different index types to handle this differently (if needed) B) Gurantees this behavior is consistent regardless of where index.Update is called from (vs doing it here which is only fixed for this particular call path).

Copy link
Contributor

@islamaliev islamaliev Apr 15, 2024

Choose a reason for hiding this comment

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

I also thought about this, but though it would be unlikely to have any other update behaviour.
Although, thinking about it again we don't know what will be in the future especially when we decide to index array or json fields or some other fancy stuff.

So having this encapsulated inside index itself would be more future-prove.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Moved ✅

@fredcarle fredcarle force-pushed the fredcarle/fix/i2510-doc-update-with-unique-index branch from ffed1d0 to 7ff9422 Compare April 16, 2024 00:25
@fredcarle fredcarle force-pushed the fredcarle/fix/i2510-doc-update-with-unique-index branch from 7ff9422 to 1c3ebd7 Compare April 16, 2024 01:47
@fredcarle fredcarle merged commit 84297bc into sourcenetwork:develop Apr 16, 2024
26 of 28 checks passed
@fredcarle fredcarle deleted the fredcarle/fix/i2510-doc-update-with-unique-index branch April 16, 2024 02:03
Copy link
Contributor

@islamaliev islamaliev left a comment

Choose a reason for hiding this comment

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

LGTM! Just have a final suggestion.

@@ -381,6 +381,11 @@ func (index *collectionUniqueIndex) Update(
oldDoc *client.Document,
newDoc *client.Document,
) error {
// We only need to update the index if one of the indexed fields
// on the document has been changed.
if !isUpdatingIndexedFields(index, oldDoc, newDoc) {
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: I think it also makes sense to do this check for regular indexes otherwise they will do unnecessary work with storage.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll open a new PR to cover that case 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Updating non indexed field of doc with unique index fails
3 participants