Skip to content

Commit

Permalink
Remove isChild from fastJsonNode (#5184)
Browse files Browse the repository at this point in the history
While forming JSON response from Subgraph, we build one intermediate data structure called
fastJsonNode tree. Each fastJsonNode has some fields which help correct encoding to JSON.
isChild and list are also part of these fields.
Currently isChild and list are used interchangeably and serve same purpose. This PR remove
isChild for simplicity and better readability of the code.
  • Loading branch information
ashish-goswami authored and danielmai committed Apr 24, 2020
1 parent a465b1a commit 836faeb
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions query/outputnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ func ToJson(l *Latency, sgl []*SubGraph) ([]byte, error) {
return sgr.toFastJSON(l)
}

func makeScalarNode(attr string, isChild bool, val []byte, list bool) *fastJsonNode {
func makeScalarNode(attr string, val []byte, list bool) *fastJsonNode {
return &fastJsonNode{
attr: attr,
isChild: isChild,
scalarVal: val,
list: list,
}
Expand All @@ -67,7 +66,6 @@ func makeScalarNode(attr string, isChild bool, val []byte, list bool) *fastJsonN
type fastJsonNode struct {
attr string
order int // relative ordering (for sorted results)
isChild bool
scalarVal []byte
attrs []*fastJsonNode
list bool
Expand All @@ -79,7 +77,7 @@ func (fj *fastJsonNode) AddValue(attr string, v types.Val) {

func (fj *fastJsonNode) AddListValue(attr string, v types.Val, list bool) {
if bs, err := valToBytes(v); err == nil {
fj.attrs = append(fj.attrs, makeScalarNode(attr, false, bs, list))
fj.attrs = append(fj.attrs, makeScalarNode(attr, bs, list))
}
}

Expand All @@ -93,24 +91,21 @@ func (fj *fastJsonNode) AddMapChild(attr string, val *fastJsonNode, isRoot bool)
}

if childNode != nil {
val.isChild = true
val.attr = attr
childNode.attrs = append(childNode.attrs, val.attrs...)
} else {
val.isChild = false
val.attr = attr
fj.attrs = append(fj.attrs, val)
}
}

func (fj *fastJsonNode) AddListChild(attr string, child *fastJsonNode) {
child.attr = attr
child.isChild = true
child.list = true
fj.attrs = append(fj.attrs, child)
}

func (fj *fastJsonNode) New(attr string) *fastJsonNode {
return &fastJsonNode{attr: attr, isChild: false}
return &fastJsonNode{attr: attr}
}

func (fj *fastJsonNode) SetUID(uid uint64, attr string) {
Expand All @@ -122,8 +117,7 @@ func (fj *fastJsonNode) SetUID(uid uint64, attr string) {
}
}
}
fj.attrs = append(fj.attrs, makeScalarNode(attr, false, []byte(fmt.Sprintf("\"%#x\"", uid)),
false))
fj.attrs = append(fj.attrs, makeScalarNode(attr, []byte(fmt.Sprintf("\"%#x\"", uid)), false))
}

func (fj *fastJsonNode) IsEmpty() bool {
Expand Down Expand Up @@ -288,7 +282,7 @@ func (fj *fastJsonNode) encode(out *bytes.Buffer) error {
if err := cur.writeKey(out); err != nil {
return err
}
if cur.isChild || cur.list {
if cur.list {
if _, err := out.WriteRune('['); err != nil {
return err
}
Expand All @@ -298,7 +292,7 @@ func (fj *fastJsonNode) encode(out *bytes.Buffer) error {
if err := cur.encode(out); err != nil {
return err
}
if cnt != 1 || (cur.isChild || cur.list) {
if cnt != 1 || cur.list {
if _, err := out.WriteRune(']'); err != nil {
return err
}
Expand All @@ -317,15 +311,15 @@ func (fj *fastJsonNode) encode(out *bytes.Buffer) error {
return err
}
}
if (cur.isChild || cur.list) && !inArray {
if cur.list && !inArray {
if _, err := out.WriteRune('['); err != nil {
return err
}
}
if err := cur.encode(out); err != nil {
return err
}
if cnt != 1 || (cur.isChild || cur.list) {
if cnt != 1 || cur.list {
if _, err := out.WriteRune(']'); err != nil {
return err
}
Expand Down Expand Up @@ -618,6 +612,7 @@ func (sg *SubGraph) toFastJSON(l *Latency) ([]byte, error) {
return nil, err
}
}

return bufw.Bytes(), nil
}

Expand Down

0 comments on commit 836faeb

Please sign in to comment.