diff --git a/bucket_funcs.go b/bucket_funcs.go index 83a1e0c..8b1a3d9 100644 --- a/bucket_funcs.go +++ b/bucket_funcs.go @@ -79,7 +79,7 @@ func GetBucket(bkt *bolt.Bucket, path ...string) (*bolt.Bucket, 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])) + ret, err = ret.CreateBucketIfNotExists([]byte(path[idx])) if err != nil { return nil, err } @@ -102,6 +102,24 @@ func SetString(bkt *bolt.Bucket, key, val string) error { 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") @@ -252,7 +270,7 @@ func SetStringList(bkt *bolt.Bucket, val []string) error { return nil } -func AddToStringList(bkt *bolt.Bucket, key string, values ...string) error { +func AddToStringList(bkt *bolt.Bucket, values ...string) error { if bkt == nil { return fmt.Errorf("bucket must not be nil") } @@ -271,6 +289,51 @@ func AddToStringList(bkt *bolt.Bucket, key string, values ...string) error { 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 {