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,7 +78,6 @@ 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 {
@ -87,7 +85,6 @@ func (b *DB) MkBucketPath(path []string) error {
} }
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,7 +122,6 @@ 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 {
@ -133,22 +129,20 @@ func (b *DB) GetValue(path []string, key string) (string, error) {
} }
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,7 +152,6 @@ 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 {
@ -166,7 +159,6 @@ func (b *DB) SetValue(path []string, key, val string) error {
} }
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,7 +242,6 @@ 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 {
@ -258,7 +249,6 @@ func (b *DB) GetBucketList(path []string) ([]string, error) {
} }
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,7 +284,6 @@ 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 {
@ -302,7 +291,6 @@ func (b *DB) GetKeyList(path []string) ([]string, error) {
} }
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,7 +323,6 @@ 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 {
@ -343,7 +330,6 @@ func (b *DB) DeletePair(path []string, key string) error {
} }
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,7 +359,6 @@ 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 {
@ -381,7 +366,6 @@ func (b *DB) DeleteBucket(path []string, key string) error {
} }
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,7 +392,6 @@ 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 {
@ -416,7 +399,6 @@ func (b *DB) RefreshDB() error {
} }
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