/
boltpersistentstate.go
145 lines (129 loc) · 3.32 KB
/
boltpersistentstate.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package chezmoi
import (
"os"
"time"
"go.etcd.io/bbolt"
)
// A BoltPersistentStateMode is a mode for opening a PersistentState.
type BoltPersistentStateMode int
// PersistentStateModes.
const (
BoltPersistentStateReadOnly BoltPersistentStateMode = iota
BoltPersistentStateReadWrite
)
// A BoltPersistentState is a state persisted with bolt.
type BoltPersistentState struct {
db *bbolt.DB
}
// NewBoltPersistentState returns a new BoltPersistentState.
func NewBoltPersistentState(system System, path AbsPath, mode BoltPersistentStateMode) (*BoltPersistentState, error) {
if _, err := system.Stat(path); os.IsNotExist(err) {
if mode == BoltPersistentStateReadOnly {
return &BoltPersistentState{}, nil
}
if err := MkdirAll(system, path.Dir(), 0o777); err != nil {
return nil, err
}
}
options := bbolt.Options{
OpenFile: func(name string, flag int, perm os.FileMode) (*os.File, error) {
rawPath, err := system.RawPath(AbsPath(name))
if err != nil {
return nil, err
}
return os.OpenFile(string(rawPath), flag, perm)
},
ReadOnly: mode == BoltPersistentStateReadOnly,
Timeout: time.Second,
}
db, err := bbolt.Open(string(path), 0o600, &options)
if err != nil {
return nil, err
}
return &BoltPersistentState{
db: db,
}, nil
}
// Close closes b.
func (b *BoltPersistentState) Close() error {
if b.db == nil {
return nil
}
return b.db.Close()
}
// CopyTo copies b to p.
func (b *BoltPersistentState) CopyTo(p PersistentState) error {
if b.db == nil {
return nil
}
return b.db.View(func(tx *bbolt.Tx) error {
return tx.ForEach(func(bucket []byte, b *bbolt.Bucket) error {
return b.ForEach(func(key, value []byte) error {
return p.Set(copyByteSlice(bucket), copyByteSlice(key), copyByteSlice(value))
})
})
})
}
// Delete deletes the value associate with key in bucket. If bucket or key does
// not exist then Delete does nothing.
func (b *BoltPersistentState) Delete(bucket, key []byte) error {
return b.db.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket(bucket)
if b == nil {
return nil
}
return b.Delete(key)
})
}
// ForEach calls fn for each key, value pair in bucket.
func (b *BoltPersistentState) ForEach(bucket []byte, fn func(k, v []byte) error) error {
if b.db == nil {
return nil
}
return b.db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket(bucket)
if b == nil {
return nil
}
return b.ForEach(func(k, v []byte) error {
return fn(copyByteSlice(k), copyByteSlice(v))
})
})
}
// Get returns the value associated with key in bucket.
func (b *BoltPersistentState) Get(bucket, key []byte) ([]byte, error) {
if b.db == nil {
return nil, nil
}
var value []byte
if err := b.db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket(bucket)
if b == nil {
return nil
}
value = copyByteSlice(b.Get(key))
return nil
}); err != nil {
return nil, err
}
return value, nil
}
// Set sets the value associated with key in bucket. bucket will be created if
// it does not already exist.
func (b *BoltPersistentState) Set(bucket, key, value []byte) error {
return b.db.Update(func(tx *bbolt.Tx) error {
b, err := tx.CreateBucketIfNotExists(bucket)
if err != nil {
return err
}
return b.Put(key, value)
})
}
func copyByteSlice(value []byte) []byte {
if value == nil {
return nil
}
result := make([]byte, len(value))
copy(result, value)
return result
}