Compare commits
7 Commits
main
...
struct-tag
Author | SHA1 | Date | |
---|---|---|---|
638cf4ded7 | |||
c64bda66f5 | |||
ac4d008f57 | |||
7c152f1273 | |||
de7d97d96b | |||
0e0dff859f | |||
0ac08f775e |
4
.gitignore
vendored
4
.gitignore
vendored
@ -19,6 +19,4 @@ _cgo_export.*
|
||||
|
||||
_testmain.go
|
||||
|
||||
*.exe
|
||||
|
||||
example/
|
||||
*.exe
|
189
bolteasable.go
Normal file
189
bolteasable.go
Normal file
@ -0,0 +1,189 @@
|
||||
package boltease
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type any interface{}
|
||||
|
||||
func (b *DB) Load(path []string, dest any) error {
|
||||
destValue := reflect.ValueOf(dest)
|
||||
fmt.Println("Loading:", path, destValue)
|
||||
if destValue.Kind() != reflect.Pointer {
|
||||
return errors.New("Destination must be a pointer")
|
||||
}
|
||||
d := reflect.Indirect(destValue)
|
||||
fmt.Println(">> d.Kind() ->", d.Kind())
|
||||
if d.Kind() != reflect.Struct {
|
||||
fmt.Println(">> Kind != Struct; GetForInterface")
|
||||
path, name := path[:len(path)-1], path[len(path)-1]
|
||||
return b.GetForInterface(path, name, dest)
|
||||
}
|
||||
|
||||
entityType := reflect.TypeOf(dest).Elem()
|
||||
var ret error
|
||||
for i := 0; i < entityType.NumField(); i++ {
|
||||
structFld := entityType.Field(i)
|
||||
fldName := FieldName(structFld)
|
||||
fmt.Println("Loading Field:", fldName, "->", structFld.Type.Kind())
|
||||
switch structFld.Type.Kind() {
|
||||
case reflect.Pointer, reflect.Struct:
|
||||
fmt.Println("Getting Pointer or Struct")
|
||||
var wrk interface{}
|
||||
var wrkV reflect.Value
|
||||
if structFld.Type.Kind() == reflect.Pointer {
|
||||
fmt.Println(" It's a pointer to something")
|
||||
wrkV = reflect.New(structFld.Type).Elem()
|
||||
} else {
|
||||
fmt.Println(" It's a struct itself")
|
||||
wrkV = reflect.New(reflect.TypeOf(structFld))
|
||||
}
|
||||
wrk = wrkV.Interface()
|
||||
fmt.Println("-> Recurse (struct)", reflect.TypeOf(wrk))
|
||||
err := b.Load(append(path, fldName), &wrk)
|
||||
if err != nil {
|
||||
fmt.Println("-> -> Error loading.", err.Error())
|
||||
if ret == nil {
|
||||
ret = err
|
||||
}
|
||||
} else {
|
||||
// Set the value
|
||||
reflect.ValueOf(dest).Elem().FieldByName(structFld.Name).Set(reflect.ValueOf(wrk))
|
||||
}
|
||||
|
||||
case reflect.String:
|
||||
fmt.Println("Getting String")
|
||||
var wrk string
|
||||
err := b.GetForInterface(path, fldName, &wrk)
|
||||
if err != nil {
|
||||
if ret == nil {
|
||||
ret = err
|
||||
}
|
||||
} else {
|
||||
// Set the value
|
||||
reflect.ValueOf(dest).Elem().FieldByName(structFld.Name).Set(reflect.ValueOf(wrk))
|
||||
}
|
||||
|
||||
case reflect.Bool:
|
||||
fmt.Println("Getting Boolean")
|
||||
var wrk bool
|
||||
err := b.GetForInterface(path, fldName, &wrk)
|
||||
if err != nil {
|
||||
if ret == nil {
|
||||
ret = err
|
||||
}
|
||||
} else {
|
||||
// Set the value
|
||||
reflect.ValueOf(dest).Elem().FieldByName(structFld.Name).Set(reflect.ValueOf(wrk))
|
||||
}
|
||||
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
fmt.Println("Getting Integer")
|
||||
var wrk int
|
||||
err := b.GetForInterface(path, fldName, &wrk)
|
||||
if err != nil {
|
||||
if ret == nil {
|
||||
ret = err
|
||||
}
|
||||
} else {
|
||||
// Set the value
|
||||
reflect.ValueOf(dest).Elem().FieldByName(structFld.Name).Set(reflect.ValueOf(wrk))
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
setField := reflect.ValueOf(dest).Elem().Field(i)
|
||||
value := entityType.Field(i)
|
||||
fldName := FieldName(value)
|
||||
if value.Type.Kind() == reflect.Struct {
|
||||
b.LoadStruct(append(path, fldName), setField.Interface())
|
||||
} else {
|
||||
err := b.GetForInterface(path, fldName, &value)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
fmt.Println("value:", value)
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
if value.Type.Kind() == reflect.Struct {
|
||||
b.LoadStruct(append(path, fldName), setField.Interface())
|
||||
} else {
|
||||
v, err := b.GetForType(path, fldName, value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
setField.Set(reflect.ValueOf(v))
|
||||
}
|
||||
*/
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func (b *DB) Save(path []string, src any) error {
|
||||
t := reflect.TypeOf(src)
|
||||
if t.Kind() == reflect.Pointer {
|
||||
// Save the actual struct
|
||||
elem := reflect.ValueOf(src).Elem()
|
||||
return b.Save(path, elem.Interface())
|
||||
}
|
||||
|
||||
if t.Kind() == reflect.Struct {
|
||||
fields := reflect.VisibleFields(t)
|
||||
r := reflect.ValueOf(src)
|
||||
for _, fld := range fields {
|
||||
f := r.FieldByName(fld.Name)
|
||||
if (f.Kind() == reflect.Struct || f.Kind() == reflect.Pointer) && f != src {
|
||||
if f.CanInterface() {
|
||||
err := b.Save(append(path, FieldName(fld)), f.Interface())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
err := b.Save(append(path, FieldName(fld)), reflect.Indirect(f))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if err := b.Set(path, FieldName(fld), f); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return b.Set(path[:len(path)-1], path[len(path)-1], src)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func FieldName(fld reflect.StructField) string {
|
||||
nm := fld.Name
|
||||
tag := fld.Tag.Get("boltease")
|
||||
if tag != "" {
|
||||
nm = tag
|
||||
}
|
||||
return nm
|
||||
}
|
||||
func FieldIgnored(fld reflect.StructField) bool {
|
||||
return fld.Tag.Get("boltease") == "-"
|
||||
}
|
||||
|
||||
func ReflectValueToInterface(val reflect.Value) interface{} {
|
||||
switch val.Kind() {
|
||||
case reflect.Pointer:
|
||||
return ReflectValueToInterface(reflect.Indirect(val))
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return val.Int()
|
||||
case reflect.Bool:
|
||||
return val.Bool()
|
||||
case reflect.String:
|
||||
return val.String()
|
||||
default:
|
||||
return val.Bytes()
|
||||
}
|
||||
}
|
263
boltease.go
263
boltease.go
@ -2,9 +2,9 @@ package boltease
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@ -35,92 +35,6 @@ func Create(fn string, m os.FileMode, opts *bolt.Options) (*DB, error) {
|
||||
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 {
|
||||
if b.dbIsOpen {
|
||||
// DB is already open, that's fine.
|
||||
@ -189,16 +103,87 @@ func (b *DB) MkBucketPath(path []string) error {
|
||||
|
||||
func (b *DB) Set(path []string, key string, val interface{}) error {
|
||||
switch v := val.(type) {
|
||||
case reflect.Value:
|
||||
return b.Set(path, key, ReflectValueToInterface(v))
|
||||
case string:
|
||||
return b.SetString(path, key, v)
|
||||
case int:
|
||||
return b.SetInt(path, key, v)
|
||||
case int8:
|
||||
return b.SetInt(path, key, int(v))
|
||||
case int16:
|
||||
return b.SetInt(path, key, int(v))
|
||||
case int32:
|
||||
return b.SetInt(path, key, int(v))
|
||||
case int64:
|
||||
return b.SetInt(path, key, int(v))
|
||||
case bool:
|
||||
return b.SetBool(path, key, v)
|
||||
case []byte:
|
||||
return b.SetBytes(path, key, v)
|
||||
default:
|
||||
return fmt.Errorf("Unknown Data Type: %v", v)
|
||||
}
|
||||
return errors.New("unknown data type")
|
||||
}
|
||||
func (b *DB) GetForInterface(path []string, key string, val interface{}) error {
|
||||
var err error
|
||||
switch v := val.(type) {
|
||||
case *string:
|
||||
*v, err = b.GetString(path, key)
|
||||
case *int:
|
||||
*v, err = b.GetInt(path, key)
|
||||
case *int8:
|
||||
var wrk int
|
||||
wrk, err = b.GetInt(path, key)
|
||||
*v = int8(wrk)
|
||||
case *int16:
|
||||
var wrk int
|
||||
wrk, err = b.GetInt(path, key)
|
||||
*v = int16(wrk)
|
||||
case *int32:
|
||||
var wrk int
|
||||
wrk, err = b.GetInt(path, key)
|
||||
*v = int32(wrk)
|
||||
case *int64:
|
||||
var wrk int
|
||||
wrk, err = b.GetInt(path, key)
|
||||
*v = int64(wrk)
|
||||
case *bool:
|
||||
*v, err = b.GetBool(path, key)
|
||||
case *[]byte:
|
||||
*v, err = b.GetBytes(path, key)
|
||||
default:
|
||||
return fmt.Errorf("Unknown Data Type: %v", v)
|
||||
}
|
||||
return err
|
||||
}
|
||||
func (b *DB) LoadInto(path []string, key string, into interface{}) error {
|
||||
var err error
|
||||
switch v := into.(type) {
|
||||
case string:
|
||||
i := new(string)
|
||||
*i, err = b.GetString(path, key)
|
||||
fmt.Printf("Loading String: %v\n", i)
|
||||
into = i
|
||||
case int, int8, int16, int32, int64:
|
||||
i := new(int)
|
||||
*i, err = b.GetInt(path, key)
|
||||
fmt.Printf("Loading Int: %v\n", i)
|
||||
into = i
|
||||
case bool:
|
||||
i := new(bool)
|
||||
*i, err = b.GetBool(path, key)
|
||||
fmt.Printf("Loading Bool: %v\n", i)
|
||||
into = i
|
||||
case []byte:
|
||||
i := new([]byte)
|
||||
*i, err = b.GetBytes(path, key)
|
||||
fmt.Printf("Loading []byte: %v\n", i)
|
||||
into = i
|
||||
default:
|
||||
return fmt.Errorf("Unknown Data Type: %v", v)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (b *DB) GetBytes(path []string, key string) ([]byte, error) {
|
||||
@ -213,18 +198,18 @@ func (b *DB) GetBytes(path []string, key string) ([]byte, error) {
|
||||
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])
|
||||
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], "/"))
|
||||
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))
|
||||
if val == nil {
|
||||
return fmt.Errorf("couldn't find value")
|
||||
return fmt.Errorf("Couldn't find value")
|
||||
}
|
||||
ret = make([]byte, len(val))
|
||||
copy(ret, val)
|
||||
@ -252,7 +237,7 @@ func (b *DB) SetBytes(path []string, key string, val []byte) error {
|
||||
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])
|
||||
return fmt.Errorf("Couldn't find bucket " + path[0])
|
||||
}
|
||||
for idx := 1; idx < len(path); idx++ {
|
||||
bkt, err = bkt.CreateBucketIfNotExists([]byte(path[idx]))
|
||||
@ -281,15 +266,15 @@ func (b *DB) GetString(path []string, key string) (string, error) {
|
||||
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])
|
||||
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], "/"))
|
||||
return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx+1], "/"))
|
||||
}
|
||||
}
|
||||
// bkt should have the last bucket in the path
|
||||
// newBkt should have the last bucket in the path
|
||||
ret = string(bkt.Get([]byte(key)))
|
||||
return nil
|
||||
})
|
||||
@ -314,7 +299,7 @@ func (b *DB) SetString(path []string, key, val string) error {
|
||||
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])
|
||||
return fmt.Errorf("Couldn't find bucket " + path[0])
|
||||
}
|
||||
for idx := 1; idx < len(path); idx++ {
|
||||
bkt, err = bkt.CreateBucketIfNotExists([]byte(path[idx]))
|
||||
@ -323,7 +308,7 @@ func (b *DB) SetString(path []string, key, val string) error {
|
||||
}
|
||||
}
|
||||
// bkt should have the last bucket in the path
|
||||
return SetString(bkt, key, val)
|
||||
return bkt.Put([]byte(key), []byte(val))
|
||||
})
|
||||
return err
|
||||
}
|
||||
@ -354,7 +339,7 @@ func (b *DB) GetBool(path []string, key string) (bool, error) {
|
||||
if r == "true" || r == "1" {
|
||||
ret = true
|
||||
} 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
|
||||
@ -397,14 +382,14 @@ func (b *DB) GetBucketList(path []string) ([]string, error) {
|
||||
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])
|
||||
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+1], " / "))
|
||||
return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx+1], " / "))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -436,14 +421,14 @@ func (b *DB) GetKeyList(path []string) ([]string, error) {
|
||||
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])
|
||||
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], " / "))
|
||||
return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx], " / "))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -474,14 +459,14 @@ func (b *DB) DeletePair(path []string, key string) error {
|
||||
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])
|
||||
return fmt.Errorf("Couldn't find bucket " + path[0])
|
||||
}
|
||||
if len(path) > 1 {
|
||||
var newBkt *bolt.Bucket
|
||||
for idx := 1; idx < len(path); idx++ {
|
||||
newBkt = bkt.Bucket([]byte(path[idx]))
|
||||
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
|
||||
@ -509,12 +494,12 @@ func (b *DB) DeleteBucket(path []string, key string) error {
|
||||
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])
|
||||
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], "/"))
|
||||
return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx], "/"))
|
||||
}
|
||||
}
|
||||
// bkt should have the last bucket in the path
|
||||
@ -541,14 +526,14 @@ func (b *DB) GetStringList(path []string) ([]string, error) {
|
||||
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])
|
||||
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+1], " / "))
|
||||
return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx+1], " / "))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -583,61 +568,7 @@ func (b *DB) SetStringList(path, values []string) error {
|
||||
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])
|
||||
return fmt.Errorf("Couldn't find bucket " + path[0])
|
||||
}
|
||||
for idx := 1; idx < len(path); idx++ {
|
||||
bkt, err = bkt.CreateBucketIfNotExists([]byte(path[idx]))
|
||||
@ -674,14 +605,14 @@ func (b *DB) GetKeyValueMap(path []string) (map[string][]byte, error) {
|
||||
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])
|
||||
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], " / "))
|
||||
return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx], " / "))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -729,7 +660,7 @@ func (b *DB) SetKeyValueMap(path []string, kvMap map[string][]byte) error {
|
||||
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])
|
||||
return fmt.Errorf("Couldn't find bucket " + path[0])
|
||||
}
|
||||
for idx := 1; idx < len(path); idx++ {
|
||||
bkt, err = bkt.CreateBucketIfNotExists([]byte(path[idx]))
|
||||
|
323
bucket_funcs.go
323
bucket_funcs.go
@ -1,323 +0,0 @@
|
||||
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 = bkt.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 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, key string, 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 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)
|
||||
}
|
2
example/.gitignore
vendored
Normal file
2
example/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
example
|
||||
example.db
|
136
example/main.go
Normal file
136
example/main.go
Normal file
@ -0,0 +1,136 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.bullercodeworks.com/brian/boltease"
|
||||
)
|
||||
|
||||
func main() {
|
||||
//example1()
|
||||
//fmt.Println()
|
||||
example2()
|
||||
}
|
||||
|
||||
func example1() {
|
||||
fmt.Println("# Example 1")
|
||||
db, err := boltease.Create("example.db", 0600, nil)
|
||||
if err != nil {
|
||||
fmt.Printf("Error Opening File: %s\n", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println("## Saving Struct")
|
||||
err = db.Save(
|
||||
[]string{"examples", "example1"},
|
||||
ExampleType{
|
||||
Name: "Example 1",
|
||||
Age: 5,
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Printf("Error saving struct: %s\n", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println("## Example 1-1: Simple")
|
||||
var v string
|
||||
err = db.GetForInterface([]string{"examples", "example1"}, "name", &v)
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err.Error())
|
||||
}
|
||||
fmt.Println("Name:", v)
|
||||
var age int
|
||||
err = db.GetForInterface([]string{"examples", "example1"}, "age", &age)
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err.Error())
|
||||
}
|
||||
fmt.Println("Age:", age)
|
||||
fmt.Println("")
|
||||
|
||||
fmt.Println("## Example 1-2: LoadStruct, simple")
|
||||
var name string
|
||||
err = db.Load(
|
||||
[]string{"examples", "example1", "name"},
|
||||
&name,
|
||||
)
|
||||
fmt.Println("Name:", name)
|
||||
if err != nil {
|
||||
fmt.Println("Err:", err)
|
||||
}
|
||||
fmt.Println("")
|
||||
|
||||
fmt.Println("## Example 1-3: Struct")
|
||||
fmt.Println("Loading into Struct")
|
||||
newStruct := ExampleType{}
|
||||
err = db.Load(
|
||||
[]string{"examples", "example1"},
|
||||
&newStruct,
|
||||
)
|
||||
fmt.Println(newStruct)
|
||||
}
|
||||
|
||||
func example2() {
|
||||
fmt.Println("# Example 2")
|
||||
db, err := boltease.Create("example.db", 0600, nil)
|
||||
if err != nil {
|
||||
fmt.Printf("Error Opening File: %s\n", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println("## Saving Struct")
|
||||
num := 12345
|
||||
err = db.Save(
|
||||
[]string{"examples", "example2"},
|
||||
&ExampleType2{
|
||||
Name: "Example 2",
|
||||
Age: 20,
|
||||
SubTypePtr: &ExampleSubType{
|
||||
SubName: "Example SubType Pointer",
|
||||
},
|
||||
SubType: ExampleSubType{
|
||||
SubName: "Example SubType",
|
||||
},
|
||||
Number: &num,
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Printf("Error saving struct: %s\n", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
newStruct := ExampleType2{}
|
||||
err = db.Load([]string{"examples", "example2"}, &newStruct)
|
||||
fmt.Println(newStruct)
|
||||
}
|
||||
|
||||
type ExampleType struct {
|
||||
Name string `boltease:"name"`
|
||||
Age int `boltease:"age"`
|
||||
}
|
||||
|
||||
func (e ExampleType) String() string {
|
||||
r, _ := json.Marshal(e)
|
||||
return string(r)
|
||||
}
|
||||
|
||||
type ExampleType2 struct {
|
||||
Name string `boltease:"name"`
|
||||
Age int `boltease:"age"`
|
||||
SubTypePtr *ExampleSubType `boltease:"subtypeptr"`
|
||||
SubType ExampleSubType `boltease:"subtype"`
|
||||
Number *int `boltease:"number"`
|
||||
}
|
||||
|
||||
func (e ExampleType2) String() string {
|
||||
r, _ := json.Marshal(e)
|
||||
return string(r)
|
||||
}
|
||||
|
||||
type ExampleSubType struct {
|
||||
SubName string `boltease:"subname"`
|
||||
}
|
||||
|
||||
func (e ExampleSubType) String() string {
|
||||
r, _ := json.Marshal(e)
|
||||
return string(r)
|
||||
}
|
Loading…
Reference in New Issue
Block a user