Production code:
- POST /api/satellite/tiles/inventory (XOR body, 5000-cap,
most-recent-per-location_hash select, present/absent shaping).
- Kestrel HttpProtocols.Http1AndHttp2 on every listener (AC-5).
- Migration 015 creates tiles_leaflet_path covering index over
(location_hash, captured_at DESC, updated_at DESC, id DESC)
INCLUDE (file_path, source); drops superseded idx_tiles_location_hash.
- TileRepository.GetByTileCoordinatesAsync rewired to filter by
location_hash (Index Only Scan via tiles_leaflet_path).
- TileRepository.GetTilesByLocationHashesAsync added with Npgsql-
direct ANY($1::uuid[]) binding (Dapper IEnumerable expansion is
incompatible with the array form).
- Uuidv5.LocationHashForTile centralises the UUIDv5(TileNamespace,
"{z}/{x}/{y}") formula — single source of truth for the cross-repo
invariant (gps-denied-onboard parity).
Contracts:
- New: contracts/api/tile-inventory.md v1.0.0.
- Bumped: contracts/data-access/tile-storage.md to v2.0.0 (joint
ownership by AZ-503-foundation + AZ-505: schema + covering index +
GetByTileCoordinatesAsync rewrite).
Tests:
- TileInventoryTests covers AC-1, AC-2 (DB-level), AC-4, AC-6.
- Http2MultiplexingTests covers AC-5 (20 concurrent multiplexed GETs
over h2c via SocketsHttpHandler + AppContext Http2Unencrypted switch).
- LeafletPathIndexOnlyTests covers AC-3 (EXPLAIN (ANALYZE, BUFFERS)
asserts Index Only Scan over tiles_leaflet_path with heap_blocks=0).
Docs:
- architecture.md, system-flows.md, data_model.md, module-layout.md,
glossary.md, modules/api_program.md, modules/dataaccess_tile_repository.md,
components/02_data_access/description.md all updated to reference the
v2.0.0 tile-storage contract + new tile-inventory contract + AC-7.
Reports:
- batch_01_cycle6_report.md, batch_01_cycle6_review.md,
implementation_completeness_cycle6_report.md (PASS),
implementation_report_tile_inventory_cycle6.md.
Task spec moved todo/ -> done/.
Co-authored-by: Cursor <cursoragent@cursor.com>
6.4 KiB
Module: DataAccess/Repositories/TileRepository
Purpose
Dapper-based repository for the tiles table. Handles CRUD operations and spatial queries for satellite tile records.
Public Interface
ITileRepository (interface)
GetByIdAsync(Guid id) → Task<TileEntity?>GetByTileCoordinatesAsync(int tileZoom, int tileX, int tileY) → Task<TileEntity?>: finds the most-recent tile across all sources / flights for the given slippy coordinates. AZ-505 rewired the predicate to computelocation_hash = Uuidv5(TileNamespace, "{z}/{x}/{y}")and filter byWHERE location_hash = $1so the read becomes anIndex Only Scanagainsttiles_leaflet_path. Selection rule preserved unchanged:ORDER BY captured_at DESC, updated_at DESC, id DESC LIMIT 1(v2.0.0 contract).GetTilesByRegionAsync(double lat, double lon, double sizeMeters, int zoomLevel) → Task<IEnumerable<TileEntity>>: spatial bounding box query (expanded by 2 × tile size to cover edges); appliesDISTINCT ON (latitude, longitude, tile_zoom, tile_size_meters)per AZ-484 to return at most one row per cell — the most-recent across sources — preserving the historical caller-facing orderlatitude DESC, longitude ASC.GetTilesByLocationHashesAsync(IReadOnlyList<Guid> locationHashes) → Task<IReadOnlyDictionary<Guid, TileEntity>>(AZ-505): bulk lookup keyed bylocation_hashfor the inventory endpoint. Single round-trip;DISTINCT ON (location_hash) ... WHERE location_hash = ANY($1::uuid[]) ORDER BY location_hash, captured_at DESC, updated_at DESC, id DESC. NOT routed through Dapper — usesNpgsqlCommandwith an explicitNpgsqlParametertypedArray | Uuidbecause Dapper's parameter expander rewritesIEnumerableto scalar placeholders, which is invalid againstANY($1::uuid[]).InsertAsync(TileEntity tile) → Task<Guid>: AZ-503 integer-only + flight-aware UPSERT —ON CONFLICT (tile_zoom, tile_x, tile_y, tile_size_meters, source, COALESCE(flight_id, '00000000-0000-0000-0000-000000000000'::uuid)) DO UPDATE file_path, latitude, longitude, captured_at, location_hash, content_sha256, updated_at.idis intentionally NOT overwritten on conflict — preserves AZ-503 AC-2 idempotence (same inputs ⇒ sameid). Supersedes the AZ-484 5-column float-based unique key (idx_tiles_unique_location_source).UpdateAsync(TileEntity tile) → Task<int>: full row update byidincludingsource,captured_at,flight_id,location_hash, andcontent_sha256.DeleteAsync(Guid id) → Task<int>
TileRepository (implementation)
Constructs a new NpgsqlConnection per method call (no connection pooling at the repository level; Npgsql pools connections internally). Logs a Slow GetTilesByRegionAsync warning when the region query exceeds 500 ms.
Internal Logic
GetTilesByRegionAsynccalculates a bounding box by expanding the requested region by 2 × tile size to ensure edge tiles are included. Uses meters-to-degrees approximation viaGeoUtils(post-AZ-377 — single source of truth for Earth constants).InsertAsyncuses the AZ-503 integer-only + flight-aware UPSERT keyed onidx_tiles_unique_identity(created by migration 014, replacing the AZ-484idx_tiles_unique_location_source). The conflict key usesCOALESCE(flight_id, '00000000-0000-0000-0000-000000000000'::uuid)so anonymous (flight_id IS NULL) and per-flight UAV rows share a flat key space. Two producers (google_maps+uav) at the same cell with the sameflight_id(typicallyNULLforgoogle_maps) still coexist viasourcediscrimination. Same-source same-flight re-insert refreshesfile_path,latitude,longitude,captured_at,location_hash,content_sha256,updated_at— but NOTid(idempotence — AZ-503 AC-2).GetByTileCoordinatesAsyncandGetTilesByRegionAsyncapply the AZ-484 selection rule unchanged: most-recent across sources, deterministic tie-break on(captured_at DESC, updated_at DESC, id DESC). AZ-505 rewroteGetByTileCoordinatesAsyncto filter onlocation_hashinstead of(tile_zoom, tile_x, tile_y)— this is a behavior-preserving rewrite (deterministic UUIDv5) that enablesIndex Only Scanagainsttiles_leaflet_path.GetTilesByRegionAsyncretains the lat/lon filter because spatial bounding-box queries don't reduce to a finite hash-set lookup.GetTilesByLocationHashesAsync(AZ-505) is the inventory hot path. Deliberately bypasses Dapper because theANY($1::uuid[])predicate requires array-typed parameter binding (NpgsqlNpgsqlDbType.Array | Uuid) that Dapper'sIEnumerableparameter expansion replaces with a comma-separated list of scalar placeholders, producing invalid SQL. ManualNpgsqlDataReadermapping toTileEntityis the trade-off. Slow-query threshold matchesGetTilesByRegionAsync(500 ms).TileEntity.Sourceis a plainstringstoring the snake_case wire value ('google_maps'|'uav'); enum<->wire conversion happens viaSatelliteProvider.Common.Enums.TileSourceConverter. This avoids Dapper issue #259 (TypeHandler bypass for enum reads — see_docs/LESSONS.mdL-001).FindExistingTileAsyncwas removed by AZ-376 (see_docs/04_refactoring/03-code-quality-refactoring/).
Dependencies
- NuGet:
Dapper,Npgsql SatelliteProvider.DataAccess.Models.TileEntitySatelliteProvider.Common.GeoUtils(Earth constants / meters-to-degrees)Microsoft.Extensions.Logging
Contract
Implements the frozen v2.0.0 contract _docs/02_document/contracts/data-access/tile-storage.md — captures the four AZ-503-foundation columns (flight_id, location_hash, content_sha256, legacy_id), the integer-only flight-aware UPSERT key (idx_tiles_unique_identity), the tiles_leaflet_path covering index (AZ-505 migration 015), the new location_hash-keyed GetByTileCoordinatesAsync read path (AZ-505), and the new GetTilesByLocationHashesAsync bulk lookup (AZ-505). Inv-1..Inv-9 from the v2.0.0 contract apply.
Consumers
TileService— all read/write operations includingGetInventoryAsync(AZ-505) which routes throughGetTilesByLocationHashesAsyncProgram.cs(ServeTile, GetTileByLatLon,GetTilesInventoryhandlers) —GetByTileCoordinatesAsync,InsertAsync, indirectlyGetTilesByLocationHashesAsyncviaTileService.GetInventoryAsync
Data Models
Operates on TileEntity.
Configuration
Receives connection string via constructor.
External Integrations
PostgreSQL — SQL queries via Dapper + Npgsql.
Security
None.
Tests
No dedicated repository tests.