mappedresource

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package mappedresource defines TreeDB's shared vocabulary for immutable file-backed resource views. It is intentionally small: subsystem-specific caches may remain specialized while using these keys, scopes, handles, stats, and maintenance pins as the common contract.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDirectViewNilHandle      = errors.New("mappedresource: nil handle")
	ErrDirectViewReleasedHandle = errors.New("mappedresource: handle is released")
	ErrDirectViewWrongEndian    = errors.New("mappedresource: direct view wrong endian")
	ErrDirectViewLengthMultiple = errors.New("mappedresource: direct view length multiple mismatch")
	ErrDirectViewUnaligned      = errors.New("mappedresource: direct view unaligned")
)

Functions

func Float32View

func Float32View(data []byte) ([]float32, error)

Float32View exposes bytes as []float32 after validation.

func Float64View

func Float64View(data []byte) ([]float64, error)

Float64View exposes bytes as []float64 after validation.

func Int64View

func Int64View(data []byte) ([]int64, error)

Int64View exposes bytes as []int64 after validation.

func NativeLittleEndian

func NativeLittleEndian() bool

func Uint16View

func Uint16View(data []byte) ([]uint16, error)

Uint16View exposes bytes as []uint16 after validation.

func Uint32View

func Uint32View(data []byte) ([]uint32, error)

Uint32View exposes bytes as []uint32 after validation.

func Uint64View

func Uint64View(data []byte) ([]uint64, error)

Uint64View exposes bytes as []uint64 after validation.

func ValidateDirectView

func ValidateDirectView(data []byte, opts DirectViewOptions) (int, error)

ValidateDirectView validates length, alignment, and endian eligibility for a zero-copy typed view. It returns the element count.

Types

type AcquireOptions

type AcquireOptions struct {
	Reason         string
	ValidationMode ValidationMode
	PreferMapped   bool
	AllowHeapCopy  bool
	FallbackReason FallbackReason
	ResourceRoot   string
	ResourcePath   string
}

AcquireOptions controls resource acquisition and accounting labels. ResourceRoot and ResourcePath identify the durable storage root/path for maintenance filtering when the handle protects file-backed resources.

type Class

type Class string

Class identifies the resource family. It is coarse enough for shared accounting but does not replace subsystem-specific formats.

const (
	ClassValueLog         Class = "value_log"
	ClassLeafLog          Class = "leaf_log"
	ClassTypedRowAsset    Class = "typed_row_asset"
	ClassTypedColumnAsset Class = "typed_column_asset"
	ClassExternalAsset    Class = "external_asset"
)

type DenyReason

type DenyReason string

DenyReason and FallbackReason provide stable accounting labels.

const (
	DenyInvalidKey      DenyReason = "invalid_key"
	DenyInvalidScope    DenyReason = "invalid_scope"
	DenyUnsupported     DenyReason = "unsupported"
	DenyOpenFailed      DenyReason = "open_failed"
	DenyOutOfBounds     DenyReason = "out_of_bounds"
	DenyReadFailed      DenyReason = "read_failed"
	DenyMmapFailed      DenyReason = "mmap_failed"
	DenyReleaseMismatch DenyReason = "release_mismatch"
)

type DirectViewOptions

type DirectViewOptions struct {
	ElementSize         uintptr
	Alignment           uintptr
	TypeName            string
	RequireLittleEndian bool
}

DirectViewOptions controls validation before raw bytes are reinterpreted as a fixed-width typed slice.

type FallbackReason

type FallbackReason string
const (
	FallbackMmapFailed FallbackReason = "mmap_failed"
	FallbackReadAt     FallbackReason = "readat"
)

type Handle

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

Handle owns a live resource view. Bytes are valid until Release. Release is idempotent and updates accounting only once.

func (*Handle) Bytes

func (h *Handle) Bytes() []byte

Bytes returns the live byte view. After Release it returns nil.

func (*Handle) Key

func (h *Handle) Key() Key

Key returns the resource key.

func (*Handle) Release

func (h *Handle) Release() error

Release releases the handle and removes its maintenance pin.

func (*Handle) Released

func (h *Handle) Released() bool

Released reports whether the handle has been released.

func (*Handle) Scope

func (h *Handle) Scope() Scope

Scope returns the lifetime scope.

func (*Handle) Source

func (h *Handle) Source() Source

Source returns the backing source.

type Key

type Key struct {
	Class      Class
	Namespace  string
	Kind       string
	Generation uint64
	PartID     uint64
	FileID     uint32
	Offset     int64
	Length     int64
	Checksum   uint64
	Version    uint16
	Encoding   string
	Section    Section
}

Key is the stable identity for a mapped or copied resource range.

Offset/Length are a physical range in FileID/object identity. Section is an optional logical refinement for internally sectioned assets. Checksum is a generic content checksum/digest slot; callers may use the lower bits when the source format stores uint32 checksums.

func (Key) Equal

func (k Key) Equal(other Key) bool

Equal reports exact key equality.

func (Key) Validate

func (k Key) Validate() error

Validate rejects incomplete or unsafe resource identities. It is deliberately format-agnostic; subsystem adapters may apply stricter checks before calling into the manager.

type Manager

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

Manager tracks resource handles and accounting. It intentionally does not choose a global cache policy; subsystems can wrap existing caches while using this manager for lifetime, pin, and stats visibility.

func NewManager

func NewManager() *Manager

NewManager constructs an empty manager.

func (*Manager) AcquireBytes

func (m *Manager) AcquireBytes(key Key, scope Scope, source Source, data []byte, opts AcquireOptions) (*Handle, error)

AcquireBytes registers caller-provided immutable bytes as a resource handle. The manager does not copy bytes; callers must ensure the slice remains stable for the handle lifetime.

func (*Manager) AcquireFileRange

func (m *Manager) AcquireFileRange(key Key, scope Scope, path string, opts AcquireOptions) (*Handle, error)

AcquireFileRange opens path and returns either an mmap-backed or heap-copy handle for key.Offset:key.Offset+key.Length. Mmap is best-effort when PreferMapped is set; AllowHeapCopy controls fallback.

func (*Manager) Float32View

func (m *Manager) Float32View(h *Handle) ([]float32, error)

Float32View records direct-view success/failure in addition to validating.

func (*Manager) Float64View

func (m *Manager) Float64View(h *Handle) ([]float64, error)

Float64View records direct-view success/failure in addition to validating.

func (*Manager) Int64View

func (m *Manager) Int64View(h *Handle) ([]int64, error)

Int64View records direct-view success/failure in addition to validating.

func (*Manager) PinSummary

func (m *Manager) PinSummary() []Pin

PinSummary returns active handles visible to maintenance.

func (*Manager) RecordHit

func (m *Manager) RecordHit()

RecordHit records an adapter cache hit.

func (*Manager) RecordMiss

func (m *Manager) RecordMiss()

RecordMiss records an adapter cache miss.

func (*Manager) Stats

func (m *Manager) Stats() Stats

Stats returns a stable snapshot.

func (*Manager) Uint16View

func (m *Manager) Uint16View(h *Handle) ([]uint16, error)

Uint16View records direct-view success/failure in addition to validating.

func (*Manager) Uint32View

func (m *Manager) Uint32View(h *Handle) ([]uint32, error)

Uint32View records direct-view success/failure in addition to validating.

func (*Manager) Uint64View

func (m *Manager) Uint64View(h *Handle) ([]uint64, error)

Uint64View records direct-view success/failure in addition to validating.

type Pin

type Pin struct {
	ID     uint64
	Key    Key
	Scope  Scope
	Source Source
	Bytes  int64
	Reason string
	Root   string
	Path   string
}

Pin describes one active handle visible to maintenance planning. Root and Path carry storage identity for DB-local filtering; AcquireFileRange fills Path from the opened file path when callers leave ResourcePath empty.

func GlobalPinSummary

func GlobalPinSummary() []Pin

GlobalPinSummary returns all active process-local resource handles visible to destructive maintenance. Callers must still filter by resource class, namespace, and subsystem-specific identity before acting.

type Scope

type Scope struct {
	Kind       ScopeKind
	ID         string
	ParentID   string
	Collection string
	Namespace  string
	Generation uint64
	Reason     string
}

Scope ties a physical resource handle to a logical lifetime. Snapshot/read view scopes should anchor ordinary query/search handles; maintenance scopes anchor destructive planning and protection checks.

func NewScope

func NewScope(kind ScopeKind, id string) Scope

NewScope constructs a scope with the minimum required fields.

func (Scope) Validate

func (s Scope) Validate() error

Validate rejects lifetime-less scopes. A non-empty ID makes ownership and tests explicit even when the scope is only process-local.

func (Scope) ValidateForKey

func (s Scope) ValidateForKey(k Key) error

ValidateForKey checks both the scope and key. It also catches accidental namespace mismatches when both sides carry a namespace.

type ScopeKind

type ScopeKind string

ScopeKind identifies the logical lifetime that owns a resource handle.

const (
	ScopeDB                 ScopeKind = "db"
	ScopeSnapshot           ScopeKind = "snapshot"
	ScopeCollectionReadView ScopeKind = "collection_read_view"
	ScopeManifestGeneration ScopeKind = "manifest_generation"
	ScopeMaintenance        ScopeKind = "maintenance"
	ScopePreparedQuery      ScopeKind = "prepared_query"
	ScopePreparedSearch     ScopeKind = "prepared_search"
	ScopeTypedRowReader     ScopeKind = "typed_row_reader"
	ScopeColumnPartReader   ScopeKind = "column_part_reader"
)

type Section

type Section struct {
	Kind     string
	Category string
	Name     string
	Column   string
	Ordinal  uint32
}

Section identifies a logical sub-range within an immutable asset. Column-part assets use this to address descriptor, dictionary, vector, adjacency, and scalar payload sections without requiring one OS file per section.

func (Section) Empty

func (s Section) Empty() bool

Empty reports whether the section identity is absent.

type Source

type Source string

Source identifies how a handle's bytes are backed.

const (
	SourceMapped          Source = "mmap"
	SourceHeapCopy        Source = "heap_copy"
	SourceDerivedMetadata Source = "derived_metadata"
)

type Stats

type Stats struct {
	ActiveHandles              int64
	ActiveMappedBytes          int64
	ActiveHeapCopyBytes        int64
	ActiveDerivedMetadataBytes int64

	TotalAcquires             uint64
	TotalReleases             uint64
	TotalMappedBytes          uint64
	TotalHeapCopyBytes        uint64
	TotalDerivedMetadataBytes uint64

	Hits          uint64
	Misses        uint64
	FallbackReads uint64
	Opens         uint64
	Closes        uint64
	Errors        uint64

	DirectViewSuccesses uint64
	DirectViewFailures  uint64

	DeniedByReason      map[DenyReason]uint64
	FallbacksByReason   map[FallbackReason]uint64
	ValidationModeReads map[ValidationMode]uint64
}

Stats is the common row+column resource accounting shape used by adapters and future column-part readers.

func GlobalStats

func GlobalStats() Stats

GlobalStats returns process-wide resource accounting across mappedresource managers. It is intended for maintenance diagnostics; subsystem-local Stats remains the precise per-manager view.

type ValidationMode

type ValidationMode string
const (
	ValidationVerify       ValidationMode = "verify"
	ValidationCachedVerify ValidationMode = "cached_verify"
	ValidationSkipChecksum ValidationMode = "skip_checksums"
)

Jump to

Keyboard shortcuts

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