ethdb: golint fixes and comments added from leveldb docs

This commit is contained in:
Kiel barry 2018-05-08 12:35:05 -07:00
parent c4a4613d95
commit 150779a590
3 changed files with 19 additions and 1 deletions

View file

@ -103,6 +103,8 @@ func (db *LDBDatabase) Put(key []byte, value []byte) error {
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) {
return db.db.Has(key, nil)
}
@ -121,6 +123,8 @@ func (db *LDBDatabase) Delete(key []byte) error {
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 {
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 {
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 {
return &ldbBatch{db: db.db, b: new(leveldb.Batch)}
}

View file

@ -16,7 +16,7 @@
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.
const IdealBatchSize = 100 * 1024

View file

@ -26,23 +26,28 @@ import (
/*
* 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 {
db map[string][]byte
lock sync.RWMutex
}
// NewMemDatabase inits a mock levelDB instance with a map.
func NewMemDatabase() (*MemDatabase, error) {
return &MemDatabase{
db: make(map[string][]byte),
}, nil
}
// NewMemDatabaseWithCap inits a mock levelDB instance with a map and sets a maximum size..
func NewMemDatabaseWithCap(size int) (*MemDatabase, error) {
return &MemDatabase{
db: make(map[string][]byte, size),
}, nil
}
// Put sets the value of the key.
func (db *MemDatabase) Put(key []byte, value []byte) error {
db.lock.Lock()
defer db.lock.Unlock()
@ -51,6 +56,7 @@ func (db *MemDatabase) Put(key []byte, value []byte) error {
return nil
}
// Has checks if a given key exists.
func (db *MemDatabase) Has(key []byte) (bool, error) {
db.lock.RLock()
defer db.lock.RUnlock()
@ -59,6 +65,7 @@ func (db *MemDatabase) Has(key []byte) (bool, error) {
return ok, nil
}
// Get returns an error if the given key is not found.
func (db *MemDatabase) Get(key []byte) ([]byte, error) {
db.lock.RLock()
defer db.lock.RUnlock()
@ -69,6 +76,7 @@ func (db *MemDatabase) Get(key []byte) ([]byte, error) {
return nil, errors.New("not found")
}
// Keys returns a list of all keys in db.db.
func (db *MemDatabase) Keys() [][]byte {
db.lock.RLock()
defer db.lock.RUnlock()
@ -80,6 +88,7 @@ func (db *MemDatabase) Keys() [][]byte {
return keys
}
// Delete deletes the key from db.db.
func (db *MemDatabase) Delete(key []byte) error {
db.lock.Lock()
defer db.lock.Unlock()
@ -88,12 +97,15 @@ func (db *MemDatabase) Delete(key []byte) error {
return nil
}
// Close performs no operation but imitates invocation of levelDB.Close().
func (db *MemDatabase) Close() {}
// NewBatch sets memBatch.db equal to the receiver.
func (db *MemDatabase) NewBatch() Batch {
return &memBatch{db: db}
}
// Len returns the number of keys in db.db.
func (db *MemDatabase) Len() int { return len(db.db) }
type kv struct{ k, v []byte }