gosuki/db.go

66 lines
1.3 KiB
Go
Raw Normal View History

2017-11-13 18:14:59 +00:00
package main
import (
2017-11-17 14:18:53 +00:00
"fmt"
2018-11-09 17:25:50 +00:00
"gomark/database"
"gomark/tools"
2017-11-17 14:18:53 +00:00
"path/filepath"
)
2018-11-09 17:25:50 +00:00
type DB = database.DB
2017-11-17 14:18:53 +00:00
// Global cache database
var (
2018-11-13 16:11:16 +00:00
CacheDB *DB // Main in memory db, is synced with disc
2017-11-17 14:18:53 +00:00
)
func initDB() {
// Initialize memory db with schema
2018-11-09 17:25:50 +00:00
cachePath := fmt.Sprintf(database.DBMemcacheFmt, database.DBCacheName)
2018-11-13 16:11:16 +00:00
CacheDB = database.New(database.DBCacheName, cachePath)
2018-05-27 15:45:06 +00:00
CacheDB.Init()
2017-11-17 14:18:53 +00:00
// Check and initialize local db as last step
// browser bookmarks should already be in cache
2017-11-17 14:18:53 +00:00
2018-11-09 17:25:50 +00:00
dbdir := tools.GetDefaultDBPath()
dbpath := filepath.Join(dbdir, database.DBFileName)
// Verifiy that local db directory path is writeable
2018-11-09 17:25:50 +00:00
err := tools.CheckWriteable(dbdir)
2018-10-28 19:19:12 +00:00
if err != nil {
log.Critical(err)
}
2017-11-17 14:18:53 +00:00
2017-11-19 16:00:37 +00:00
// If local db exists load it to cacheDB
var exists bool
2018-11-09 17:25:50 +00:00
if exists, err = tools.CheckFileExists(dbpath); exists {
2018-10-28 19:19:12 +00:00
if err != nil {
log.Warning(err)
}
2018-10-28 19:26:38 +00:00
log.Infof("<%s> exists, preloading to cache", dbpath)
2018-11-13 16:11:16 +00:00
er := CacheDB.SyncFromDisk(dbpath)
if er != nil {
log.Critical(er)
}
} else {
2018-10-28 19:19:12 +00:00
if err != nil {
log.Error(err)
}
// Else initialize it
2018-05-27 15:45:06 +00:00
initLocalDB(CacheDB, dbpath)
}
}
//Initialize the local database file
func initLocalDB(db *DB, dbpath string) {
2017-11-19 16:00:37 +00:00
log.Infof("Initializing local db at '%s'", dbpath)
err := db.SyncToDisk(dbpath)
2018-10-28 19:19:12 +00:00
if err != nil {
log.Critical(err)
}
2017-11-13 18:14:59 +00:00
}