Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export TopK fields so we can index them outside of this package #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions top_k.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ type TopK struct {

// TopKElement is the struct used to return the results of the TopK
type TopKElement struct {
element string
count uint64
Element string
Count uint64
}

// NewTopK creates new TopK
Expand Down Expand Up @@ -119,16 +119,16 @@ func (t *TopK) Values() []TopKElement {
results = append(results, TopKElement{t.heap[i].value, t.heap[i].frequency})
}
sort.Slice(results, func(i, j int) bool {
if results[i].count == results[j].count {
c := strings.Compare(results[i].element, results[j].element)
if results[i].Count == results[j].Count {
c := strings.Compare(results[i].Element, results[j].Element)
if c == -1 {
return true
}
if c == 1 {
return false
}
}
return results[i].count > results[j].count
return results[i].Count > results[j].Count
})
return results
}
Expand Down
6 changes: 3 additions & 3 deletions top_k_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,16 @@ func (t *TopKRedis) Values() ([]TopKElement, error) {
results = append(results, TopKElement{elements[i].Member.(string), uint64(elements[i].Score)})
}
sort.Slice(results, func(i, j int) bool {
if results[i].count == results[j].count {
c := strings.Compare(results[i].element, results[j].element)
if results[i].Count == results[j].Count {
c := strings.Compare(results[i].Element, results[j].Element)
if c == -1 {
return true
}
if c == 1 {
return false
}
}
return results[i].count > results[j].count
return results[i].Count > results[j].Count
})
return results, nil
}
Expand Down
16 changes: 8 additions & 8 deletions top_k_redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func TestTopKRedisBasic(t *testing.T) {
}

for i := range val1 {
if val1[i].count != uint64(frequencyMap[val1[i].element]) {
t.Errorf("frequency doesn't match for %s. Instead found %d and %d", val1[i].element, val1[i].count, frequencyMap[val1[i].element])
if val1[i].Count != uint64(frequencyMap[val1[i].Element]) {
t.Errorf("frequency doesn't match for %s. Instead found %d and %d", val1[i].Element, val1[i].Count, frequencyMap[val1[i].Element])
}
}
}
Expand All @@ -57,11 +57,11 @@ func TestTopRedisKDifferentKs(t *testing.T) {
val, _ := topk.Values()

for i := range expectedTopElements {
if strings.Compare(expectedTopElements[i], val[i].element) != 0 {
if strings.Compare(expectedTopElements[i], val[i].Element) != 0 {
t.Errorf("values at position %d don't match", i)
}
if val[i].count != uint64(frequencyMap[val[i].element]) {
t.Errorf("frequency doesn't match for %s. Instead found %d and %d", val[i].element, val[i].count, frequencyMap[val[i].element])
if val[i].Count != uint64(frequencyMap[val[i].Element]) {
t.Errorf("frequency doesn't match for %s. Instead found %d and %d", val[i].Element, val[i].Count, frequencyMap[val[i].Element])
}
}

Expand All @@ -73,11 +73,11 @@ func TestTopRedisKDifferentKs(t *testing.T) {
val, _ = topk.Values()

for i := 0; i < 6; i++ {
if strings.Compare(expectedTopElements[i], val[i].element) != 0 {
if strings.Compare(expectedTopElements[i], val[i].Element) != 0 {
t.Errorf("values at position %d don't match", i)
}
if val[i].count != uint64(frequencyMap[val[i].element]) {
t.Errorf("frequency doesn't match for %s. Instead found %d and %d", val[i].element, val[i].count, frequencyMap[val[i].element])
if val[i].Count != uint64(frequencyMap[val[i].Element]) {
t.Errorf("frequency doesn't match for %s. Instead found %d and %d", val[i].Element, val[i].Count, frequencyMap[val[i].Element])
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions top_k_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func TestTopKBasic(t *testing.T) {
}

for i := range val1 {
if val1[i].count != uint64(frequencyMap[val1[i].element]) {
t.Errorf("frequency doesn't match for %s. Instead found %d and %d", val1[i].element, val1[i].count, frequencyMap[val1[i].element])
if val1[i].Count != uint64(frequencyMap[val1[i].Element]) {
t.Errorf("frequency doesn't match for %s. Instead found %d and %d", val1[i].Element, val1[i].Count, frequencyMap[val1[i].Element])
}
}
}
Expand All @@ -104,11 +104,11 @@ func TestTopKDifferentKs(t *testing.T) {
val := topk.Values()

for i := range expectedTopElements {
if strings.Compare(expectedTopElements[i], val[i].element) != 0 {
if strings.Compare(expectedTopElements[i], val[i].Element) != 0 {
t.Errorf("values at position %d don't match", i)
}
if val[i].count != uint64(frequencyMap[val[i].element]) {
t.Errorf("frequency doesn't match for %s. Instead found %d and %d", val[i].element, val[i].count, frequencyMap[val[i].element])
if val[i].Count != uint64(frequencyMap[val[i].Element]) {
t.Errorf("frequency doesn't match for %s. Instead found %d and %d", val[i].Element, val[i].Count, frequencyMap[val[i].Element])
}
}

Expand All @@ -120,11 +120,11 @@ func TestTopKDifferentKs(t *testing.T) {
val = topk.Values()

for i := 0; i < 3; i++ {
if strings.Compare(expectedTopElements[i], val[i].element) != 0 {
if strings.Compare(expectedTopElements[i], val[i].Element) != 0 {
t.Errorf("values at position %d don't match", i)
}
if val[i].count != uint64(frequencyMap[val[i].element]) {
t.Errorf("frequency doesn't match for %s. Instead found %d and %d", val[i].element, val[i].count, frequencyMap[val[i].element])
if val[i].Count != uint64(frequencyMap[val[i].Element]) {
t.Errorf("frequency doesn't match for %s. Instead found %d and %d", val[i].Element, val[i].Count, frequencyMap[val[i].Element])
}
}
}
Expand Down