StableSort the fastmap so we don't have to do it in the handler
This commit is contained in:
parent
c3f471817a
commit
12ad5f3d69
@ -4,6 +4,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"slices"
|
||||||
|
|
||||||
"github.com/goccy/go-json"
|
"github.com/goccy/go-json"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -87,6 +89,19 @@ func (m *Fastmap[K, V]) Iter(fn func(key K, value V) bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Fastmap[K, V]) StableSortByKey() {
|
||||||
|
slices.SortStableFunc(m.store, func(a, b fastmapValue[K, V]) int {
|
||||||
|
aKey := fmt.Sprint(a.Key)
|
||||||
|
bKey := fmt.Sprint(b.Key)
|
||||||
|
return strings.Compare(aKey, bKey)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Update the index map after sorting
|
||||||
|
for i, v := range m.store {
|
||||||
|
m.idx[v.Key] = i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (m *Fastmap[K, V]) MarshalText() ([]byte, error) {
|
func (m *Fastmap[K, V]) MarshalText() ([]byte, error) {
|
||||||
var builder strings.Builder
|
var builder strings.Builder
|
||||||
for _, v := range m.store {
|
for _, v := range m.store {
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
"brunel/helpers"
|
"brunel/helpers"
|
||||||
"brunel/packages"
|
"brunel/packages"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
@ -36,30 +35,9 @@ func Packages(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
packs := packages.GetPackages()
|
packs := packages.GetPackages()
|
||||||
|
|
||||||
// Convert map to slice for sorting
|
|
||||||
packSlice := make([]struct {
|
|
||||||
Key string
|
|
||||||
Value domain.SourcePackage
|
|
||||||
}, 0, packs.Len())
|
|
||||||
|
|
||||||
packs.Iter(func(k string, v domain.SourcePackage) bool {
|
|
||||||
packSlice = append(packSlice, struct {
|
|
||||||
Key string
|
|
||||||
Value domain.SourcePackage
|
|
||||||
}{k, v})
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
|
|
||||||
// Stable sort the slice
|
|
||||||
sort.SliceStable(packSlice, func(i, j int) bool {
|
|
||||||
return packSlice[i].Key < packSlice[j].Key
|
|
||||||
})
|
|
||||||
|
|
||||||
finalReturn := fastmap.New[string, domain.SourcePackage]()
|
finalReturn := fastmap.New[string, domain.SourcePackage]()
|
||||||
|
|
||||||
for _, item := range packSlice {
|
packs.Iter(func(k string, source domain.SourcePackage) bool {
|
||||||
k, source := item.Key, item.Value
|
|
||||||
|
|
||||||
matchesFilter := filter == ""
|
matchesFilter := filter == ""
|
||||||
matchesSearch := search == "" || strings.Contains(strings.ToLower(k), search)
|
matchesSearch := search == "" || strings.Contains(strings.ToLower(k), search)
|
||||||
|
|
||||||
@ -70,13 +48,14 @@ func Packages(c *fiber.Ctx) error {
|
|||||||
if !matchesSearch && strings.Contains(strings.ToLower(key), search) {
|
if !matchesSearch && strings.Contains(strings.ToLower(key), search) {
|
||||||
matchesSearch = true
|
matchesSearch = true
|
||||||
}
|
}
|
||||||
return !(matchesFilter && matchesSearch) // stop iterating if we've found matches for both
|
return !(matchesFilter && matchesSearch)
|
||||||
})
|
})
|
||||||
|
|
||||||
if matchesFilter && matchesSearch {
|
if matchesFilter && matchesSearch {
|
||||||
finalReturn.Set(k, source)
|
finalReturn.Set(k, source)
|
||||||
}
|
}
|
||||||
}
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
result := finalReturn.GetPage(adjustedPageNum, pageSize)
|
result := finalReturn.GetPage(adjustedPageNum, pageSize)
|
||||||
|
|
||||||
|
@ -44,6 +44,7 @@ func ProcessPackages() error {
|
|||||||
currentPackagesFastMap.Set(k, v)
|
currentPackagesFastMap.Set(k, v)
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
currentPackagesFastMap.StableSortByKey()
|
||||||
|
|
||||||
LastUpdateTime = time.Now()
|
LastUpdateTime = time.Now()
|
||||||
helpers.ReloadCache()
|
helpers.ReloadCache()
|
||||||
@ -129,6 +130,7 @@ func LoadFromDb() error {
|
|||||||
for _, pkg := range packages {
|
for _, pkg := range packages {
|
||||||
currentPackagesFastMap.Set(pkg.Name, pkg)
|
currentPackagesFastMap.Set(pkg.Name, pkg)
|
||||||
}
|
}
|
||||||
|
currentPackagesFastMap.StableSortByKey()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user