Skip to content

Commit a456958

Browse files
stefanbergerJared Cordasco
authored andcommitted
Prepare boltutil for reading and writing another map
Refactor the code so that another function can also read and write maps into the bolt db. Signed-off-by: Stefan Berger <stefanb@linux.ibm.com> (cherry picked from commit 02cc148) Signed-off-by: Jared Cordasco <jcordasc@coglib.com>
1 parent 4bb9151 commit a456958

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

metadata/boltutil/helpers.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ var (
3232
// ReadLabels reads the labels key from the bucket
3333
// Uses the key "labels"
3434
func ReadLabels(bkt *bolt.Bucket) (map[string]string, error) {
35-
lbkt := bkt.Bucket(bucketKeyLabels)
35+
return readMap(bkt, bucketKeyLabels)
36+
}
37+
38+
func readMap(bkt *bolt.Bucket, bucketName []byte) (map[string]string, error) {
39+
lbkt := bkt.Bucket(bucketName)
3640
if lbkt == nil {
3741
return nil, nil
3842
}
@@ -53,9 +57,13 @@ func ReadLabels(bkt *bolt.Bucket) (map[string]string, error) {
5357
// bucket. Typically, this removes zero-value entries.
5458
// Uses the key "labels"
5559
func WriteLabels(bkt *bolt.Bucket, labels map[string]string) error {
60+
return writeMap(bkt, bucketKeyLabels, labels)
61+
}
62+
63+
func writeMap(bkt *bolt.Bucket, bucketName []byte, labels map[string]string) error {
5664
// Remove existing labels to keep from merging
57-
if lbkt := bkt.Bucket(bucketKeyLabels); lbkt != nil {
58-
if err := bkt.DeleteBucket(bucketKeyLabels); err != nil {
65+
if lbkt := bkt.Bucket(bucketName); lbkt != nil {
66+
if err := bkt.DeleteBucket(bucketName); err != nil {
5967
return err
6068
}
6169
}
@@ -64,7 +72,7 @@ func WriteLabels(bkt *bolt.Bucket, labels map[string]string) error {
6472
return nil
6573
}
6674

67-
lbkt, err := bkt.CreateBucket(bucketKeyLabels)
75+
lbkt, err := bkt.CreateBucket(bucketName)
6876
if err != nil {
6977
return err
7078
}

0 commit comments

Comments
 (0)