Skip to content

Commit

Permalink
Make index check if doc is indexable
Browse files Browse the repository at this point in the history
  • Loading branch information
islamaliev committed May 19, 2023
1 parent f054f1a commit 5fae460
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 33 deletions.
8 changes: 1 addition & 7 deletions db/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,13 +793,7 @@ func (c *collection) indexNewDoc(ctx context.Context, txn datastore.Txn, doc *cl
return err
}
for _, index := range indexes {
indexedFieldName := index.Description().Fields[0].Name
fieldVal, err := doc.Get(indexedFieldName)
if err != nil {
return nil
}
docDataStoreKey := c.getDSKeyFromDockey(doc.Key())
err = index.Save(ctx, txn, docDataStoreKey, fieldVal)
err = index.Save(ctx, txn, doc)
if err != nil {
return err
}
Expand Down
15 changes: 10 additions & 5 deletions db/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

type CollectionIndex interface {
Save(context.Context, datastore.Txn, core.DataStoreKey, any) error
Save(context.Context, datastore.Txn, *client.Document) error
Name() string
Description() client.IndexDescription
}
Expand Down Expand Up @@ -97,17 +97,22 @@ var _ CollectionIndex = (*collectionSimpleIndex)(nil)
func (i *collectionSimpleIndex) Save(
ctx context.Context,
txn datastore.Txn,
key core.DataStoreKey,
val any,
doc *client.Document,
) error {
data, err := i.convertFunc(val)
indexedFieldName := i.desc.Fields[0].Name
fieldVal, err := doc.Get(indexedFieldName)
if err != nil {
return nil
}

data, err := i.convertFunc(fieldVal)
if err != nil {
return NewErrCanNotIndexInvalidFieldValue(err)
}
indexDataStoreKey := core.IndexDataStoreKey{}
indexDataStoreKey.CollectionID = strconv.Itoa(int(i.collection.ID()))
indexDataStoreKey.IndexID = strconv.Itoa(int(i.desc.ID))
indexDataStoreKey.FieldValues = []string{string(data), key.DocKey}
indexDataStoreKey.FieldValues = []string{string(data), doc.Key().String()}
keyStr := indexDataStoreKey.ToDS()
err = txn.Datastore().Put(ctx, keyStr, []byte{})
if err != nil {
Expand Down
42 changes: 21 additions & 21 deletions db/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,27 @@ func TestGetCollectionIndexes_ShouldReturnListOfCollectionIndexes(t *testing.T)
assert.Equal(t, productsIndexDesc, productIndexes[0])
}

func TestGetCollectionIndexes_IfStorageFails_ReturnError(t *testing.T) {
f := newIndexTestFixture(t)
f.createUserCollectionIndexOnName()

f.db.Close(f.ctx)

_, err := f.getCollectionIndexes(usersColName)
assert.Error(t, err)
}

func TestGetCollectionIndexes_IfInvalidIndexIsStored_ReturnError(t *testing.T) {
f := newIndexTestFixture(t)

indexKey := core.NewCollectionIndexKey(usersColName, "users_name_index")
err := f.txn.Systemstore().Put(f.ctx, indexKey.ToDS(), []byte("invalid"))
assert.NoError(t, err)

_, err = f.getCollectionIndexes(usersColName)
assert.ErrorIs(t, err, NewErrInvalidStoredIndex(nil))
}

func TestCollectionGetIndexes_ShouldReturnIndexes(t *testing.T) {
f := newIndexTestFixture(t)

Expand Down Expand Up @@ -688,27 +709,6 @@ func TestCollectionGetIndexes_IfFailsToCreateTxn_ShouldNotCache(t *testing.T) {
assert.Equal(t, testUsersColIndexName, indexes[0].Name)
}

func TestGetCollectionIndexes_IfStorageFails_ReturnError(t *testing.T) {
f := newIndexTestFixture(t)
f.createUserCollectionIndexOnName()

f.db.Close(f.ctx)

_, err := f.getCollectionIndexes(usersColName)
assert.Error(t, err)
}

func TestGetCollectionIndexes_IfInvalidIndexIsStored_ReturnError(t *testing.T) {
f := newIndexTestFixture(t)

indexKey := core.NewCollectionIndexKey(usersColName, "users_name_index")
err := f.txn.Systemstore().Put(f.ctx, indexKey.ToDS(), []byte("invalid"))
assert.NoError(t, err)

_, err = f.getCollectionIndexes(usersColName)
assert.ErrorIs(t, err, NewErrInvalidStoredIndex(nil))
}

func TestCollectionGetIndexes_IfInvalidIndexIsStored_ReturnError(t *testing.T) {
f := newIndexTestFixture(t)

Expand Down
2 changes: 2 additions & 0 deletions db/indexed_docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,10 @@ func TestNonUnique_StoringIndexedFieldValueOfDifferentTypes(t *testing.T) {
testCase := []struct {
Name string
FieldKind client.FieldKind
// FieldVal is the value the index will receive for serialization
FieldVal any
ShouldFail bool
// Stored is the value that is stored as part of the index value key
Stored string
}{
{Name: "invalid int", FieldKind: client.FieldKind_INT, FieldVal: "invalid", ShouldFail: true},
Expand Down

0 comments on commit 5fae460

Please sign in to comment.