cas

package
v1.8.2 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: MIT Imports: 13 Imported by: 2

Documentation

Overview

Package cas provides content-addressable storage for Trove. Objects are stored under their content hash, enabling automatic deduplication, integrity verification, and immutable storage patterns.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = fmt.Errorf("cas: hash not found: %w", driver.ErrNotFound)

ErrNotFound is returned when a hash is not in the index.

It wraps driver.ErrNotFound, so content missing from the CAS classifies the same way as an object missing from a bucket: errors.Is(err, cas.ErrNotFound) still identifies it precisely, while a caller that only needs to know the content is permanently gone can match trove.ErrNotFound or call trove.Permanent and get the right answer without special-casing the CAS.

Functions

This section is empty.

Types

type CAS

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

CAS provides content-addressable storage operations. Objects are stored under their content hash, enabling deduplication and integrity verification.

func New

func New(store driver.Driver, opts ...Option) *CAS

New creates a new CAS instance backed by the given driver.

func (*CAS) Algorithm

func (c *CAS) Algorithm() HashAlgorithm

Algorithm returns the hash algorithm used by this CAS instance.

func (*CAS) Bucket

func (c *CAS) Bucket() string

Bucket returns the bucket this CAS stores content in.

func (*CAS) Exists

func (c *CAS) Exists(ctx context.Context, hash string) (bool, error)

Exists checks if content with the given hash is stored.

func (*CAS) GC

func (c *CAS) GC(ctx context.Context) (*GCResult, error)

GC performs garbage collection, removing unreferenced and unpinned objects.

func (*CAS) Pin

func (c *CAS) Pin(ctx context.Context, hash string) error

Pin prevents garbage collection of a CAS object.

func (*CAS) Retrieve

func (c *CAS) Retrieve(ctx context.Context, hash string) (*driver.ObjectReader, error)

Retrieve gets content by hash.

func (*CAS) Stat

func (c *CAS) Stat(ctx context.Context, hash string) (*Entry, error)

Stat returns the index entry for a hash without reading its content.

It returns ErrNotFound when the index has no entry. With the default in-memory index that includes content still present in the bucket after a restart, because the index does not survive one.

func (*CAS) Store

func (c *CAS) Store(ctx context.Context, r io.Reader) (hash string, info *driver.ObjectInfo, err error)

Store stores content and returns its hash. If identical content already exists, the reference count is incremented and no duplicate is stored.

It records a "store cas object" span with the algorithm, the size and whether the content was a duplicate. The hash is never recorded: it identifies the content.

func (*CAS) Unpin

func (c *CAS) Unpin(ctx context.Context, hash string) error

Unpin allows garbage collection of a CAS object.

type Entry

type Entry struct {
	Hash     string `json:"hash"`
	Bucket   string `json:"bucket"`
	Key      string `json:"key"`
	Size     int64  `json:"size"`
	RefCount int    `json:"ref_count"`
	Pinned   bool   `json:"pinned"`
}

Entry represents a CAS index entry mapping a content hash to its storage location.

type GCResult

type GCResult struct {
	// Scanned is the number of entries examined.
	Scanned int `json:"scanned"`

	// Deleted is the number of unreferenced objects removed.
	Deleted int `json:"deleted"`

	// FreedBytes is the total bytes freed.
	FreedBytes int64 `json:"freed_bytes"`

	// Errors is the number of entries that failed to delete.
	Errors int `json:"errors"`
}

GCResult reports the outcome of a garbage collection run.

type HashAlgorithm

type HashAlgorithm int

HashAlgorithm identifies the hash algorithm for CAS addressing.

const (
	// AlgSHA256 uses SHA-256 (default, widely compatible).
	AlgSHA256 HashAlgorithm = iota
	// AlgBlake3 uses BLAKE3 (faster, modern).
	AlgBlake3
	// AlgXXHash uses XXHash (non-cryptographic, fastest).
	AlgXXHash
)

func (HashAlgorithm) String

func (a HashAlgorithm) String() string

String returns the algorithm name.

type Index

type Index interface {
	// Get returns the entry for a hash, or an error if not found.
	Get(ctx context.Context, hash string) (*Entry, error)

	// Put stores or updates an entry. If the hash already exists,
	// the ref count is incremented.
	Put(ctx context.Context, entry *Entry) error

	// Delete removes an entry by hash.
	Delete(ctx context.Context, hash string) error

	// IncrementRef increments the reference count for a hash.
	IncrementRef(ctx context.Context, hash string) error

	// DecrementRef decrements the reference count for a hash.
	DecrementRef(ctx context.Context, hash string) error

	// Pin prevents garbage collection of the entry.
	Pin(ctx context.Context, hash string) error

	// Unpin allows garbage collection.
	Unpin(ctx context.Context, hash string) error

	// ListUnpinned returns all entries with ref_count=0 and pinned=false.
	ListUnpinned(ctx context.Context) ([]*Entry, error)
}

Index maps content hashes to their storage locations. Implementations can be in-memory, database-backed, etc.

type MemoryIndex

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

MemoryIndex is an in-memory Index implementation for testing.

func NewMemoryIndex

func NewMemoryIndex() *MemoryIndex

NewMemoryIndex creates a new in-memory CAS index.

func (*MemoryIndex) DecrementRef

func (m *MemoryIndex) DecrementRef(_ context.Context, hash string) error

DecrementRef decrements the reference count (min 0).

func (*MemoryIndex) Delete

func (m *MemoryIndex) Delete(_ context.Context, hash string) error

Delete removes an entry.

func (*MemoryIndex) Get

func (m *MemoryIndex) Get(_ context.Context, hash string) (*Entry, error)

Get returns the entry for a hash.

func (*MemoryIndex) IncrementRef

func (m *MemoryIndex) IncrementRef(_ context.Context, hash string) error

IncrementRef increments the reference count.

func (*MemoryIndex) ListUnpinned

func (m *MemoryIndex) ListUnpinned(_ context.Context) ([]*Entry, error)

ListUnpinned returns entries eligible for garbage collection.

func (*MemoryIndex) Pin

func (m *MemoryIndex) Pin(_ context.Context, hash string) error

Pin marks the entry as pinned.

func (*MemoryIndex) Put

func (m *MemoryIndex) Put(_ context.Context, entry *Entry) error

Put stores or updates an entry.

func (*MemoryIndex) Unpin

func (m *MemoryIndex) Unpin(_ context.Context, hash string) error

Unpin marks the entry as unpinned.

type Option

type Option func(*CAS)

Option configures a CAS instance.

func WithAlgorithm

func WithAlgorithm(alg HashAlgorithm) Option

WithAlgorithm sets the hash algorithm (default: SHA256).

func WithBucket

func WithBucket(bucket string) Option

WithBucket sets the storage bucket for CAS objects.

func WithIndex

func WithIndex(idx Index) Option

WithIndex sets the CAS index (default: in-memory).

func WithMetrics

func WithMetrics(r observe.Recorder) Option

WithMetrics records CAS stores and garbage collection on r (trove_cas_stores_total, trove_cas_gc_deleted_total and trove_cas_gc_freed_bytes_total). A nil r, the default, records nothing. trove.WithCAS passes the Trove's own Recorder.

Jump to

Keyboard shortcuts

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