github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/database/leveldb/go_level_db.go (about) 1 package db 2 3 import ( 4 "fmt" 5 "path" 6 7 "github.com/syndtr/goleveldb/leveldb" 8 "github.com/syndtr/goleveldb/leveldb/errors" 9 "github.com/syndtr/goleveldb/leveldb/iterator" 10 "github.com/syndtr/goleveldb/leveldb/opt" 11 "github.com/syndtr/goleveldb/leveldb/util" 12 13 . "github.com/tendermint/tmlibs/common" 14 ) 15 16 func init() { 17 dbCreator := func(name string, dir string) (DB, error) { 18 return NewGoLevelDB(name, dir) 19 } 20 registerDBCreator(LevelDBBackendStr, dbCreator, false) 21 registerDBCreator(GoLevelDBBackendStr, dbCreator, false) 22 } 23 24 type GoLevelDB struct { 25 db *leveldb.DB 26 } 27 28 func NewGoLevelDB(name string, dir string) (*GoLevelDB, error) { 29 dbPath := path.Join(dir, name+".db") 30 db, err := leveldb.OpenFile(dbPath, nil) 31 if err != nil { 32 return nil, err 33 } 34 database := &GoLevelDB{db: db} 35 return database, nil 36 } 37 38 func (db *GoLevelDB) Get(key []byte) []byte { 39 res, err := db.db.Get(key, nil) 40 if err != nil { 41 if err == errors.ErrNotFound { 42 return nil 43 } else { 44 PanicCrisis(err) 45 } 46 } 47 return res 48 } 49 50 func (db *GoLevelDB) Set(key []byte, value []byte) { 51 err := db.db.Put(key, value, nil) 52 if err != nil { 53 PanicCrisis(err) 54 } 55 } 56 57 func (db *GoLevelDB) SetSync(key []byte, value []byte) { 58 err := db.db.Put(key, value, &opt.WriteOptions{Sync: true}) 59 if err != nil { 60 PanicCrisis(err) 61 } 62 } 63 64 func (db *GoLevelDB) Delete(key []byte) { 65 err := db.db.Delete(key, nil) 66 if err != nil { 67 PanicCrisis(err) 68 } 69 } 70 71 func (db *GoLevelDB) DeleteSync(key []byte) { 72 err := db.db.Delete(key, &opt.WriteOptions{Sync: true}) 73 if err != nil { 74 PanicCrisis(err) 75 } 76 } 77 78 func (db *GoLevelDB) DB() *leveldb.DB { 79 return db.db 80 } 81 82 func (db *GoLevelDB) Close() { 83 db.db.Close() 84 } 85 86 func (db *GoLevelDB) Print() { 87 str, _ := db.db.GetProperty("leveldb.stats") 88 fmt.Printf("%v\n", str) 89 90 iter := db.db.NewIterator(nil, nil) 91 for iter.Next() { 92 key := iter.Key() 93 value := iter.Value() 94 fmt.Printf("[%X]:\t[%X]\n", key, value) 95 } 96 } 97 98 func (db *GoLevelDB) Stats() map[string]string { 99 keys := []string{ 100 "leveldb.num-files-at-level{n}", 101 "leveldb.stats", 102 "leveldb.sstables", 103 "leveldb.blockpool", 104 "leveldb.cachedblock", 105 "leveldb.openedtables", 106 "leveldb.alivesnaps", 107 "leveldb.aliveiters", 108 } 109 110 stats := make(map[string]string) 111 for _, key := range keys { 112 str, err := db.db.GetProperty(key) 113 if err == nil { 114 stats[key] = str 115 } 116 } 117 return stats 118 } 119 120 type goLevelDBIterator struct { 121 source iterator.Iterator 122 } 123 124 // Key returns a copy of the current key. 125 func (it *goLevelDBIterator) Key() []byte { 126 key := it.source.Key() 127 k := make([]byte, len(key)) 128 copy(k, key) 129 130 return k 131 } 132 133 // Value returns a copy of the current value. 134 func (it *goLevelDBIterator) Value() []byte { 135 val := it.source.Value() 136 v := make([]byte, len(val)) 137 copy(v, val) 138 139 return v 140 } 141 142 func (it *goLevelDBIterator) Seek(point []byte) bool { 143 return it.source.Seek(point) 144 } 145 146 func (it *goLevelDBIterator) Error() error { 147 return it.source.Error() 148 } 149 150 func (it *goLevelDBIterator) Next() bool { 151 return it.source.Next() 152 } 153 154 func (it *goLevelDBIterator) Release() { 155 it.source.Release() 156 } 157 158 func (db *GoLevelDB) Iterator() Iterator { 159 return &goLevelDBIterator{db.db.NewIterator(nil, nil)} 160 } 161 162 func (db *GoLevelDB) IteratorPrefix(prefix []byte) Iterator { 163 return &goLevelDBIterator{db.db.NewIterator(util.BytesPrefix(prefix), nil)} 164 } 165 166 func (db *GoLevelDB) NewBatch() Batch { 167 batch := new(leveldb.Batch) 168 return &goLevelDBBatch{db, batch} 169 } 170 171 //-------------------------------------------------------------------------------- 172 173 type goLevelDBBatch struct { 174 db *GoLevelDB 175 batch *leveldb.Batch 176 } 177 178 func (mBatch *goLevelDBBatch) Set(key, value []byte) { 179 mBatch.batch.Put(key, value) 180 } 181 182 func (mBatch *goLevelDBBatch) Delete(key []byte) { 183 mBatch.batch.Delete(key) 184 } 185 186 func (mBatch *goLevelDBBatch) Write() { 187 err := mBatch.db.db.Write(mBatch.batch, nil) 188 if err != nil { 189 PanicCrisis(err) 190 } 191 }