-
Notifications
You must be signed in to change notification settings - Fork 199
/
snapshotDb.go
101 lines (81 loc) · 2.18 KB
/
snapshotDb.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
package trie
import (
"os"
"sync"
"github.com/ElrondNetwork/elrond-go/common"
)
type snapshotDb struct {
common.DBWriteCacher
numReferences uint32
shouldBeRemoved bool
shouldBeDisconnected bool
path string
mutex sync.RWMutex
}
// DecreaseNumReferences decreases the num references counter
func (s *snapshotDb) DecreaseNumReferences() {
s.mutex.Lock()
defer s.mutex.Unlock()
if s.numReferences > 0 {
s.numReferences--
}
if s.numReferences == 0 && s.shouldBeRemoved {
removeSnapshot(s.DBWriteCacher, s.path)
return
}
if s.numReferences == 0 && s.shouldBeDisconnected {
err := disconnectSnapshot(s.DBWriteCacher)
if err != nil {
log.Error("snapshotDb: disconnectSnapshot", "error", err.Error())
}
return
}
}
func disconnectSnapshot(db common.DBWriteCacher) error {
return db.Close()
}
func removeSnapshot(db common.DBWriteCacher, path string) {
err := disconnectSnapshot(db)
if err != nil {
log.Error("trie storage manager: disconnectSnapshot", "error", err.Error())
return
}
log.Debug("remove trie snapshot db", "snapshot path", path)
go removeDirectory(path)
}
func removeDirectory(path string) {
err := os.RemoveAll(path)
if err != nil {
log.Error(err.Error())
}
}
// IncreaseNumReferences increases the num references counter
func (s *snapshotDb) IncreaseNumReferences() {
s.mutex.Lock()
defer s.mutex.Unlock()
s.numReferences++
}
// MarkForRemoval marks the current db for removal. When the numReferences buffer reaches 0, the db will be removed
func (s *snapshotDb) MarkForRemoval() {
s.mutex.Lock()
defer s.mutex.Unlock()
s.shouldBeRemoved = true
}
// MarkForDisconnection marks the current db for disconnection. When the numReferences buffer reaches 0, the db will be disconnected
func (s *snapshotDb) MarkForDisconnection() {
s.mutex.Lock()
defer s.mutex.Unlock()
s.shouldBeDisconnected = true
}
// SetPath sets the db path
func (s *snapshotDb) SetPath(path string) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.path = path
}
// IsInUse returns true if the numReferences counter is greater than 0
func (s *snapshotDb) IsInUse() bool {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.numReferences > 0
}