1
0

feat: modify cache api to store more than one kernel for version

This commit is contained in:
dump_stack() 2023-05-15 18:37:09 +00:00
parent 8949b53ccc
commit 8f1a2afc53
Signed by: dump_stack
GPG Key ID: BE44DA8C062D87DC
3 changed files with 28 additions and 14 deletions

View File

@ -1,6 +1,7 @@
package debian package debian
import ( import (
"errors"
"sync" "sync"
"github.com/rapidloop/skv" "github.com/rapidloop/skv"
@ -21,12 +22,18 @@ func NewCache(path string) (c *Cache, err error) {
return return
} }
func (c Cache) Put(p DebianKernel) error { func (c Cache) Put(p []DebianKernel) error {
return c.store.Put(p.Version.Package, p) if len(p) == 0 {
return errors.New("empty slice")
}
return c.store.Put(p[0].Version.Package, p)
} }
func (c Cache) Get(version string) (p DebianKernel, err error) { func (c Cache) Get(version string) (p []DebianKernel, err error) {
err = c.store.Get(version, &p) err = c.store.Get(version, &p)
if len(p) == 0 {
err = skv.ErrNotFound
}
return return
} }

View File

@ -34,15 +34,16 @@ func TestCache(t *testing.T) {
Image: image, Image: image,
} }
err = c.Put(dk) err = c.Put([]DebianKernel{dk})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
dk2, err := c.Get(version) dk2s, err := c.Get(version)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
dk2 := dk2s[0]
if dk.Image.Deb.Hash != dk2.Image.Deb.Hash { if dk.Image.Deb.Hash != dk2.Image.Deb.Hash {
t.Fatalf("mismatch") t.Fatalf("mismatch")
@ -56,10 +57,11 @@ func TestCache(t *testing.T) {
} }
defer c.Close() defer c.Close()
dk3, err := c.Get(version) dk3s, err := c.Get(version)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
dk3 := dk3s[0]
if dk.Image.Deb.Hash != dk3.Image.Deb.Hash { if dk.Image.Deb.Hash != dk3.Image.Deb.Hash {
t.Fatalf("mismatch") t.Fatalf("mismatch")

View File

@ -137,11 +137,12 @@ func GetCachedKernel(deb string) (dk DebianKernel, err error) {
} }
for _, version := range versions { for _, version := range versions {
var tmpdk DebianKernel var tmpdks []DebianKernel
tmpdk, err = c.Get(version) tmpdks, err = c.Get(version)
if err != nil { if err != nil {
continue continue
} }
tmpdk := tmpdks[0]
if deb == tmpdk.Image.Deb.Name { if deb == tmpdk.Image.Deb.Name {
dk = tmpdk dk = tmpdk
@ -225,12 +226,16 @@ func findKbuild(versions []string, kpkgver string) (
func getKernelsByVersion(slog zerolog.Logger, c *Cache, toolsVersions []string, func getKernelsByVersion(slog zerolog.Logger, c *Cache, toolsVersions []string,
version string) (kernels []DebianKernel) { version string) (kernels []DebianKernel) {
dk, err := c.Get(version) var dk DebianKernel
if err == nil && !dk.Internal.Invalid { dks, err := c.Get(version)
if err == nil {
dk = dks[0]
if !dk.Internal.Invalid {
slog.Trace().Msgf("found in cache") slog.Trace().Msgf("found in cache")
kernels = append(kernels, dk) kernels = append(kernels, dk)
return return
} }
}
if dk.Internal.Invalid { if dk.Internal.Invalid {
refetch := dk.Internal.LastFetch.AddDate(0, 0, RefetchDays) refetch := dk.Internal.LastFetch.AddDate(0, 0, RefetchDays)
@ -280,7 +285,7 @@ func getKernelsByVersion(slog zerolog.Logger, c *Cache, toolsVersions []string,
kernels = append(kernels, dk) kernels = append(kernels, dk)
} }
err = c.Put(dk) err = c.Put([]DebianKernel{dk})
if err != nil { if err != nil {
slog.Error().Err(err).Msg("put to cache") slog.Error().Err(err).Msg("put to cache")
return return