MCPcopy
hub / github.com/dgraph-io/dgraph / sortAndPaginateUsingVar

Method sortAndPaginateUsingVar

query/query.go:2697–2738  ·  view source on GitHub ↗
(ctx context.Context)

Source from the content-addressed store, hash-verified

2695}
2696
2697func (sg *SubGraph) sortAndPaginateUsingVar(ctx context.Context) error {
2698 // nil has a different meaning from an initialized map of zero length here. If the variable
2699 // didn't return any values then UidToVal would be an empty with zero length. If the variable
2700 // was used before definition, UidToVal would be nil.
2701 if sg.Params.UidToVal == nil {
2702 return errors.Errorf("Variable: [%s] used before definition.", sg.Params.Order[0].Attr)
2703 }
2704
2705 for i := range sg.uidMatrix {
2706 ul := sg.uidMatrix[i]
2707 uids := make([]uint64, 0, len(ul.Uids))
2708 values := make([][]types.Val, 0, len(ul.Uids))
2709 for _, uid := range ul.Uids {
2710 v, ok := sg.Params.UidToVal.Get(uid)
2711 if !ok {
2712 // We skip the UIDs which don't have a value
2713 continue
2714 }
2715 values = append(values, []types.Val{v})
2716 uids = append(uids, uid)
2717 }
2718 if len(values) == 0 {
2719 continue
2720 }
2721 if err := types.Sort(values, &uids, []bool{sg.Params.Order[0].Desc}, ""); err != nil {
2722 return err
2723 }
2724 sg.uidMatrix[i].Uids = uids
2725 }
2726
2727 if sg.Params.Count != 0 || sg.Params.Offset != 0 {
2728 // Apply the pagination.
2729 for i := range sg.uidMatrix {
2730 start, end := x.PageRange(sg.Params.Count, sg.Params.Offset, len(sg.uidMatrix[i].Uids))
2731 sg.uidMatrix[i].Uids = sg.uidMatrix[i].Uids[start:end]
2732 }
2733 }
2734
2735 // Update the destUids as we might have removed some UIDs.
2736 sg.updateDestUids()
2737 return nil
2738}
2739
2740// isValidArg checks if arg passed is valid keyword.
2741func isValidArg(a string) bool {

Callers 1

Calls 5

updateDestUidsMethod · 0.95
SortFunction · 0.92
PageRangeFunction · 0.92
GetMethod · 0.65
ErrorfMethod · 0.45

Tested by

no test coverage detected