-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathrecurse.go
248 lines (221 loc) · 6.31 KB
/
recurse.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
/*
* 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 (
"context"
"math"
"strconv"
"github.com/dgraph-io/dgraph/algo"
"github.com/dgraph-io/dgraph/codec"
"github.com/dgraph-io/dgraph/x"
"github.com/dgraph-io/sroar"
"github.com/pkg/errors"
)
func (start *SubGraph) expandRecurse(ctx context.Context, maxDepth uint64) error {
// Note: Key format is - "attr|fromUID|toUID"
reachMap := make(map[string]*sroar.Bitmap)
allowLoop := start.Params.RecurseArgs.AllowLoop
var numEdges uint64
var exec []*SubGraph
var err error
rrch := make(chan error, len(exec))
startChildren := make([]*SubGraph, len(start.Children))
copy(startChildren, start.Children)
// Empty children before giving to ProcessGraph as we are only concerned with DestUids.
start.Children = start.Children[:0]
// Process the root first.
go ProcessGraph(ctx, start, nil, rrch)
select {
case err = <-rrch:
if err != nil {
return err
}
case <-ctx.Done():
return ctx.Err()
}
if start.UnknownAttr {
return nil
}
// Add children back and expand if necessary
if exec, err = expandChildren(ctx, start, startChildren); err != nil {
return err
}
dummy := &SubGraph{}
var depth uint64
for {
if depth >= maxDepth {
return nil
}
depth++
// When the maximum depth has been reached, avoid retrieving any facets as
// the nodes at the other end of the edge will not be a part of this query.
// Otherwise, the facets will be included in the query without any other
// information about the node, which is quite counter-intuitive.
if depth == maxDepth {
for _, sg := range exec {
sg.Params.Facet = nil
}
}
rrch := make(chan error, len(exec))
for _, sg := range exec {
go ProcessGraph(ctx, sg, dummy, rrch)
}
var recurseErr error
for range exec {
select {
case err = <-rrch:
if err != nil {
if recurseErr == nil {
recurseErr = err
}
}
case <-ctx.Done():
if recurseErr == nil {
recurseErr = ctx.Err()
}
}
}
if recurseErr != nil {
return recurseErr
}
for _, sg := range exec {
// sg.uidMatrix can be empty. Continue if that is the case.
if len(sg.uidMatrix) == 0 {
continue
}
if sg.UnknownAttr {
continue
}
if len(sg.Filters) > 0 {
// We need to do this in case we had some filters.
sg.updateUidMatrix()
}
for mIdx, fromUID := range codec.GetUids(sg.SrcUIDs) {
if allowLoop {
// TODO: This needs to be optimized.
for _, ul := range sg.uidMatrix {
numEdges += codec.ListCardinality(ul)
}
} else {
ul := sg.uidMatrix[mIdx]
ur := codec.FromListNoCopy(ul)
if ur == nil {
continue
}
key := sg.Attr + "|" + strconv.Itoa(int(fromUID))
prev, ok := reachMap[key]
if !ok {
reachMap[key] = codec.FromList(ul)
continue
}
// Any edges that we have seen before, do not consider
// them again.
if len(sg.uidMatrix[mIdx].SortedUids) > 0 {
// we will have to keep the order, so using ApplyFilter
algo.ApplyFilter(sg.uidMatrix[mIdx], func(uid uint64, i int) bool {
if ur.Contains(uid) {
return false
}
numEdges++
return true
})
} else {
ur.AndNot(prev) // This would only keep the UIDs which are NEW.
sg.uidMatrix[mIdx].Bitmap = ur.ToBuffer()
numEdges += uint64(ur.GetCardinality())
prev.Or(ur) // Add the new UIDs to our "reach"
reachMap[key] = prev
}
}
}
if len(sg.Params.Order) > 0 || len(sg.Params.FacetsOrder) > 0 {
// Can't use merge sort if the UIDs are not sorted.
sg.updateDestUids()
} else {
sg.DestMap = codec.Merge(sg.uidMatrix)
}
}
// modify the exec and attach child nodes.
var out []*SubGraph
var exp []*SubGraph
for _, sg := range exec {
if sg.UnknownAttr {
continue
}
if sg.DestMap.IsEmpty() {
continue
}
if exp, err = expandChildren(ctx, sg, startChildren); err != nil {
return err
}
out = append(out, exp...)
}
if numEdges > x.Config.LimitQueryEdge {
// If we've seen too many edges, stop the query.
return errors.Errorf("Exceeded query edge limit = %v. Found %v edges.",
x.Config.LimitQueryEdge, numEdges)
}
if len(out) == 0 {
return nil
}
exec = out
}
}
// expandChildren adds child nodes to a SubGraph with no children, expanding them if necessary.
func expandChildren(ctx context.Context, sg *SubGraph, children []*SubGraph) ([]*SubGraph, error) {
if len(sg.Children) > 0 {
return nil, errors.New("Subgraph should not have any children")
}
// Add children and expand if necessary
sg.Children = append(sg.Children, children...)
expandedChildren, err := expandSubgraph(ctx, sg)
if err != nil {
return nil, err
}
out := make([]*SubGraph, 0, len(expandedChildren))
sg.Children = sg.Children[:0]
// Link new child nodes back to parent destination UIDs
for _, child := range expandedChildren {
newChild := new(SubGraph)
newChild.copyFiltersRecurse(child)
newChild.SrcUIDs = codec.ToList(sg.DestMap)
newChild.Params.Var = child.Params.Var
sg.Children = append(sg.Children, newChild)
out = append(out, newChild)
}
return out, nil
}
func recurse(ctx context.Context, sg *SubGraph) error {
if !sg.Params.Recurse {
return errors.Errorf("Invalid recurse path query")
}
depth := sg.Params.RecurseArgs.Depth
if depth == 0 {
if sg.Params.RecurseArgs.AllowLoop {
return errors.Errorf("Depth must be > 0 when loop is true for recurse query")
}
// If no depth is specified, expand till we reach all leaf nodes
// or we see reach too many nodes.
depth = math.MaxUint64
}
for _, child := range sg.Children {
if len(child.Children) > 0 {
return errors.Errorf(
"recurse queries require that all predicates are specified in one level")
}
}
return sg.expandRecurse(ctx, depth)
}