sortByValue fetches values and sort UIDList.
(ctx context.Context, ts *pb.SortMessage, ul *pb.List, typ types.TypeID)
| 773 | |
| 774 | // sortByValue fetches values and sort UIDList. |
| 775 | func sortByValue(ctx context.Context, ts *pb.SortMessage, ul *pb.List, |
| 776 | typ types.TypeID) ([]types.Val, error) { |
| 777 | lenList := len(ul.Uids) |
| 778 | uids := make([]uint64, 0, lenList) |
| 779 | values := make([][]types.Val, 0, lenList) |
| 780 | multiSortVals := make([]types.Val, 0, lenList) |
| 781 | order := ts.Order[0] |
| 782 | |
| 783 | var lang string |
| 784 | if langCount := len(order.Langs); langCount == 1 { |
| 785 | lang = order.Langs[0] |
| 786 | } else if langCount > 1 { |
| 787 | return nil, errors.Errorf("Sorting on multiple language is not supported.") |
| 788 | } |
| 789 | |
| 790 | // nullsList is the list of UIDs for which value doesn't exist. |
| 791 | var nullsList []uint64 |
| 792 | var nullVals [][]types.Val |
| 793 | for i := range lenList { |
| 794 | select { |
| 795 | case <-ctx.Done(): |
| 796 | return multiSortVals, ctx.Err() |
| 797 | default: |
| 798 | uid := ul.Uids[i] |
| 799 | val, err := fetchValue(uid, order.Attr, order.Langs, typ, ts.ReadTs) |
| 800 | if err != nil { |
| 801 | // Value couldn't be found or couldn't be converted to the sort type. |
| 802 | // It will be appended to the end of the result based on the pagination. |
| 803 | val.Value = nil |
| 804 | nullsList = append(nullsList, uid) |
| 805 | nullVals = append(nullVals, []types.Val{val}) |
| 806 | continue |
| 807 | } |
| 808 | uids = append(uids, uid) |
| 809 | values = append(values, []types.Val{val}) |
| 810 | } |
| 811 | } |
| 812 | err := types.Sort(values, &uids, []bool{order.Desc}, lang) |
| 813 | ul.Uids = append(uids, nullsList...) |
| 814 | values = append(values, nullVals...) |
| 815 | if len(ts.Order) > 1 { |
| 816 | for _, v := range values { |
| 817 | multiSortVals = append(multiSortVals, v[0]) |
| 818 | } |
| 819 | } |
| 820 | return multiSortVals, err |
| 821 | } |
| 822 | |
| 823 | // fetchValue gets the value for a given UID. |
| 824 | func fetchValue(uid uint64, attr string, langs []string, scalar types.TypeID, |
no test coverage detected