Skip to content

Commit

Permalink
Replace sourceInfo with Collection
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Oct 2, 2023
1 parent d8f26fc commit e08bcaf
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 30 deletions.
16 changes: 4 additions & 12 deletions planner/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,9 @@ import (
"github.com/sourcenetwork/defradb/planner/mapper"
)

// sourceInfo stores info about the data source
type sourceInfo struct {
collectionDescription client.CollectionDescription
// and more
}

type planSource struct {
info sourceInfo
plan planNode
collection client.Collection
plan planNode
}

func (p *Planner) getSource(parsed *mapper.Select) (planSource, error) {
Expand All @@ -43,9 +37,7 @@ func (p *Planner) getCollectionScanPlan(parsed *mapper.Select) (planSource, erro
}

return planSource{
plan: scan,
info: sourceInfo{
collectionDescription: col.Description(),
},
plan: scan,
collection: col,
}, nil
}
2 changes: 1 addition & 1 deletion planner/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func (p *Planner) expandGroupNodePlan(topNodeSelect *selectTopNode) error {
childSelect,
pipe,
false,
&topNodeSelect.selectNode.sourceInfo,
topNodeSelect.selectNode.collection,
)
if err != nil {
return err
Expand Down
17 changes: 8 additions & 9 deletions planner/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
cid "github.com/ipfs/go-cid"
"github.com/sourcenetwork/immutable"

"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/client/request"
"github.com/sourcenetwork/defradb/core"
"github.com/sourcenetwork/defradb/db/base"
Expand Down Expand Up @@ -101,9 +102,7 @@ type selectNode struct {
// was created
origSource planNode

// cache information about the original data source
// collection name, meta-data, etc.
sourceInfo sourceInfo
collection client.Collection

// top level filter expression
// filter is split between select, scan, and typeIndexJoin.
Expand Down Expand Up @@ -244,7 +243,7 @@ func (n *selectNode) initSource() ([]aggregateNode, error) {
}
n.source = sourcePlan.plan
n.origSource = sourcePlan.plan
n.sourceInfo = sourcePlan.info
n.collection = sourcePlan.collection

// split filter
// apply the root filter to the source
Expand Down Expand Up @@ -278,7 +277,7 @@ func (n *selectNode) initSource() ([]aggregateNode, error) {
// instead of a prefix scan + filter via the Primary Index (0), like here:
spans := make([]core.Span, len(n.selectReq.DocKeys.Value()))
for i, docKey := range n.selectReq.DocKeys.Value() {
dockeyIndexKey := base.MakeDocKey(sourcePlan.info.collectionDescription, docKey)
dockeyIndexKey := base.MakeDocKey(sourcePlan.collection.Description(), docKey)
spans[i] = core.NewSpan(dockeyIndexKey, dockeyIndexKey.PrefixEnd())
}
origScan.Spans(core.NewSpans(spans...))
Expand Down Expand Up @@ -404,7 +403,7 @@ func (p *Planner) SelectFromSource(
selectReq *mapper.Select,
source planNode,
fromCollection bool,
providedSourceInfo *sourceInfo,
collection client.Collection,
) (planNode, error) {
s := &selectNode{
planner: p,
Expand All @@ -419,8 +418,8 @@ func (p *Planner) SelectFromSource(
orderBy := selectReq.OrderBy
groupBy := selectReq.GroupBy

if providedSourceInfo != nil {
s.sourceInfo = *providedSourceInfo
if collection != nil {
s.collection = collection
}

if fromCollection {
Expand All @@ -429,7 +428,7 @@ func (p *Planner) SelectFromSource(
return nil, err
}

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

aggregates, err := s.initFields(selectReq)
Expand Down
14 changes: 6 additions & 8 deletions planner/type_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ func (p *Planner) makeTypeIndexJoin(
var joinPlan planNode
var err error

desc := parent.sourceInfo.collectionDescription
typeFieldDesc, ok := desc.Schema.GetField(subType.Name)
typeFieldDesc, ok := parent.collection.Schema().GetField(subType.Name)
if !ok {
return nil, client.NewErrFieldNotExist(subType.Name)
}
Expand Down Expand Up @@ -245,7 +244,7 @@ func (p *Planner) makeTypeJoinOne(
}

// get the correct sub field schema type (collection)
subTypeFieldDesc, ok := parent.sourceInfo.collectionDescription.Schema.GetField(subType.Name)
subTypeFieldDesc, ok := parent.collection.Schema().GetField(subType.Name)
if !ok {
return nil, client.NewErrFieldNotExist(subType.Name)
}
Expand All @@ -262,7 +261,7 @@ func (p *Planner) makeTypeJoinOne(
subTypeSchema := subTypeCollection.Schema()
subTypeField, subTypeFieldNameFound := subTypeCollection.Description().GetFieldByRelation(
subTypeFieldDesc.RelationName,
parent.sourceInfo.collectionDescription.Name,
parent.collection.Name(),
subTypeFieldDesc.Name,
&subTypeSchema,
)
Expand Down Expand Up @@ -369,8 +368,7 @@ func (n *typeJoinOne) valuesPrimary(doc core.Doc) (core.Doc, error) {

// create the collection key for the sub doc
slct := n.subType.(*selectTopNode).selectNode
desc := slct.sourceInfo.collectionDescription
subKeyIndexKey := base.MakeDocKey(desc, subDocKeyStr)
subKeyIndexKey := base.MakeDocKey(slct.collection.Description(), subDocKeyStr)

// reset span
n.spans = core.NewSpans(core.NewSpan(subKeyIndexKey, subKeyIndexKey.PrefixEnd()))
Expand Down Expand Up @@ -477,7 +475,7 @@ func (p *Planner) makeTypeJoinMany(
return nil, err
}

subTypeFieldDesc, ok := parent.sourceInfo.collectionDescription.Schema.GetField(subType.Name)
subTypeFieldDesc, ok := parent.collection.Schema().GetField(subType.Name)
if !ok {
return nil, client.NewErrFieldNotExist(subType.Name)
}
Expand All @@ -490,7 +488,7 @@ func (p *Planner) makeTypeJoinMany(
subTypeSchema := subTypeCollection.Schema()
rootField, rootNameFound := subTypeCollection.Description().GetFieldByRelation(
subTypeFieldDesc.RelationName,
parent.sourceInfo.collectionDescription.Name,
parent.collection.Name(),
subTypeFieldDesc.Name,
&subTypeSchema,
)
Expand Down

0 comments on commit e08bcaf

Please sign in to comment.