diff options
Diffstat (limited to 'ethdb')
-rw-r--r-- | ethdb/database.go | 41 | ||||
-rw-r--r-- | ethdb/database_test.go | 3 |
2 files changed, 31 insertions, 13 deletions
diff --git a/ethdb/database.go b/ethdb/database.go index c93344409..ace56c6c7 100644 --- a/ethdb/database.go +++ b/ethdb/database.go @@ -17,12 +17,12 @@ package ethdb import ( + "path/filepath" "strconv" "strings" "sync" "time" - "github.com/ethereum/go-ethereum/compression/rle" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/metrics" @@ -36,6 +36,14 @@ import ( var OpenFileLimit = 64 +// cacheRatio specifies how the total alloted cache is distributed between the +// various system databases. +var cacheRatio = map[string]float64{ + "blockchain": 1.0 / 13.0, + "extra": 2.0 / 13.0, + "state": 10.0 / 13.0, +} + type LDBDatabase struct { fn string // filename for reporting db *leveldb.DB // LevelDB instance @@ -57,14 +65,24 @@ type LDBDatabase struct { // 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. -func NewLDBDatabase(file string) (*LDBDatabase, error) { - // Open the db - db, err := leveldb.OpenFile(file, &opt.Options{OpenFilesCacheCapacity: OpenFileLimit}) - // check for corruption and attempt to recover - if _, iscorrupted := err.(*errors.ErrCorrupted); iscorrupted { +func NewLDBDatabase(file string, cache int) (*LDBDatabase, error) { + // Calculate the cache allowance for this particular database + cache = int(float64(cache) * cacheRatio[filepath.Base(file)]) + if cache < 16 { + cache = 16 + } + glog.V(logger.Info).Infof("Alloted %dMB cache to %s", cache, file) + + // Open the db and recover any potential corruptions + db, err := leveldb.OpenFile(file, &opt.Options{ + OpenFilesCacheCapacity: OpenFileLimit, + BlockCacheCapacity: cache / 2 * opt.MiB, + WriteBuffer: cache / 4 * opt.MiB, // Two of these are used internally + }) + if _, corrupted := err.(*errors.ErrCorrupted); corrupted { db, err = leveldb.RecoverFile(file, nil) } - // (re) check for errors and abort if opening of the db failed + // (Re)check for errors and abort if opening of the db failed if err != nil { return nil, err } @@ -81,12 +99,12 @@ func (self *LDBDatabase) Put(key []byte, value []byte) error { defer self.putTimer.UpdateSince(time.Now()) } // Generate the data to write to disk, update the meter and write - dat := rle.Compress(value) + //value = rle.Compress(value) if self.writeMeter != nil { - self.writeMeter.Mark(int64(len(dat))) + self.writeMeter.Mark(int64(len(value))) } - return self.db.Put(key, dat, nil) + return self.db.Put(key, value, nil) } // Get returns the given key if it's present. @@ -107,7 +125,8 @@ func (self *LDBDatabase) Get(key []byte) ([]byte, error) { if self.readMeter != nil { self.readMeter.Mark(int64(len(dat))) } - return rle.Decompress(dat) + return dat, nil + //return rle.Decompress(dat) } // Delete deletes the key from the queue and database diff --git a/ethdb/database_test.go b/ethdb/database_test.go index 2465036aa..ae4906166 100644 --- a/ethdb/database_test.go +++ b/ethdb/database_test.go @@ -28,8 +28,7 @@ func newDb() *LDBDatabase { if common.FileExist(file) { os.RemoveAll(file) } - - db, _ := NewLDBDatabase(file) + db, _ := NewLDBDatabase(file, 0) return db } |