-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathbacked_map.go
More file actions
113 lines (96 loc) · 2.1 KB
/
Copy pathbacked_map.go
File metadata and controls
113 lines (96 loc) · 2.1 KB
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
104
105
106
107
108
109
110
111
112
113
package vecmt
import (
"github.com/Fantom-foundation/lachesis-base/kvdb"
"github.com/ethereum/go-ethereum/common"
)
type backedMap struct {
cache map[string][]byte
backup kvdb.Store
memSize int
maxMemSize int
batchSize int
}
func newBackedMap(backup kvdb.Store, maxMemSize, batchSize int) *backedMap {
return &backedMap{
cache: make(map[string][]byte),
backup: backup,
maxMemSize: maxMemSize,
batchSize: batchSize,
}
}
func (w *backedMap) has(key []byte) (bool, error) {
if _, ok := w.cache[string(key)]; ok {
return true, nil
}
val, err := w.backup.Get(key)
if err != nil {
return false, err
}
return val != nil, nil
}
func (w *backedMap) get(key []byte) ([]byte, error) {
if val, ok := w.cache[string(key)]; ok {
return common.CopyBytes(val), nil
}
return w.backup.Get(key)
}
func (w *backedMap) close() error {
batch := w.backup.NewBatch()
defer batch.Reset()
for key, val := range w.cache {
err := batch.Put([]byte(key), val)
if err != nil {
return err
}
}
err := batch.Write()
if err != nil {
return err
}
w.cache = nil
// backing kvdb.Store not closed here intentionally (is a table)
return nil
}
func (w *backedMap) add(key string, val []byte) {
lenBefore := len(w.cache)
w.cache[key] = val
// TODO it works correctly only if new key/value have the same size (which is practically true currently)
if len(w.cache) > lenBefore {
w.memSize += mapMemEst(len(key), len(val))
}
}
// mayUnload evicts and flushes one batch of data
func (w *backedMap) mayUnload() error {
for w.memSize > w.maxMemSize {
err := w.unload(w.batchSize)
if err != nil {
return err
}
}
return nil
}
func (w *backedMap) unload(toUnload int) error {
batch := w.backup.NewBatch()
defer batch.Reset()
for key, val := range w.cache {
err := batch.Put([]byte(key), val)
if err != nil {
return err
}
delete(w.cache, key)
rmS := mapMemEst(len(key), len(val))
if rmS <= w.memSize {
w.memSize -= rmS
} else {
w.memSize = 0
}
if batch.ValueSize() >= toUnload {
break
}
}
err := batch.Write()
if err != nil {
return err
}
return nil
}