fix db package and ALLCAPS lint

pull/75/head
Minh Doan 6 years ago
parent b77dd1a7cb
commit 5905760a60
  1. 18
      db/db.go
  2. 20
      db/db_test.go
  3. 2
      db/interface.go
  4. 16
      db/memory_db.go
  5. 4
      node/node_handler.go
  6. 8
      proto/common.go
  7. 8
      proto/consensus/consensus.go

@ -17,8 +17,7 @@ const (
writePauseWarningThrottler = 1 * time.Minute
)
var OpenFileLimit = 64
// LDBDatabase is database based on leveldb.
type LDBDatabase struct {
fn string // filename for reporting
db *leveldb.DB // LevelDB instance
@ -73,6 +72,7 @@ func (db *LDBDatabase) Put(key []byte, value []byte) error {
return db.db.Put(key, value, nil)
}
// Has is used to check if the given key is included into the database.
func (db *LDBDatabase) Has(key []byte) (bool, error) {
return db.db.Has(key, nil)
}
@ -91,6 +91,7 @@ func (db *LDBDatabase) Delete(key []byte) error {
return db.db.Delete(key, nil)
}
// NewIterator returns the current iterator of the db.
func (db *LDBDatabase) NewIterator() iterator.Iterator {
return db.db.NewIterator(nil, nil)
}
@ -100,6 +101,7 @@ func (db *LDBDatabase) NewIteratorWithPrefix(prefix []byte) iterator.Iterator {
return db.db.NewIterator(util.BytesPrefix(prefix), nil)
}
// Close closes the database.
func (db *LDBDatabase) Close() {
// Stop the metrics collection to avoid internal database races
db.quitLock.Lock()
@ -121,12 +123,15 @@ func (db *LDBDatabase) Close() {
}
}
// LDB returns the pointer to leveldb on which the LDBDatabase is built.
func (db *LDBDatabase) LDB() *leveldb.DB {
return db.db
}
// TODO(minhdoan): Might add meter func from ethereum-go repo
/* TODO(minhdoan): Might add meter func from ethereum-go repo
*/
// NewBatch returns Batch interface for a series of leveldb transactions.
func (db *LDBDatabase) NewBatch() Batch {
return &ldbBatch{db: db.db, b: new(leveldb.Batch)}
}
@ -137,26 +142,31 @@ type ldbBatch struct {
size int
}
// Put is used to put key, value into the batch of transactions.
func (b *ldbBatch) Put(key, value []byte) error {
b.b.Put(key, value)
b.size += len(value)
return nil
}
// Delete is used to delete the item associated with the given key as a part of the batch.
func (b *ldbBatch) Delete(key []byte) error {
b.b.Delete(key)
b.size += 1
b.size++
return nil
}
// Write writes the patch of transactions.
func (b *ldbBatch) Write() error {
return b.db.Write(b.b, nil)
}
// ValueSize returns the size of the patch.
func (b *ldbBatch) ValueSize() int {
return b.size
}
// Reset resets the batch.
func (b *ldbBatch) Reset() {
b.b.Reset()
b.size = 0

@ -26,7 +26,7 @@ func newTestLDB() (*LDBDatabase, func()) {
}
}
var test_values = []string{"", "a", "1251", "\x00123\x00"}
var testValues = []string{"", "a", "1251", "\x00123\x00"}
func TestLDB_PutGet(t *testing.T) {
db, remove := newTestLDB()
@ -41,14 +41,14 @@ func TestMemoryDB_PutGet(t *testing.T) {
func testPutGet(db Database, t *testing.T) {
t.Parallel()
for _, k := range test_values {
for _, k := range testValues {
err := db.Put([]byte(k), nil)
if err != nil {
t.Fatalf("put failed: %v", err)
}
}
for _, k := range test_values {
for _, k := range testValues {
data, err := db.Get([]byte(k))
if err != nil {
t.Fatalf("get failed: %v", err)
@ -63,14 +63,14 @@ func testPutGet(db Database, t *testing.T) {
t.Fatalf("expect to return a not found error")
}
for _, v := range test_values {
for _, v := range testValues {
err := db.Put([]byte(v), []byte(v))
if err != nil {
t.Fatalf("put failed: %v", err)
}
}
for _, v := range test_values {
for _, v := range testValues {
data, err := db.Get([]byte(v))
if err != nil {
t.Fatalf("get failed: %v", err)
@ -80,14 +80,14 @@ func testPutGet(db Database, t *testing.T) {
}
}
for _, v := range test_values {
for _, v := range testValues {
err := db.Put([]byte(v), []byte("?"))
if err != nil {
t.Fatalf("put override failed: %v", err)
}
}
for _, v := range test_values {
for _, v := range testValues {
data, err := db.Get([]byte(v))
if err != nil {
t.Fatalf("get failed: %v", err)
@ -97,7 +97,7 @@ func testPutGet(db Database, t *testing.T) {
}
}
for _, v := range test_values {
for _, v := range testValues {
orig, err := db.Get([]byte(v))
if err != nil {
t.Fatalf("get failed: %v", err)
@ -112,14 +112,14 @@ func testPutGet(db Database, t *testing.T) {
}
}
for _, v := range test_values {
for _, v := range testValues {
err := db.Delete([]byte(v))
if err != nil {
t.Fatalf("delete %q failed: %v", v, err)
}
}
for _, v := range test_values {
for _, v := range testValues {
_, err := db.Get([]byte(v))
if err == nil {
t.Fatalf("got deleted value %q", v)

@ -1,6 +1,6 @@
package db
// Code using batches should try to add this much data to the batch.
// IdealBatchSize is the max size of batch transactions.
// The value was determined empirically.
const IdealBatchSize = 100 * 1024

@ -7,26 +7,27 @@ import (
"github.com/simple-rules/harmony-benchmark/utils"
)
/*
* This is a test memory database. Do not use for any production it does not get persisted
*/
// MemDatabase is the test memory database. It won't be used for any production.
type MemDatabase struct {
db map[string][]byte
lock sync.RWMutex
}
// NewMemDatabase returns a pointer of the new creation of MemDatabase.
func NewMemDatabase() *MemDatabase {
return &MemDatabase{
db: make(map[string][]byte),
}
}
// NewMemDatabase returns a pointer of the new creation of MemDatabase with the given size.
func NewMemDatabaseWithCap(size int) *MemDatabase {
return &MemDatabase{
db: make(map[string][]byte, size),
}
}
// Put puts (key, value) item into MemDatabase.
func (db *MemDatabase) Put(key []byte, value []byte) error {
db.lock.Lock()
defer db.lock.Unlock()
@ -35,6 +36,7 @@ func (db *MemDatabase) Put(key []byte, value []byte) error {
return nil
}
// Has checks if the key is included into MemDatabase.
func (db *MemDatabase) Has(key []byte) (bool, error) {
db.lock.RLock()
defer db.lock.RUnlock()
@ -43,6 +45,7 @@ func (db *MemDatabase) Has(key []byte) (bool, error) {
return ok, nil
}
// Get gets value of the given key.
func (db *MemDatabase) Get(key []byte) ([]byte, error) {
db.lock.RLock()
defer db.lock.RUnlock()
@ -53,6 +56,7 @@ func (db *MemDatabase) Get(key []byte) ([]byte, error) {
return nil, errors.New("not found")
}
// Keys returns all keys of the given MemDatabase.
func (db *MemDatabase) Keys() [][]byte {
db.lock.RLock()
defer db.lock.RUnlock()
@ -64,6 +68,7 @@ func (db *MemDatabase) Keys() [][]byte {
return keys
}
// Delete deletes the given key.
func (db *MemDatabase) Delete(key []byte) error {
db.lock.Lock()
defer db.lock.Unlock()
@ -72,12 +77,15 @@ func (db *MemDatabase) Delete(key []byte) error {
return nil
}
// Close closes the given db.
func (db *MemDatabase) Close() {}
// NewBatch returns a batch of MemDatabase transactions.
func (db *MemDatabase) NewBatch() Batch {
return &memBatch{db: db}
}
// Len returns the length of the given db.
func (db *MemDatabase) Len() int { return len(db.db) }
type kv struct {
@ -99,7 +107,7 @@ func (b *memBatch) Put(key, value []byte) error {
func (b *memBatch) Delete(key []byte) error {
b.writes = append(b.writes, kv{utils.CopyBytes(key), nil, true})
b.size += 1
b.size++
return nil
}

@ -82,10 +82,10 @@ func (node *Node) NodeHandler(conn net.Conn) {
node.log.Error("Announce message should be sent to IdentityChain")
}
}
case proto.CONSENSUS:
case proto.Consensus:
actionType := consensus.ConsensusMessageType(msgType)
switch actionType {
case consensus.CONSENSUS:
case consensus.Consensus:
if consensusObj.IsLeader {
consensusObj.ProcessMessageLeader(msgPayload)
} else {

@ -9,10 +9,10 @@ The message structure of any message in Harmony network
---- content start -----
1 byte - message category
0x00: CONSENSUS
0x00: Consensus
0x01: NODE...
1 byte - message type
- for CONSENSUS category
- for Consensus category
0x00: consensus
0x01: sharding ...
- for NODE category
@ -24,9 +24,9 @@ n - 2 bytes - actual message payload
// The message category enum
type MessageCategory byte
//CONSENSUS and other message categories
//Consensus and other message categories
const (
CONSENSUS MessageCategory = iota
Consensus MessageCategory = iota
NODE
CLIENT
IDENTITY

@ -71,11 +71,11 @@ RESPONSE:
// the number of bytes consensus message type occupies
const ConsensusMessageTypeBytes = 1
// The specific types of message under CONSENSUS category
// The specific types of message under Consensus category
type ConsensusMessageType byte
const (
CONSENSUS ConsensusMessageType = iota
Consensus ConsensusMessageType = iota
// TODO: add more types
)
@ -133,8 +133,8 @@ func GetConsensusMessagePayload(message []byte) ([]byte, error) {
// Concatenate msgType as one byte with payload, and return the whole byte array
func ConstructConsensusMessage(consensusMsgType MessageType, payload []byte) []byte {
byteBuffer := bytes.NewBuffer([]byte{byte(proto.CONSENSUS)})
byteBuffer.WriteByte(byte(CONSENSUS))
byteBuffer := bytes.NewBuffer([]byte{byte(proto.Consensus)})
byteBuffer.WriteByte(byte(Consensus))
byteBuffer.WriteByte(byte(consensusMsgType))
byteBuffer.Write(payload)
return byteBuffer.Bytes()

Loading…
Cancel
Save