cache

package
v1.0.0-rc0 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package cache provides cache implementations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HashBuildConfig

func HashBuildConfig(buildConfig *network.BuildConfig) string

func IsNotActive

func IsNotActive(err error) bool

IsNotActive returns true if the error is a NotActiveError.

func MakeCacheKey

func MakeCacheKey(networkType, commitHash string, buildConfig *network.BuildConfig) string

MakeCacheKey creates a cache key: {networkType}/{commitHash}-{configHash} This ensures binaries with different configs are cached separately.

func WriteMetadata

func WriteMetadata(path string, metadata *Metadata) error

Types

type BinaryCache

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

func NewBinaryCache

func NewBinaryCache(homeDir, binaryName string, logger *output.Logger) *BinaryCache

func (*BinaryCache) BinaryName

func (c *BinaryCache) BinaryName() string

func (*BinaryCache) CacheDir

func (c *BinaryCache) CacheDir() string

func (*BinaryCache) Clean

func (c *BinaryCache) Clean() error

func (*BinaryCache) GetBinaryPath

func (c *BinaryCache) GetBinaryPath(cacheKey string) string

func (*BinaryCache) GetBinaryPathWithConfig

func (c *BinaryCache) GetBinaryPathWithConfig(networkType, commitHash string, buildConfig *network.BuildConfig) string

func (*BinaryCache) GetEntryDir

func (c *BinaryCache) GetEntryDir(cacheKey string) string

func (*BinaryCache) Initialize

func (c *BinaryCache) Initialize() error

func (*BinaryCache) IsCached

func (c *BinaryCache) IsCached(cacheKey string) bool

func (*BinaryCache) IsCachedWithConfig

func (c *BinaryCache) IsCachedWithConfig(networkType, commitHash string, buildConfig *network.BuildConfig) bool

func (*BinaryCache) List

func (c *BinaryCache) List() []*CachedBinary

func (*BinaryCache) Lookup

func (c *BinaryCache) Lookup(cacheKey string) *CachedBinary

func (*BinaryCache) LookupWithConfig

func (c *BinaryCache) LookupWithConfig(networkType, commitHash string, buildConfig *network.BuildConfig) *CachedBinary

func (*BinaryCache) Remove

func (c *BinaryCache) Remove(cacheKey string) error

func (*BinaryCache) RemoveWithConfig

func (c *BinaryCache) RemoveWithConfig(networkType, commitHash string, buildConfig *network.BuildConfig) error

func (*BinaryCache) Stats

func (c *BinaryCache) Stats() *CacheStats

func (*BinaryCache) Store

func (c *BinaryCache) Store(sourcePath string, cached *CachedBinary) error

func (*BinaryCache) ValidateKey

func (c *BinaryCache) ValidateKey(cacheKey string) error

func (*BinaryCache) ValidateWithConfig

func (c *BinaryCache) ValidateWithConfig(networkType, commitHash string, buildConfig *network.BuildConfig) error

type BinaryCacheAdapter

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

BinaryCacheAdapter implements ports.BinaryCache.

func NewBinaryCacheAdapter

func NewBinaryCacheAdapter(homeDir, binaryName string, logger *output.Logger) (*BinaryCacheAdapter, error)

NewBinaryCacheAdapter creates a new BinaryCacheAdapter.

func (*BinaryCacheAdapter) CacheDir

func (a *BinaryCacheAdapter) CacheDir() string

CacheDir returns the cache directory path.

func (*BinaryCacheAdapter) Clean

func (a *BinaryCacheAdapter) Clean() error

Clean removes all cached binaries.

func (*BinaryCacheAdapter) Get

func (a *BinaryCacheAdapter) Get(ref string) (string, bool)

Get retrieves a cached binary path by ref.

func (*BinaryCacheAdapter) GetActive

func (a *BinaryCacheAdapter) GetActive() (string, error)

GetActive returns the currently active binary path.

func (*BinaryCacheAdapter) Has

func (a *BinaryCacheAdapter) Has(ref string) bool

Has checks if a binary is cached.

func (*BinaryCacheAdapter) List

func (a *BinaryCacheAdapter) List() []string

List returns all cached binary refs.

func (*BinaryCacheAdapter) ListDetailed

func (a *BinaryCacheAdapter) ListDetailed() []ports.CachedBinaryInfo

ListDetailed returns detailed information about all cached binaries.

func (*BinaryCacheAdapter) Remove

func (a *BinaryCacheAdapter) Remove(ref string) error

Remove deletes a cached binary.

func (*BinaryCacheAdapter) SetActive

func (a *BinaryCacheAdapter) SetActive(ref string) error

SetActive sets the active binary version. The ref can be: - A full cache key (49 chars: {commitHash}-{tagsHash}) - A commit hash (40 chars) - will search for matching cache entry - A directory name on disk (for entries created by other processes)

func (*BinaryCacheAdapter) Stats

func (a *BinaryCacheAdapter) Stats() ports.CacheStats

Stats returns cache statistics.

func (*BinaryCacheAdapter) Store

func (a *BinaryCacheAdapter) Store(ctx context.Context, ref string, binaryPath string) (string, error)

Store saves a binary to the cache.

func (*BinaryCacheAdapter) SymlinkInfo

func (a *BinaryCacheAdapter) SymlinkInfo() (*ports.SymlinkInfo, error)

SymlinkInfo returns information about the current symlink.

func (*BinaryCacheAdapter) SymlinkPath

func (a *BinaryCacheAdapter) SymlinkPath() string

SymlinkPath returns the symlink path.

type BinaryScanner

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

BinaryScanner scans cache directory for binaries matching criteria. This implements the Scanner component from the implementation plan.

Design Decision: Uses filesystem abstraction for testability. Performance: Can scan 100+ binaries in <1 second (filesystem I/O bound).

func NewBinaryScanner

func NewBinaryScanner(fs filesystem.FileSystem) *BinaryScanner

NewBinaryScanner creates a new binary scanner with the given filesystem. Use filesystem.NewOSFileSystem() for production, or a mock for testing.

func (*BinaryScanner) ScanAllNetworks

func (s *BinaryScanner) ScanAllNetworks(ctx context.Context, cacheDir, binaryName string) ([]CachedBinaryMetadata, error)

ScanAllNetworks finds all binaries across ALL network directories. Use this when networkType is unknown or empty (fallback scanning).

Parameters:

  • ctx: Context for cancellation
  • cacheDir: Base cache directory (e.g., ~/.devnet-builder/cache/binaries)
  • binaryName: Binary name to search for (e.g., "stabled")

Returns:

  • Slice of CachedBinaryMetadata sorted by modification time (most recent first)
  • Empty slice if no binaries found
  • Error only for permission denied or filesystem failures

This method scans subdirectories of cacheDir that look like network names (mainnet, testnet, devnet, etc.) and searches for binaries in each.

func (*BinaryScanner) ScanCachedBinaries

func (s *BinaryScanner) ScanCachedBinaries(ctx context.Context, cacheDir, networkType, binaryName string) ([]CachedBinaryMetadata, error)

ScanCachedBinaries finds all binaries matching the specified criteria.

Parameters:

  • ctx: Context for cancellation (currently unused but reserved for future)
  • cacheDir: Base cache directory (e.g., ~/.devnet-builder/cache/binaries)
  • networkType: Network filter ("mainnet", "testnet")
  • binaryName: Binary name to search for (e.g., "stabled")

Returns:

  • Slice of CachedBinaryMetadata sorted by modification time (most recent first)
  • Empty slice (not error) if cache directory doesn't exist
  • Error only for permission denied or other filesystem failures

Directory Structure Expected:

{cacheDir}/{networkType}/{cacheKey}/{binaryName}
Example: ~/.devnet-builder/cache/binaries/mainnet/80ad31b-empty/stabled

Edge Cases Handled:

  • EC-001: Empty cache directory → returns empty slice
  • EC-009: Permission denied → returns error with actionable message
  • Invalid cache key format → skips silently (logs could be added)
  • Non-directory entries in network dir → skips
  • Missing binary in cache key dir → skips

type CacheError

type CacheError struct {
	Operation string
	Message   string
}

CacheError is returned when cache operations fail.

func (*CacheError) Error

func (e *CacheError) Error() string

type CacheStats

type CacheStats struct {
	TotalEntries int
	TotalSize    int64
}

type CachedBinary

type CachedBinary struct {
	CommitHash  string               `json:"commit_hash"`
	Ref         string               `json:"ref"`
	BuildTime   time.Time            `json:"build_time"`
	Size        int64                `json:"size"`
	NetworkType string               `json:"network_type"`
	BuildConfig *network.BuildConfig `json:"build_config"`
	BinaryPath  string               `json:"-"`
	DirKey      string               `json:"-"`
}

func (*CachedBinary) ActualDirKey

func (c *CachedBinary) ActualDirKey() string

func (*CachedBinary) CacheKey

func (c *CachedBinary) CacheKey() string

CacheKey returns the cache key: {networkType}/{commitHash}-{configHash}

func (*CachedBinary) ToMetadata

func (c *CachedBinary) ToMetadata() *Metadata

type CachedBinaryMetadata

type CachedBinaryMetadata struct {
	// Filesystem attributes
	Path    string    // Absolute path: ~/.devnet-builder/cache/binaries/mainnet/80ad31b-empty/stabled
	Name    string    // Binary name: "stabled", "aultd"
	Size    int64     // File size in bytes
	ModTime time.Time // Last modification timestamp

	// Derived display attributes
	SizeHuman       string // Human-readable: "45.2 MB"
	ModTimeRelative string // Relative time: "2 hours ago"

	// Version information (from binary version command - populated by validator)
	Version    string // Semantic version: "v1.0.0"
	CommitHash string // Full git commit: "80ad31b1234567890abcdef1234567890abcdef" (40 chars)

	// Cache key components (parsed from directory structure)
	CommitHashShort string // First 8 chars: "80ad31b"
	CacheKey        string // Full key: "mainnet/80ad31b-empty"
	NetworkType     string // "mainnet", "testnet", "devnet"
	ConfigHash      string // Build config hash: "empty", "abc123"

	// Validation status (populated by validator)
	IsValid         bool   // true if passed validation (executable, version detectable)
	ValidationError string // Error message if validation failed (for logging)
}

CachedBinaryMetadata represents a discovered cached binary with all metadata. This struct matches the data model defined in specs/interactive-binary-select/data-model.md

type Metadata

type Metadata struct {
	CommitHash  string               `json:"commit_hash"`
	Ref         string               `json:"ref"`
	BuildTime   time.Time            `json:"build_time"`
	Size        int64                `json:"size"`
	NetworkType string               `json:"network_type"`
	BuildConfig *network.BuildConfig `json:"build_config"`
}

func ReadMetadata

func ReadMetadata(path string) (*Metadata, error)

type NotActiveError

type NotActiveError struct {
	BinaryName string
}

NotActiveError is returned when no active binary is set.

func (*NotActiveError) Error

func (e *NotActiveError) Error() string

type SymlinkManager

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

func NewSymlinkManager

func NewSymlinkManager(homeDir, binaryName string) *SymlinkManager

func (*SymlinkManager) Switch

func (m *SymlinkManager) Switch(targetPath string) error

Switch atomically switches the symlink using the atomic rename pattern.

func (*SymlinkManager) SwitchToCache

func (m *SymlinkManager) SwitchToCache(cache *BinaryCache, cacheKey string) error

func (*SymlinkManager) SwitchToCacheWithConfig

func (m *SymlinkManager) SwitchToCacheWithConfig(cache *BinaryCache, networkType, commitHash string, buildConfig *network.BuildConfig) error

func (*SymlinkManager) SymlinkPath

func (m *SymlinkManager) SymlinkPath() string

Jump to

Keyboard shortcuts

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