/
pinner.go
335 lines (271 loc) · 7.78 KB
/
pinner.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package catfs
import (
"errors"
capnp "github.com/sahib/brig/catfs/capnp"
c "github.com/sahib/brig/catfs/core"
"github.com/sahib/brig/catfs/db"
ie "github.com/sahib/brig/catfs/errors"
n "github.com/sahib/brig/catfs/nodes"
h "github.com/sahib/brig/util/hashlib"
capnp_lib "zombiezen.com/go/capnproto2"
)
// errNotPinnedSentinel is returned to signal an early exit in Walk()
var errNotPinnedSentinel = errors.New("not pinned")
// pinCacheEntry is one entry in the pin cache.
type pinCacheEntry struct {
Inodes map[uint64]bool
}
func capnpToPinCacheEntry(data []byte) (*pinCacheEntry, error) {
msg, err := capnp_lib.Unmarshal(data)
if err != nil {
return nil, err
}
capEntry, err := capnp.ReadRootPinEntry(msg)
if err != nil {
return nil, err
}
capPins, err := capEntry.Pins()
if err != nil {
return nil, err
}
entry := &pinCacheEntry{
Inodes: make(map[uint64]bool),
}
for idx := 0; idx < capPins.Len(); idx++ {
capPin := capPins.At(idx)
entry.Inodes[capPin.Inode()] = capPin.IsPinned()
}
return entry, nil
}
func pinEnryToCapnpData(entry *pinCacheEntry) ([]byte, error) {
msg, seg, err := capnp_lib.NewMessage(capnp_lib.SingleSegment(nil))
if err != nil {
return nil, err
}
capEntry, err := capnp.NewRootPinEntry(seg)
if err != nil {
return nil, err
}
capPinList, err := capnp.NewPin_List(seg, int32(len(entry.Inodes)))
if err != nil {
return nil, err
}
idx := 0
for inode, isPinned := range entry.Inodes {
capPin, err := capnp.NewPin(seg)
if err != nil {
return nil, err
}
capPin.SetInode(inode)
capPin.SetIsPinned(isPinned)
if err := capPinList.Set(idx, capPin); err != nil {
return nil, err
}
idx++
}
if err := capEntry.SetPins(capPinList); err != nil {
return nil, err
}
return msg.Marshal()
}
// Pinner remembers which hashes are pinned and if they are pinned explicitly.
// Its API can be used to safely change the pinning state. It assumes that it
// is the only entitiy the pins & unpins nodes.
type Pinner struct {
bk FsBackend
lkr *c.Linker
}
// NewPinner creates a new pin cache at `pinDbPath`, possibly erroring out.
// `lkr` and `bk` are used to make PinNode() and UnpinNode() work.
func NewPinner(lkr *c.Linker, bk FsBackend) (*Pinner, error) {
return &Pinner{lkr: lkr, bk: bk}, nil
}
// Close the pinning cache.
func (pc *Pinner) Close() error {
// currently a no-op
return nil
}
func getEntry(kv db.Database, hash h.Hash) (*pinCacheEntry, error) {
data, err := kv.Get("pins", hash.B58String())
if err != nil {
if err == db.ErrNoSuchKey {
return nil, nil
}
return nil, err
}
return capnpToPinCacheEntry(data)
}
// remember the pin state of a certain hash.
// This does change anything in the backend but only changes the caching structure.
// Use with care to avoid data inconsistencies.
func (pc *Pinner) remember(inode uint64, hash h.Hash, isPinned, isExplicit bool) error {
return pc.lkr.AtomicWithBatch(func(batch db.Batch) (bool, error) {
oldEntry, err := getEntry(pc.lkr.KV(), hash)
if err != nil {
return true, err
}
var inodes map[uint64]bool
if oldEntry != nil {
inodes = oldEntry.Inodes
} else {
inodes = make(map[uint64]bool)
}
if !isPinned {
delete(inodes, inode)
} else {
inodes[inode] = isExplicit
}
entry := pinCacheEntry{
Inodes: inodes,
}
data, err := pinEnryToCapnpData(&entry)
if err != nil {
return true, err
}
batch.Put(data, "pins", hash.B58String())
return false, nil
})
}
// IsPinned returns two boolean values indicating the pin status of `inode` and
// `hash`. If the first value is true, the content is pinned, if the second is
// true it is pinned explicitly.
func (pc *Pinner) IsPinned(inode uint64, hash h.Hash) (bool, bool, error) {
data, err := pc.lkr.KV().Get("pins", hash.B58String())
if err != nil && err != db.ErrNoSuchKey {
return false, false, err
}
if err == nil {
// cache hit
entry, err := capnpToPinCacheEntry(data)
if err != nil {
return false, false, err
}
isExplicit, ok := entry.Inodes[inode]
return ok, isExplicit, nil
}
// We do not have this information yet.
// Create a new entry based on the backend information.
// silence a key error, ok will be false then.
isPinned, err := pc.bk.IsPinned(hash)
if err != nil {
return false, false, err
}
// remember the file to be pinned non-explicitly:
if err := pc.remember(inode, hash, isPinned, false); err != nil {
return false, false, err
}
return isPinned, false, nil
}
////////////////////////////
// Pin will remember the node at `inode` with hash `hash` as `explicit`ly pinned.
func (pc *Pinner) Pin(inode uint64, hash h.Hash, explicit bool) error {
isPinned, isExplicit, err := pc.IsPinned(inode, hash)
if err != nil {
return err
}
if isPinned {
if isExplicit && !explicit {
// will not "downgrade" an existing pin.
return nil
}
} else {
if err := pc.bk.Pin(hash); err != nil {
return err
}
}
return pc.remember(inode, hash, true, explicit)
}
// Unpin pins the content at `inode` and `hash`. If the pin was explicit,
// `explicit` must be true to make this work.
func (pc *Pinner) Unpin(inode uint64, hash h.Hash, explicit bool) error {
isPinned, isExplicit, err := pc.IsPinned(inode, hash)
if err != nil {
return err
}
if isPinned {
if isExplicit && !explicit {
return nil
}
if err := pc.bk.Unpin(hash); err != nil {
return err
}
}
return pc.remember(inode, hash, false, explicit)
}
////////////////////////////
// doPinOp recursively walks over all children of a node and pins or unpins them.
func (pc *Pinner) doPinOp(op func(uint64, h.Hash, bool) error, nd n.Node, explicit bool) error {
return n.Walk(pc.lkr, nd, true, func(child n.Node) error {
if child.Type() != n.NodeTypeFile {
return nil
}
file, ok := child.(*n.File)
if !ok {
return ie.ErrBadNode
}
return op(file.Inode(), file.BackendHash(), explicit)
})
}
// PinNode tries to pin the node referenced by `nd`.
// The difference to calling Pin(nd.BackendHash()) is,
// that this method will pin directories recursively, if given.
//
// If the file is already pinned exclusively and you want
// to pin it non-exclusive, this will be a no-op.
// In this case you have to unpin it first exclusively.
func (pc *Pinner) PinNode(nd n.Node, explicit bool) error {
return pc.doPinOp(pc.Pin, nd, explicit)
}
// UnpinNode is the exact opposite of PinNode.
func (pc *Pinner) UnpinNode(nd n.Node, explicit bool) error {
return pc.doPinOp(pc.Unpin, nd, explicit)
}
// IsNodePinned checks if all `nd` is pinned and if so, exlusively.
// If `nd` is a directory, it will only return true if all children
// are also pinned (same for second return value).
func (pc *Pinner) IsNodePinned(nd n.Node) (bool, bool, error) {
// Handle special case:
// empty directories should count as pinned.
// (for the sake of the definition that a directory is pinned,
// if all children are also pinned)
if nd.Type() == n.NodeTypeDirectory && nd.NChildren() == 0 {
return true, true, nil
}
pinCount := 0
explicitCount := 0
totalCount := 0
err := n.Walk(pc.lkr, nd, true, func(child n.Node) error {
if child.Type() != n.NodeTypeFile {
return nil
}
file, ok := child.(*n.File)
if !ok {
return ie.ErrBadNode
}
totalCount++
isPinned, isExplicit, err := pc.IsPinned(file.Inode(), file.BackendHash())
if err != nil {
return err
}
if isExplicit {
explicitCount++
}
if isPinned {
// Make sure that we do not count empty directories
// as pinned nodes.
pinCount++
} else {
// Return a special error here to stop Walk() iterating.
// One file is enough to stop IsPinned() from being true.
return errNotPinnedSentinel
}
return nil
})
if err != nil && err != errNotPinnedSentinel {
return false, false, err
}
if err == errNotPinnedSentinel {
return false, false, nil
}
return pinCount > 0, explicitCount == totalCount, nil
}