Skip to content

Commit

Permalink
refactor: Use new logger in query package (#236)
Browse files Browse the repository at this point in the history
* Remove unwanted comment

* Removed commented out code

* Remove commented out code

* Use logger in query.planner

* Remove commented code

* Use logger in query.schema

* Remove commented code

* Remove printlns from test file
  • Loading branch information
AndrewSisley authored Mar 1, 2022
1 parent b8ac34b commit a98e826
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 69 deletions.
4 changes: 2 additions & 2 deletions db/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
// and creates the necessary collections, query types, etc.
func (db *DB) AddSchema(ctx context.Context, schema string) error {
// @todo: create collection after generating query types
types, astdoc, err := db.schema.Generator.FromSDL(schema)
types, astdoc, err := db.schema.Generator.FromSDL(ctx, schema)
if err != nil {
return err
}
Expand Down Expand Up @@ -57,7 +57,7 @@ func (db *DB) loadSchema(ctx context.Context) error {
sdl += "\n" + string(buf)
}

_, _, err = db.schema.Generator.FromSDL(sdl)
_, _, err = db.schema.Generator.FromSDL(ctx, sdl)
return err
}

Expand Down
3 changes: 0 additions & 3 deletions query/graphql/parser/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ func getQueryFilterObject(query string) (*Filter, error) {
func TestNewFilterFromString(t *testing.T) {
_, err := NewFilterFromString(`name: {_eq: "bob"}`)
assert.NoError(t, err)

// fmt.Println(f.Conditions)
// assert.True(t, false)
}

func TestParseConditions_Empty(t *testing.T) {
Expand Down
8 changes: 0 additions & 8 deletions query/graphql/parser/mutation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
package parser

import (
"fmt"
"testing"

gqlp "github.com/graphql-go/graphql/language/parser"
Expand All @@ -33,7 +32,6 @@ func TestMutationParse_Create_Simple(t *testing.T) {
})

doc, err := gqlp.Parse(gqlp.ParseParams{Source: source})
fmt.Println(doc)
assert.NoError(t, err)

q, err := ParseQuery(doc)
Expand Down Expand Up @@ -65,7 +63,6 @@ func TestMutationParse_Create_Error_Missing_Data(t *testing.T) {
})

doc, err := gqlp.Parse(gqlp.ParseParams{Source: source})
t.Log(doc)
assert.NoError(t, err)

_, err = ParseQuery(doc)
Expand Down Expand Up @@ -97,7 +94,6 @@ func TestMutationParse_Error_Invalid_Mutation(t *testing.T) {
})

doc, err := gqlp.Parse(gqlp.ParseParams{Source: source})
fmt.Println(doc)
assert.NoError(t, err)

_, err = ParseQuery(doc)
Expand Down Expand Up @@ -129,7 +125,6 @@ func TestMutationParse_Update_Simple_Object(t *testing.T) {
})

doc, err := gqlp.Parse(gqlp.ParseParams{Source: source})
fmt.Println(doc)
assert.NoError(t, err)

q, err := ParseQuery(doc)
Expand Down Expand Up @@ -161,7 +156,6 @@ func TestMutationParse_Update_Simple_Array(t *testing.T) {
})

doc, err := gqlp.Parse(gqlp.ParseParams{Source: source})
fmt.Println(doc)
assert.NoError(t, err)

_, err = ParseQuery(doc)
Expand Down Expand Up @@ -233,7 +227,6 @@ func TestMutationParse_Update_Simple_UnderscoreName(t *testing.T) {
})

doc, err := gqlp.Parse(gqlp.ParseParams{Source: source})
fmt.Println(doc)
assert.NoError(t, err)

q, err := ParseQuery(doc)
Expand Down Expand Up @@ -265,7 +258,6 @@ func TestMutationParse_Delete_Simple(t *testing.T) {
})

doc, err := gqlp.Parse(gqlp.ParseParams{Source: source})
fmt.Println(doc)
assert.NoError(t, err)

q, err := ParseQuery(doc)
Expand Down
1 change: 0 additions & 1 deletion query/graphql/planner/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ func (p *Planner) commitSelectBlock(parsed *parser.CommitSelect) (*commitSelectN
return nil, err
}
dag.cid = &c
// fmt.Println("got cid:", c)
} // @todo: handle error if no CID is given

return &commitSelectNode{
Expand Down
2 changes: 1 addition & 1 deletion query/graphql/planner/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (e *QueryExecutor) ExecQuery(ctx context.Context, db client.DB, txn core.Tx
}

planner := makePlanner(ctx, db, txn)
return planner.queryDocs(q)
return planner.queryDocs(ctx, q)
}

func (e *QueryExecutor) MakePlanFromParser(ctx context.Context, db client.DB, txn core.Txn, query *parser.Query) (planNode, error) {
Expand Down
18 changes: 9 additions & 9 deletions query/graphql/planner/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ import (

"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/core"
"github.com/sourcenetwork/defradb/logging"
"github.com/sourcenetwork/defradb/query/graphql/parser"
)

var (
log = logging.MustNewLogger("defra.query.planner")
)

// planNode is an interface all nodes in the plan tree need to implement
type planNode interface {
// Initializes or Re-Initializes an existing planNode
Expand Down Expand Up @@ -252,11 +257,6 @@ func (p *Planner) expandMultiNode(plan MultiNode, parentPlan *selectTopNode) err
return nil
}

// func (p *Planner) expandSelectNodePlan(plan *selectNode) error {
// fmt.Println("Expanding select plan")
// return p.expandPlan(plan.source)
// }

func (p *Planner) expandTypeIndexJoinPlan(plan *typeIndexJoin, parentPlan *selectTopNode) error {
switch node := plan.joinPlan.(type) {
case *typeJoinOne:
Expand Down Expand Up @@ -389,23 +389,23 @@ func (p *Planner) walkAndFindPlanType(plan, target planNode) planNode {
return src
}

func (p *Planner) queryDocs(query *parser.Query) ([]map[string]interface{}, error) {
func (p *Planner) queryDocs(ctx context.Context, query *parser.Query) ([]map[string]interface{}, error) {
plan, err := p.makePlan(query)
if err != nil {
return nil, err
}

if err = plan.Start(); err != nil {
if err2 := (plan.Close()); err2 != nil {
fmt.Println(err2)
log.ErrorE(ctx, "Error closing plan node", err2)
}
return nil, err
}

var next bool
if next, err = plan.Next(); err != nil {
if err2 := (plan.Close()); err2 != nil {
fmt.Println(err2)
log.ErrorE(ctx, "Error closing plan node", err2)
}
return nil, err
}
Expand All @@ -424,7 +424,7 @@ func (p *Planner) queryDocs(query *parser.Query) ([]map[string]interface{}, erro
next, err = plan.Next()
if err != nil {
if err2 := (plan.Close()); err2 != nil {
fmt.Println(err2)
log.ErrorE(ctx, "Error closing plan node", err2)
}
return nil, err
}
Expand Down
9 changes: 3 additions & 6 deletions query/graphql/planner/type_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,6 @@ func (p *Planner) makeTypeJoinOne(parent *selectNode, source planNode, subType *
typeJoin.primary = false
}

// fmt.Println("Parent filter:", parent.filter)
// fmt.Println("source filter:", source.filter)
return typeJoin, nil
}

Expand Down Expand Up @@ -453,7 +451,7 @@ func (n *typeJoinOne) valuesPrimary(doc map[string]interface{}) map[string]inter

// re-initialize the sub type plan
if err := n.subType.Init(); err != nil {
// @todo pair up on the error handling / logging properly.
log.ErrorE(n.p.ctx, "Sub-type initalization error at scan node reset", err)
return doc
}

Expand All @@ -462,8 +460,8 @@ func (n *typeJoinOne) valuesPrimary(doc map[string]interface{}) map[string]inter
// with an empty map for the subdoc
next, err := n.subType.Next()

// @todo pair up on the error handling / logging properly.
if err != nil {
log.ErrorE(n.p.ctx, "Sub-type initalization error at scan node reset", err)
return doc
}

Expand Down Expand Up @@ -584,8 +582,7 @@ func (n *typeJoinMany) Values() map[string]interface{} {

// reset scan node
if err := n.subType.Init(); err != nil {
// @todo pair up on the error handling / logging properly.
fmt.Println("sub-type initalization error at scan node reset : %w", err)
log.ErrorE(n.p.ctx, "Sub-type initalization error at scan node reset", err)
}

for {
Expand Down
5 changes: 3 additions & 2 deletions query/graphql/schema/descriptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package schema

import (
"context"
"testing"

"github.com/sourcenetwork/defradb/core"
Expand Down Expand Up @@ -337,10 +338,11 @@ func TestSingleSimpleType(t *testing.T) {
}

func runCreateDescriptionTest(t *testing.T, testcase descriptionTestCase) {
ctx := context.Background()
sm, err := NewSchemaManager()
assert.NoError(t, err, testcase.description)

types, _, err := sm.Generator.FromSDL(testcase.sdl)
types, _, err := sm.Generator.FromSDL(ctx, testcase.sdl)
assert.NoError(t, err, testcase.description)

assert.Len(t, types, len(testcase.targetDescs), testcase.description)
Expand All @@ -350,7 +352,6 @@ func runCreateDescriptionTest(t *testing.T, testcase descriptionTestCase) {
assert.Equal(t, len(descs), len(testcase.targetDescs), testcase.description)

for i, d := range descs {
// fmt.Println(d.Schema.Fields)
assert.Equal(t, testcase.targetDescs[i], d, testcase.description)
}
}
Expand Down
Loading

0 comments on commit a98e826

Please sign in to comment.