From 631f5baad59f19a65e97a7704a9a0c85de60e52d Mon Sep 17 00:00:00 2001 From: mahis1067 Date: Mon, 16 Nov 2026 20:40:39 -0500 Subject: [PATCH] inital commit --- docs/demo.md | 99 +++++++++++++++++++++++++++++++ docs/packfiles.md | 18 ++++++ docs/performance.md | 25 ++++++++ docs/writeups/module-1.md | 122 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 264 insertions(+) create mode 100644 docs/demo.md create mode 100644 docs/writeups/module-1.md diff --git a/docs/demo.md b/docs/demo.md new file mode 100644 index 0000000..d609cf3 --- /dev/null +++ b/docs/demo.md @@ -0,0 +1,99 @@ +# Storage demo + +This is the Module 1 segment for the shared Week 9 demo. It uses a disposable +temporary repository and the existing Python object API. There is no `pack` +CLI command; packing is demonstrated with `ObjectStore.pack_objects`. + +## Presenter flow + +Run this from the repository root with the project environment selected: + +```python +from pathlib import Path +from tempfile import TemporaryDirectory + +from minigit.commits import CommitManager +from minigit.objects import ObjectStore, TreeEntry + +with TemporaryDirectory(prefix="minigit-storage-demo-") as directory: + root = Path(directory) + store = ObjectStore(root) + commits = CommitManager(root, store=store) + + original = b"hello\n" + changed = b"hello, storage\n" + original_id = store.hash_object(original, "blob") + same_id = store.hash_object(original, "blob") + changed_id = store.hash_object(changed, "blob") + assert original_id == same_id + assert original_id != changed_id + + blob = store.write_object(original, "blob") + nested = store.write_tree([TreeEntry("100644", "blob", blob, "hello.txt")]) + root_tree = store.write_tree([TreeEntry("40000", "tree", nested, "nested")]) + base = commits.create_commit(root_tree, [], "demo ", "base") + side = commits.create_commit(root_tree, [base], "demo ", "side") + merge = commits.create_commit(root_tree, [base, side], "demo ", "merge") + + objects = [blob, nested, root_tree, base, side, merge] + store.pack_objects(objects) + for object_id in objects: + store._object_path(object_id).unlink() + + packed_store = ObjectStore(root) + assert packed_store.read_object(blob) == ("blob", original) + assert packed_store.read_tree(root_tree)[0].type == "tree" + assert commits.read_commit(merge).parents == [base, side] +``` + +Expected results: + +```text +identical bytes -> same object ID: true +changed bytes -> different object ID: true +nested tree -> tree object links to blob and subtree: true +merge commit -> preserves two parents: true +packed-only snapshot -> all six objects read successfully: true +``` + +## Loose, packed-only, and mixed reads + +Use the same object list to show the three supported storage states: + +| State | Fixture action | Expected read | +| --- | --- | --- | +| Loose | Keep all loose object files | Every object returns its original type and bytes | +| Packed-only | Call `pack_objects`, then remove loose files in the temporary repository | Every object is read through the published index and pack | +| Mixed | Pack only a subset and keep the remaining loose files | Loose objects are preferred; packed objects are read as fallback | + +The removal step is safe here because the repository is created by +`TemporaryDirectory`. Do not remove loose objects from a working repository as +part of the demo. + +The larger shared fixture is reproducible with: + +```powershell +.\.venv\Scripts\python.exe scripts\benchmark_packfiles.py --profile +``` + +It reports loose bytes, retained loose bytes, pack bytes, index bytes, pack +time, and packed-only read time. `--no-cache --profile` is the controlled +comparison for repeated index and pack validation. + +## Failure guarantees to explain + +- A missing hash or absent object raises `ObjectNotFoundError`. +- Malformed loose or packed bytes raise `ObjectCorruptError`. +- A corrupt loose copy wins over a valid packed copy; it is never silently + replaced by fallback lookup. +- Duplicate writes validate the existing object before returning its hash. +- Pack publication validates both temporary files, renames the pack first, and + publishes the index last. A failed publication leaves earlier readable + objects intact and does not expose an unpublished index. + +## Rehearsal record + +The flow above was rehearsed successfully on revision +`006740a34dc70c70b47397b25a9b681ecdf3f463` with Python 3.11.9 on Windows. +The object regression suite passed 79 tests. The demo intentionally does not +claim pruning, garbage collection, delta compression, or a pack CLI. diff --git a/docs/packfiles.md b/docs/packfiles.md index 09e6554..1813d93 100644 --- a/docs/packfiles.md +++ b/docs/packfiles.md @@ -5,6 +5,10 @@ through `read_object`, and returns `(pack_path, index_path)`. Loose originals remain in place; new objects are written loose. Pruning and delta compression are not implemented. +Packfiles are currently a Python API, not a CLI command. The supported entry +point is `ObjectStore.pack_objects`; `hash-object` and `cat-file` continue to +operate on individual objects. + ```python from minigit.objects import ObjectStore @@ -45,6 +49,14 @@ its type, zlib end-of-stream, uncompressed length, and hash. Duplicate writes an `has_object` use the same validation and work for packed-only objects. An invalid published index raises `ObjectCorruptError`; it is not silently ignored. +After validation, an `ObjectStore` instance caches the parsed index together +with the index and pack modification times and sizes. Every lookup still +enumerates the current `pack-*.idx` files, so an existing store discovers a +newly published pack. A changed published file invalidates its cached metadata +and repeats full validation; the selected record is then decoded and hash +checked on every read. The cache stores metadata, not object payloads, and is +discarded when the store instance is discarded. + ## Publication and compatibility Both temporary files are closed and validated before publication. The pack is @@ -56,3 +68,9 @@ an orphaned pack without an index is ignored. Loose originals remain readable. History, checkout, diff, merge, and networking use the same object API for loose, packed, and mixed storage. Packfiles are a local storage format: push continues to transfer validated individual objects through the existing protocol. + +## Readiness limitations + +The current format retains loose originals after packing, has no pruning or +garbage collection, and does not use delta compression. The index and pack +formats are version 1 and are intentionally unchanged by the storage demo. diff --git a/docs/performance.md b/docs/performance.md index 0fe1c11..6b1021a 100644 --- a/docs/performance.md +++ b/docs/performance.md @@ -69,3 +69,28 @@ most of its time is the Myers diff, which was not changed. ## Re-run Run these numbers again with the shared benchmark (#47) once it is merged. + +## Object-storage measurements + +The Week 8 shared fixture uses 512 deterministic blobs with 2,048-byte +payloads. On Python 3.11.9 and Windows 10 build 26200, the cached run recorded: + +| Measurement | Result | +| --- | ---: | +| Loose object write time | 1,972.382 ms | +| Loose stored bytes | 27,534 | +| Loose fresh-store read time | 7,278.564 ms | +| Pack time | 128.798 ms | +| Retained loose bytes after packing | 27,534 | +| Pack bytes | 33,179 | +| Index bytes | 18,444 | +| Packed-only retained loose bytes | 0 | +| Packed-only fresh-store read time | 190.982 ms | + +The controlled `--no-cache` comparison took 5,017.308 ms for the packed-only +fresh-store read. Three repeated packed reads took 28.802 seconds without the +metadata cache and 0.688 seconds with it. These results are fixture-specific +measurements, not performance guarantees. The cache avoids repeated complete +pack validation but does not cache object payloads; loose-file latency, +filesystem discovery, retained loose originals, pruning, and delta compression +remain separate concerns. diff --git a/docs/writeups/module-1.md b/docs/writeups/module-1.md new file mode 100644 index 0000000..e0ad244 --- /dev/null +++ b/docs/writeups/module-1.md @@ -0,0 +1,122 @@ +# Module 1: object storage + +## What was built + +Module 1 owns every read and write under `.minigit/objects/`. `ObjectStore` +provides the stable boundary used by trees, commits, the working tree, and +networking: + +- `hash_object(data, type)` computes an object ID without writing. +- `write_object(data, type)` validates duplicate objects and atomically writes + compressed loose storage. +- `read_object(hash)` returns `(type, bytes)` with loose-first lookup. +- `write_tree` and `read_tree` serialize and validate nested tree entries. +- `pack_objects(hashes)` creates and publishes a local pack/index pair. + +The public object API and SHA-1 IDs are unchanged by the packfile work. + +## Object framing and hashing + +An object ID is the SHA-1 digest of: + +```text + \0 +``` + +The length is measured in bytes, not characters, so binary and UTF-8 payloads +are addressed consistently. A loose object stores the same header and payload +inside one zlib stream at: + +```text +.minigit/objects// +``` + +Writes use a temporary file in the destination directory, flush the compressed +bytes, and publish with `os.replace`. A failed write removes its temporary +file. Rewriting an existing object is not accepted blindly: duplicate writes +read and validate the existing object first. + +## Trees and commits + +Trees are object payloads containing one newline-terminated entry per line: + +```text + \t\n +``` + +Entries are sorted by name and validated for supported modes, object types, +lowercase SHA-1 hashes, unique names, and safe filenames. A nested tree points +to another tree object, while a blob entry points to file bytes. + +A commit stores the root tree hash, zero or more parent hashes, author +metadata, and the message. Consequently, a commit points to the root tree, +the root tree points through subtrees to blobs, and the complete snapshot is +reachable through object IDs. Two-parent commits preserve both parent links. + +## Pack layout and lookup + +`pack_objects` reads selected objects through `read_object`, deduplicates the +requested hashes, and writes version-1 `.pack` and `.idx` files under +`.minigit/objects/pack/`. Each pack record preserves the object type and exact +uncompressed bytes, while zlib compresses the payload. The index maps each raw +20-byte SHA-1 to a record offset and length. + +Publication is deliberately ordered: the temporary pack and index are flushed, +fsynced, and validated; the pack is renamed first; the index is renamed last. +Readers discover only published indexes. An interrupted publication can leave +an orphan pack, but cannot make an unpublished index visible. Loose originals +remain readable and are not pruned. + +Reads check loose storage first. If no loose file exists, each published index +is validated before a matching packed record is decoded. The per-store index +metadata cache avoids repeating complete pack validation after the first +validated read. Current pack/index metadata is still discovered on every lookup, +and changed files invalidate the cache. Selected records remain decompressed, +length-checked, and hash-checked on every read. + +## Error behavior and tradeoffs + +- An invalid or missing hash raises `ObjectNotFoundError`. +- A missing object raises `ObjectNotFoundError`. +- Malformed compressed bytes, headers, lengths, types, hashes, indexes, or packs + raise `ObjectCorruptError`. +- A corrupt loose object never falls back to a valid packed copy. +- A corrupt published index or pack is not silently skipped. + +The design favors correctness and simple recovery over aggressive storage +reclamation. It keeps loose originals, has no pruning or garbage collection, +does not use delta compression, and retains a simple version-1 format. The +metadata cache reduces repeated parsing and decompression work but does not +cache object payloads, so loose-file latency and filesystem discovery remain +costs. + +## Measured results + +The shared Week 8 fixture used 512 deterministic blobs with 2,048-byte +payloads on Python 3.11.9 and Windows 10 build 26200. The recorded cached run +reported: + +| Measurement | Result | +| --- | ---: | +| Loose object write time | 1,972.382 ms | +| Loose stored bytes | 27,534 | +| Loose fresh-store read time | 7,278.564 ms | +| Pack time | 128.798 ms | +| Retained loose bytes after packing | 27,534 | +| Pack bytes | 33,179 | +| Index bytes | 18,444 | +| Packed-only retained loose bytes | 0 | +| Packed-only fresh-store read time | 190.982 ms | + +The controlled uncached comparison took 5,017.308 ms for the packed-only +fresh-store read. Three repeated packed reads took 28.802 seconds uncached +versus 0.688 seconds with cached metadata. These are measurements from the +shared fixture, not guarantees for every filesystem or repository size. + +## What I would change next + +First, measure cache memory and lookup behavior with many published packs before +choosing a cache bound or eviction policy. Next, add explicit lifecycle tooling +for pruning loose originals and obsolete packs only after reachability and +crash-recovery rules are specified. Delta compression and a format redesign +would be separate compatibility decisions, not incremental demo work.