1
0

Implement package cache API

This commit is contained in:
dump_stack() 2023-05-11 18:45:44 +00:00
parent ac74e450c3
commit d04a9de932
Signed by: dump_stack
GPG Key ID: BE44DA8C062D87DC
2 changed files with 98 additions and 0 deletions

30
distro/debian/cache.go Normal file
View File

@ -0,0 +1,30 @@
package debian
import (
"github.com/rapidloop/skv"
"code.dumpstack.io/tools/out-of-tree/distro/debian/snapshot"
)
type Cache struct {
store *skv.KVStore
}
func NewCache(path string) (c *Cache, err error) {
c = &Cache{}
c.store, err = skv.Open(path)
return
}
func (c Cache) Put(p snapshot.Package) error {
return c.store.Put(p.Version, p)
}
func (c Cache) Get(version string) (p snapshot.Package, err error) {
err = c.store.Get(version, &p)
return
}
func (c Cache) Close() error {
return c.store.Close()
}

View File

@ -0,0 +1,68 @@
package debian
import (
"os"
"path/filepath"
"testing"
"github.com/rapidloop/skv"
"code.dumpstack.io/tools/out-of-tree/distro/debian/snapshot"
)
func TestCache(t *testing.T) {
dir, err := os.MkdirTemp("", "out-of-tree_cache_test_")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
path := filepath.Join(dir, "debian.cache")
c, err := NewCache(path)
if err != nil {
t.Fatal(err)
}
packages, err := snapshot.Packages("linux", "4.17.14-1", "amd64",
`^linux-(image|headers)-[0-9\.\-]*-(amd64|amd64-unsigned)$`)
if err != nil {
t.Fatal(err)
}
err = c.Put(packages[0])
if err != nil {
t.Fatal(err)
}
p, err := c.Get(packages[0].Version)
if err != nil {
t.Fatal(err)
}
if p.Deb.Hash != packages[0].Deb.Hash {
t.Fatalf("mismatch")
}
c.Close()
c, err = NewCache(path)
if err != nil {
t.Fatal(err)
}
defer c.Close()
p, err = c.Get(packages[0].Version)
if err != nil {
t.Fatal(err)
}
if p.Deb.Hash != packages[0].Deb.Hash {
t.Fatalf("mismatch")
}
p, err = c.Get("key not exist")
if err == nil || err != skv.ErrNotFound {
t.Fatal(err)
}
}