store

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: MIT Imports: 10 Imported by: 0 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 persists the Directory as a single atomically-written JSON document and generates the stable identities its records carry.

Index

Constants

View Source
const (
	// IDAlphabet is Crockford base32 in lowercase: the digits and the letters,
	// less i, l, o and u. Those are the characters people confuse when reading
	// an ID aloud or retyping one while repairing the document by hand.
	IDAlphabet = "0123456789abcdefghjkmnpqrstvwxyz"

	// IDSize is the number of random characters after the prefix. 32^6 is
	// about a billion values, which is ample for a directory of a few hundred
	// people; the store still rejects a collision at load rather than assuming.
	IDSize = 6
)
View Source
const CurrentSchema = 1

CurrentSchema is the document version this binary writes and is the highest it can read.

View Source
const CurrentSettingsSchema = 1

CurrentSettingsSchema is the settings file version this binary writes and is the highest it can read. It is versioned apart from the document, for the reason ADR-0013 keeps the file apart at all: the two change separately.

View Source
const CurrentTrashSchema = 1

CurrentTrashSchema is the Trash file version this binary writes and is the highest it can read. It is versioned apart from the document: ADR-0012 keeps the Trash out of the document precisely so the two can change separately.

View Source
const TrashRetention = 30 * 24 * time.Hour

TrashRetention is the least time a deleted Household is kept (§3).

Variables

View Source
var (
	// ErrSchemaTooNew means the document was written by a newer binary. This is
	// the rollback case: reading it would silently drop fields this binary does
	// not know about, so the store refuses rather than corrupting the Directory.
	ErrSchemaTooNew = errors.New("document schema is newer than this binary understands")

	// ErrSchemaMissing means the document has no schema field, so its version
	// cannot be established.
	ErrSchemaMissing = errors.New("document has no schema version")
)
View Source
var (
	// ErrNotInTrash means a restore named a Household the Trash does not hold.
	ErrNotInTrash = errors.New("household is not in the trash")

	// ErrUnrestorable means a trashed Household needs another — its parent or
	// its Shared Address — that is neither in the Directory nor in the Trash.
	// The retention rule in Purge prevents this; only a hand edit produces it.
	ErrUnrestorable = errors.New("household needs another that is neither in the directory nor the trash")

	// ErrNotInDocument means a deletion named a Household the document does not hold.
	ErrNotInDocument = errors.New("household is not in the directory")

	// ErrBlocked means a deletion named a Household something depends on: a
	// Household beneath it, one sharing its Address, or its being Memorial
	// (rolo.Tree.DeleteBlock).
	ErrBlocked = errors.New("household cannot be deleted while something depends on it")
)
View Source
var ErrMixedSave = errors.New("a save changed the settings together with the directory")

ErrMixedSave means one save changed the settings together with the document or the Trash. No save does that today, and Persist relies on it: with only one file to write, a title change needs no crash-ordering rule (ADR-0013). A future save that needs both must add that rule rather than remove this check.

Functions

func Delete

func Delete(doc *Document, t *Trash, entry TrashEntry) (*Document, *Trash, error)

Delete moves entry's Household out of doc and into t, returning new values and leaving both inputs untouched. It refuses a Household that rolo.Tree.DeleteBlock says something depends on, with ErrBlocked, so no caller can leave a Household pointing at one that is gone.

func NewHouseholdID

func NewHouseholdID() (rolo.HouseholdID, error)

NewHouseholdID generates a stable identity for a Household.

func NewPersonID

func NewPersonID() (rolo.PersonID, error)

NewPersonID generates a stable identity for a Person.

func Persist

func Persist(paths Paths, prev, next State) error

Persist writes whatever changed between prev and next.

A settings change is written on its own; see ErrMixedSave.

No filesystem renames two files atomically, so the order of the document and Trash writes is chosen so that a crash between them can only ever duplicate a Household, never lose one. The decision is made on the document alone: when the document gains a Household it did not have — a restore, or an undo reaching back past it — that Household is leaving the Trash, so the document is written first; otherwise the Trash is written first. A Trash losing entries by purge is never what drives the order: a purge is a deliberate loss (an entry's 30 days are up, or it was only being kept for one that stays gone), not a Household moving between the two files, and the Trash can shrink between prev and next — during a deletion that also purges an expired entry, or an undo that reaches back past one — without the document gaining anything. OpenTrash reconciles the duplicate on the next start (ADR-0012).

func Restore

func Restore(doc *Document, t *Trash, id rolo.HouseholdID) (*Document, *Trash, []rolo.Household, error)

Restore moves id, and every trashed Household it needs, from t back into doc. It returns the restored Households with id first, for the announcement to name.

func Save

func Save(path string, doc *Document) error

Save writes the document to path atomically.

func SaveSettings

func SaveSettings(path string, s *Settings) error

SaveSettings writes the settings to path atomically, with the document's permissions.

func SaveTrash

func SaveTrash(path string, t *Trash) error

SaveTrash writes the Trash to path atomically, with the document's permissions: it holds the same relatives' addresses and birth dates.

Types

type Document

type Document struct {
	Schema     int              `json:"schema"`
	Households []rolo.Household `json:"households"`
}

Document is the on-disk shape of the Directory: a schema version and a flat slice of Households. The navigation tree is not stored; it is derived from the Households' parent links by Tree.

func Load

func Load(path string) (*Document, error)

Load reads and validates the document at path.

Validation happens here rather than at render time so that a structural problem surfaces at startup, where the Operator sees it, rather than when the Editor clicks Export.

func (*Document) Tree

func (d *Document) Tree() (*rolo.Tree, error)

Tree derives the navigation tree, validating the parent links.

type Paths

type Paths struct {
	Document string
	Trash    string
	Settings string
}

Paths names the files Persist may write.

func (Paths) Write

func (p Paths) Write(prev, next State) error

Write persists next over prev at these paths. It is Persist as a live.Writer, so the production adapter needs no glue.

type Settings

type Settings struct {
	Schema int `json:"schema"`

	// Title is the Directory Title exactly as the Editor set it. Empty means
	// it was never set; the printed default belongs to internal/render.
	Title string `json:"title"`

	// Reply is the Reply Instructions exactly as the Editor set them, printed
	// at the head of every Proof Sheet (CONTEXT.md). Empty means none. It is
	// omitted when empty so a settings file that never had it is unchanged.
	Reply string `json:"reply_instructions,omitempty"`
}

Settings holds the Editor's choices about the Directory as a whole rather than about any one Household: the Directory Title and the Reply Instructions (CONTEXT.md). It lives in its own file beside the document; see ADR-0013.

Like Document and Trash, Settings is never mutated once built: a change is a new value, which is what lets Persist tell what changed by comparing pointers.

func LoadSettings

func LoadSettings(path string) (*Settings, error)

LoadSettings reads the settings at path. A missing file is an untitled Directory: that is every installation's first state, and every installation that predates the file.

func NewSettings

func NewSettings() *Settings

NewSettings returns the settings of a Directory nobody has titled yet.

func (*Settings) WithReply added in v0.3.0

func (s *Settings) WithReply(reply string) *Settings

WithReply returns a copy of s carrying reply, leaving s untouched.

func (*Settings) WithTitle

func (s *Settings) WithTitle(title string) *Settings

WithTitle returns a copy of s carrying title, leaving s untouched.

type State

type State struct {
	Document *Document
	Trash    *Trash

	// Settings is never written when nil, so a State built without it — by a
	// test that predates the settings file — cannot erase the title on disk.
	Settings *Settings
}

State is everything the service persists: the Directory, its Trash and the Editor's settings. None is mutated once built; a change is a new State, which is what lets Persist tell what changed by comparing pointers.

func Open

func Open(p Paths, now time.Time) (State, error)

Open loads everything the service persists, in the order the files depend on one another: the document, then the Trash reconciled against it — so a crash that left a Household in both files is settled before anything is served (ADR-0012) — then the settings, where a missing file is an untitled Directory (ADR-0013).

type Trash

type Trash struct {
	Schema  int          `json:"schema"`
	Entries []TrashEntry `json:"entries"`
}

Trash holds deleted Households until they are purged. It lives in its own file beside the document; see ADR-0012.

Like Document, a Trash is never mutated once built: every change returns a new value. Persist relies on that, comparing pointers to decide which files changed.

func LoadTrash

func LoadTrash(path string) (*Trash, error)

LoadTrash reads the Trash at path. A missing file is an empty Trash: nothing has been deleted yet, which is every installation's first state.

func NewTrash

func NewTrash() *Trash

NewTrash returns an empty Trash at the current schema. Entries is non-nil so the file reads "entries": [] rather than null.

func OpenTrash

func OpenTrash(path string, doc *Document, now time.Time) (*Trash, error)

OpenTrash loads the Trash for doc at startup: it reconciles any duplicate a crash left behind, purges what has expired, and writes the file back only if either changed — so a service with nothing deleted never creates one.

func (*Trash) Chain

func (t *Trash) Chain(id rolo.HouseholdID, doc *Document) ([]rolo.HouseholdID, error)

Chain lists the Households that must return together to restore id: id itself first, then every trashed Household it needs, transitively. Anything already in doc needs nothing. If id is both in the Trash and the Document, the Document wins over the stale duplicate and the entry is not restorable.

func (*Trash) Entry

func (t *Trash) Entry(id rolo.HouseholdID) (TrashEntry, bool)

Entry returns the entry holding id.

func (*Trash) KeptUntil

func (t *Trash) KeptUntil(id rolo.HouseholdID) time.Time

KeptUntil is when id can no longer be restored: its own Expires, or, if later, the Expires of any entry that needs it to be restorable — directly or through another entry — since Purge keeps id alive for exactly as long as anything else in the Trash still needs it. This is the date the Recently deleted page shows; Expires alone can read as already past while the entry is still there, restorable, because something newer anchors it. A hand-repaired trash.json can name a cycle (an entry sharing its own address, or two entries each needing the other), so this guards against re-entering an ID it has already visited, the way Purge's mark and Chain's visit do.

func (*Trash) Purge

func (t *Trash) Purge(now time.Time) (*Trash, bool)

Purge drops entries whose time is up.

An entry past its 30 days is still kept while any unexpired entry needs it, directly or through another entry. Otherwise deleting Clyde/Doris and then, three weeks later, Dave/Diane beneath them would leave Dave/Diane unrestorable after only nine days — Restore brings their parent back with them, and could not if it were gone.

func (*Trash) Reconcile

func (t *Trash) Reconcile(doc *Document) (*Trash, bool)

Reconcile drops every entry whose Household is also in doc. A crash between the two writes of a deletion or a restore leaves such a duplicate by design, and the document wins because it is what the Editor last saw (ADR-0012).

type TrashEntry

type TrashEntry struct {
	DeletedAt time.Time `json:"deleted_at"`
	// Path is the Household's Path at the moment of deletion. It is for display
	// only: the entry's own Parent is what a restore follows, and the Path can
	// no longer be derived once the Household has left the tree.
	Path      string         `json:"path"`
	Household rolo.Household `json:"household"`
}

TrashEntry is one deleted Household, exactly as it was when deleted.

func (TrashEntry) Expires

func (e TrashEntry) Expires() time.Time

Expires is when this entry's own 30 days end. It may be kept longer, for as long as a newer entry needs it; see Purge.

Jump to

Keyboard shortcuts

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