applyOrderAndPagination orders each posting list by a given attribute before applying pagination.
(ctx context.Context)
| 2509 | // applyOrderAndPagination orders each posting list by a given attribute |
| 2510 | // before applying pagination. |
| 2511 | func (sg *SubGraph) applyOrderAndPagination(ctx context.Context) error { |
| 2512 | if len(sg.Params.Order) == 0 && len(sg.Params.FacetsOrder) == 0 { |
| 2513 | return nil |
| 2514 | } |
| 2515 | |
| 2516 | sg.updateUidMatrix() |
| 2517 | |
| 2518 | // See if we need to apply order based on facet. |
| 2519 | if len(sg.Params.FacetsOrder) != 0 { |
| 2520 | return sg.sortAndPaginateUsingFacet(ctx) |
| 2521 | } |
| 2522 | |
| 2523 | for _, it := range sg.Params.NeedsVar { |
| 2524 | // TODO(pawan) - Return error if user uses var order with predicates. |
| 2525 | if len(sg.Params.Order) > 0 && it.Name == sg.Params.Order[0].Attr && |
| 2526 | (it.Typ == dql.ValueVar) { |
| 2527 | // If the Order name is same as var name and it's a value variable, we sort using that variable. |
| 2528 | return sg.sortAndPaginateUsingVar(ctx) |
| 2529 | } |
| 2530 | } |
| 2531 | |
| 2532 | // Todo: fix offset for cascade queries. |
| 2533 | if sg.Params.Count == 0 { |
| 2534 | // Only retrieve up to 1000 results by default. |
| 2535 | sg.Params.Count = 1000 |
| 2536 | } |
| 2537 | |
| 2538 | x.AssertTrue(len(sg.Params.Order) > 0) |
| 2539 | |
| 2540 | ns, err := x.ExtractNamespace(ctx) |
| 2541 | if err != nil { |
| 2542 | return errors.Wrapf(err, "While ordering and paginating") |
| 2543 | } |
| 2544 | order := sg.createOrderForTask(ns) |
| 2545 | sortMsg := &pb.SortMessage{ |
| 2546 | Order: order, |
| 2547 | UidMatrix: sg.uidMatrix, |
| 2548 | Offset: int32(sg.Params.Offset), |
| 2549 | Count: int32(sg.Params.Count), |
| 2550 | ReadTs: sg.ReadTs, |
| 2551 | } |
| 2552 | result, err := worker.SortOverNetwork(ctx, sortMsg) |
| 2553 | if err != nil { |
| 2554 | return err |
| 2555 | } |
| 2556 | |
| 2557 | x.AssertTrue(len(result.UidMatrix) == len(sg.uidMatrix)) |
| 2558 | if sg.facetsMatrix != nil { |
| 2559 | // The order of uids in the lists which are part of the uidMatrix would have been changed |
| 2560 | // after sort. We want to update the order of lists in the facetMatrix accordingly. |
| 2561 | for idx, rl := range result.UidMatrix { |
| 2562 | fl := make([]*pb.Facets, 0, len(sg.facetsMatrix[idx].FacetsList)) |
| 2563 | for _, uid := range rl.Uids { |
| 2564 | // Find index of this uid in original sorted uid list. |
| 2565 | oidx := algo.IndexOf(sg.uidMatrix[idx], uid) |
| 2566 | // Find corresponding facet. |
| 2567 | fl = append(fl, sg.facetsMatrix[idx].FacetsList[oidx]) |
| 2568 | } |
no test coverage detected