Browse Source

Merge pull request #5 from tendermint/iterate-data

Exposed an iterator to access all of the underlying key/value pairs for all databases.
pull/1842/head
Ethan Buchman 8 years ago
committed by GitHub
parent
commit
9643f60bc2
4 changed files with 103 additions and 0 deletions
  1. +18
    -0
      c_level_db.go
  2. +9
    -0
      db.go
  3. +29
    -0
      go_level_db.go
  4. +47
    -0
      mem_db.go

+ 18
- 0
c_level_db.go View File

@ -106,6 +106,24 @@ func (db *CLevelDB) Print() {
}
}
func (db *CLevelDB) Stats() map[string]string {
// TODO: Find the available properties for the C LevelDB implementation
keys := []string{}
stats := make(map[string]string)
for _, key := range keys {
str, err := db.db.GetProperty(key)
if err == nil {
stats[key] = str
}
}
return stats
}
func (db *CLevelDB) Iterator() Iterator {
return db.db.NewIterator(nil, nil)
}
func (db *CLevelDB) NewBatch() Batch {
batch := levigo.NewWriteBatch()
return &cLevelDBBatch{db, batch}


+ 9
- 0
db.go View File

@ -13,6 +13,8 @@ type DB interface {
// For debugging
Print()
Iterator() Iterator
Stats() map[string]string
}
type Batch interface {
@ -21,6 +23,13 @@ type Batch interface {
Write()
}
type Iterator interface {
Next() bool
Key() []byte
Value() []byte
}
//-----------------------------------------------------------------------------
const (


+ 29
- 0
go_level_db.go View File

@ -82,6 +82,9 @@ func (db *GoLevelDB) Close() {
}
func (db *GoLevelDB) Print() {
str, _ := db.db.GetProperty("leveldb.stats")
fmt.Printf("%v\n", str)
iter := db.db.NewIterator(nil, nil)
for iter.Next() {
key := iter.Key()
@ -90,6 +93,32 @@ func (db *GoLevelDB) Print() {
}
}
func (db *GoLevelDB) Stats() map[string]string {
keys := []string{
"leveldb.num-files-at-level{n}",
"leveldb.stats",
"leveldb.sstables",
"leveldb.blockpool",
"leveldb.cachedblock",
"leveldb.openedtables",
"leveldb.alivesnaps",
"leveldb.aliveiters",
}
stats := make(map[string]string)
for _, key := range keys {
str, err := db.db.GetProperty(key)
if err == nil {
stats[key] = str
}
}
return stats
}
func (db *GoLevelDB) Iterator() Iterator {
return db.db.NewIterator(nil, nil)
}
func (db *GoLevelDB) NewBatch() Batch {
batch := new(leveldb.Batch)
return &goLevelDBBatch{db, batch}


+ 47
- 0
mem_db.go View File

@ -65,6 +65,53 @@ func (db *MemDB) Print() {
}
}
func (db *MemDB) Stats() map[string]string {
stats := make(map[string]string)
stats["database.type"] = "memDB"
return stats
}
type memDBIterator struct {
last int
keys []string
db *MemDB
}
func newMemDBIterator() *memDBIterator {
return &memDBIterator{}
}
func (it *memDBIterator) Next() bool {
if it.last >= len(it.keys) {
return false
}
it.last++
return true
}
func (it *memDBIterator) Key() []byte {
return []byte(it.keys[it.last])
}
func (it *memDBIterator) Value() []byte {
return it.db.Get(it.Key())
}
func (db *MemDB) Iterator() Iterator {
it := newMemDBIterator()
it.db = db
it.last = -1
db.mtx.Lock()
defer db.mtx.Unlock()
// unfortunately we need a copy of all of the keys
for key, _ := range db.db {
it.keys = append(it.keys, key)
}
return it
}
func (db *MemDB) NewBatch() Batch {
return &memDBBatch{db, nil}
}


Loading…
Cancel
Save