Use of LevelDB instead of SQLite

This commit is contained in:
Thomas Schwery 2017-03-02 15:34:22 +01:00
parent a42b8bf39c
commit dca2c12487

70
main.go
View file

@ -25,6 +25,7 @@ import (
"github.com/gregjones/httpcache/leveldbcache"
"github.com/leekchan/accounting"
"github.com/logrusorgru/aurora"
"github.com/syndtr/goleveldb/leveldb"
ESI "./client"
@ -35,10 +36,6 @@ import (
ESIUniverse "./client/universe"
ESIWallet "./client/wallet"
"database/sql"
_ "github.com/mattn/go-sqlite3"
httptransport "github.com/go-openapi/runtime/client"
)
@ -78,12 +75,13 @@ var (
defaultDateFormat = "_2 Jan 2006, 15:04"
cfgFilePath = flag.String("config", "configuration.toml", "Path to the configuration file.")
cacheDBPath = flag.String("cache", "cache.db", "Path to the cache sqlite database.")
cacheDBPath = flag.String("cache", "cache.ldb", "Path to the cache leveldb database.")
insecureFlag = flag.Bool("insecure", false, "Do not check the HTTPS certificate")
)
var ctx = context.Background()
var messages = make(chan *oauth2.Token)
var cacheDB *leveldb.DB
func main() {
flag.Parse()
@ -97,6 +95,13 @@ func main() {
log.SetOutput(f)
var cacheDBerr error
cacheDB, cacheDBerr = leveldb.OpenFile(*cacheDBPath, nil)
if cacheDBerr != nil {
fmt.Println("Unable to initialize the LevelDB cache.")
log.Fatal(cacheDBerr)
}
cErr := readConfigurationFile()
if cErr != nil {
fmt.Println("Missing configuration file.")
@ -109,11 +114,7 @@ func main() {
cToken = getNewAuthorizationToken()
}
ldb, ldbErr := leveldbcache.New("cache.ldb")
if ldbErr != nil {
fmt.Println("Unable to initialize the LevelDB cache.")
log.Fatal(ldbErr)
}
ldb := leveldbcache.NewWithDB(cacheDB)
cachingTransport := httpcache.NewTransport(ldb)
if *insecureFlag {
@ -829,32 +830,13 @@ func getCachedDataInt(key string) int32 {
}
func getCachedData(key string) string {
db, err := sql.Open("sqlite3", *cacheDBPath)
if err != nil {
log.Fatal(err)
}
defer db.Close()
_, err = db.Exec("CREATE TABLE IF NOT EXISTS properties (id text NOT NULL PRIMARY KEY, value TEXT);")
if err != nil {
log.Fatal(err)
}
stmt, err := db.Prepare("SELECT value FROM properties WHERE id = ?")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
var response string
err = stmt.QueryRow(key).Scan(&response)
response, err := cacheDB.Get([]byte(key), nil)
if err != nil {
return ""
}
return response
return string(response[:])
}
func putCacheDataIntSub(key int32, subkey string, value int32) {
@ -874,34 +856,10 @@ func putCacheDataSub64(key int64, subkey string, value string) {
}
func putCacheData(key string, value string) {
db, err := sql.Open("sqlite3", *cacheDBPath)
err := cacheDB.Put([]byte(key), []byte(value), nil)
if err != nil {
log.Fatal(err)
}
defer db.Close()
_, err = db.Exec("CREATE TABLE IF NOT EXISTS properties (id text NOT NULL PRIMARY KEY, value TEXT);")
if err != nil {
log.Fatal(err)
}
tx, err := db.Begin()
if err != nil {
log.Fatal(err)
}
stmt, err := tx.Prepare("INSERT OR REPLACE INTO properties(id, value) values(?, ?)")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
_, err = stmt.Exec(key, value)
if err != nil {
log.Fatal(err)
}
tx.Commit()
}
func getDatabaseToken() *oauth2.Token {