directory_scan
Find newly added files by comparing directory mtimes.
Creating, deleting or renaming a file bumps its parent directory's mtime and
nothing above it. So a pass that recorded the mtime of every directory can find
every directory whose contents changed with one stat each, and needs to list
only those. On a network share that is the difference between one round trip
per directory and one per file — measured at roughly 5x on a 4970-directory,
20031-file share, where the files contribute 7 round trips instead of 20031.
What this does not see is a file rewritten in place: that changes neither its name nor its parent directory's mtime. Detecting those is the full walk's job, and this is deliberately not a replacement for it.
The scan needs two things from the last pass, both already per-datasource:
directory_metadata rows for the mtimes, and the file_metadata rows that say
which paths were already known.
Module
Functions
find_new_files
def find_new_files( root: str, cache: CacheProtocol, task_hash: str, since: float, *, workers: int | None = None,) ‑> list[str]:Return the paths of files added under root since the last pass.
One stat per known directory, then a listing of only those that moved. A
directory named by a mover's listing but absent from the stored set is new,
so it and everything under it are new in full.
A directory whose stored modified_at is unset — every directory, the
first time, since the index walk does not read mtimes — is judged against
since instead, exactly as _tree_changed_since judges the whole tree.
Either way the observed mtime is written back, so the next scan compares
against the directory's own value and no longer re-lists directories that
merely changed during the last pass. The write-back is free: the stat
that produced it had to be made regardless.
Arguments
root: The datasource root, used as provenance on refreshed rows.cache: Open cache for the pod'sbackground_cache.db.task_hash: The datasource's task hash.since: POSIX timestamp of the last completed pass, used for directories whose own mtime has not been recorded yet. The pass's start, so a file created while it ran is not missed.workers: How manystats and listings to keep in flight. Defaults tofile_metadata_io_concurrency.
Returns The new files' paths, sorted.
outermost
def outermost(paths: Iterable[str | Path]) ‑> list[str]:Return paths with any that sit under another left out, sorted.
Link targets are kept disjoint, so a count summed over them adds no file twice and each covers its own subtree.
Arguments
paths: Normalized directories.
Returns The outermost of them, as strings.
record_walked_directories
def record_walked_directories( root: str, cache: CacheProtocol, task_hash: str, *, run_id: str | None = None, dataset_identifier: str | None = None, datasource_type: str | None = None,) ‑> int:Walk root and store every directory under it, so a scan can find them.
Backfill only. The index pass collects the same set for free through
WalkedDirectories, so this exists for a datasource that was indexed
before directory rows were written and would otherwise have nothing for the
first scan to stat.
Mtimes are not read here, for the same reason the index pass does not read
them: it would cost a stat per directory, and the first scan makes that
stat anyway.
Arguments
root: The datasource root to walk.cache: Open cache for the pod'sbackground_cache.db.task_hash: Provenance column stamped on each row.run_id: TheRunthese rows belong to, when there is one.dataset_identifier: Slash-joined"{pod_name}/{datasource_name}".datasource_type: Datasource type string.
Returns The number of directories recorded.
Classes
LinkTargets
class LinkTargets(root: Path):Collects where a walk's tree leaves itself through a link.
Fed from the walk's on_directory hook with each directory's normalized
spelling, so a directory reached through a link (a symlink, a Windows
junction) arrives as its resolved target. Any directory that does not
resolve to the root or under it is one: the walk only ever descends from
the root, so the tree has left itself there. Links are found at any
depth, and a link inside a link's target is checked against the targets
already found.
file_metadata rows are keyed on the same resolved spelling, so the files
beneath a target are stored there and not under the root; selection reads
these targets to find them.
Called on the walk's pool threads, so the set is guarded.
Arguments
root: The datasource root, normalized.
Variables
root : Path- The normalized root the targets are found for.
Methods
targets
def targets(self) ‑> list[str]:Return the targets found, with any under another left out, sorted.
A walk reports every directory beneath a target, not only the target itself; the outermost one of each covers the rest.
WalkedDirectories
class WalkedDirectories():Collects the directories a walk reached, for the caller to persist.
Fed from the walk's on_directory hook, which scantree calls on its pool
threads, so the set is guarded. Mirrors IndexingStats and WalkSkips:
the walk fills it, the caller decides what to do with it.
Deliberately records paths only, never mtimes. A directory's own mtime
cannot be had for free during a walk — the parent's listing carries no
timestamps on Linux and macOS, and on Windows carries the copy held in the
parent's NTFS index, which is not updated when the subdirectory's contents
change. Reading it would cost one stat per directory on top of the
index, which is the walk we most want to keep cheap.
The scan that consumes this set stats every directory in it anyway, so it
is the side that learns the mtimes, and it writes them back at no extra
cost. Until it has, find_new_files compares against the last pass's start
instead, as _tree_changed_since does.
Methods
as_records
def as_records( self, task_hash: str, *, source_path: str | None = None, dataset_identifier: str | None = None, datasource_type: str | None = None,) ‑> list[DirectoryMetadataRecord]:Return the collected directories as rows ready for the cache.
modified_at is left unset: the walk did not read it, and the first
scan fills it in from the stat it makes anyway.