-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
outputnode.go
1040 lines (942 loc) · 25.6 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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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"
"fmt"
"math"
"sort"
"strconv"
"strings"
"sync"
"time"
"unicode/utf8"
"github.com/golang/glog"
"github.com/pkg/errors"
geom "github.com/twpayne/go-geom"
"github.com/twpayne/go-geom/encoding/geojson"
"github.com/dgraph-io/dgo/v200/protos/api"
"github.com/dgraph-io/dgraph/algo"
"github.com/dgraph-io/dgraph/protos/pb"
"github.com/dgraph-io/dgraph/task"
"github.com/dgraph-io/dgraph/types"
"github.com/dgraph-io/dgraph/types/facets"
"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)
}
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 *fastJsonNode, isRoot bool) {
var childNode *fastJsonNode
for _, c := range fj.attrs {
if c.attr == attr {
childNode = c
break
}
}
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
fj.attrs = append(fj.attrs, child)
}
func (fj *fastJsonNode) New(attr string) *fastJsonNode {
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
}
var (
boolTrue = []byte("true")
boolFalse = []byte("false")
emptyString = []byte(`""`)
// Below variables are used in stringJsonMarshal function.
bufferPool = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
hex = "0123456789abcdef"
escapeHTML = true
)
// stringJsonMarshal is replacement for json.Marshal() function only for string type.
// This function is encodeState.string(string, escapeHTML) in "encoding/json/encode.go".
// It should be in sync with encodeState.string function.
func stringJsonMarshal(s string) []byte {
e := bufferPool.Get().(*bytes.Buffer)
e.Reset()
e.WriteByte('"')
start := 0
for i := 0; i < len(s); {
if b := s[i]; b < utf8.RuneSelf {
if htmlSafeSet[b] || (!escapeHTML && safeSet[b]) {
i++
continue
}
if start < i {
e.WriteString(s[start:i])
}
e.WriteByte('\\')
switch b {
case '\\', '"':
e.WriteByte(b)
case '\n':
e.WriteByte('n')
case '\r':
e.WriteByte('r')
case '\t':
e.WriteByte('t')
default:
// This encodes bytes < 0x20 except for \t, \n and \r.
// If escapeHTML is set, it also escapes <, >, and &
// because they can lead to security holes when
// user-controlled strings are rendered into JSON
// and served to some browsers.
e.WriteString(`u00`)
e.WriteByte(hex[b>>4])
e.WriteByte(hex[b&0xF])
}
i++
start = i
continue
}
c, size := utf8.DecodeRuneInString(s[i:])
if c == utf8.RuneError && size == 1 {
if start < i {
e.WriteString(s[start:i])
}
e.WriteString(`\ufffd`)
i += size
start = i
continue
}
// U+2028 is LINE SEPARATOR.
// U+2029 is PARAGRAPH SEPARATOR.
// They are both technically valid characters in JSON strings,
// but don't work in JSONP, which has to be evaluated as JavaScript,
// and can lead to security holes there. It is valid JSON to
// escape them, so we do so unconditionally.
// See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion.
if c == '\u2028' || c == '\u2029' {
if start < i {
e.WriteString(s[start:i])
}
e.WriteString(`\u202`)
e.WriteByte(hex[c&0xF])
i += size
start = i
continue
}
i += size
}
if start < len(s) {
e.WriteString(s[start:])
}
e.WriteByte('"')
buf := append([]byte(nil), e.Bytes()...)
bufferPool.Put(e)
return buf
}
func valToBytes(v types.Val) ([]byte, error) {
switch v.Tid {
case types.StringID, types.DefaultID:
switch str := v.Value.(type) {
case string:
return stringJsonMarshal(str), nil
default:
return json.Marshal(str)
}
case types.BinaryID:
return []byte(fmt.Sprintf("%q", v.Value)), nil
case types.IntID:
// In types.Convert(), we always convert to int64 for IntID type. fmt.Sprintf is slow
// and hence we are using strconv.FormatInt() here. Since int64 and int are most common int
// types we are using FormatInt for those.
switch num := v.Value.(type) {
case int64:
return []byte(strconv.FormatInt(num, 10)), nil
case int:
return []byte(strconv.FormatInt(int64(num), 10)), nil
default:
return []byte(fmt.Sprintf("%d", v.Value)), nil
}
case types.FloatID:
// +Inf, -Inf and NaN are not representable in JSON.
// Please see https://golang.org/src/encoding/json/encode.go?s=6458:6501#L573
if math.IsInf(v.Value.(float64), 0) || math.IsNaN(v.Value.(float64)) {
return nil, errors.New("Unsupported Inf or NaN in float field")
}
return []byte(fmt.Sprintf("%f", v.Value)), nil
case types.BoolID:
if v.Value.(bool) {
return boolTrue, nil
}
return boolFalse, nil
case types.DateTimeID:
// Return empty string instead of zero-time value string - issue#3166
t := v.Value.(time.Time)
if t.IsZero() {
return emptyString, 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) error {
if _, err := out.WriteRune('"'); err != nil {
return err
}
if _, err := out.WriteString(fj.attr); err != nil {
return err
}
if _, err := out.WriteRune('"'); err != nil {
return err
}
if _, err := out.WriteRune(':'); err != nil {
return err
}
return nil
}
func (fj *fastJsonNode) attachFacets(fieldName string, isList bool,
fList []*api.Facet, facetIdx int) error {
for _, f := range fList {
fName := facetName(fieldName, f)
fVal, err := facets.ValFor(f)
if err != nil {
return err
}
if !isList {
fj.AddValue(fName, fVal)
} else {
facetNode := &fastJsonNode{attr: fName}
facetNode.AddValue(strconv.Itoa(facetIdx), fVal)
fj.AddMapChild(fName, facetNode, false)
}
}
return nil
}
func (fj *fastJsonNode) encode(out *bytes.Buffer) error {
// set relative ordering
for i, a := range fj.attrs {
a.order = i
}
i := 0
if i < len(fj.attrs) {
if _, err := out.WriteRune('{'); err != nil {
return err
}
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 {
if err := cur.writeKey(out); err != nil {
return err
}
if _, err := out.WriteRune('['); err != nil {
return err
}
inArray = true
}
if err := cur.encode(out); err != nil {
return err
}
cnt++
} else {
if cnt == 1 {
if err := cur.writeKey(out); err != nil {
return err
}
if cur.isChild || cur.list {
if _, err := out.WriteRune('['); err != nil {
return err
}
inArray = true
}
}
if err := cur.encode(out); err != nil {
return err
}
if cnt != 1 || (cur.isChild || cur.list) {
if _, err := out.WriteRune(']'); err != nil {
return err
}
inArray = false
}
cnt = 1
}
if _, err := out.WriteRune(','); err != nil {
return err
}
cur = next
} else {
if cnt == 1 {
if err := cur.writeKey(out); err != nil {
return err
}
}
if (cur.isChild || 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 _, err := out.WriteRune(']'); err != nil {
return err
}
}
break
}
}
if _, err := out.WriteRune('}'); err != nil {
return err
}
} else {
if _, err := out.Write(fj.scalarVal); err != nil {
return err
}
}
return nil
}
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 > x.Config.NormalizeNodeLimit {
return nil, errors.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
}
// normalize returns all attributes of fj and its children (if any).
func (fj *fastJsonNode) normalize() ([][]*fastJsonNode, error) {
cnt := 0
for _, a := range fj.attrs {
// Here we are counting all non-scalar attributes of fj. If there are any such
// attributes, we will flatten it, otherwise we will return all attributes.
// When we call addMapChild it tries to find whether there is already an attribute
// with attr field same as attribute argument of addMapChild. If it doesn't find any
// such attribute, it creates an attribute with isChild = false. In those cases
// sometimes cnt remains zero and normalize returns attributes without flattening.
// So we are using len(a.attrs) > 0 instead of a.isChild
if len(a.attrs) > 0 {
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 {
// Check comment at previous occurrence of len(a.attrs) > 0
if len(a.attrs) == 0 {
attrs = append(attrs, a)
}
}
parentSlice = append(parentSlice, attrs)
for ci := 0; ci < len(fj.attrs); {
childNode := fj.attrs[ci]
// Check comment at previous occurrence of len(a.attrs) > 0
if len(childNode.attrs) == 0 {
ci++
continue
}
childSlice := make([][]*fastJsonNode, 0, 5)
for ci < len(fj.attrs) && childNode.attr == fj.attrs[ci].attr {
childSlice = append(childSlice, fj.attrs[ci].attrs)
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) addAggregations(sg *SubGraph) error {
for _, child := range sg.Children {
aggVal, ok := child.Params.UidToVal[0]
if !ok {
if len(child.Params.NeedsVar) == 0 {
return errors.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 handleCountUIDNodes(sg *SubGraph, n *fastJsonNode, count int) bool {
addedNewChild := false
fieldName := sg.fieldName()
for _, child := range sg.Children {
uidCount := child.Attr == "uid" && child.Params.DoCount && child.IsInternal()
normWithoutAlias := child.Params.Alias == "" && child.Params.Normalize
if uidCount && !normWithoutAlias {
addedNewChild = true
c := types.ValueForType(types.IntID)
c.Value = int64(count)
field := child.Params.Alias
if field == "" {
field = "count"
}
fjChild := n.New(fieldName)
fjChild.AddValue(field, c)
n.AddListChild(fieldName, fjChild)
}
}
return addedNewChild
}
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 := handleCountUIDNodes(sg, fj, len(sg.DestUIDs.Uids))
if sg.Params.IsGroupBy {
if len(sg.GroupbyRes) == 0 {
return errors.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.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
}
// Extensions represents the extra information appended to query results.
type Extensions struct {
Latency *api.Latency `json:"server_latency,omitempty"`
Txn *api.TxnContext `json:"txn,omitempty"`
Metrics *api.Metrics `json:"metrics,omitempty"`
}
func (sg *SubGraph) toFastJSON(l *Latency) ([]byte, error) {
encodingStart := time.Now()
defer func() {
l.Json = time.Since(encodingStart)
}()
var seedNode *fastJsonNode
var err error
n := seedNode.New("_root_")
for _, sg := range sg.Children {
err = processNodeUids(n, 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.attrs) == 0 {
if _, err := bufw.WriteString(`{}`); err != nil {
return nil, err
}
} else {
if err := n.encode(&bufw); err != nil {
return nil, err
}
}
return bufw.Bytes(), nil
}
func (sg *SubGraph) fieldName() string {
fieldName := sg.Attr
if sg.Params.Alias != "" {
fieldName = sg.Params.Alias
}
return fieldName
}
func addCount(pc *SubGraph, count uint64, dst *fastJsonNode) {
if pc.Params.Normalize && pc.Params.Alias == "" {
return
}
c := types.ValueForType(types.IntID)
c.Value = int64(count)
fieldName := pc.Params.Alias
if fieldName == "" {
fieldName = fmt.Sprintf("count(%s)", pc.Attr)
}
dst.AddValue(fieldName, c)
}
func aggWithVarFieldName(pc *SubGraph) string {
if pc.Params.Alias != "" {
return pc.Params.Alias
}
fieldName := fmt.Sprintf("val(%v)", pc.Params.Var)
if len(pc.Params.NeedsVar) > 0 {
fieldName = fmt.Sprintf("val(%v)", pc.Params.NeedsVar[0].Name)
if pc.SrcFunc != nil {
fieldName = fmt.Sprintf("%s(%v)", pc.SrcFunc.Name, fieldName)
}
}
return fieldName
}
func addInternalNode(pc *SubGraph, uid uint64, dst *fastJsonNode) error {
sv, ok := pc.Params.UidToVal[uid]
if !ok || sv.Value == nil {
return nil
}
fieldName := aggWithVarFieldName(pc)
dst.AddValue(fieldName, sv)
return nil
}
func addCheckPwd(pc *SubGraph, vals []*pb.TaskValue, dst *fastJsonNode) {
c := types.ValueForType(types.BoolID)
if len(vals) == 0 {
c.Value = false
} else {
c.Value = task.ToBool(vals[0])
}
fieldName := pc.Params.Alias
if fieldName == "" {
fieldName = fmt.Sprintf("checkpwd(%s)", pc.Attr)
}
dst.AddValue(fieldName, c)
}
func alreadySeen(parentIds []uint64, uid uint64) bool {
for _, id := range parentIds {
if id == uid {
return true
}
}
return false
}
func facetName(fieldName string, f *api.Facet) string {
if f.Alias != "" {
return f.Alias
}
return fieldName + x.FacetDelimeter + f.Key
}
// This method gets the values and children for a subprotos.
func (sg *SubGraph) preTraverse(uid uint64, dst *fastJsonNode) error {
if sg.Params.IgnoreReflex {
if alreadySeen(sg.Params.ParentIds, uid) {
// A node can't have itself as the child at any level.
return nil
}
// Push myself to stack before sending this to children.
sg.Params.ParentIds = append(sg.Params.ParentIds, uid)
}
var invalidUids map[uint64]bool
// We go through all predicate children of the subprotos.
for _, pc := range sg.Children {
if pc.Params.IgnoreResult {
continue
}
if pc.IsInternal() {
if pc.Params.Expand != "" {
continue
}
if pc.Params.Normalize && pc.Params.Alias == "" {
continue
}
if err := addInternalNode(pc, uid, dst); err != nil {
return err
}
continue
}
if len(pc.uidMatrix) == 0 {
// Can happen in recurse query.
continue
}
if len(pc.facetsMatrix) > 0 && len(pc.facetsMatrix) != len(pc.uidMatrix) {
return errors.Errorf("Length of facetsMatrix and uidMatrix mismatch: %d vs %d",
len(pc.facetsMatrix), len(pc.uidMatrix))
}
idx := algo.IndexOf(pc.SrcUIDs, uid)
if idx < 0 {
continue
}
if pc.Params.IsGroupBy {
if len(pc.GroupbyRes) <= idx {
return errors.Errorf("Unexpected length while adding Groupby. Idx: [%v], len: [%v]",
idx, len(pc.GroupbyRes))
}
dst.addGroupby(pc, pc.GroupbyRes[idx], pc.fieldName())
continue
}
fieldName := pc.fieldName()
switch {
case len(pc.counts) > 0:
addCount(pc, uint64(pc.counts[idx]), dst)
case pc.SrcFunc != nil && pc.SrcFunc.Name == "checkpwd":
addCheckPwd(pc, pc.valueMatrix[idx].Values, dst)
case idx < len(pc.uidMatrix) && len(pc.uidMatrix[idx].Uids) > 0:
var fcsList []*pb.Facets
if pc.Params.Facet != nil {
fcsList = pc.facetsMatrix[idx].FacetsList
}
if sg.Params.IgnoreReflex {
pc.Params.ParentIds = sg.Params.ParentIds
}
// We create as many predicate entity children as the length of uids for
// this predicate.
ul := pc.uidMatrix[idx]
// noneEmptyUID will store indexes of non empty UIDs.
// This will be used for indexing facet response
var nonEmptyUID []int
for childIdx, childUID := range ul.Uids {
if fieldName == "" || (invalidUids != nil && invalidUids[childUID]) {
continue
}
uc := dst.New(fieldName)
if rerr := pc.preTraverse(childUID, uc); rerr != nil {
if rerr.Error() == "_INV_" {
if invalidUids == nil {
invalidUids = make(map[uint64]bool)
}
invalidUids[childUID] = true
continue // next UID.
}
// Some other error.
glog.Errorf("Error while traversal: %v", rerr)
return rerr
}
if !uc.IsEmpty() {
if sg.Params.GetUid {
uc.SetUID(childUID, "uid")
}
nonEmptyUID = append(nonEmptyUID, childIdx) // append index to nonEmptyUID.
if pc.Params.Normalize {
// We will normalize at each level instead of
// calling normalize after pretraverse.
// Now normalize() only flattens one level,
// the expectation is that its children have
// already been normalized.
normAttrs, err := uc.normalize()
if err != nil {
return err
}
for _, c := range normAttrs {
// Adding as list child irrespective of the type of pc
// (list or non-list), otherwise result might be inconsistent or might
// depend on children and grandchildren of pc. Consider the case:
// boss: uid .
// friend: [uid] .
// name: string .
// For query like:
// {
// me(func: uid(0x1)) {
// boss @normalize {
// name
// }
// }
// }
// boss will be non list type in response, but for query like:
// {
// me(func: uid(0x1)) {
// boss @normalize {
// friend {
// name
// }
// }
// }
// }
// boss should be of list type because there can be mutliple friends of
// boss.
dst.AddListChild(fieldName, &fastJsonNode{attrs: c})
}
continue
}
if pc.List {
dst.AddListChild(fieldName, uc)
} else {
dst.AddMapChild(fieldName, uc, false)
}
}
}
// Now fill facets for non empty UIDs.
facetIdx := 0
for _, uidIdx := range nonEmptyUID {
if pc.Params.Facet != nil && len(fcsList) > uidIdx {
fs := fcsList[uidIdx]
err := dst.attachFacets(fieldName, pc.List, fs.Facets, facetIdx)
if err != nil {
return err
}
facetIdx++
}
}
// add value for count(uid) nodes if any.
_ = handleCountUIDNodes(pc, dst, len(ul.Uids))
default:
if pc.Params.Alias == "" && len(pc.Params.Langs) > 0 && pc.Params.Langs[0] != "*" {
fieldName += "@"
fieldName += strings.Join(pc.Params.Langs, ":")
}
if pc.Attr == "uid" {
dst.SetUID(uid, pc.fieldName())
continue
}
if len(pc.facetsMatrix) > idx && len(pc.facetsMatrix[idx].FacetsList) > 0 {
// In case of Value we have only one Facets.
for i, fcts := range pc.facetsMatrix[idx].FacetsList {
if err := dst.attachFacets(fieldName, pc.List, fcts.Facets, i); err != nil {
return err
}
}
}
if len(pc.valueMatrix) <= idx {
continue
}
for i, tv := range pc.valueMatrix[idx].Values {
// if conversion not possible, we ignore it in the result.
sv, convErr := convertWithBestEffort(tv, pc.Attr)
if convErr != nil {
return convErr
}
if pc.Params.ExpandAll && len(pc.LangTags[idx].Lang) != 0 {
if i >= len(pc.LangTags[idx].Lang) {
return errors.Errorf(
"pb.error: all lang tags should be either present or absent")
}
fieldNameWithTag := fieldName
lang := pc.LangTags[idx].Lang[i]
if lang != "" && lang != "*" {
fieldNameWithTag += "@" + lang
}