rowsync

package
v0.4.6 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: MPL-2.0-no-copyleft-exception Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package rowsync defines common row storage adapter interfaces and types.

Every durable collection flows through one pipeline: fetch source and destination rows, build one rowsync.Plan with a rowsync.Planner, then apply that plan to a destination. Destinations never recompute changes: they apply Plan.Actions (complete rows in Before/After) and, for PostgreSQL, commit base-table and log changes in one transaction.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action[T any] struct {
	Kind          ActionKind
	Key           string
	Before        *T
	After         *T
	ChangedFields []string
}

Action is one deterministic change to apply.

type ActionKind

type ActionKind uint8

ActionKind identifies one planned destination action.

const (
	InsertAction ActionKind = iota
	UpdateAction
	DeleteAction
)

type ActionSummary

type ActionSummary struct {
	Kind          ActionKind
	KeyDigest     string
	ChangedFields []string
}

ActionSummary is safe for audit output. It never contains the source key.

type AuditRecord

type AuditRecord struct {
	Kind          ActionKind
	KeyDigest     string
	ChangedFields []string
	BeforeDigest  string // sha256 of the complete previous row; "" for inserts
	AfterDigest   string // sha256 of the complete new row; "" for deletes
}

AuditRecord maps one plan action to safe audit metadata: action kind, key digest, changed fields, and digests of the complete old and new rows. Raw row data (which may hold secrets, tokens, or authorization codes) is never exposed; consumers needing full rows hold the plan itself.

type Counts

type Counts struct {
	Inserted int
	Updated  int
	Deleted  int
}

Counts summarizes planned actions.

type Fetcher

type Fetcher[T any] interface {
	Init(context.Context) ([]Message, error)
	Fetch(context.Context) ([]T, error)
}

Fetcher reads records from a source.

type InvalidReporter

type InvalidReporter interface {
	InvalidCount() int
}

InvalidReporter reports the count of invalid rows in a source.

type InvalidRowReporter

type InvalidRowReporter interface {
	InvalidRows() []int
}

InvalidRowReporter reports the row numbers of invalid rows in a source.

type Merge

type Merge[T any] func(previous, current []T) ([]T, error)

Merge combines a partial source with existing state. Merged pipelines are insert-update only: destination rows the source lacks survive.

type Message

type Message struct {
	String string `json:"string"`
	Type   string `json:"type"`
}

Message is an informational message returned during Init.

type Mode

type Mode uint8

Mode controls whether a plan may delete destination rows.

const (
	// UpdateMode inserts and updates rows but never deletes destination rows.
	UpdateMode Mode = iota
	// SyncMode makes the source authoritative and includes deletes.
	SyncMode
)

type Plan

type Plan[T any] struct {
	Mode     Mode
	Previous []T
	Current  []T
	Final    []T
	Actions  []Action[T]
}

Plan is the complete result of comparing source and destination rows.

func (Plan[T]) AuditRecords

func (p Plan[T]) AuditRecords() []AuditRecord

AuditRecords returns one safe audit record per planned action, in plan order. Row digests are stable for value-type rows within a build.

func (Plan[T]) Count

func (p Plan[T]) Count() Counts

Count returns the number of actions by kind.

func (Plan[T]) RedactedActions

func (p Plan[T]) RedactedActions() []ActionSummary

RedactedActions returns deterministic action metadata without exposing keys.

type PlanResult

type PlanResult[T any] struct {
	Source []T
	Plan   Plan[T]
}

PlanResult contains source rows and the unapplied plan.

func BuildMergedPlan

func BuildMergedPlan[T any](
	ctx context.Context,
	input Fetcher[T],
	output Fetcher[T],
	planner Planner[T],
	merge Merge[T],
) (PlanResult[T], error)

BuildMergedPlan is BuildPlan with an explicit merge step for partial input.

func BuildPlan

func BuildPlan[T any](ctx context.Context, input Fetcher[T], output Fetcher[T], planner Planner[T]) (PlanResult[T], error)

BuildPlan fetches source and destination rows and builds one plan. It does not apply the plan.

type Planner

type Planner[T any] struct {
	Mode          Mode
	Key           func(T) string
	Equal         func(T, T) bool
	ChangedFields func(T, T) []string
}

Planner compares rows by a stable key.

func (Planner[T]) Build

func (p Planner[T]) Build(previous, current []T) (Plan[T], error)

Build creates a deterministic action plan. Empty or duplicate keys fail.

type Result

type Result[T any] struct {
	Source      []T
	Plan        Plan[T]
	Invalid     int
	InvalidRows []int
}

Result reports the outcome of a fetch → plan → apply run.

func RunMergedStore

func RunMergedStore[T any](
	ctx context.Context,
	input Fetcher[T],
	output Updater[T],
	merge Merge[T],
	planner Planner[T],
) (Result[T], error)

RunMergedStore is RunStore with an explicit merge step for partial sources. The planner is forced to UpdateMode: a partial (merged) source is never authoritative, so destination rows the source lacks survive.

func RunStore

func RunStore[T any](
	ctx context.Context,
	input Fetcher[T],
	output Updater[T],
	planner Planner[T],
) (Result[T], error)

RunStore executes the fetch → plan → apply pipeline. The planner's Mode is the explicit operation choice: UpdateMode is insert-update only, SyncMode makes the source authoritative and deletes destination rows the source lacks. An empty source is rejected before planning unless the destination is also empty, in which case the run is a no-op.

type Updater

type Updater[T any] interface {
	Fetcher[T]
	Update(context.Context, Plan[T]) error
}

Updater reads and writes records to a destination. Update applies a plan built elsewhere; it must not recompute changes or refetch rows it already has (Plan.Before carries the complete previous rows for deletes).

Jump to

Keyboard shortcuts

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