chunk

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 15 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package chunk defines the immutable content-addressed units used by compactfs.

Index

Constants

View Source
const (
	// HashAlgorithm identifies content addressing in backend keys and diagnostics.
	HashAlgorithm = "xxh3-128"
	// HashSize is the byte length of an uncompressed chunk's content digest.
	HashSize = 16

	// StagedCodec identifies durable raw data awaiting background encoding.
	// These references require the matching local writeback cache to mount.
	StagedCodec = "compactfs-raw-v1"

	// DefaultSize is the default uncompressed chunk size. It is large enough to
	// amortize object-storage requests while keeping random reads bounded.
	DefaultSize = 4 * 1024 * 1024
)

Variables

View Source
var ErrChecksumMismatch = errors.New("chunk checksum mismatch")

ErrChecksumMismatch means staged bytes no longer match their content address.

Functions

func Hash

func Hash(data []byte) string

Hash returns the canonical 32-character XXH3-128 digest of raw chunk data. XXH3 is a non-cryptographic hash intended for fast content addressing.

func Key

func Key(hash string) (string, error)

Key returns the sharded backend key for a content hash.

Types

type Metrics

type Metrics struct {
	PutOps                  int64
	PutBytes                int64
	PutNanos                int64
	PutErrors               int64
	EncodeOps               int64
	EncodeBytes             int64
	EncodeNanos             int64
	EncodeErrors            int64
	EncodeBufferAllocations int64
	EncodeBufferReuses      int64
	EncodeBufferPoolBytes   int64
	BackendStatOps          int64
	BackendStatNanos        int64
	BackendStatErrors       int64
	BackendPutOps           int64
	BackendPutBytes         int64
	BackendPutNanos         int64
	BackendPutErrors        int64
	BackendImportOps        int64
	BackendImportBytes      int64
	BackendImportNanos      int64
	BackendImportFallbacks  int64
	BackendRangeOps         int64
	BackendRangeBytes       int64
	BackendGetOps           int64
	BackendGetBytes         int64
	BackendGetNanos         int64
	BackendGetErrors        int64
	CacheGetOps             int64
	CacheGetBytes           int64
	CacheGetNanos           int64
	CacheGetErrors          int64
	CacheHits               int64
	CacheMisses             int64
	CachePutOps             int64
	CachePutBytes           int64
	CachePutNanos           int64
	CachePutErrors          int64
	DecodeOps               int64
	DecodeBytes             int64
	DecodeNanos             int64
	DecodeErrors            int64
}

Metrics is a cumulative snapshot of chunk-store activity. Durations are in nanoseconds so callers can calculate throughput without depending on a particular presentation format.

type Ref

type Ref struct {
	Hash        string `json:"hash"`
	RawSize     int64  `json:"raw_size"`
	StoredSize  int64  `json:"stored_size"`
	Codec       string `json:"codec"`
	ContentType string `json:"content_type,omitempty"`
}

Ref identifies one compressed chunk in a file manifest.

type Span

type Span struct {
	Start int
	End   int
}

Span is one byte range in a chunked payload.

func (Span) Len

func (s Span) Len() int

type Splitter

type Splitter struct {
	Size int
}

Splitter cuts data into fixed-size, non-overlapping spans. Fixed chunks make random FUSE writes predictable and allow direct chunk-index replacement.

func (Splitter) Spans

func (s Splitter) Spans(dataLen int) ([]Span, error)

Spans returns byte ranges [Start, End) covering data.

type Store

type Store struct {
	Backend  backend.Backend
	Codec    codec.Codec
	Fallback codec.Codec
	Cache    *cache.Cache
	// SkipWriteCache avoids storing a second encoded copy when a durable raw
	// writeback cache already owns newly written data. Backend reads may still
	// populate Cache for objects whose raw copy has been evicted.
	SkipWriteCache bool
	// Decoders contains codecs that may be referenced by existing chunk
	// metadata even when Codec has since changed. Built-in gzip and zstd
	// decoders are supplied automatically; this map is for application codecs.
	Decoders map[string]codec.Codec
	// MaxAttempts is the total number of attempts for one immutable chunk. A
	// value below two disables retries. The filesystem config supplies a
	// bounded default for asynchronous write-back.
	MaxAttempts int
	// RetryBackoff is the delay before the second attempt. Each subsequent
	// delay doubles and is capped at one second.
	RetryBackoff time.Duration
	// contains filtered or unexported fields
}

Store owns immutable compressed chunks and performs content-addressed deduplication. The hash is always over raw bytes, so codec changes do not silently alias incompatible data.

func (*Store) Get

func (s *Store) Get(ctx context.Context, ref Ref, dst io.Writer) error

func (*Store) Metrics

func (s *Store) Metrics() Metrics

Metrics returns a lock-free cumulative activity snapshot for this store.

func (*Store) OpenRawRange

func (s *Store) OpenRawRange(ctx context.Context, ref Ref, offset, size int64) (*os.File, error)

OpenRawRange opens a read-only local object without materializing its whole chunk. Only raw references qualify. The caller owns the immutable fd and may read the validated range [offset, offset+size). ErrNotSupported requests the normal cache/decode path without performing remote I/O. BackendGetBytes and BackendRangeBytes count requested logical range bytes, not disk sector I/O.

func (*Store) Put

func (s *Store) Put(ctx context.Context, raw []byte) (Ref, error)

func (*Store) PutFile

func (s *Store) PutFile(ctx context.Context, source *os.File) (Ref, error)

PutFile stores one immutable regular file as a chunk. Linux maps its bytes read-only instead of copying the whole chunk into another anonymous buffer. Raw representations can use backend.FileImporter to reuse durable file data. Its offset and ownership are preserved. The source inode must remain immutable even after return because a backend may share it with the object; callers may close or unlink the source. Hashing and retries match Put.

func (*Store) PutFileWithHash

func (s *Store) PutFileWithHash(ctx context.Context, source *os.File, expectedHash string) (Ref, error)

PutFileWithHash verifies a staged file against its expected content address before encoding or publishing it. The immutability and ownership requirements are the same as PutFile. A mismatch returns ErrChecksumMismatch without any backend publication; expectedHash must be a canonical XXH3-128 digest.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL