Clean up a bit

This commit is contained in:
Brian Buller 2016-10-10 09:29:41 -05:00
parent afaa7b518b
commit e17c81af9d
1 changed files with 104 additions and 25 deletions

View File

@ -2,6 +2,7 @@ package boltease
import ( import (
"fmt" "fmt"
"os"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -13,25 +14,62 @@ import (
// DB is a struct for accomplishing this // DB is a struct for accomplishing this
type DB struct { type DB struct {
localFile string filename string
localDB *bolt.DB boltDB *bolt.DB
mode os.FileMode
options *bolt.Options
dbIsOpen bool
} }
// Open returns the DB object // Create makes sure we can get open the file and returns the DB object
func Open(filename string) (*DB, error) { func Create(fn string, m os.FileMode, opts *bolt.Options) (*DB, error) {
var err error var err error
b := DB{localFile: filename} b := DB{filename: fn, mode: m, options: opts}
b.localDB, err = bolt.Open(filename, 0644, nil) b.boltDB, err = bolt.Open(fn, m, opts)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Go ahead and make sure it's fresh defer b.boltDB.Close()
return &b, nil return &b, nil
} }
func (b *DB) OpenDB() error {
if b.dbIsOpen {
// DB is already open, that's fine.
return nil
}
var err error
if b.boltDB, err = bolt.Open(b.filename, b.mode, b.options); err != nil {
return err
}
b.dbIsOpen = true
return err
}
func (b *DB) CloseDB() error {
if !b.dbIsOpen {
// DB is already closed, that's fine.
return nil
}
var err error
if err = b.boltDB.Close(); err != nil {
return err
}
b.dbIsOpen = false
return err
}
// MkBucketPath builds all buckets in the string slice // MkBucketPath builds all buckets in the string slice
func (b *DB) MkBucketPath(path []string) error { func (b *DB) MkBucketPath(path []string) error {
err := b.localDB.Update(func(tx *bolt.Tx) error { var err error
if !b.dbIsOpen {
if err = b.OpenDB(); err != nil {
return err
}
defer b.CloseDB()
}
err = b.boltDB.Update(func(tx *bolt.Tx) error {
var err error var err error
bkt := tx.Bucket([]byte(path[0])) bkt := tx.Bucket([]byte(path[0]))
if bkt == nil { if bkt == nil {
@ -67,20 +105,25 @@ func (b *DB) MkBucketPath(path []string) error {
func (b *DB) GetValue(path []string, key string) (string, error) { func (b *DB) GetValue(path []string, key string) (string, error) {
var err error var err error
var ret string var ret string
b.localDB.View(func(tx *bolt.Tx) error { 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])) 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 newBkt *bolt.Bucket
for idx := 1; idx < len(path); idx++ { for idx := 1; idx < len(path); idx++ {
newBkt = bkt.Bucket([]byte(path[idx])) bkt = bkt.Bucket([]byte(path[idx]))
if newBkt == 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], "/"))
} }
} }
// newBkt should have the last bucket in the path // newBkt should have the last bucket in the path
ret = string(newBkt.Get([]byte(key))) ret = string(bkt.Get([]byte(key)))
return nil return nil
}) })
return ret, err return ret, err
@ -89,23 +132,31 @@ func (b *DB) GetValue(path []string, key string) (string, error) {
// SetValue sets the value of key at path to val // SetValue sets the value of key at path to val
// path is a slice of tokens // path is a slice of tokens
func (b *DB) SetValue(path []string, key, val string) error { func (b *DB) SetValue(path []string, key, val string) error {
err := b.MkBucketPath(path) var err error
if !b.dbIsOpen {
if err = b.OpenDB(); err != nil {
return err
}
defer b.CloseDB()
}
err = b.MkBucketPath(path)
if err != nil { if err != nil {
return err return err
} }
b.localDB.Update(func(tx *bolt.Tx) error { err = b.boltDB.Update(func(tx *bolt.Tx) error {
newBkt := tx.Bucket([]byte(path[0])) bkt := tx.Bucket([]byte(path[0]))
if newBkt == 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++ {
newBkt, err = newBkt.CreateBucketIfNotExists([]byte(path[idx])) bkt, err = bkt.CreateBucketIfNotExists([]byte(path[idx]))
if err != nil { if err != nil {
return err return err
} }
} }
// newBkt should have the last bucket in the path // bkt should have the last bucket in the path
return newBkt.Put([]byte(key), []byte(val)) return bkt.Put([]byte(key), []byte(val))
}) })
return err return err
} }
@ -160,7 +211,7 @@ func (b *DB) GetTimestamp(path []string, key string) (time.Time, error) {
return time.Unix(0, 0), err return time.Unix(0, 0), err
} }
// SetTimestamp returns the value at 'path' // 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.SetValue(path, key, val.Format(time.RFC3339))
} }
@ -169,7 +220,14 @@ func (b *DB) SetTimestamp(path []string, key string, val time.Time) error {
func (b *DB) GetBucketList(path []string) ([]string, error) { func (b *DB) GetBucketList(path []string) ([]string, error) {
var err error var err error
var ret []string var ret []string
err = b.localDB.Update(func(tx *bolt.Tx) error { 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])) 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])
@ -203,7 +261,14 @@ func (b *DB) GetBucketList(path []string) ([]string, error) {
func (b *DB) GetKeyList(path []string) ([]string, error) { func (b *DB) GetKeyList(path []string) ([]string, error) {
var err error var err error
var ret []string var ret []string
err = b.localDB.Update(func(tx *bolt.Tx) error { 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])) 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])
@ -234,7 +299,14 @@ func (b *DB) GetKeyList(path []string) ([]string, error) {
// DeletePair deletes the pair with key at path // DeletePair deletes the pair with key at path
func (b *DB) DeletePair(path []string, key string) error { func (b *DB) DeletePair(path []string, key string) error {
var err error var err error
err = b.localDB.Update(func(tx *bolt.Tx) error { if !b.dbIsOpen {
if err = b.OpenDB(); err != nil {
return err
}
defer b.CloseDB()
}
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])
@ -262,7 +334,14 @@ 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 {
var err error var err error
err = b.localDB.Update(func(tx *bolt.Tx) error { if !b.dbIsOpen {
if err = b.OpenDB(); err != nil {
return err
}
defer b.CloseDB()
}
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])