/
file_block_map_memory.go
72 lines (62 loc) · 1.9 KB
/
file_block_map_memory.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
// 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/pkg/errors"
)
type fileBlockMapMemoryInfo struct {
pps data.PathPartString
block *data.FileBlock
}
// fileBlockMapMemory is an internal structure to track file block
// data in memory when putting blocks.
type fileBlockMapMemory struct {
blocks map[data.BlockPointer]map[string]fileBlockMapMemoryInfo
}
var _ fileBlockMap = (*fileBlockMapMemory)(nil)
func newFileBlockMapMemory() *fileBlockMapMemory {
return &fileBlockMapMemory{
blocks: make(map[data.BlockPointer]map[string]fileBlockMapMemoryInfo),
}
}
func (fbmm *fileBlockMapMemory) putTopBlock(
_ context.Context, parentPtr data.BlockPointer,
childName data.PathPartString, topBlock *data.FileBlock) error {
nameMap, ok := fbmm.blocks[parentPtr]
if !ok {
nameMap = make(map[string]fileBlockMapMemoryInfo)
fbmm.blocks[parentPtr] = nameMap
}
nameMap[childName.Plaintext()] = fileBlockMapMemoryInfo{childName, topBlock}
return nil
}
func (fbmm *fileBlockMapMemory) GetTopBlock(
_ context.Context, parentPtr data.BlockPointer,
childName data.PathPartString) (*data.FileBlock, error) {
nameMap, ok := fbmm.blocks[parentPtr]
if !ok {
return nil, errors.Errorf("No such parent %s", parentPtr)
}
info, ok := nameMap[childName.Plaintext()]
if !ok {
return nil, errors.Errorf(
"No such name %s in parent %s", childName, parentPtr)
}
return info.block, nil
}
func (fbmm *fileBlockMapMemory) getFilenames(
_ context.Context, parentPtr data.BlockPointer) (
names []data.PathPartString, err error) {
nameMap, ok := fbmm.blocks[parentPtr]
if !ok {
return nil, nil
}
names = make([]data.PathPartString, 0, len(nameMap))
for _, info := range nameMap {
names = append(names, info.pps)
}
return names, nil
}