From 4d719daf3982512b61878e0f4de3c81f32c6febc Mon Sep 17 00:00:00 2001 From: = Date: Wed, 8 Dec 2021 05:55:30 +0100 Subject: [PATCH] Make boltstore imlement binarystore --- store/bolt.go | 36 ++++++++++++++++++++++++++++++++++++ store/bolt_test.go | 9 +++++++++ 2 files changed, 45 insertions(+) diff --git a/store/bolt.go b/store/bolt.go index fe44a71..7a43956 100644 --- a/store/bolt.go +++ b/store/bolt.go @@ -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 +} diff --git a/store/bolt_test.go b/store/bolt_test.go index b969bce..d05458b 100644 --- a/store/bolt_test.go +++ b/store/bolt_test.go @@ -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) +}