From 6064c80250f32dfc55a7bfd6113f4ed87c3fcd97 Mon Sep 17 00:00:00 2001 From: "Paul W. Homer" Date: Thu, 23 Mar 2017 11:37:46 -0400 Subject: [PATCH] Modified all db types to conform to the interface changes. --- c_level_db.go | 17 +++++++++++++++++ db.go | 5 +++-- go_level_db.go | 32 +++++++++++++++++++++++++------- mem_db.go | 49 +++++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 88 insertions(+), 15 deletions(-) diff --git a/c_level_db.go b/c_level_db.go index 6c87c2949..41ed4dc16 100644 --- a/c_level_db.go +++ b/c_level_db.go @@ -106,6 +106,23 @@ func (db *CLevelDB) Print() { } } +func (db *CLevelDB) Stats() map[string]string { + 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} diff --git a/db.go b/db.go index 9cd4e1c21..b88499092 100644 --- a/db.go +++ b/db.go @@ -12,9 +12,9 @@ type DB interface { NewBatch() Batch // For debugging - Iterator() Iterator - Next(Iterator) (key []byte, value []byte) Print() + Iterator() Iterator + Stats() map[string]string } type Batch interface { @@ -25,6 +25,7 @@ type Batch interface { type Iterator interface { Next() bool + Key() []byte Value() []byte } diff --git a/go_level_db.go b/go_level_db.go index 3020d4a03..ac936d01b 100644 --- a/go_level_db.go +++ b/go_level_db.go @@ -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,15 +93,30 @@ func (db *GoLevelDB) Print() { } } -func (db *GoLevelDB) Iterator() Iterator { - return db.db.NewIterator(nil, nil) -} +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.alibeiters", + } -func (db *GoLevelDB) Next(iter Iterator) ([]byte, []byte) { - if iter.Next() { - return iter.Key(), iter.Value() + stats := make(map[string]string) + for _, key := range keys { + str, err := db.db.GetProperty(key) + if err == nil { + stats[key] = str + } } - return nil, nil + return stats +} + +func (db *GoLevelDB) Iterator() Iterator { + return db.db.NewIterator(nil, nil) } func (db *GoLevelDB) NewBatch() Batch { diff --git a/mem_db.go b/mem_db.go index dcd86e6d5..ef410e88c 100644 --- a/mem_db.go +++ b/mem_db.go @@ -65,14 +65,51 @@ func (db *MemDB) Print() { } } -// TODO: needs to be wired to range db.db -func (db *MemDB) Iterator() Iterator { - return nil +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 (it *memDBIterator) Create(db *MemDB) *memDBIterator { + if it == nil { + it = &memDBIterator{} + } + it.db = db + it.last = -1 + + // unfortunately we need a copy of all of the keys + for key, _ := range db.db { + it.keys = append(it.keys, key) + } + return it } -// TODO: needs to be wired to range db.db -func (db *MemDB) Next(iter Iterator) (key []byte, value []byte) { - return nil, nil +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.db[it.keys[it.last]] +} + +func (db *MemDB) Iterator() Iterator { + var it *memDBIterator + return it.Create(db) } func (db *MemDB) NewBatch() Batch {