Skip to content

Commit

Permalink
ethdb: golint fixes and comments added from leveldb docs
Browse files Browse the repository at this point in the history
  • Loading branch information
kielbarry committed May 14, 2018
1 parent c4a4613 commit 150779a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
6 changes: 6 additions & 0 deletions ethdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)}
}
Expand Down
2 changes: 1 addition & 1 deletion ethdb/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions ethdb/memory_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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 }
Expand Down

0 comments on commit 150779a

Please sign in to comment.