forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_block_map_disk.go
97 lines (85 loc) · 2.48 KB
/
file_block_map_disk.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
// Copyright 2019 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 (
"context"
"github.com/keybase/client/go/kbfs/data"
"github.com/keybase/client/go/kbfs/kbfsblock"
"github.com/keybase/client/go/kbfs/libkey"
"github.com/pkg/errors"
)
// fileBlockMapDisk tracks block info while making a revision, by
// using a disk-based block cache.
type fileBlockMapDisk struct {
dirtyBcache *DirtyBlockCacheDisk
kmd libkey.KeyMetadata
ptrs map[data.BlockPointer]map[string]data.BlockPointer
}
var _ fileBlockMap = (*fileBlockMapDisk)(nil)
func newFileBlockMapDisk(
dirtyBcache *DirtyBlockCacheDisk, kmd libkey.KeyMetadata) *fileBlockMapDisk {
return &fileBlockMapDisk{
dirtyBcache: dirtyBcache,
kmd: kmd,
ptrs: make(map[data.BlockPointer]map[string]data.BlockPointer),
}
}
func (fbmd *fileBlockMapDisk) putTopBlock(
ctx context.Context, parentPtr data.BlockPointer, childName string,
topBlock *data.FileBlock) error {
// To reuse the DirtyBlockCacheDisk code, we need to assign a
// random BlockPointer to this block.
id, err := kbfsblock.MakeTemporaryID()
if err != nil {
return err
}
ptr := data.BlockPointer{ID: id}
err = fbmd.dirtyBcache.Put(
ctx, fbmd.kmd.TlfID(), ptr, data.MasterBranch, topBlock)
if err != nil {
return err
}
ptrMap, ok := fbmd.ptrs[parentPtr]
if !ok {
ptrMap = make(map[string]data.BlockPointer)
fbmd.ptrs[parentPtr] = ptrMap
}
ptrMap[childName] = ptr
return nil
}
func (fbmd *fileBlockMapDisk) GetTopBlock(
ctx context.Context, parentPtr data.BlockPointer, childName string) (
*data.FileBlock, error) {
ptrMap, ok := fbmd.ptrs[parentPtr]
if !ok {
return nil, errors.Errorf("No such parent %s", parentPtr)
}
ptr, ok := ptrMap[childName]
if !ok {
return nil, errors.Errorf(
"No such name %s in parent %s", childName, parentPtr)
}
block, err := fbmd.dirtyBcache.Get(ctx, fbmd.kmd.TlfID(), ptr, data.MasterBranch)
if err != nil {
return nil, err
}
fblock, ok := block.(*data.FileBlock)
if !ok {
return nil, errors.Errorf(
"Unexpected block type for file block: %T", block)
}
return fblock, nil
}
func (fbmd *fileBlockMapDisk) getFilenames(
_ context.Context, parentPtr data.BlockPointer) (names []string, err error) {
ptrMap, ok := fbmd.ptrs[parentPtr]
if !ok {
return nil, nil
}
names = make([]string, 0, len(ptrMap))
for name := range ptrMap {
names = append(names, name)
}
return names, nil
}