mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 18:02:24 +00:00
ethdb: golint fixes and comments added from leveldb docs
This commit is contained in:
parent
c4a4613d95
commit
150779a590
3 changed files with 19 additions and 1 deletions
|
|
@ -103,6 +103,8 @@ func (db *LDBDatabase) Put(key []byte, value []byte) error {
|
||||||
return db.db.Put(key, value, nil)
|
return db.db.Put(key, value, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Has returns true if the DB does contain the given key.
|
||||||
|
// It is safe to modify the contents of the argument after Has returns.
|
||||||
func (db *LDBDatabase) Has(key []byte) (bool, error) {
|
func (db *LDBDatabase) Has(key []byte) (bool, error) {
|
||||||
return db.db.Has(key, nil)
|
return db.db.Has(key, nil)
|
||||||
}
|
}
|
||||||
|
|
@ -121,6 +123,8 @@ func (db *LDBDatabase) Delete(key []byte) error {
|
||||||
return db.db.Delete(key, nil)
|
return db.db.Delete(key, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewIterator returns an iterator for the latest snapshot of the underlying DB
|
||||||
|
// and returns a range for all keys in db.
|
||||||
func (db *LDBDatabase) NewIterator() iterator.Iterator {
|
func (db *LDBDatabase) NewIterator() iterator.Iterator {
|
||||||
return db.db.NewIterator(nil, nil)
|
return db.db.NewIterator(nil, nil)
|
||||||
}
|
}
|
||||||
|
|
@ -150,6 +154,7 @@ func (db *LDBDatabase) Close() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LDB returns the memory address for leveldb.DB.
|
||||||
func (db *LDBDatabase) LDB() *leveldb.DB {
|
func (db *LDBDatabase) LDB() *leveldb.DB {
|
||||||
return db.db
|
return db.db
|
||||||
}
|
}
|
||||||
|
|
@ -356,6 +361,7 @@ func (db *LDBDatabase) meter(refresh time.Duration) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewBatch populates the ldbBatch for read and writes to the db.
|
||||||
func (db *LDBDatabase) NewBatch() Batch {
|
func (db *LDBDatabase) NewBatch() Batch {
|
||||||
return &ldbBatch{db: db.db, b: new(leveldb.Batch)}
|
return &ldbBatch{db: db.db, b: new(leveldb.Batch)}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
package ethdb
|
package ethdb
|
||||||
|
|
||||||
// Code using batches should try to add this much data to the batch.
|
// IdealBatchSize is the amount of data vode using batches should try to add to the batch.
|
||||||
// The value was determined empirically.
|
// The value was determined empirically.
|
||||||
const IdealBatchSize = 100 * 1024
|
const IdealBatchSize = 100 * 1024
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,23 +26,28 @@ import (
|
||||||
/*
|
/*
|
||||||
* This is a test memory database. Do not use for any production it does not get persisted
|
* This is a test memory database. Do not use for any production it does not get persisted
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// MemDatabase imitates the key-value store levelDB for the test memory database.
|
||||||
type MemDatabase struct {
|
type MemDatabase struct {
|
||||||
db map[string][]byte
|
db map[string][]byte
|
||||||
lock sync.RWMutex
|
lock sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewMemDatabase inits a mock levelDB instance with a map.
|
||||||
func NewMemDatabase() (*MemDatabase, error) {
|
func NewMemDatabase() (*MemDatabase, error) {
|
||||||
return &MemDatabase{
|
return &MemDatabase{
|
||||||
db: make(map[string][]byte),
|
db: make(map[string][]byte),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewMemDatabaseWithCap inits a mock levelDB instance with a map and sets a maximum size..
|
||||||
func NewMemDatabaseWithCap(size int) (*MemDatabase, error) {
|
func NewMemDatabaseWithCap(size int) (*MemDatabase, error) {
|
||||||
return &MemDatabase{
|
return &MemDatabase{
|
||||||
db: make(map[string][]byte, size),
|
db: make(map[string][]byte, size),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Put sets the value of the key.
|
||||||
func (db *MemDatabase) Put(key []byte, value []byte) error {
|
func (db *MemDatabase) Put(key []byte, value []byte) error {
|
||||||
db.lock.Lock()
|
db.lock.Lock()
|
||||||
defer db.lock.Unlock()
|
defer db.lock.Unlock()
|
||||||
|
|
@ -51,6 +56,7 @@ func (db *MemDatabase) Put(key []byte, value []byte) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Has checks if a given key exists.
|
||||||
func (db *MemDatabase) Has(key []byte) (bool, error) {
|
func (db *MemDatabase) Has(key []byte) (bool, error) {
|
||||||
db.lock.RLock()
|
db.lock.RLock()
|
||||||
defer db.lock.RUnlock()
|
defer db.lock.RUnlock()
|
||||||
|
|
@ -59,6 +65,7 @@ func (db *MemDatabase) Has(key []byte) (bool, error) {
|
||||||
return ok, nil
|
return ok, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get returns an error if the given key is not found.
|
||||||
func (db *MemDatabase) Get(key []byte) ([]byte, error) {
|
func (db *MemDatabase) Get(key []byte) ([]byte, error) {
|
||||||
db.lock.RLock()
|
db.lock.RLock()
|
||||||
defer db.lock.RUnlock()
|
defer db.lock.RUnlock()
|
||||||
|
|
@ -69,6 +76,7 @@ func (db *MemDatabase) Get(key []byte) ([]byte, error) {
|
||||||
return nil, errors.New("not found")
|
return nil, errors.New("not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Keys returns a list of all keys in db.db.
|
||||||
func (db *MemDatabase) Keys() [][]byte {
|
func (db *MemDatabase) Keys() [][]byte {
|
||||||
db.lock.RLock()
|
db.lock.RLock()
|
||||||
defer db.lock.RUnlock()
|
defer db.lock.RUnlock()
|
||||||
|
|
@ -80,6 +88,7 @@ func (db *MemDatabase) Keys() [][]byte {
|
||||||
return keys
|
return keys
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Delete deletes the key from db.db.
|
||||||
func (db *MemDatabase) Delete(key []byte) error {
|
func (db *MemDatabase) Delete(key []byte) error {
|
||||||
db.lock.Lock()
|
db.lock.Lock()
|
||||||
defer db.lock.Unlock()
|
defer db.lock.Unlock()
|
||||||
|
|
@ -88,12 +97,15 @@ func (db *MemDatabase) Delete(key []byte) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close performs no operation but imitates invocation of levelDB.Close().
|
||||||
func (db *MemDatabase) Close() {}
|
func (db *MemDatabase) Close() {}
|
||||||
|
|
||||||
|
// NewBatch sets memBatch.db equal to the receiver.
|
||||||
func (db *MemDatabase) NewBatch() Batch {
|
func (db *MemDatabase) NewBatch() Batch {
|
||||||
return &memBatch{db: db}
|
return &memBatch{db: db}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Len returns the number of keys in db.db.
|
||||||
func (db *MemDatabase) Len() int { return len(db.db) }
|
func (db *MemDatabase) Len() int { return len(db.db) }
|
||||||
|
|
||||||
type kv struct{ k, v []byte }
|
type kv struct{ k, v []byte }
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue