mirror of
https://bitbucket.org/s_l_teichmann/mtsatellite
synced 2024-11-08 03:00:26 +01:00
104 lines
2.0 KiB
Go
104 lines
2.0 KiB
Go
// Copyright 2014 by Sascha L. Teichmann
|
|
// Use of this source code is governed by the MIT license
|
|
// that can be found in the LICENSE file.
|
|
|
|
package main
|
|
|
|
import (
|
|
leveldb "github.com/jmhodges/levigo"
|
|
)
|
|
|
|
type LevelDBBackend struct {
|
|
cache *leveldb.Cache
|
|
db *leveldb.DB
|
|
tx *leveldb.WriteBatch
|
|
}
|
|
|
|
func NewLeveDBBackend(path string, cacheSize int) (ldb *LevelDBBackend, err error) {
|
|
cache := leveldb.NewLRUCache(cacheSize * 1024 * 1024)
|
|
opts := leveldb.NewOptions()
|
|
opts.SetCache(cache)
|
|
opts.SetCreateIfMissing(true)
|
|
var db *leveldb.DB
|
|
if db, err = leveldb.Open(path, opts); err != nil {
|
|
cache.Close()
|
|
return
|
|
}
|
|
ldb = &LevelDBBackend{
|
|
cache: cache,
|
|
db: db}
|
|
return
|
|
}
|
|
|
|
func (ldb *LevelDBBackend) Shutdown() error {
|
|
tx := ldb.tx
|
|
if tx != nil {
|
|
ldb.tx = nil
|
|
tx.Close()
|
|
}
|
|
ldb.db.Close()
|
|
ldb.cache.Close()
|
|
return nil
|
|
}
|
|
|
|
func (ldb *LevelDBBackend) Fetch(hash, key []byte) (value []byte, err error) {
|
|
ro := leveldb.NewReadOptions()
|
|
value, err = ldb.db.Get(ro, key)
|
|
ro.Close()
|
|
return
|
|
}
|
|
|
|
func (ldb *LevelDBBackend) InTransaction() bool {
|
|
return ldb.tx != nil
|
|
}
|
|
|
|
func (ldb *LevelDBBackend) keyExists(key []byte) (exists bool, err error) {
|
|
ro := leveldb.NewReadOptions()
|
|
defer ro.Close()
|
|
var data []byte
|
|
if data, err = ldb.db.Get(ro, key); err != nil {
|
|
return
|
|
}
|
|
exists = data != nil
|
|
return
|
|
}
|
|
|
|
func (ldb *LevelDBBackend) Store(hash, key, value []byte) (exists bool, err error) {
|
|
|
|
var pos int64
|
|
if pos, err = bytes2pos(key); err != nil {
|
|
return
|
|
}
|
|
// Re-code it to make LevelDB happy.
|
|
key = pos2bytes(pos)
|
|
|
|
if exists, err = ldb.keyExists(key); err != nil {
|
|
return
|
|
}
|
|
|
|
if ldb.tx != nil {
|
|
ldb.tx.Put(key, value)
|
|
return
|
|
}
|
|
|
|
wo := leveldb.NewWriteOptions()
|
|
err = ldb.db.Put(wo, key, value)
|
|
wo.Close()
|
|
|
|
return
|
|
}
|
|
|
|
func (ldb *LevelDBBackend) BeginTransaction() error {
|
|
ldb.tx = leveldb.NewWriteBatch()
|
|
return nil
|
|
}
|
|
|
|
func (ldb *LevelDBBackend) CommitTransaction() (err error) {
|
|
tx := ldb.tx
|
|
ldb.tx = nil
|
|
wo := leveldb.NewWriteOptions()
|
|
err = ldb.db.Write(wo, tx)
|
|
wo.Close()
|
|
return
|
|
}
|