Documentation
¶
Overview ¶
Package pipedatasets keeps pipeline datasets: named snapshots of a stream that a pipeline materialised into DuckDB with the `builtin:datatug/dataset` sink, to be read back, joined and analysed with SQL.
The word "dataset" already names a recordset definition of a DataTug project (`datatug dataset`), so the DuckDB materialisations are always called pipeline datasets, in code, in the CLI and in the API.
A pipeline dataset is an explicit cache, never a transparent proxy: it holds what one run wrote and changes only when a run writes it again. Every dataset is one DuckDB file `<name>.duckdb` with the single table `data`, and a provenance sidecar `<name>.json` that says which pipeline, node and run wrote it, from which queries, when, and under which pipeline definition.
The files hold rows a policy admitted for one person, so they live outside every Git repository, below ~/.datatug/pipeline-datasets/<project-key>/: directories are 0700 and files 0600, a root inside a Git repository or behind a link is refused, a dataset is written beside its final name and renamed into place, and one writer at a time holds the dataset's lock while readers keep reading the file they opened.
DuckDB needs cgo. A build without it keeps every listing and provenance function and answers ErrRequiresCGO where rows would be written or read.
Index ¶
- Constants
- Variables
- func Available() bool
- func DefaultBase() (string, error)
- func ProjectKey(projectID, projectDir string) string
- func TableIdentifier(name string) string
- func ValidName(name string) bool
- type Column
- type Freshness
- type Provenance
- type ReadOptions
- type Source
- type Store
- func (s *Store) Delete(name string) error
- func (s *Store) Dir() string
- func (s *Store) Get(name string) (Provenance, error)
- func (s *Store) List() ([]Provenance, error)
- func (s *Store) NewWriter(name string) (*Writer, error)
- func (s *Store) Query(ctx context.Context, sqlText string, names []string, ...) error
- func (s *Store) Read(ctx context.Context, name string, o ReadOptions, emit func(row []byte) error) error
- type Writer
- type WrittenBy
Constants ¶
const ( // DefaultDir is the root of every project's pipeline datasets, below the // user's home directory. DefaultDir = ".datatug/pipeline-datasets" // DefaultMaxBytes bounds the staged rows of one dataset. DefaultMaxBytes = 2 << 30 // MaxDatasets bounds the datasets of one project. MaxDatasets = 500 // TableName is the table of a dataset file. TableName = "data" // ValueColumn is the column that holds an item that is not a JSON object. ValueColumn = "value" )
const ( StalePipelineChanged = "pipeline-changed" StalePipelineMissing = "pipeline-missing" )
Stale reasons.
const ProvenanceVersion = 1
ProvenanceVersion is the format marker of a sidecar.
Variables ¶
var ( // ErrRequiresCGO is returned where DuckDB is needed in a build without cgo. ErrRequiresCGO = errors.New("pipeline datasets need DuckDB, which requires a cgo build of datatug (CGO_ENABLED=1)") // ErrNotFound is returned for a dataset that is not there. ErrNotFound = errors.New("pipeline dataset not found") // ErrBusy is returned when another run is writing the dataset. ErrBusy = errors.New("pipeline dataset is being written by another run") // ErrTooLarge is returned when a dataset would pass the store's size bound. ErrTooLarge = errors.New("pipeline dataset is larger than the store allows") // ErrInsideGit is returned for a root inside a Git repository. ErrInsideGit = errors.New("pipeline datasets must be stored outside Git repositories") // ErrUnsafeRoot is returned for a root that is a link, is not a directory, // or cannot be made private. ErrUnsafeRoot = errors.New("pipeline datasets root is not a private directory") )
Errors a caller can test for.
Functions ¶
func Available ¶
func Available() bool
Available reports whether this build can write and read pipeline datasets.
func ProjectKey ¶
ProjectKey names the folder of a project's datasets: a readable form of the project id and a digest of the project's folder, so two checkouts of one project keep separate caches.
func TableIdentifier ¶
TableIdentifier is the SQL name of a dataset in a query over datasets: the name with every `-` replaced by `_`.
Types ¶
type Freshness ¶
type Freshness struct {
AgeSeconds int64 `json:"ageSeconds"`
// Stale is true when the pipeline that wrote the dataset no longer has
// the definition it had then, or is gone.
Stale bool `json:"stale"`
Reason string `json:"reason,omitempty"`
}
Freshness says how old a dataset is and whether its pipeline changed since.
func FreshnessOf ¶
func FreshnessOf(p Provenance, currentHash string) Freshness
FreshnessOf judges p against the current digest of its pipeline's definition; currentHash is "" when the pipeline is gone.
type Provenance ¶
type Provenance struct {
Version int `json:"version"`
Name string `json:"name"`
// PipelineID, Node and RunID say which node of which run wrote the dataset.
PipelineID string `json:"pipelineId"`
Node string `json:"node"`
RunID string `json:"runId"`
// PipelineHash is the digest of the pipeline definition the run executed;
// the dataset is stale when the definition no longer has it.
PipelineHash string `json:"pipelineHash,omitempty"`
StartedAt time.Time `json:"startedAt"`
FinishedAt time.Time `json:"finishedAt"`
Rows int64 `json:"rows"`
// Bytes is the size of the rows as NDJSON; FileBytes of the DuckDB file.
Bytes int64 `json:"bytes"`
FileBytes int64 `json:"fileBytes"`
Columns []Column `json:"columns"`
// Wrapped is true when the items were not JSON objects and each was stored
// as {"value": item}.
Wrapped bool `json:"wrapped,omitempty"`
Sources []Source `json:"sources"`
// WrittenBy is absent in a sidecar written before sessions were recorded.
WrittenBy *WrittenBy `json:"writtenBy,omitempty"`
}
Provenance is the sidecar of a dataset: who wrote it, from what, and when.
type ReadOptions ¶
type ReadOptions struct {
// Limit is the most rows to return; 0 is all of them.
Limit int
// Offset skips rows.
Offset int
}
ReadOptions bound a read.
type Source ¶
type Source struct {
// Node is the pipeline node that ran the query.
Node string `json:"node"`
// Kind is `query` (a saved query), `sql` or `tugql` (inline text).
Kind string `json:"kind"`
QueryID string `json:"queryId,omitempty"`
Environment string `json:"environment,omitempty"`
// Connection is the id of the project database the query ran against.
Connection string `json:"connection,omitempty"`
// TextHash is the SHA-256 of an inline query text.
TextHash string `json:"textHash,omitempty"`
}
Source is one query a dataset was read from. It names the query and the connection by their project ids and never holds a credential or a URL.
type Store ¶
type Store struct {
// MaxBytes bounds the staged rows of one dataset (DefaultMaxBytes when 0).
MaxBytes int64
// contains filtered or unexported fields
}
Store is the pipeline datasets of one project.
func Open ¶
Open checks the folder base/projectKey without creating it: it must not lie inside a Git working tree or a bare repository, and must not be a link or anything but a directory (sessionstore.ResolvePrivateDirectory).
func OpenDefault ¶
OpenDefault opens the store of a project below DefaultBase.
func (*Store) Delete ¶
Delete removes a dataset. A dataset that is not there is ErrNotFound; one that is being written is ErrBusy.
func (*Store) Get ¶
func (s *Store) Get(name string) (Provenance, error)
Get returns the provenance of a dataset. A dataset exists when both its sidecar and its DuckDB file do.
func (*Store) List ¶
func (s *Store) List() ([]Provenance, error)
List returns the provenance of every dataset, sorted by name. A sidecar that cannot be read is skipped: one bad file does not hide the others.
func (*Store) NewWriter ¶
NewWriter takes the dataset's lock and opens its stage. It fails with ErrRequiresCGO before anything is written in a build without DuckDB, and with ErrBusy while another run writes the same dataset.
func (*Store) Query ¶
func (s *Store) Query(ctx context.Context, sqlText string, names []string, emit func(row []byte) error) error
Query runs one SELECT over the named datasets, each visible as a table named TableIdentifier(name), and calls emit with every result row as the bytes of one JSON object. The query runs in a DuckDB session that has the datasets attached read-only and no other access: no files, no network, no extensions, no configuration changes.
func (*Store) Read ¶
func (s *Store) Read(ctx context.Context, name string, o ReadOptions, emit func(row []byte) error) error
Read calls emit with every row of a dataset as the bytes of one JSON object, in stored order. A dataset whose items were wrapped is returned unwrapped, as the items it was given.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer stages the rows of one dataset. Exactly one of Commit and Abort ends it; until Commit the stored dataset, if any, is untouched.
func (*Writer) Abort ¶
func (w *Writer) Abort()
Abort drops the stage and releases the lock. It is safe after Commit.
func (*Writer) Commit ¶
func (w *Writer) Commit(ctx context.Context, p Provenance) (Provenance, error)
Commit builds the DuckDB file from the stage, writes the sidecar and puts both in place. p carries what the caller knows (pipeline, node, run, sources, pipeline hash); the store fills in the rest.
type WrittenBy ¶
type WrittenBy struct {
// Principal is the id of the principal, for people to read; empty when
// the session had none.
Principal string `json:"principal,omitempty"`
// Context identifies the principal together with the exact policies that
// decided what it could read. It holds no policy text and no path.
Context string `json:"context"`
}
WrittenBy is the session a dataset was written under. A dataset holds the rows that session was allowed to read, so a host gives its rows back only to the same session (or to the local owner of the project).