[AZ-505] Tile inventory endpoint + HTTP/2 + Leaflet covering index

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>
This commit is contained in:
Oleksandr Bezdieniezhnykh
2026-05-12 21:16:37 +03:00
parent 3c7cd4e56b
commit 909f69cb3a
26 changed files with 1780 additions and 65 deletions
@@ -143,6 +143,86 @@ public class TileService : ITileService
return MapToMetadata(entity);
}
public async Task<TileInventoryResponse> GetInventoryAsync(TileInventoryRequest request, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(request);
cancellationToken.ThrowIfCancellationRequested();
var tiles = request.Tiles;
var hashes = request.LocationHashes;
// Defensive guards. The HTTP handler rejects these cases with HTTP
// 400 before reaching the service; this preserves the same invariant
// for any future non-HTTP caller (and keeps unit-tests grounded).
var hasTiles = tiles is { Count: > 0 };
var hasHashes = hashes is { Count: > 0 };
if (hasTiles == hasHashes)
{
throw new ArgumentException(
"TileInventoryRequest must populate exactly one of `Tiles` or `LocationHashes` (and not both).",
nameof(request));
}
// Build the (request entry → location_hash) mapping. When the caller
// supplied coords, compute UUIDv5 server-side; when they supplied
// pre-computed hashes, use them verbatim. We keep both representations
// in lockstep so the response can echo the request entry's coord
// triple back to the caller (Tiles input branch) or zero them out
// (LocationHashes input branch).
var entries = new List<(int Zoom, int X, int Y, Guid Hash)>(hasTiles ? tiles!.Count : hashes!.Count);
if (hasTiles)
{
foreach (var coord in tiles!)
{
var hash = Uuidv5.LocationHashForTile(coord.TileZoom, coord.TileX, coord.TileY);
entries.Add((coord.TileZoom, coord.TileX, coord.TileY, hash));
}
}
else
{
foreach (var hash in hashes!)
{
entries.Add((0, 0, 0, hash));
}
}
var distinctHashes = entries.Select(e => e.Hash).Distinct().ToArray();
var rows = await _tileRepository.GetTilesByLocationHashesAsync(distinctHashes);
var results = new List<TileInventoryEntry>(entries.Count);
foreach (var (zoom, x, y, hash) in entries)
{
if (rows.TryGetValue(hash, out var tile))
{
results.Add(new TileInventoryEntry
{
TileZoom = hasTiles ? zoom : tile.TileZoom,
TileX = hasTiles ? x : tile.TileX,
TileY = hasTiles ? y : tile.TileY,
LocationHash = hash,
Present = true,
Id = tile.Id,
CapturedAt = tile.CapturedAt,
Source = tile.Source,
FlightId = tile.FlightId,
ResolutionMPerPx = tile.TileSizePixels > 0 ? tile.TileSizeMeters / tile.TileSizePixels : null
});
}
else
{
results.Add(new TileInventoryEntry
{
TileZoom = zoom,
TileX = x,
TileY = y,
LocationHash = hash,
Present = false
});
}
}
return new TileInventoryResponse { Results = results };
}
private TileEntity BuildTileEntity(DownloadedTileInfoV2 downloaded)
{
var now = DateTime.UtcNow;
@@ -153,10 +233,8 @@ public class TileService : ITileService
// gps-denied-onboard produces byte-identical IDs.
var idName = string.Create(CultureInfo.InvariantCulture,
$"{downloaded.ZoomLevel}/{downloaded.X}/{downloaded.Y}/{source}/{Guid.Empty}");
var locationHashName = string.Create(CultureInfo.InvariantCulture,
$"{downloaded.ZoomLevel}/{downloaded.X}/{downloaded.Y}");
var id = Uuidv5.Create(Uuidv5.TileNamespace, idName);
var locationHash = Uuidv5.Create(Uuidv5.TileNamespace, locationHashName);
var locationHash = Uuidv5.LocationHashForTile(downloaded.ZoomLevel, downloaded.X, downloaded.Y);
// content_sha256 is computed from the actual JPEG body on disk. Google Maps
// downloads land on disk before this method runs (FilePath is set by the
// downloader), so a single read here is safe and avoids re-streaming. If