Skip to content

Commit

Permalink
Polish
Browse files Browse the repository at this point in the history
  • Loading branch information
islamaliev committed Jun 4, 2024
1 parent 7fc35a5 commit 17219c1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
3 changes: 2 additions & 1 deletion internal/planner/mapper/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ type Select struct {
// Selects.
Fields []Requestable

// SkipResolve is a flag that indicates that the fields in this Select don't need to be resolved.
// SkipResolve is a flag that indicates that the fields in this Select don't need to be resolved,
// i.e. it's value doesn't need to be fetched and provided to the user.
// It is used to avoid resolving related objects if they are used only in a filter and not requested in a response.
SkipResolve bool
}
Expand Down
2 changes: 2 additions & 0 deletions internal/planner/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,8 @@ func (p *Planner) tryOptimizeJoinDirection(node *invertibleTypeJoin, parentPlan
relatedField,
mapper.Field{Name: subFieldName, Index: subFieldInd},
), relatedField)
// At the moment we just take the first index, but later we want to run some kind of analysis to
// determine which index is best to use. https://github.com/sourcenetwork/defradb/issues/2680
err := node.invertJoinDirectionWithIndex(fieldFilter, indexes[0])
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions internal/planner/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ func findIndexByFieldName(col client.Collection, fieldName string) immutable.Opt
}
indexes := col.Description().GetIndexesOnField(field.Name)
if len(indexes) > 0 {
// At the moment we just take the first index, but later we want to run some kind of analysis to
// determine which index is best to use. https://github.com/sourcenetwork/defradb/issues/2680
return immutable.Some(indexes[0])
}
}
Expand Down
25 changes: 12 additions & 13 deletions internal/planner/type_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ func (join *invertibleTypeJoin) Spans(spans core.Spans) {

func (join *invertibleTypeJoin) Source() planNode { return join.parentSide.plan }

type primaryObjectsFetcher struct {
type primaryObjectsRetriever struct {
relIDFieldDef client.FieldDefinition
primarySide *joinSide
secondarySide *joinSide
Expand All @@ -510,17 +510,17 @@ type primaryObjectsFetcher struct {
resultSecondaryDoc core.Doc
}

func newPrimaryObjectsFetcher(
func newPrimaryObjectsRetriever(
primarySide, secondarySide *joinSide,
) primaryObjectsFetcher {
j := primaryObjectsFetcher{
) primaryObjectsRetriever {
j := primaryObjectsRetriever{
primarySide: primarySide,
secondarySide: secondarySide,
}
return j
}

func (j *primaryObjectsFetcher) fetchPrimaryDocsReferencingSecondaryDoc() error {
func (j *primaryObjectsRetriever) retrievePrimaryDocsReferencingSecondaryDoc() error {
relIDFieldDef, ok := j.primarySide.col.Definition().GetFieldByName(
j.primarySide.relFieldDef.Name + request.RelatedObjectID)
if !ok {
Expand All @@ -531,7 +531,7 @@ func (j *primaryObjectsFetcher) fetchPrimaryDocsReferencingSecondaryDoc() error

j.relIDFieldDef = relIDFieldDef

primaryDocs, err := j.fetchPrimaryDocs()
primaryDocs, err := j.retrievePrimaryDocs()

if err != nil {
return err
Expand All @@ -542,7 +542,7 @@ func (j *primaryObjectsFetcher) fetchPrimaryDocsReferencingSecondaryDoc() error
return nil
}

func (j *primaryObjectsFetcher) addIDFieldToScanner() {
func (j *primaryObjectsRetriever) addIDFieldToScanner() {
found := false
for i := range j.primaryScan.fields {
if j.primaryScan.fields[i].Name == j.relIDFieldDef.Name {
Expand All @@ -555,7 +555,7 @@ func (j *primaryObjectsFetcher) addIDFieldToScanner() {
}
}

func (j *primaryObjectsFetcher) collectDocs(numDocs int) ([]core.Doc, error) {
func (j *primaryObjectsRetriever) collectDocs(numDocs int) ([]core.Doc, error) {
p := j.primarySide.plan
if err := p.Init(); err != nil {
return nil, NewErrSubTypeInit(err)
Expand All @@ -580,7 +580,7 @@ func (j *primaryObjectsFetcher) collectDocs(numDocs int) ([]core.Doc, error) {
return docs, nil
}

func (j *primaryObjectsFetcher) fetchPrimaryDocs() ([]core.Doc, error) {
func (j *primaryObjectsRetriever) retrievePrimaryDocs() ([]core.Doc, error) {
j.addIDFieldToScanner()

secondaryDoc := j.secondarySide.plan.Value()
Expand Down Expand Up @@ -649,9 +649,9 @@ func joinPrimaryDocs(primaryDocs []core.Doc, secondarySide, primarySide *joinSid
}

func (join *invertibleTypeJoin) fetchPrimaryDocsReferencingSecondaryDoc() ([]core.Doc, core.Doc, error) {
secJoiner := newPrimaryObjectsFetcher(join.getPrimarySide(), join.getSecondarySide())
err := secJoiner.fetchPrimaryDocsReferencingSecondaryDoc()
return secJoiner.resultPrimaryDocs, secJoiner.resultSecondaryDoc, err
retriever := newPrimaryObjectsRetriever(join.getPrimarySide(), join.getSecondarySide())
err := retriever.retrievePrimaryDocsReferencingSecondaryDoc()
return retriever.resultPrimaryDocs, retriever.resultSecondaryDoc, err
}

func (join *invertibleTypeJoin) Next() (bool, error) {
Expand Down Expand Up @@ -713,7 +713,6 @@ func (join *invertibleTypeJoin) nextJoinedSecondaryDoc() (bool, error) {
join.encounteredDocIDs = append(join.encounteredDocIDs, secondaryDocID)
}

// check if there can ever be false (a.k.a. hasDoc = false)
hasDoc, err := fetchDocWithID(secondSide.plan, secondaryDocID)
if err != nil {
return false, err
Expand Down

0 comments on commit 17219c1

Please sign in to comment.