Skip to content

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:

from crystalia_data_model.types import mint_md5, rollup_dir, record_survey, mint_catalog, validate
  • 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 the Kind, Grade, Robustness and CoverageRule enums.
  • Validation: validate, ensure_valid.
  • Errors: CrystaliaModelError and its subclasses, and ViolationCode.
  • Leaf and folder descriptors: the fact leaves, mint_md5, mint_md5_region, region_length, mint_glimpse_content, the older mint_glimpse* and mint_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

  1. Mint, never construct. Every id comes from a mint_* helper, rollup_dir, record_survey or mint_catalog. Never build an id by hand.
  2. Validate at both ends. Call ensure_valid(d) before writing a descriptor. Call validate(d) after loading one; skip and report any descriptor that fails.
  3. Store only what belongs to the type. Write no coverage. Write offset only on positional descriptors, and length (the number of bytes hashed) only on hash descriptors.
  4. Ask the API for coverage, robustness and strength. Use get_coverage, coverage_of, get_robustness and is_strong. Robustness and coverage are different things; do not derive one from the other.
  5. Compare types through the registry. Use is_a, grade and is_strong. Never match on the text of hasType.
  6. Judge sameness with comparison-grade types. Use authority-grade types only afterwards, to decide which copy is newer or authoritative. glimpse-content and glimpse-dir-content ignore file name and timestamps, so their ids survive a timestamp-only rewrite. The older glimpse* and glimpse-dir* types include timestamps, so their ids change.
  7. Never publish an incomplete rollup. Pass rollup_dir the entry count from the same listing the survey walked, not from a second listing. If any listed entry cannot be described, the survey fails.
  8. Build rollups with rollup_dir only. It is pure, so the (name, child) pairs may be collected by separate jobs, in any order, and merged in one final call. It raises IncompleteRollup when a child is missing.
  9. Record one survey and one catalog per run. record_survey describes the run and mint_catalog describes what it wrote. mint_catalog sets conformsTo to SCHEMA_IRI; a reader should refuse a catalog whose conformsTo names a version it does not support.
  10. Name items, do not address them. An item's label is its name within its parent. The absolute address of a copy goes in one place: the StorageLocation of the root item.
  11. 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.
  12. 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 the types package, 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.

  1. 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, and validate's IRI check) requires the caller to supply child_names= (the (name, child) entries), typically rebuilt by the reader from the labels of the items it loaded. Without it, validate runs every other check and skips only the IRI check for rollups.
  2. 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-region on a file ≤ 32 KB) has fewer hasDescriptor edges after a round-trip than it has roles. recompute_id (and hence validate) take a resolve= callback and rebuild the full role list from a role-count floor (the number of distinct role child types, not the raw edge count) — pass resolve whenever a loaded descriptor might exhibit this collapse.
  3. mint_bookend does not return the children it mints. Unlike every other composite minter (mint_glimpse*, mint_glimpse_dir*), which take pre-minted head/tail/etc. as parameters, mint_bookend takes raw values (head/tail hex, size, format_id) and mints the md5-region head/tail and the file-size/format leaves internally — but returns only the top-level bookend-strong/bookend-weak composite. A caller writing an RDF graph has no handle on those four child descriptors, only the IRIs referenced inside the composite's hasDescriptor. 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 and mint_bookend's internal ones carry identical IRIs; nothing drifts.