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"
|
2017-11-14 16:55:19 +00:00
|
|
|
)
|
|
|
|
|
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() {
|
2017-11-16 13:27:50 +00:00
|
|
|
// 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-16 13:27:50 +00:00
|
|
|
|
2017-11-17 14:18:53 +00:00
|
|
|
// Check and initialize local db as last step
|
2017-11-17 15:42:15 +00:00
|
|
|
// 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)
|
2017-11-17 15:42:15 +00:00
|
|
|
// 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
|
2017-11-17 15:07:03 +00:00
|
|
|
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)
|
|
|
|
}
|
2017-11-17 15:07:03 +00:00
|
|
|
} else {
|
2018-10-28 19:19:12 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
2017-11-17 15:07:03 +00:00
|
|
|
// Else initialize it
|
2018-05-27 15:45:06 +00:00
|
|
|
initLocalDB(CacheDB, dbpath)
|
2017-11-16 13:27:50 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 15:07:03 +00:00
|
|
|
}
|
|
|
|
|
2017-11-23 01:51:23 +00:00
|
|
|
//Initialize the local database file
|
2017-11-17 15:07:03 +00:00
|
|
|
func initLocalDB(db *DB, dbpath string) {
|
|
|
|
|
2017-11-19 16:00:37 +00:00
|
|
|
log.Infof("Initializing local db at '%s'", dbpath)
|
2017-11-17 15:42:15 +00:00
|
|
|
err := db.SyncToDisk(dbpath)
|
2018-10-28 19:19:12 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Critical(err)
|
|
|
|
}
|
2017-11-17 15:07:03 +00:00
|
|
|
|
2017-11-13 18:14:59 +00:00
|
|
|
}
|