aboutsummaryrefslogtreecommitdiffstats
path: root/ethdb/database.go
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2015-07-22 18:46:20 +0800
committerPéter Szilágyi <peterke@gmail.com>2015-07-22 19:00:52 +0800
commitc7e7778f2a7d80fa12643db546db98fa70f2e384 (patch)
treeec31d9621aa5b265af6a4c9098c2ad913c1a325d /ethdb/database.go
parentf1daed65b1f89cae6327cd9f49668628c6980ade (diff)
downloadgo-tangerine-c7e7778f2a7d80fa12643db546db98fa70f2e384.tar
go-tangerine-c7e7778f2a7d80fa12643db546db98fa70f2e384.tar.gz
go-tangerine-c7e7778f2a7d80fa12643db546db98fa70f2e384.tar.bz2
go-tangerine-c7e7778f2a7d80fa12643db546db98fa70f2e384.tar.lz
go-tangerine-c7e7778f2a7d80fa12643db546db98fa70f2e384.tar.xz
go-tangerine-c7e7778f2a7d80fa12643db546db98fa70f2e384.tar.zst
go-tangerine-c7e7778f2a7d80fa12643db546db98fa70f2e384.zip
cmd, core, eth, ethdb: cache flag to allocate memory for db internal use
Diffstat (limited to 'ethdb/database.go')
-rw-r--r--ethdb/database.go31
1 files changed, 25 insertions, 6 deletions
diff --git a/ethdb/database.go b/ethdb/database.go
index c75136a1b..38e454c00 100644
--- a/ethdb/database.go
+++ b/ethdb/database.go
@@ -17,6 +17,7 @@
package ethdb
import (
+ "path/filepath"
"strconv"
"strings"
"sync"
@@ -36,6 +37,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 +66,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
}