core

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2026 License: MIT Imports: 20 Imported by: 2 modules, 4 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.

README

fvs-v2-core

The engine behind FVS (Fused Versioned Storage): a small, dependency-light Go core for content-addressed, deduplicated, copy-on-write storage.

What's inside

  • Content-addressed blocks (BLAKE3). Every block is named by the hash of its content, so identical data is stored once (deduplication).
  • Disk and in-memory block stores (CAS). Atomic writes, and reads are re-verified against the content hash, so tampering or bit-rot surfaces as ErrBlockCorrupt instead of returning bad data.
  • Copy-on-write file abstraction. Fixed-size blocks mapped through a CoW map, with snapshot isolation. The building block for snapshots and the FUSE write path.

Usage

store, _ := core.NewDiskBlockStore(".fvs2/blocks")

id, _ := store.Put([]byte("hello"))   // content-addressed; same content, same id
data, _ := store.Get(id)              // re-hashed on read; ErrBlockCorrupt on tamper

CoWFile builds on a BlockStore plus a CoWMap to give a block-addressed, copy-on-write file with ReadAt / WriteAt and immutable Snapshot() views.

Test

go test ./...

License

MIT. See LICENSE.

Documentation

Index

Constants

View Source
const DefaultFrameCacheBytes = 64 << 20

DefaultFrameCacheBytes bounds the decompressed frame cache: enough for sequential reads through a mount to hit warm frames, small enough not to compete with the page cache.

Variables

View Source
var ErrBlockCorrupt = errors.New("block corrupt: content hash mismatch")

ErrBlockCorrupt is returned by a BlockStore when a stored block's content no longer matches its content-address (BLAKE3) id, i.e. the block has been tampered with or has suffered bit-rot on disk.

View Source
var ErrBlockNotFound = errors.New("block not found")
View Source
var ErrInvalidOffset = errors.New("invalid offset")

Functions

func IsTextHead

func IsTextHead(head []byte) bool

IsTextHead classifies content from its leading bytes: no NUL and at least 70% printable (tab, newlines, printable ASCII, and any byte >= 128 so UTF-8 sequences count). Carved in stone as policy 1 of the format.

func NewObjectBackedBlockStore added in v0.1.3

func NewObjectBackedBlockStore(dir string) (*DiskBlockStore, *ObjectStore, error)

NewObjectBackedBlockStore opens one block store and its shared object index.

Types

type BlockGetter added in v0.1.3

type BlockGetter interface {
	Get(BlockID) ([]byte, error)
}

type BlockID

type BlockID string

BlockID is a content-address identifier for a stored block. The in-memory implementation uses blake3(data) encoded as hex.

func ContentID

func ContentID(data []byte) BlockID

ContentID returns the content-address (BLAKE3) id data would get in a BlockStore, without storing it.

type BlockStore

type BlockStore interface {
	Put(data []byte) (BlockID, error)
	Get(id BlockID) ([]byte, error)
}

BlockStore is a minimal content-addressed block store. Implementations should deduplicate by content hash.

type ChunkParams

type ChunkParams struct {
	Min int
	Avg int
	Max int
}

ChunkParams controls content-defined chunking. Sizes are in bytes. Min == Avg == Max degenerates to fixed-size chunks of exactly Max bytes.

func DefaultChunkParams

func DefaultChunkParams() ChunkParams

DefaultChunkParams are the chunking parameters for format-2 repos. Part of the on-disk format: changing them changes every chunk boundary.

func FixedChunkParams

func FixedChunkParams(blockSize int) ChunkParams

FixedChunkParams returns parameters that reproduce the legacy format-1 fixed-size blocks of blockSize bytes.

func ParamsForContent

func ParamsForContent(policy int, base ChunkParams, head []byte) ChunkParams

ParamsForContent picks the chunking parameters under a policy: policy 0 always uses base; policy 1 switches text-like content to TextChunkParams.

func TextChunkParams

func TextChunkParams() ChunkParams

TextChunkParams is the CDC target for text-like content: small chunks so a scattered one-line edit invalidates kilobytes, not a 16 KiB block.

type Chunker

type Chunker struct {
	// contains filtered or unexported fields
}

Chunker splits a stream into content-defined chunks (FastCDC-style gear hash with normalized chunking). Boundaries depend only on content and ChunkParams.

func NewChunker

func NewChunker(r io.Reader, p ChunkParams) (*Chunker, error)

NewChunker returns a Chunker reading from r with the given parameters.

func (*Chunker) Next

func (c *Chunker) Next() ([]byte, error)

Next returns the next chunk. The returned slice is only valid until the following Next call. It returns io.EOF after the last chunk.

type CoWFile

type CoWFile struct {
	// contains filtered or unexported fields
}

func NewCoWFile

func NewCoWFile(store BlockStore, m CoWMap, blockSize int) (*CoWFile, error)

func (*CoWFile) ReadAt

func (f *CoWFile) ReadAt(p []byte, off int64) (int, error)

func (*CoWFile) Size

func (f *CoWFile) Size() int64

func (*CoWFile) Snapshot

func (f *CoWFile) Snapshot() *CoWFileView

func (*CoWFile) WriteAt

func (f *CoWFile) WriteAt(p []byte, off int64) (int, error)

type CoWFileView

type CoWFileView struct {
	// contains filtered or unexported fields
}

func (*CoWFileView) ReadAt

func (v *CoWFileView) ReadAt(p []byte, off int64) (int, error)

func (*CoWFileView) Size

func (v *CoWFileView) Size() int64

type CoWMap

type CoWMap interface {
	Snapshot() Snapshot
	Get(key string) (BlockID, bool)
	Set(key string, id BlockID)
}

CoWMap is a minimal copy-on-write key->BlockID map. Snapshot() must return an immutable view that is not affected by future writes.

type DiskBlockStore

type DiskBlockStore struct {
	// contains filtered or unexported fields
}

func NewDiskBlockStore

func NewDiskBlockStore(dir string) (*DiskBlockStore, error)

func (*DiskBlockStore) CollectObjectGarbage added in v0.1.3

func (s *DiskBlockStore) CollectObjectGarbage(ctx context.Context, live map[BlockID]struct{}, dryRun bool) (ObjectGCResult, error)

CollectObjectGarbage removes whole-file objects which contain no live block.

func (*DiskBlockStore) Compact

func (s *DiskBlockStore) Compact(orderedLive []BlockID) error

Compact rewrites the store to contain exactly the given chunks, in the given order: the frame amnesty. Dead chunks in old packs disappear because old packs are deleted after the new one is durable; loose garbage is swept in the same pass.

func (*DiskBlockStore) CompactOptions

func (s *DiskBlockStore) CompactOptions(orderedLive []BlockID, opts PackOptions) error

CompactOptions is Compact with explicit frame geometry.

func (*DiskBlockStore) Delete

func (s *DiskBlockStore) Delete(id BlockID) error

Delete removes a loose block. Deleting a missing block is not an error; chunks living inside packs are reclaimed by Compact (frame amnesty), so deleting them individually is a no-op by design.

func (*DiskBlockStore) ForEach

func (s *DiskBlockStore) ForEach(fn func(BlockID) error) error

ForEach calls fn for every block currently in the store. Temp files from in-flight writes are skipped. Iteration stops at the first error.

func (*DiskBlockStore) FrameCacheStats

func (s *DiskBlockStore) FrameCacheStats() FrameCacheStats

FrameCacheStats returns the current frame cache counters.

func (*DiskBlockStore) Get

func (s *DiskBlockStore) Get(id BlockID) ([]byte, error)

func (*DiskBlockStore) Has

func (s *DiskBlockStore) Has(id BlockID) (bool, error)

Has reports whether a block is present without reading it.

func (*DiskBlockStore) HasPacks

func (s *DiskBlockStore) HasPacks() (bool, error)

HasPacks reports whether any pack file exists, so callers can pick the amnesty path over per-chunk sweeping.

func (*DiskBlockStore) Put

func (s *DiskBlockStore) Put(data []byte) (BlockID, error)

func (*DiskBlockStore) PutDeferred

func (s *DiskBlockStore) PutDeferred(data []byte) (BlockID, error)

PutDeferred writes a block but leaves directory durability to Sync.

func (*DiskBlockStore) SetFrameCacheBytes

func (s *DiskBlockStore) SetFrameCacheBytes(n int64)

SetFrameCacheBytes caps the decompressed frame cache at n bytes (<= 0 restores the default), evicting immediately if the cache is over the new cap.

func (*DiskBlockStore) Size

func (s *DiskBlockStore) Size(id BlockID) (int64, error)

Size returns the stored size of a block in bytes.

func (*DiskBlockStore) Sync

func (s *DiskBlockStore) Sync() error

Sync makes prior deferred block renames durable.

func (*DiskBlockStore) VerifyPacks

func (s *DiskBlockStore) VerifyPacks() (frames, chunks int, err error)

VerifyPacks re-reads every pack frame, checking the stored frame checksum against the compressed payload and every chunk's content address against its extracted bytes. It returns how many frames and chunks were verified.

func (*DiskBlockStore) WritePack

func (s *DiskBlockStore) WritePack(ordered []BlockID) error

WritePack stores the given chunks, in the given order, into a new immutable pack file, then removes their loose copies. Order matters: the caller groups related chunks (versions of the same file) so the frame compression window can capture their redundancy.

func (*DiskBlockStore) WritePackOptions

func (s *DiskBlockStore) WritePackOptions(ordered []BlockID, opts PackOptions) error

WritePackOptions is WritePack with explicit frame geometry.

type FrameCacheStats

type FrameCacheStats struct {
	Hits   int64
	Misses int64
	Bytes  int64
}

FrameCacheStats reports the frame cache counters: hits, misses and the decompressed bytes currently held.

type MaterializeOptions added in v0.1.3

type MaterializeOptions struct {
	Mode          uint32
	Size          int64
	ContentDigest string
	PruneLoose    bool
	Deferred      bool
}

type MaterializeResult added in v0.1.3

type MaterializeResult struct {
	ObjectID BlockID
	Written  int64
	Reused   int64
}

type MemBlockStore

type MemBlockStore struct {
	// contains filtered or unexported fields
}

MemBlockStore is a tiny in-memory content-addressed store suitable for unit tests. It deduplicates blocks by blake3(content).

func NewMemBlockStore

func NewMemBlockStore() *MemBlockStore

func (*MemBlockStore) Get

func (s *MemBlockStore) Get(id BlockID) ([]byte, error)

func (*MemBlockStore) Put

func (s *MemBlockStore) Put(data []byte) (BlockID, error)

type MemCoWMap

type MemCoWMap struct {
	// contains filtered or unexported fields
}

MemCoWMap is a tiny in-memory CoW map intended for unit tests. Snapshot() clones the map to ensure snapshot isolation.

func NewMemCoWMap

func NewMemCoWMap() *MemCoWMap

func (*MemCoWMap) Get

func (c *MemCoWMap) Get(key string) (BlockID, bool)

func (*MemCoWMap) Set

func (c *MemCoWMap) Set(key string, id BlockID)

func (*MemCoWMap) Snapshot

func (c *MemCoWMap) Snapshot() Snapshot

type ObjectGCResult added in v0.1.3

type ObjectGCResult struct {
	RemovedObjects int
	RemovedBytes   int64
}

type ObjectStore added in v0.1.3

type ObjectStore struct {
	// contains filtered or unexported fields
}

func OpenObjectStore added in v0.1.3

func OpenObjectStore(blocks string) (*ObjectStore, error)

func (*ObjectStore) CollectGarbage added in v0.1.3

func (s *ObjectStore) CollectGarbage(ctx context.Context, live map[BlockID]struct{}, dryRun bool) (ObjectGCResult, error)

func (*ObjectStore) Compact added in v0.1.3

func (s *ObjectStore) Compact() error

func (*ObjectStore) CompactIfNeeded added in v0.1.3

func (s *ObjectStore) CompactIfNeeded(journalBytes int64) error

func (*ObjectStore) MaterializeBlocks added in v0.1.3

func (s *ObjectStore) MaterializeBlocks(ctx context.Context, destination string, blocks []BlockID, sizes []int64, getter BlockGetter, options MaterializeOptions) (MaterializeResult, error)

func (*ObjectStore) Sync added in v0.1.3

func (s *ObjectStore) Sync() error

type PackOptions

type PackOptions struct {
	// FrameTarget is the uncompressed frame size to cut at (default 256 KiB).
	FrameTarget int
	// BestCompression trades pack-time CPU for size.
	BestCompression bool
	// LongWindow enables long-distance matching inside frames.
	LongWindow bool
}

PackOptions tune the frame geometry and compression effort. Hot packs use small frames for cheap random access; cold history uses large frames and maximum effort, so whole file lineages share one compression window.

func ColdPackOptions

func ColdPackOptions() PackOptions

ColdPackOptions is the cold-tier preset: 4 MiB frames, maximum effort. The first read of an old state costs one bigger decompression, still bounded.

type Snapshot

type Snapshot interface {
	Get(key string) (BlockID, bool)
}

Snapshot is a read-only view of a key->BlockID mapping.

Jump to

Keyboard shortcuts

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