Skip to content

Commit

Permalink
add index equality check
Browse files Browse the repository at this point in the history
  • Loading branch information
fredcarle committed Apr 9, 2024
1 parent bd3dee6 commit 1f104b9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
20 changes: 18 additions & 2 deletions db/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package db

import (
"bytes"
"context"
"time"

Expand Down Expand Up @@ -303,7 +304,7 @@ func (index *collectionUniqueIndex) Save(
txn datastore.Txn,
doc *client.Document,
) error {
key, val, err := index.prepareIndexRecordToStore(ctx, txn, doc)
key, val, err := index.prepareIndexRecordToStore(ctx, txn, doc, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -349,6 +350,7 @@ func (index *collectionUniqueIndex) prepareIndexRecordToStore(
ctx context.Context,
txn datastore.Txn,
doc *client.Document,
oldDoc *client.Document,
) (core.IndexDataStoreKey, []byte, error) {
key, val, err := index.getDocumentsIndexRecord(doc)
if err != nil {
Expand All @@ -361,6 +363,15 @@ 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.Compare(oldVal, val) == 0 {

Check failure on line 371 in db/index.go

View workflow job for this annotation

GitHub Actions / Lint GoLang job

S1004: should use bytes.Equal(oldVal, val) instead (gosimple)
return core.IndexDataStoreKey{}, nil, nil
}
}
return core.IndexDataStoreKey{}, nil, index.newUniqueIndexError(doc)
}
}
Expand All @@ -381,10 +392,15 @@ func (index *collectionUniqueIndex) Update(
oldDoc *client.Document,
newDoc *client.Document,
) error {
newKey, newVal, err := index.prepareIndexRecordToStore(ctx, txn, newDoc)
newKey, newVal, err := index.prepareIndexRecordToStore(ctx, txn, newDoc, oldDoc)
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
1 change: 0 additions & 1 deletion tests/integration/index/update_unique_composite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func TestUniqueCompositeIndexUpdate_UponUpdatingDocWithExistingFieldValue_Should
{
"email": "another@gmail.com"
}`,
ExpectedError: "can not index a doc's field(s) that violates unique index",
},
},
}
Expand Down
1 change: 0 additions & 1 deletion tests/integration/index/update_unique_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func TestUniqueIndexUpdate_UponUpdatingDocNonIndexedField_ShouldSucceed(t *testi
{
"age": 37
}`,
ExpectedError: "can not index a doc's field(s) that violates unique index",
},
},
}
Expand Down

0 comments on commit 1f104b9

Please sign in to comment.