Compare commits
13 Commits
Author | SHA1 | Date | |
---|---|---|---|
e31f75cb63 | |||
2ff40dcfdc | |||
58cd1cf257 | |||
1ccac4d7b9 | |||
4131d1be15 | |||
8c9a920404 | |||
75d1766e1a | |||
2121e5a434 | |||
4c7d152811 | |||
a3bfc9ba7b | |||
f45b90f172 | |||
2b96cfe1b6 | |||
f4e5a5e71c |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -19,4 +19,6 @@ _cgo_export.*
|
|||||||
|
|
||||||
_testmain.go
|
_testmain.go
|
||||||
|
|
||||||
*.exe
|
*.exe
|
||||||
|
|
||||||
|
example/
|
||||||
|
442
boltease.go
442
boltease.go
@@ -1,6 +1,8 @@
|
|||||||
package boltease
|
package boltease
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -33,6 +35,92 @@ func Create(fn string, m os.FileMode, opts *bolt.Options) (*DB, error) {
|
|||||||
return &b, nil
|
return &b, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetStruct takes a database, a path, a struct, and a function that takes a
|
||||||
|
// path and an element and processes it.
|
||||||
|
func SetStruct[T any](b *DB, path []string, value T, fn func([]string, T) error) 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 fn(path, value)
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetStructList takes a path, a slice of structs, and a function that takes
|
||||||
|
// a bolt bucket and one element of the slice and processes it.
|
||||||
|
func SetStructList[T any](b *DB, path []string, values []T, fn func(*bolt.Bucket, T) error) 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
|
||||||
|
// We need to get the sequence number, if it's greater than the length of
|
||||||
|
// 'values', we're going to have to delete values.
|
||||||
|
seq := bkt.Sequence()
|
||||||
|
bkt.SetSequence(0)
|
||||||
|
for _, v := range values {
|
||||||
|
id, _ := bkt.NextSequence()
|
||||||
|
bId := make([]byte, 8)
|
||||||
|
binary.BigEndian.PutUint64(bId, id)
|
||||||
|
structBkt, err := bkt.CreateBucketIfNotExists(bId)
|
||||||
|
if err = fn(structBkt, v); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
currSeq := bkt.Sequence()
|
||||||
|
for ; seq < currSeq; seq++ {
|
||||||
|
bId := make([]byte, 8)
|
||||||
|
binary.BigEndian.PutUint64(bId, seq)
|
||||||
|
err = bkt.Delete(bId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func (b *DB) OpenDB() error {
|
func (b *DB) OpenDB() error {
|
||||||
if b.dbIsOpen {
|
if b.dbIsOpen {
|
||||||
// DB is already open, that's fine.
|
// DB is already open, that's fine.
|
||||||
@@ -99,39 +187,56 @@ func (b *DB) MkBucketPath(path []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetValue returns the value at path
|
func (b *DB) Set(path []string, key string, val interface{}) error {
|
||||||
// path is a slice of strings
|
switch v := val.(type) {
|
||||||
// key is the key to get
|
case string:
|
||||||
func (b *DB) GetValue(path []string, key string) (string, error) {
|
return b.SetString(path, key, v)
|
||||||
|
case int:
|
||||||
|
return b.SetInt(path, key, v)
|
||||||
|
case bool:
|
||||||
|
return b.SetBool(path, key, v)
|
||||||
|
case []byte:
|
||||||
|
return b.SetBytes(path, key, v)
|
||||||
|
}
|
||||||
|
return errors.New("unknown data type")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *DB) GetBytes(path []string, key string) ([]byte, error) {
|
||||||
var err error
|
var err error
|
||||||
var ret string
|
var ret []byte
|
||||||
if !b.dbIsOpen {
|
if !b.dbIsOpen {
|
||||||
if err = b.OpenDB(); err != nil {
|
if err = b.OpenDB(); err != nil {
|
||||||
return ret, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer b.CloseDB()
|
defer b.CloseDB()
|
||||||
}
|
}
|
||||||
err = b.boltDB.View(func(tx *bolt.Tx) error {
|
err = b.boltDB.View(func(tx *bolt.Tx) error {
|
||||||
bkt := tx.Bucket([]byte(path[0]))
|
bkt := tx.Bucket([]byte(path[0]))
|
||||||
if bkt == nil {
|
if bkt == nil {
|
||||||
return fmt.Errorf("Couldn't find bucket " + path[0])
|
return fmt.Errorf("couldn't find bucket " + path[0])
|
||||||
}
|
}
|
||||||
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
|
||||||
ret = string(bkt.Get([]byte(key)))
|
val := bkt.Get([]byte(key))
|
||||||
|
if val == nil {
|
||||||
|
return fmt.Errorf("couldn't find value")
|
||||||
|
}
|
||||||
|
ret = make([]byte, len(val))
|
||||||
|
copy(ret, val)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
return ret, err
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetValue sets the value of key at path to val
|
func (b *DB) SetBytes(path []string, key string, val []byte) error {
|
||||||
// path is a slice of tokens
|
|
||||||
func (b *DB) SetValue(path []string, key, val string) error {
|
|
||||||
var err error
|
var err error
|
||||||
if !b.dbIsOpen {
|
if !b.dbIsOpen {
|
||||||
if err = b.OpenDB(); err != nil {
|
if err = b.OpenDB(); err != nil {
|
||||||
@@ -147,7 +252,7 @@ func (b *DB) SetValue(path []string, key, val string) error {
|
|||||||
err = b.boltDB.Update(func(tx *bolt.Tx) error {
|
err = b.boltDB.Update(func(tx *bolt.Tx) error {
|
||||||
bkt := tx.Bucket([]byte(path[0]))
|
bkt := tx.Bucket([]byte(path[0]))
|
||||||
if bkt == nil {
|
if bkt == nil {
|
||||||
return fmt.Errorf("Couldn't find bucket " + path[0])
|
return fmt.Errorf("couldn't find bucket " + path[0])
|
||||||
}
|
}
|
||||||
for idx := 1; idx < len(path); idx++ {
|
for idx := 1; idx < len(path); idx++ {
|
||||||
bkt, err = bkt.CreateBucketIfNotExists([]byte(path[idx]))
|
bkt, err = bkt.CreateBucketIfNotExists([]byte(path[idx]))
|
||||||
@@ -156,7 +261,69 @@ func (b *DB) SetValue(path []string, key, val string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// bkt should have the last bucket in the path
|
// bkt should have the last bucket in the path
|
||||||
return bkt.Put([]byte(key), []byte(val))
|
return bkt.Put([]byte(key), val)
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetString returns the value at path
|
||||||
|
// path is a slice of strings
|
||||||
|
// key is the key to get
|
||||||
|
func (b *DB) GetString(path []string, key string) (string, error) {
|
||||||
|
var err error
|
||||||
|
var ret string
|
||||||
|
if !b.dbIsOpen {
|
||||||
|
if err = b.OpenDB(); err != nil {
|
||||||
|
return ret, 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], "/"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// bkt should have the last bucket in the path
|
||||||
|
ret = string(bkt.Get([]byte(key)))
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return ret, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetString sets the value of key at path to val
|
||||||
|
// path is a slice of tokens
|
||||||
|
func (b *DB) SetString(path []string, key, val 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
|
||||||
|
return SetString(bkt, key, val)
|
||||||
})
|
})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -165,7 +332,7 @@ func (b *DB) SetValue(path []string, key, val string) error {
|
|||||||
// If the value cannot be parsed as an int, error
|
// If the value cannot be parsed as an int, error
|
||||||
func (b *DB) GetInt(path []string, key string) (int, error) {
|
func (b *DB) GetInt(path []string, key string) (int, error) {
|
||||||
var ret int
|
var ret int
|
||||||
r, err := b.GetValue(path, key)
|
r, err := b.GetString(path, key)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
ret, err = strconv.Atoi(r)
|
ret, err = strconv.Atoi(r)
|
||||||
}
|
}
|
||||||
@@ -174,7 +341,7 @@ func (b *DB) GetInt(path []string, key string) (int, error) {
|
|||||||
|
|
||||||
// SetInt Sets an integer value
|
// SetInt Sets an integer value
|
||||||
func (b *DB) SetInt(path []string, key string, val int) error {
|
func (b *DB) SetInt(path []string, key string, val int) error {
|
||||||
return b.SetValue(path, key, strconv.Itoa(val))
|
return b.SetString(path, key, strconv.Itoa(val))
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetBool returns the value at 'path'
|
// GetBool returns the value at 'path'
|
||||||
@@ -182,12 +349,12 @@ func (b *DB) SetInt(path []string, key string, val int) error {
|
|||||||
// We check 'true/false' and '1/0', else error
|
// We check 'true/false' and '1/0', else error
|
||||||
func (b *DB) GetBool(path []string, key string) (bool, error) {
|
func (b *DB) GetBool(path []string, key string) (bool, error) {
|
||||||
var ret bool
|
var ret bool
|
||||||
r, err := b.GetValue(path, key)
|
r, err := b.GetString(path, key)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if r == "true" || r == "1" {
|
if r == "true" || r == "1" {
|
||||||
ret = true
|
ret = true
|
||||||
} else if r != "false" && r != "0" {
|
} else if r != "false" && r != "0" {
|
||||||
err = fmt.Errorf("Cannot parse as a boolean")
|
err = fmt.Errorf("cannot parse as a boolean")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ret, err
|
return ret, err
|
||||||
@@ -196,15 +363,15 @@ func (b *DB) GetBool(path []string, key string) (bool, error) {
|
|||||||
// SetBool Sets a boolean value
|
// SetBool Sets a boolean value
|
||||||
func (b *DB) SetBool(path []string, key string, val bool) error {
|
func (b *DB) SetBool(path []string, key string, val bool) error {
|
||||||
if val {
|
if val {
|
||||||
return b.SetValue(path, key, "true")
|
return b.SetString(path, key, "true")
|
||||||
}
|
}
|
||||||
return b.SetValue(path, key, "false")
|
return b.SetString(path, key, "false")
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetTimestamp returns the value at 'path'
|
// GetTimestamp returns the value at 'path'
|
||||||
// If the value cannot be parsed as a RFC3339, error
|
// If the value cannot be parsed as a RFC3339, error
|
||||||
func (b *DB) GetTimestamp(path []string, key string) (time.Time, error) {
|
func (b *DB) GetTimestamp(path []string, key string) (time.Time, error) {
|
||||||
r, err := b.GetValue(path, key)
|
r, err := b.GetString(path, key)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return time.Parse(time.RFC3339, r)
|
return time.Parse(time.RFC3339, r)
|
||||||
}
|
}
|
||||||
@@ -213,7 +380,7 @@ func (b *DB) GetTimestamp(path []string, key string) (time.Time, error) {
|
|||||||
|
|
||||||
// SetTimestamp saves a timestamp into the db
|
// SetTimestamp saves a timestamp into the db
|
||||||
func (b *DB) SetTimestamp(path []string, key string, val time.Time) error {
|
func (b *DB) SetTimestamp(path []string, key string, val time.Time) error {
|
||||||
return b.SetValue(path, key, val.Format(time.RFC3339))
|
return b.SetString(path, key, val.Format(time.RFC3339))
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetBucketList returns a list of all sub-buckets at path
|
// GetBucketList returns a list of all sub-buckets at path
|
||||||
@@ -230,14 +397,14 @@ func (b *DB) GetBucketList(path []string) ([]string, error) {
|
|||||||
err = b.boltDB.Update(func(tx *bolt.Tx) error {
|
err = b.boltDB.Update(func(tx *bolt.Tx) error {
|
||||||
bkt := tx.Bucket([]byte(path[0]))
|
bkt := tx.Bucket([]byte(path[0]))
|
||||||
if bkt == nil {
|
if bkt == nil {
|
||||||
return fmt.Errorf("Couldn't find bucket " + path[0])
|
return fmt.Errorf("couldn't find bucket " + path[0])
|
||||||
}
|
}
|
||||||
var berr error
|
var berr error
|
||||||
if len(path) > 1 {
|
if len(path) > 1 {
|
||||||
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], " / "))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -269,14 +436,14 @@ func (b *DB) GetKeyList(path []string) ([]string, error) {
|
|||||||
err = b.boltDB.Update(func(tx *bolt.Tx) error {
|
err = b.boltDB.Update(func(tx *bolt.Tx) error {
|
||||||
bkt := tx.Bucket([]byte(path[0]))
|
bkt := tx.Bucket([]byte(path[0]))
|
||||||
if bkt == nil {
|
if bkt == nil {
|
||||||
return fmt.Errorf("Couldn't find bucket " + path[0])
|
return fmt.Errorf("couldn't find bucket " + path[0])
|
||||||
}
|
}
|
||||||
var berr error
|
var berr error
|
||||||
if len(path) > 1 {
|
if len(path) > 1 {
|
||||||
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], " / "))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -307,14 +474,14 @@ func (b *DB) DeletePair(path []string, key string) error {
|
|||||||
err = b.boltDB.Update(func(tx *bolt.Tx) error {
|
err = b.boltDB.Update(func(tx *bolt.Tx) error {
|
||||||
bkt := tx.Bucket([]byte(path[0]))
|
bkt := tx.Bucket([]byte(path[0]))
|
||||||
if bkt == nil {
|
if bkt == nil {
|
||||||
return fmt.Errorf("Couldn't find bucket " + path[0])
|
return fmt.Errorf("couldn't find bucket " + path[0])
|
||||||
}
|
}
|
||||||
if len(path) > 1 {
|
if len(path) > 1 {
|
||||||
var newBkt *bolt.Bucket
|
var newBkt *bolt.Bucket
|
||||||
for idx := 1; idx < len(path); idx++ {
|
for idx := 1; idx < len(path); idx++ {
|
||||||
newBkt = bkt.Bucket([]byte(path[idx]))
|
newBkt = bkt.Bucket([]byte(path[idx]))
|
||||||
if newBkt == nil {
|
if newBkt == nil {
|
||||||
return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx], "/"))
|
return fmt.Errorf("couldn't find bucket " + strings.Join(path[:idx], "/"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bkt = newBkt
|
bkt = newBkt
|
||||||
@@ -342,12 +509,12 @@ func (b *DB) DeleteBucket(path []string, key string) error {
|
|||||||
err = b.boltDB.Update(func(tx *bolt.Tx) error {
|
err = b.boltDB.Update(func(tx *bolt.Tx) error {
|
||||||
bkt := tx.Bucket([]byte(path[0]))
|
bkt := tx.Bucket([]byte(path[0]))
|
||||||
if bkt == nil {
|
if bkt == nil {
|
||||||
return fmt.Errorf("Couldn't find bucket " + path[0])
|
return fmt.Errorf("couldn't find bucket " + path[0])
|
||||||
}
|
}
|
||||||
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], "/"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// bkt should have the last bucket in the path
|
// bkt should have the last bucket in the path
|
||||||
@@ -360,8 +527,8 @@ func (b *DB) DeleteBucket(path []string, key string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetValueList returns a string slice of all values in the bucket at path
|
// GetStringList returns a string slice of all values in the bucket at path
|
||||||
func (b *DB) GetValueList(path []string) ([]string, error) {
|
func (b *DB) GetStringList(path []string) ([]string, error) {
|
||||||
var err error
|
var err error
|
||||||
var ret []string
|
var ret []string
|
||||||
if !b.dbIsOpen {
|
if !b.dbIsOpen {
|
||||||
@@ -374,14 +541,14 @@ func (b *DB) GetValueList(path []string) ([]string, error) {
|
|||||||
err = b.boltDB.Update(func(tx *bolt.Tx) error {
|
err = b.boltDB.Update(func(tx *bolt.Tx) error {
|
||||||
bkt := tx.Bucket([]byte(path[0]))
|
bkt := tx.Bucket([]byte(path[0]))
|
||||||
if bkt == nil {
|
if bkt == nil {
|
||||||
return fmt.Errorf("Couldn't find bucket " + path[0])
|
return fmt.Errorf("couldn't find bucket " + path[0])
|
||||||
}
|
}
|
||||||
var berr error
|
var berr error
|
||||||
if len(path) > 1 {
|
if len(path) > 1 {
|
||||||
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 +565,208 @@ func (b *DB) GetValueList(path []string) ([]string, error) {
|
|||||||
})
|
})
|
||||||
return ret, err
|
return ret, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetStringList puts a string slice into the bucket at path
|
||||||
|
func (b *DB) SetStringList(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
|
||||||
|
// We need to get the sequence number, if it's greater than the length
|
||||||
|
// of 'values', we're going to have to delete values.
|
||||||
|
seq := bkt.Sequence()
|
||||||
|
bkt.SetSequence(0)
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
currSeq := bkt.Sequence()
|
||||||
|
for ; seq < currSeq; seq++ {
|
||||||
|
bId := make([]byte, 8)
|
||||||
|
binary.BigEndian.PutUint64(bId, seq)
|
||||||
|
err = bkt.Delete(bId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddToStringList adds strings to a bucket of string slices
|
||||||
|
func (b *DB) AddToStringList(path []string, 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
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *DB) StringListContains(path []string, value string) (bool, error) {
|
||||||
|
list, err := b.GetStringList(path)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
for i := range list {
|
||||||
|
if list[i] == value {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetKeyValueMap returns a map of all key/value pairs in the bucket at path
|
||||||
|
func (b *DB) GetKeyValueMap(path []string) (map[string][]byte, error) {
|
||||||
|
var err error
|
||||||
|
ret := make(map[string][]byte)
|
||||||
|
if !b.dbIsOpen {
|
||||||
|
if err = b.OpenDB(); err != nil {
|
||||||
|
return ret, err
|
||||||
|
}
|
||||||
|
defer b.CloseDB()
|
||||||
|
}
|
||||||
|
|
||||||
|
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])
|
||||||
|
}
|
||||||
|
var berr error
|
||||||
|
if len(path) > 1 {
|
||||||
|
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], " / "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// bkt should have the last bucket in the path
|
||||||
|
berr = bkt.ForEach(func(k, v []byte) error {
|
||||||
|
if v != nil {
|
||||||
|
// Must be a key
|
||||||
|
ret[string(k)] = v
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return berr
|
||||||
|
})
|
||||||
|
return ret, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetKeyValueStringMap returns all key/value pairs at path as a map
|
||||||
|
func (b *DB) GetKeyValueStringMap(path []string) (map[string]string, error) {
|
||||||
|
ret := make(map[string]string)
|
||||||
|
btMap, err := b.GetKeyValueMap(path)
|
||||||
|
if err != nil {
|
||||||
|
return ret, err
|
||||||
|
}
|
||||||
|
for k, v := range btMap {
|
||||||
|
ret[k] = string(v)
|
||||||
|
}
|
||||||
|
return ret, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetKeyValueMap puts all key/value pairs into the bucket at path
|
||||||
|
func (b *DB) SetKeyValueMap(path []string, kvMap map[string][]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
|
||||||
|
for k, v := range kvMap {
|
||||||
|
err = bkt.Put([]byte(k), v)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetKeyValueStringMap takes a map[string]string and puts all key/value pairs into the bucket at path
|
||||||
|
func (b *DB) SetKeyValueStringMap(path []string, kvMap map[string]string) error {
|
||||||
|
chain := make(map[string][]byte)
|
||||||
|
for k, v := range kvMap {
|
||||||
|
chain[k] = []byte(v)
|
||||||
|
}
|
||||||
|
return b.SetKeyValueMap(path, chain)
|
||||||
|
}
|
||||||
|
386
bucket_funcs.go
Normal file
386
bucket_funcs.go
Normal file
@@ -0,0 +1,386 @@
|
|||||||
|
package boltease
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/boltdb/bolt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func DoUpdate(b *DB, path []string, fn func(bkt *bolt.Bucket) error) 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 fn(bkt)
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func DoRead(b *DB, path []string, fn func(bkt *bolt.Bucket) error) error {
|
||||||
|
var err error
|
||||||
|
if !b.dbIsOpen {
|
||||||
|
if err = b.OpenDB(); err != nil {
|
||||||
|
return 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], "/"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// bkt should have the last bucket in the path
|
||||||
|
return fn(bkt)
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// These bucket functions are meant to be run inside of a boltdb
|
||||||
|
// transaction (Update/View)
|
||||||
|
// GetBucket must be run inside of an Update transaction
|
||||||
|
// Otherwise, any Get* function can be in either.
|
||||||
|
// And any Set* function needs to be in an Update.
|
||||||
|
func GetBucket(bkt *bolt.Bucket, path ...string) (*bolt.Bucket, error) {
|
||||||
|
if bkt == nil {
|
||||||
|
return nil, fmt.Errorf("bucket must not be nil")
|
||||||
|
}
|
||||||
|
var ret *bolt.Bucket
|
||||||
|
var err error
|
||||||
|
ret, err = bkt.CreateBucketIfNotExists([]byte(path[0]))
|
||||||
|
if len(path) > 1 {
|
||||||
|
for idx := 1; idx < len(path); idx++ {
|
||||||
|
ret, err = ret.CreateBucketIfNotExists([]byte(path[idx]))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetString(bkt *bolt.Bucket, key string) (string, error) {
|
||||||
|
if bkt == nil {
|
||||||
|
return "", fmt.Errorf("bucket must not be nil")
|
||||||
|
}
|
||||||
|
return string(bkt.Get([]byte(key))), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetString(bkt *bolt.Bucket, key, val string) error {
|
||||||
|
if bkt == nil {
|
||||||
|
return fmt.Errorf("bucket must not be nil")
|
||||||
|
}
|
||||||
|
return bkt.Put([]byte(key), []byte(val))
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetUint64(bkt *bolt.Bucket, key string) (uint64, error) {
|
||||||
|
if bkt == nil {
|
||||||
|
return 0, fmt.Errorf("bucket must not be nil")
|
||||||
|
}
|
||||||
|
r, err := GetString(bkt, key)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return strconv.ParseUint(r, 10, 64)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetUint64(bkt *bolt.Bucket, key string, val uint64) error {
|
||||||
|
if bkt == nil {
|
||||||
|
return fmt.Errorf("bucket must not be nil")
|
||||||
|
}
|
||||||
|
return SetString(bkt, key, strconv.FormatUint(val, 10))
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetInt(bkt *bolt.Bucket, key string) (int, error) {
|
||||||
|
if bkt == nil {
|
||||||
|
return 0, fmt.Errorf("bucket must not be nil")
|
||||||
|
}
|
||||||
|
r, err := GetString(bkt, key)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return strconv.Atoi(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetInt(bkt *bolt.Bucket, key string, val int) error {
|
||||||
|
if bkt == nil {
|
||||||
|
return fmt.Errorf("bucket must not be nil")
|
||||||
|
}
|
||||||
|
return SetString(bkt, key, strconv.Itoa(val))
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetBool(bkt *bolt.Bucket, key string) (bool, error) {
|
||||||
|
r, err := GetString(bkt, key)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
if r == "true" || r == "1" {
|
||||||
|
return true, nil
|
||||||
|
} else if r == "false" || r == "0" {
|
||||||
|
return false, nil
|
||||||
|
} else {
|
||||||
|
return false, fmt.Errorf("cannot parse as a boolean")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetBool(bkt *bolt.Bucket, key string, val bool) error {
|
||||||
|
if bkt == nil {
|
||||||
|
return fmt.Errorf("bucket must not be nil")
|
||||||
|
}
|
||||||
|
if val {
|
||||||
|
return SetString(bkt, key, "true")
|
||||||
|
}
|
||||||
|
return SetString(bkt, key, "false")
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetTimestamp(bkt *bolt.Bucket, key string) (time.Time, error) {
|
||||||
|
r, err := GetString(bkt, key)
|
||||||
|
if err != nil {
|
||||||
|
return time.Unix(0, 0), err
|
||||||
|
}
|
||||||
|
return time.Parse(time.RFC3339, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetTimestamp(bkt *bolt.Bucket, key string, val time.Time) error {
|
||||||
|
return SetString(bkt, key, val.Format(time.RFC3339))
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetBucketList(bkt *bolt.Bucket) ([]string, error) {
|
||||||
|
var ret []string
|
||||||
|
if bkt == nil {
|
||||||
|
return ret, fmt.Errorf("bucket must not be nil")
|
||||||
|
}
|
||||||
|
bkt.ForEach(func(k, v []byte) error {
|
||||||
|
if v == nil {
|
||||||
|
// Must be a bucket
|
||||||
|
ret = append(ret, string(k))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetKeyList(bkt *bolt.Bucket) ([]string, error) {
|
||||||
|
var ret []string
|
||||||
|
if bkt == nil {
|
||||||
|
return ret, fmt.Errorf("bucket must not be nil")
|
||||||
|
}
|
||||||
|
bkt.ForEach(func(k, v []byte) error {
|
||||||
|
if v != nil {
|
||||||
|
ret = append(ret, string(k))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeletePair(bkt *bolt.Bucket, key string) error {
|
||||||
|
if bkt == nil {
|
||||||
|
return fmt.Errorf("bucket must not be nil")
|
||||||
|
}
|
||||||
|
// Make sure that the key belongs to a pair
|
||||||
|
if tst := bkt.Bucket([]byte(key)); tst != nil {
|
||||||
|
return fmt.Errorf("key is a bucket")
|
||||||
|
}
|
||||||
|
return bkt.Delete([]byte(key))
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteBucket(bkt *bolt.Bucket, key string) error {
|
||||||
|
if bkt == nil {
|
||||||
|
return fmt.Errorf("bucket must not be nil")
|
||||||
|
}
|
||||||
|
// Make sure that the key belongs to a pair
|
||||||
|
if tst := bkt.Bucket([]byte(key)); tst == nil {
|
||||||
|
return fmt.Errorf("key is not a bucket")
|
||||||
|
}
|
||||||
|
return bkt.DeleteBucket([]byte(key))
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetStringList(bkt *bolt.Bucket) ([]string, error) {
|
||||||
|
var ret []string
|
||||||
|
if bkt == nil {
|
||||||
|
return ret, fmt.Errorf("bucket must not be nil")
|
||||||
|
}
|
||||||
|
bkt.ForEach(func(k, v []byte) error {
|
||||||
|
if v != nil {
|
||||||
|
// Must be a key
|
||||||
|
ret = append(ret, string(v))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetStringList(bkt *bolt.Bucket, val []string) error {
|
||||||
|
if bkt == nil {
|
||||||
|
return fmt.Errorf("bucket must not be nil")
|
||||||
|
}
|
||||||
|
genseq := func(id uint64) []byte {
|
||||||
|
bId := make([]byte, 8)
|
||||||
|
binary.BigEndian.PutUint64(bId, id)
|
||||||
|
return bId
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
// We need to get the sequence number, if it's greater than the length of
|
||||||
|
// val, we're going to have to delete values
|
||||||
|
seq := bkt.Sequence()
|
||||||
|
bkt.SetSequence(0)
|
||||||
|
for _, v := range val {
|
||||||
|
id, _ := bkt.NextSequence()
|
||||||
|
if err = bkt.Put(genseq(id), []byte(v)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
currSeq := bkt.Sequence()
|
||||||
|
for ; seq < currSeq; seq++ {
|
||||||
|
err = bkt.Delete(genseq(seq))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddToStringList(bkt *bolt.Bucket, values ...string) error {
|
||||||
|
if bkt == nil {
|
||||||
|
return fmt.Errorf("bucket must not be nil")
|
||||||
|
}
|
||||||
|
genseq := func(id uint64) []byte {
|
||||||
|
bId := make([]byte, 8)
|
||||||
|
binary.BigEndian.PutUint64(bId, id)
|
||||||
|
return bId
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
for _, v := range values {
|
||||||
|
id, _ := bkt.NextSequence()
|
||||||
|
if err = bkt.Put(genseq(id), []byte(v)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddToUniqueStringList(bkt *bolt.Bucket, values ...string) error {
|
||||||
|
if bkt == nil {
|
||||||
|
return fmt.Errorf("bucket must not be nil")
|
||||||
|
}
|
||||||
|
genseq := func(id uint64) []byte {
|
||||||
|
bId := make([]byte, 8)
|
||||||
|
binary.BigEndian.PutUint64(bId, id)
|
||||||
|
return bId
|
||||||
|
}
|
||||||
|
currList, _ := GetStringList(bkt)
|
||||||
|
var err error
|
||||||
|
for _, v := range values {
|
||||||
|
add := true
|
||||||
|
for _, currV := range currList {
|
||||||
|
if currV == v {
|
||||||
|
add = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if add {
|
||||||
|
id, _ := bkt.NextSequence()
|
||||||
|
if err = bkt.Put(genseq(id), []byte(v)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func StringListContains(bkt *bolt.Bucket, value string) (bool, error) {
|
||||||
|
if bkt == nil {
|
||||||
|
return false, fmt.Errorf("bucket must not be nil")
|
||||||
|
}
|
||||||
|
currList, err := GetStringList(bkt)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
for i := range currList {
|
||||||
|
if currList[i] == value {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetKeyValueMap(bkt *bolt.Bucket) (map[string][]byte, error) {
|
||||||
|
ret := make(map[string][]byte)
|
||||||
|
if bkt == nil {
|
||||||
|
return ret, fmt.Errorf("bucket must not be nil")
|
||||||
|
}
|
||||||
|
bkt.ForEach(func(k, v []byte) error {
|
||||||
|
if v != nil {
|
||||||
|
ret[string(k)] = v
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetKeyValueMap(bkt *bolt.Bucket, kvMap map[string][]byte) error {
|
||||||
|
if bkt == nil {
|
||||||
|
return fmt.Errorf("bucket must not be nil")
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
for k, v := range kvMap {
|
||||||
|
err = bkt.Put([]byte(k), v)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetKeyValueStringMap(bkt *bolt.Bucket) (map[string]string, error) {
|
||||||
|
ret := make(map[string]string)
|
||||||
|
if bkt == nil {
|
||||||
|
return ret, fmt.Errorf("bucket must not be nil")
|
||||||
|
}
|
||||||
|
btMap, err := GetKeyValueMap(bkt)
|
||||||
|
if err != nil {
|
||||||
|
return ret, err
|
||||||
|
}
|
||||||
|
for k, v := range btMap {
|
||||||
|
ret[k] = string(v)
|
||||||
|
}
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetKeyValueStringMap(bkt *bolt.Bucket, kvMap map[string]string) error {
|
||||||
|
chain := make(map[string][]byte)
|
||||||
|
for k, v := range kvMap {
|
||||||
|
chain[k] = []byte(v)
|
||||||
|
}
|
||||||
|
return SetKeyValueMap(bkt, chain)
|
||||||
|
}
|
8
go.mod
Normal file
8
go.mod
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
module git.bullercodeworks.com/brian/boltease
|
||||||
|
|
||||||
|
go 1.23.0
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/boltdb/bolt v1.3.1
|
||||||
|
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect
|
||||||
|
)
|
4
go.sum
Normal file
4
go.sum
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4=
|
||||||
|
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
|
||||||
|
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf h1:2ucpDCmfkl8Bd/FsLtiD653Wf96cW37s+iGx93zsu4k=
|
||||||
|
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
Reference in New Issue
Block a user