ethdb: updates to pr

This commit is contained in:
kiel barry 2018-05-14 10:02:42 -07:00 committed by Kiel barry
parent 10e0f15146
commit 12e387cf74

View file

@ -23,24 +23,21 @@ import (
"github.com/ethereum/go-ethereum/common"
)
/*
* 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.
// MemDatabase is an in-memory key-value store.
type MemDatabase struct {
db map[string][]byte
lock sync.RWMutex
}
// NewMemDatabase inits a mock levelDB instance with a map.
// NewMemDatabase creates an in-memory key-value store.
func NewMemDatabase() *MemDatabase {
return &MemDatabase{
db: make(map[string][]byte),
}
}
// NewMemDatabaseWithCap inits a mock levelDB instance with a map and sets a maximum size..
// NewMemDatabaseWithCap creates an in-memory key-value store with a specific
// starting capacity item.
func NewMemDatabaseWithCap(size int) *MemDatabase {
return &MemDatabase{
db: make(map[string][]byte, size),
@ -64,7 +61,7 @@ func (db *MemDatabase) Has(key []byte) (bool, error) {
return ok, nil
}
// Get returns an error if the given key is not found.
// Get retrieves the value associated with a given key.
func (db *MemDatabase) Get(key []byte) ([]byte, error) {
db.lock.RLock()
defer db.lock.RUnlock()
@ -75,7 +72,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.
// Keys returns a list of all the keys present in the database.
func (db *MemDatabase) Keys() [][]byte {
db.lock.RLock()
defer db.lock.RUnlock()
@ -87,7 +84,7 @@ func (db *MemDatabase) Keys() [][]byte {
return keys
}
// Delete deletes the key from db.db.
// Delete removes the specified entry from the database.
func (db *MemDatabase) Delete(key []byte) error {
db.lock.Lock()
defer db.lock.Unlock()
@ -96,15 +93,16 @@ func (db *MemDatabase) Delete(key []byte) error {
return nil
}
// Close performs no operation but imitates invocation of levelDB.Close().
// Close implements the closer interface. For the memory database, this is a noop.
func (db *MemDatabase) Close() {}
// NewBatch sets memBatch.db equal to the receiver.
// NewBatch creates a memory buffer to group together database writes and flush
// them out in one go. Batches are not atomic, just an performance optimization.
func (db *MemDatabase) NewBatch() Batch {
return &memBatch{db: db}
}
// Len returns the number of keys in db.db.
// Len returns the number of entries in the database.
func (db *MemDatabase) Len() int { return len(db.db) }
type kv struct{ k, v []byte }