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) {
var err error
b := DB{localFile: filename}
b.localDB, err = bolt.Open(filename, 0644, nil)
if err != nil {
if err = b.Open(); err != nil {
return nil, err
}
defer b.localDB.Close()
defer b.Close()
// Go ahead and make sure it's fresh
b.RefreshDB()
return &b, nil
@ -79,7 +78,6 @@ func (b *DB) Offline() {
// MkBucketPath builds all buckets in the string slice
func (b *DB) MkBucketPath(path []string) error {
var err error
/*
if !b.isOpen {
err = b.Open()
if err != nil {
@ -87,7 +85,6 @@ func (b *DB) MkBucketPath(path []string) error {
}
defer b.Close()
}
*/
b.RefreshDB()
err = b.localDB.Update(func(tx *bolt.Tx) error {
var err error
@ -125,7 +122,6 @@ func (b *DB) MkBucketPath(path []string) error {
func (b *DB) GetValue(path []string, key string) (string, error) {
var err error
var ret string
/*
if !b.isOpen {
err = b.Open()
if err != nil {
@ -133,22 +129,20 @@ func (b *DB) GetValue(path []string, key string) (string, error) {
}
defer b.Close()
}
*/
b.RefreshDB()
b.localDB.View(func(tx *bolt.Tx) error {
bkt := tx.Bucket([]byte(path[0]))
if bkt == nil {
return fmt.Errorf("Couldn't find bucket " + path[0])
}
var newBkt *bolt.Bucket
for idx := 1; idx < len(path); idx++ {
newBkt = bkt.Bucket([]byte(path[idx]))
if newBkt == nil {
bkt = bkt.Bucket([]byte(path[idx]))
if bkt == nil {
return fmt.Errorf("Couldn't find bucket " + strings.Join(path[:idx], "/"))
}
}
// newBkt should have the last bucket in the path
ret = string(newBkt.Get([]byte(key)))
ret = string(bkt.Get([]byte(key)))
return nil
})
return ret, err
@ -158,7 +152,6 @@ func (b *DB) GetValue(path []string, key string) (string, error) {
// path is a slice of tokens
func (b *DB) SetValue(path []string, key, val string) error {
var err error
/*
if !b.isOpen {
err = b.Open()
if err != nil {
@ -166,7 +159,6 @@ func (b *DB) SetValue(path []string, key, val string) error {
}
defer b.Close()
}
*/
b.RefreshDB()
err = b.MkBucketPath(path)
if err != nil {
@ -250,7 +242,6 @@ func (b *DB) SetTimestamp(path []string, key string, val time.Time) error {
func (b *DB) GetBucketList(path []string) ([]string, error) {
var err error
var ret []string
/*
if !b.isOpen {
err = b.Open()
if err != nil {
@ -258,7 +249,6 @@ func (b *DB) GetBucketList(path []string) ([]string, error) {
}
defer b.Close()
}
*/
b.RefreshDB()
err = b.localDB.Update(func(tx *bolt.Tx) error {
bkt := tx.Bucket([]byte(path[0]))
@ -294,7 +284,6 @@ func (b *DB) GetBucketList(path []string) ([]string, error) {
func (b *DB) GetKeyList(path []string) ([]string, error) {
var err error
var ret []string
/*
if !b.isOpen {
err = b.Open()
if err != nil {
@ -302,7 +291,6 @@ func (b *DB) GetKeyList(path []string) ([]string, error) {
}
defer b.Close()
}
*/
b.RefreshDB()
err = b.localDB.Update(func(tx *bolt.Tx) error {
bkt := tx.Bucket([]byte(path[0]))
@ -335,7 +323,6 @@ func (b *DB) GetKeyList(path []string) ([]string, error) {
// DeletePair deletes the pair with key at path
func (b *DB) DeletePair(path []string, key string) error {
var err error
/*
if !b.isOpen {
err = b.Open()
if err != nil {
@ -343,7 +330,6 @@ func (b *DB) DeletePair(path []string, key string) error {
}
defer b.Close()
}
*/
b.RefreshDB()
err = b.localDB.Update(func(tx *bolt.Tx) error {
bkt := tx.Bucket([]byte(path[0]))
@ -373,7 +359,6 @@ func (b *DB) DeletePair(path []string, key string) error {
// DeleteBucket deletes the bucket key at path
func (b *DB) DeleteBucket(path []string, key string) error {
var err error
/*
if !b.isOpen {
err = b.Open()
if err != nil {
@ -381,7 +366,6 @@ func (b *DB) DeleteBucket(path []string, key string) error {
}
defer b.Close()
}
*/
b.RefreshDB()
err = b.localDB.Update(func(tx *bolt.Tx) error {
bkt := tx.Bucket([]byte(path[0]))
@ -408,7 +392,6 @@ func (b *DB) DeleteBucket(path []string, key string) error {
// RefreshDB makes sure that the DB is fresh with the server version
func (b *DB) RefreshDB() error {
var err error
/*
if !b.isOpen {
err = b.Open()
if err != nil {
@ -416,7 +399,6 @@ func (b *DB) RefreshDB() error {
}
defer b.Close()
}
*/
if b.online {
// TODO: Compare latest change here with lates on server
// Then sync, if needed