mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 18:02:24 +00:00
ethdb: updates to pr
This commit is contained in:
parent
10e0f15146
commit
12e387cf74
1 changed files with 11 additions and 13 deletions
|
|
@ -23,24 +23,21 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
// MemDatabase is an in-memory key-value store.
|
||||||
* 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.
|
// NewMemDatabase creates an in-memory key-value store.
|
||||||
func NewMemDatabase() *MemDatabase {
|
func NewMemDatabase() *MemDatabase {
|
||||||
return &MemDatabase{
|
return &MemDatabase{
|
||||||
db: make(map[string][]byte),
|
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 {
|
func NewMemDatabaseWithCap(size int) *MemDatabase {
|
||||||
return &MemDatabase{
|
return &MemDatabase{
|
||||||
db: make(map[string][]byte, size),
|
db: make(map[string][]byte, size),
|
||||||
|
|
@ -64,7 +61,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.
|
// Get retrieves the value associated with a given key.
|
||||||
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()
|
||||||
|
|
@ -75,7 +72,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.
|
// Keys returns a list of all the keys present in the database.
|
||||||
func (db *MemDatabase) Keys() [][]byte {
|
func (db *MemDatabase) Keys() [][]byte {
|
||||||
db.lock.RLock()
|
db.lock.RLock()
|
||||||
defer db.lock.RUnlock()
|
defer db.lock.RUnlock()
|
||||||
|
|
@ -87,7 +84,7 @@ func (db *MemDatabase) Keys() [][]byte {
|
||||||
return keys
|
return keys
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete deletes the key from db.db.
|
// Delete removes the specified entry from the database.
|
||||||
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()
|
||||||
|
|
@ -96,15 +93,16 @@ func (db *MemDatabase) Delete(key []byte) error {
|
||||||
return nil
|
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() {}
|
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 {
|
func (db *MemDatabase) NewBatch() Batch {
|
||||||
return &memBatch{db: db}
|
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) }
|
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