-
Notifications
You must be signed in to change notification settings - Fork 9
/
trie_cache.go
62 lines (56 loc) · 1.44 KB
/
trie_cache.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
/**
* @file
* @copyright defined in aergo/LICENSE.txt
*/
package trie
import (
//"fmt"
"sync"
"github.com/aergoio/aergo-lib/db"
)
type CacheDB struct {
// liveCache contains the first levels of the trie (nodes that have 2 non default children)
liveCache map[Hash][][]byte
// liveMux is a lock for liveCache
liveMux sync.RWMutex
// updatedNodes that have will be flushed to disk
updatedNodes map[Hash][][]byte
// updatedMux is a lock for updatedNodes
updatedMux sync.RWMutex
// nodesToRevert will be deleted from db
nodesToRevert [][]byte
// revertMux is a lock for updatedNodes
revertMux sync.RWMutex
// lock for CacheDB
lock sync.RWMutex
// store is the interface to disk db
store db.DB
}
// commit stores the updated nodes to disk.
func (db *CacheDB) commit() {
db.updatedMux.Lock()
defer db.updatedMux.Unlock()
txn := db.store.NewTx(true)
// NOTE The tx interface doesnt handle ErrTxnTooBig
for key, batch := range db.updatedNodes {
//node := key
var node []byte
txn.Set(append(node, key[:]...), db.serializeBatch(batch))
//txn.Set(node[:], db.serializeBatch(batch))
}
txn.Commit()
}
func (db *CacheDB) serializeBatch(batch [][]byte) []byte {
serialized := make([]byte, 4) //, 30*33)
if batch[0][0] == 1 {
// the batch node is a shortcut
bitSet(serialized, 31)
}
for i := 1; i < 31; i++ {
if len(batch[i]) != 0 {
bitSet(serialized, i-1)
serialized = append(serialized, batch[i]...)
}
}
return serialized
}