forked from hyperledger-archives/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_db.go
103 lines (82 loc) · 2.04 KB
/
cache_db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package storage
import (
dbm "github.com/tendermint/tendermint/libs/db"
)
type CacheDB struct {
cache *KVCache
backend KVIterableReader
}
func NewCacheDB(backend KVIterableReader) *CacheDB {
return &CacheDB{
cache: NewKVCache(),
backend: backend,
}
}
// DB implementation
func (cdb *CacheDB) Get(key []byte) []byte {
value, deleted := cdb.cache.Info(key)
if deleted {
return nil
}
if value != nil {
return value
}
return cdb.backend.Get(key)
}
func (cdb *CacheDB) Has(key []byte) bool {
value, deleted := cdb.cache.Info(key)
return !deleted && (value != nil || cdb.backend.Has(key))
}
func (cdb *CacheDB) Iterator(low, high []byte) KVIterator {
// Keys from cache will sort first because of order in MultiIterator and Uniq will take the first KVs so KVs
// appearing in cache will override values from backend.
return Uniq(NewMultiIterator(false, cdb.cache.Iterator(low, high), cdb.backend.Iterator(low, high)))
}
func (cdb *CacheDB) ReverseIterator(low, high []byte) KVIterator {
return Uniq(NewMultiIterator(true, cdb.cache.ReverseIterator(low, high), cdb.backend.ReverseIterator(low, high)))
}
func (cdb *CacheDB) Set(key, value []byte) {
cdb.cache.Set(key, value)
}
func (cdb *CacheDB) SetSync(key, value []byte) {
cdb.cache.Set(key, value)
}
func (cdb *CacheDB) Delete(key []byte) {
cdb.cache.Delete(key)
}
func (cdb *CacheDB) DeleteSync(key []byte) {
cdb.Delete(key)
}
func (cdb *CacheDB) Close() {
}
func (cdb *CacheDB) NewBatch() dbm.Batch {
return &cacheBatch{
cache: NewKVCache(),
backend: cdb,
}
}
func (cdb *CacheDB) Commit(writer KVWriter) {
cdb.cache.WriteTo(writer)
cdb.cache.Reset()
}
type cacheBatch struct {
cache *KVCache
backend *CacheDB
}
func (cb *cacheBatch) Set(key, value []byte) {
cb.cache.Set(key, value)
}
func (cb *cacheBatch) Delete(key []byte) {
cb.cache.Delete(key)
}
func (cb *cacheBatch) Write() {
cb.cache.WriteTo(cb.backend)
}
func (cb *cacheBatch) WriteSync() {
cb.Write()
}
func (cdb *CacheDB) Print() {
}
func (cdb *CacheDB) Stats() map[string]string {
return map[string]string{}
}