Compare commits

..

No commits in common. "main" and "v1.0.1" have entirely different histories.
main ... v1.0.1

2 changed files with 31 additions and 68 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 // SetStruct takes a database, a path, a struct, and a function that takes a
// path and an element and processes it. // path and an element and processes it.
func SetStructInDB[T any](b *DB, path []string, value T, fn func([]string, T) error) error { func SetStruct[T any](b *DB, path []string, value T, fn func([]string, T) error) 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 {
@ -69,7 +69,7 @@ func SetStructInDB[T any](b *DB, path []string, value T, fn func([]string, T) er
// SetStructList takes a path, a slice of structs, and a function that takes // 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. // a bolt bucket and one element of the slice and processes it.
func SetStructListDB[T any](b *DB, path []string, values []T, fn func(*bolt.Bucket, T) error) error { func SetStructList[T any](b *DB, path []string, values []T, fn func(*bolt.Bucket, T) error) 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 {
@ -78,10 +78,6 @@ func SetStructListDB[T any](b *DB, path []string, values []T, fn func(*bolt.Buck
defer b.CloseDB() 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) err = b.MkBucketPath(path)
if err != nil { if err != nil {
return err return err
@ -97,8 +93,11 @@ func SetStructListDB[T any](b *DB, path []string, values []T, fn func(*bolt.Buck
return err return err
} }
} }
// bkt should have the last bucket in the path // 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 { for _, v := range values {
id, _ := bkt.NextSequence() id, _ := bkt.NextSequence()
bId := make([]byte, 8) bId := make([]byte, 8)
@ -108,6 +107,15 @@ func SetStructListDB[T any](b *DB, path []string, values []T, fn func(*bolt.Buck
return err 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 nil
}) })
return err return err
@ -490,9 +498,6 @@ func (b *DB) DeletePair(path []string, key string) error {
// DeleteBucket deletes the bucket key at path // DeleteBucket deletes the bucket key at path
func (b *DB) DeleteBucket(path []string, key string) error { func (b *DB) DeleteBucket(path []string, key string) error {
if len(path) == 0 {
return errors.New("no path")
}
var err error var err error
if !b.dbIsOpen { if !b.dbIsOpen {
if err = b.OpenDB(); err != nil { if err = b.OpenDB(); err != nil {
@ -571,10 +576,6 @@ func (b *DB) SetStringList(path, values []string) error {
defer b.CloseDB() 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) err = b.MkBucketPath(path)
if err != nil { if err != nil {
return err return err
@ -592,6 +593,10 @@ func (b *DB) SetStringList(path, values []string) error {
} }
// bkt should have the last bucket in the path // 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 { for _, v := range values {
id, _ := bkt.NextSequence() id, _ := bkt.NextSequence()
bId := make([]byte, 8) bId := make([]byte, 8)
@ -601,6 +606,15 @@ func (b *DB) SetStringList(path, values []string) error {
return err 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 nil
}) })
return err return err

View File

@ -143,12 +143,11 @@ func GetBool(bkt *bolt.Bucket, key string) (bool, error) {
if err != nil { if err != nil {
return false, err return false, err
} }
switch r { if r == "true" || r == "1" {
case "true", "1":
return true, nil return true, nil
case "false", "0": } else if r == "false" || r == "0" {
return false, nil return false, nil
default: } else {
return false, fmt.Errorf("cannot parse as a boolean") return false, fmt.Errorf("cannot parse as a boolean")
} }
} }
@ -385,53 +384,3 @@ func SetKeyValueStringMap(bkt *bolt.Bucket, kvMap map[string]string) error {
} }
return SetKeyValueMap(bkt, chain) 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
}