db

package
v0.0.0-...-6d79af0 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrBufferFull = errors.New("batchwriter: ingestion channel is full, record dropped")

ErrBufferFull is returned by Enqueue when the ingestion channel is at capacity.

Functions

This section is empty.

Types

type BatchInserter

type BatchInserter interface {
	CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error)
}

BatchInserter defines the interface for executing bulk CopyFrom operations.

type BatchWriter

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

BatchWriter buffers incoming TelemetryRecords and periodically flushes them to TimescaleDB via pgx.CopyFrom.

func NewBatchWriter

func NewBatchWriter(inserter BatchInserter, cfg config.IngestionConfig, tableName string) *BatchWriter

NewBatchWriter creates a new BatchWriter instance from validated IngestionConfig and target table name.

func (*BatchWriter) DroppedCount

func (w *BatchWriter) DroppedCount() int64

DroppedCount returns the number of records dropped due to a full ingestion channel.

func (*BatchWriter) Enqueue

func (w *BatchWriter) Enqueue(record TelemetryRecord) error

Enqueue attempts a non-blocking send of a TelemetryRecord into the ingestion channel. If the channel is at capacity, the record is dropped and ErrBufferFull is returned. Callers should log or count dropped records at their discretion.

func (*BatchWriter) Flush

func (w *BatchWriter) Flush(ctx context.Context) error

Flush writes the current in-memory buffer to TimescaleDB using pgx.CopyFrom.

func (*BatchWriter) Start

func (w *BatchWriter) Start(ctx context.Context)

Start launches the background worker that processes incoming records and periodic timer flushes.

func (*BatchWriter) Stats

func (w *BatchWriter) Stats() (int64, int64)

Stats returns total inserted count, total batch count, and total dropped record count.

func (*BatchWriter) Stop

func (w *BatchWriter) Stop()

Stop signals the writer to flush pending records and wait for worker completion.

type DB

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

DB wraps the TimescaleDB connection pool.

func NewDB

func NewDB(ctx context.Context, cfg config.DatabaseConfig) (*DB, error)

NewDB creates and tests a connection pool to TimescaleDB/PostgreSQL using validated configuration parameters.

func (*DB) AsBatchInserter

func (d *DB) AsBatchInserter() BatchInserter

AsBatchInserter returns the underlying pool as a BatchInserter interface, allowing the BatchWriter to use it without exposing the full pgxpool.Pool.

func (*DB) Close

func (d *DB) Close()

Close gracefully closes the connection pool.

func (*DB) RunMigrations

func (d *DB) RunMigrations(ctx context.Context) error

RunMigrations applies all pending migrations using goose. It opens a database/sql connection via pgx stdlib adapter (required by goose), runs the embedded migrations, and closes the temporary connection.

type TelemetryRecord

type TelemetryRecord struct {
	Time            time.Time
	PhysicalAssetID string
	MetricName      string
	Value           float64
	Quality         string
}

TelemetryRecord represents a single flattened sensor reading to be inserted into TimescaleDB.

Jump to

Keyboard shortcuts

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