Skip to content

Commit

Permalink
simplify isUpdatingIndexedFields
Browse files Browse the repository at this point in the history
  • Loading branch information
fredcarle committed Apr 12, 2024
1 parent d7b7a32 commit ce9e34a
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions db/collection_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (c *collection) updateIndexedDoc(
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, doc) {
if isUpdatingIndexedFields(index, oldDoc, doc) {
err = index.Update(ctx, txn, oldDoc, doc)
if err != nil {
return err
Expand Down Expand Up @@ -593,16 +593,13 @@ func generateIndexName(col client.Collection, fields []client.IndexedFieldDescri
return sb.String()
}

func isUpdatingIndexedFields(index CollectionIndex, doc *client.Document) bool {
for _, docField := range doc.Fields() {
// It is safe to discard the error here for simplicity.
val, _ := doc.GetValueWithField(docField)
if val.IsDirty() {
for _, indexedFields := range index.Description().Fields {
if indexedFields.Name == docField.Name() {
return true
}
}
func isUpdatingIndexedFields(index CollectionIndex, oldDoc, newDoc *client.Document) bool {
for _, indexedFields := range index.Description().Fields {
// For simplicity, it is safe to discard the errors.
oldVal, _ := oldDoc.GetValue(indexedFields.Name)
newVal, _ := newDoc.GetValue(indexedFields.Name)
if oldVal.Value() != newVal.Value() {
return true
}
}
return false
Expand Down

0 comments on commit ce9e34a

Please sign in to comment.