Documentation
¶
Overview ¶
Package store persists the Directory as a single atomically-written JSON document and generates the stable identities its records carry.
Index ¶
- Constants
- Variables
- func Delete(doc *Document, t *Trash, entry TrashEntry) (*Document, *Trash, error)
- func NewHouseholdID() (rolo.HouseholdID, error)
- func NewPersonID() (rolo.PersonID, error)
- func Persist(paths Paths, prev, next State) error
- func Restore(doc *Document, t *Trash, id rolo.HouseholdID) (*Document, *Trash, []rolo.Household, error)
- func Save(path string, doc *Document) error
- func SaveSettings(path string, s *Settings) error
- func SaveTrash(path string, t *Trash) error
- type Document
- type Paths
- type Settings
- type State
- type Trash
- func (t *Trash) Chain(id rolo.HouseholdID, doc *Document) ([]rolo.HouseholdID, error)
- func (t *Trash) Entry(id rolo.HouseholdID) (TrashEntry, bool)
- func (t *Trash) KeptUntil(id rolo.HouseholdID) time.Time
- func (t *Trash) Purge(now time.Time) (*Trash, bool)
- func (t *Trash) Reconcile(doc *Document) (*Trash, bool)
- type TrashEntry
Constants ¶
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 )
const CurrentSchema = 1
CurrentSchema is the document version this binary writes and is the highest it can read.
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.
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.
const TrashRetention = 30 * 24 * time.Hour
TrashRetention is the least time a deleted Household is kept (§3).
Variables ¶
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") )
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") )
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 ¶
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 ¶
NewPersonID generates a stable identity for a Person.
func Persist ¶
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 SaveSettings ¶
SaveSettings writes the settings to path atomically, with the document's permissions.
Types ¶
type Document ¶
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.
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 ¶
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.
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 ¶
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 ¶
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 ¶
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 ¶
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.
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.