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/gregjones/httpcache/leveldbcache"
"github.com/leekchan/accounting" "github.com/leekchan/accounting"
"github.com/logrusorgru/aurora" "github.com/logrusorgru/aurora"
"github.com/syndtr/goleveldb/leveldb"
ESI "./client" ESI "./client"
@ -35,10 +36,6 @@ import (
ESIUniverse "./client/universe" ESIUniverse "./client/universe"
ESIWallet "./client/wallet" ESIWallet "./client/wallet"
"database/sql"
_ "github.com/mattn/go-sqlite3"
httptransport "github.com/go-openapi/runtime/client" httptransport "github.com/go-openapi/runtime/client"
) )
@ -78,12 +75,13 @@ var (
defaultDateFormat = "_2 Jan 2006, 15:04" defaultDateFormat = "_2 Jan 2006, 15:04"
cfgFilePath = flag.String("config", "configuration.toml", "Path to the configuration file.") 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") insecureFlag = flag.Bool("insecure", false, "Do not check the HTTPS certificate")
) )
var ctx = context.Background() var ctx = context.Background()
var messages = make(chan *oauth2.Token) var messages = make(chan *oauth2.Token)
var cacheDB *leveldb.DB
func main() { func main() {
flag.Parse() flag.Parse()
@ -97,6 +95,13 @@ func main() {
log.SetOutput(f) 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() cErr := readConfigurationFile()
if cErr != nil { if cErr != nil {
fmt.Println("Missing configuration file.") fmt.Println("Missing configuration file.")
@ -109,11 +114,7 @@ func main() {
cToken = getNewAuthorizationToken() cToken = getNewAuthorizationToken()
} }
ldb, ldbErr := leveldbcache.New("cache.ldb") ldb := leveldbcache.NewWithDB(cacheDB)
if ldbErr != nil {
fmt.Println("Unable to initialize the LevelDB cache.")
log.Fatal(ldbErr)
}
cachingTransport := httpcache.NewTransport(ldb) cachingTransport := httpcache.NewTransport(ldb)
if *insecureFlag { if *insecureFlag {
@ -829,32 +830,13 @@ func getCachedDataInt(key string) int32 {
} }
func getCachedData(key string) string { func getCachedData(key string) string {
db, err := sql.Open("sqlite3", *cacheDBPath)
if err != nil {
log.Fatal(err)
}
defer db.Close() response, err := cacheDB.Get([]byte(key), nil)
_, 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)
if err != nil { if err != nil {
return "" return ""
} }
return response return string(response[:])
} }
func putCacheDataIntSub(key int32, subkey string, value int32) { 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) { func putCacheData(key string, value string) {
db, err := sql.Open("sqlite3", *cacheDBPath) err := cacheDB.Put([]byte(key), []byte(value), nil)
if err != nil { if err != nil {
log.Fatal(err) 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 { func getDatabaseToken() *oauth2.Token {