Make boltstore imlement binarystore

This commit is contained in:
Torjus Håkestad 2021-12-08 05:55:30 +01:00
parent ee7c48dad7
commit 4d719daf39
2 changed files with 45 additions and 0 deletions

View File

@ -22,6 +22,7 @@ var bktKeyCerts = []byte("certs")
var bktKeyKeys = []byte("keys")
var bktKeyUsers = []byte("users")
var bktKeyRevoked = []byte("revoked")
var bktKeyBinary = []byte("binaries")
func NewBoltStore(path string) (*BoltStore, error) {
s := &BoltStore{}
@ -46,6 +47,9 @@ func NewBoltStore(path string) (*BoltStore, error) {
if _, err := t.CreateBucketIfNotExists(bktKeyRevoked); err != nil {
return err
}
if _, err := t.CreateBucketIfNotExists(bktKeyBinary); err != nil {
return err
}
return nil
})
if err != nil {
@ -298,3 +302,35 @@ func (s *BoltStore) GetUserByUsername(username string) (*pb.User, error) {
}
return nil, ErrNoSuchItem
}
// BinaryStore
func binaryKey(version string, os string, arch string) []byte {
return []byte(fmt.Sprintf("%s,%s,%s", version, os, arch))
}
func (s *BoltStore) StoreBinary(release *pb.Binary) error {
return s.db.Update(func(tx *bolt.Tx) error {
bkt := tx.Bucket(bktKeyBinary)
key := binaryKey(release.Version, release.Os, release.Arch)
return bkt.Put(key, release.Data)
})
}
func (s *BoltStore) GetBinary(version string, os string, arch string) (*pb.Binary, error) {
binary := &pb.Binary{}
err := s.db.View(func(tx *bolt.Tx) error {
bkt := tx.Bucket(bktKeyBinary)
key := binaryKey(version, os, arch)
binary.Data = bkt.Get(key)
return nil
})
if err != nil {
return nil, err
}
if binary.Data == nil {
return nil, ErrNoSuchItem
}
return binary, nil
}

View File

@ -37,3 +37,12 @@ func TestBoltUserStore(t *testing.T) {
}
doUserStoreTests(s, t)
}
func TestBoltBinaryStore(t *testing.T) {
path := filepath.Join(t.TempDir(), "boltstore.db")
s, err := store.NewBoltStore(path)
defer s.Close()
if err != nil {
t.Fatalf("Error opening store: %s", err)
}
doBinaryStoreTests(s, t)
}