Documentation
¶
Overview ¶
Package store keeps records on disk between runs, so a report never has to read a line's whole history from its sources again.
A store is a directory. It holds one manifest, store.json, and Parquet files laid out as
<table>/<source>/<scope>/<month>.parquet
where a scope is a repository or a ticket project and the month is the month the record was created in. A record never moves between files, so a sync only rewrites the months it touched.
The files are plain Parquet with the column names of the record layout. Any tool that reads Parquet can query them.
Index ¶
- Constants
- Variables
- func DefaultDir(owner string) (string, error)
- func Key(source, name string) string
- type Manifest
- type Scope
- type Store
- func (s *Store) Close() error
- func (s *Store) Count(table, source, scope string) (int64, error)
- func (s *Store) CreatedAt() time.Time
- func (s *Store) DropScope(source, name string) error
- func (s *Store) Owner() string
- func (s *Store) PutScope(sc Scope) error
- func (s *Store) Scope(source, name string) (Scope, bool)
- func (s *Store) Scopes(source string) []Scope
- func (s *Store) Size() (int64, error)
- type Table
- func (t Table[T]) Count(s *Store, source, scope string) (int64, error)
- func (t Table[T]) Each(s *Store, source, scope string, from, to time.Time, ...) error
- func (t Table[T]) Parts(s *Store, source, scope string) ([]string, error)
- func (t Table[T]) Put(s *Store, source, scope string, rows []T) (added, changed int, err error)
- func (t Table[T]) Read(s *Store, source, scope, part string) ([]T, error)
Constants ¶
const Format = 1
Format is the version of the directory layout and of the manifest.
const ManifestFile = "store.json"
ManifestFile is the name of the manifest inside a store.
Variables ¶
var ( Changes = Table[record.Change]{Name: "changes", ID: func(r *record.Change) string { return r.ID }, At: func(r *record.Change) time.Time { return r.CreatedAt }} CIRuns = Table[record.CIRun]{Name: "ci_runs", ID: func(r *record.CIRun) string { return r.ID }, At: func(r *record.CIRun) time.Time { return r.CreatedAt }} Deploys = Table[record.Deploy]{Name: "deploys", ID: func(r *record.Deploy) string { return r.ID }, At: func(r *record.Deploy) time.Time { return r.CreatedAt }} Tickets = Table[record.Ticket]{Name: "tickets", ID: func(r *record.Ticket) string { return r.ID }, At: func(r *record.Ticket) time.Time { return r.CreatedAt }} Links = Table[record.Link]{Name: "links", ID: func(r *record.Link) string { return r.Kind + " " + r.From + " " + r.To }} )
The tables of the store, one per record kind.
var ErrNoStore = errors.New("no store")
ErrNoStore means the directory holds no store yet.
var TableNames = []string{Changes.Name, CIRuns.Name, Deploys.Name, Tickets.Name, Links.Name}
TableNames lists the tables in the order a status prints them.
Functions ¶
func DefaultDir ¶
DefaultDir is where the store of an owner lives when no directory is given: $FACTORYMETER_STORE, or factorymeter/<owner> under the user's cache directory.
Types ¶
type Manifest ¶
type Manifest struct {
Format int `json:"format"`
Records int `json:"records"`
Owner string `json:"owner"`
CreatedAt time.Time `json:"created_at"`
Scopes map[string]*Scope `json:"scopes"`
}
Manifest describes what a store holds.
type Scope ¶
type Scope struct {
// Source is the system the records came from, such as "github".
Source string `json:"source"`
// Name is the scope's name on the source, such as "owner/repo".
Name string `json:"name"`
Private bool `json:"private"`
// From is the time the store holds this scope's records from.
From time.Time `json:"from"`
// SyncedAt is when the last sync of this scope finished. Zero while the
// first one has not finished.
SyncedAt time.Time `json:"synced_at"`
// Notes are limits the last sync ran into, in plain sentences.
Notes []string `json:"notes"`
// Took is how long the last finished sync of this scope took, in seconds.
// TookFull is set when that sync read the whole window and not only what
// changed. A display uses the two to say how long the next sync may take.
Took float64 `json:"took_seconds,omitempty"`
TookFull bool `json:"took_full,omitempty"`
// State belongs to the source adapter. It holds what the adapter needs to
// read only what changed next time.
State json.RawMessage `json:"state"`
}
Scope is one repository or one ticket project in the store.
type Store ¶
type Store struct {
Dir string
// contains filtered or unexported fields
}
Store is an open store directory.
func OpenWrite ¶
OpenWrite opens the store of an owner for a sync, and creates it when the directory holds none. Only one process can hold a store open for writing. Close releases it.
func (*Store) DropScope ¶
DropScope forgets a scope: its row in the manifest and its files in every table. A source calls it before it reads a scope again from nothing, so no record of the earlier read is left behind. The row goes first. A crash between the two leaves files without a row, and the next read from nothing drops them.
func (*Store) PutScope ¶
PutScope writes a scope to the manifest. The manifest file is replaced in one step, so a crash leaves either the old one or the new one.
type Table ¶
type Table[T any] struct { // Name is the directory of the table. Name string // ID returns the id a row is replaced by. ID func(*T) string // At returns the time that decides which month file a row lives in. It // must never change for a record. Nil keeps every row in one file. At func(*T) time.Time }
Table is one kind of record in the store.
func (Table[T]) Each ¶
func (t Table[T]) Each(s *Store, source, scope string, from, to time.Time, fn func(part string, rows []T) error) error
Each calls fn with the rows of every month file of a scope that can hold a record created between from and to, oldest month first. A zero from or to leaves that side open. Rows outside the range in the first and last month are passed too: the caller filters by the time it cares about.
func (Table[T]) Put ¶
Put adds rows to a scope. A row whose id is already stored replaces the stored one. It reports how many rows were new and how many stored rows changed. A month file whose rows all stay the same is not rewritten, so a sync that finds nothing new leaves the store untouched. Only one caller may write to a scope of a table at a time.