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

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 3 additions & 29 deletions db/collection_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,9 @@ func (c *collection) updateIndexedDoc(
}
txn := mustGetContextTxn(ctx)
for _, index := range c.indexes {
// We only need to update the index if one of the indexed fields
// on the document has been changed.
if isUpdatingIndexedFields(index, oldDoc, doc) {
err = index.Update(ctx, txn, oldDoc, doc)
if err != nil {
return err
}
err = index.Update(ctx, txn, oldDoc, doc)
if err != nil {
return err
}
}
return nil
Expand Down Expand Up @@ -594,25 +590,3 @@ func generateIndexName(col client.Collection, fields []client.IndexedFieldDescri
}
return sb.String()
}

func isUpdatingIndexedFields(index CollectionIndex, oldDoc, newDoc *client.Document) bool {
for _, indexedFields := range index.Description().Fields {
oldVal, getOldValErr := oldDoc.GetValue(indexedFields.Name)
newVal, getNewValErr := newDoc.GetValue(indexedFields.Name)

// GetValue will return an error when the field doesn't exist.
// This will happen for oldDoc only if the field hasn't been set
// when first creating the document. For newDoc, this will happen
// only if the field hasn't been set when first creating the document
// AND the field hasn't been set on the update.
switch {
case getOldValErr != nil && getNewValErr != nil:
continue
case getOldValErr != nil && getNewValErr == nil:
return true
case oldVal.Value() != newVal.Value():
return true
}
}
return false
}
27 changes: 27 additions & 0 deletions db/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 👍

return nil
}
newKey, newVal, err := index.prepareIndexRecordToStore(ctx, txn, newDoc)
if err != nil {
return err
Expand All @@ -403,3 +408,25 @@ func (index *collectionUniqueIndex) deleteDocIndex(
}
return index.deleteIndexKey(ctx, txn, key)
}

func isUpdatingIndexedFields(index CollectionIndex, oldDoc, newDoc *client.Document) bool {
for _, indexedFields := range index.Description().Fields {
oldVal, getOldValErr := oldDoc.GetValue(indexedFields.Name)
newVal, getNewValErr := newDoc.GetValue(indexedFields.Name)

// GetValue will return an error when the field doesn't exist.
// This will happen for oldDoc only if the field hasn't been set
// when first creating the document. For newDoc, this will happen
// only if the field hasn't been set when first creating the document
// AND the field hasn't been set on the update.
switch {
case getOldValErr != nil && getNewValErr != nil:
continue
case getOldValErr != nil && getNewValErr == nil:
return true
case oldVal.Value() != newVal.Value():
return true
}
}
return false
}
Loading