diff options
Diffstat (limited to 'ethdb')
-rw-r--r-- | ethdb/database.go | 41 | ||||
-rw-r--r-- | ethdb/interface.go | 30 | ||||
-rw-r--r-- | ethdb/memory_database.go | 24 |
3 files changed, 79 insertions, 16 deletions
diff --git a/ethdb/database.go b/ethdb/database.go index 9e80e5409..047821c30 100644 --- a/ethdb/database.go +++ b/ethdb/database.go @@ -61,9 +61,7 @@ type LDBDatabase struct { quitChan chan chan error // Quit channel to stop the metrics collection before closing the database } -// NewLDBDatabase returns a LevelDB wrapped object. LDBDatabase does not persist data by -// it self but requires a background poller which syncs every X. `Flush` should be called -// when data needs to be stored and written to disk. +// NewLDBDatabase returns a LevelDB wrapped object. func NewLDBDatabase(file string, cache int) (*LDBDatabase, error) { // Calculate the cache allowance for this particular database cache = int(float64(cache) * cacheRatio[filepath.Base(file)]) @@ -142,11 +140,6 @@ func (self *LDBDatabase) NewIterator() iterator.Iterator { return self.db.NewIterator(nil, nil) } -// Flush flushes out the queue to leveldb -func (self *LDBDatabase) Flush() error { - return nil -} - func (self *LDBDatabase) Close() { // Stop the metrics collection to avoid internal database races self.quitLock.Lock() @@ -159,12 +152,14 @@ func (self *LDBDatabase) Close() { glog.V(logger.Error).Infof("metrics failure in '%s': %v\n", self.fn, err) } } - // Flush and close the database - if err := self.Flush(); err != nil { - glog.V(logger.Error).Infof("flushing '%s' failed: %v\n", self.fn, err) + err := self.db.Close() + if glog.V(logger.Error) { + if err == nil { + glog.Infoln("closed db:", self.fn) + } else { + glog.Errorf("error closing db %s: %v", self.fn, err) + } } - self.db.Close() - glog.V(logger.Error).Infoln("flushed and closed db:", self.fn) } func (self *LDBDatabase) LDB() *leveldb.DB { @@ -268,3 +263,23 @@ func (self *LDBDatabase) meter(refresh time.Duration) { } } } + +// TODO: remove this stuff and expose leveldb directly + +func (db *LDBDatabase) NewBatch() Batch { + return &ldbBatch{db: db.db, b: new(leveldb.Batch)} +} + +type ldbBatch struct { + db *leveldb.DB + b *leveldb.Batch +} + +func (b *ldbBatch) Put(key, value []byte) error { + b.b.Put(key, value) + return nil +} + +func (b *ldbBatch) Write() error { + return b.db.Write(b.b, nil) +} diff --git a/ethdb/interface.go b/ethdb/interface.go new file mode 100644 index 000000000..f4b787a52 --- /dev/null +++ b/ethdb/interface.go @@ -0,0 +1,30 @@ +// Copyright 2014 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. + +package ethdb + +type Database interface { + Put(key []byte, value []byte) error + Get(key []byte) ([]byte, error) + Delete(key []byte) error + Close() + NewBatch() Batch +} + +type Batch interface { + Put(key, value []byte) error + Write() error +} diff --git a/ethdb/memory_database.go b/ethdb/memory_database.go index d50f8f9d4..81911f23f 100644 --- a/ethdb/memory_database.go +++ b/ethdb/memory_database.go @@ -36,8 +36,7 @@ func NewMemDatabase() (*MemDatabase, error) { } func (db *MemDatabase) Put(key []byte, value []byte) error { - db.db[string(key)] = value - + db.db[string(key)] = common.CopyBytes(value) return nil } @@ -92,6 +91,25 @@ func (db *MemDatabase) LastKnownTD() []byte { return data } -func (db *MemDatabase) Flush() error { +func (db *MemDatabase) NewBatch() Batch { + return &memBatch{db: db} +} + +type kv struct{ k, v []byte } + +type memBatch struct { + db *MemDatabase + writes []kv +} + +func (w *memBatch) Put(key, value []byte) error { + w.writes = append(w.writes, kv{key, common.CopyBytes(value)}) + return nil +} + +func (w *memBatch) Write() error { + for _, kv := range w.writes { + w.db.db[string(kv.k)] = kv.v + } return nil } |