Skip to content

Commit

Permalink
fixed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Harshil Goel committed Sep 14, 2023
1 parent 14957d1 commit 4af29dd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 33 deletions.
5 changes: 3 additions & 2 deletions types/select.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 Dgraph Labs, Inc. and Contributors
* Copyright 2023 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@
package types

// Below functions are taken from go's sort library zsortinterface.go
// https://go.dev/src/sort/zsortinterface.go
func insertionSort(data byValue, a, b int) {
for i := a + 1; i < b; i++ {
for j := i; j > a && data.Less(j, j-1); j-- {
Expand Down Expand Up @@ -85,7 +86,7 @@ func partition(data byValue, a, b, pivot int) int {
return partitionIndex
}

func QuickSelect(data byValue, low, high, k int) {
func quickSelect(data byValue, low, high, k int) {
var pivotIndex int

for {
Expand Down
22 changes: 6 additions & 16 deletions types/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,11 @@ func (s sortBase) Swap(i, j int) {
}
}

type byValue struct {
sortBase
}
type byValue struct{ sortBase }

func (s byValue) IsNil(i int) bool {
func (s byValue) isNil(i int) bool {
first := s.values[i]
if len(first) == 0 {
return true
}
if first[0].Value == nil {
return true
}
return false
return len(first) == 0 || first[0].Value == nil
}

// Less compares two elements
Expand All @@ -81,7 +73,6 @@ func (s byValue) Less(i, j int) bool {
}

if second[vidx].Value == nil {
//fmt.Println("second val true", vidx, i, j, first[vidx].Value)
return true
}

Expand Down Expand Up @@ -110,8 +101,7 @@ func IsSortable(tid TypeID) bool {
}
}

// SortWithFacet sorts the given array in-place and considers the given facets to calculate
// the proper ordering.
// SortTopN finds and places the first n elements in 0-N
func SortTopN(v [][]Val, ul *[]uint64, desc []bool, lang string, n int) error {
if len(v) == 0 || len(v[0]) == 0 {
return nil
Expand All @@ -137,7 +127,7 @@ func SortTopN(v [][]Val, ul *[]uint64, desc []bool, lang string, n int) error {

nul := 0
for i := 0; i < len(*ul); i++ {
if toBeSorted.IsNil(i) {
if toBeSorted.isNil(i) {
continue
}
if i != nul {
Expand All @@ -149,7 +139,7 @@ func SortTopN(v [][]Val, ul *[]uint64, desc []bool, lang string, n int) error {
if nul > n {
b1 := sortBase{v[:nul], desc, ul, nil, cl}
toBeSorted1 := byValue{b1}
QuickSelect(toBeSorted1, 0, nul-1, n)
quickSelect(toBeSorted1, 0, nul-1, n)
}
toBeSorted.values = toBeSorted.values[:n]
sort.Sort(toBeSorted)
Expand Down
19 changes: 5 additions & 14 deletions types/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,10 @@ func getUIDList(n int) *pb.List {
return &pb.List{Uids: data}
}

const charset = "abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
const charset = "abcdefghijklmnopqrstuvwxyz"

func StringWithCharset(length int) string {
var seededRand *rand.Rand = rand.New(
rand.NewSource(time.Now().UnixNano()))
var seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
Expand Down Expand Up @@ -90,8 +88,7 @@ func TestQuickSelect(t *testing.T) {

ul := getUIDList(n)
list := getList()
err := SortTopN(list, &ul.Uids, []bool{false}, "", k)
require.NoError(t, err)
require.NoError(t, SortTopN(list, &ul.Uids, []bool{false}, "", k))

for i := 0; i < k; i++ {
for j := k; j < n; j++ {
Expand Down Expand Up @@ -123,12 +120,9 @@ func BenchmarkSortQuickSort(b *testing.B) {
for i := 0; i < b.N; i++ {
ul := getUIDList(n)
list := getList()
b.StartTimer()
k1 := time.Now()
b.ResetTimer()
err := Sort(list, &ul.Uids, []bool{false}, "")
b.StopTimer()
k2 := time.Since(k1)
b.ReportMetric(k2.Seconds(), "Time")
require.NoError(b, err)
}
})
Expand All @@ -138,12 +132,9 @@ func BenchmarkSortQuickSort(b *testing.B) {
for i := 0; i < b.N; i++ {
ul := getUIDList(n)
list := getList()
b.StartTimer()
k1 := time.Now()
b.ResetTimer()
err := SortTopN(list, &ul.Uids, []bool{false}, "", j)
k2 := time.Since(k1)
b.StopTimer()
b.ReportMetric(k2.Seconds(), "Time")
require.NoError(b, err)
}
})
Expand Down
1 change: 0 additions & 1 deletion worker/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,6 @@ func multiSort(ctx context.Context, r *sortresult, ts *pb.SortMessage) error {
}

start, end := x.PageRange(int(ts.Count), int(r.multiSortOffsets[i]), len(ul.Uids))
// TODO(harshil.goel): can be improved for other cases too
if end < len(ul.Uids)/2 {
//nolint:gosec
if err := types.SortTopN(vals, &ul.Uids, desc, "", end); err != nil {
Expand Down

0 comments on commit 4af29dd

Please sign in to comment.