Merge Github Repo

This commit is contained in:
Brian Buller 2021-04-06 16:36:24 -05:00
parent 8d9019e01b
commit f4e5a5e71c

View File

@ -1,6 +1,7 @@
package boltease package boltease
import ( import (
"encoding/binary"
"fmt" "fmt"
"os" "os"
"strconv" "strconv"
@ -99,6 +100,68 @@ func (b *DB) MkBucketPath(path []string) error {
return err 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 // GetValue returns the value at path
// path is a slice of strings // path is a slice of strings
// key is the key to get // 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++ { for idx := 1; idx < len(path); idx++ {
bkt = bkt.Bucket([]byte(path[idx])) bkt = bkt.Bucket([]byte(path[idx]))
if bkt == nil { 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 // 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++ { for idx := 1; idx < len(path); idx++ {
bkt = bkt.Bucket([]byte(path[idx])) bkt = bkt.Bucket([]byte(path[idx]))
if bkt == nil { 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 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
}