github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/libkbfs/dir_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  // dirBlockMapMemory is an internal structure to track file block
    15  // data in memory when putting blocks.
    16  type dirBlockMapMemory struct {
    17  	blocks map[data.BlockPointer]*data.DirBlock
    18  }
    19  
    20  var _ dirBlockMap = (*dirBlockMapMemory)(nil)
    21  
    22  func newDirBlockMapMemory() *dirBlockMapMemory {
    23  	return &dirBlockMapMemory{make(map[data.BlockPointer]*data.DirBlock)}
    24  }
    25  
    26  func (dbmm *dirBlockMapMemory) putBlock(
    27  	_ context.Context, ptr data.BlockPointer, block *data.DirBlock) error {
    28  	dbmm.blocks[ptr] = block
    29  	return nil
    30  }
    31  
    32  func (dbmm *dirBlockMapMemory) getBlock(
    33  	_ context.Context, ptr data.BlockPointer) (*data.DirBlock, error) {
    34  	block, ok := dbmm.blocks[ptr]
    35  	if !ok {
    36  		return nil, errors.Errorf("No such block for %s", ptr)
    37  	}
    38  	return block, nil
    39  }
    40  
    41  func (dbmm *dirBlockMapMemory) hasBlock(
    42  	_ context.Context, ptr data.BlockPointer) (bool, error) {
    43  	_, ok := dbmm.blocks[ptr]
    44  	return ok, nil
    45  }
    46  
    47  func (dbmm *dirBlockMapMemory) deleteBlock(
    48  	_ context.Context, ptr data.BlockPointer) error {
    49  	delete(dbmm.blocks, ptr)
    50  	return nil
    51  }
    52  
    53  func (dbmm *dirBlockMapMemory) numBlocks() int {
    54  	return len(dbmm.blocks)
    55  }