From f4e5a5e71cbef2bb6eab29788619d334861927b5 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Tue, 6 Apr 2021 16:36:24 -0500 Subject: [PATCH] Merge Github Repo --- boltease.go | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 105 insertions(+), 2 deletions(-) diff --git a/boltease.go b/boltease.go index 4590cdf..fbc5089 100644 --- a/boltease.go +++ b/boltease.go @@ -1,6 +1,7 @@ package boltease import ( + "encoding/binary" "fmt" "os" "strconv" @@ -99,6 +100,68 @@ func (b *DB) MkBucketPath(path []string) error { return err } +func (b *DB) GetBytes(path []string, key string) ([]byte, error) { + var err error + var ret []byte + if !b.dbIsOpen { + if err = b.OpenDB(); err != nil { + return nil, err + } + defer b.CloseDB() + } + err = b.boltDB.View(func(tx *bolt.Tx) error { + bkt := tx.Bucket([]byte(path[0])) + if bkt == nil { + return fmt.Errorf("Couldn't find bucket " + path[0]) + } + for idx := 1; idx < len(path); idx++ { + bkt = bkt.Bucket([]byte(path[idx])) + if bkt == nil { + return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx+1], "/")) + } + } + // newBkt should have the last bucket in the path + val := bkt.Get([]byte(key)) + ret = make([]byte, len(val)) + copy(ret, val) + return nil + }) + if err != nil { + return nil, err + } + return ret, nil +} + +func (b *DB) SetBytes(path []string, key string, val []byte) error { + var err error + if !b.dbIsOpen { + if err = b.OpenDB(); err != nil { + return err + } + defer b.CloseDB() + } + + err = b.MkBucketPath(path) + if err != nil { + return err + } + err = b.boltDB.Update(func(tx *bolt.Tx) error { + bkt := tx.Bucket([]byte(path[0])) + if bkt == nil { + return fmt.Errorf("Couldn't find bucket " + path[0]) + } + for idx := 1; idx < len(path); idx++ { + bkt, err = bkt.CreateBucketIfNotExists([]byte(path[idx])) + if err != nil { + return err + } + } + // bkt should have the last bucket in the path + return bkt.Put([]byte(key), val) + }) + return err +} + // GetValue returns the value at path // path is a slice of strings // key is the key to get @@ -119,7 +182,7 @@ func (b *DB) GetValue(path []string, key string) (string, error) { for idx := 1; idx < len(path); idx++ { bkt = bkt.Bucket([]byte(path[idx])) if bkt == nil { - return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx], "/")) + return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx+1], "/")) } } // newBkt should have the last bucket in the path @@ -381,7 +444,7 @@ func (b *DB) GetValueList(path []string) ([]string, error) { for idx := 1; idx < len(path); idx++ { bkt = bkt.Bucket([]byte(path[idx])) if bkt == nil { - return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx], " / ")) + return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx+1], " / ")) } } } @@ -398,3 +461,43 @@ func (b *DB) GetValueList(path []string) ([]string, error) { }) return ret, err } + +// SetValueList puts a string slice into the bucket at path +func (b *DB) SetValueList(path, values []string) error { + var err error + if !b.dbIsOpen { + if err = b.OpenDB(); err != nil { + return err + } + defer b.CloseDB() + } + + err = b.MkBucketPath(path) + if err != nil { + return err + } + err = b.boltDB.Update(func(tx *bolt.Tx) error { + bkt := tx.Bucket([]byte(path[0])) + if bkt == nil { + return fmt.Errorf("Couldn't find bucket " + path[0]) + } + for idx := 1; idx < len(path); idx++ { + bkt, err = bkt.CreateBucketIfNotExists([]byte(path[idx])) + if err != nil { + return err + } + } + // bkt should have the last bucket in the path + for _, v := range values { + id, _ := bkt.NextSequence() + bId := make([]byte, 8) + binary.BigEndian.PutUint64(bId, id) + err = bkt.Put(bId, []byte(v)) + if err != nil { + return err + } + } + return nil + }) + return err +}