2017-07-27 15:27:10 +00:00
|
|
|
package gime
|
2017-07-28 11:44:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-08-01 20:39:24 +00:00
|
|
|
"fmt"
|
2017-08-02 19:14:53 +00:00
|
|
|
"time"
|
2017-07-28 11:44:08 +00:00
|
|
|
|
|
|
|
"github.com/br0xen/boltease"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GimeDB struct {
|
|
|
|
db *boltease.DB
|
|
|
|
archDb *boltease.DB
|
|
|
|
dbOpened int
|
|
|
|
dbArchOpened int
|
|
|
|
path string
|
|
|
|
filename, arch string
|
2017-08-01 20:39:24 +00:00
|
|
|
|
2018-01-11 16:52:44 +00:00
|
|
|
AllTypes []int
|
2017-07-28 11:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2018-01-11 16:52:44 +00:00
|
|
|
TypeCurrent = 1 // 001
|
|
|
|
TypeRecent = 2 // 010
|
|
|
|
TypeNoArchive = 3 // 011
|
|
|
|
TypeArchive = 4 // 100
|
|
|
|
TypeAll = 7 // 111
|
2017-08-02 19:14:53 +00:00
|
|
|
|
|
|
|
ArchiveDays = time.Hour * 24 * 90 // Archive anything older than 90 days
|
2017-07-28 11:44:08 +00:00
|
|
|
)
|
|
|
|
|
2018-01-11 16:52:44 +00:00
|
|
|
func TypeToString(tp int) string {
|
|
|
|
switch tp {
|
|
|
|
case TypeCurrent:
|
|
|
|
return "current"
|
|
|
|
case TypeRecent:
|
|
|
|
return "recent"
|
|
|
|
case TypeArchive:
|
|
|
|
return "archive"
|
|
|
|
}
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
|
2017-07-28 11:44:08 +00:00
|
|
|
// 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,
|
2018-01-11 16:52:44 +00:00
|
|
|
AllTypes: []int{TypeCurrent, TypeRecent, TypeArchive},
|
2017-08-01 20:39:24 +00:00
|
|
|
}
|
|
|
|
if err := gdb.initDatabase(); err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return nil, err
|
2017-07-28 11:44:08 +00:00
|
|
|
}
|
|
|
|
return &gdb, nil
|
|
|
|
}
|
|
|
|
|
2017-07-31 12:00:16 +00:00
|
|
|
// Load a TimeEntry collection from a database
|
2018-01-11 16:52:44 +00:00
|
|
|
func (gdb *GimeDB) LoadTimeEntryCollection(tp int) *TimeEntryCollection {
|
2017-07-31 12:00:16 +00:00
|
|
|
ret := new(TimeEntryCollection)
|
|
|
|
entries := gdb.dbGetAllTimeEntries(tp)
|
|
|
|
for i := range entries {
|
|
|
|
ret.Push(&entries[i])
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2017-07-28 11:44:08 +00:00
|
|
|
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
|
2018-01-11 16:52:44 +00:00
|
|
|
if err := gdb.db.MkBucketPath([]string{TypeToString(TypeCurrent)}); err != nil {
|
2017-07-28 11:44:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Create the path to the bucket to store recent time entries
|
2018-01-11 16:52:44 +00:00
|
|
|
if err := gdb.db.MkBucketPath([]string{TypeToString(TypeRecent)}); err != nil {
|
2017-07-28 11:44:08 +00:00
|
|
|
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
|
2018-01-11 16:52:44 +00:00
|
|
|
return gdb.archDb.MkBucketPath([]string{TypeToString(TypeArchive)})
|
2017-07-28 11:44:08 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 16:52:44 +00:00
|
|
|
func (gdb *GimeDB) openDBType(tp int) (*boltease.DB, error) {
|
2017-07-28 11:44:08 +00:00
|
|
|
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
|
|
|
|
}
|
2018-01-11 16:52:44 +00:00
|
|
|
return nil, errors.New("Invalid database type: " + TypeToString(tp))
|
2017-07-28 11:44:08 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 16:52:44 +00:00
|
|
|
func (gdb *GimeDB) closeDBType(tp int) error {
|
2017-07-28 11:44:08 +00:00
|
|
|
if tp == TypeCurrent || tp == TypeRecent {
|
|
|
|
return gdb.closeDatabase()
|
|
|
|
} else if tp == TypeArchive {
|
|
|
|
return gdb.closeArchiveDatabase()
|
|
|
|
}
|
2018-01-11 16:52:44 +00:00
|
|
|
return errors.New("Invalid database type: " + TypeToString(tp))
|
2017-07-28 11:44:08 +00:00
|
|
|
}
|