package gime import ( "errors" "github.com/br0xen/boltease" ) type GimeDB struct { db *boltease.DB archDb *boltease.DB dbOpened int dbArchOpened int path string filename, arch string } const ( TypeCurrent = "current" TypeRecent = "recent" TypeArchive = "archive" TypeUnknown = "unknown" // Not really a type, used for searches and stuff ) const AllTypes = []string{TypeCurrent, TypeRecent, TypeArchive} // Load Database returns a database loaded off the files given // name and archName located in path func LoadDatabase(path, name, archName string) (*GimeDB, error) { if path[len(path)-1] != '/' { path = path + "/" } gdb := GimeDB{ path: path, filename: name, arch: archName, } gdb.initDatabase() return &gdb, nil } // Load a TimeEntry collection from a database func (gdb *GimeDB) LoadTimeEntryCollection(tp string) *TimeEntryCollection { ret := new(TimeEntryCollection) entries := gdb.dbGetAllTimeEntries(tp) for i := range entries { ret.Push(&entries[i]) } return ret } func (gdb *GimeDB) openDatabase() error { gdb.dbOpened += 1 if gdb.dbOpened == 1 { var err error gdb.db, err = boltease.Create(gdb.path+gdb.filename, 0600, nil) if err != nil { return err } } return nil } func (gdb *GimeDB) closeDatabase() error { gdb.dbOpened -= 1 if gdb.dbOpened == 0 { return gdb.db.CloseDB() } return nil } func (gdb *GimeDB) openArchiveDatabase() error { gdb.dbArchOpened += 1 if gdb.dbArchOpened == 1 { var err error gdb.archDb, err = boltease.Create(gdb.path+gdb.arch, 0600, nil) if err != nil { return err } } return nil } func (gdb *GimeDB) closeArchiveDatabase() error { gdb.dbArchOpened -= 1 if gdb.dbArchOpened == 0 { return gdb.archDb.CloseDB() } return nil } func (gdb *GimeDB) initDatabase() error { // Initialize the current/recent/settings database var err error if err = gdb.openDatabase(); err != nil { return err } defer gdb.closeDatabase() // Create the path to the bucket to store application settings if err := gdb.db.MkBucketPath([]string{"settings"}); err != nil { return err } // Create the path to the bucket to store the current time entry if err := gdb.db.MkBucketPath([]string{TypeCurrent}); err != nil { return err } // Create the path to the bucket to store recent time entries if err := gdb.db.MkBucketPath([]string{TypeRecent}); err != nil { return err } // Now initialize the Archive Database if err = gdb.openArchiveDatabase(); err != nil { return err } defer gdb.closeArchiveDatabase() // Create the path to the bucket to store archived time entries return gdb.archDb.MkBucketPath([]string{TypeArchive}) } func (gdb *GimeDB) openDBType(tp string) (*boltease.DB, error) { var err error if tp == TypeCurrent || tp == TypeRecent { if err = gdb.openDatabase(); err != nil { return nil, err } return gdb.db, err } else if tp == TypeArchive { if err = gdb.openArchiveDatabase(); err != nil { return nil, err } return gdb.archDb, err } return nil, errors.New("Invalid database type: " + tp) } func (gdb *GimeDB) closeDBType(tp string) error { if tp == TypeCurrent || tp == TypeRecent { return gdb.closeDatabase() } else if tp == TypeArchive { return gdb.closeArchiveDatabase() } return errors.New("Invalid database type: " + tp) }