@@ -38,27 +38,27 @@ import (
3838)
3939
4040const (
41- // leveldbDegradationWarnInterval specifies how often warning should be printed
42- // if the leveldb database cannot keep up with requested writes.
43- leveldbDegradationWarnInterval = time .Minute
41+ // degradationWarnInterval specifies how often warning should be printed if the
42+ // leveldb database cannot keep up with requested writes.
43+ degradationWarnInterval = time .Minute
4444
45- // leveldbMinCache is the minimum amount of memory in megabytes to allocate to
46- // leveldb read and write caching, split half and half.
47- leveldbMinCache = 16
45+ // minCache is the minimum amount of memory in megabytes to allocate to leveldb
46+ // read and write caching, split half and half.
47+ minCache = 16
4848
49- // leveldbMinHandles is the minimum number of files handles to allocate to the
50- // open database files.
51- leveldbMinHandles = 16
49+ // minHandles is the minimum number of files handles to allocate to the open
50+ // database files.
51+ minHandles = 16
5252
5353 // metricsGatheringInterval specifies the interval to retrieve leveldb database
5454 // compaction, io and pause stats to report to the user.
5555 metricsGatheringInterval = 3 * time .Second
5656)
5757
58- // LevelDBDatabase is a persistent key-value store. Apart from basic data storage
58+ // Database is a persistent key-value store. Apart from basic data storage
5959// functionality it also supports batch writes and iterating over the keyspace in
6060// binary-alphabetical order.
61- type LevelDBDatabase struct {
61+ type Database struct {
6262 fn string // filename for reporting
6363 db * leveldb.DB // LevelDB instance
6464
@@ -78,13 +78,13 @@ type LevelDBDatabase struct {
7878
7979// New returns a wrapped LevelDB object. The namespace is the prefix that the
8080// metrics reporting should use for surfacing internal stats.
81- func New (file string , cache int , handles int , namespace string ) (* LevelDBDatabase , error ) {
81+ func New (file string , cache int , handles int , namespace string ) (* Database , error ) {
8282 // Ensure we have some minimal caching and file guarantees
83- if cache < leveldbMinCache {
84- cache = leveldbMinCache
83+ if cache < minCache {
84+ cache = minCache
8585 }
86- if handles < leveldbMinHandles {
87- handles = leveldbMinHandles
86+ if handles < minHandles {
87+ handles = minHandles
8888 }
8989 logger := log .New ("database" , file )
9090 logger .Info ("Allocated cache and file handles" , "cache" , common .StorageSize (cache * 1024 * 1024 ), "handles" , handles )
@@ -103,7 +103,7 @@ func New(file string, cache int, handles int, namespace string) (*LevelDBDatabas
103103 return nil , err
104104 }
105105 // Assemble the wrapper with all the registered metrics
106- ldb := & LevelDBDatabase {
106+ ldb := & Database {
107107 fn : file ,
108108 db : db ,
109109 log : logger ,
@@ -124,7 +124,7 @@ func New(file string, cache int, handles int, namespace string) (*LevelDBDatabas
124124
125125// Close stops the metrics collection, flushes any pending data to disk and closes
126126// all io accesses to the underlying key-value store.
127- func (db * LevelDBDatabase ) Close () error {
127+ func (db * Database ) Close () error {
128128 db .quitLock .Lock ()
129129 defer db .quitLock .Unlock ()
130130
@@ -140,12 +140,12 @@ func (db *LevelDBDatabase) Close() error {
140140}
141141
142142// Has retrieves if a key is present in the key-value store.
143- func (db * LevelDBDatabase ) Has (key []byte ) (bool , error ) {
143+ func (db * Database ) Has (key []byte ) (bool , error ) {
144144 return db .db .Has (key , nil )
145145}
146146
147147// Get retrieves the given key if it's present in the key-value store.
148- func (db * LevelDBDatabase ) Get (key []byte ) ([]byte , error ) {
148+ func (db * Database ) Get (key []byte ) ([]byte , error ) {
149149 dat , err := db .db .Get (key , nil )
150150 if err != nil {
151151 return nil , err
@@ -154,38 +154,38 @@ func (db *LevelDBDatabase) Get(key []byte) ([]byte, error) {
154154}
155155
156156// Put inserts the given value into the key-value store.
157- func (db * LevelDBDatabase ) Put (key []byte , value []byte ) error {
157+ func (db * Database ) Put (key []byte , value []byte ) error {
158158 return db .db .Put (key , value , nil )
159159}
160160
161161// Delete removes the key from the key-value store.
162- func (db * LevelDBDatabase ) Delete (key []byte ) error {
162+ func (db * Database ) Delete (key []byte ) error {
163163 return db .db .Delete (key , nil )
164164}
165165
166166// NewBatch creates a write-only key-value store that buffers changes to its host
167167// database until a final write is called.
168- func (db * LevelDBDatabase ) NewBatch () ethdb.Batch {
169- return & levelDBBatch {
168+ func (db * Database ) NewBatch () ethdb.Batch {
169+ return & batch {
170170 db : db .db ,
171171 b : new (leveldb.Batch ),
172172 }
173173}
174174
175175// NewIterator creates a binary-alphabetical iterator over the entire keyspace
176176// contained within the leveldb database.
177- func (db * LevelDBDatabase ) NewIterator () ethdb.Iterator {
177+ func (db * Database ) NewIterator () ethdb.Iterator {
178178 return db .NewIteratorWithPrefix (nil )
179179}
180180
181181// NewIteratorWithPrefix creates a binary-alphabetical iterator over a subset
182182// of database content with a particular key prefix.
183- func (db * LevelDBDatabase ) NewIteratorWithPrefix (prefix []byte ) ethdb.Iterator {
183+ func (db * Database ) NewIteratorWithPrefix (prefix []byte ) ethdb.Iterator {
184184 return db .db .NewIterator (util .BytesPrefix (prefix ), nil )
185185}
186186
187187// Stat returns a particular internal stat of the database.
188- func (db * LevelDBDatabase ) Stat (property string ) (string , error ) {
188+ func (db * Database ) Stat (property string ) (string , error ) {
189189 return db .db .GetProperty (property )
190190}
191191
@@ -196,12 +196,12 @@ func (db *LevelDBDatabase) Stat(property string) (string, error) {
196196// A nil start is treated as a key before all keys in the data store; a nil limit
197197// is treated as a key after all keys in the data store. If both is nil then it
198198// will compact entire data store.
199- func (db * LevelDBDatabase ) Compact (start []byte , limit []byte ) error {
199+ func (db * Database ) Compact (start []byte , limit []byte ) error {
200200 return db .db .CompactRange (util.Range {Start : start , Limit : limit })
201201}
202202
203203// Path returns the path to the database directory.
204- func (db * LevelDBDatabase ) Path () string {
204+ func (db * Database ) Path () string {
205205 return db .fn
206206}
207207
@@ -222,7 +222,7 @@ func (db *LevelDBDatabase) Path() string {
222222//
223223// This is how the iostats look like (currently):
224224// Read(MB):3895.04860 Write(MB):3654.64712
225- func (db * LevelDBDatabase ) meter (refresh time.Duration ) {
225+ func (db * Database ) meter (refresh time.Duration ) {
226226 // Create the counters to store current and previous compaction values
227227 compactions := make ([][]float64 , 2 )
228228 for i := 0 ; i < 2 ; i ++ {
@@ -326,7 +326,7 @@ func (db *LevelDBDatabase) meter(refresh time.Duration) {
326326 // If a warning that db is performing compaction has been displayed, any subsequent
327327 // warnings will be withheld for one minute not to overwhelm the user.
328328 if paused && delayN - delaystats [0 ] == 0 && duration .Nanoseconds ()- delaystats [1 ] == 0 &&
329- time .Now ().After (lastWritePaused .Add (leveldbDegradationWarnInterval )) {
329+ time .Now ().After (lastWritePaused .Add (degradationWarnInterval )) {
330330 db .log .Warn ("Database compacting, degraded performance" )
331331 lastWritePaused = time .Now ()
332332 }
@@ -379,40 +379,40 @@ func (db *LevelDBDatabase) meter(refresh time.Duration) {
379379 errc <- merr
380380}
381381
382- // levelDBBatch is a write-only leveldb batch that commits changes to its host
383- // database when Write is called. A batch cannot be used concurrently.
384- type levelDBBatch struct {
382+ // batch is a write-only leveldb batch that commits changes to its host database
383+ // when Write is called. A batch cannot be used concurrently.
384+ type batch struct {
385385 db * leveldb.DB
386386 b * leveldb.Batch
387387 size int
388388}
389389
390390// Put inserts the given value into the batch for later committing.
391- func (b * levelDBBatch ) Put (key , value []byte ) error {
391+ func (b * batch ) Put (key , value []byte ) error {
392392 b .b .Put (key , value )
393393 b .size += len (value )
394394 return nil
395395}
396396
397397// Delete inserts the a key removal into the batch for later committing.
398- func (b * levelDBBatch ) Delete (key []byte ) error {
398+ func (b * batch ) Delete (key []byte ) error {
399399 b .b .Delete (key )
400- b .size += 1
400+ b .size ++
401401 return nil
402402}
403403
404404// ValueSize retrieves the amount of data queued up for writing.
405- func (b * levelDBBatch ) ValueSize () int {
405+ func (b * batch ) ValueSize () int {
406406 return b .size
407407}
408408
409409// Write flushes any accumulated data to disk.
410- func (b * levelDBBatch ) Write () error {
410+ func (b * batch ) Write () error {
411411 return b .db .Write (b .b , nil )
412412}
413413
414414// Reset resets the batch for reuse.
415- func (b * levelDBBatch ) Reset () {
415+ func (b * batch ) Reset () {
416416 b .b .Reset ()
417417 b .size = 0
418418}
0 commit comments