Skip to content

Commit

Permalink
WIP - Replace planner.getCollectionDesc with db.GetCollectionByName
Browse files Browse the repository at this point in the history
They both do pretty much the same thing, only the db is a public func and returns a collection instead of just its description.
  • Loading branch information
AndrewSisley committed Oct 2, 2023
1 parent 71a0305 commit d8f26fc
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 41 deletions.
31 changes: 2 additions & 29 deletions planner/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@
package planner

import (
"encoding/json"

"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/core"
"github.com/sourcenetwork/defradb/errors"
"github.com/sourcenetwork/defradb/planner/mapper"
)

Expand All @@ -36,7 +32,7 @@ func (p *Planner) getSource(parsed *mapper.Select) (planSource, error) {
}

func (p *Planner) getCollectionScanPlan(parsed *mapper.Select) (planSource, error) {
colDesc, err := p.getCollectionDesc(parsed.CollectionName)
col, err := p.db.GetCollectionByName(p.ctx, parsed.CollectionName)
if err != nil {
return planSource{}, err
}
Expand All @@ -49,30 +45,7 @@ func (p *Planner) getCollectionScanPlan(parsed *mapper.Select) (planSource, erro
return planSource{
plan: scan,
info: sourceInfo{
collectionDescription: colDesc,
collectionDescription: col.Description(),
},
}, nil
}

func (p *Planner) getCollectionDesc(name string) (client.CollectionDescription, error) {
collectionKey := core.NewCollectionKey(name)
var desc client.CollectionDescription
schemaVersionIdBytes, err := p.txn.Systemstore().Get(p.ctx, collectionKey.ToDS())
if err != nil {
return desc, errors.Wrap("failed to get collection description", err)
}

schemaVersionId := string(schemaVersionIdBytes)
schemaVersionKey := core.NewCollectionSchemaVersionKey(schemaVersionId)
buf, err := p.txn.Systemstore().Get(p.ctx, schemaVersionKey.ToDS())
if err != nil {
return desc, err
}

err = json.Unmarshal(buf, &desc)
if err != nil {
return desc, err
}

return desc, nil
}
4 changes: 2 additions & 2 deletions planner/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,12 +424,12 @@ func (p *Planner) SelectFromSource(
}

if fromCollection {
desc, err := p.getCollectionDesc(selectReq.Name)
col, err := p.db.GetCollectionByName(p.ctx, selectReq.Name)
if err != nil {
return nil, err
}

s.sourceInfo = sourceInfo{desc}
s.sourceInfo = sourceInfo{col.Description()}
}

aggregates, err := s.initFields(selectReq)
Expand Down
8 changes: 4 additions & 4 deletions planner/sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ func (p *Planner) isValueFloat(
}

if !source.ChildTarget.HasValue {
parentDescription, err := p.getCollectionDesc(parent.CollectionName)
parentCollection, err := p.db.GetCollectionByName(p.ctx, parent.CollectionName)
if err != nil {
return false, err
}

fieldDescription, fieldDescriptionFound := parentDescription.Schema.GetField(source.Name)
fieldDescription, fieldDescriptionFound := parentCollection.Schema().GetField(source.Name)
if !fieldDescriptionFound {
return false, client.NewErrFieldNotExist(source.Name)
}
Expand Down Expand Up @@ -125,12 +125,12 @@ func (p *Planner) isValueFloat(
return false, nil
}

childCollectionDescription, err := p.getCollectionDesc(child.CollectionName)
childCollection, err := p.db.GetCollectionByName(p.ctx, child.CollectionName)
if err != nil {
return false, err
}

fieldDescription, fieldDescriptionFound := childCollectionDescription.Schema.GetField(source.ChildTarget.Name)
fieldDescription, fieldDescriptionFound := childCollection.Schema().GetField(source.ChildTarget.Name)
if !fieldDescriptionFound {
return false, client.NewErrFieldNotExist(source.ChildTarget.Name)
}
Expand Down
14 changes: 8 additions & 6 deletions planner/type_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,16 +254,17 @@ func (p *Planner) makeTypeJoinOne(
// check if the field we're querying is the primary side of the relation
isPrimary := subTypeFieldDesc.RelationType.IsSet(client.Relation_Type_Primary)

subTypeCollectionDesc, err := p.getCollectionDesc(subType.CollectionName)
subTypeCollection, err := p.db.GetCollectionByName(p.ctx, subType.CollectionName)
if err != nil {
return nil, err
}

subTypeField, subTypeFieldNameFound := subTypeCollectionDesc.GetFieldByRelation(
subTypeSchema := subTypeCollection.Schema()
subTypeField, subTypeFieldNameFound := subTypeCollection.Description().GetFieldByRelation(
subTypeFieldDesc.RelationName,
parent.sourceInfo.collectionDescription.Name,
subTypeFieldDesc.Name,
&subTypeCollectionDesc.Schema,
&subTypeSchema,
)
if !subTypeFieldNameFound {
return nil, client.NewErrFieldNotExist(subTypeFieldDesc.RelationName)
Expand Down Expand Up @@ -481,16 +482,17 @@ func (p *Planner) makeTypeJoinMany(
return nil, client.NewErrFieldNotExist(subType.Name)
}

subTypeCollectionDesc, err := p.getCollectionDesc(subType.CollectionName)
subTypeCollection, err := p.db.GetCollectionByName(p.ctx, subType.CollectionName)
if err != nil {
return nil, err
}

rootField, rootNameFound := subTypeCollectionDesc.GetFieldByRelation(
subTypeSchema := subTypeCollection.Schema()
rootField, rootNameFound := subTypeCollection.Description().GetFieldByRelation(
subTypeFieldDesc.RelationName,
parent.sourceInfo.collectionDescription.Name,
subTypeFieldDesc.Name,
&subTypeCollectionDesc.Schema,
&subTypeSchema,
)

if !rootNameFound {
Expand Down

0 comments on commit d8f26fc

Please sign in to comment.