diff --git a/db/collection_index.go b/db/collection_index.go index 4fa1578037..0c1921dd62 100644 --- a/db/collection_index.go +++ b/db/collection_index.go @@ -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 @@ -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 -} diff --git a/db/index.go b/db/index.go index c041d3945c..693a18a5bf 100644 --- a/db/index.go +++ b/db/index.go @@ -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 @@ -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 +}