Artifact 90655863db61e5175f2a5f6460bd8d02e8991b28137587f2edef8a01299d5850:
- File
internal/content/sparse.go
— part of check-in
[b28c06506b]
at
2026-10-22 02:57:50
on branch feat/horizon-cones
— fix(content): classify sparse stores from crosslink tables, not the whole store
Sparse classification read, inflated and delta-expanded every held blob, file content included, under a whole-store byte budget. On a clone of the Fossil SCM repository (67,894 blobs, 8.4 GB expanded) LoadAbsences failed at once with "byte budget exceeded", so no path-cone or content-horizon store of a real repository could classify its phantoms, and pull, verify and compaction failed with it.
Reference facts now come from the tables wherever they provably stand in for raw cards. A blob the crosslinker recorded as no artifact and mlink names as a file version is never read. A completely linked check-in (event row, no orphan row, a primary parent edge) whose primary parent and baseline trees are themselves reproduced contributes its mlink rows as F cards and its plink, cherrypick and tagxref rows as references; by induction over the primary chain those rows give the same (name, file) pairs as its raw cards. Everything else is still read raw: unlinked and orphaned check-ins, roots, check-ins whose parent never arrived, other artifacts, and every check-in once the store has seen a sub-manifest, since trees are not crosslinked. Budgets are charged per round for the selected blobs and their delta ancestors only, and a base's bytes are released once its last delta is applied.
A content horizon now reads only its held set's manifests and the baselines and nodes they reach, on demand, with one visited set and a byte budget scaled to the held set.
On the Fossil SCM clone, cone {src} classifies in about 2 s at a 65 MiB peak heap, reading 8,655 of 67,894 blobs; the inside/outside sets equal the all-raw result, and Outside/Required agree on every artifact. (user: [email protected], size: 11649) [annotate] [blame] [check-ins using] [more...]
package content import ( "context" "database/sql" "errors" "fmt" "github.com/danmestas/go-libfossil/db" "github.com/danmestas/go-libfossil/internal/cone" libfossil "github.com/danmestas/go-libfossil/internal/fsltype" ) // The store cone of draft-fossil-sparse-cone-00 §5 lives in a table of its // own, created when a cone is set and dropped when it is cleared, so a dense // repository's schema is untouched. `fossil rebuild` drops every table it // does not know, which puts the cone in the disposable tier §5.2 recommends: // losing it reclassifies every elided phantom as a transit phantom, and the // store heals toward dense through ordinary synchronization. const storeConeSchema = `CREATE TABLE IF NOT EXISTS storecone( id INTEGER PRIMARY KEY CHECK(id=1), cone BLOB NOT NULL )` // StoreCone returns the store cone, or nil when the store is dense. A cone // this implementation cannot read -- a later serialization version, or bytes // that do not parse -- is policy loss (§3.1, §5.2) and reads as dense, never // as an error: the cost is bandwidth, not data. func StoreCone(q db.Querier) (*cone.Cone, error) { if q == nil { panic("content.StoreCone: q must not be nil") } var n int if err := q.QueryRow( "SELECT count(*) FROM sqlite_master WHERE type='table' AND name='storecone'", ).Scan(&n); err != nil { return nil, fmt.Errorf("content.StoreCone: %w", err) } if n == 0 { return nil, nil } var data []byte err := q.QueryRow("SELECT cone FROM storecone WHERE id=1").Scan(&data) if errors.Is(err, sql.ErrNoRows) { return nil, nil } if err != nil { return nil, fmt.Errorf("content.StoreCone: %w", err) } c, err := cone.Parse(data) if err != nil { return nil, nil } return c, nil } // SetStoreCone replaces the store cone; nil makes the store dense. Neither // direction moves content: narrowing deletes nothing (§5.4), and widening // only makes the newly covered elided phantoms wanted again, for the next // synchronization to fetch. A structural horizon (draft-fossil-horizon-00 // §5) is refused: this store implements only the content horizon, and // would otherwise hold a cone whose depth its classification ignores. func SetStoreCone(q db.Querier, c *cone.Cone) error { if q == nil { panic("content.SetStoreCone: q must not be nil") } if c == nil { if _, err := q.Exec("DROP TABLE IF EXISTS storecone"); err != nil { return fmt.Errorf("content.SetStoreCone: %w", err) } return nil } if h, ok := c.Horizon(); ok && h.Depth() != cone.DepthContent { return fmt.Errorf("content.SetStoreCone: a %v horizon is not implemented", h.Depth()) } if _, err := q.Exec(storeConeSchema); err != nil { return fmt.Errorf("content.SetStoreCone: %w", err) } if _, err := q.Exec("REPLACE INTO storecone(id, cone) VALUES(1, ?)", c.Serialize()); err != nil { return fmt.Errorf("content.SetStoreCone: %w", err) } return nil } // Absence classifies an artifact the store names: held, or absent for one // of the three reasons of §5.2. type Absence int const ( // Held is not absent: the store holds the content. Held Absence = iota // Transit is wanted and not yet arrived; synchronization requests it. Transit // Shunned is forbidden and never requested. Shunned // Elided is deliberately absent under the store cone. Synchronization // does not request it, and its absence is healthy. Elided ) func (a Absence) String() string { switch a { case Held: return "held" case Transit: return "transit" case Shunned: return "shunned" case Elided: return "elided" } return fmt.Sprintf("Absence(%d)", int(a)) } // Absences is the classification of every phantom in a store, computed from // the store cone at load time. Elision is never stored on a phantom (§5.2): // a cone change is a reload, not a migration. type Absences struct { cone *cone.Cone class map[libfossil.FslID]Absence counts [Elided + 1]int work ReferenceWork // what classification read, for tests to bound } // LoadAbsences classifies the store's phantoms. Shunned names win; raw F/S // provenance proves elision only outside every covered alias and structural // reference. Retained delta ancestors remain transit. Crosslink references // supplement raw provenance for historical names, including prior rename paths. // A partial tree need not be crosslinked to classify its outside S children. // A content horizon in the cone also elides the blobs below it. func LoadAbsences(q db.Querier) (*Absences, error) { return LoadAbsencesContext(context.Background(), q) } // LoadAbsencesContext carries cancellation through raw provenance discovery. func LoadAbsencesContext(ctx context.Context, q db.Querier) (*Absences, error) { return loadAbsences(ctx, q, defaultReferenceLimits) } func loadAbsences(ctx context.Context, q db.Querier, limits referenceLimits) (*Absences, error) { if ctx == nil || q == nil { panic("content.LoadAbsencesContext: nil context or querier") } if err := ctx.Err(); err != nil { return nil, err } c, err := StoreCone(q) if err != nil { return nil, err } a := &Absences{cone: c, class: map[libfossil.FslID]Absence{}} rows, err := q.Query(` SELECT p.rid, EXISTS(SELECT 1 FROM shun WHERE uuid=b.uuid), EXISTS(SELECT 1 FROM delta d JOIN blob h ON h.rid=d.rid WHERE d.srcid=p.rid AND h.size>=0) FROM phantom p JOIN blob b ON b.rid=p.rid`) if err != nil { return nil, fmt.Errorf("content.LoadAbsences: phantoms: %w", err) } candidates := map[libfossil.FslID]bool{} for rows.Next() { var rid libfossil.FslID var shunned, base bool if err := rows.Scan(&rid, &shunned, &base); err != nil { rows.Close() return nil, fmt.Errorf("content.LoadAbsences: phantoms: %w", err) } switch { case shunned: a.class[rid] = Shunned default: a.class[rid] = Transit if c != nil && !base { candidates[rid] = true } } } if err := errors.Join(rows.Err(), rows.Close()); err != nil { return nil, fmt.Errorf("content.LoadAbsences: phantoms: %w", err) } if len(candidates) > 0 { if err := a.classify(ctx, q, candidates, limits); err != nil { return nil, err } } for _, k := range a.class { a.counts[k]++ } return a, nil } // classify decides the candidates, the phantoms that are neither shunned // nor a held delta's base. Path members elide by name. A content horizon // (draft-fossil-horizon-00 §3.2, §4) then elides every blob no check-in in // its held set names, so a kept blob is in both keep sets; it never elides // structure, and under one path members do not either. func (a *Absences) classify( ctx context.Context, q db.Querier, candidates map[libfossil.FslID]bool, limits referenceLimits, ) error { facts, work, err := loadConeReferenceFactsBounded(ctx, q, limits.refs, true) a.work = work if err != nil { return err } refs, err := facts.Classify(ctx, a.cone) if err != nil { return err } if err := keepRequiredPhantoms(q, candidates, refs); err != nil { return err } if err := a.elide(q, candidates); err != nil { return err } h, hasHorizon := a.cone.Horizon() for rid := range candidates { if hasHorizon && refs.structure(rid) { a.class[rid] = Transit } else if refs.Outside(rid) { a.class[rid] = Elided } else if refs.Required(rid) { a.class[rid] = Transit } } if !hasHorizon { return nil } above, placed, err := horizonAbove(ctx, q, h, limits, &a.work) if err != nil || !placed { return err } for rid := range candidates { if refs.file(rid) && !refs.structure(rid) && !above[refs.names[rid]] { a.class[rid] = Elided } } return nil } // An artifact may be both a file's content and a structural reference or // delta ancestor. Its out-of-cone filename does not permit either hole. func keepRequiredPhantoms(q db.Querier, candidates map[libfossil.FslID]bool, refs *ConeReferences) error { var haveForum bool if err := q.QueryRow(`SELECT EXISTS( SELECT 1 FROM sqlite_master WHERE name='forumpost' AND type='table')`). Scan(&haveForum); err != nil { return fmt.Errorf("content.LoadAbsences: forum table: %w", err) } query := ` WITH RECURSIVE retained(rid) AS ( SELECT d.srcid FROM delta d JOIN blob b ON b.rid=d.rid WHERE b.size>=0 UNION SELECT d.srcid FROM delta d JOIN retained r ON d.rid=r.rid ) SELECT rid FROM retained UNION SELECT objid FROM event UNION SELECT mid FROM mlink UNION SELECT pid FROM plink UNION SELECT cid FROM plink UNION SELECT baseid FROM plink UNION SELECT rid FROM orphan UNION SELECT srcid FROM tagxref UNION SELECT origid FROM tagxref UNION SELECT rid FROM tagxref UNION SELECT attachid FROM attachment UNION SELECT b.rid FROM attachment a JOIN blob b ON b.uuid=a.src UNION SELECT parentid FROM cherrypick UNION SELECT childid FROM cherrypick` if haveForum { query += ` UNION SELECT fpid FROM forumpost UNION SELECT froot FROM forumpost UNION SELECT fprev FROM forumpost UNION SELECT firt FROM forumpost` } rows, err := q.Query(query + " INTERSECT SELECT rid FROM phantom") if err != nil { return fmt.Errorf("content.LoadAbsences: structural references: %w", err) } for rows.Next() { var rid libfossil.FslID if err := rows.Scan(&rid); err != nil { rows.Close() return fmt.Errorf("content.LoadAbsences: structural references: %w", err) } delete(candidates, rid) } if err := errors.Join(rows.Err(), rows.Close()); err != nil { return fmt.Errorf("content.LoadAbsences: structural references: %w", err) } // orphan.baseline also records deferred S children, not just B baselines. // Only raw tree provenance can distinguish a healthy outside subtree. rows, err = q.Query("SELECT baseline FROM orphan") if err != nil { return err } for rows.Next() { var rid libfossil.FslID if err := rows.Scan(&rid); err != nil { return errors.Join(err, rows.Close()) } if !refs.tree[refs.names[rid]] || !refs.Outside(rid) { delete(candidates, rid) } } return errors.Join(rows.Err(), rows.Close()) } // elide marks the candidates F cards name only outside the cone. func (a *Absences) elide(q db.Querier, candidates map[libfossil.FslID]bool) error { rows, err := q.Query(` SELECT m.fid, f.name FROM phantom p JOIN mlink m ON m.fid=p.rid JOIN filename f ON f.fnid=m.fnid UNION SELECT m.pid, f.name FROM phantom p JOIN mlink m ON m.pid=p.rid JOIN filename f ON f.fnid=CASE WHEN m.pfnid>0 THEN m.pfnid ELSE m.fnid END`) if err != nil { return fmt.Errorf("content.LoadAbsences: references: %w", err) } out := map[libfossil.FslID]bool{} in := map[libfossil.FslID]bool{} for rows.Next() { var rid libfossil.FslID var name string if err := rows.Scan(&rid, &name); err != nil { rows.Close() return fmt.Errorf("content.LoadAbsences: references: %w", err) } if !candidates[rid] { continue } if a.cone.Covers(name) { in[rid] = true } else { out[rid] = true } } if err := errors.Join(rows.Err(), rows.Close()); err != nil { return fmt.Errorf("content.LoadAbsences: references: %w", err) } for rid := range out { if !in[rid] { a.class[rid] = Elided } } return nil } // Cone is the store cone the classification used, nil for a dense store. func (a *Absences) Cone() *cone.Cone { return a.cone } // Class reports why rid is absent, or Held when it is not a phantom. func (a *Absences) Class(rid libfossil.FslID) Absence { if k, ok := a.class[rid]; ok { return k } return Held } // Count is the number of phantoms in class k. Held is not counted. func (a *Absences) Count(k Absence) int { if k == Held { panic("content.Absences.Count: held artifacts are not counted") } return a.counts[k] }