-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathoutputnode.go
520 lines (467 loc) · 12 KB
/
outputnode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*
* Copyright 2017-2018 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package query
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"sort"
"strings"
"time"
geom "github.com/twpayne/go-geom"
"github.com/twpayne/go-geom/encoding/geojson"
"github.com/dgraph-io/dgo/protos/api"
"github.com/dgraph-io/dgraph/algo"
"github.com/dgraph-io/dgraph/types"
"github.com/dgraph-io/dgraph/x"
)
// ToJson converts the list of subgraph into a JSON response by calling toFastJSON.
func ToJson(l *Latency, sgl []*SubGraph) ([]byte, error) {
sgr := &SubGraph{}
for _, sg := range sgl {
if sg.Params.Alias == "var" || sg.Params.Alias == "shortest" {
continue
}
if sg.Params.GetUid {
sgr.Params.GetUid = true
}
sgr.Children = append(sgr.Children, sg)
}
return sgr.toFastJSON(l)
}
// outputNode is the generic output / writer for preTraverse.
type outputNode interface {
AddValue(attr string, v types.Val)
AddListValue(attr string, v types.Val, list bool)
AddMapChild(attr string, node outputNode, isRoot bool)
AddListChild(attr string, child outputNode)
New(attr string) outputNode
SetUID(uid uint64, attr string)
IsEmpty() bool
addCountAtRoot(*SubGraph)
addGroupby(*SubGraph, *groupResults, string)
addAggregations(*SubGraph) error
}
func makeScalarNode(attr string, isChild bool, val []byte, list bool) *fastJsonNode {
return &fastJsonNode{
attr: attr,
isChild: isChild,
scalarVal: val,
list: list,
}
}
type fastJsonNode struct {
attr string
order int // relative ordering (for sorted results)
isChild bool
scalarVal []byte
attrs []*fastJsonNode
list bool
}
func (fj *fastJsonNode) AddValue(attr string, v types.Val) {
fj.AddListValue(attr, v, false)
}
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))
}
}
func (fj *fastJsonNode) AddMapChild(attr string, val outputNode, isRoot bool) {
var childNode *fastJsonNode
for _, c := range fj.attrs {
if c.attr == attr {
childNode = c
break
}
}
if childNode != nil {
val.(*fastJsonNode).isChild = true
val.(*fastJsonNode).attr = attr
childNode.attrs = append(childNode.attrs, val.(*fastJsonNode).attrs...)
} else {
val.(*fastJsonNode).isChild = false
val.(*fastJsonNode).attr = attr
fj.attrs = append(fj.attrs, val.(*fastJsonNode))
}
}
func (fj *fastJsonNode) AddListChild(attr string, child outputNode) {
child.(*fastJsonNode).attr = attr
child.(*fastJsonNode).isChild = true
fj.attrs = append(fj.attrs, child.(*fastJsonNode))
}
func (fj *fastJsonNode) New(attr string) outputNode {
return &fastJsonNode{attr: attr, isChild: false}
}
func (fj *fastJsonNode) SetUID(uid uint64, attr string) {
// if we're in debug mode, uid may be added second time, skip this
if attr == "uid" {
for _, a := range fj.attrs {
if a.attr == attr {
return
}
}
}
fj.attrs = append(fj.attrs, makeScalarNode(attr, false, []byte(fmt.Sprintf("\"%#x\"", uid)),
false))
}
func (fj *fastJsonNode) IsEmpty() bool {
return len(fj.attrs) == 0
}
func valToBytes(v types.Val) ([]byte, error) {
switch v.Tid {
case types.StringID, types.DefaultID:
return json.Marshal(v.Value)
case types.BinaryID:
return []byte(fmt.Sprintf("%q", v.Value)), nil
case types.IntID:
return []byte(fmt.Sprintf("%d", v.Value)), nil
case types.FloatID:
return []byte(fmt.Sprintf("%f", v.Value)), nil
case types.BoolID:
if v.Value.(bool) {
return []byte("true"), nil
}
return []byte("false"), nil
case types.DateTimeID:
// Return empty string instead of zero-time value string - issue#3166
t := v.Value.(time.Time)
if t.IsZero() {
return []byte(`""`), nil
}
return t.MarshalJSON()
case types.GeoID:
return geojson.Marshal(v.Value.(geom.T))
case types.UidID:
return []byte(fmt.Sprintf("\"%#x\"", v.Value)), nil
case types.PasswordID:
return []byte(fmt.Sprintf("%q", v.Value.(string))), nil
default:
return nil, errors.New("Unsupported types.Val.Tid")
}
}
type nodeSlice []*fastJsonNode
func (n nodeSlice) Len() int {
return len(n)
}
func (n nodeSlice) Less(i, j int) bool {
cmp := strings.Compare(n[i].attr, n[j].attr)
if cmp == 0 {
return n[i].order < n[j].order
}
return cmp < 0
}
func (n nodeSlice) Swap(i, j int) {
n[i], n[j] = n[j], n[i]
}
func (fj *fastJsonNode) writeKey(out *bytes.Buffer) {
out.WriteRune('"')
out.WriteString(fj.attr)
out.WriteRune('"')
out.WriteRune(':')
}
func (fj *fastJsonNode) encode(out *bytes.Buffer) {
// set relative ordering
for i, a := range fj.attrs {
a.order = i
}
i := 0
if i < len(fj.attrs) {
out.WriteRune('{')
cur := fj.attrs[i]
i++
cnt := 1
last := false
inArray := false
for {
var next *fastJsonNode
if i < len(fj.attrs) {
next = fj.attrs[i]
i++
} else {
last = true
}
if !last {
if cur.attr == next.attr {
if cnt == 1 {
cur.writeKey(out)
out.WriteRune('[')
inArray = true
}
cur.encode(out)
cnt++
} else {
if cnt == 1 {
cur.writeKey(out)
if cur.isChild || cur.list {
out.WriteRune('[')
inArray = true
}
}
cur.encode(out)
if cnt != 1 || (cur.isChild || cur.list) {
out.WriteRune(']')
inArray = false
}
cnt = 1
}
out.WriteRune(',')
cur = next
} else {
if cnt == 1 {
cur.writeKey(out)
}
if (cur.isChild || cur.list) && !inArray {
out.WriteRune('[')
}
cur.encode(out)
if cnt != 1 || (cur.isChild || cur.list) {
out.WriteRune(']')
}
break
}
}
out.WriteRune('}')
} else {
out.Write(fj.scalarVal)
}
}
func merge(parent [][]*fastJsonNode, child [][]*fastJsonNode) ([][]*fastJsonNode, error) {
if len(parent) == 0 {
return child, nil
}
// Here we merge two slices of maps.
mergedList := make([][]*fastJsonNode, 0, len(parent)*len(child))
cnt := 0
for _, pa := range parent {
for _, ca := range child {
cnt += len(pa) + len(ca)
if cnt > int(x.Config.NormalizeNodeLimit) {
return nil, x.Errorf("Couldn't evaluate @normalize directive - too many results")
}
list := make([]*fastJsonNode, 0, len(pa)+len(ca))
list = append(list, pa...)
list = append(list, ca...)
mergedList = append(mergedList, list)
}
}
return mergedList, nil
}
func (fj *fastJsonNode) normalize() ([][]*fastJsonNode, error) {
cnt := 0
for _, a := range fj.attrs {
if a.isChild {
cnt++
}
}
if cnt == 0 {
// Recursion base case
// There are no children, we can just return slice with fj.attrs map.
return [][]*fastJsonNode{fj.attrs}, nil
}
parentSlice := make([][]*fastJsonNode, 0, 5)
// If the parents has attrs, lets add them to the slice so that it can be
// merged with children later.
attrs := make([]*fastJsonNode, 0, len(fj.attrs)-cnt)
for _, a := range fj.attrs {
if !a.isChild {
attrs = append(attrs, a)
}
}
parentSlice = append(parentSlice, attrs)
for ci := 0; ci < len(fj.attrs); {
childNode := fj.attrs[ci]
if !childNode.isChild {
ci++
continue
}
childSlice := make([][]*fastJsonNode, 0, 5)
for ci < len(fj.attrs) && childNode.attr == fj.attrs[ci].attr {
normalized, err := fj.attrs[ci].normalize()
if err != nil {
return nil, err
}
childSlice = append(childSlice, normalized...)
ci++
}
// Merging with parent.
var err error
parentSlice, err = merge(parentSlice, childSlice)
if err != nil {
return nil, err
}
}
for i, slice := range parentSlice {
sort.Sort(nodeSlice(slice))
first := -1
last := 0
for i := range slice {
if slice[i].attr == "uid" {
if first == -1 {
first = i
}
last = i
}
}
if first != -1 && first != last {
if first == 0 {
parentSlice[i] = slice[last:]
} else {
parentSlice[i] = append(slice[:first], slice[last:]...)
}
}
}
return parentSlice, nil
}
func (fj *fastJsonNode) addGroupby(sg *SubGraph, res *groupResults, fname string) {
// Don't add empty groupby
if len(res.group) == 0 {
return
}
g := fj.New(fname)
for _, grp := range res.group {
uc := g.New("@groupby")
for _, it := range grp.keys {
uc.AddValue(it.attr, it.key)
}
for _, it := range grp.aggregates {
uc.AddValue(it.attr, it.key)
}
g.AddListChild("@groupby", uc)
}
fj.AddListChild(fname, g)
}
func (fj *fastJsonNode) addCountAtRoot(sg *SubGraph) {
c := types.ValueForType(types.IntID)
c.Value = int64(len(sg.DestUIDs.Uids))
n1 := fj.New(sg.Params.Alias)
field := sg.Params.uidCountAlias
if field == "" {
field = "count"
}
n1.AddValue(field, c)
fj.AddListChild(sg.Params.Alias, n1)
}
func (fj *fastJsonNode) addAggregations(sg *SubGraph) error {
for _, child := range sg.Children {
aggVal, ok := child.Params.uidToVal[0]
if !ok {
if len(child.Params.NeedsVar) == 0 {
return x.Errorf("Only aggregated variables allowed within empty block.")
}
// the aggregation didn't happen, most likely was called with unset vars.
// See: query.go:fillVars
aggVal = types.Val{Tid: types.FloatID, Value: float64(0)}
}
if child.Params.Normalize && child.Params.Alias == "" {
continue
}
fieldName := aggWithVarFieldName(child)
n1 := fj.New(fieldName)
n1.AddValue(fieldName, aggVal)
fj.AddListChild(sg.Params.Alias, n1)
}
if fj.IsEmpty() {
fj.AddListChild(sg.Params.Alias, &fastJsonNode{})
}
return nil
}
func processNodeUids(fj *fastJsonNode, sg *SubGraph) error {
var seedNode *fastJsonNode
if sg.Params.IsEmpty {
return fj.addAggregations(sg)
}
if sg.uidMatrix == nil {
fj.AddListChild(sg.Params.Alias, &fastJsonNode{})
return nil
}
hasChild := false
if sg.Params.uidCount && !(sg.Params.uidCountAlias == "" && sg.Params.Normalize) {
hasChild = true
fj.addCountAtRoot(sg)
}
if sg.Params.isGroupBy {
if len(sg.GroupbyRes) == 0 {
return x.Errorf("Expected GroupbyRes to have length > 0.")
}
fj.addGroupby(sg, sg.GroupbyRes[0], sg.Params.Alias)
return nil
}
lenList := len(sg.uidMatrix[0].Uids)
for i := 0; i < lenList; i++ {
uid := sg.uidMatrix[0].Uids[i]
if algo.IndexOf(sg.DestUIDs, uid) < 0 {
// This UID was filtered. So Ignore it.
continue
}
n1 := seedNode.New(sg.Params.Alias)
if err := sg.preTraverse(uid, n1); err != nil {
if err.Error() == "_INV_" {
continue
}
return err
}
if n1.IsEmpty() {
continue
}
hasChild = true
if !sg.Params.Normalize {
fj.AddListChild(sg.Params.Alias, n1)
continue
}
// Lets normalize the response now.
normalized, err := n1.(*fastJsonNode).normalize()
if err != nil {
return err
}
for _, c := range normalized {
fj.AddListChild(sg.Params.Alias, &fastJsonNode{attrs: c})
}
}
if !hasChild {
// So that we return an empty key if the root didn't have any children.
fj.AddListChild(sg.Params.Alias, &fastJsonNode{})
}
return nil
}
type Extensions struct {
Latency *api.Latency `json:"server_latency,omitempty"`
Txn *api.TxnContext `json:"txn,omitempty"`
}
func (sg *SubGraph) toFastJSON(l *Latency) ([]byte, error) {
defer func() {
l.Json = time.Since(l.Start) - l.Parsing - l.Processing
}()
var seedNode *fastJsonNode
var err error
n := seedNode.New("_root_")
for _, sg := range sg.Children {
err = processNodeUids(n.(*fastJsonNode), sg)
if err != nil {
return nil, err
}
}
// According to GraphQL spec response should only contain data, errors and extensions as top
// level keys. Hence we send server_latency under extensions key.
// https://facebook.github.io/graphql/#sec-Response-Format
var bufw bytes.Buffer
if len(n.(*fastJsonNode).attrs) == 0 {
bufw.WriteString(`{}`)
} else {
n.(*fastJsonNode).encode(&bufw)
}
return bufw.Bytes(), nil
}