github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/libkbfs/file_block_map_memory.go (about) 1 // Copyright 2019 Keybase Inc. All rights reserved. 2 // Use of this source code is governed by a BSD 3 // license that can be found in the LICENSE file. 4 5 package libkbfs 6 7 import ( 8 "context" 9 10 "github.com/keybase/client/go/kbfs/data" 11 "github.com/pkg/errors" 12 ) 13 14 type fileBlockMapMemoryInfo struct { 15 pps data.PathPartString 16 block *data.FileBlock 17 } 18 19 // fileBlockMapMemory is an internal structure to track file block 20 // data in memory when putting blocks. 21 type fileBlockMapMemory struct { 22 blocks map[data.BlockPointer]map[string]fileBlockMapMemoryInfo 23 } 24 25 var _ fileBlockMap = (*fileBlockMapMemory)(nil) 26 27 func newFileBlockMapMemory() *fileBlockMapMemory { 28 return &fileBlockMapMemory{ 29 blocks: make(map[data.BlockPointer]map[string]fileBlockMapMemoryInfo), 30 } 31 } 32 33 func (fbmm *fileBlockMapMemory) putTopBlock( 34 _ context.Context, parentPtr data.BlockPointer, 35 childName data.PathPartString, topBlock *data.FileBlock) error { 36 nameMap, ok := fbmm.blocks[parentPtr] 37 if !ok { 38 nameMap = make(map[string]fileBlockMapMemoryInfo) 39 fbmm.blocks[parentPtr] = nameMap 40 } 41 nameMap[childName.Plaintext()] = fileBlockMapMemoryInfo{childName, topBlock} 42 return nil 43 } 44 45 func (fbmm *fileBlockMapMemory) GetTopBlock( 46 _ context.Context, parentPtr data.BlockPointer, 47 childName data.PathPartString) (*data.FileBlock, error) { 48 nameMap, ok := fbmm.blocks[parentPtr] 49 if !ok { 50 return nil, errors.Errorf("No such parent %s", parentPtr) 51 } 52 info, ok := nameMap[childName.Plaintext()] 53 if !ok { 54 return nil, errors.Errorf( 55 "No such name %s in parent %s", childName, parentPtr) 56 } 57 return info.block, nil 58 } 59 60 func (fbmm *fileBlockMapMemory) getFilenames( 61 _ context.Context, parentPtr data.BlockPointer) ( 62 names []data.PathPartString, err error) { 63 nameMap, ok := fbmm.blocks[parentPtr] 64 if !ok { 65 return nil, nil 66 } 67 names = make([]data.PathPartString, 0, len(nameMap)) 68 for _, info := range nameMap { 69 names = append(names, info.pps) 70 } 71 return names, nil 72 }