Skip to content

Commit

Permalink
refactor: Make latest commits syntax sugar (sourcenetwork#890)
Browse files Browse the repository at this point in the history
* Make latestCommits syntax sugar around commits

* Remove commit-type related code
  • Loading branch information
AndrewSisley committed Oct 12, 2022
1 parent 7ff9038 commit db0631d
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 83 deletions.
13 changes: 0 additions & 13 deletions query/graphql/mapper/commitSelect.go
Expand Up @@ -12,25 +12,13 @@ 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.
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

Expand All @@ -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,
}
Expand Down
1 change: 0 additions & 1 deletion query/graphql/mapper/mapper.go
Expand Up @@ -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,
Expand Down
35 changes: 14 additions & 21 deletions query/graphql/parser/commit.go
Expand Up @@ -16,32 +16,18 @@ 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)
)

type CommitSelect struct {
Alias string
Name string

Type CommitType
DocKey string
FieldName client.Option[string]
Cid string
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions query/graphql/parser/types/types.go
Expand Up @@ -84,6 +84,8 @@ const (

ExplainLabel = "explain"

LatestCommitsQueryName = "latestCommits"

CommitTypeName = "Commit"
LinksFieldName = "links"
HeightFieldName = "height"
Expand Down
48 changes: 6 additions & 42 deletions query/graphql/planner/commit.go
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions query/graphql/planner/dagscan.go
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions query/graphql/planner/select.go
Expand Up @@ -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
Expand Down

0 comments on commit db0631d

Please sign in to comment.