From 150779a590cabc1a9333127ba3cd7d4c86cd6b1c Mon Sep 17 00:00:00 2001 From: Kiel barry Date: Tue, 8 May 2018 12:35:05 -0700 Subject: [PATCH] ethdb: golint fixes and comments added from leveldb docs --- ethdb/database.go | 6 ++++++ ethdb/interface.go | 2 +- ethdb/memory_database.go | 12 ++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/ethdb/database.go b/ethdb/database.go index 001d8f0bb9..41357ea65c 100644 --- a/ethdb/database.go +++ b/ethdb/database.go @@ -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)} } diff --git a/ethdb/interface.go b/ethdb/interface.go index 5373120030..af75678f41 100644 --- a/ethdb/interface.go +++ b/ethdb/interface.go @@ -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 diff --git a/ethdb/memory_database.go b/ethdb/memory_database.go index 8efd7bf845..dd6d3c1965 100644 --- a/ethdb/memory_database.go +++ b/ethdb/memory_database.go @@ -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 }