Artifact 8d8ea5c982f6403bba217ecb45afe2623a76f40ac39dae4771ec5fdc93c3f974:
- File
internal/manifest/deltify_test.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: 8932) [annotate] [blame] [check-ins using]
package manifest import ( "fmt" "sort" "strings" "testing" "time" "github.com/danmestas/libfossil/internal/content" libfossil "github.com/danmestas/libfossil/internal/fsltype" "github.com/danmestas/libfossil/internal/repo" ) // incrementalHistory commits nRev revisions of nFile files into r, editing // roughly one line in twenty on each revision. This is the corpus shape the // deltification numbers in issue #71 were measured against: enough shared // text between adjacent versions that a delta should be a small fraction of // the whole, and enough revisions that chain depth becomes observable. func incrementalHistory(t *testing.T, r *repo.Repo, nFile, nRev, nLine int) { t.Helper() bodies := make([][]string, nFile) for f := range bodies { lines := make([]string, nLine) for i := range lines { lines[i] = fmt.Sprintf("file %d line %04d: the quick brown fox jumps over the lazy dog", f, i) } bodies[f] = lines } base := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) var parent libfossil.FslID for rev := 0; rev < nRev; rev++ { files := make([]File, nFile) for f := range bodies { if rev > 0 { for i := rev % 20; i < nLine; i += 20 { bodies[f][i] = fmt.Sprintf("file %d line %04d: revision %d edited this line", f, i, rev) } } body := "" for _, l := range bodies[f] { body += l + "\n" } files[f] = File{Name: fmt.Sprintf("src/file%d.txt", f), Content: []byte(body)} } rid, _, err := Checkin(r, CheckinOpts{ Files: files, Comment: fmt.Sprintf("revision %d", rev), User: "testuser", Parent: parent, Time: base.Add(time.Duration(rev) * time.Hour), }) if err != nil { t.Fatalf("Checkin(rev %d): %v", rev, err) } parent = rid } } // storageStats reports how the repository actually stored what it was given. // Content bytes come from sum(length(content)) rather than the SQLite file // size: without VACUUM, pages freed by rewriting a blob into delta form stay // in the file, so file size does not move when deltification works. type storageStats struct { artifacts int deltaEncoded int contentBytes int64 depthP50 int depthMax int } func collectStorageStats(t *testing.T, r *repo.Repo) storageStats { t.Helper() var s storageStats if err := r.DB().QueryRow( "SELECT count(*), coalesce(sum(length(content)), 0) FROM blob WHERE size >= 0", ).Scan(&s.artifacts, &s.contentBytes); err != nil { t.Fatalf("blob totals: %v", err) } src := map[libfossil.FslID]libfossil.FslID{} rows, err := r.DB().Query("SELECT rid, srcid FROM delta") if err != nil { t.Fatalf("delta scan: %v", err) } for rows.Next() { var rid, srcid libfossil.FslID if err := rows.Scan(&rid, &srcid); err != nil { t.Fatalf("delta row: %v", err) } src[rid] = srcid } rows.Close() s.deltaEncoded = len(src) depths := make([]int, 0, s.artifacts) ridRows, err := r.DB().Query("SELECT rid FROM blob WHERE size >= 0") if err != nil { t.Fatalf("rid scan: %v", err) } for ridRows.Next() { var rid libfossil.FslID if err := ridRows.Scan(&rid); err != nil { t.Fatalf("rid row: %v", err) } d := 0 for cur := rid; ; d++ { next, ok := src[cur] if !ok { break } if d > s.artifacts { t.Fatalf("delta chain from rid=%d does not terminate", rid) } cur = next } depths = append(depths, d) } ridRows.Close() sort.Ints(depths) if len(depths) > 0 { s.depthP50 = depths[len(depths)/2] s.depthMax = depths[len(depths)-1] } return s } func TestCheckinDeltifiesIncrementalHistory(t *testing.T) { r := setupTestRepo(t) incrementalHistory(t, r, 4, 12, 400) s := collectStorageStats(t, r) rate := float64(s.deltaEncoded) / float64(s.artifacts) t.Logf("artifacts=%d delta-encoded=%d (%.0f%%) p50depth=%d maxdepth=%d contentBytes=%d", s.artifacts, s.deltaEncoded, rate*100, s.depthP50, s.depthMax, s.contentBytes) if s.deltaEncoded == 0 { t.Fatalf("delta table is empty: nothing on the commit path deltified") } // Canonical fossil reaches 78% on this corpus shape. Demand a // substantial majority rather than pinning an exact rate, since our // artifact mix (no baseline manifests) is not identical. if rate < 0.60 { t.Errorf("delta encoding rate %.0f%% < 60%%", rate*100) } // This corpus stored 58,973 content bytes with every artifact whole and // 23,994 once deltified. The bound sits between the two so a regression // that quietly stops deltifying fails here rather than passing on the // encoding-rate check alone. if s.contentBytes > 35000 { t.Errorf("content bytes %d exceeds 35000; deltification is not paying off", s.contentBytes) } // Backward deltification makes each artifact's depth its age in // revisions, so depth must stay on the order of the history length. if s.depthMax > 50 { t.Errorf("max chain depth %d is larger than the history justifies", s.depthMax) } } // TestCheckinDeltifiesParentManifest covers the check-in manifest itself, // which the incremental-history corpus above does not exercise: when every // file changes on every revision, every F-card hash changes too, so adjacent // manifests are genuinely dissimilar and the 75% rule declines them. Touching // one file per revision leaves the other F-cards identical, which is the // common real-world shape and where manifest deltification pays. func TestCheckinDeltifiesParentManifest(t *testing.T) { r := setupTestRepo(t) // Long file names make the manifest big enough that the unchanged // F-cards dominate, the same way a real project's manifest does. names := []string{ "src/internal/storage/engine/compaction_scheduler.go", "src/internal/storage/engine/write_ahead_log_reader.go", "src/internal/storage/engine/manifest_version_edit.go", "src/internal/storage/engine/block_cache_eviction.go", } bodies := make([][]byte, len(names)) for i := range bodies { bodies[i] = []byte(strings.Repeat(fmt.Sprintf("package engine // file %d\n", i), 60)) } base := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) var parent libfossil.FslID manifestRids := make([]libfossil.FslID, 0, 12) for rev := 0; rev < 12; rev++ { touched := rev % len(names) if rev > 0 { bodies[touched] = append(bodies[touched], []byte(fmt.Sprintf("// revision %d touched this file\n", rev))...) } files := make([]File, len(names)) for i := range names { files[i] = File{Name: names[i], Content: bodies[i]} } rid, _, err := Checkin(r, CheckinOpts{ Files: files, Comment: "revision", User: "testuser", Parent: parent, Time: base.Add(time.Duration(rev) * time.Hour), }) if err != nil { t.Fatalf("Checkin(rev %d): %v", rev, err) } manifestRids = append(manifestRids, rid) parent = rid } // Every manifest but the tip must be a delta against its immediate // child, and the tip must be whole. Check each index on its own rather // than counting, so a regression from 11-of-12 to 1-of-12 names the // manifest that stopped being encoded. The last index is handled // separately: indexing manifestRids[i+1] there would be out of range, // and treating that as the tip assertion rather than a guard keeps the // loop from depending on the property it exists to verify. last := len(manifestRids) - 1 deltified := 0 for i, rid := range manifestRids { var srcid libfossil.FslID err := r.DB().QueryRow("SELECT srcid FROM delta WHERE rid=?", rid).Scan(&srcid) if i == last { if err == nil { t.Errorf("tip manifest rid=%d was stored as a delta against %d", rid, srcid) } continue } if err != nil { t.Errorf("manifest %d (rid=%d) was not deltified", i, rid) continue } deltified++ if want := manifestRids[i+1]; srcid != want { t.Errorf("manifest %d deltified against rid %d, want its child %d", i, srcid, want) } } if want := len(manifestRids) - 1; deltified != want { t.Errorf("deltified %d of %d non-tip manifests, want all %d", deltified, want, want) } } // TestDeltifiedArtifactsAllExpand is the invariant deltification must not // break: every stored artifact, delta-encoded or not, still expands to // content matching its declared UUID. content.Expand verifies that hash // internally, so a successful call on every rid is the assertion. func TestDeltifiedArtifactsAllExpand(t *testing.T) { r := setupTestRepo(t) incrementalHistory(t, r, 3, 8, 200) rows, err := r.DB().Query("SELECT rid FROM blob WHERE size >= 0 ORDER BY rid") if err != nil { t.Fatalf("rid scan: %v", err) } defer rows.Close() n := 0 for rows.Next() { var rid libfossil.FslID if err := rows.Scan(&rid); err != nil { t.Fatalf("rid row: %v", err) } if _, err := content.Expand(r.DB(), rid); err != nil { t.Errorf("content.Expand(rid=%d): %v", rid, err) } n++ } if n == 0 { t.Fatal("no artifacts to expand") } if s := collectStorageStats(t, r); s.deltaEncoded == 0 { t.Fatal("no artifacts were delta-encoded; the expansion check proved nothing") } }