github.com/turingchain2020/turingchain@v1.1.21/common/db/go_mem_db.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package db 6 7 import ( 8 "bytes" 9 10 log "github.com/turingchain2020/turingchain/common/log/log15" 11 "github.com/turingchain2020/turingchain/types" 12 "github.com/syndtr/goleveldb/leveldb/comparer" 13 "github.com/syndtr/goleveldb/leveldb/memdb" 14 "github.com/syndtr/goleveldb/leveldb/util" 15 ) 16 17 var mlog = log.New("module", "db.memdb") 18 19 // memdb 应该无需区分同步与异步操作 20 21 func init() { 22 dbCreator := func(name string, dir string, cache int) (DB, error) { 23 return NewGoMemDB(name, dir, cache) 24 } 25 registerDBCreator(memDBBackendStr, dbCreator, false) 26 } 27 28 //GoMemDB db 29 type GoMemDB struct { 30 BaseDB 31 db *memdb.DB 32 } 33 34 //NewGoMemDB new 35 func NewGoMemDB(name string, dir string, cache int) (*GoMemDB, error) { 36 return &GoMemDB{ 37 db: memdb.New(comparer.DefaultComparer, 0), 38 }, nil 39 } 40 41 //Get get 42 func (db *GoMemDB) Get(key []byte) ([]byte, error) { 43 v, err := db.db.Get(key) 44 if err != nil { 45 return nil, ErrNotFoundInDb 46 } 47 return cloneByte(v), nil 48 } 49 50 //Set set 51 func (db *GoMemDB) Set(key []byte, value []byte) error { 52 err := db.db.Put(key, value) 53 if err != nil { 54 llog.Error("Set", "error", err) 55 return err 56 } 57 return nil 58 } 59 60 //SetSync 设置同步 61 func (db *GoMemDB) SetSync(key []byte, value []byte) error { 62 err := db.db.Put(key, value) 63 if err != nil { 64 llog.Error("SetSync", "error", err) 65 return err 66 } 67 return nil 68 } 69 70 //Delete 删除 71 func (db *GoMemDB) Delete(key []byte) error { 72 err := db.db.Delete(key) 73 if err != nil { 74 llog.Error("Delete", "error", err) 75 return err 76 } 77 return nil 78 } 79 80 //DeleteSync 删除同步 81 func (db *GoMemDB) DeleteSync(key []byte) error { 82 err := db.db.Delete(key) 83 if err != nil { 84 llog.Error("DeleteSync", "error", err) 85 return err 86 } 87 return nil 88 } 89 90 //DB db 91 func (db *GoMemDB) DB() *memdb.DB { 92 return db.db 93 } 94 95 //Close 关闭 96 func (db *GoMemDB) Close() { 97 } 98 99 //Print 打印 100 func (db *GoMemDB) Print() { 101 it := db.db.NewIterator(nil) 102 for it.Next() { 103 mlog.Info("Print", "key", string(it.Key()), "value", string(it.Value())) 104 } 105 } 106 107 //Stats ... 108 func (db *GoMemDB) Stats() map[string]string { 109 //TODO 110 return nil 111 } 112 113 //Iterator 迭代器 114 func (db *GoMemDB) Iterator(start []byte, end []byte, reverse bool) Iterator { 115 if end == nil { 116 end = bytesPrefix(start) 117 } 118 if bytes.Equal(end, types.EmptyValue) { 119 end = nil 120 } 121 r := &util.Range{Start: start, Limit: end} 122 it := db.db.NewIterator(r) 123 base := itBase{start, end, reverse} 124 return &goLevelDBIt{it, base} 125 } 126 127 type kv struct{ k, v []byte } 128 type memBatch struct { 129 db *GoMemDB 130 writes []kv 131 size int 132 len int 133 } 134 135 //NewBatch new 136 func (db *GoMemDB) NewBatch(sync bool) Batch { 137 return &memBatch{db: db} 138 } 139 140 func (b *memBatch) Set(key, value []byte) { 141 b.writes = append(b.writes, kv{cloneByte(key), cloneByte(value)}) 142 b.size += len(value) 143 b.size += len(key) 144 b.len += len(value) 145 } 146 147 func (b *memBatch) Delete(key []byte) { 148 b.writes = append(b.writes, kv{cloneByte(key), nil}) 149 b.size += len(key) 150 b.len++ 151 } 152 153 func (b *memBatch) Write() error { 154 var err error 155 for _, kv := range b.writes { 156 if kv.v == nil { 157 err = b.db.Delete(kv.k) 158 } else { 159 err = b.db.Set(kv.k, kv.v) 160 } 161 } 162 return err 163 } 164 165 func (b *memBatch) ValueSize() int { 166 return b.size 167 } 168 169 //ValueLen batch数量 170 func (b *memBatch) ValueLen() int { 171 return b.len 172 } 173 174 func (b *memBatch) Reset() { 175 //重置batch自己的buf,不能调用db reset,将直接清空db历史数据 176 b.writes = b.writes[:0] 177 b.size = 0 178 b.len = 0 179 } 180 181 func (b *memBatch) UpdateWriteSync(sync bool) { 182 }