stream

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 4 Imported by: 1

Documentation

Overview

Package stream provides a generic streaming iterator for database results.

Stream[T] is a lazy, pull-based iterator over database results. It holds an open server-side cursor and decodes one row at a time. Supports Go 1.23+ range-over-func via the All method.

The streaming system supports composable transforms (Map, Filter, Reduce, Chunk, Take, ForEach) as zero-allocation pipeline operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ForEach

func ForEach[T any](s *Stream[T], fn func(T) error) error

ForEach processes every element with a callback. Returns first error.

func Reduce

func Reduce[T, A any](s *Stream[T], initial A, fn func(A, T) A) (A, error)

Reduce accumulates a value over the entire stream.

Types

type BatchCursor

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

BatchCursor wraps a base cursor-creating function to implement batched fetch semantics. It fetches batchSize rows at a time from the underlying source, yielding them one at a time.

func NewBatchCursor

func NewBatchCursor(fetchBatch func(offset, limit int) (Cursor, error), batchSize int) *BatchCursor

NewBatchCursor creates a BatchCursor that fetches rows in batches of batchSize. The fetchBatch function is called each time a new batch is needed, with the current offset and the batch size as limit.

func (*BatchCursor) Close

func (bc *BatchCursor) Close() error

Close releases the current batch cursor and marks the BatchCursor as done.

func (*BatchCursor) Columns

func (bc *BatchCursor) Columns() ([]string, error)

Columns returns the column names from the current batch's result set.

func (*BatchCursor) Err

func (bc *BatchCursor) Err() error

Err returns any error encountered during iteration.

func (*BatchCursor) Next

func (bc *BatchCursor) Next() bool

Next advances to the next row, fetching a new batch when the current batch is exhausted. Returns false when no more rows are available.

func (*BatchCursor) Scan

func (bc *BatchCursor) Scan(dest ...any) error

Scan copies the current row's column values into dest.

type ChangeEvent

type ChangeEvent[T any] struct {
	// Operation is the type of change.
	Operation ChangeOp

	// Before is the previous state (nil for inserts; available if driver supports it).
	Before *T

	// After is the new state (nil for deletes).
	After *T

	// Timestamp is the server timestamp of the change.
	Timestamp time.Time

	// ResumeToken is an opaque token for resuming the stream after disconnect.
	ResumeToken any
}

ChangeEvent represents a single change from a CDC stream.

type ChangeOp

type ChangeOp int

ChangeOp represents the type of change in a CDC event.

const (
	ChangeInsert ChangeOp = iota
	ChangeUpdate
	ChangeDelete
	ChangeReplace // MongoDB-specific
)

func (ChangeOp) String

func (op ChangeOp) String() string

String returns a human-readable name for the change operation.

type ChangeSource

type ChangeSource[T any] interface {
	// Next blocks until the next change event is available.
	// Returns false when the stream is closed or an error occurred.
	Next(ctx context.Context) bool

	// Event returns the current change event.
	Event() ChangeEvent[T]

	// Err returns any error encountered.
	Err() error

	// Close stops the change stream.
	Close() error

	// ResumeToken returns the current resume token for reconnection.
	ResumeToken() any
}

ChangeSource is the interface that drivers implement to provide CDC events.

type ChangeStream

type ChangeStream[T any] struct {
	// contains filtered or unexported fields
}

ChangeStream is a long-lived iterator over real-time database changes. It wraps a ChangeSource and supports automatic reconnection with resume tokens.

func NewChangeStream

func NewChangeStream[T any](source ChangeSource[T]) *ChangeStream[T]

NewChangeStream creates a new ChangeStream from a driver-specific source.

func (*ChangeStream[T]) All

func (cs *ChangeStream[T]) All(yield func(ChangeEvent[T], error) bool)

All returns a range-over-func iterator for change events.

func (*ChangeStream[T]) Close

func (cs *ChangeStream[T]) Close() error

Close stops the change stream and releases resources.

func (*ChangeStream[T]) Err

func (cs *ChangeStream[T]) Err() error

Err returns any error encountered.

func (*ChangeStream[T]) Event

func (cs *ChangeStream[T]) Event() ChangeEvent[T]

Event returns the current change event.

func (*ChangeStream[T]) Next

func (cs *ChangeStream[T]) Next(ctx context.Context) bool

Next blocks until the next change event is available.

func (*ChangeStream[T]) ResumeToken

func (cs *ChangeStream[T]) ResumeToken() any

ResumeToken returns the current resume token for reconnection.

type Cursor

type Cursor interface {
	// Next advances the cursor to the next row.
	// Returns false when there are no more rows or an error occurred.
	Next() bool

	// Scan copies the current row's column values into dest.
	Scan(dest ...any) error

	// Columns returns the column names from the result set.
	Columns() ([]string, error)

	// Close releases the cursor and underlying resources.
	Close() error

	// Err returns any error encountered during iteration.
	Err() error
}

Cursor is the interface that database drivers implement to provide streaming row access. Each driver wraps its native cursor type.

type DecodeFunc

type DecodeFunc[T any] func(cursor Cursor) (T, error)

DecodeFunc decodes a cursor row into a value of type T.

type HookRunner

type HookRunner interface {
	RunStreamRowHook(ctx context.Context, qc any, row any) (int, error) // returns Decision as int
}

HookRunner is an optional interface for per-row hook execution. This avoids importing the hook package directly.

type Stream

type Stream[T any] struct {
	// contains filtered or unexported fields
}

Stream is a lazy, pull-based iterator over database results.

func Chunk

func Chunk[T any](s *Stream[T], size int) *Stream[[]T]

Chunk groups elements into fixed-size batches.

func Filter

func Filter[T any](s *Stream[T], fn func(T) bool) *Stream[T]

Filter yields only elements where the predicate returns true.

func Map

func Map[T, U any](s *Stream[T], fn func(T) (U, error)) *Stream[U]

Map transforms each element in the stream.

func New

func New[T any](cursor Cursor, decode DecodeFunc[T]) *Stream[T]

New creates a new Stream from a cursor and a decode function.

func NewWithHooks

func NewWithHooks[T any](cursor Cursor, decode DecodeFunc[T], hooks HookRunner, qc any) *Stream[T]

NewWithHooks creates a new Stream with per-row hook execution.

func Take

func Take[T any](s *Stream[T], n int) *Stream[T]

Take yields at most n elements then marks the stream as done.

func (*Stream[T]) All

func (s *Stream[T]) All(yield func(T, error) bool)

All returns a range-over-func iterator for use with Go 1.23+ for-range.

for user, err := range userStream.All {
    ...
}

func (*Stream[T]) Close

func (s *Stream[T]) Close() error

Close releases the server-side cursor and underlying connection. Always defer this.

func (*Stream[T]) Collect

func (s *Stream[T]) Collect(ctx context.Context) ([]T, error)

Collect drains the stream into a slice.

func (*Stream[T]) Count

func (s *Stream[T]) Count(ctx context.Context) (int64, error)

Count drains the stream counting rows without allocating models.

func (*Stream[T]) Err

func (s *Stream[T]) Err() error

Err returns the first error encountered during iteration.

func (*Stream[T]) Next

func (s *Stream[T]) Next(ctx context.Context) bool

Next advances the cursor and decodes the next row. Returns false when exhausted or on error (check Err()).

If hooks are configured, RunStreamRowHook is called after decoding each row. A Skip decision causes the row to be skipped (continue to next). A Deny decision stops iteration with an error.

func (*Stream[T]) Value

func (s *Stream[T]) Value() T

Value returns the current decoded row. Only valid after Next() returns true.

func (*Stream[T]) WithHooks

func (s *Stream[T]) WithHooks(runner HookRunner, qc any) *Stream[T]

WithHooks sets the hook runner and query context on the stream, returning the stream for chaining. If runner is nil, hooks are disabled.

Jump to

Keyboard shortcuts

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