Skip to content

Commit

Permalink
refactor: Remove commit query (sourcenetwork#841)
Browse files Browse the repository at this point in the history
* Port commit tests with feature parity to allCommits

Moves the commit query tests that do not exist for allCommits, where the behaviour matches exactly (copy-paste with name changes).

* Port commit tests with corrected behaviour

Moves the commit test(s) to allCommits where the behaviour of commit was incorrect, but correct in allCommits, and where allCommits was previously untested.

* Remove commit tests that already exist for allCommits

Removes commit tests for behaviour that is already tested by existing commit tests.

* Remove commit test that is not applicable for allCommits

* Remove commit test utils

* Remove commit query

* Move allCommits tests

No code changes, just moved the files (sperate commit to make it easier to spot changes)

* Rename allCommits to commits
  • Loading branch information
AndrewSisley committed Oct 11, 2022
1 parent 7fb337a commit 7869986
Show file tree
Hide file tree
Showing 25 changed files with 154 additions and 521 deletions.
5 changes: 2 additions & 3 deletions query/graphql/mapper/commitSelect.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ type CommitType int
const (
NoneCommitType = CommitType(iota)
LatestCommits
AllCommits
OneCommit
Commits
)

// CommitSelect represents a commit request from a consumer.
//
// E.g. allCommits, or latestCommits.
// E.g. commits, or latestCommits.
type CommitSelect struct {
// The underlying Select, defining the information requested.
Select
Expand Down
6 changes: 2 additions & 4 deletions query/graphql/parser/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ type CommitType int
const (
NoneCommitType = CommitType(iota)
LatestCommits
AllCommits
OneCommit
Commits
)

var (
commitNameToType = map[string]CommitType{
"latestCommits": LatestCommits,
"allCommits": AllCommits,
"commit": OneCommit,
"commits": Commits,
}

_ Selection = (*CommitSelect)(nil)
Expand Down
4 changes: 2 additions & 2 deletions query/graphql/parser/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

var dbAPIQueryNames = map[string]bool{
"latestCommits": true,
"allCommits": true,
"commits": true,
"commit": true,
}

Expand Down Expand Up @@ -354,7 +354,7 @@ func parseField(root parserTypes.SelectionType, field *ast.Field) *Field {

func parseAPIQuery(field *ast.Field) (Selection, error) {
switch field.Name.Value {
case "latestCommits", "allCommits", "commit":
case "latestCommits", "commits":
return parseCommitSelect(field)
default:
return nil, errors.New("Unknown query")
Expand Down
30 changes: 5 additions & 25 deletions query/graphql/planner/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ func (p *Planner) CommitSelect(parsed *mapper.CommitSelect) (planNode, error) {
switch parsed.Type {
case mapper.LatestCommits:
commit, err = p.commitSelectLatest(parsed)
case mapper.OneCommit:
commit, err = p.commitSelectBlock(parsed)
case mapper.AllCommits:
case mapper.Commits:
commit, err = p.commitSelectAll(parsed)
default:
return nil, errors.New("Invalid CommitSelect type")
Expand All @@ -130,8 +128,8 @@ func (p *Planner) CommitSelect(parsed *mapper.CommitSelect) (planNode, error) {

// commitSelectLatest is a CommitSelect node initalized with a headsetScanNode and a DocKey
func (p *Planner) commitSelectLatest(parsed *mapper.CommitSelect) (*commitSelectNode, error) {
dag := p.DAGScan(parsed)
headset := p.HeadScan(parsed)
dag := p.DAGScan(parsed, headset)
// @todo: Get Collection field ID
if !parsed.FieldName.HasValue() {
dag.field = core.COMPOSITE_NAMESPACE
Expand All @@ -143,7 +141,7 @@ func (p *Planner) commitSelectLatest(parsed *mapper.CommitSelect) (*commitSelect
key := core.DataStoreKey{}.WithDocKey(parsed.DocKey).WithFieldId(dag.field)
headset.key = key
}
dag.headset = headset

commit := &commitSelectNode{
p: p,
source: dag,
Expand All @@ -152,29 +150,11 @@ func (p *Planner) commitSelectLatest(parsed *mapper.CommitSelect) (*commitSelect
return commit, nil
}

// commitSelectBlock is a CommitSelect node initialized without a headsetScanNode, and is expected
// to be given a target CID in the parser.CommitSelect object. It returns a single commit if found.
func (p *Planner) commitSelectBlock(parsed *mapper.CommitSelect) (*commitSelectNode, error) {
dag := p.DAGScan(parsed)
if parsed.Cid != "" {
c, err := cid.Decode(parsed.Cid)
if err != nil {
return nil, err
}
dag.cid = &c
} // @todo: handle error if no CID is given

return &commitSelectNode{
p: p,
source: dag,
}, 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) {
dag := p.DAGScan(parsed)
headset := p.HeadScan(parsed)
dag := p.DAGScan(parsed, headset)

if parsed.Cid != "" {
c, err := cid.Decode(parsed.Cid)
Expand Down Expand Up @@ -202,7 +182,7 @@ func (p *Planner) commitSelectAll(parsed *mapper.CommitSelect) (*commitSelectNod

headset.key = key
}
dag.headset = headset

dag.depthLimit = math.MaxUint32 // infinite depth
// dag.key = &key
commit := &commitSelectNode{
Expand Down
58 changes: 17 additions & 41 deletions query/graphql/planner/dagscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,13 @@ type dagScanNode struct {
parsed *mapper.CommitSelect
}

func (p *Planner) DAGScan(parsed *mapper.CommitSelect) *dagScanNode {
func (p *Planner) DAGScan(parsed *mapper.CommitSelect, headset *headsetScanNode) *dagScanNode {
return &dagScanNode{
p: p,
visitedNodes: make(map[string]bool),
queuedCids: list.New(),
parsed: parsed,
headset: headset,
docMapper: docMapper{&parsed.DocumentMapping},
}
}
Expand All @@ -171,17 +172,11 @@ func (n *dagScanNode) Kind() string {
}

func (n *dagScanNode) Init() error {
if n.headset != nil {
return n.headset.Init()
}
return nil
return n.headset.Init()
}

func (n *dagScanNode) Start() error {
if n.headset != nil {
return n.headset.Start()
}
return nil
return n.headset.Start()
}

// Spans needs to parse the given span set. dagScanNode only
Expand All @@ -194,33 +189,20 @@ func (n *dagScanNode) Spans(spans core.Spans) {
return
}

// if we have a headset, pass along
// otherwise, try to parse as a CID
if n.headset != nil {
// make sure we have the correct field suffix
headSetSpans := core.Spans{
HasValue: spans.HasValue,
Value: make([]core.Span, len(spans.Value)),
}
copy(headSetSpans.Value, spans.Value)
span := headSetSpans.Value[0].Start()
if !strings.HasSuffix(span.ToString(), n.field) {
headSetSpans.Value[0] = core.NewSpan(span.WithFieldId(n.field), core.DataStoreKey{})
}
n.headset.Spans(headSetSpans)
} else {
data := spans.Value[0].Start().ToString()
c, err := cid.Decode(data)
if err == nil {
n.cid = &c
}
// make sure we have the correct field suffix
headSetSpans := core.Spans{
HasValue: spans.HasValue,
Value: make([]core.Span, len(spans.Value)),
}
copy(headSetSpans.Value, spans.Value)
span := headSetSpans.Value[0].Start()
if !strings.HasSuffix(span.ToString(), n.field) {
headSetSpans.Value[0] = core.NewSpan(span.WithFieldId(n.field), core.DataStoreKey{})
}
n.headset.Spans(headSetSpans)
}

func (n *dagScanNode) Close() error {
if n.headset == nil {
return nil
}
return n.headset.Close()
}

Expand Down Expand Up @@ -275,7 +257,7 @@ func (n *dagScanNode) Next() (bool, error) {
}
n.queuedCids.Remove(c)
n.currentCid = &cid
} else if n.currentCid == nil && n.headset != nil {
} else if n.currentCid == nil {
if next, err := n.headset.Next(); !next {
return false, err
}
Expand All @@ -292,11 +274,6 @@ func (n *dagScanNode) Next() (bool, error) {
return false, nil
}
n.currentCid = n.cid
} else if n.currentCid == nil {
// add this final elseif case in case another function
// manually sets the CID. Should prob migrate any remote CID
// updates to use the queuedCids.
return false, nil // no queued CIDs and no headset available
}

// skip already visited CIDs
Expand Down Expand Up @@ -326,7 +303,7 @@ func (n *dagScanNode) Next() (bool, error) {
// based on the specified depth limit.
// The default query 'latestCommit' only cares about
// the current latest heads, so it has a depth limit
// of 1. The query 'allCommits' doesn't have a depth
// of 1. The query 'commits' doesn't have a depth
// limit, so it will continue to traverse the graph
// until there are no more links, and no more explored
// HEAD paths.
Expand Down Expand Up @@ -378,8 +355,7 @@ blocks of the MerkleCRDTs.
The current available endpoints are:
- latestCommit: Given a docid, and optionally a field name, return the latest dag commit
- allCommits: Given a docid, and optionally a field name, return all the dag commits
- oneCommit: Given a cid, return the specific commit
- commits: Given a docid, and optionally a field name, return all the dag commits
Additionally, theres a subselection available on the Document query called _version,
which returns the current dag commit for the stored CRDT value.
Expand Down
2 changes: 1 addition & 1 deletion query/graphql/planner/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ 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.OneCommit
commitSlct.Type = mapper.Commits
} else {
commitSlct.Type = mapper.LatestCommits
}
Expand Down
5 changes: 2 additions & 3 deletions query/graphql/schema/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,8 @@ func defaultQueryType() *gql.Object {
},

// database API queries
schemaTypes.QueryAllCommits.Name: schemaTypes.QueryAllCommits,
schemaTypes.QueryCommits.Name: schemaTypes.QueryCommits,
schemaTypes.QueryLatestCommits.Name: schemaTypes.QueryLatestCommits,
schemaTypes.QueryCommit.Name: schemaTypes.QueryCommit,
},
})
}
Expand Down Expand Up @@ -170,7 +169,7 @@ func defaultTypes() []gql.Type {
schemaTypes.StringOperatorBlock,
schemaTypes.NotNullstringOperatorBlock,

schemaTypes.AllCommitsOrderArg,
schemaTypes.CommitsOrderArg,
schemaTypes.CommitLinkObject,
schemaTypes.CommitObject,
schemaTypes.DeltaObject,
Expand Down
18 changes: 5 additions & 13 deletions query/graphql/schema/types/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ var (
},
})

AllCommitsOrderArg = gql.NewInputObject(
CommitsOrderArg = gql.NewInputObject(
gql.InputObjectConfig{
Name: "allCommitsOrderArg",
Name: "commitsOrderArg",
Fields: gql.InputObjectConfigFieldMap{
"height": &gql.InputObjectFieldConfig{
Type: OrderingEnum,
Expand All @@ -103,13 +103,13 @@ var (
},
)

QueryAllCommits = &gql.Field{
Name: "allCommits",
QueryCommits = &gql.Field{
Name: "commits",
Type: gql.NewList(CommitObject),
Args: gql.FieldConfigArgument{
"dockey": NewArgConfig(gql.ID),
"field": NewArgConfig(gql.String),
"order": NewArgConfig(AllCommitsOrderArg),
"order": NewArgConfig(CommitsOrderArg),
"cid": NewArgConfig(gql.ID),
parserTypes.LimitClause: NewArgConfig(gql.Int),
parserTypes.OffsetClause: NewArgConfig(gql.Int),
Expand All @@ -124,12 +124,4 @@ var (
"field": NewArgConfig(gql.String),
},
}

QueryCommit = &gql.Field{
Name: "commit",
Type: CommitObject,
Args: gql.FieldConfigArgument{
"cid": NewArgConfig(gql.NewNonNull(gql.ID)),
},
}
)
29 changes: 0 additions & 29 deletions tests/integration/query/all_commits/utils.go

This file was deleted.

Loading

0 comments on commit 7869986

Please sign in to comment.