Artifact 0bcaa404065ef2ed6b446525a47b3de42d099328aa6a408243606bbdd78221c3:
- File
internal/manifest/mlink.go
— part of check-in
[2368cad91a]
at
2026-07-19 20:55:48
on branch trunk
— Deltify predecessors on the commit path (#87)
* Deltify predecessors on the commit path
Every locally-created artifact was stored whole. The delta table was in the schema and stayed empty, so a repository grew with the number of commits rather than the amount of change. blob.StoreDelta existed and worked but had no caller on the commit path; the only delta-store in use was blob.StoreDeltaRaw, from the sync receive path, for deltas that arrive pre-encoded. The write path never made a deltify decision at all.
Port canonical Fossil's content_deltify (2.28 src/content.c:852-947) as content.Deltify. Fossil deltifies backwards: a new artifact is always stored whole and its PREDECESSOR is rewritten as a delta against it, so the tip of every chain is full content and reading the newest version of a file stays a single blob load. Chain depth becomes an artifact's age in revisions rather than something that compounds, because an artifact already stored as a delta is never re-deltified.
blob.StoreDelta turned out to be the wrong primitive to wire up: it inserts a new row as a delta, whereas the policy needs to rewrite an existing row. It is left alone. content.Deltify lives in the content package because it needs Expand, which blob cannot import.
The policy is stated once, in content.Deltify, with each rule's canonical line cited: the 50-byte floors, the 75% ratio, the never-redeltify rule that bounds depth, the private-into-public refusal, and the loop break that undeltas the source and then still declines the pairing. Two call sites pass pairs, mirroring canonical's: insertMlinkRow for file content (manifest.c:1562) and Checkin for the parent manifest (checkin.c:3234). insertMlinkRow is shared by the fresh-commit and crosslink paths, so file deltification cannot drift between them.
On a 12-revision, 4-file history: 60 artifacts, 44 delta-encoded (73%), p50 chain depth 4, max 11, and 23,994 content bytes measured as sum(length(content)) -- down from 58,973 with everything whole. A repository produced this way passes fossil 2.28 test-integrity (60/60 blobs, 0 errors) and fossil rebuild, which re-derives every table by walking the chains.
* Bound the deltify ancestor walk, require a transaction, harden tests
Review follow-ups on the deltification policy.
Bound the ancestor walk. It was a bare loop with no cycle guard: a sync peer can shape a cyclic delta graph, because blob.StoreDeltaRaw records delta(rid,srcid) links from the wire without a cycle check, and an unbounded walk would spin inside the write transaction. Guard with the seen-set idiom content.walkDeltaChain already uses, plus a step cap. In the current ordering IsAvailable runs first on both rids and follows the same links under its own depth bound, so Deltify declines before the walk begins -- but that protection is incidental to a different question, so the bound belongs to the loop that needs it. Both behaviours are now pinned: the walk reports a cycle, and Deltify declines an ungrounded source.
Move both size floors ahead of the walk. Canonical returns on a sub-50-byte target (content.c:877-885) before entering the candidate loop, so it never reaches the content_undelta on :902. Running the walk first meant a small target whose successor chained back to it would inflate the source to full content and then decline anyway -- a storage regression and exactly the divergence from canonical's layout the surrounding comment claims to avoid.
Take *db.Tx instead of db.Querier. The rewrite is two statements, and a blob holding delta bytes with no delta row expands as though it were full content: silent corruption rather than a detectable error. Canonical wraps the pair in db_begin_transaction (content.c:938-941); the signature now makes that non-negotiable at compile time. Both call sites already passed a transaction.
Three tests were named for behaviours they did not pin, and each is now verified to fail when the behaviour is reverted:
- The 75% ratio test had no assertion at all; deleting the rule left it passing. It now asserts the pair is declined and stays whole. - The loop-break test could not distinguish declining from proceeding, so the undelta-then-proceed behaviour corrected in the previous commit was untested. It now asserts the target is left whole. - The manifest test checked only "at least one", so a regression from 11-of-12 to 3-of-12 passed. It now checks each index and names the manifest that stopped being encoded.
Also cover the source-side 50-byte floor, which the tiny-artifact test could not reach because it made both artifacts tiny and returned at the target check first, and assert that Undelta restores blob.size while Deltify leaves it alone (content.c:761 vs :935).
Gate the fossil-binary test on REQUIRE_FOSSIL_BIN, set in CI. A skip is invisible without -v, so a run with no fossil binary was byte-identical to a pass -- for the one criterion our own tests cannot substitute for.
Document that blob.StoreDelta is not the commit path's primitive. It inserts a new row as a delta; deltifying rewrites an existing one. It has no non-test callers and the package doc advertised it as the delta-storing entry point.
* Document what the deltify loop-safety argument rests on
Two comments, no behaviour change.
The IsAvailable(srcRid) guard is ours, not canonical's: content.c guards only content_is_available(rid) and then calls content_get(srcid) unguarded. It is also load-bearing beyond phantoms, because it screens out the cyclic delta graphs that would otherwise reach the ancestor walk. Say both, at the guard.
And say at the walk why its seen-set never fires in practice, so that someone reordering the prologue meets the reason rather than concluding the bound is dead code. The note lived only in the test file, which is not where that reader is looking. (user: [email protected], size: 6541) [annotate] [blame] [check-ins using]
package manifest import ( "database/sql" "errors" "fmt" "strings" "github.com/danmestas/libfossil/db" "github.com/danmestas/libfossil/internal/content" libfossil "github.com/danmestas/libfossil/internal/fsltype" ) // maxMlinkMergeParents bounds the merge-parent lookup loop in // resolveMlinkParent. TigerStyle: every loop needs an explicit bound. A // check-in with more merge parents than this is almost certainly corrupt // input, not a legitimate octopus merge. const maxMlinkMergeParents = 1024 // permToMperm converts a Fossil F-card permission string to the mlink.mperm // encoding used by canonical Fossil (src/manifest.c add_one_mlink): 0 = // regular file, 1 = executable, 2 = symlink. // // Canonical manifest_file_mperm (src/manifest.c:1482-1492) does a substring // test (strstr), not an exact match: perm fields can carry more than one // character (e.g. Fossil's " w" rename placeholder — see #51), and // internal/deck/parse.go:194 assigns the F-card perm field verbatim from // remote input over xfer. An exact match would silently drop the // executable bit for any multi-character perm string containing "x", which // is the exact invariant PR #48 landed to protect. x is tested before l to // match canonical's check order. func permToMperm(perm string) int64 { switch { case strings.Contains(perm, "x"): return 1 case strings.Contains(perm, "l"): return 2 default: return 0 } } // resolveMlinkParent implements Fossil's mlink parent-column convention — // the single place in this package that knows it. See // src/manifest.c:1668-1679 (the add_mlink header comment) in canonical // Fossil: // // - a file carried over from the primary parent: pid = the parent file's // blob rid, pmid = the primary parent's manifest rid // - a file added by this check-in (absent from every parent): pid = 0 // - a file added by a merge (absent from the primary parent, present in // a merge parent): pid = -1 // // pmid is always the primary parent's manifest rid, or 0 if this check-in // has no parent at all. Both insertMlinks (direct check-in) and // insertCheckinMlinks (xfer/Crosslink ingestion) route every mlink row // through this function so the two write paths cannot drift apart again. func resolveMlinkParent(tx *db.Tx, fnid int64, primaryParentMid libfossil.FslID, mergeParentMids []libfossil.FslID) (pmid, pid int64, err error) { if tx == nil { panic("manifest.resolveMlinkParent: tx must not be nil") } if fnid <= 0 { panic("manifest.resolveMlinkParent: fnid must be positive") } if len(mergeParentMids) > maxMlinkMergeParents { panic("manifest.resolveMlinkParent: mergeParentMids exceeds bound") } if primaryParentMid <= 0 { return 0, 0, nil // no parent at all: file is new to this check-in } pmid = int64(primaryParentMid) var parentFid int64 err = tx.QueryRow("SELECT fid FROM mlink WHERE mid=? AND fnid=?", primaryParentMid, fnid).Scan(&parentFid) switch { case err == nil: return pmid, parentFid, nil // carried over from the primary parent case !errors.Is(err, sql.ErrNoRows): return 0, 0, fmt.Errorf("lookup primary parent fid: %w", err) } // Not present in the primary parent's file set. If a merge parent // carried this filename, the file was added by the merge (pid=-1); // otherwise it is genuinely new to this check-in (pid=0). // // Known divergence from canonical: real Fossil's rule is not "present // in a merge parent" but count(*) < nLink over the per-fnid mlink rows // it writes for every parent transition (src/manifest.c:1905-1915). For // a file that exists in a merge parent with DIFFERENT content — i.e. a // conflict resolved during the merge — canonical emits an auxiliary // row and leaves pid=0, but this function returns pid=-1. Low blast // radius: every mlink consumer in this package (finfo.go, dephantomize.go) // filters on pid != fid, which holds either way. Left as-is rather than // tracking per-parent content equality, which the single-row-per-file // model this package uses does not otherwise need. for _, mp := range mergeParentMids { if mp <= 0 { continue } var exists int64 err = tx.QueryRow("SELECT 1 FROM mlink WHERE mid=? AND fnid=?", mp, fnid).Scan(&exists) switch { case err == nil: return pmid, -1, nil // added by merge case !errors.Is(err, sql.ErrNoRows): return 0, 0, fmt.Errorf("lookup merge parent fid: %w", err) } } return pmid, 0, nil // added by this check-in } // insertMlinkRow inserts one mlink row, resolving pmid/pid via // resolveMlinkParent and pfnid via oldName (empty if the file was not // renamed). fid must be 0 when the check-in deletes the file. mperm is the // raw Fossil permission string ("", "x", "l"); this function converts it. // isaux is always written 0: neither write path in this package produces // more than one mlink row per (mid, fnid), so the auxiliary/merge-parent // row distinction canonical Fossil uses does not arise here. func insertMlinkRow(tx *db.Tx, mid libfossil.FslID, fid int64, fnid int64, oldName string, perm string, primaryParentMid libfossil.FslID, mergeParentMids []libfossil.FslID) error { if tx == nil { panic("manifest.insertMlinkRow: tx must not be nil") } if mid <= 0 { panic("manifest.insertMlinkRow: mid must be positive") } if fnid <= 0 { panic("manifest.insertMlinkRow: fnid must be positive") } pmid, pid, err := resolveMlinkParent(tx, fnid, primaryParentMid, mergeParentMids) if err != nil { return fmt.Errorf("resolve mlink parent: %w", err) } var pfnid int64 if oldName != "" { pfnid, err = ensureFilename(tx, oldName) if err != nil { return fmt.Errorf("prior filename %q: %w", oldName, err) } } if _, err := tx.Exec( "INSERT INTO mlink(mid, fid, pmid, pid, fnid, pfnid, mperm, isaux) VALUES(?, ?, ?, ?, ?, ?, ?, ?)", mid, fid, pmid, pid, fnid, pfnid, permToMperm(perm), 0, ); err != nil { return fmt.Errorf("mlink: %w", err) } // Store the file's previous version as a delta against this one. Both // write paths in this package funnel through here, so this is the only // place file content is deltified. Mirrors add_mlink's tail in canonical // Fossil (src/manifest.c:1562, `if( pid && fid ) content_deltify(pid, // &fid, 1, 0)`); content.Deltify holds the policy and declines on its // own when the pair is not worth encoding. if pid > 0 && fid > 0 { if _, err := content.Deltify(tx, libfossil.FslID(pid), libfossil.FslID(fid)); err != nil { return fmt.Errorf("deltify prior file version: %w", err) } } return nil }