Browse Source

Implement DebugDB (#166)

pull/1780/head
Jae Kwon 7 years ago
committed by GitHub
parent
commit
b0e0dc5de3
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 239 additions and 15 deletions
  1. +3
    -3
      db/common_test.go
  2. +8
    -4
      db/db_test.go
  3. +216
    -0
      db/debug_db.go
  4. +4
    -3
      db/mem_batch.go
  5. +5
    -5
      db/prefix_db.go
  6. +3
    -0
      merkle/simple_tree.go

+ 3
- 3
db/common_test.go View File

@ -107,11 +107,11 @@ func (mdb *mockDB) SetNoLockSync([]byte, []byte) {
mdb.calls["SetNoLockSync"] += 1 mdb.calls["SetNoLockSync"] += 1
} }
func (mdb *mockDB) Delete([]byte, []byte) {
func (mdb *mockDB) Delete([]byte) {
mdb.calls["Delete"] += 1 mdb.calls["Delete"] += 1
} }
func (mdb *mockDB) DeleteSync([]byte, []byte) {
func (mdb *mockDB) DeleteSync([]byte) {
mdb.calls["DeleteSync"] += 1 mdb.calls["DeleteSync"] += 1
} }
@ -144,7 +144,7 @@ func (mdb *mockDB) NewBatch() Batch {
func (mdb *mockDB) Print() { func (mdb *mockDB) Print() {
mdb.calls["Print"] += 1 mdb.calls["Print"] += 1
fmt.Sprintf("mockDB{%v}", mdb.Stats())
fmt.Printf("mockDB{%v}", mdb.Stats())
} }
func (mdb *mockDB) Stats() map[string]string { func (mdb *mockDB) Stats() map[string]string {


+ 8
- 4
db/db_test.go View File

@ -111,7 +111,8 @@ func TestDBIteratorNonemptyBeginAfter(t *testing.T) {
func TestDBBatchWrite1(t *testing.T) { func TestDBBatchWrite1(t *testing.T) {
mdb := newMockDB() mdb := newMockDB()
batch := mdb.NewBatch()
ddb := NewDebugDB(t.Name(), mdb)
batch := ddb.NewBatch()
batch.Set(bz("1"), bz("1")) batch.Set(bz("1"), bz("1"))
batch.Set(bz("2"), bz("2")) batch.Set(bz("2"), bz("2"))
@ -131,7 +132,8 @@ func TestDBBatchWrite1(t *testing.T) {
func TestDBBatchWrite2(t *testing.T) { func TestDBBatchWrite2(t *testing.T) {
mdb := newMockDB() mdb := newMockDB()
batch := mdb.NewBatch()
ddb := NewDebugDB(t.Name(), mdb)
batch := ddb.NewBatch()
batch.Set(bz("1"), bz("1")) batch.Set(bz("1"), bz("1"))
batch.Set(bz("2"), bz("2")) batch.Set(bz("2"), bz("2"))
@ -151,7 +153,8 @@ func TestDBBatchWrite2(t *testing.T) {
func TestDBBatchWriteSync1(t *testing.T) { func TestDBBatchWriteSync1(t *testing.T) {
mdb := newMockDB() mdb := newMockDB()
batch := mdb.NewBatch()
ddb := NewDebugDB(t.Name(), mdb)
batch := ddb.NewBatch()
batch.Set(bz("1"), bz("1")) batch.Set(bz("1"), bz("1"))
batch.Set(bz("2"), bz("2")) batch.Set(bz("2"), bz("2"))
@ -171,7 +174,8 @@ func TestDBBatchWriteSync1(t *testing.T) {
func TestDBBatchWriteSync2(t *testing.T) { func TestDBBatchWriteSync2(t *testing.T) {
mdb := newMockDB() mdb := newMockDB()
batch := mdb.NewBatch()
ddb := NewDebugDB(t.Name(), mdb)
batch := ddb.NewBatch()
batch.Set(bz("1"), bz("1")) batch.Set(bz("1"), bz("1"))
batch.Set(bz("2"), bz("2")) batch.Set(bz("2"), bz("2"))


+ 216
- 0
db/debug_db.go View File

@ -0,0 +1,216 @@
package db
import (
"fmt"
"sync"
)
//----------------------------------------
// debugDB
type debugDB struct {
label string
db DB
}
// For printing all operationgs to the console for debugging.
func NewDebugDB(label string, db DB) debugDB {
return debugDB{
label: label,
db: db,
}
}
// Implements atomicSetDeleter.
func (ddb debugDB) Mutex() *sync.Mutex { return nil }
// Implements DB.
func (ddb debugDB) Get(key []byte) (value []byte) {
defer fmt.Printf("%v.Get(%X) %X\n", ddb.label, key, value)
value = ddb.db.Get(key)
return
}
// Implements DB.
func (ddb debugDB) Has(key []byte) (has bool) {
defer fmt.Printf("%v.Has(%X) %v\n", ddb.label, key, has)
return ddb.db.Has(key)
}
// Implements DB.
func (ddb debugDB) Set(key []byte, value []byte) {
fmt.Printf("%v.Set(%X, %X)\n", ddb.label, key, value)
ddb.db.Set(key, value)
}
// Implements DB.
func (ddb debugDB) SetSync(key []byte, value []byte) {
fmt.Printf("%v.SetSync(%X, %X)\n", ddb.label, key, value)
ddb.db.SetSync(key, value)
}
// Implements atomicSetDeleter.
func (ddb debugDB) SetNoLock(key []byte, value []byte) {
fmt.Printf("%v.SetNoLock(%X, %X)\n", ddb.label, key, value)
ddb.db.Set(key, value)
}
// Implements atomicSetDeleter.
func (ddb debugDB) SetNoLockSync(key []byte, value []byte) {
fmt.Printf("%v.SetNoLockSync(%X, %X)\n", ddb.label, key, value)
ddb.db.SetSync(key, value)
}
// Implements DB.
func (ddb debugDB) Delete(key []byte) {
fmt.Printf("%v.Delete(%X)\n", ddb.label, key)
ddb.db.Delete(key)
}
// Implements DB.
func (ddb debugDB) DeleteSync(key []byte) {
fmt.Printf("%v.DeleteSync(%X)\n", ddb.label, key)
ddb.db.DeleteSync(key)
}
// Implements atomicSetDeleter.
func (ddb debugDB) DeleteNoLock(key []byte) {
fmt.Printf("%v.DeleteNoLock(%X)\n", ddb.label, key)
ddb.db.Delete(key)
}
// Implements atomicSetDeleter.
func (ddb debugDB) DeleteNoLockSync(key []byte) {
fmt.Printf("%v.DeleteNoLockSync(%X)\n", ddb.label, key)
ddb.db.DeleteSync(key)
}
// Implements DB.
func (ddb debugDB) Iterator(start, end []byte) Iterator {
fmt.Printf("%v.Iterator(%X, %X)\n", ddb.label, start, end)
return NewDebugIterator(ddb.label, ddb.db.Iterator(start, end))
}
// Implements DB.
func (ddb debugDB) ReverseIterator(start, end []byte) Iterator {
fmt.Printf("%v.ReverseIterator(%X, %X)\n", ddb.label, start, end)
return NewDebugIterator(ddb.label, ddb.db.ReverseIterator(start, end))
}
// Implements DB.
func (ddb debugDB) NewBatch() Batch {
fmt.Printf("%v.NewBatch()\n", ddb.label)
return NewDebugBatch(ddb.label, ddb.db.NewBatch())
}
// Implements DB.
func (ddb debugDB) Close() {
fmt.Printf("%v.Close()\n", ddb.label)
ddb.db.Close()
}
// Implements DB.
func (ddb debugDB) Print() {
ddb.db.Print()
}
// Implements DB.
func (ddb debugDB) Stats() map[string]string {
return ddb.db.Stats()
}
//----------------------------------------
// debugIterator
type debugIterator struct {
label string
itr Iterator
}
// For printing all operationgs to the console for debugging.
func NewDebugIterator(label string, itr Iterator) debugIterator {
return debugIterator{
label: label,
itr: itr,
}
}
// Implements Iterator.
func (ditr debugIterator) Domain() (start []byte, end []byte) {
defer fmt.Printf("%v.itr.Domain() (%X,%X)\n", ditr.label, start, end)
start, end = ditr.itr.Domain()
return
}
// Implements Iterator.
func (ditr debugIterator) Valid() (ok bool) {
defer fmt.Printf("%v.itr.Valid() %v\n", ditr.label, ok)
ok = ditr.itr.Valid()
return
}
// Implements Iterator.
func (ditr debugIterator) Next() {
fmt.Printf("%v.itr.Next()\n", ditr.label)
ditr.itr.Next()
}
// Implements Iterator.
func (ditr debugIterator) Key() (key []byte) {
fmt.Printf("%v.itr.Key() %X\n", ditr.label, key)
key = ditr.itr.Key()
return
}
// Implements Iterator.
func (ditr debugIterator) Value() (value []byte) {
fmt.Printf("%v.itr.Value() %X\n", ditr.label, value)
value = ditr.itr.Value()
return
}
// Implements Iterator.
func (ditr debugIterator) Close() {
fmt.Printf("%v.itr.Close()\n", ditr.label)
ditr.itr.Close()
}
//----------------------------------------
// debugBatch
type debugBatch struct {
label string
bch Batch
}
// For printing all operationgs to the console for debugging.
func NewDebugBatch(label string, bch Batch) debugBatch {
return debugBatch{
label: label,
bch: bch,
}
}
// Implements Batch.
func (dbch debugBatch) Set(key, value []byte) {
fmt.Printf("%v.batch.Set(%X, %X)\n", dbch.label, key, value)
dbch.bch.Set(key, value)
}
// Implements Batch.
func (dbch debugBatch) Delete(key []byte) {
fmt.Printf("%v.batch.Delete(%X)\n", dbch.label, key)
dbch.bch.Delete(key)
}
// Implements Batch.
func (dbch debugBatch) Write() {
fmt.Printf("%v.batch.Write()\n", dbch.label)
dbch.bch.Write()
}
// Implements Batch.
func (dbch debugBatch) WriteSync() {
fmt.Printf("%v.batch.WriteSync()\n", dbch.label)
dbch.bch.WriteSync()
}

+ 4
- 3
db/mem_batch.go View File

@ -45,9 +45,10 @@ func (mBatch *memBatch) WriteSync() {
} }
func (mBatch *memBatch) write(doSync bool) { func (mBatch *memBatch) write(doSync bool) {
mtx := mBatch.db.Mutex()
mtx.Lock()
defer mtx.Unlock()
if mtx := mBatch.db.Mutex(); mtx != nil {
mtx.Lock()
defer mtx.Unlock()
}
for i, op := range mBatch.ops { for i, op := range mBatch.ops {
if doSync && i == (len(mBatch.ops)-1) { if doSync && i == (len(mBatch.ops)-1) {


+ 5
- 5
db/prefix_db.go View File

@ -123,10 +123,10 @@ func (pdb *prefixDB) Iterator(start, end []byte) Iterator {
pdb.mtx.Lock() pdb.mtx.Lock()
defer pdb.mtx.Unlock() defer pdb.mtx.Unlock()
pstart := append([]byte(pdb.prefix), start...)
pstart := append(pdb.prefix, start...)
pend := []byte(nil) pend := []byte(nil)
if end != nil { if end != nil {
pend = append([]byte(pdb.prefix), end...)
pend = append(pdb.prefix, end...)
} }
return newUnprefixIterator( return newUnprefixIterator(
pdb.prefix, pdb.prefix,
@ -144,11 +144,11 @@ func (pdb *prefixDB) ReverseIterator(start, end []byte) Iterator {
pstart := []byte(nil) pstart := []byte(nil)
if start != nil { if start != nil {
pstart = append([]byte(pdb.prefix), start...)
pstart = append(pdb.prefix, start...)
} }
pend := []byte(nil) pend := []byte(nil)
if end != nil { if end != nil {
pend = append([]byte(pdb.prefix), end...)
pend = append(pdb.prefix, end...)
} }
return newUnprefixIterator( return newUnprefixIterator(
pdb.prefix, pdb.prefix,
@ -201,7 +201,7 @@ func (pdb *prefixDB) Stats() map[string]string {
} }
func (pdb *prefixDB) prefixed(key []byte) []byte { func (pdb *prefixDB) prefixed(key []byte) []byte {
return append([]byte(pdb.prefix), key...)
return append(pdb.prefix, key...)
} }
//---------------------------------------- //----------------------------------------


+ 3
- 0
merkle/simple_tree.go View File

@ -31,6 +31,9 @@ import (
func SimpleHashFromTwoHashes(left []byte, right []byte) []byte { func SimpleHashFromTwoHashes(left []byte, right []byte) []byte {
var hasher = ripemd160.New() var hasher = ripemd160.New()
err := encodeByteSlice(hasher, left) err := encodeByteSlice(hasher, left)
if err != nil {
panic(err)
}
err = encodeByteSlice(hasher, right) err = encodeByteSlice(hasher, right)
if err != nil { if err != nil {
panic(err) panic(err)


Loading…
Cancel
Save