store

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 15 Imported by: 1 module, 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 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

View Source
const Format = 1

Format is the version of the directory layout and of the manifest.

View Source
const ManifestFile = "store.json"

ManifestFile is the name of the manifest inside a store.

Variables

View Source
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.

View Source
var ErrNoStore = errors.New("no store")

ErrNoStore means the directory holds no store yet.

View Source
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

func DefaultDir(owner string) (string, error)

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.

func Key

func Key(source, name string) string

Key is the manifest key and the directory of a scope.

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 Open

func Open(dir string) (*Store, error)

Open opens a store for reading. It fails with ErrNoStore when the directory holds none.

func OpenWrite

func OpenWrite(dir, owner string, now time.Time) (*Store, error)

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) Close

func (s *Store) Close() error

Close releases the write lock. It does nothing on a store opened for reading.

func (*Store) Count

func (s *Store) Count(table, source, scope string) (int64, error)

Count returns how many rows a table holds for a scope, by table name.

func (*Store) CreatedAt

func (s *Store) CreatedAt() time.Time

CreatedAt is when the store was first written.

func (*Store) DropScope

func (s *Store) DropScope(source, name string) error

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) Owner

func (s *Store) Owner() string

Owner is the owner the store holds.

func (*Store) PutScope

func (s *Store) PutScope(sc Scope) error

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.

func (*Store) Scope

func (s *Store) Scope(source, name string) (Scope, bool)

Scope returns one scope, and whether the store knows it.

func (*Store) Scopes

func (s *Store) Scopes(source string) []Scope

Scopes lists the scopes of a source, by name. An empty source lists all.

func (*Store) Size

func (s *Store) Size() (int64, error)

Size returns the bytes the store takes on disk.

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]) Count

func (t Table[T]) Count(s *Store, source, scope string) (int64, error)

Count returns how many rows a scope has. It reads only the file footers.

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]) Parts

func (t Table[T]) Parts(s *Store, source, scope string) ([]string, error)

Parts lists the month files a scope has, oldest first.

func (Table[T]) Put

func (t Table[T]) Put(s *Store, source, scope string, rows []T) (added, changed int, err error)

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.

func (Table[T]) Read

func (t Table[T]) Read(s *Store, source, scope, part string) ([]T, error)

Read returns the rows of one month file, oldest first.

Jump to

Keyboard shortcuts

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