forked from dgraph-io/dgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.go
241 lines (206 loc) · 5.71 KB
/
sort.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
package worker
import (
"golang.org/x/net/context"
"github.com/dgraph-io/dgraph/group"
"github.com/dgraph-io/dgraph/posting"
"github.com/dgraph-io/dgraph/schema"
"github.com/dgraph-io/dgraph/task"
"github.com/dgraph-io/dgraph/types"
"github.com/dgraph-io/dgraph/x"
)
var emptySortResult task.SortResult
// SortOverNetwork sends sort query over the network.
func SortOverNetwork(ctx context.Context, q *task.Sort) (*task.SortResult, error) {
gid := group.BelongsTo(q.Attr)
x.Trace(ctx, "worker.Sort attr: %v groupId: %v", q.Attr, gid)
if groups().ServesGroup(gid) {
// No need for a network call, as this should be run from within this instance.
return processSort(q)
}
// Send this over the network.
// TODO: Send the request to multiple servers as described in Jeff Dean's talk.
addr := groups().AnyServer(gid)
pl := pools().get(addr)
conn, err := pl.Get()
if err != nil {
return &emptySortResult, x.Wrapf(err, "SortOverNetwork: while retrieving connection.")
}
defer pl.Put(conn)
x.Trace(ctx, "Sending request to %v", addr)
c := NewWorkerClient(conn)
var reply *task.SortResult
cerr := make(chan error, 1)
go func() {
var err error
reply, err = c.Sort(ctx, q)
cerr <- err
}()
select {
case <-ctx.Done():
return &emptySortResult, ctx.Err()
case err := <-cerr:
if err != nil {
x.TraceError(ctx, x.Wrapf(err, "Error while calling Worker.Sort"))
}
return reply, err
}
}
// Sort is used to sort given UID matrix.
func (w *grpcWorker) Sort(ctx context.Context, s *task.Sort) (*task.SortResult, error) {
if ctx.Err() != nil {
return &emptySortResult, ctx.Err()
}
gid := group.BelongsTo(s.Attr)
//x.Trace(ctx, "Attribute: %q NumUids: %v groupId: %v Sort", q.Attr(), q.UidsLength(), gid)
var reply *task.SortResult
x.AssertTruef(groups().ServesGroup(gid),
"attr: %q groupId: %v Request sent to wrong server.", s.Attr, gid)
c := make(chan error, 1)
go func() {
var err error
reply, err = processSort(s)
c <- err
}()
select {
case <-ctx.Done():
return &emptySortResult, ctx.Err()
case err := <-c:
return reply, err
}
}
var (
errContinue = x.Errorf("Continue processing buckets")
errDone = x.Errorf("Done processing buckets")
)
// processSort does either a coarse or a fine sort.
func processSort(ts *task.Sort) (*task.SortResult, error) {
attr := ts.Attr
x.AssertTruef(ts.Count > 0,
("We do not yet support negative or infinite count with sorting: %s %d. " +
"Try flipping order and return first few elements instead."),
attr, ts.Count)
n := len(ts.UidMatrix)
out := make([]intersectedList, n)
for i := 0; i < n; i++ {
// offsets[i] is the offset for i-th posting list. It gets decremented as we
// iterate over buckets.
out[i].offset = int(ts.Offset)
out[i].ulist = &task.List{Uids: []uint64{}}
}
// Iterate over every bucket in TokensTable.
t := posting.GetTokensTable(attr)
var token string
if ts.Desc {
token = t.GetLast()
} else {
token = t.GetFirst()
}
BUCKETS:
for len(token) > 0 {
err := intersectBucket(ts, attr, token, out)
switch err {
case errDone:
break BUCKETS
case errContinue:
// Continue iterating over tokens.
default:
return &emptySortResult, err
}
if ts.Desc {
token = t.GetPrev(token)
} else {
token = t.GetNext(token)
}
}
r := new(task.SortResult)
for _, il := range out {
r.UidMatrix = append(r.UidMatrix, il.ulist)
}
return r, nil
}
type intersectedList struct {
offset int
ulist *task.List
}
func intersectBucket(ts *task.Sort, attr, token string, out []intersectedList) error {
count := int(ts.Count)
sType, err := schema.TypeOf(attr)
if err != nil || !sType.IsScalar() {
return x.Errorf("Cannot sort attribute %s of type object.", attr)
}
scalar := sType
key := x.IndexKey(attr, token)
pl, decr := posting.GetOrCreate(key, 0)
defer decr()
for i, ul := range ts.UidMatrix {
il := &out[i]
if count > 0 && len(il.ulist.Uids) >= count {
continue
}
// Intersect index with i-th input UID list.
listOpt := posting.ListOptions{Intersect: ul}
result := pl.Uids(listOpt)
n := len(result.Uids)
// Check offsets[i].
if il.offset >= n {
// We are going to skip the whole intersection. No need to do actual
// sorting. Just update offsets[i].
il.offset -= n
continue
}
// Sort results by value before applying offset.
sortByValue(attr, result, scalar, ts.Desc)
if il.offset > 0 {
result.Uids = result.Uids[il.offset:n]
il.offset = 0
n = len(result.Uids)
}
// n is number of elements to copy from result to out.
if count > 0 {
slack := count - len(il.ulist.Uids)
if slack < n {
n = slack
}
}
// Copy from result to out.
for j := 0; j < n; j++ {
il.ulist.Uids = append(il.ulist.Uids, result.Uids[j])
}
} // end for loop
// Check out[i] sizes for all i.
for i := 0; i < len(ts.UidMatrix); i++ { // Iterate over UID lists.
if len(out[i].ulist.Uids) < count {
return errContinue
}
x.AssertTrue(len(out[i].ulist.Uids) == count)
}
return errDone
}
// sortByValue fetches values and sort UIDList.
func sortByValue(attr string, ul *task.List, typ types.TypeID, desc bool) error {
values := make([]types.Val, len(ul.Uids))
for i, uid := range ul.Uids {
val, err := fetchValue(uid, attr, typ)
if err != nil {
return err
}
values[i] = val
}
return types.Sort(typ, values, ul, desc)
}
// fetchValue gets the value for a given UID.
func fetchValue(uid uint64, attr string, scalar types.TypeID) (types.Val, error) {
// TODO: Maybe use posting.Get
pl, decr := posting.GetOrCreate(x.DataKey(attr, uid), group.BelongsTo(attr))
defer decr()
src, err := pl.Value()
if err != nil {
return types.Val{}, err
}
dst := types.ValueForType(scalar)
err = types.Convert(src, &dst)
if err != nil {
return types.Val{}, err
}
return dst, nil
}