From 17219c1b362ea0d74930a188a91d61141c1ce433 Mon Sep 17 00:00:00 2001 From: Islam Aleiv Date: Tue, 4 Jun 2024 11:22:38 +0200 Subject: [PATCH] Polish --- internal/planner/mapper/select.go | 3 ++- internal/planner/planner.go | 2 ++ internal/planner/select.go | 2 ++ internal/planner/type_join.go | 25 ++++++++++++------------- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/internal/planner/mapper/select.go b/internal/planner/mapper/select.go index 2c245c7287..eebb4033f2 100644 --- a/internal/planner/mapper/select.go +++ b/internal/planner/mapper/select.go @@ -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 } diff --git a/internal/planner/planner.go b/internal/planner/planner.go index b76c55bbb9..f7a875af70 100644 --- a/internal/planner/planner.go +++ b/internal/planner/planner.go @@ -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 diff --git a/internal/planner/select.go b/internal/planner/select.go index 60c895167d..5d7e448c73 100644 --- a/internal/planner/select.go +++ b/internal/planner/select.go @@ -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]) } } diff --git a/internal/planner/type_join.go b/internal/planner/type_join.go index b6ce65dbdd..0b6dd5a57e 100644 --- a/internal/planner/type_join.go +++ b/internal/planner/type_join.go @@ -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 @@ -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 { @@ -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 @@ -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 { @@ -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) @@ -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() @@ -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) { @@ -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