Skip to content

Commit

Permalink
apply feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
fredcarle committed Apr 16, 2024
1 parent 5d7aff6 commit 1c3ebd7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
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) {
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
}

0 comments on commit 1c3ebd7

Please sign in to comment.