diff --git a/query/query.go b/query/query.go index 56905a5cee6..f974fa6166b 100644 --- a/query/query.go +++ b/query/query.go @@ -2339,8 +2339,8 @@ func (sg *SubGraph) sortAndPaginateUsingFacet(ctx context.Context) error { if len(values) == 0 { continue } - if err := types.SortWithFacet(values, &pb.List{Uids: uids}, - facetList, []bool{sg.Params.FacetOrderDesc}, ""); err != nil { + if err := types.SortWithFacet(values, &uids, facetList, + []bool{sg.Params.FacetOrderDesc}, ""); err != nil { return err } sg.uidMatrix[i].Uids = uids @@ -2387,8 +2387,7 @@ func (sg *SubGraph) sortAndPaginateUsingVar(ctx context.Context) error { if len(values) == 0 { continue } - if err := types.Sort(values, &pb.List{Uids: uids}, - []bool{sg.Params.Order[0].Desc}, ""); err != nil { + if err := types.Sort(values, &uids, []bool{sg.Params.Order[0].Desc}, ""); err != nil { return err } sg.uidMatrix[i].Uids = uids diff --git a/types/sort.go b/types/sort.go index f98c39b5be0..0b96c1a53ea 100644 --- a/types/sort.go +++ b/types/sort.go @@ -30,19 +30,20 @@ import ( type sortBase struct { values [][]Val // Each uid could have multiple values which we need to sort it by. desc []bool // Sort orders for different values. - ul *pb.List + ul *[]uint64 o []*pb.Facets cl *collate.Collator // Compares Unicode strings according to the given collation order. } // Len returns size of vector. +// skipcq: CRT-P0003 func (s sortBase) Len() int { return len(s.values) } // Swap swaps two elements. +// skipcq: CRT-P0003 func (s sortBase) Swap(i, j int) { s.values[i], s.values[j] = s.values[j], s.values[i] - data := s.ul.Uids - data[i], data[j] = data[j], data[i] + (*s.ul)[i], (*s.ul)[j] = (*s.ul)[j], (*s.ul)[i] if s.o != nil { s.o[i], s.o[j] = s.o[j], s.o[i] } @@ -51,6 +52,7 @@ func (s sortBase) Swap(i, j int) { type byValue struct{ sortBase } // Less compares two elements +// skipcq: CRT-P0003 func (s byValue) Less(i, j int) bool { first, second := s.values[i], s.values[j] if len(first) == 0 || len(second) == 0 { @@ -84,7 +86,7 @@ func (s byValue) Less(i, j int) bool { // SortWithFacet sorts the given array in-place and considers the given facets to calculate // the proper ordering. -func SortWithFacet(v [][]Val, ul *pb.List, l []*pb.Facets, desc []bool, lang string) error { +func SortWithFacet(v [][]Val, ul *[]uint64, l []*pb.Facets, desc []bool, lang string) error { if len(v) == 0 || len(v[0]) == 0 { return nil } @@ -113,7 +115,7 @@ func SortWithFacet(v [][]Val, ul *pb.List, l []*pb.Facets, desc []bool, lang str } // Sort sorts the given array in-place. -func Sort(v [][]Val, ul *pb.List, desc []bool, lang string) error { +func Sort(v [][]Val, ul *[]uint64, desc []bool, lang string) error { return SortWithFacet(v, ul, nil, desc, lang) } diff --git a/types/sort_test.go b/types/sort_test.go index 6f2594a07ea..1d70e111866 100644 --- a/types/sort_test.go +++ b/types/sort_test.go @@ -55,7 +55,7 @@ func getUIDList(n int) *pb.List { func TestSortStrings(t *testing.T) { list := getInput(t, StringID, []string{"bb", "aaa", "aa", "bab"}) ul := getUIDList(4) - require.NoError(t, Sort(list, ul, []bool{false}, "")) + require.NoError(t, Sort(list, &ul.Uids, []bool{false}, "")) require.EqualValues(t, []uint64{300, 200, 400, 100}, ul.Uids) require.EqualValues(t, []string{"aa", "aaa", "bab", "bb"}, toString(t, list, StringID)) @@ -66,7 +66,7 @@ func TestSortLanguage(t *testing.T) { list := getInput(t, StringID, []string{"öffnen", "zumachen"}) ul := getUIDList(2) - require.NoError(t, Sort(list, ul, []bool{false}, "de")) + require.NoError(t, Sort(list, &ul.Uids, []bool{false}, "de")) require.EqualValues(t, []uint64{100, 200}, ul.Uids) require.EqualValues(t, []string{"öffnen", "zumachen"}, toString(t, list, StringID)) @@ -74,7 +74,7 @@ func TestSortLanguage(t *testing.T) { // Sorting strings of swedish language. list = getInput(t, StringID, []string{"öppna", "zon"}) - require.NoError(t, Sort(list, ul, []bool{false}, "sv")) + require.NoError(t, Sort(list, &ul.Uids, []bool{false}, "sv")) require.EqualValues(t, []uint64{200, 100}, ul.Uids) require.EqualValues(t, []string{"zon", "öppna"}, toString(t, list, StringID)) @@ -83,7 +83,7 @@ func TestSortLanguage(t *testing.T) { func TestSortInts(t *testing.T) { list := getInput(t, IntID, []string{"22", "111", "11", "212"}) ul := getUIDList(4) - require.NoError(t, Sort(list, ul, []bool{false}, "")) + require.NoError(t, Sort(list, &ul.Uids, []bool{false}, "")) require.EqualValues(t, []uint64{300, 100, 200, 400}, ul.Uids) require.EqualValues(t, []string{"11", "22", "111", "212"}, toString(t, list, IntID)) @@ -92,7 +92,7 @@ func TestSortInts(t *testing.T) { func TestSortFloats(t *testing.T) { list := getInput(t, FloatID, []string{"22.2", "11.2", "11.5", "2.12"}) ul := getUIDList(4) - require.NoError(t, Sort(list, ul, []bool{false}, "")) + require.NoError(t, Sort(list, &ul.Uids, []bool{false}, "")) require.EqualValues(t, []uint64{400, 200, 300, 100}, ul.Uids) require.EqualValues(t, []string{"2.12", "11.2", "11.5", "22.2"}, @@ -102,7 +102,7 @@ func TestSortFloats(t *testing.T) { func TestSortFloatsDesc(t *testing.T) { list := getInput(t, FloatID, []string{"22.2", "11.2", "11.5", "2.12"}) ul := getUIDList(4) - require.NoError(t, Sort(list, ul, []bool{true}, "")) + require.NoError(t, Sort(list, &ul.Uids, []bool{true}, "")) require.EqualValues(t, []uint64{100, 300, 200, 400}, ul.Uids) require.EqualValues(t, []string{"22.2", "11.5", "11.2", "2.12"}, @@ -118,7 +118,7 @@ func TestSortDateTimes(t *testing.T) { } list := getInput(t, DateTimeID, in) ul := getUIDList(4) - require.NoError(t, Sort(list, ul, []bool{false}, "")) + require.NoError(t, Sort(list, &ul.Uids, []bool{false}, "")) require.EqualValues(t, []uint64{400, 200, 300, 100}, ul.Uids) require.EqualValues(t, []string{"2006-01-02T15:04:01Z", "2006-01-02T15:04:05Z", @@ -133,7 +133,7 @@ func TestSortIntAndFloat(t *testing.T) { {{Tid: IntID, Value: int64(100)}}, } ul := getUIDList(3) - require.NoError(t, Sort(list, ul, []bool{false}, "")) + require.NoError(t, Sort(list, &ul.Uids, []bool{false}, "")) require.EqualValues(t, []uint64{200, 100, 300}, ul.Uids) require.EqualValues(t, []string{"21.5", "55", "100"}, @@ -188,7 +188,7 @@ func TestSortMismatchedTypes(t *testing.T) { {{Tid: FloatID, Value: 33.33}}, } ul := getUIDList(7) - require.NoError(t, Sort(list, ul, []bool{false}, "")) + require.NoError(t, Sort(list, &ul.Uids, []bool{false}, "")) // Don't care about relative ordering between types. However, like types // should be sorted with each other. diff --git a/worker/sort.go b/worker/sort.go index f6943a9f7ba..88582588931 100644 --- a/worker/sort.go +++ b/worker/sort.go @@ -390,7 +390,7 @@ func multiSort(ctx context.Context, r *sortresult, ts *pb.SortMessage) error { x.AssertTrue(idx >= 0) vals[j] = sortVals[idx] } - if err := types.Sort(vals, ul, desc, ""); err != nil { + if err := types.Sort(vals, &ul.Uids, desc, ""); err != nil { return err } // Paginate @@ -711,7 +711,7 @@ func sortByValue(ctx context.Context, ts *pb.SortMessage, ul *pb.List, values = append(values, []types.Val{val}) } } - err := types.Sort(values, &pb.List{Uids: uids}, []bool{order.Desc}, lang) + err := types.Sort(values, &uids, []bool{order.Desc}, lang) ul.Uids = uids if len(ts.Order) > 1 { for _, v := range values {