file

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package file provides a file system-backed key-value storage adapter.

pkg/adapter/storage/file/storage.go

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound indicates the requested key doesn't exist
	ErrNotFound = errors.New("key not found")

	// ErrInvalidKey indicates the key is empty or contains invalid characters
	ErrInvalidKey = errors.New("invalid key")
)

Functions

This section is empty.

Types

type Storage

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

Storage implements port.StoragePort using file system storage. It provides atomic write operations using temp file + rename pattern.

Architecture: Secondary Adapter (implements domain port) - Depends on: domain/port.StoragePort interface (dependency inversion) - Used by: DLQ, Tokenizer, AuditLogger - Pure infrastructure concern (no business logic)

func NewStorage

func NewStorage(baseDir string) (*Storage, error)

NewStorage creates a new file-based storage adapter. The baseDir will be created if it doesn't exist.

Example:

storage, err := file.NewStorage("/var/lib/morpheus/storage")
if err != nil {
    log.Fatal(err)
}

Architecture Note: This is a SECONDARY ADAPTER that implements the StoragePort interface. It has NO knowledge of the domain layer - it simply provides file storage.

func (*Storage) Close

func (s *Storage) Close() error

Close performs any cleanup operations. Currently a no-op for file storage, but included for interface compatibility.

func (*Storage) Delete

func (s *Storage) Delete(ctx context.Context, key string) error

Delete removes the file associated with the key. Returns ErrNotFound if the key doesn't exist.

Example:

err := storage.Delete(ctx, "job-123")

func (*Storage) List

func (s *Storage) List(ctx context.Context, prefix string) ([]string, error)

List returns all keys with the given prefix. Useful for querying stored items (e.g., all failed jobs).

Example:

keys, err := storage.List(ctx, "dlq/")
// Returns: ["dlq/job-1", "dlq/job-2", ...]

func (*Storage) Load

func (s *Storage) Load(ctx context.Context, key string) (any, error)

Load retrieves data from file and unmarshals it. Returns ErrNotFound if the key doesn't exist.

Example:

var job entity.Job
data, err := storage.Load(ctx, "job-123")
if err == file.ErrNotFound {
    // Handle missing key
}
json.Unmarshal(data.([]byte), &job)

func (*Storage) Save

func (s *Storage) Save(ctx context.Context, key string, data any) error

Save persists data to a file with atomic write operation. Uses temp file + rename pattern to ensure atomicity.

Data is serialized to JSON format. The key becomes the filename.

Example:

err := storage.Save(ctx, "job-123", job)

Jump to

Keyboard shortcuts

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