Fixed bug in 'GetValue'

This commit is contained in:
Brian Buller 2016-03-31 08:40:37 -05:00
parent ac3cdddaea
commit 96c0d8c0c2

View File

@ -31,11 +31,10 @@ type DB struct {
func Create(filename string) (*DB, error) { func Create(filename string) (*DB, error) {
var err error var err error
b := DB{localFile: filename} b := DB{localFile: filename}
b.localDB, err = bolt.Open(filename, 0644, nil) if err = b.Open(); err != nil {
if err != nil {
return nil, err return nil, err
} }
defer b.localDB.Close() defer b.Close()
// Go ahead and make sure it's fresh // Go ahead and make sure it's fresh
b.RefreshDB() b.RefreshDB()
return &b, nil return &b, nil
@ -79,15 +78,13 @@ func (b *DB) Offline() {
// 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 {
var err error var err error
/* if !b.isOpen {
if !b.isOpen { err = b.Open()
err = b.Open() if err != nil {
if err != nil { return err
return err
}
defer b.Close()
} }
*/ defer b.Close()
}
b.RefreshDB() b.RefreshDB()
err = b.localDB.Update(func(tx *bolt.Tx) error { err = b.localDB.Update(func(tx *bolt.Tx) error {
var err error var err error
@ -125,30 +122,27 @@ 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
/* if !b.isOpen {
if !b.isOpen { err = b.Open()
err = b.Open() if err != nil {
if err != nil { return ret, err
return ret, err
}
defer b.Close()
} }
*/ defer b.Close()
}
b.RefreshDB() b.RefreshDB()
b.localDB.View(func(tx *bolt.Tx) error { b.localDB.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
@ -158,15 +152,13 @@ func (b *DB) GetValue(path []string, key string) (string, error) {
// 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 {
var err error var err error
/* if !b.isOpen {
if !b.isOpen { err = b.Open()
err = b.Open() if err != nil {
if err != nil { return err
return err
}
defer b.Close()
} }
*/ defer b.Close()
}
b.RefreshDB() b.RefreshDB()
err = b.MkBucketPath(path) err = b.MkBucketPath(path)
if err != nil { if err != nil {
@ -250,15 +242,13 @@ 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
/* if !b.isOpen {
if !b.isOpen { err = b.Open()
err = b.Open() if err != nil {
if err != nil { return ret, err
return ret, err
}
defer b.Close()
} }
*/ defer b.Close()
}
b.RefreshDB() b.RefreshDB()
err = b.localDB.Update(func(tx *bolt.Tx) error { err = b.localDB.Update(func(tx *bolt.Tx) error {
bkt := tx.Bucket([]byte(path[0])) bkt := tx.Bucket([]byte(path[0]))
@ -294,15 +284,13 @@ 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
/* if !b.isOpen {
if !b.isOpen { err = b.Open()
err = b.Open() if err != nil {
if err != nil { return ret, err
return ret, err
}
defer b.Close()
} }
*/ defer b.Close()
}
b.RefreshDB() b.RefreshDB()
err = b.localDB.Update(func(tx *bolt.Tx) error { err = b.localDB.Update(func(tx *bolt.Tx) error {
bkt := tx.Bucket([]byte(path[0])) bkt := tx.Bucket([]byte(path[0]))
@ -335,15 +323,13 @@ 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
/* if !b.isOpen {
if !b.isOpen { err = b.Open()
err = b.Open() if err != nil {
if err != nil { return err
return err
}
defer b.Close()
} }
*/ defer b.Close()
}
b.RefreshDB() b.RefreshDB()
err = b.localDB.Update(func(tx *bolt.Tx) error { err = b.localDB.Update(func(tx *bolt.Tx) error {
bkt := tx.Bucket([]byte(path[0])) bkt := tx.Bucket([]byte(path[0]))
@ -373,15 +359,13 @@ 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
/* if !b.isOpen {
if !b.isOpen { err = b.Open()
err = b.Open() if err != nil {
if err != nil { return err
return err
}
defer b.Close()
} }
*/ defer b.Close()
}
b.RefreshDB() b.RefreshDB()
err = b.localDB.Update(func(tx *bolt.Tx) error { err = b.localDB.Update(func(tx *bolt.Tx) error {
bkt := tx.Bucket([]byte(path[0])) bkt := tx.Bucket([]byte(path[0]))
@ -408,15 +392,13 @@ func (b *DB) DeleteBucket(path []string, key string) error {
// RefreshDB makes sure that the DB is fresh with the server version // RefreshDB makes sure that the DB is fresh with the server version
func (b *DB) RefreshDB() error { func (b *DB) RefreshDB() error {
var err error var err error
/* if !b.isOpen {
if !b.isOpen { err = b.Open()
err = b.Open() if err != nil {
if err != nil { return err
return err
}
defer b.Close()
} }
*/ defer b.Close()
}
if b.online { if b.online {
// TODO: Compare latest change here with lates on server // TODO: Compare latest change here with lates on server
// Then sync, if needed // Then sync, if needed