Skip to content

Commit

Permalink
move index check
Browse files Browse the repository at this point in the history
  • Loading branch information
fredcarle committed Apr 11, 2024
1 parent b01eb2f commit 1cf6d64
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
25 changes: 22 additions & 3 deletions db/collection_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,13 @@ func (c *collection) updateIndexedDoc(
return err
}
for _, index := range c.indexes {
err = index.Update(ctx, txn, oldDoc, doc)
if err != nil {
return err
// We only need to update the index if one of the indexed fields
// on the document has been changed.
if isUpdatingIndexedFields(index, doc) {
err = index.Update(ctx, txn, oldDoc, doc)
if err != nil {
return err
}
}
}
return nil
Expand Down Expand Up @@ -588,3 +592,18 @@ 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
}
}
}
}
return false
}
15 changes: 0 additions & 15 deletions db/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
package db

import (
"bytes"
"context"
"time"

Expand Down Expand Up @@ -363,15 +362,6 @@ func (index *collectionUniqueIndex) prepareIndexRecordToStore(
return core.IndexDataStoreKey{}, nil, err
}
if exists {
if oldDoc != nil {
oldKey, oldVal, err := index.getDocumentsIndexRecord(oldDoc)
if err != nil {
return core.IndexDataStoreKey{}, nil, err
}
if oldKey.ToString() == key.ToString() && bytes.Equal(oldVal, val) {
return core.IndexDataStoreKey{}, nil, nil
}
}
return core.IndexDataStoreKey{}, nil, index.newUniqueIndexError(doc)
}
}
Expand All @@ -396,11 +386,6 @@ func (index *collectionUniqueIndex) Update(
if err != nil {
return err
}
if newKey.ToString() == "" {
// This will happen when the updated doc results in the same key-value pair.
// The outcome is a no-op.
return nil
}
err = index.deleteDocIndex(ctx, txn, oldDoc)
if err != nil {
return err
Expand Down

0 comments on commit 1cf6d64

Please sign in to comment.