Let DB be closed when not in use

This commit is contained in:
Brian Buller 2016-03-01 22:28:30 -06:00
parent 24ca043895
commit a617c771d9
1 changed files with 16 additions and 2 deletions

View File

@ -23,22 +23,36 @@ type DB struct {
apiToken string apiToken string
remoteMD5 string remoteMD5 string
online bool online bool
isOpen bool
localDB *bolt.DB localDB *bolt.DB
} }
// Open returns the DB object // Create returns the DB object
func Open(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) b.localDB, err = bolt.Open(filename, 0644, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer b.localDB.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
} }
// Open opens the DB so things can be done
func (b *DB) Open() error {
var err error
b.localDB, err = Open(b.localFile, 0644, nil)
return err
}
// Close closes the DB
func (b *DB) Close() error {
return b.localDB.Close()
}
// Offline sets this DB to offline mode // Offline sets this DB to offline mode
// That means it won't try to sync anything // That means it won't try to sync anything
func (b *DB) Offline() { func (b *DB) Offline() {