Skip to content

Commit

Permalink
wip: Implementated only with node names (no, attributes yet).
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzadlone committed Apr 30, 2022
1 parent 7d72e41 commit e858e5e
Show file tree
Hide file tree
Showing 28 changed files with 300 additions and 183 deletions.
20 changes: 5 additions & 15 deletions db/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

package container

import (
"github.com/sourcenetwork/defradb/utils"
)

// DocumentContainer is a specialized buffer to store potentially
// thousands of document value maps. Its used by the Planner system
// to store documents that need to have logic applied to all of them.
Expand Down Expand Up @@ -52,7 +56,7 @@ func (c *DocumentContainer) AddDoc(doc map[string]interface{}) error {
return nil
}
// append to docs slice
copyDoc := copyMap(doc)
copyDoc := utils.CopyMap(doc)
c.docs = append(c.docs, copyDoc)
c.numDocs++
return nil
Expand All @@ -74,17 +78,3 @@ func (c *DocumentContainer) Close() {
c.docs = nil
c.numDocs = 0
}

func copyMap(m map[string]interface{}) map[string]interface{} {
cp := make(map[string]interface{})
for k, v := range m {
vm, ok := v.(map[string]interface{})
if ok {
cp[k] = copyMap(vm)
} else {
cp[k] = v
}
}

return cp
}
5 changes: 5 additions & 0 deletions query/graphql/planner/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type commitSelectTopNode struct {
plan planNode
}

func (n *commitSelectTopNode) Explain() string { return "commitSelectTopNode" }
func (n *commitSelectTopNode) Init() error { return n.plan.Init() }
func (n *commitSelectTopNode) Start() error { return n.plan.Start() }
func (n *commitSelectTopNode) Next() (bool, error) { return n.plan.Next() }
Expand All @@ -51,6 +52,10 @@ type commitSelectNode struct {
subRenderInfo map[string]renderInfo
}

func (n *commitSelectNode) Explain() string {
return "commitSelectNode"
}

func (n *commitSelectNode) Init() error {
return n.source.Init()
}
Expand Down
4 changes: 4 additions & 0 deletions query/graphql/planner/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func (p *Planner) Count(field *parser.Field) (*countNode, error) {
}, nil
}

func (n *countNode) Explain() string {
return "countNode"
}

func (n *countNode) Init() error {
return n.plan.Init()
}
Expand Down
2 changes: 2 additions & 0 deletions query/graphql/planner/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type createNode struct {
returned bool
}

func (n *createNode) Explain() string { return "createNode" }

func (n *createNode) Init() error { return nil }

func (n *createNode) Start() error {
Expand Down
8 changes: 8 additions & 0 deletions query/graphql/planner/dagscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ type headsetScanNode struct {
fetcher fetcher.HeadFetcher
}

func (n *headsetScanNode) Explain() string {
return "headsetScanNode"
}

func (h *headsetScanNode) Init() error {
return h.initScan()
}
Expand Down Expand Up @@ -152,6 +156,10 @@ func (p *Planner) DAGScan() *dagScanNode {
}
}

func (n *dagScanNode) Explain() string {
return "dagScanNode"
}

func (n *dagScanNode) Init() error {
if n.headset != nil {
return n.headset.Init()
Expand Down
4 changes: 4 additions & 0 deletions query/graphql/planner/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func (n *deleteNode) Spans(spans core.Spans) {
/* no-op */
}

func (n *deleteNode) Explain() string {
return "deleteNode"
}

func (n *deleteNode) Init() error {
return nil
}
Expand Down
1 change: 0 additions & 1 deletion query/graphql/planner/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/sourcenetwork/defradb/query/graphql/parser"
"github.com/sourcenetwork/defradb/query/graphql/schema"

//github.com/uber-go/multierr
gql "github.com/graphql-go/graphql"
gqlp "github.com/graphql-go/graphql/language/parser"
"github.com/graphql-go/graphql/language/source"
Expand Down
55 changes: 55 additions & 0 deletions query/graphql/planner/explain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2022 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package planner

import (
"strings"
)

// Response:
// query @explain {
// user {
// _key
// }
// }

// Input to ExplainAllText:
// [ "selectTopNode" "selectNode" "scanNode" ]

// Outputs:
// ----------------
// Explain Request:
// ----------------
// selectTopNode
// selectNode
// scanNode

// ExplainAllText formats with proper indenting all the node names.
func ExplainAllText(nodesToExplain []string) []map[string]interface{} {

var explainResponse []map[string]interface{}

var explainBuilder string = "----------------\nExplain Request:\n----------------\n"

for i, node := range nodesToExplain {
explainBuilder = explainBuilder + strings.Repeat("\t", i) + node + "\n"
}

// fmt.Println(explainBuilder)

return append(
explainResponse,
map[string]interface{}{
"explain": explainBuilder,
},
)

}
4 changes: 4 additions & 0 deletions query/graphql/planner/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func (p *Planner) GroupBy(n *parser.GroupBy, childSelect *parser.Select) (*group
return &groupNodeObj, nil
}

func (n *groupNode) Explain() string {
return "groupNode"
}

func (n *groupNode) Init() error {
// We need to make sure state is cleared down on Init,
// this function may be called multiple times per instance (for example during a join)
Expand Down
8 changes: 8 additions & 0 deletions query/graphql/planner/limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func (p *Planner) HardLimit(n *parser.Limit) (*hardLimitNode, error) {
}, nil
}

func (n *hardLimitNode) Explain() string {
return "hardLimitNode"
}

func (n *hardLimitNode) Init() error {
n.rowIndex = 0
return n.plan.Init()
Expand Down Expand Up @@ -102,6 +106,10 @@ func (p *Planner) RenderLimit(n *parser.Limit) (*renderLimitNode, error) {
}, nil
}

func (n *renderLimitNode) Explain() string {
return "renderLimitNode"
}

func (n *renderLimitNode) Init() error {
n.rowIndex = 0
return n.plan.Init()
Expand Down
4 changes: 4 additions & 0 deletions query/graphql/planner/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ func (p *parallelNode) applyToPlans(fn func(n planNode) error) error {
return nil
}

func (n *parallelNode) Explain() string {
return "parallelNode"
}

func (p *parallelNode) Init() error {
return p.applyToPlans(func(n planNode) error {
return n.Init()
Expand Down
22 changes: 15 additions & 7 deletions query/graphql/planner/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,28 @@
package planner

var (
_ planNode = (*scanNode)(nil)
_ planNode = (*headsetScanNode)(nil)
_ planNode = (*hardLimitNode)(nil)
_ planNode = (*renderLimitNode)(nil)
_ planNode = (*commitSelectNode)(nil)
_ planNode = (*commitSelectTopNode)(nil)
_ planNode = (*countNode)(nil)
_ planNode = (*createNode)(nil)
_ planNode = (*dagScanNode)(nil)
_ planNode = (*deleteNode)(nil)
_ planNode = (*groupNode)(nil)
_ planNode = (*hardLimitNode)(nil)
_ planNode = (*headsetScanNode)(nil)
_ planNode = (*parallelNode)(nil)
_ planNode = (*pipeNode)(nil)
_ planNode = (*renderLimitNode)(nil)
_ planNode = (*renderNode)(nil)
_ planNode = (*scanNode)(nil)
_ planNode = (*selectNode)(nil)
_ planNode = (*selectTopNode)(nil)
_ planNode = (*sortNode)(nil)
_ planNode = (*renderNode)(nil)
_ planNode = (*sumNode)(nil)
_ planNode = (*typeIndexJoin)(nil)
_ planNode = (*typeJoinOne)(nil)
_ planNode = (*typeJoinMany)(nil)
_ planNode = (*countNode)(nil)
_ planNode = (*typeJoinOne)(nil)
_ planNode = (*updateNode)(nil)
)

// type joinNode struct {
Expand Down
7 changes: 6 additions & 1 deletion query/graphql/planner/pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package planner
import (
"github.com/sourcenetwork/defradb/core"
"github.com/sourcenetwork/defradb/db/container"
"github.com/sourcenetwork/defradb/utils"
)

// A lazily loaded cache-node that allows retrieval of cached documents at arbitrary indexes.
Expand All @@ -37,6 +38,10 @@ func newPipeNode() pipeNode {
}
}

func (n *pipeNode) Explain() string {
return "pipeNode"
}

func (n *pipeNode) Init() error {
// We need to make sure state is cleared down on Init,
// this function may be called multiple times per instance (for example during a join)
Expand Down Expand Up @@ -73,6 +78,6 @@ func (n *pipeNode) Next() (bool, error) {

// Values must be copied out of the node, in case consumers mutate the item
// for example: when rendering
n.currentValue = copyMap(n.docs.At(n.docIndex))
n.currentValue = utils.CopyMap(n.docs.At(n.docIndex))
return true, nil
}
Loading

0 comments on commit e858e5e

Please sign in to comment.