Skip to content

Python tool for aircraft Doppler audio geolocation using historical ADS-B flight data (Source-available)

perf: cache managed flight index digests (#218)

Cinna Davis55d97b180976
diff --git a/src/dopplergeist/data_access.py b/src/dopplergeist/data_access.py
index c77ed07..4ca9830 100644
--- a/src/dopplergeist/data_access.py
+++ b/src/dopplergeist/data_access.py
@@ -136,6 +136,36 @@ def cache_lock(path):
yield
+def cached_sha256(path):
+ """Reuse a digest receipt for an immutable cache file while its stat identity holds."""
+ path = Path(path)
+ receipt = path.with_name(path.name + ".sha256.json")
+ lock = receipt.with_name(receipt.name + ".lock")
+ with cache_lock(lock):
+ info = path.stat()
+ identity = [info.st_dev, info.st_ino, info.st_size, info.st_mtime_ns, info.st_ctime_ns]
+ try:
+ saved = json.loads(receipt.read_text())
+ digest = saved.get("sha256")
+ if (saved.get("version") == 1 and saved.get("identity") == identity and
+ isinstance(digest, str) and len(digest) == 64 and
+ all(char in "0123456789abcdef" for char in digest)):
+ current = path.stat()
+ current_identity = [current.st_dev, current.st_ino, current.st_size,
+ current.st_mtime_ns, current.st_ctime_ns]
+ if identity == current_identity:
+ return digest
+ identity = current_identity
+ except (OSError, UnicodeError, AttributeError, json.JSONDecodeError):
+ pass
+ digest = sha256(path)
+ after = path.stat()
+ if identity != [after.st_dev, after.st_ino, after.st_size, after.st_mtime_ns, after.st_ctime_ns]:
+ raise OSError(f"File changed while hashing: {path}")
+ write_json(receipt, {"version": 1, "identity": identity, "sha256": digest})
+ return digest
+
+
def download(url, path, *, size=None, digest=None, offline=False, session=None):
"""Resume only verified byte ranges; atomically publish validated files.
diff --git a/src/dopplergeist/tiled_search.py b/src/dopplergeist/tiled_search.py
index 04acebe..ef508e1 100644
--- a/src/dopplergeist/tiled_search.py
+++ b/src/dopplergeist/tiled_search.py
@@ -20,7 +20,7 @@ import numpy as np
import pandas as pd
from pyproj import Transformer
-from .data_access import CONUS, DataUnavailable, content_hash, sha256, write_json
+from .data_access import CONUS, DataUnavailable, cached_sha256, content_hash, sha256, write_json
from .finite_search import RetentionLedger, bounded_assessment, classified_assessment, identify_mode, mode_seed
from .resource_usage import process_peak_rss_kib
from .geometry import AcousticSupportError, ecef
@@ -720,7 +720,7 @@ def search_national(indexes, terrain, events, measurements, *, time_range_s, out
rasters += terrain.geoid.rasters
terrain_signature = {"constant_m": getattr(terrain, "constant", None),
"rasters": {str(r.name): sha256(r.name) for r in rasters}}
- identity = {"indexes": [{"input_hash": i.report["input_hash"], "file_sha256": sha256(i.path)} for i in indexes],
+ identity = {"indexes": [{"input_hash": i.report["input_hash"], "file_sha256": cached_sha256(i.path)} for i in indexes],
"terrain": terrain_signature, "additional_input_hashes": input_hashes or {},
"events": events, "acoustic_hashes": acoustic, "bounds": list(bounds), "time_range_s": list(time_range_s),
"budget": asdict(budget), "scoring": scoring or {}, "filters": filters, "crs": crs, "code": modules,