state

package
v1.300057.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// FileMode is the file permissions used for the state file.
	FileMode = 0644
)

Variables

This section is empty.

Functions

func FilePath

func FilePath(dir, name string) string

FilePath combines the directory and escaped name.

Types

type FileRangeManager

type FileRangeManager Manager[Range, RangeList]

FileRangeManager is a state manager that handles the Range.

func NewFileRangeManager

func NewFileRangeManager(cfg ManagerConfig) FileRangeManager

type FileRangeQueue

type FileRangeQueue Queue[Range]

type Manager

type Manager[I, O any] interface {
	Queue[I]
	// Restore loads the previous state.
	Restore() (O, error)
	// Run starts the update/save loop.
	Run(ch Notification)
}

Manager handles persistence of state.

type ManagerConfig

type ManagerConfig struct {
	// Name is the metadata that will be persisted in the last line of the state file.
	Name string
	// StateFileDir is the directory where the state file will be written to.
	StateFileDir string
	// StateFilePrefix is an optional prefix added to the filename. Can be used to group state files.
	StateFilePrefix string
	// QueueSize determines the size of the internal buffer for pending state changes.
	QueueSize int
	// SaveInterval determines how often the state is persisted.
	SaveInterval time.Duration
	// MaxPersistedItems is the maximum number of items to persist in the saved state. If zero or negative, the
	// persistence is unbounded.
	MaxPersistedItems int
}

ManagerConfig provides all options available to configure a Manager.

func (ManagerConfig) StateFilePath

func (c ManagerConfig) StateFilePath() string

StateFilePath returns the full path to the state file.

type Notification

type Notification struct {
	Delete chan struct{}
	Done   chan struct{}
}

Notification contains channels used to stop the Manager run loop.

type Queue

type Queue[T any] interface {
	ID() string
	// Enqueue the current state in memory.
	Enqueue(state T)
}

Queue handles queued state changes.

type Range

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

Range represents a pair of offsets [start, end).

func NewRange

func NewRange(start, end uint64) Range

func (Range) Contains

func (r Range) Contains(other Range) bool

Contains returns true if the range completely contains the other range.

func (Range) EndOffset

func (r Range) EndOffset() uint64

EndOffset returns the exclusive end of the range.

func (Range) EndOffsetInt64

func (r Range) EndOffsetInt64() int64

EndOffsetInt64 is the int64 version of EndOffset. If end exceeds math.MaxInt64, returns 0.

func (Range) IsEndOffsetUnbounded

func (r Range) IsEndOffsetUnbounded() bool

IsEndOffsetUnbounded returns true if the end offset is the unbounded representation (i.e. math.MaxUint64).

func (Range) IsValid

func (r Range) IsValid() bool

IsValid returns true if the range is ordered (i.e. start < end).

func (Range) MarshalText

func (r Range) MarshalText() ([]byte, error)

MarshalText serializes the range into "start-end". If the end is unbounded, the format is "start-".

func (*Range) Set

func (r *Range) Set(start, end uint64)

Set updates the start and end offsets of the range. If the new start is before the current start, it indicates file truncation and increments the sequence number.

func (*Range) SetInt64

func (r *Range) SetInt64(start, end int64)

SetInt64 is the int64 version of Set. If start or end are negative, the range is not updated.

func (*Range) Shift

func (r *Range) Shift(newEnd uint64)

Shift moves the previous end to the start and sets the new end. If the new end is before the previous one, it resets the range to [0, newEnd) and increments the sequence number.

func (*Range) ShiftInt64

func (r *Range) ShiftInt64(newEnd int64)

ShiftInt64 is the int64 version of Shift. If newEnd is negative, the range is not updated.

func (Range) StartOffset

func (r Range) StartOffset() uint64

StartOffset returns the inclusive start of the range.

func (Range) StartOffsetInt64

func (r Range) StartOffsetInt64() int64

StartOffsetInt64 is the int64 version of StartOffset. If start exceeds math.MaxInt64, returns 0.

func (Range) String

func (r Range) String() string

String returns a string representation of the range "start-end". If the end is unbounded, returns "start-".

func (*Range) UnmarshalText

func (r *Range) UnmarshalText(text []byte) error

UnmarshalText supports unmarshalling both the "start-end" and "start-" formats.

type RangeList

type RangeList []Range

RangeList is a slice of Range values.

func InvertRanges

func InvertRanges(sorted RangeList) RangeList

InvertRanges returns all the gaps between the ranges in sorted order. Assumes that the passed in RangeList is sorted.

func (RangeList) Last

func (r RangeList) Last() Range

Last returns the last Range in the slice. If empty, returns the zero-value Range.

func (RangeList) OnlyUseMaxOffset

func (r RangeList) OnlyUseMaxOffset() bool

OnlyUseMaxOffset returns true if the RangeList is either empty or only contains a single Range that starts at 0. The intention of this is to maintain backwards compatibility with state files that only store the offset.

func (RangeList) String

func (r RangeList) String() string

String returns a string representation of all stored ranges (e.g. "[0-5,10-15]").

type RangeQueueBatcher

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

RangeQueueBatcher is meant for merging continuous ranges before sending them to the FileRangeQueue.

func NewRangeQueueBatcher

func NewRangeQueueBatcher(queue FileRangeQueue) *RangeQueueBatcher

func (*RangeQueueBatcher) Done

func (b *RangeQueueBatcher) Done()

Done enqueues the built range (if valid) on the queue.

func (*RangeQueueBatcher) Merge

func (b *RangeQueueBatcher) Merge(r Range)

Merge stores the min start and max end between the current state and the provided range.

type RangeTracker

type RangeTracker interface {
	encoding.TextMarshaler
	encoding.TextUnmarshaler
	// Insert a Range into the store. Returns false if the Range is already contained by another Range in the store or is
	// invalid.
	Insert(Range) bool
	// Ranges returns all ranges in sorted order.
	Ranges() RangeList
	// Len returns the number of ranges in the store.
	Len() int
	// Clear all stored ranges.
	Clear()
}

RangeTracker manages a collection of ranges. Handles insertion, retrieval, and serialization.

Jump to

Keyboard shortcuts

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