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
import (
"errors"
"sync"
"github.com/rapidloop/skv"
@ -21,12 +22,18 @@ func NewCache(path string) (c *Cache, err error) {
return
}
func (c Cache) Put(p DebianKernel) error {
return c.store.Put(p.Version.Package, p)
func (c Cache) Put(p []DebianKernel) error {
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)
if len(p) == 0 {
err = skv.ErrNotFound
}
return
}

View File

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

View File

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