From 8c4dab77ba48dc68073fe1df79e7000043c0f966 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Mon, 14 Sep 2015 09:35:57 +0200 Subject: all: move common.Database to package ethdb --- ethdb/interface.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 ethdb/interface.go (limited to 'ethdb') diff --git a/ethdb/interface.go b/ethdb/interface.go new file mode 100644 index 000000000..598e8eaa3 --- /dev/null +++ b/ethdb/interface.go @@ -0,0 +1,25 @@ +// 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 . + +package ethdb + +type Database interface { + Put(key []byte, value []byte) error + Get(key []byte) ([]byte, error) + Delete(key []byte) error + Close() + Flush() error +} -- cgit v1.2.3 From 8b32f10f16f19c0b8985399fafdfe31af29493a1 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Tue, 18 Aug 2015 13:42:21 +0200 Subject: ethdb: add NewBatch --- ethdb/database.go | 20 ++++++++++++++++++++ ethdb/interface.go | 6 ++++++ ethdb/memory_database.go | 23 +++++++++++++++++++++++ 3 files changed, 49 insertions(+) (limited to 'ethdb') diff --git a/ethdb/database.go b/ethdb/database.go index 9e80e5409..ad87f853d 100644 --- a/ethdb/database.go +++ b/ethdb/database.go @@ -268,3 +268,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 index 598e8eaa3..acb1b57c0 100644 --- a/ethdb/interface.go +++ b/ethdb/interface.go @@ -22,4 +22,10 @@ type Database interface { Delete(key []byte) error Close() Flush() error + 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..4fcce1812 100644 --- a/ethdb/memory_database.go +++ b/ethdb/memory_database.go @@ -95,3 +95,26 @@ func (db *MemDatabase) LastKnownTD() []byte { func (db *MemDatabase) Flush() error { return nil } + +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 +} -- cgit v1.2.3 From d581dfee5fbd46f3e6c54e3fab2717105e6bd510 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Mon, 6 Jul 2015 01:19:16 +0200 Subject: ethdb: copy stored memdb values Storing a value in LevelDB copies the bytes, modifying the value afterwards does not affect the content of the database. This commit ensures that MemDatabase satisfies the same property. --- ethdb/memory_database.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'ethdb') diff --git a/ethdb/memory_database.go b/ethdb/memory_database.go index 4fcce1812..fd5663fec 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 } -- cgit v1.2.3 From b25258996059439df82687cc653ed14a5a9edce1 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Mon, 14 Sep 2015 09:45:40 +0200 Subject: ethdb: remove Flush --- ethdb/database.go | 21 ++++++++------------- ethdb/interface.go | 1 - ethdb/memory_database.go | 4 ---- 3 files changed, 8 insertions(+), 18 deletions(-) (limited to 'ethdb') diff --git a/ethdb/database.go b/ethdb/database.go index ad87f853d..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 { diff --git a/ethdb/interface.go b/ethdb/interface.go index acb1b57c0..f4b787a52 100644 --- a/ethdb/interface.go +++ b/ethdb/interface.go @@ -21,7 +21,6 @@ type Database interface { Get(key []byte) ([]byte, error) Delete(key []byte) error Close() - Flush() error NewBatch() Batch } diff --git a/ethdb/memory_database.go b/ethdb/memory_database.go index fd5663fec..81911f23f 100644 --- a/ethdb/memory_database.go +++ b/ethdb/memory_database.go @@ -91,10 +91,6 @@ func (db *MemDatabase) LastKnownTD() []byte { return data } -func (db *MemDatabase) Flush() error { - return nil -} - func (db *MemDatabase) NewBatch() Batch { return &memBatch{db: db} } -- cgit v1.2.3