Skip to content

Commit

Permalink
Remove sortpkg
Browse files Browse the repository at this point in the history
  • Loading branch information
jarrel-b committed Dec 12, 2023
1 parent cbf949c commit dbea1b5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 39 deletions.
59 changes: 37 additions & 22 deletions publicapi/feed.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package publicapi

import (
"container/heap"
heappkg "container/heap"
"context"
"database/sql"
"fmt"
"math"
sortpkg "sort"
"sort"
"sync"
"time"

Expand All @@ -33,7 +33,6 @@ import (
"github.com/mikeydub/go-gallery/service/task"
"github.com/mikeydub/go-gallery/util"
"github.com/mikeydub/go-gallery/util/retry"
"github.com/mikeydub/go-gallery/util/sort"
)

const trendingFeedCacheKey = "trending:feedEvents:all"
Expand Down Expand Up @@ -989,7 +988,7 @@ func loadFeedEntities(ctx context.Context, d *dataloader.Loaders, typs []persist
<-postsDone

// Sort in descending order
sortpkg.Slice(errored, func(i, j int) bool { return errored[i] > errored[j] })
sort.Slice(errored, func(i, j int) bool { return errored[i] > errored[j] })

// Filter out errored entities
for _, pos := range errored {
Expand All @@ -1007,21 +1006,8 @@ func loadFeedEntities(ctx context.Context, d *dataloader.Loaders, typs []persist
return entities, nil
}

type entityScore struct {
v db.FeedEntityScore
s float64
}

func (n entityScore) Less(a any) bool {
other, ok := a.(entityScore)
if !ok {
return false
}
return n.s < other.s
}

func (api FeedAPI) scoreFeedEntities(ctx context.Context, n int, trendData []db.FeedEntityScore, scoreF func(db.FeedEntityScore) float64) []db.FeedEntityScore {
h := make(sort.Heap[entityScore], 0)
h := make(heap[entityScore], 0)

var wg sync.WaitGroup

Expand All @@ -1043,13 +1029,13 @@ func (api FeedAPI) scoreFeedEntities(ctx context.Context, n int, trendData []db.
for _, node := range scores {
// Add first n items in the heap
if h.Len() < n {
heap.Push(&h, node)
heappkg.Push(&h, node)
continue
}
// If the score is greater than the smallest score in the heap, replace it
if node.s > h[0].s {
heap.Pop(&h)
heap.Push(&h, node)
heappkg.Pop(&h)
heappkg.Push(&h, node)
}
}

Expand All @@ -1059,7 +1045,7 @@ func (api FeedAPI) scoreFeedEntities(ctx context.Context, n int, trendData []db.
// such that the highest score is first
i := h.Len() - 1
for h.Len() > 0 {
node := heap.Pop(&h)
node := heappkg.Pop(&h)
scoredEntities[i] = node.(entityScore).v
i--
}
Expand Down Expand Up @@ -1098,6 +1084,35 @@ func engagementFactor(interactions float64) float64 {
return math.Log2(2 + interactions)
}

type entityScore struct {
v db.FeedEntityScore
s float64
}

func (n entityScore) Less(a any) bool {
other, ok := a.(entityScore)
if !ok {
return false
}
return n.s < other.s
}

type lt interface{ Less(j any) bool }
type heap[T lt] []T

func (h heap[T]) Len() int { return len(h) }
func (h heap[T]) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *heap[T]) Push(s any) { *h = append(*h, s.(T)) }
func (h heap[T]) Less(i, j int) bool { return h[i].Less(h[j]) }

func (h *heap[T]) Pop() any {
old := *h
n := len(old)
item := old[n-1]
*h = old[:n-1]
return item
}

type feedPaginator struct {
QueryFunc func(params positionPagingParams) ([]any, error)
CursorFunc func(node any) (pos int64, feedEntityType []persist.FeedEntityType, ids []persist.DBID, err error)
Expand Down
17 changes: 0 additions & 17 deletions util/sort/sort.go

This file was deleted.

0 comments on commit dbea1b5

Please sign in to comment.