This page covers an experimental part of DASCore: its API and file formats may change in any minor release without a deprecation period. See what experimental means.
The inventory tutorial covers what an inventory says and the methods that use it. This note covers the machinery in between: when a spool’s inventory is read, and why several things that look like they should read it do not. In short, attaching an inventory and reading it are deliberately separate events, and most of what a spool does happens between them.
A directory that carries its own inventory involves three separate events:
Moment
When
Cost
Discovery
opening the spool
a stat of each spelling
Reading
the first question only an inventory can answer
one parse
Refreshing
when asked, and never otherwise
one parse
Discovery is eager because it has to be: whether the directory carries an inventory is a fact about the directory, and a spool which learned it later would answer the same question two different ways over its lifetime. It is also cheap — find_inventory stats for .inventory/, .inventory.yaml, .inventory.yml, and .inventory.json, and stops.
Reading is lazy because it need not happen at all. A spool opened to count its patches, sort them, chunk them, or extract one has no use for an inventory, and a malformed one must not stop any of that.
spool = dc.spool(archive).update()# Attached, unread: an InventoryRef holds where it is, not what it says.reference = spool._inventoryassert reference._inventory isNone# None of this is a question an inventory can answer.assertlen(spool) ==1_ = spool.get_contents()assert reference._inventory isNone
So a malformed inventory cannot stop you loading data. Only inventory-backed calls fail, and their errors name the directory the inventory came from and say the spool found it on opening, since nobody typed its path.
Deciding without reading
Laziness is only worth having if the decision to read is itself free. A spool asked to select has to know whether the query is one the index can answer, and it settles that without touching the inventory.
It can, because the observing-system facts are the models’ — gauge_length, pulse_width, interrogator.model and the rest are the same set for every inventory that ever existed, so which names an inventory could state is known from dascore.core.inventory alone. Spool._classify_query compares the requested names against three sets: what the index has as attrs, what it has as coordinates, and that fixed vocabulary. Only a name outside all three is a question the inventory must be read to answer.
# A query the index can answer entirely, on a spool with an inventory.start = spool.get_contents()["time_min"].min()assertlen(spool.select(time=(start, None))) ==1assert reference._inventory isNone# `zone` is a label group; nothing but the inventory knows it._ = spool.select(zone="north")assert reference._inventory isnotNone
A name the index already carries keeps the index’s meaning. If an archive has its own latitude coordinate, attaching an inventory that could also place channels in latitude does not move that name into the inventory’s namespace. Otherwise attaching an inventory would silently change what an existing name selects, making it a data-changing operation, which it is designed not to be.
Comparison never reads either side
Two spools compare equal on what they are, and an attachment is a place or a value rather than the document behind it. InventoryRef.__eq__ compares paths; an Inventory compares as itself; a place is never equal to a value.
Comparing the resolved documents was rejected: == would do file I/O, comparing spools could raise on an unreadable inventory, and the answer would depend on whether something had read one first. The same reasoning covers +: combining two spools compares their attachments, and combining must not be the thing which discovers that a file three directories away is malformed.
Read once, with no modification time
An inventory is read once and held. There is no modification-time check, and re-reading never happens behind the caller’s back.
# Change the file under the running program.inventory.new(schema_version=1).io.to_yaml(archive /".inventory.yaml")held = reference._inventory_ = spool.select(zone="north")assert reference._inventory is held # the same object; nothing re-read
This is the opposite of how the spool index behaves, and deliberately so. The index is a cache whose truth is the files, so a file whose modification time has moved is a thing to re-scan. An inventory is an input: a file which changes under a running program is a new input, not a stale one, and quietly adopting it would rewrite metadata on patches which had already been described.
Refreshing is therefore something a program says, not something it gets. attach_inventory() with no argument means “the one this directory carries, read it again”; the same path again means the same thing for one named by hand. That is the whole authoring loop — edit the file, re-attach, see the change.
A read that failed is not held, and is tried again next time, so fixing an unreadable inventory takes effect without rebuilding the spool.
One holder, however the spool is sliced
A spool copy-constructs from its parent — select, sort, and chunk each return a new one — so a naive attachment would be copied along with it, and a spool sliced ten ways would read its inventory ten times.
The holder is shared instead. Every spool descended from one attachment points at the same InventoryRef, whose read is guarded by a lock held across the parse rather than merely around the assignment, so threads mapping over one spool cannot each read a large authoring directory.
sliced = spool.select(time=(start, None))sorted_spool = spool.sort("time")chunked = spool.chunk(time=None)assert sliced._inventory is referenceassert sorted_spool._inventory is referenceassert chunked._inventory is reference
Sharing means a spool sliced any number of ways reads its inventory once. More importantly, two views of one parent can never disagree about it; two slices giving different metadata for one channel would be a hard bug to find.