github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/forensics/storage/cache_db.go (about) 1 package storage 2 3 import ( 4 "github.com/hyperledger/burrow/storage" 5 dbm "github.com/tendermint/tm-db" 6 ) 7 8 type CacheDB struct { 9 cache *KVCache 10 backend storage.KVIterableReader 11 } 12 13 func NewCacheDB(backend storage.KVIterableReader) *CacheDB { 14 return &CacheDB{ 15 cache: NewKVCache(), 16 backend: backend, 17 } 18 } 19 20 // DB implementation 21 func (cdb *CacheDB) Get(key []byte) ([]byte, error) { 22 value, deleted := cdb.cache.Info(key) 23 if deleted { 24 return nil, nil 25 } 26 if value != nil { 27 return value, nil 28 } 29 return cdb.backend.Get(key) 30 } 31 32 func (cdb *CacheDB) Has(key []byte) (bool, error) { 33 value, deleted := cdb.cache.Info(key) 34 has, err := cdb.backend.Has(key) 35 if err != nil { 36 return false, err 37 } 38 return !deleted && (value != nil || has), nil 39 } 40 41 func (cdb *CacheDB) Iterator(low, high []byte) (storage.KVIterator, error) { 42 // Keys from cache will sort first because of order in MultiIterator and Uniq will take the first KVs so KVs 43 // appearing in cache will override values from backend. 44 iterator, err := cdb.backend.Iterator(low, high) 45 if err != nil { 46 return nil, err 47 } 48 49 return Uniq(NewMultiIterator(false, cdb.cache.Iterator(low, high), iterator)), nil 50 } 51 52 func (cdb *CacheDB) ReverseIterator(low, high []byte) (storage.KVIterator, error) { 53 iterator, err := cdb.backend.ReverseIterator(low, high) 54 if err != nil { 55 return nil, err 56 } 57 58 return Uniq(NewMultiIterator(true, cdb.cache.ReverseIterator(low, high), iterator)), nil 59 } 60 61 func (cdb *CacheDB) Set(key, value []byte) error { 62 cdb.cache.Set(key, value) 63 return nil 64 } 65 66 func (cdb *CacheDB) SetSync(key, value []byte) error { 67 cdb.cache.Set(key, value) 68 return nil 69 } 70 71 func (cdb *CacheDB) Delete(key []byte) error { 72 cdb.cache.Delete(key) 73 return nil 74 } 75 76 func (cdb *CacheDB) DeleteSync(key []byte) error { 77 return cdb.Delete(key) 78 } 79 80 func (cdb *CacheDB) Close() error { 81 return nil 82 } 83 84 func (cdb *CacheDB) NewBatch() dbm.Batch { 85 return &cacheBatch{ 86 cache: NewKVCache(), 87 backend: cdb, 88 } 89 } 90 91 func (cdb *CacheDB) Commit(writer storage.KVWriter) { 92 cdb.cache.WriteTo(writer) 93 cdb.cache.Reset() 94 } 95 96 type cacheBatch struct { 97 cache *KVCache 98 backend *CacheDB 99 } 100 101 func (cb *cacheBatch) Set(key, value []byte) error { 102 cb.cache.Set(key, value) 103 return nil 104 } 105 106 func (cb *cacheBatch) Delete(key []byte) error { 107 cb.cache.Delete(key) 108 return nil 109 } 110 111 func (cb *cacheBatch) Write() error { 112 cb.cache.WriteTo(cb.backend) 113 return nil 114 } 115 116 func (cb *cacheBatch) Close() error { 117 return nil 118 } 119 120 func (cb *cacheBatch) WriteSync() error { 121 return cb.Write() 122 } 123 124 func (cdb *CacheDB) Print() error { 125 return nil 126 } 127 128 func (cdb *CacheDB) Stats() map[string]string { 129 return map[string]string{} 130 }