Skip to content

Commit

Permalink
Pass in schema to GetFieldByName
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Oct 2, 2023
1 parent 9b65924 commit 0f37f7b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
21 changes: 11 additions & 10 deletions client/descriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ func (col CollectionDescription) GetFieldByID(id FieldID, schema *SchemaDescript
return FieldDescription{}, false
}

// GetFieldByName returns the field for the given field name. If such a field is found it
// will return it and true, if it is not found it will return false.
func (col CollectionDescription) GetFieldByName(fieldName string, schema *SchemaDescription) (FieldDescription, bool) {
for _, field := range schema.Fields {
if field.Name == fieldName {
return field, true
}
}
return FieldDescription{}, false
}

// GetFieldByRelation returns the field that supports the relation of the given name.
func (col CollectionDescription) GetFieldByRelation(
relationName string,
Expand Down Expand Up @@ -92,16 +103,6 @@ type SchemaDescription struct {
Fields []FieldDescription
}

// GetFieldKey returns the field ID for the given field name.
func (sd SchemaDescription) GetFieldKey(fieldName string) uint32 {
for _, field := range sd.Fields {
if field.Name == fieldName {
return uint32(field.ID)
}
}
return uint32(0)
}

// GetField returns the field of the given name.
func (sd SchemaDescription) GetField(name string) (FieldDescription, bool) {
for _, field := range sd.Fields {
Expand Down
8 changes: 7 additions & 1 deletion db/base/collection_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func MakeDocKey(col client.CollectionDescription, docKey string) core.DataStoreK

func MakePrimaryIndexKeyForCRDT(
c client.CollectionDescription,
schema client.SchemaDescription,
ctype client.CType,
key core.DataStoreKey,
fieldName string,
Expand All @@ -42,7 +43,12 @@ func MakePrimaryIndexKeyForCRDT(
case client.COMPOSITE:
return MakeCollectionKey(c).WithInstanceInfo(key).WithFieldId(core.COMPOSITE_NAMESPACE), nil
case client.LWW_REGISTER:
return MakeCollectionKey(c).WithInstanceInfo(key).WithFieldId(fmt.Sprint(c.Schema.GetFieldKey(fieldName))), nil
field, ok := c.GetFieldByName(fieldName, &schema)
if !ok {
return core.DataStoreKey{}, client.NewErrFieldNotExist(fieldName)
}

return MakeCollectionKey(c).WithInstanceInfo(key).WithFieldId(fmt.Sprint(field.ID)), nil
}
return core.DataStoreKey{}, ErrInvalidCrdtType
}
8 changes: 4 additions & 4 deletions db/fetcher/versioned.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,13 +357,13 @@ func (vf *VersionedFetcher) merge(c cid.Cid) error {
return err
}

fieldID := vf.col.Schema.GetFieldKey(l.Name)
if fieldID == uint32(0) {
field, ok := vf.col.GetFieldByName(l.Name, &vf.col.Schema)
if !ok {
return client.NewErrFieldNotExist(l.Name)
}
// @todo: Right now we ONLY handle LWW_REGISTER, need to swith on this and
// get CType from descriptions
if err := vf.processNode(fieldID, subNd, client.LWW_REGISTER, l.Name); err != nil {
if err := vf.processNode(uint32(field.ID), subNd, client.LWW_REGISTER, l.Name); err != nil {
return err
}
}
Expand All @@ -380,7 +380,7 @@ func (vf *VersionedFetcher) processNode(
// handle CompositeDAG
mcrdt, exists := vf.mCRDTs[crdtIndex]
if !exists {
key, err := base.MakePrimaryIndexKeyForCRDT(*vf.col, ctype, vf.key, fieldName)
key, err := base.MakePrimaryIndexKeyForCRDT(*vf.col, vf.col.Schema, ctype, vf.key, fieldName)
if err != nil {
return err
}
Expand Down

0 comments on commit 0f37f7b

Please sign in to comment.