package gime import ( "errors" "fmt" "time" "github.com/br0xen/boltease" ) type GimeDB struct { db *boltease.DB archDb *boltease.DB dbOpened int dbArchOpened int path string filename, arch string AllTypes []int } const ( TypeCurrent = 1 // 001 TypeRecent = 2 // 010 TypeNoArchive = 3 // 011 TypeArchive = 4 // 100 TypeAll = 7 // 111 ArchiveDays = time.Hour * 24 * 90 // Archive anything older than 90 days ) func TypeToString(tp int) string { switch tp { case TypeCurrent: return "current" case TypeRecent: return "recent" case TypeArchive: return "archive" } return "unknown" } // 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, AllTypes: []int{TypeCurrent, TypeRecent, TypeArchive}, } if err := gdb.initDatabase(); err != nil { fmt.Println(err.Error()) return nil, err } return &gdb, nil } // Load a TimeEntry collection from a database func (gdb *GimeDB) LoadTimeEntryCollection(tp int) *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{TypeToString(TypeCurrent)}); err != nil { return err } // Create the path to the bucket to store recent time entries if err := gdb.db.MkBucketPath([]string{TypeToString(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{TypeToString(TypeArchive)}) } func (gdb *GimeDB) openDBType(tp int) (*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: " + TypeToString(tp)) } func (gdb *GimeDB) closeDBType(tp int) error { if tp == TypeCurrent || tp == TypeRecent { return gdb.closeDatabase() } else if tp == TypeArchive { return gdb.closeArchiveDatabase() } return errors.New("Invalid database type: " + TypeToString(tp)) }