-
Notifications
You must be signed in to change notification settings - Fork 10
/
prefix.go
112 lines (89 loc) · 2.71 KB
/
prefix.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
104
105
106
107
108
109
110
111
112
package prefixmanager
import (
"github.com/sedracoin/sedrad/domain/prefixmanager/prefix"
"github.com/sedracoin/sedrad/infrastructure/db/database"
)
var activePrefixKey = database.MakeBucket(nil).Key([]byte("active-prefix"))
var inactivePrefixKey = database.MakeBucket(nil).Key([]byte("inactive-prefix"))
// ActivePrefix returns the current active database prefix, and whether it exists
func ActivePrefix(dataAccessor database.DataAccessor) (*prefix.Prefix, bool, error) {
prefixBytes, err := dataAccessor.Get(activePrefixKey)
if database.IsNotFoundError(err) {
return nil, false, nil
}
if err != nil {
return nil, false, err
}
prefix, err := prefix.Deserialize(prefixBytes)
if err != nil {
return nil, false, err
}
return prefix, true, nil
}
// InactivePrefix returns the current inactive database prefix, and whether it exists
func InactivePrefix(dataAccessor database.DataAccessor) (*prefix.Prefix, bool, error) {
prefixBytes, err := dataAccessor.Get(inactivePrefixKey)
if database.IsNotFoundError(err) {
return nil, false, nil
}
if err != nil {
return nil, false, err
}
prefix, err := prefix.Deserialize(prefixBytes)
if err != nil {
return nil, false, err
}
return prefix, true, nil
}
// DeleteInactivePrefix deletes all data associated with the inactive database prefix, including itself.
func DeleteInactivePrefix(db database.Database) error {
prefixBytes, err := db.Get(inactivePrefixKey)
if database.IsNotFoundError(err) {
return nil
}
if err != nil {
return err
}
prefix, err := prefix.Deserialize(prefixBytes)
if err != nil {
return err
}
err = deletePrefix(db, prefix)
if err != nil {
return err
}
err = db.Delete(inactivePrefixKey)
if err != nil {
return err
}
log.Infof("Compacting database after prefix delete")
return db.Compact()
}
func deletePrefix(dataAccessor database.DataAccessor, prefix *prefix.Prefix) error {
log.Infof("Deleting database prefix %x", prefix)
prefixBucket := database.MakeBucket(prefix.Serialize())
cursor, err := dataAccessor.Cursor(prefixBucket)
if err != nil {
return err
}
defer cursor.Close()
for ok := cursor.First(); ok; ok = cursor.Next() {
key, err := cursor.Key()
if err != nil {
return err
}
err = dataAccessor.Delete(key)
if err != nil {
return err
}
}
return nil
}
// SetPrefixAsActive sets the given prefix as the active prefix
func SetPrefixAsActive(dataAccessor database.DataAccessor, prefix *prefix.Prefix) error {
return dataAccessor.Put(activePrefixKey, prefix.Serialize())
}
// SetPrefixAsInactive sets the given prefix as the inactive prefix
func SetPrefixAsInactive(dataAccessor database.DataAccessor, prefix *prefix.Prefix) error {
return dataAccessor.Put(inactivePrefixKey, prefix.Serialize())
}