Wrap up cycle 5 verification + documentation: - Steps 10/11 wrap-up reports (implementation_completeness + implementation_report) for the AZ-503-foundation + AZ-504 batch. - Step 12 test-spec sync: AZ-503-foundation/AZ-504 ACs appended; AZ-505 deferred ACs recorded. - Step 13 update-docs: architecture, data-model, glossary, module- layout, uav-tile-upload contract (v1.1.0), DataAccess + Services + Tests module docs synced; new common_uuidv5.md module doc. - Step 14 security audit: PASS_WITH_WARNINGS; 0 new Critical/High; 2 new Low informational (F1 flightId provenance, F2 pgcrypto deploy gap). - Step 15 performance test: PASS_WITH_INFRA_WARNINGS; PT-08 passed twice (AZ-504 fix verified); PT-01/02 failed due to recurring local Docker/colima DNS cold-start (not an app regression). Cycle-3 perf-harness leftover stays OPEN with replay #5 documented. - Autodev state moved to Step 16 (Deploy). Co-authored-by: Cursor <cursoragent@cursor.com>
4.7 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 for the given slippy coordinates. Selection rule:ORDER BY captured_at DESC, updated_at DESC, id DESC LIMIT 1(AZ-484 v1.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.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-503 does NOT rewrite the read path to uselocation_hash— that's deferred to AZ-505 alongside the Leaflet covering index.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 v1.0.0 contract _docs/02_document/contracts/data-access/tile-storage.md plus the AZ-503-introduced columns (flight_id, location_hash, content_sha256, legacy_id) and the integer-only UPSERT key. Schema invariants Inv-1..Inv-5 (UPSERT semantics, selection rule, source value space) are preserved; the only contract change is that the UPSERT conflict detection no longer depends on bit-identical float latitude/longitude (AZ-503 AC-4).
Consumers
TileService— all read/write operationsProgram.cs(ServeTile, GetTileByLatLon handlers) —GetByTileCoordinatesAsync,InsertAsync
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.