mirror of
https://github.com/azaion/satellite-provider.git
synced 2026-06-21 22:11:14 +00:00
581dff206e
AZ-357 — eliminate year-based tile cache expiry (LF-1): - Migration 012: drop 5-col unique index, dedupe by (lat,lon,zoom, size) keeping max(updated_at), add new 4-col unique index, make version column nullable + drop default. Column itself preserved per coderule (column drops require explicit confirmation; tracked in AZ-373 / C20). - TileEntity.Version, TileMetadata.Version, DownloadTileResponse. Version: int -> int? (HTTP shape preserved; field still in JSON). - TileService.DownloadAndStoreTilesAsync: drop currentVersion year computation and the .Where(t => t.Version == currentVersion) cache filter. BuildTileEntity: drop year arg; write Version=null. - TileRepository: ON CONFLICT now 4-col; lookup queries ORDER BY updated_at DESC instead of version DESC. - Tests: replace inverted BT02b with positive AZ357_AC1 (prior-year cached tile is reused). Add BuildTileEntity_ DoesNotPopulateVersion_AZ357 to enforce the no-write contract. - 69 unit + 5 smoke + 3 stub-contract integration tests pass. Cumulative code review (batches 7-9, 7 tasks): VERDICT=PASS. Report at _docs/03_implementation/reviews/batch_09_review.md. Zero Critical/High/Medium/Low findings. Architecture baseline remains clean. Co-authored-by: Cursor <cursoragent@cursor.com>
165 lines
7.0 KiB
C#
165 lines
7.0 KiB
C#
using Dapper;
|
|
using Microsoft.Extensions.Logging;
|
|
using Npgsql;
|
|
using SatelliteProvider.DataAccess.Models;
|
|
|
|
namespace SatelliteProvider.DataAccess.Repositories;
|
|
|
|
public class TileRepository : ITileRepository
|
|
{
|
|
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 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, created_at as CreatedAt, updated_at as UpdatedAt
|
|
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);
|
|
const string sql = @"
|
|
SELECT 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, created_at as CreatedAt, updated_at as UpdatedAt
|
|
FROM tiles
|
|
WHERE tile_zoom = @TileZoom AND tile_x = @TileX AND tile_y = @TileY
|
|
ORDER BY updated_at DESC
|
|
LIMIT 1";
|
|
|
|
return await connection.QuerySingleOrDefaultAsync<TileEntity>(sql, new { TileZoom = tileZoom, TileX = tileX, TileY = tileY });
|
|
}
|
|
|
|
public async Task<TileEntity?> FindExistingTileAsync(double latitude, double longitude, double tileSizeMeters, int zoomLevel, int version)
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString);
|
|
const string sql = @"
|
|
SELECT 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, created_at as CreatedAt, updated_at as UpdatedAt
|
|
FROM tiles
|
|
WHERE ABS(latitude - @Latitude) < 0.0001
|
|
AND ABS(longitude - @Longitude) < 0.0001
|
|
AND ABS(tile_size_meters - @TileSizeMeters) < 1
|
|
AND tile_zoom = @TileZoom
|
|
AND version = @Version
|
|
LIMIT 1";
|
|
|
|
return await connection.QuerySingleOrDefaultAsync<TileEntity>(sql, new
|
|
{
|
|
Latitude = latitude,
|
|
Longitude = longitude,
|
|
TileSizeMeters = tileSizeMeters,
|
|
TileZoom = zoomLevel,
|
|
Version = version
|
|
});
|
|
}
|
|
|
|
public async Task<IEnumerable<TileEntity>> GetTilesByRegionAsync(double latitude, double longitude, double sizeMeters, int zoomLevel)
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString);
|
|
|
|
const double EARTH_CIRCUMFERENCE_METERS = 40075016.686;
|
|
const int TILE_SIZE_PIXELS = 256;
|
|
var latRad = latitude * Math.PI / 180.0;
|
|
var metersPerPixel = (EARTH_CIRCUMFERENCE_METERS * Math.Cos(latRad)) / (Math.Pow(2, zoomLevel) * TILE_SIZE_PIXELS);
|
|
var tileSizeMeters = metersPerPixel * TILE_SIZE_PIXELS;
|
|
|
|
var expandedSizeMeters = sizeMeters + (tileSizeMeters * 2);
|
|
|
|
var latRange = expandedSizeMeters / 111000.0;
|
|
var lonRange = expandedSizeMeters / (111000.0 * Math.Cos(latitude * Math.PI / 180.0));
|
|
|
|
const string sql = @"
|
|
SELECT 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, created_at as CreatedAt, updated_at as UpdatedAt
|
|
FROM tiles
|
|
WHERE latitude BETWEEN @MinLat AND @MaxLat
|
|
AND longitude BETWEEN @MinLon AND @MaxLon
|
|
AND tile_zoom = @TileZoom
|
|
ORDER BY latitude DESC, longitude ASC, updated_at DESC";
|
|
|
|
return await connection.QueryAsync<TileEntity>(sql, new
|
|
{
|
|
MinLat = latitude - latRange / 2,
|
|
MaxLat = latitude + latRange / 2,
|
|
MinLon = longitude - lonRange / 2,
|
|
MaxLon = longitude + lonRange / 2,
|
|
TileZoom = zoomLevel
|
|
});
|
|
}
|
|
|
|
public async Task<Guid> InsertAsync(TileEntity tile)
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString);
|
|
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,
|
|
created_at, updated_at)
|
|
VALUES (@Id, @TileZoom, @TileX, @TileY, @Latitude, @Longitude, @TileSizeMeters,
|
|
@TileSizePixels, @ImageType, @MapsVersion, @Version, @FilePath,
|
|
@CreatedAt, @UpdatedAt)
|
|
ON CONFLICT (latitude, longitude, tile_zoom, tile_size_meters)
|
|
DO UPDATE SET
|
|
file_path = EXCLUDED.file_path,
|
|
tile_x = EXCLUDED.tile_x,
|
|
tile_y = EXCLUDED.tile_y,
|
|
updated_at = EXCLUDED.updated_at
|
|
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,
|
|
updated_at = @UpdatedAt
|
|
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 });
|
|
}
|
|
}
|