wal

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package wal is the write-ahead log: CRC-framed records appended to numbered segment files, replayed in order to reconstruct the in-memory index after a crash. A record frames a length, a body (type + payload) and a CRC32C of the body; on replay a truncated or torn final record ends the log cleanly (crash recovery), while a complete record with a bad CRC is surfaced as corruption. Append-style and group-commit-friendly.

Index

Constants

View Source
const (

	// DefaultMaxSegmentBytes is the size at which the writer rotates to a new segment.
	DefaultMaxSegmentBytes = 32 << 20 // 32 MiB
)

Variables

View Source
var ErrCorrupt = errors.New("wal: corrupt record")

ErrCorrupt is returned when a complete record fails its CRC check.

Functions

func Replay

func Replay(data []byte, h Handlers) error

Replay reads every complete record from data and dispatches it to h. It stops cleanly at end-of-log or a torn final record (returning nil), and returns an ErrCorrupt-wrapping error on a complete record whose CRC fails. Records already applied before the stopping point are kept.

func ReplayDir

func ReplayDir(dir string, h Handlers) error

ReplayDir replays every segment in dir, in ascending segment order, dispatching each record to h. A torn final record in the last segment ends replay cleanly.

Types

type Handlers

type Handlers struct {
	OnSeries  func(id signal.SeriesID, s signal.Series) error
	OnSamples func(id signal.SeriesID, ts []int64, values []float64) error
}

Handlers receives decoded records during Replay. Unset handlers skip their record type.

type SegmentWriter

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

SegmentWriter appends WAL records to numbered segment files in a directory, rotating to a fresh segment once the current one reaches the size limit. Replaying the directory in order (ReplayDir) reconstructs the logged state. Not safe for concurrent use.

func Create

func Create(dir string, maxBytes int) (*SegmentWriter, error)

Create opens (creating the directory if needed) a new segmented WAL writer. A non-positive maxBytes uses DefaultMaxSegmentBytes.

func (*SegmentWriter) Close

func (sw *SegmentWriter) Close() error

Close syncs and closes the current segment.

func (*SegmentWriter) Sync

func (sw *SegmentWriter) Sync() error

Sync flushes the current segment to stable storage.

func (*SegmentWriter) Write

func (sw *SegmentWriter) Write(p []byte) (int, error)

Write implements io.Writer, appending to the current segment and tracking its size so the writer knows when to rotate.

func (*SegmentWriter) WriteSamples

func (sw *SegmentWriter) WriteSamples(id signal.SeriesID, ts []int64, values []float64) error

WriteSamples logs a run of samples for one series, rotating first if the current segment is full.

func (*SegmentWriter) WriteSeries

func (sw *SegmentWriter) WriteSeries(id signal.SeriesID, s signal.Series) error

WriteSeries logs a series registration, rotating to a new segment first if the current one is full.

type Writer

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

Writer appends framed records to an io.Writer (typically a segment file). It reuses internal buffers, so it is not safe for concurrent use.

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter returns a Writer over w.

func (*Writer) WriteSamples

func (wr *Writer) WriteSamples(id signal.SeriesID, ts []int64, values []float64) error

WriteSamples logs a run of samples for one series: its signal.SeriesID then the (timestamp, value) pairs. ts and values must have the same length.

func (*Writer) WriteSeries

func (wr *Writer) WriteSeries(id signal.SeriesID, s signal.Series) error

WriteSeries logs a series registration: its signal.SeriesID and full identity (Resource + Scope + data-point attributes). Replaying it reconstructs the series.

Jump to

Keyboard shortcuts

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