Compare commits

..

4 Commits
v1.0.1 ... main

2 changed files with 68 additions and 31 deletions

View File

@ -37,7 +37,7 @@ func Create(fn string, m os.FileMode, opts *bolt.Options) (*DB, error) {
// 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 {
func SetStructInDB[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 {
@ -69,7 +69,7 @@ func SetStruct[T any](b *DB, path []string, value T, fn func([]string, T) error)
// 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 {
func SetStructListDB[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 {
@ -78,6 +78,10 @@ func SetStructList[T any](b *DB, path []string, values []T, fn func(*bolt.Bucket
defer b.CloseDB()
}
// We remove the bucket, if it already exists
bktPth, bktNm := path[:len(path)-2], path[len(path)-1]
b.DeleteBucket(bktPth, bktNm)
err = b.MkBucketPath(path)
if err != nil {
return err
@ -93,11 +97,8 @@ func SetStructList[T any](b *DB, path []string, values []T, fn func(*bolt.Bucket
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)
@ -107,15 +108,6 @@ func SetStructList[T any](b *DB, path []string, values []T, fn func(*bolt.Bucket
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
@ -498,6 +490,9 @@ func (b *DB) DeletePair(path []string, key string) error {
// DeleteBucket deletes the bucket key at path
func (b *DB) DeleteBucket(path []string, key string) error {
if len(path) == 0 {
return errors.New("no path")
}
var err error
if !b.dbIsOpen {
if err = b.OpenDB(); err != nil {
@ -576,6 +571,10 @@ func (b *DB) SetStringList(path, values []string) error {
defer b.CloseDB()
}
// We remove the bucket, if it already exists
bktPth, bktNm := path[:len(path)-2], path[len(path)-1]
b.DeleteBucket(bktPth, bktNm)
err = b.MkBucketPath(path)
if err != nil {
return err
@ -593,10 +592,6 @@ func (b *DB) SetStringList(path, values []string) error {
}
// 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)
@ -606,15 +601,6 @@ func (b *DB) SetStringList(path, values []string) error {
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

View File

@ -143,11 +143,12 @@ func GetBool(bkt *bolt.Bucket, key string) (bool, error) {
if err != nil {
return false, err
}
if r == "true" || r == "1" {
switch r {
case "true", "1":
return true, nil
} else if r == "false" || r == "0" {
case "false", "0":
return false, nil
} else {
default:
return false, fmt.Errorf("cannot parse as a boolean")
}
}
@ -384,3 +385,53 @@ func SetKeyValueStringMap(bkt *bolt.Bucket, kvMap map[string]string) error {
}
return SetKeyValueMap(bkt, chain)
}
func GetStruct[T any](bkt *bolt.Bucket, fn func(*bolt.Bucket) (T, error)) (T, error) {
return fn(bkt)
}
func SetStruct[T any](bkt *bolt.Bucket, value T, fn func(*bolt.Bucket, T) error) error {
return fn(bkt, value)
}
func GetStructList[T any](bkt *bolt.Bucket, fn func(*bolt.Bucket) (T, error)) ([]T, error) {
var ret []T
if bkt == nil {
return nil, fmt.Errorf("bucket must not be nil")
}
keyList, err := GetBucketList(bkt)
if err != nil {
return nil, err
}
for _, k := range keyList {
sBkt, err := GetBucket(bkt, k)
if err != nil {
return nil, err
}
v, err := fn(sBkt)
if err != nil {
return nil, err
}
ret = append(ret, v)
}
return ret, nil
}
func SetStructList[T any](bkt *bolt.Bucket, values []T, fn func(*bolt.Bucket, T) error) error {
bkt.SetSequence(0)
for _, v := range values {
id, _ := bkt.NextSequence()
bId := make([]byte, 8)
binary.BigEndian.PutUint64(bId, id)
// Delete the bucket if it already exists
bkt.DeleteBucket(bId)
structBkt, err := bkt.CreateBucketIfNotExists(bId)
if err != nil {
return err
}
if err = fn(structBkt, v); err != nil {
return err
}
}
return nil
}