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 17, 2023
1 parent 3f99bca commit 6e13d5a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 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
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 6e13d5a

Please sign in to comment.