store

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 4 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 store is the interface every engine implements and the types its callers speak. The engines live in subpackages (clickhouse, opensearch), the composite that serves one logical store from two is in composite, and factory builds any of them from configuration.

Where the engines differ

A filter is meant to mean the same on any engine, and almost all of them do. These do not, and a caller that relies on the difference has to know:

Text matching. OpContains, OpNotContains, OpContainsAny and OpNotContainsAny are a case-insensitive substring on ClickHouse. On OpenSearch they are a "match" query, which compares analysed words: "deni" finds nothing in "denied" there, and word order and stemming play a part. OpSearch reads the raw-text column on ClickHouse and the field named in opensearch.Config.TextFields on OpenSearch, with every word required.

Anchored matching. OpStartsWith and OpEndsWith are case-insensitive on both, but OpenSearch evaluates them against the indexed terms, so the field has to be a keyword. On an analysed text field they match individual words, not the whole value.

Retention. ClickHouse can move data to a cold volume after Retention.ColdAfter. OpenSearch cannot: SetRetention changes Keep only, and asking it to add or remove a cold tier returns ErrUnsupported. Reading or setting retention there also needs an ISM policy id in the configuration.

Raw queries. RawQuerier takes the engine's own query text, so what runs on one does not run on the other. FetchQuery is the agnostic form: one Go signature, each engine's own SQL dialect, and the tenant predicate is the caller's — the scope must be AllTenants.

Neither engine's behaviour has been changed to hide these: making OpenSearch answer a substring would need wildcard queries over keyword subfields, which depends on how an installation maps its indices.

Index

Constants

View Source
const AllTenants = "*"

AllTenants is the only way to read across tenants, so that doing so is explicit and greppable.

Variables

View Source
var (
	ErrNoTenant    = errors.New("store: scope has no tenant")
	ErrUnsupported = errors.New("store: operation not supported by this driver")
	ErrNotFound    = errors.New("store: record not found")
)

Functions

This section is empty.

Types

type Admin

type Admin interface {
	Usage(ctx context.Context) ([]DatasetUsage, error)
	Retention(ctx context.Context, dataset Dataset) (Retention, error)
	SetRetention(ctx context.Context, dataset Dataset, r Retention) error
	Health(ctx context.Context) (Health, error)

	// Drop deletes every record matching the scope, and only those: it is
	// bounded by the scope's tenant like any other operation.
	Drop(ctx context.Context, s Scope) (int64, error)
}

type Bucket

type Bucket struct {
	Key   string
	Count int64
}

type BulkWriter

type BulkWriter interface {
	Write(s Scope, doc []byte) error
	Flush(ctx context.Context) error
	Close(ctx context.Context) error
}

BulkWriter batches records. Write reports failures instead of dropping them.

type CalendarUnit

type CalendarUnit string
const (
	CalendarNone  CalendarUnit = ""
	CalendarHour  CalendarUnit = "hour"
	CalendarDay   CalendarUnit = "day"
	CalendarWeek  CalendarUnit = "week"
	CalendarMonth CalendarUnit = "month"
)

type Dataset

type Dataset string

Dataset names a family of records. The values are defined by the application, not here: what datasets exist and how they map to physical storage is product knowledge, and a driver receives that mapping as configuration.

type DatasetUsage

type DatasetUsage struct {
	Dataset   Dataset
	DataType  string
	Documents int64
	Bytes     int64
	Oldest    time.Time
	Newest    time.Time
}

type Field

type Field struct {
	Name       string
	Type       string
	Filterable bool // supports exact-match filters
	Searchable bool // supports text matching
	Conflict   bool // type differs across the underlying storage
}

Field describes one queryable field of a dataset.

type Filter

type Filter struct {
	Field string
	Op    Op
	Value any
}

Filter is one predicate. Filters in a slice are ANDed; there is no OR and no nesting, so that a driver never has to implement a boolean evaluator.

type Group

type Group struct {
	Field    string
	Key      string
	Count    int64
	Children []Group
	Hits     []json.RawMessage
}

type GroupOpts

type GroupOpts struct {
	Limit   int
	TopHits int
	SortBy  string
	Order   SortOrder
}

type Health

type Health struct {
	Status      HealthStatus
	Message     string
	DiskUsedPct float64
}

type HealthStatus

type HealthStatus string
const (
	HealthOK          HealthStatus = "ok"
	HealthDegraded    HealthStatus = "degraded"
	HealthUnavailable HealthStatus = "unavailable"
)

type Interval

type Interval struct {
	Duration time.Duration
	Calendar CalendarUnit
}

Interval buckets a timeline. Calendar takes precedence over Duration.

type Op

type Op string
const (
	OpEq          Op = "eq"
	OpNotEq       Op = "not_eq"
	OpIn          Op = "in"
	OpNotIn       Op = "not_in"
	OpGt          Op = "gt"
	OpGte         Op = "gte"
	OpLt          Op = "lt"
	OpLte         Op = "lte"
	OpBetween     Op = "between"  // Value is [2]any{lo, hi}
	OpContains    Op = "contains" // substring on ClickHouse, word match on OpenSearch: see the package docs
	OpNotContains Op = "not_contains"
	OpExists      Op = "exists" // Value ignored

	// Anchored matching. A prefix or suffix is not the same question as
	// "contains", and answering one as the other returns rows the caller did
	// not ask for.
	OpStartsWith    Op = "starts_with"
	OpNotStartsWith Op = "not_starts_with"
	OpEndsWith      Op = "ends_with"
	OpNotEndsWith   Op = "not_ends_with"

	// The negations of the two above them.
	OpNotExists  Op = "not_exists" // Value ignored
	OpNotBetween Op = "not_between"

	// OpSearch matches anywhere in the record rather than in a field, which is
	// what a search box asks. Field is ignored; a dataset that has no text to
	// search is an error rather than a query that matches nothing.
	OpSearch    Op = "search"
	OpNotSearch Op = "not_search"

	// Value is a list: matches if any of them is contained.
	OpContainsAny    Op = "contains_any"
	OpNotContainsAny Op = "not_contains_any"
)

type Page

type Page struct {
	Offset int
	Limit  int
	SortBy string
	Order  SortOrder
}

type Point

type Point struct {
	At    time.Time
	Count int64
}

type RawQuerier

type RawQuerier interface {
	Dialect() string
	RawQuery(ctx context.Context, s Scope, query string) ([]map[string]any, error)
}

RawQuerier carries engine-specific query text. It stays outside Store because anything reaching through it is unportable by construction.

type Reader

type Reader interface {
	Count(ctx context.Context, s Scope, filters []Filter) (int64, error)

	// CountInWindow counts over a trailing window ending at Scope.To, or at now.
	CountInWindow(ctx context.Context, s Scope, filters []Filter, window time.Duration) (int64, error)

	FetchByID(ctx context.Context, s Scope, id string) (json.RawMessage, error)
	FetchN(ctx context.Context, s Scope, filters []Filter, n int) ([]json.RawMessage, error)
	FetchPage(ctx context.Context, s Scope, filters []Filter, page Page) ([]json.RawMessage, int64, error)
	TopValues(ctx context.Context, s Scope, field string, filters []Filter, n int) ([]Bucket, error)
	Timeline(ctx context.Context, s Scope, filters []Filter, interval Interval) ([]Point, error)
	TimelineByField(ctx context.Context, s Scope, field string, filters []Filter, interval Interval, n int) ([]Series, error)
	GroupBy(ctx context.Context, s Scope, fields []string, filters []Filter, opts GroupOpts) ([]Group, error)

	//make sql queries over the storage
	FetchQuery(ctx context.Context, s Scope, sql string) ([]json.RawMessage, error)

	// DescribeFields lists the queryable fields of a dataset.
	DescribeFields(ctx context.Context, s Scope) ([]Field, error)
}

type Retention

type Retention struct {
	Keep      time.Duration
	ColdAfter time.Duration
}

Retention is how long data is kept. ColdAfter is only honoured by engines that can tier; OpenSearch rejects a change to it with ErrUnsupported.

func (Retention) Tiered

func (r Retention) Tiered() bool

type Scope

type Scope struct {
	Tenant   string
	Dataset  Dataset
	DataType string
	From, To time.Time
}

Scope narrows an operation. Tenant is mandatory and drivers inject it themselves: empty is an error, not "every tenant".

type Series

type Series struct {
	Key    string
	Points []Point
}

type SortOrder

type SortOrder string
const (
	Asc  SortOrder = "asc"
	Desc SortOrder = "desc"
)

type Store

type Store interface {
	Reader
	Writer
	Admin
}

type Writer

type Writer interface {
	// Insert generates an id when one is not given; an explicit id means upsert.
	Insert(ctx context.Context, s Scope, id string, doc any) error

	BulkWriter(dataset Dataset) (BulkWriter, error)
	UpdateWhere(ctx context.Context, s Scope, filters []Filter, patch map[string]any) (int64, error)

	// Flush makes prior writes visible to reads. Engines that are already
	// read-your-writes may implement it as a no-op.
	Flush(ctx context.Context, dataset Dataset) error
}

Directories

Path Synopsis
Package cache is a thin, nil-safe wrapper around a shared Redis/Valkey instance.
Package cache is a thin, nil-safe wrapper around a shared Redis/Valkey instance.
Package clickhouse implements store.Store over ClickHouse.
Package clickhouse implements store.Store over ClickHouse.
Package composite serves one logical store from two: a primary that takes every write and holds everything from Cutover on, and a read-only secondary store that holds what came before.
Package composite serves one logical store from two: a primary that takes every write and holds everything from Cutover on, and a read-only secondary store that holds what came before.
Package factory builds a store from configuration, so that every process of an installation — the event processor, the backend, the plugins — reaches its data the same way and the choice of engine is a setting, not code.
Package factory builds a store from configuration, so that every process of an installation — the event processor, the backend, the plugins — reaches its data the same way and the choice of engine is a setting, not code.

Jump to

Keyboard shortcuts

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