(ctx context.Context, r *sortresult, ts *pb.SortMessage)
| 371 | } |
| 372 | |
| 373 | func multiSort(ctx context.Context, r *sortresult, ts *pb.SortMessage) error { |
| 374 | span := trace.SpanFromContext(ctx) |
| 375 | span.SetAttributes(attribute.String("multiSort", "true")) |
| 376 | |
| 377 | // SrcUids for other queries are all the uids present in the response of the first sort. |
| 378 | dest := destUids(r.reply.UidMatrix) |
| 379 | |
| 380 | // For each uid in dest uids, we have multiple values which belong to different attributes. |
| 381 | // 1 -> [ "Alice", 23, "1932-01-01"] |
| 382 | // 10 -> [ "Bob", 35, "1912-02-01" ] |
| 383 | sortVals := make([][]types.Val, len(dest.Uids)) |
| 384 | for idx := range sortVals { |
| 385 | sortVals[idx] = make([]types.Val, len(ts.Order)) |
| 386 | } |
| 387 | |
| 388 | seen := make(map[uint64]struct{}) |
| 389 | // Walk through the uidMatrix and put values for this attribute in sortVals. |
| 390 | for i, ul := range r.reply.UidMatrix { |
| 391 | x.AssertTrue(len(ul.Uids) == len(r.vals[i])) |
| 392 | for j, uid := range ul.Uids { |
| 393 | uidx := algo.IndexOf(dest, uid) |
| 394 | x.AssertTrue(uidx >= 0) |
| 395 | |
| 396 | if _, ok := seen[uid]; ok { |
| 397 | // We have already seen this uid. |
| 398 | continue |
| 399 | } |
| 400 | seen[uid] = struct{}{} |
| 401 | sortVals[uidx][0] = r.vals[i][j] |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | // Execute rest of the sorts concurrently. |
| 406 | och := make(chan orderResult, len(ts.Order)-1) |
| 407 | for i := 1; i < len(ts.Order); i++ { |
| 408 | in := &pb.Query{ |
| 409 | Attr: ts.Order[i].Attr, |
| 410 | UidList: dest, |
| 411 | Langs: ts.Order[i].Langs, |
| 412 | ReadTs: ts.ReadTs, |
| 413 | } |
| 414 | go fetchValues(ctx, in, i, och) |
| 415 | } |
| 416 | |
| 417 | var oerr error |
| 418 | // TODO - Verify behavior with multiple langs. |
| 419 | for i := 1; i < len(ts.Order); i++ { |
| 420 | or := <-och |
| 421 | if or.err != nil { |
| 422 | if oerr == nil { |
| 423 | oerr = or.err |
| 424 | } |
| 425 | continue |
| 426 | } |
| 427 | |
| 428 | result := or.r |
| 429 | x.AssertTrue(len(result.ValueMatrix) == len(dest.Uids)) |
| 430 | for i := range dest.Uids { |
no test coverage detected