|
|
@ -1,8 +1,6 @@ |
|
|
|
package ethdb |
|
|
|
package ethdb |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"sync" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/compression/rle" |
|
|
|
"github.com/ethereum/go-ethereum/compression/rle" |
|
|
|
"github.com/ethereum/go-ethereum/logger" |
|
|
|
"github.com/ethereum/go-ethereum/logger" |
|
|
|
"github.com/ethereum/go-ethereum/logger/glog" |
|
|
|
"github.com/ethereum/go-ethereum/logger/glog" |
|
|
@ -15,14 +13,10 @@ import ( |
|
|
|
var OpenFileLimit = 64 |
|
|
|
var OpenFileLimit = 64 |
|
|
|
|
|
|
|
|
|
|
|
type LDBDatabase struct { |
|
|
|
type LDBDatabase struct { |
|
|
|
|
|
|
|
// filename for reporting
|
|
|
|
fn string |
|
|
|
fn string |
|
|
|
|
|
|
|
// LevelDB instance
|
|
|
|
mu sync.Mutex |
|
|
|
|
|
|
|
db *leveldb.DB |
|
|
|
db *leveldb.DB |
|
|
|
|
|
|
|
|
|
|
|
queue map[string][]byte |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
quit chan struct{} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// NewLDBDatabase returns a LevelDB wrapped object. LDBDatabase does not persist data by
|
|
|
|
// NewLDBDatabase returns a LevelDB wrapped object. LDBDatabase does not persist data by
|
|
|
@ -42,83 +36,37 @@ func NewLDBDatabase(file string) (*LDBDatabase, error) { |
|
|
|
database := &LDBDatabase{ |
|
|
|
database := &LDBDatabase{ |
|
|
|
fn: file, |
|
|
|
fn: file, |
|
|
|
db: db, |
|
|
|
db: db, |
|
|
|
quit: make(chan struct{}), |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
database.makeQueue() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return database, nil |
|
|
|
return database, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *LDBDatabase) makeQueue() { |
|
|
|
|
|
|
|
self.queue = make(map[string][]byte) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Put puts the given key / value to the queue
|
|
|
|
// Put puts the given key / value to the queue
|
|
|
|
func (self *LDBDatabase) Put(key []byte, value []byte) { |
|
|
|
func (self *LDBDatabase) Put(key []byte, value []byte) { |
|
|
|
self.mu.Lock() |
|
|
|
self.db.Put(key, rle.Compress(value), nil) |
|
|
|
defer self.mu.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.queue[string(key)] = value |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Get returns the given key if it's present.
|
|
|
|
// Get returns the given key if it's present.
|
|
|
|
func (self *LDBDatabase) Get(key []byte) ([]byte, error) { |
|
|
|
func (self *LDBDatabase) Get(key []byte) ([]byte, error) { |
|
|
|
self.mu.Lock() |
|
|
|
|
|
|
|
defer self.mu.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check queue first
|
|
|
|
|
|
|
|
if dat, ok := self.queue[string(key)]; ok { |
|
|
|
|
|
|
|
return dat, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dat, err := self.db.Get(key, nil) |
|
|
|
dat, err := self.db.Get(key, nil) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return rle.Decompress(dat) |
|
|
|
return rle.Decompress(dat) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Delete deletes the key from the queue and database
|
|
|
|
// Delete deletes the key from the queue and database
|
|
|
|
func (self *LDBDatabase) Delete(key []byte) error { |
|
|
|
func (self *LDBDatabase) Delete(key []byte) error { |
|
|
|
self.mu.Lock() |
|
|
|
|
|
|
|
defer self.mu.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// make sure it's not in the queue
|
|
|
|
|
|
|
|
delete(self.queue, string(key)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return self.db.Delete(key, nil) |
|
|
|
return self.db.Delete(key, nil) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *LDBDatabase) LastKnownTD() []byte { |
|
|
|
|
|
|
|
data, _ := self.Get([]byte("LTD")) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(data) == 0 { |
|
|
|
|
|
|
|
data = []byte{0x0} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return data |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (self *LDBDatabase) NewIterator() iterator.Iterator { |
|
|
|
func (self *LDBDatabase) NewIterator() iterator.Iterator { |
|
|
|
return self.db.NewIterator(nil, nil) |
|
|
|
return self.db.NewIterator(nil, nil) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Flush flushes out the queue to leveldb
|
|
|
|
// Flush flushes out the queue to leveldb
|
|
|
|
func (self *LDBDatabase) Flush() error { |
|
|
|
func (self *LDBDatabase) Flush() error { |
|
|
|
self.mu.Lock() |
|
|
|
return nil |
|
|
|
defer self.mu.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
batch := new(leveldb.Batch) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for key, value := range self.queue { |
|
|
|
|
|
|
|
batch.Put([]byte(key), rle.Compress(value)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
self.makeQueue() // reset the queue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glog.V(logger.Detail).Infoln("Flush database: ", self.fn) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return self.db.Write(batch, nil) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *LDBDatabase) Close() { |
|
|
|
func (self *LDBDatabase) Close() { |
|
|
|