Skip to content

Commit

Permalink
refactor: Change sortNode to orderNode. (#591)
Browse files Browse the repository at this point in the history
- Relevant issue: Resolves #590

- Description:
Changes `sortNode` to `orderNode`, and some file names and other instances for consistency.
  • Loading branch information
shahzadlone authored Jul 6, 2022
1 parent 0f54d22 commit 2c920a1
Show file tree
Hide file tree
Showing 18 changed files with 90 additions and 102 deletions.
2 changes: 1 addition & 1 deletion db/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
// 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.
// For example, in the sortNode and future groupNode. The Document
// For example, in the orderNode and future groupNode. The Document
// Container acts as an array, so you can append, index, and get the
// length of all the documents inside.
// Close() is called if you want to free all the memory associated
Expand Down
32 changes: 10 additions & 22 deletions query/graphql/parser/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,23 @@ type parseFn func(*ast.ObjectValue) (interface{}, error)
// ParseConditionsInOrder is similar to ParseConditions, except instead
// of returning a map[string]interface{}, we return a []interface{}. This
// is to maintain the ordering info of the statements within the ObjectValue.
// This function is mostly used by the Sort parser, which needs to parse
// This function is mostly used by the Order parser, which needs to parse
// conditions in the same way as the Filter object, however the order
// of the arguments is important.
func ParseConditionsInOrder(stmt *ast.ObjectValue) ([]parserTypes.SortCondition, error) {
func ParseConditionsInOrder(stmt *ast.ObjectValue) ([]parserTypes.OrderCondition, error) {
cond, err := parseConditionsInOrder(stmt)
if err != nil {
return nil, err
}

if v, ok := cond.([]parserTypes.SortCondition); ok {
if v, ok := cond.([]parserTypes.OrderCondition); ok {
return v, nil
}
return nil, errors.New("Failed to parse statement")
}

func parseConditionsInOrder(stmt *ast.ObjectValue) (interface{}, error) {
conditions := make([]parserTypes.SortCondition, 0)
conditions := make([]parserTypes.OrderCondition, 0)
if stmt == nil {
return conditions, nil
}
Expand All @@ -97,20 +97,20 @@ func parseConditionsInOrder(stmt *ast.ObjectValue) (interface{}, error) {
}

switch v := val.(type) {
case string: // base direction parsed (hopefully, check NameToSortDirection)
dir, ok := parserTypes.NameToSortDirection[v]
case string: // base direction parsed (hopefully, check NameToOrderDirection)
dir, ok := parserTypes.NameToOrderDirection[v]
if !ok {
return nil, errors.New("Invalid sort direction string")
return nil, errors.New("Invalid order direction string")
}
conditions = append(conditions, parserTypes.SortCondition{
conditions = append(conditions, parserTypes.OrderCondition{
Field: name,
Direction: dir,
})

case []parserTypes.SortCondition: // flatten and incorporate the parsed slice into our current one
case []parserTypes.OrderCondition: // flatten and incorporate the parsed slice into our current one
for _, cond := range v {
// prepend the current field name, to the parsed condition from the slice
// Eg. sort: {author: {name: ASC, birthday: DESC}}
// Eg. order: {author: {name: ASC, birthday: DESC}}
// This results in an array of [name, birthday] converted to
// [author.name, author.birthday].
// etc.
Expand All @@ -126,18 +126,6 @@ func parseConditionsInOrder(stmt *ast.ObjectValue) (interface{}, error) {
return conditions, nil
}

// func flattenSortDirectionSlice(namespace string, slice []interface{}) []interface{} {
// res := make([]interface{}, 0)
// for _, v := range slice {
// switch n := v.(type) {
// case []interface{}:
// res = append(res, flatten(namespace, n)...)
// default:
// res = append(res, n)
// }
// }
// }

// parseConditions loops over the stmt ObjectValue fields, and extracts
// all the relevant name/value pairs.
func ParseConditions(stmt *ast.ObjectValue) (map[string]interface{}, error) {
Expand Down
2 changes: 1 addition & 1 deletion query/graphql/parser/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func parseSelect(rootType parserTypes.SelectionType, field *ast.Field, index int
slct.Limit = &parserTypes.Limit{}
}
slct.Limit.Offset = i
} else if prop == parserTypes.OrderClause { // parse sort (order by)
} else if prop == parserTypes.OrderClause { // parse order by
obj := astValue.(*ast.ObjectValue)
cond, err := ParseConditionsInOrder(obj)
if err != nil {
Expand Down
18 changes: 9 additions & 9 deletions query/graphql/parser/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,31 @@ package types
import "github.com/graphql-go/graphql/language/ast"

type (
SortDirection string
OrderDirection string

SelectionType int

// Enum for different types of read Select queries
SelectQueryType int

SortCondition struct {
OrderCondition struct {
// field may be a compound field statement
// since the sort statement allows sorting on
// since the order statement allows ordering on
// sub objects.
//
// Given the statement: {sort: {author: {birthday: DESC}}}
// Given the statement: {order: {author: {birthday: DESC}}}
// The field value would be "author.birthday"
// and the direction would be "DESC"
Field string
Direction SortDirection
Direction OrderDirection
}

GroupBy struct {
Fields []string
}

OrderBy struct {
Conditions []SortCondition
Conditions []OrderCondition
Statement *ast.ObjectValue
}

Expand Down Expand Up @@ -87,8 +87,8 @@ const (
LinksNameFieldName = "name"
LinksCidFieldName = "cid"

ASC = SortDirection("ASC")
DESC = SortDirection("DESC")
ASC = OrderDirection("ASC")
DESC = OrderDirection("DESC")
)

const (
Expand All @@ -103,7 +103,7 @@ const (
)

var (
NameToSortDirection = map[string]SortDirection{
NameToOrderDirection = map[string]OrderDirection{
string(ASC): ASC,
string(DESC): DESC,
}
Expand Down
2 changes: 1 addition & 1 deletion query/graphql/planner/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var (
_ explainablePlanNode = (*commitSelectNode)(nil)
_ explainablePlanNode = (*countNode)(nil)
_ explainablePlanNode = (*dagScanNode)(nil)
_ explainablePlanNode = (*sortNode)(nil)
_ explainablePlanNode = (*orderNode)(nil)
_ explainablePlanNode = (*sumNode)(nil)
_ explainablePlanNode = (*updateNode)(nil)
)
Expand Down
2 changes: 1 addition & 1 deletion query/graphql/planner/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var (
_ planNode = (*scanNode)(nil)
_ planNode = (*selectNode)(nil)
_ planNode = (*selectTopNode)(nil)
_ planNode = (*sortNode)(nil)
_ planNode = (*orderNode)(nil)
_ planNode = (*sumNode)(nil)
_ planNode = (*typeIndexJoin)(nil)
_ planNode = (*typeJoinMany)(nil)
Expand Down
58 changes: 29 additions & 29 deletions query/graphql/planner/sort.go → query/graphql/planner/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ type valueIterator interface {
Close() error
}

type sortingStrategy interface {
type orderingStrategy interface {
valueIterator
// Add a document to the strategy node.
// copies data if its needed.
// Ideally stores inside a valuesNode
// rowContainer buffer.
Add(core.Doc) error
// Finish finalizes and applies the actual
// sorting mechanism to all the stored data.
// ordering mechanism to all the stored data.
Finish()
}

// order the results
type sortNode struct {
type orderNode struct {
docMapper

p *Planner
Expand All @@ -52,54 +52,54 @@ type sortNode struct {
// an already sorted plan
valueIter valueIterator

// sortStrategy is an encapsulate planNode
// orderStrategy is an encapsulate planNode
// that sorts, then provides the values
// sorted
sortStrategy sortingStrategy
orderStrategy orderingStrategy

// indicates if our underlying sortStrategy is still
// indicates if our underlying orderStrategy is still
// consuming and sorting data.
needSort bool
}

// OrderBy creates a new sortNode which returns the underlying
// OrderBy creates a new orderNode which returns the underlying
// plans values in a sorted mannor. The field to sort by, and the
// direction of sorting is determined by the given mapper.OrderBy
// direction of ordering is determined by the given mapper.OrderBy
// object.
func (p *Planner) OrderBy(parsed *mapper.Select, n *mapper.OrderBy) (*sortNode, error) {
func (p *Planner) OrderBy(parsed *mapper.Select, n *mapper.OrderBy) (*orderNode, error) {
if n == nil { // no orderby info
return nil, nil
}

return &sortNode{
return &orderNode{
p: p,
ordering: n.Conditions,
needSort: true,
docMapper: docMapper{&parsed.DocumentMapping},
}, nil
}

func (n *sortNode) Kind() string {
return "sortNode"
func (n *orderNode) Kind() string {
return "orderNode"
}

func (n *sortNode) Init() error {
func (n *orderNode) Init() error {
// reset stateful data
n.needSort = true
n.sortStrategy = nil
n.orderStrategy = nil
return n.plan.Init()
}
func (n *sortNode) Start() error { return n.plan.Start() }
func (n *orderNode) Start() error { return n.plan.Start() }

func (n *sortNode) Spans(spans core.Spans) { n.plan.Spans(spans) }
func (n *orderNode) Spans(spans core.Spans) { n.plan.Spans(spans) }

func (n *sortNode) Value() core.Doc {
func (n *orderNode) Value() core.Doc {
return n.valueIter.Value()
}

// Explain method returns a map containing all attributes of this node that
// are to be explained, subscribes / opts-in this node to be an explainablePlanNode.
func (n *sortNode) Explain() (map[string]interface{}, error) {
func (n *orderNode) Explain() (map[string]interface{}, error) {
orderings := []map[string]interface{}{}

for _, element := range n.ordering {
Expand Down Expand Up @@ -134,12 +134,12 @@ func (n *sortNode) Explain() (map[string]interface{}, error) {
}, nil
}

func (n *sortNode) Next() (bool, error) {
func (n *orderNode) Next() (bool, error) {
for n.needSort {
// make sure our sortStrategy is initialized
if n.sortStrategy == nil {
// make sure our orderStrategy is initialized
if n.orderStrategy == nil {
v := n.p.newContainerValuesNode(n.ordering)
n.sortStrategy = newAllSortStrategy(v)
n.orderStrategy = newAllSortStrategy(v)
}

// consume data (from plan) (Next / Values())
Expand All @@ -148,14 +148,14 @@ func (n *sortNode) Next() (bool, error) {
return false, err
}
if !next {
n.sortStrategy.Finish()
n.valueIter = n.sortStrategy
n.orderStrategy.Finish()
n.valueIter = n.orderStrategy
n.needSort = false
break
}

// consuming data, sort
if err := n.sortStrategy.Add(n.plan.Value()); err != nil {
if err := n.orderStrategy.Add(n.plan.Value()); err != nil {
return false, err
}
}
Expand All @@ -167,7 +167,7 @@ func (n *sortNode) Next() (bool, error) {
return true, nil
}

func (n *sortNode) Close() error {
func (n *orderNode) Close() error {
err := n.plan.Close()
if err != nil {
return err
Expand All @@ -177,13 +177,13 @@ func (n *sortNode) Close() error {
return n.valueIter.Close()
}

if n.sortStrategy != nil {
return n.sortStrategy.Close()
if n.orderStrategy != nil {
return n.orderStrategy.Close()
}
return nil
}

func (n *sortNode) Source() planNode { return n.plan }
func (n *orderNode) Source() planNode { return n.plan }

// allSortStrategy is the simplest sort strategy available.
// it consumes all the data into the underlying valueNode
Expand Down
6 changes: 3 additions & 3 deletions query/graphql/planner/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,9 @@ func (p *Planner) expandSelectTopNodePlan(plan *selectTopNode, parentPlan *selec
p.expandAggregatePlans(plan)

// if order
if plan.sort != nil {
plan.sort.plan = plan.plan
plan.plan = plan.sort
if plan.order != nil {
plan.order.plan = plan.plan
plan.plan = plan.order
}

if plan.limit != nil {
Expand Down
14 changes: 7 additions & 7 deletions query/graphql/planner/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type selectTopNode struct {
docMapper

group *groupNode
sort *sortNode
order *orderNode
limit planNode
aggregates []aggregateNode

Expand Down Expand Up @@ -361,7 +361,7 @@ func (p *Planner) SelectFromSource(
}
s.filter = parsed.Filter
limit := parsed.Limit
sort := parsed.OrderBy
orderBy := parsed.OrderBy
groupBy := parsed.GroupBy

if providedSourceInfo != nil {
Expand Down Expand Up @@ -392,15 +392,15 @@ func (p *Planner) SelectFromSource(
return nil, err
}

sortPlan, err := p.OrderBy(parsed, sort)
orderPlan, err := p.OrderBy(parsed, orderBy)
if err != nil {
return nil, err
}

top := &selectTopNode{
selectnode: s,
limit: limitPlan,
sort: sortPlan,
order: orderPlan,
group: groupPlan,
aggregates: aggregates,
docMapper: docMapper{&parsed.DocumentMapping},
Expand All @@ -417,7 +417,7 @@ func (p *Planner) Select(parsed *mapper.Select) (planNode, error) {
docMapper: docMapper{&parsed.DocumentMapping},
}
limit := parsed.Limit
sort := parsed.OrderBy
order := parsed.OrderBy
groupBy := parsed.GroupBy

aggregates, err := s.initSource()
Expand All @@ -435,15 +435,15 @@ func (p *Planner) Select(parsed *mapper.Select) (planNode, error) {
return nil, err
}

sortPlan, err := p.OrderBy(parsed, sort)
orderPlan, err := p.OrderBy(parsed, order)
if err != nil {
return nil, err
}

top := &selectTopNode{
selectnode: s,
limit: limitPlan,
sort: sortPlan,
order: orderPlan,
group: groupPlan,
aggregates: aggregates,
docMapper: docMapper{&parsed.DocumentMapping},
Expand Down
File renamed without changes.
Loading

0 comments on commit 2c920a1

Please sign in to comment.