-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
node_cache.go
460 lines (392 loc) · 11.1 KB
/
node_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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package libkbfs
import (
"fmt"
"sync"
"github.com/keybase/client/go/kbfs/data"
)
type nodeCacheEntry struct {
core *nodeCore
refCount int
}
// nodeCacheStandard implements the NodeCache interface by tracking
// the reference counts of nodeStandard Nodes, and using their member
// fields to construct paths.
type nodeCacheStandard struct {
folderBranch data.FolderBranch
lock sync.RWMutex
nodes map[data.BlockRef]*nodeCacheEntry
rootWrappers []func(Node) Node
makeObfuscator func() data.Obfuscator
}
var _ NodeCache = (*nodeCacheStandard)(nil)
func newNodeCacheStandard(fb data.FolderBranch) *nodeCacheStandard {
return &nodeCacheStandard{
folderBranch: fb,
nodes: make(map[data.BlockRef]*nodeCacheEntry),
}
}
// lock must be locked for writing by the caller
func (ncs *nodeCacheStandard) forgetLocked(core *nodeCore) {
ref := core.pathNode.Ref()
entry, ok := ncs.nodes[ref]
if !ok {
return
}
if entry.core != core {
return
}
entry.refCount--
if entry.refCount <= 0 {
delete(ncs.nodes, ref)
}
}
// should be called only by nodeStandardFinalizer().
func (ncs *nodeCacheStandard) forget(core *nodeCore) {
ncs.lock.Lock()
defer ncs.lock.Unlock()
ncs.forgetLocked(core)
}
// lock must be held for writing by the caller
func (ncs *nodeCacheStandard) newChildForParentLocked(parent Node) (*nodeStandard, error) {
nodeStandard, ok := parent.Unwrap().(*nodeStandard)
if !ok {
return nil, ParentNodeNotFoundError{data.BlockRef{}}
}
ref := nodeStandard.core.pathNode.Ref()
entry, ok := ncs.nodes[ref]
if !ok {
return nil, ParentNodeNotFoundError{ref}
}
if nodeStandard.core != entry.core {
return nil, ParentNodeNotFoundError{ref}
}
return nodeStandard, nil
}
func (ncs *nodeCacheStandard) wrapNodeStandard(
n Node, rootWrappers []func(Node) Node, parent Node) Node {
if parent != nil {
return parent.WrapChild(n)
}
for _, f := range rootWrappers {
n = f(n)
}
return n
}
func (ncs *nodeCacheStandard) makeNodeStandardForEntryLocked(
entry *nodeCacheEntry) *nodeStandard {
entry.refCount++
return makeNodeStandard(entry.core)
}
// GetOrCreate implements the NodeCache interface for nodeCacheStandard.
func (ncs *nodeCacheStandard) GetOrCreate(
ptr data.BlockPointer, name data.PathPartString, parent Node,
et data.EntryType) (n Node, err error) {
var rootWrappers []func(Node) Node
defer func() {
if n != nil {
n = ncs.wrapNodeStandard(n, rootWrappers, parent)
}
}()
if !ptr.IsValid() {
// Temporary code to track down bad block
// pointers. Remove when not needed anymore.
panic(InvalidBlockRefError{ptr.Ref()})
}
if name.Plaintext() == "" {
return nil, EmptyNameError{ptr.Ref()}
}
ncs.lock.Lock()
defer ncs.lock.Unlock()
rootWrappers = ncs.rootWrappers
entry, ok := ncs.nodes[ptr.Ref()]
if ok {
// If the entry happens to be unlinked, we may be in a
// situation where a node got unlinked and then recreated, but
// someone held onto a node the whole time and so it never got
// removed from the cache. In that case, forcibly remove it
// from the cache to make room for the new node.
if parent != nil && entry.core.parent == nil {
delete(ncs.nodes, ptr.Ref())
} else {
return ncs.makeNodeStandardForEntryLocked(entry), nil
}
}
if parent != nil {
// Make sure a child can be made for this parent.
_, err := ncs.newChildForParentLocked(parent)
if err != nil {
return nil, err
}
}
entry = &nodeCacheEntry{}
if et == data.Dir && ncs.makeObfuscator != nil {
entry.core = newNodeCoreForDir(
ptr, name, parent, ncs, ncs.makeObfuscator())
} else {
entry.core = newNodeCore(ptr, name, parent, ncs, et)
}
ncs.nodes[ptr.Ref()] = entry
return ncs.makeNodeStandardForEntryLocked(entry), nil
}
// Get implements the NodeCache interface for nodeCacheStandard.
func (ncs *nodeCacheStandard) Get(ref data.BlockRef) (n Node) {
if ref == (data.BlockRef{}) {
return nil
}
// Temporary code to track down bad block pointers. Remove (or
// return an error) when not needed anymore.
if !ref.IsValid() {
panic(InvalidBlockRefError{ref})
}
var rootWrappers []func(Node) Node
var parent Node
defer func() {
if n != nil {
n = ncs.wrapNodeStandard(n, rootWrappers, parent)
}
}()
ncs.lock.Lock()
defer ncs.lock.Unlock()
rootWrappers = ncs.rootWrappers
entry, ok := ncs.nodes[ref]
if !ok {
return nil
}
ns := ncs.makeNodeStandardForEntryLocked(entry)
parent = ns.core.parent // get while under lock
return ns
}
// UpdatePointer implements the NodeCache interface for nodeCacheStandard.
func (ncs *nodeCacheStandard) UpdatePointer(
oldRef data.BlockRef, newPtr data.BlockPointer) (updatedNode NodeID) {
if oldRef == (data.BlockRef{}) && newPtr == (data.BlockPointer{}) {
return nil
}
if !oldRef.IsValid() {
panic(fmt.Sprintf("invalid oldRef %s with newPtr %s", oldRef, newPtr))
}
if !newPtr.IsValid() {
panic(fmt.Sprintf("invalid newPtr %s with oldRef %s", newPtr, oldRef))
}
ncs.lock.Lock()
defer ncs.lock.Unlock()
entry, ok := ncs.nodes[oldRef]
if !ok {
return nil
}
// Cannot update the pointer for an unlinked node.
if entry.core.cachedPath.IsValid() {
return nil
}
entry.core.pathNode.BlockPointer = newPtr
delete(ncs.nodes, oldRef)
ncs.nodes[newPtr.Ref()] = entry
return entry.core
}
// Move implements the NodeCache interface for nodeCacheStandard.
func (ncs *nodeCacheStandard) Move(
ref data.BlockRef, newParent Node, newName data.PathPartString) (
undoFn func(), err error) {
if ref == (data.BlockRef{}) {
return nil, nil
}
// Temporary code to track down bad block pointers. Remove (or
// return an error) when not needed anymore.
if !ref.IsValid() {
panic(InvalidBlockRefError{ref})
}
if newName.Plaintext() == "" {
return nil, EmptyNameError{ref}
}
ncs.lock.Lock()
defer ncs.lock.Unlock()
entry, ok := ncs.nodes[ref]
if !ok {
return nil, nil
}
newParentNS, err := ncs.newChildForParentLocked(newParent)
if err != nil {
return nil, err
}
oldParent := entry.core.parent
oldName := entry.core.pathNode.Name
entry.core.parent = newParentNS
entry.core.pathNode.Name = newName
return func() {
entry.core.parent = oldParent
entry.core.pathNode.Name = oldName
}, nil
}
// Unlink implements the NodeCache interface for nodeCacheStandard.
func (ncs *nodeCacheStandard) Unlink(
ref data.BlockRef, oldPath data.Path, oldDe data.DirEntry) (undoFn func()) {
if ref == (data.BlockRef{}) {
return nil
}
// Temporary code to track down bad block pointers. Remove (or
// return an error) when not needed anymore.
if !ref.IsValid() {
panic(InvalidBlockRefError{ref})
}
ncs.lock.Lock()
defer ncs.lock.Unlock()
entry, ok := ncs.nodes[ref]
if !ok {
return nil
}
if entry.core.cachedPath.IsValid() {
// Already unlinked!
return nil
}
oldParent := entry.core.parent
oldName := entry.core.pathNode.Name
entry.core.cachedPath = oldPath
entry.core.cachedDe = oldDe
entry.core.parent = nil
entry.core.pathNode.Name = data.PathPartString{}
return func() {
entry.core.cachedPath = data.Path{}
entry.core.cachedDe = data.DirEntry{}
entry.core.parent = oldParent
entry.core.pathNode.Name = oldName
}
}
// IsUnlinked implements the NodeCache interface for
// nodeCacheStandard.
func (ncs *nodeCacheStandard) IsUnlinked(node Node) bool {
ncs.lock.RLock()
defer ncs.lock.RUnlock()
ns, ok := node.Unwrap().(*nodeStandard)
if !ok {
return false
}
return ns.core.cachedPath.IsValid()
}
// UnlinkedDirEntry implements the NodeCache interface for
// nodeCacheStandard.
func (ncs *nodeCacheStandard) UnlinkedDirEntry(node Node) data.DirEntry {
ncs.lock.RLock()
defer ncs.lock.RUnlock()
ns, ok := node.Unwrap().(*nodeStandard)
if !ok {
return data.DirEntry{}
}
return ns.core.cachedDe
}
// UpdateUnlinkedDirEntry implements the NodeCache interface for
// nodeCacheStandard.
func (ncs *nodeCacheStandard) UpdateUnlinkedDirEntry(
node Node, newDe data.DirEntry) {
ncs.lock.Lock()
defer ncs.lock.Unlock()
ns, ok := node.Unwrap().(*nodeStandard)
if !ok {
return
}
ns.core.cachedDe = newDe
}
// PathFromNode implements the NodeCache interface for nodeCacheStandard.
func (ncs *nodeCacheStandard) PathFromNode(node Node) (p data.Path) {
ncs.lock.RLock()
defer ncs.lock.RUnlock()
ns, ok := node.Unwrap().(*nodeStandard)
if !ok {
p.Path = nil
return
}
p.ChildObfuscator = ns.core.obfuscator
for ns != nil {
core := ns.core
if core.parent == nil && len(core.cachedPath.Path) > 0 {
// The node was unlinked, but is still in use, so use its
// cached path. The path is already reversed, so append
// it backwards one-by-one to the existing path. If this
// is the first node, we can just optimize by returning
// the complete cached path.
if len(p.Path) == 0 {
return core.cachedPath
}
for i := len(core.cachedPath.Path) - 1; i >= 0; i-- {
p.Path = append(p.Path, core.cachedPath.Path[i])
}
break
}
p.Path = append(p.Path, *core.pathNode)
if core.parent != nil {
ns = core.parent.Unwrap().(*nodeStandard)
} else {
break
}
}
// need to reverse the path nodes
for i := len(p.Path)/2 - 1; i >= 0; i-- {
opp := len(p.Path) - 1 - i
p.Path[i], p.Path[opp] = p.Path[opp], p.Path[i]
}
// TODO: would it make any sense to cache the constructed path?
p.FolderBranch = ncs.folderBranch
return
}
// AllNodes implements the NodeCache interface for nodeCacheStandard.
func (ncs *nodeCacheStandard) AllNodes() (nodes []Node) {
ncs.lock.RLock()
defer ncs.lock.RUnlock()
nodes = make([]Node, 0, len(ncs.nodes))
for _, entry := range ncs.nodes {
nodes = append(nodes, ncs.makeNodeStandardForEntryLocked(entry))
}
return nodes
}
// AllNodeChildren implements the NodeCache interface for nodeCacheStandard.
func (ncs *nodeCacheStandard) AllNodeChildren(n Node) (nodes []Node) {
ncs.lock.RLock()
defer ncs.lock.RUnlock()
nodes = make([]Node, 0, len(ncs.nodes))
entryIDs := make(map[NodeID]bool)
for _, entry := range ncs.nodes {
var pathIDs []NodeID
parent := entry.core.parent
for parent != nil {
// If the node's parent is what we're looking for (or on
// the path to what we're looking for), include it in the
// list.
parentID := parent.GetID()
if parentID == n.GetID() || entryIDs[parentID] {
nodes = append(nodes, ncs.makeNodeStandardForEntryLocked(entry))
for _, id := range pathIDs {
entryIDs[id] = true
}
entryIDs[entry.core] = true
break
}
// Otherwise, remember this parent and continue back
// toward the root.
pathIDs = append(pathIDs, parentID)
ns, ok := parent.Unwrap().(*nodeStandard)
if !ok {
break
}
parent = ns.core.parent
}
}
return nodes
}
func (ncs *nodeCacheStandard) AddRootWrapper(f func(Node) Node) {
ncs.lock.Lock()
defer ncs.lock.Unlock()
ncs.rootWrappers = append(ncs.rootWrappers, f)
}
func (ncs *nodeCacheStandard) SetObfuscatorMaker(
makeOb func() data.Obfuscator) {
ncs.lock.Lock()
defer ncs.lock.Unlock()
ncs.makeObfuscator = makeOb
}
func (ncs *nodeCacheStandard) ObfuscatorMaker() func() data.Obfuscator {
ncs.lock.RLock()
defer ncs.lock.RUnlock()
return ncs.makeObfuscator
}