Files
satellite-provider/SatelliteProvider.DataAccess/Repositories/TileRepository.cs
T
Oleksandr Bezdieniezhnykh c646aa93e2
ci/woodpecker/push/01-test Pipeline was successful
ci/woodpecker/push/02-build-push Pipeline was successful
[AZ-503] Tile identity → UUIDv5 + integer UPSERT (foundation)
Foundation half of original AZ-503 (split during /autodev step 10 batch 2
on user choice; deferred work moved to AZ-505 with a Blocks link).

Adds deterministic tile identity (UUIDv5 over (z, x, y, source, flight_id))
shared cross-repo with gps-denied-onboard via the pinned TileNamespace
5b8d0c2e-7f1a-4d3b-9c5e-1f3a8e7d2b6c, switches the tiles UPSERT key from
floats to integers with per-flight separation, plumbs FlightId through
UavTileMetadata + handler, and writes UAV evidence to per-flight
on-disk directories so two flights at the same (z, x, y) coexist.

- Common: pure-C# RFC 9562 Uuidv5 (no third-party dep) + FlightId DTO
  field; 10 Python-reference unit vectors verify byte parity.
- DataAccess: migration 014 adds flight_id (uuid NULL), location_hash
  (uuid NOT NULL, backfilled via session-scoped pg_temp.uuidv5),
  content_sha256 (bytea NULL), legacy_id (uuid NULL = preserves
  pre-AZ-503 random id one cycle); drops idx_tiles_unique_location_source
  (AZ-484) and adds idx_tiles_unique_identity keyed on
  (tile_zoom, tile_x, tile_y, tile_size_meters, source,
   COALESCE(flight_id, '00000000-...'::uuid)) + idx_tiles_location_hash.
- TileRepository: ColumnList + UPSERT updated; id never updated on
  conflict (preserves AC-2 idempotence). UpdateAsync extended.
- Services: TileService and UavTileUploadHandler compute deterministic
  Id + LocationHash + ContentSha256 before insert; UAV file path
  becomes ./tiles/uav/{flight_id or 'none'}/{z}/{x}/{y}.jpg.
- Tests: Uuidv5Tests (10 reference vectors), UavTileFilePathTests
  (per-flight + anonymous paths), UavTileUploadHandlerTests (AC-2,
  AC-3, AC-7, AC-11 unit-level), UavUploadTests (AC-3 + AC-4
  integration: multi-flight DB coexistence with shared location_hash
  + distinct file_path; float-different lat/lon collapse to 1 row),
  MigrationTests (column shape, idx_tiles_unique_identity supersedes
  AZ-484 index, deterministic backfill).
- IntegrationTests project references Common to reuse Uuidv5 in raw
  SQL seeds.
- AZ-488 MultiSourceCoexistence seed fixed to populate location_hash
  (otherwise migration 014's NOT NULL constraint fails).

ACs covered: AC-1, AC-2, AC-3, AC-4, AC-7, AC-8, AC-11.
ACs deferred to AZ-505: AC-5, AC-6, AC-9, AC-10, AC-12.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-12 17:07:35 +03:00

183 lines
8.3 KiB
C#

using System.Diagnostics;
using Dapper;
using Microsoft.Extensions.Logging;
using Npgsql;
using SatelliteProvider.Common.Configs;
using SatelliteProvider.Common.Utils;
using SatelliteProvider.DataAccess.Models;
namespace SatelliteProvider.DataAccess.Repositories;
public class TileRepository : ITileRepository
{
private const int SlowQueryThresholdMs = 500;
private const string ColumnList = @"id, tile_zoom as TileZoom, tile_x as TileX, tile_y as TileY,
latitude, longitude,
tile_size_meters as TileSizeMeters, tile_size_pixels as TileSizePixels,
image_type as ImageType, maps_version as MapsVersion, version,
file_path as FilePath, source, captured_at as CapturedAt,
created_at as CreatedAt, updated_at as UpdatedAt,
flight_id as FlightId, location_hash as LocationHash,
content_sha256 as ContentSha256, legacy_id as LegacyId";
private readonly string _connectionString;
private readonly ILogger<TileRepository> _logger;
public TileRepository(string connectionString, ILogger<TileRepository> logger)
{
_connectionString = connectionString;
_logger = logger;
}
public async Task<TileEntity?> GetByIdAsync(Guid id)
{
using var connection = new NpgsqlConnection(_connectionString);
const string sql = $@"
SELECT {ColumnList}
FROM tiles
WHERE id = @Id";
return await connection.QuerySingleOrDefaultAsync<TileEntity>(sql, new { Id = id });
}
public async Task<TileEntity?> GetByTileCoordinatesAsync(int tileZoom, int tileX, int tileY)
{
using var connection = new NpgsqlConnection(_connectionString);
// AZ-484 selection rule: most-recent across sources, deterministic tie-break on
// (captured_at DESC, updated_at DESC, id DESC).
const string sql = $@"
SELECT {ColumnList}
FROM tiles
WHERE tile_zoom = @TileZoom AND tile_x = @TileX AND tile_y = @TileY
ORDER BY captured_at DESC, updated_at DESC, id DESC
LIMIT 1";
return await connection.QuerySingleOrDefaultAsync<TileEntity>(sql, new { TileZoom = tileZoom, TileX = tileX, TileY = tileY });
}
public async Task<IEnumerable<TileEntity>> GetTilesByRegionAsync(double latitude, double longitude, double sizeMeters, int zoomLevel)
{
using var connection = new NpgsqlConnection(_connectionString);
var latRad = latitude * Math.PI / 180.0;
var metersPerPixel = (GeoUtils.EarthEquatorialCircumferenceMeters * Math.Cos(latRad)) / (Math.Pow(2, zoomLevel) * MapConfig.DefaultTileSizePixels);
var tileSizeMeters = metersPerPixel * MapConfig.DefaultTileSizePixels;
var expandedSizeMeters = sizeMeters + (tileSizeMeters * 2);
var latRange = expandedSizeMeters / GeoUtils.MetersPerDegreeLatitude;
var lonRange = expandedSizeMeters / (GeoUtils.MetersPerDegreeLatitude * Math.Cos(latitude * Math.PI / 180.0));
// AZ-484 selection rule: at most one row per (lat, lon, zoom, size) cell, picking
// the most-recent across sources via DISTINCT ON, with deterministic tie-break on
// (captured_at DESC, updated_at DESC, id DESC). The outer ORDER BY restores the
// pre-AZ-484 caller-facing order (latitude DESC, longitude ASC); the pre-AZ-484
// updated_at DESC tiebreak is unreachable here because DISTINCT ON already
// guarantees one row per (latitude, longitude, ...) tuple.
const string sql = $@"
SELECT * FROM (
SELECT DISTINCT ON (latitude, longitude, tile_zoom, tile_size_meters)
{ColumnList}
FROM tiles
WHERE latitude BETWEEN @MinLat AND @MaxLat
AND longitude BETWEEN @MinLon AND @MaxLon
AND tile_zoom = @TileZoom
ORDER BY latitude, longitude, tile_zoom, tile_size_meters,
captured_at DESC, updated_at DESC, id DESC
) deduped
ORDER BY latitude DESC, longitude ASC";
var stopwatch = Stopwatch.StartNew();
var tiles = await connection.QueryAsync<TileEntity>(sql, new
{
MinLat = latitude - latRange / 2,
MaxLat = latitude + latRange / 2,
MinLon = longitude - lonRange / 2,
MaxLon = longitude + lonRange / 2,
TileZoom = zoomLevel
});
stopwatch.Stop();
if (stopwatch.ElapsedMilliseconds > SlowQueryThresholdMs)
{
_logger.LogWarning(
"Slow GetTilesByRegionAsync: {ElapsedMs} ms (threshold {ThresholdMs} ms) for lat={Latitude}, lon={Longitude}, sizeMeters={SizeMeters}, zoom={ZoomLevel}",
stopwatch.ElapsedMilliseconds, SlowQueryThresholdMs, latitude, longitude, sizeMeters, zoomLevel);
}
return tiles;
}
public async Task<Guid> InsertAsync(TileEntity tile)
{
using var connection = new NpgsqlConnection(_connectionString);
// AZ-503: integer-keyed UPSERT with per-flight separation. The conflict key
// is (tile_zoom, tile_x, tile_y, tile_size_meters, source, COALESCE(flight_id, '00...0')).
// Two UAV flights uploading the same (z, x, y) cell coexist as distinct rows
// because their flight_id values differ; legacy/google_maps rows collapse on
// the zero-UUID coalesce, preserving AZ-484 single-row-per-cell semantics for
// those producers. Float-based latitude/longitude is no longer part of the key
// so independently-rounded center coords always converge on the same row.
//
// `id` is deliberately NOT updated on conflict — legacy random ids and AZ-503
// deterministic ids both stay stable, matching AC-2 ("the `id` column is not
// regenerated").
const string sql = @"
INSERT INTO tiles (id, tile_zoom, tile_x, tile_y, latitude, longitude, tile_size_meters,
tile_size_pixels, image_type, maps_version, version, file_path,
source, captured_at, created_at, updated_at,
flight_id, location_hash, content_sha256, legacy_id)
VALUES (@Id, @TileZoom, @TileX, @TileY, @Latitude, @Longitude, @TileSizeMeters,
@TileSizePixels, @ImageType, @MapsVersion, @Version, @FilePath,
@Source, @CapturedAt, @CreatedAt, @UpdatedAt,
@FlightId, @LocationHash, @ContentSha256, @LegacyId)
ON CONFLICT (tile_zoom, tile_x, tile_y, tile_size_meters, source,
COALESCE(flight_id, '00000000-0000-0000-0000-000000000000'::uuid))
DO UPDATE SET
file_path = EXCLUDED.file_path,
latitude = EXCLUDED.latitude,
longitude = EXCLUDED.longitude,
captured_at = EXCLUDED.captured_at,
updated_at = EXCLUDED.updated_at,
content_sha256 = EXCLUDED.content_sha256
RETURNING id";
return await connection.ExecuteScalarAsync<Guid>(sql, tile);
}
public async Task<int> UpdateAsync(TileEntity tile)
{
using var connection = new NpgsqlConnection(_connectionString);
const string sql = @"
UPDATE tiles
SET tile_zoom = @TileZoom,
tile_x = @TileX,
tile_y = @TileY,
latitude = @Latitude,
longitude = @Longitude,
tile_size_meters = @TileSizeMeters,
tile_size_pixels = @TileSizePixels,
image_type = @ImageType,
maps_version = @MapsVersion,
version = @Version,
file_path = @FilePath,
source = @Source,
captured_at = @CapturedAt,
updated_at = @UpdatedAt,
flight_id = @FlightId,
location_hash = @LocationHash,
content_sha256 = @ContentSha256
WHERE id = @Id";
return await connection.ExecuteAsync(sql, tile);
}
public async Task<int> DeleteAsync(Guid id)
{
using var connection = new NpgsqlConnection(_connectionString);
const string sql = "DELETE FROM tiles WHERE id = @Id";
return await connection.ExecuteAsync(sql, new { Id = id });
}
}