scratch

package
v3.6.7 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package scratch provides an abstraction for scratch space.

Index

Constants

View Source
const InvalidHandle = Handle(0)

InvalidHandle is the zero value for Handle.

Variables

This section is empty.

Functions

This section is empty.

Types

type Filesystem

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

Filesystem is an implementation of Store that retains data on the filesystem.

Filesystem supports a memory fallback mechanism if writing data fails.

func NewFilesystem

func NewFilesystem(logger log.Logger, path string) (*Filesystem, error)

NewFilesystem returns a new Filesystem store. Stored data is persisted to disk with a random filename and an extension ".lokiscratch".

To avoid leaking scratch files between process iterations, NewFilesystem will remove any existing ".lokiscratch" files in path upon startup.

NewFilesystem creates the path if it does not exist.

func (*Filesystem) Put

func (fs *Filesystem) Put(p []byte) Handle

Put stores the contents of p into scratch space.

Put will be written with a random filename and the extension ".lokiscratch". If writing to the filesystem fails, p will instead be stored in-memory.

func (*Filesystem) Read

func (fs *Filesystem) Read(h Handle) (io.ReadSeekCloser, error)

Read returns a reader for the file identifier by h. Read returns HandleNotFoundError if h doesn't exist either on disk or in-memory.

Callers must close the reader when done to release resources. Reading may fail if the handle is removed while reading data.

func (*Filesystem) Remove

func (fs *Filesystem) Remove(h Handle) error

Remove removes the handle identified by h. If h is stored on disk, its corresponding file will be removed. Remove returns HandleNotFoundError if h doesn't exist on disk or in-memory.

type Handle

type Handle uint64

Handle is a identifier for data placed into scratch space. Handles are unique per store. Handles are immutable once created and can only be read or deleted.

The zero value for handle is invalid.

type HandleNotFoundError

type HandleNotFoundError Handle

HandleNotFoundError is returned when performing an operation on a handle that doesn't exist.

func (HandleNotFoundError) Error

func (e HandleNotFoundError) Error() string

Error returns a string representation of e.

type Memory

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

Memory is an implementation of Store that retains data in memory.

func NewMemory

func NewMemory() *Memory

NewMemory returns a new Memory store.

func (*Memory) Put

func (m *Memory) Put(p []byte) Handle

Put stores the contents of p into memory.

func (*Memory) Read

func (m *Memory) Read(h Handle) (io.ReadSeekCloser, error)

Read returns a reader for h. Read returns HandleNotFoundError if h doesn't exist in memory.

func (*Memory) Remove

func (m *Memory) Remove(h Handle) error

Remove removes h. Remove returns HandleNotFoundError if h doesn't exist in memory.

type Metrics

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

Metrics is a set of metrics for monitoring a Store.

func NewMetrics

func NewMetrics() *Metrics

NewMetrics returns a new Metrics. Returned Metrics must be registered to a registerer using Metrics.Register.

func (*Metrics) Register

func (m *Metrics) Register(reg prometheus.Registerer) error

Register registers metrics to report to reg.

func (*Metrics) Unregister

func (m *Metrics) Unregister(reg prometheus.Registerer)

Unregister unregisters metrics from reg.

type Store

type Store interface {

	// Put stores the contents of p into scratch space.
	Put(p []byte) Handle

	// Read returns a reader for the file identifier by h. Read returns
	// [InvalidHandleError] if h doesn't exist in the Store.
	//
	// Callers must close the reader when done to release resources. Reading may
	// fail if the handle is removed while reading data.
	Read(h Handle) (io.ReadSeekCloser, error)

	// Remove removes the handle identified by h. Remove returns
	// [InvalidHandleError] if h doesn't exist in the Store.
	Remove(h Handle) error
}

Store is an abstraction for temporarily storing data. Store is ephemeral and starts fresh every time it is created.

Store implementations must be safe for concurrent access.

func ObserveStore

func ObserveStore(metrics *Metrics, store Store) Store

ObserveStore wraps a store and accumulates statistics into the provided Metrics. If metrics is nil, ObserveStore performs no wrapping.

func Open

func Open(logger log.Logger, path string) (Store, error)

Open returns the default Store implementation based on whether path is set.

If path is empty, Open returns a Memory store. If path is non-empty, Open returns a Filesystem store.

Open returns an error if the path is non-empty but refers to a non-existent directory.

To observe metrics on the returned store, wrap the result with ObserveStore.

Jump to

Keyboard shortcuts

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