Consumer Contract¶
The rules a tool must follow to write or read catalogs in the
Crystalia data model, and the Python API that implements them. It is written for
authors of collectors, viewers and other tools built on the
crystalia-data-model package. The
Data Dictionary describes the classes and slots; this page describes how to use
them correctly.
The API¶
Every public name is importable from crystalia_data_model.types:
- Identity:
canonical_bytes,mint_leaf,mint_positional,mint_composite,recompute_id,hex_of,iri_of_hex,nfc. - Type registry:
TypeEntry,Registry,type_of,ancestors,is_a,grade,is_strong,default_registry, and theKind,Grade,RobustnessandCoverageRuleenums. - Validation:
validate,ensure_valid. - Errors:
CrystaliaModelErrorand its subclasses, andViolationCode. - Leaf and folder descriptors: the fact leaves,
mint_md5,mint_md5_region,region_length,mint_glimpse_content, the oldermint_glimpse*andmint_glimpse_dir*helpers,rollup_dir. - Coverage and robustness:
get_coverage,coverage_of,literal_coverage,get_robustness,comparable,mint_md5_chunked,asserted,ObjectContext,register_region_rule. - Formats, bookends and zip archives:
detect_format,unverified_rules,judge_bookend,mint_bookend,mint_bookend_dir,mint_zip_cd,mint_zip_manifest,parse_central_directory. - Provenance:
mint_method,canonical_parameters,mint_collector,mint_operator,mint_software_agent,mint_role,mint_association,mint_host,mint_storage_location,record_survey,mint_catalog,SCHEMA_IRI. - Inference:
infer,InferenceRule,DEFAULT_RULES,cross_algorithm_rule.
Rules for writers and readers¶
- Mint, never construct. Every id comes from a
mint_*helper,rollup_dir,record_surveyormint_catalog. Never build an id by hand. - Validate at both ends. Call
ensure_valid(d)before writing a descriptor. Callvalidate(d)after loading one; skip and report any descriptor that fails. - Store only what belongs to the type. Write no
coverage. Writeoffsetonly on positional descriptors, andlength(the number of bytes hashed) only on hash descriptors. - Ask the API for coverage, robustness and strength. Use
get_coverage,coverage_of,get_robustnessandis_strong. Robustness and coverage are different things; do not derive one from the other. - Compare types through the registry. Use
is_a,gradeandis_strong. Never match on the text ofhasType. - Judge sameness with comparison-grade types. Use authority-grade types only afterwards, to
decide which copy is newer or authoritative.
glimpse-contentandglimpse-dir-contentignore file name and timestamps, so their ids survive a timestamp-only rewrite. The olderglimpse*andglimpse-dir*types include timestamps, so their ids change. - Never publish an incomplete rollup. Pass
rollup_dirthe entry count from the same listing the survey walked, not from a second listing. If any listed entry cannot be described, the survey fails. - Build rollups with
rollup_dironly. It is pure, so the(name, child)pairs may be collected by separate jobs, in any order, and merged in one final call. It raisesIncompleteRollupwhen a child is missing. - Record one survey and one catalog per run.
record_surveydescribes the run andmint_catalogdescribes what it wrote.mint_catalogsetsconformsTotoSCHEMA_IRI; a reader should refuse a catalog whoseconformsTonames a version it does not support. - Name items, do not address them. An item's
labelis its name within its parent. The absolute address of a copy goes in one place: theStorageLocationof the root item. - A new algorithm is a new name. A changed algorithm or parameter set gets a new method or type name. Names are never reused with a different meaning.
- Ship the whole model. A tool that bundles the model includes all five schema files
(
crystalia_base,dcterms,schemaorg,prov,linkml_crystalia) together with the generated classes and thetypespackage, unchanged.
The package holds a catalog in memory and does not index it. A tool that handles more than about a million files needs its own streaming writer and indexes.
How descriptors hang off an item¶
An Item (file or directory) carries a flat list in hasDescriptor: one top-level descriptor
per method the collector ran on it, as siblings — never nested inside one another. Only that
top-level descriptor's own children (below) recurse; they are not also linked from the Item
directly. Item.label is the item's name within its parent; it is independent of any filename
child a method may emit.
relpath is optional and stands alone. When a tool writes it (mint_relpath), it is its own
leaf, attached directly to Item.hasDescriptor as a sibling of the content descriptors. It is
never a role inside glimpse, bookend or any other composite. Its value depends on where the
survey started, so the names along isPartOf remain the record of where an item sits. A file
Item that has glimpse-content, bookend-strong and a relpath therefore has three top-level
descriptors in its hasDescriptor list, not one.
Per-file methods (top-level descriptor hangs directly off the file Item):
| Method | Kind | Grade | Children (role → child type) |
|---|---|---|---|
glimpse-content |
field composite | comparison | file-size, head→md5-region (no filename/ctime/mtime) |
glimpse |
field composite | authority | filename, file-size, head→md5-region, ctime, mtime |
glimpse-slim |
field composite | authority | filename, file-size, mtime, head→md5-region |
glimpse-light |
field composite | authority | file-size, head→md5-region, mtime |
glimpse-meta |
field composite | authority | filename, file-size, mtime (no content hash) |
bookend-strong / bookend-weak |
field composite | comparison | head→md5-region, tail→md5-region, format, file-size (fixed 16 KB + 16 KB; head/tail may be the same md5-region IRI on a file ≤ 32 KB — see round-trip note 2 below) |
zip-cd |
leaf | authority | none — value = md5(raw central directory), length = CD byte count |
zip-manifest |
leaf | comparison | none — value = md5(sorted NFC member name, CRC-32, size); no length |
md5 |
leaf | comparison | none |
md5-region |
positional | comparison | none (a block hash; position is in its own identity) |
md5-chunked |
region composite | comparison | unnamed md5-region children ordered by offset, total = chunk count |
Per-directory methods (top-level descriptor hangs directly off the directory Item; children
are the top-level descriptors of the same method already minted for each entry — a folder does
not re-derive its children's shape):
| Method | Kind | Grade | Own fields | Named children |
|---|---|---|---|---|
glimpse-dir-content |
rollup | comparison | none | each entry's glimpse-content (or sub-glimpse-dir-content) |
glimpse-dir |
rollup | authority | filename, mtime, count |
each entry's glimpse |
glimpse-dir-slim |
rollup | authority | filename, mtime |
each entry's glimpse-slim |
glimpse-dir-light |
rollup | authority | mtime |
each entry's glimpse-light |
glimpse-dir-meta |
field composite (not a rollup — no per-entry children) | authority | filename, mtime, count |
— |
bookend-dir-strong / bookend-dir-weak |
rollup | comparison | none | per entry: zip-manifest (zip with a readable CD), else bookend-*, else sub-bookend-dir-* |
Rollups (order=named) carry no fixed roles and no stored total — rollup_dir takes the
expected count from the survey's own listing and raises IncompleteRollup rather than minting a
partial rollup (rule 7). md5-chunked is the one region composite that does store
total, since a stream/chunk set can be partial by design, not by failure.
Round-trip notes¶
Three properties of RDF and of the helpers that a reader or writer has to allow for.
- Rollup identity needs a name map. A rollup's stored children (
hasDescriptor) are content IRIs only — RDF does not carry the(name, child)pairs back out. Re-deriving a rollup's IRI after a round-trip (recompute_id, andvalidate's IRI check) requires the caller to supplychild_names=(the(name, child)entries), typically rebuilt by the reader from the labels of the items it loaded. Without it,validateruns every other check and skips only the IRI check for rollups. - RDF collapses duplicate child edges. A composite where two roles resolve to the same child
IRI (e.g. a bookend whose head and tail are the same
md5-regionon a file ≤ 32 KB) has fewerhasDescriptoredges after a round-trip than it has roles.recompute_id(and hencevalidate) take aresolve=callback and rebuild the full role list from a role-count floor (the number of distinct role child types, not the raw edge count) — passresolvewhenever a loaded descriptor might exhibit this collapse. mint_bookenddoes not return the children it mints. Unlike every other composite minter (mint_glimpse*,mint_glimpse_dir*), which take pre-mintedhead/tail/etc. as parameters,mint_bookendtakes raw values (head/tail hex,size,format_id) and mints themd5-regionhead/tail and thefile-size/formatleaves internally — but returns only the top-levelbookend-strong/bookend-weakcomposite. A caller writing an RDF graph has no handle on those four child descriptors, only the IRIs referenced inside the composite'shasDescriptor. Work around it by minting the same four children yourself and writing them alongside the composite — minting is content-addressed, so your independently-minted leaves andmint_bookend's internal ones carry identical IRIs; nothing drifts.