diff --git a/query/graphql/mapper/commitSelect.go b/query/graphql/mapper/commitSelect.go index b69eb91350..3bf7e57dcf 100644 --- a/query/graphql/mapper/commitSelect.go +++ b/query/graphql/mapper/commitSelect.go @@ -12,15 +12,6 @@ package mapper import "github.com/sourcenetwork/defradb/client" -// CommitType represents a type of [CommitSelect] -type CommitType int - -const ( - NoneCommitType = CommitType(iota) - LatestCommits - Commits -) - // CommitSelect represents a commit request from a consumer. // // E.g. commits, or latestCommits. @@ -28,9 +19,6 @@ type CommitSelect struct { // The underlying Select, defining the information requested. Select - // The type of commit select request. - Type CommitType - // The key of the target document for which to get commits for. DocKey string @@ -52,7 +40,6 @@ func (s *CommitSelect) cloneTo(index int) *CommitSelect { return &CommitSelect{ Select: *s.Select.cloneTo(index), DocKey: s.DocKey, - Type: s.Type, FieldName: s.FieldName, Cid: s.Cid, } diff --git a/query/graphql/mapper/mapper.go b/query/graphql/mapper/mapper.go index abf14c98aa..eeb91a0615 100644 --- a/query/graphql/mapper/mapper.go +++ b/query/graphql/mapper/mapper.go @@ -791,7 +791,6 @@ func ToCommitSelect(ctx context.Context, txn datastore.Txn, parsed *parser.Commi return &CommitSelect{ Select: *underlyingSelect, DocKey: parsed.DocKey, - Type: CommitType(parsed.Type), FieldName: parsed.FieldName, Depth: parsed.Depth, Cid: parsed.Cid, diff --git a/query/graphql/parser/commit.go b/query/graphql/parser/commit.go index 36ac109170..f55c6ef560 100644 --- a/query/graphql/parser/commit.go +++ b/query/graphql/parser/commit.go @@ -16,24 +16,11 @@ import ( "github.com/graphql-go/graphql/language/ast" "github.com/sourcenetwork/defradb/client" - "github.com/sourcenetwork/defradb/errors" + "github.com/sourcenetwork/defradb/core" parserTypes "github.com/sourcenetwork/defradb/query/graphql/parser/types" ) -type CommitType int - -const ( - NoneCommitType = CommitType(iota) - LatestCommits - Commits -) - var ( - commitNameToType = map[string]CommitType{ - "latestCommits": LatestCommits, - "commits": Commits, - } - _ Selection = (*CommitSelect)(nil) ) @@ -41,7 +28,6 @@ type CommitSelect struct { Alias string Name string - Type CommitType DocKey string FieldName client.Option[string] Cid string @@ -75,12 +61,6 @@ func parseCommitSelect(field *ast.Field) (*CommitSelect, error) { Alias: getFieldAlias(field), } - var ok bool - commit.Type, ok = commitNameToType[commit.Name] - if !ok { - return nil, errors.New("Unknown Database query") - } - for _, argument := range field.Arguments { prop := argument.Name.Value if prop == parserTypes.DocKey { @@ -142,6 +122,19 @@ func parseCommitSelect(field *ast.Field) (*CommitSelect, error) { } } + // latestCommits is just syntax sugar around a commits query + if commit.Name == parserTypes.LatestCommitsQueryName { + // Depth is not exposed as an input parameter for latestCommits, + // so we can blindly set it here without worrying about existing + // values + commit.Depth = client.Some(uint64(1)) + + if !commit.FieldName.HasValue() { + // latest commits defaults to composite commits only at the moment + commit.FieldName = client.Some(core.COMPOSITE_NAMESPACE) + } + } + // no sub fields (unlikely) if field.SelectionSet == nil { return commit, nil diff --git a/query/graphql/parser/types/types.go b/query/graphql/parser/types/types.go index 1a6667f0fb..8254e29e8f 100644 --- a/query/graphql/parser/types/types.go +++ b/query/graphql/parser/types/types.go @@ -84,6 +84,8 @@ const ( ExplainLabel = "explain" + LatestCommitsQueryName = "latestCommits" + CommitTypeName = "Commit" LinksFieldName = "links" HeightFieldName = "height" diff --git a/query/graphql/planner/commit.go b/query/graphql/planner/commit.go index b628b15292..07460ad3a4 100644 --- a/query/graphql/planner/commit.go +++ b/query/graphql/planner/commit.go @@ -16,7 +16,6 @@ import ( cid "github.com/ipfs/go-cid" "github.com/sourcenetwork/defradb/core" - "github.com/sourcenetwork/defradb/errors" "github.com/sourcenetwork/defradb/query/graphql/mapper" ) @@ -107,20 +106,11 @@ func (n *commitSelectNode) Explain() (map[string]any, error) { } func (p *Planner) CommitSelect(parsed *mapper.CommitSelect) (planNode, error) { - // check type of commit select (all, latest, one) - var commit *commitSelectNode - var err error - switch parsed.Type { - case mapper.LatestCommits: - commit, err = p.commitSelectLatest(parsed) - case mapper.Commits: - commit, err = p.commitSelectAll(parsed) - default: - return nil, errors.New("Invalid CommitSelect type") - } + commit, err := p.buildCommitSelectNode(parsed) if err != nil { return nil, err } + plan, err := p.SelectFromSource(&parsed.Select, commit, false, nil) if err != nil { return nil, err @@ -132,34 +122,7 @@ func (p *Planner) CommitSelect(parsed *mapper.CommitSelect) (planNode, error) { }, nil } -// commitSelectLatest is a CommitSelect node initalized with a headsetScanNode and a DocKey -func (p *Planner) commitSelectLatest(parsed *mapper.CommitSelect) (*commitSelectNode, error) { - headset := p.HeadScan(parsed) - dag := p.DAGScan(parsed, headset) - // @todo: Get Collection field ID - if !parsed.FieldName.HasValue() { - dag.field = core.COMPOSITE_NAMESPACE - } else { - dag.field = parsed.FieldName.Value() - } - - if parsed.DocKey != "" { - key := core.DataStoreKey{}.WithDocKey(parsed.DocKey).WithFieldId(dag.field) - headset.key = key - } - - commit := &commitSelectNode{ - p: p, - source: dag, - docMapper: docMapper{&parsed.DocumentMapping}, - } - - return commit, nil -} - -// commitSelectAll is a CommitSelect initialized with a headsetScanNode, and will -// recursively return all graph commits in order. -func (p *Planner) commitSelectAll(parsed *mapper.CommitSelect) (*commitSelectNode, error) { +func (p *Planner) buildCommitSelectNode(parsed *mapper.CommitSelect) (*commitSelectNode, error) { headset := p.HeadScan(parsed) dag := p.DAGScan(parsed, headset) @@ -191,11 +154,12 @@ func (p *Planner) commitSelectAll(parsed *mapper.CommitSelect) (*commitSelectNod } if parsed.Depth.HasValue() { - dag.depthLimit = uint32(parsed.Depth.Value()) + dag.depthLimit = parsed.Depth.Value() } else { // infinite depth - dag.depthLimit = math.MaxUint32 + dag.depthLimit = math.MaxUint64 } + // dag.key = &key commit := &commitSelectNode{ p: p, diff --git a/query/graphql/planner/dagscan.go b/query/graphql/planner/dagscan.go index 0fe9255e89..ce81b65b96 100644 --- a/query/graphql/planner/dagscan.go +++ b/query/graphql/planner/dagscan.go @@ -146,8 +146,8 @@ type dagScanNode struct { // since the depth check is done after the // block scan. // If we need an infinite depth, use math.MaxUint32 - depthLimit uint32 - depthVisited uint32 + depthLimit uint64 + depthVisited uint64 visitedNodes map[string]bool queuedCids *list.List @@ -312,7 +312,7 @@ func (n *dagScanNode) Next() (bool, error) { n.depthVisited++ n.visitedNodes[n.currentCid.String()] = true // mark the current node as "visited" if n.depthVisited < n.depthLimit { - // look for HEAD links + // traverse the merkle dag to fetch related commits for _, h := range heads { // queue our found heads n.queuedCids.PushFront(h.Cid) diff --git a/query/graphql/planner/select.go b/query/graphql/planner/select.go index dfe0d6288b..65a3bf1e63 100644 --- a/query/graphql/planner/select.go +++ b/query/graphql/planner/select.go @@ -294,10 +294,8 @@ func (n *selectNode) initFields(parsed *mapper.Select) ([]aggregateNode, error) // a OneCommit subquery, with the supplied parameters. commitSlct.DocKey = parsed.DocKeys.Value[0] // @todo check length commitSlct.Cid = parsed.Cid - commitSlct.Type = mapper.Commits - } else { - commitSlct.Type = mapper.LatestCommits } + commitPlan, err := n.p.CommitSelect(commitSlct) if err != nil { return nil, err