Trying to auto-open & auto-close db...

Had to comment changes until fixed.
This commit is contained in:
Brian Buller 2016-03-02 07:50:32 -06:00
parent a617c771d9
commit 9129007194

View File

@ -44,13 +44,25 @@ func Create(filename string) (*DB, error) {
// Open opens the DB so things can be done // Open opens the DB so things can be done
func (b *DB) Open() error { func (b *DB) Open() error {
var err error var err error
b.localDB, err = Open(b.localFile, 0644, nil) if !b.isOpen {
b.localDB, err = bolt.Open(b.localFile, 0644, nil)
if err == nil {
b.isOpen = true
}
}
return err return err
} }
// Close closes the DB // Close closes the DB
func (b *DB) Close() error { func (b *DB) Close() error {
return b.localDB.Close() var err error
if b.isOpen {
err = b.localDB.Close()
if err == nil {
b.isOpen = false
}
}
return err
} }
// Offline sets this DB to offline mode // Offline sets this DB to offline mode
@ -61,8 +73,18 @@ 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
/*
if !b.isOpen {
err = b.Open()
if err != nil {
return err
}
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
bkt := tx.Bucket([]byte(path[0])) bkt := tx.Bucket([]byte(path[0]))
if bkt == nil { if bkt == nil {
@ -98,6 +120,15 @@ 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 {
err = b.Open()
if err != nil {
return ret, err
}
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]))
@ -121,8 +152,18 @@ func (b *DB) GetValue(path []string, key string) (string, error) {
// SetValue sets the value at path to val // SetValue sets the value 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 {
var err error
/*
if !b.isOpen {
err = b.Open()
if err != nil {
return err
}
defer b.Close()
}
*/
b.RefreshDB() b.RefreshDB()
err := b.MkBucketPath(path) err = b.MkBucketPath(path)
if err != nil { if err != nil {
return err return err
} }
@ -202,10 +243,18 @@ func (b *DB) SetTimestamp(path []string, key string, val time.Time) error {
// GetBucketList returns a list of all sub-buckets at path // GetBucketList returns a list of all sub-buckets at path
func (b *DB) GetBucketList(path []string) ([]string, error) { func (b *DB) GetBucketList(path []string) ([]string, error) {
b.RefreshDB()
var err error var err error
var ret []string var ret []string
/*
if !b.isOpen {
err = b.Open()
if err != nil {
return ret, err
}
defer b.Close()
}
*/
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]))
if bkt == nil { if bkt == nil {
@ -238,10 +287,18 @@ func (b *DB) GetBucketList(path []string) ([]string, error) {
// GetKeyList returns a list of all keys at path // GetKeyList returns a list of all keys at path
func (b *DB) GetKeyList(path []string) ([]string, error) { func (b *DB) GetKeyList(path []string) ([]string, error) {
b.RefreshDB()
var err error var err error
var ret []string var ret []string
/*
if !b.isOpen {
err = b.Open()
if err != nil {
return ret, err
}
defer b.Close()
}
*/
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]))
if bkt == nil { if bkt == nil {
@ -272,8 +329,17 @@ 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 {
b.RefreshDB()
var err error var err error
/*
if !b.isOpen {
err = b.Open()
if err != nil {
return err
}
defer b.Close()
}
*/
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]))
if bkt == nil { if bkt == nil {
@ -301,8 +367,17 @@ 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 {
b.RefreshDB()
var err error var err error
/*
if !b.isOpen {
err = b.Open()
if err != nil {
return err
}
defer b.Close()
}
*/
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]))
if bkt == nil { if bkt == nil {
@ -328,6 +403,15 @@ 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 {
err = b.Open()
if err != nil {
return err
}
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