Files
satellite-provider/_docs/02_document/modules/dataaccess_tile_repository.md
T
Oleksandr Bezdieniezhnykh 51b572108a
ci/woodpecker/push/01-test Pipeline was successful
ci/woodpecker/push/02-build-push Pipeline was successful
[AZ-484] Cycle 1 Steps 12-16: docs, security, perf, deploy report
Captures the post-implementation autodev gates for AZ-484 multi-source
tile storage:

- Step 12 (Test-Spec Sync): added 7 AC rows (AZ-484 AC-1..AC-7) and a
  PT-07 NFR row to traceability-matrix.md; added PT-07 scenario to
  performance-tests.md.
- Step 13 (Update Docs): refreshed data_model.md (tiles columns +
  indexes + selection rule + UPSERT contract + migrations 012/013),
  module-layout.md (Common/Enums section with L-001 guidance,
  DataAccess imports-from now lists 6 sites), 6 module / component
  docs to reflect the new repo signatures, source/captured_at fields,
  and Dapper enum bypass workaround. ripple_log_cycle1.md records
  zero out-of-scope ripple.
- Step 14 (Security Audit): PASS_WITH_WARNINGS - 0 Critical, 0 High,
  5 Medium, 5 Low. AZ-484 itself added zero new findings. Hardening
  items (Postgres default creds, .env in build context, GMaps key
  rotation, ASP.NET Core 8.0.21 -> 8.0.25, rate limiter) recorded
  for separate tickets.
- Step 15 (Performance Test): all PT-01..PT-07 scenarios Unverified
  (non-blocking); PT-07 baseline-comparison harness deferred to a
  leftover for next cycle.
- Step 16 (Deploy): cycle deploy report covering migration safety,
  rollback path, post-deploy verification, security caveats.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-11 10:03:05 +03:00

3.5 KiB
Raw Blame History

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); applies DISTINCT 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 order latitude DESC, longitude ASC.
  • InsertAsync(TileEntity tile) → Task<Guid>: per-source UPSERT — ON CONFLICT (latitude, longitude, tile_zoom, tile_size_meters, source) DO UPDATE file_path, tile_x, tile_y, captured_at, updated_at (AZ-484 5-column unique key).
  • UpdateAsync(TileEntity tile) → Task<int>: full row update by id including source and captured_at.
  • 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

  • GetTilesByRegionAsync calculates a bounding box by expanding the requested region by 2 × tile size to ensure edge tiles are included. Uses meters-to-degrees approximation via GeoUtils (post-AZ-377 — single source of truth for Earth constants).
  • InsertAsync uses a per-source UPSERT pattern keyed on the 5-column unique index idx_tiles_unique_location_source (created by migration 013). Two producers (e.g., google_maps + uav) coexist for the same cell; same-source re-insert refreshes captured_at and updated_at.
  • GetByTileCoordinatesAsync and GetTilesByRegionAsync apply the AZ-484 selection rule: most-recent across sources, deterministic tie-break on (captured_at DESC, updated_at DESC, id DESC).
  • TileEntity.Source is a plain string storing the snake_case wire value ('google_maps' | 'uav'); enum<->wire conversion happens via SatelliteProvider.Common.Enums.TileSourceConverter. This avoids Dapper issue #259 (TypeHandler bypass for enum reads — see _docs/LESSONS.md L-001).
  • FindExistingTileAsync was removed by AZ-376 (see _docs/04_refactoring/03-code-quality-refactoring/).

Dependencies

  • NuGet: Dapper, Npgsql
  • SatelliteProvider.DataAccess.Models.TileEntity
  • SatelliteProvider.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. Schema invariants Inv-1..Inv-5 are enforced here (UPSERT key, selection rule, source value space).

Consumers

  • TileService — all read/write operations
  • Program.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.