LevelDB: Do not create cache if cache size <= 0.

This commit is contained in:
Sascha L. Teichmann 2014-08-08 10:05:03 +02:00
parent 787ea5d0ee
commit 2e47262ce8

View File

@ -23,13 +23,21 @@ type LevelDBSession struct {
} }
func NewLeveDBBackend(path string, cacheSize int) (ldb *LevelDBBackend, err error) { func NewLeveDBBackend(path string, cacheSize int) (ldb *LevelDBBackend, err error) {
cache := leveldb.NewLRUCache(cacheSize * 1024 * 1024)
opts := leveldb.NewOptions() opts := leveldb.NewOptions()
var cache *leveldb.Cache
if cacheSize > 0 {
cache = leveldb.NewLRUCache(cacheSize * 1024 * 1024)
opts.SetCache(cache) opts.SetCache(cache)
}
opts.SetCreateIfMissing(true) opts.SetCreateIfMissing(true)
var db *leveldb.DB var db *leveldb.DB
if db, err = leveldb.Open(path, opts); err != nil { if db, err = leveldb.Open(path, opts); err != nil {
if cache != nil {
cache.Close() cache.Close()
}
return return
} }
ldb = &LevelDBBackend{ ldb = &LevelDBBackend{
@ -63,7 +71,9 @@ func (ldbs *LevelDBSession) Close() error {
func (ldb *LevelDBBackend) Shutdown() error { func (ldb *LevelDBBackend) Shutdown() error {
ldb.db.Close() ldb.db.Close()
if ldb.cache != nil {
ldb.cache.Close() ldb.cache.Close()
}
return nil return nil
} }