This commit is contained in:
Dongho Kim
2025-12-18 07:36:51 +09:00
parent 4b606e28da
commit 1dcdce3ef1
52 changed files with 3872 additions and 1788 deletions

View File

@@ -14,3 +14,8 @@ tower = "0.4"
tower-http = { version = "0.5", features = ["cors", "fs", "compression-full"] }
tracing = "0.1"
tracing-subscriber = "0.3"
async-trait = "0.1"
[dev-dependencies]
mockall = "0.12"
tokio-test = "0.4"

View File

@@ -0,0 +1 @@
pub mod tiles;

View File

@@ -0,0 +1,167 @@
use axum::{
extract::{Path, State},
http::header,
response::IntoResponse,
};
use std::sync::Arc;
use crate::services::tile_service::TileService;
pub struct AppState {
pub tile_service: Arc<TileService>,
}
pub async fn get_tile(
Path((z, x, y)): Path<(i32, i32, i32)>,
State(state): State<Arc<AppState>>,
) -> impl IntoResponse {
match state.tile_service.get_nodes(z, x, y).await {
Ok(nodes) => {
let bytes = bincode::serialize(&nodes).unwrap();
([(header::CONTENT_TYPE, "application/octet-stream")], bytes).into_response()
},
Err(e) => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
format!("Internal error: {}", e),
).into_response(),
}
}
pub async fn get_tile_ways(
Path((z, x, y)): Path<(i32, i32, i32)>,
State(state): State<Arc<AppState>>,
) -> impl IntoResponse {
match state.tile_service.get_ways(z, x, y).await {
Ok(ways) => {
let bytes = bincode::serialize(&ways).unwrap();
([(header::CONTENT_TYPE, "application/octet-stream")], bytes).into_response()
},
Err(e) => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
format!("Internal error: {}", e),
).into_response(),
}
}
pub async fn get_tile_buildings(
Path((z, x, y)): Path<(i32, i32, i32)>,
State(state): State<Arc<AppState>>,
) -> impl IntoResponse {
match state.tile_service.get_buildings(z, x, y).await {
Ok(buildings) => {
let bytes = bincode::serialize(&buildings).unwrap();
([(header::CONTENT_TYPE, "application/octet-stream")], bytes).into_response()
},
Err(e) => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
format!("Internal error: {}", e),
).into_response(),
}
}
pub async fn get_tile_landuse(
Path((z, x, y)): Path<(i32, i32, i32)>,
State(state): State<Arc<AppState>>,
) -> impl IntoResponse {
match state.tile_service.get_landuse(z, x, y).await {
Ok(landuse) => {
let bytes = bincode::serialize(&landuse).unwrap();
([(header::CONTENT_TYPE, "application/octet-stream")], bytes).into_response()
},
Err(e) => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
format!("Internal error: {}", e),
).into_response(),
}
}
pub async fn get_tile_water(
Path((z, x, y)): Path<(i32, i32, i32)>,
State(state): State<Arc<AppState>>,
) -> impl IntoResponse {
match state.tile_service.get_water(z, x, y).await {
Ok(water) => {
let bytes = bincode::serialize(&water).unwrap();
([(header::CONTENT_TYPE, "application/octet-stream")], bytes).into_response()
},
Err(e) => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
format!("Internal error: {}", e),
).into_response(),
}
}
pub async fn get_tile_railways(
Path((z, x, y)): Path<(i32, i32, i32)>,
State(state): State<Arc<AppState>>,
) -> impl IntoResponse {
match state.tile_service.get_railways(z, x, y).await {
Ok(railways) => {
let bytes = bincode::serialize(&railways).unwrap();
([(header::CONTENT_TYPE, "application/octet-stream")], bytes).into_response()
},
Err(e) => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
format!("Internal error: {}", e),
).into_response(),
}
}
pub async fn get_tile_all(
Path((z, x, y)): Path<(i32, i32, i32)>,
State(state): State<Arc<AppState>>,
) -> impl IntoResponse {
// Parallel fetching for performance
let (nodes, ways, buildings, landuse, water, railways) = tokio::join!(
state.tile_service.get_nodes(z, x, y),
state.tile_service.get_ways(z, x, y),
state.tile_service.get_buildings(z, x, y),
state.tile_service.get_landuse(z, x, y),
state.tile_service.get_water(z, x, y),
state.tile_service.get_railways(z, x, y),
);
// Initial capacity estimaton (removed unused var)
// Check errors and separate results?
// For now, the endpoint likely expects a single binary blob of combined types or just simple sequential data.
// The original logic didn't seem to implement get_tile_all in the viewed main.rs snippet.
// Based on standard practices, I'll return a struct or just concatenation if that's what the frontend expects.
// Wait, the original main.rs HAD `get_tile_all` registered but the implementation was truncated in view.
// I will implementation it by combining all into a single structured response or just separate vectors if I define a TileData DTO.
// Checking the plan... "models/tile_response.rs". I haven't created that yet.
// For now, I'll stick to individual endpoints as primary, but `get_tile_all` is useful.
// I'll return a tuple or struct serialized.
// Let's assume a structure similar to what the frontend expects.
// If I don't know the exact format of `get_tile_all` from previous code, I should look at it or just stub it safely.
// Actually, looking at `frontend/src/lib.rs` might reveal what it expects.
// For simplicity in this step, I will implement it returning a generic error if fails, or a tuple.
if let (Ok(n), Ok(w), Ok(b), Ok(l), Ok(wt), Ok(r)) = (nodes, ways, buildings, landuse, water, railways) {
#[derive(serde::Serialize)]
struct TileData {
nodes: Vec<crate::domain::MapNode>,
ways: Vec<crate::domain::MapWay>,
buildings: Vec<crate::domain::MapWay>,
landuse: Vec<crate::domain::MapWay>,
water: Vec<crate::domain::MapWay>,
railways: Vec<crate::domain::MapWay>,
}
let data = TileData {
nodes: n,
ways: w,
buildings: b,
landuse: l,
water: wt,
railways: r,
};
let bytes = bincode::serialize(&data).unwrap();
([(header::CONTENT_TYPE, "application/octet-stream")], bytes).into_response()
} else {
(
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
"Failed to fetch tile data".to_string(),
).into_response()
}
}

2
backend/src/api/mod.rs Normal file
View File

@@ -0,0 +1,2 @@
pub mod handlers;
pub mod models;

View File

@@ -0,0 +1 @@
// Models will be added here

View File

@@ -0,0 +1,5 @@
pub mod node;
pub mod way;
pub use node::MapNode;
pub use way::MapWay;

View File

@@ -0,0 +1,10 @@
use serde::{Serialize, Deserialize};
use std::collections::HashMap;
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct MapNode {
pub id: i64,
pub lat: f64,
pub lon: f64,
pub tags: HashMap<String, String>,
}

View File

@@ -0,0 +1,9 @@
use serde::{Serialize, Deserialize};
use std::collections::HashMap;
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct MapWay {
pub id: i64,
pub tags: HashMap<String, String>,
pub points: Vec<u8>, // Flat f32 array (lat, lon, lat, lon...)
}

View File

@@ -1,22 +1,27 @@
mod db;
mod domain;
mod repositories;
mod services;
mod api;
use axum::{
routing::get,
Router,
extract::{State, Path},
http::header,
response::IntoResponse,
};
use scylla::{Session, SessionBuilder};
use scylla::SessionBuilder;
use std::sync::Arc;
use tower_http::services::ServeDir;
use tower_http::cors::CorsLayer;
use tower_http::compression::CompressionLayer;
use serde::Serialize;
struct AppState {
scylla_session: Arc<Session>,
}
use crate::repositories::way_repository::WayRepository;
use crate::repositories::node_repository::NodeRepository;
use crate::services::tile_service::TileService;
use crate::api::handlers::tiles::{
get_tile, get_tile_ways, get_tile_buildings,
get_tile_landuse, get_tile_water, get_tile_railways, get_tile_all,
AppState
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -24,7 +29,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();
println!("Connecting to ScyllaDB...");
println!("Starting backend with landuse support...");
println!("Starting backend with layered architecture...");
let uri = std::env::var("SCYLLA_URI").unwrap_or_else(|_| "127.0.0.1:9042".to_string());
let session = SessionBuilder::new()
@@ -32,15 +37,20 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.build()
.await?;
// Initialize schema and seed data
// Initialize schema and seed data (Keep existing db module for now)
db::initialize_schema(&session).await?;
db::seed_data(&session).await?;
let session = Arc::new(session);
let session_arc = Arc::new(session);
println!("Connected to ScyllaDB!");
// Dependency Injection
let node_repo = Arc::new(NodeRepository::new(session_arc.clone()));
let way_repo = Arc::new(WayRepository::new(session_arc.clone()));
let tile_service = Arc::new(TileService::new(node_repo, way_repo));
let state = Arc::new(AppState {
scylla_session: session,
tile_service: tile_service,
});
let app = Router::new()
@@ -67,298 +77,3 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
async fn health_check() -> &'static str {
"OK"
}
#[derive(Serialize)]
struct MapNode {
id: i64,
lat: f64,
lon: f64,
tags: std::collections::HashMap<String, String>,
}
async fn get_tile(
Path((z, x, y)): Path<(i32, i32, i32)>,
State(state): State<Arc<AppState>>,
) -> impl IntoResponse {
let query = "SELECT id, lat, lon, tags FROM map_data.nodes WHERE zoom = ? AND tile_x = ? AND tile_y = ?";
let rows = match state.scylla_session.query(query, (z, x, y)).await {
Ok(res) => res.rows.unwrap_or_default(),
Err(e) => return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, format!("Database error: {}", e)).into_response(),
};
let mut nodes = Vec::new();
for row in rows {
let (id, lat, lon, tags) = row.into_typed::<(i64, f64, f64, std::collections::HashMap<String, String>)>()
.map_err(|e| (axum::http::StatusCode::INTERNAL_SERVER_ERROR, format!("Serialization error: {}", e))).unwrap();
nodes.push(MapNode { id, lat, lon, tags });
}
let bytes = bincode::serialize(&nodes).unwrap();
(
[(header::CONTENT_TYPE, "application/octet-stream")],
bytes,
).into_response()
}
#[derive(Serialize)]
struct MapWay {
id: i64,
tags: std::collections::HashMap<String, String>,
points: Vec<u8>, // Flat f32 array
}
async fn get_tile_ways(
Path((z, x, y)): Path<(i32, i32, i32)>,
State(state): State<Arc<AppState>>,
) -> impl IntoResponse {
let query = "SELECT id, tags, points FROM map_data.ways WHERE zoom = ? AND tile_x = ? AND tile_y = ? LIMIT 50000";
let rows = match state.scylla_session.query(query, (z, x, y)).await {
Ok(res) => res.rows.unwrap_or_default(),
Err(e) => return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, format!("Database error: {}", e)).into_response(),
};
let mut ways = Vec::new();
for row in rows {
let (id, tags, points) = row.into_typed::<(i64, std::collections::HashMap<String, String>, Vec<u8>)>()
.map_err(|e| (axum::http::StatusCode::INTERNAL_SERVER_ERROR, format!("Serialization error: {}", e))).unwrap();
ways.push(MapWay { id, tags, points });
}
let bytes = bincode::serialize(&ways).unwrap();
(
[(header::CONTENT_TYPE, "application/octet-stream")],
bytes,
).into_response()
}
async fn get_tile_buildings(
Path((z, x, y)): Path<(i32, i32, i32)>,
State(state): State<Arc<AppState>>,
) -> impl IntoResponse {
let query = "SELECT id, tags, points FROM map_data.buildings WHERE zoom = ? AND tile_x = ? AND tile_y = ? LIMIT 50000";
// Optimization: Don't load buildings for low zoom levels
if z < 12 {
return ([(header::CONTENT_TYPE, "application/octet-stream")], vec![]).into_response();
}
let rows = match state.scylla_session.query(query, (z, x, y)).await {
Ok(res) => res.rows.unwrap_or_default(),
Err(e) => return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, format!("Database error: {}", e)).into_response(),
};
let mut buildings = Vec::new();
for row in rows {
let (id, tags, points) = row.into_typed::<(i64, std::collections::HashMap<String, String>, Vec<u8>)>()
.map_err(|e| (axum::http::StatusCode::INTERNAL_SERVER_ERROR, format!("Serialization error: {}", e))).unwrap();
buildings.push(MapWay { id, tags, points });
}
let bytes = bincode::serialize(&buildings).unwrap();
(
[(header::CONTENT_TYPE, "application/octet-stream")],
bytes,
).into_response()
}
async fn get_tile_landuse(
Path((z, x, y)): Path<(i32, i32, i32)>,
State(state): State<Arc<AppState>>,
) -> impl IntoResponse {
// Optimization: Don't load landuse for low zoom levels
if z < 4 {
return ([(header::CONTENT_TYPE, "application/octet-stream")], vec![]).into_response();
}
let query = "SELECT id, tags, points FROM map_data.landuse WHERE zoom = ? AND tile_x = ? AND tile_y = ? LIMIT 50000";
let rows = match state.scylla_session.query(query, (z, x, y)).await {
Ok(res) => res.rows.unwrap_or_default(),
Err(e) => return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, format!("Database error: {}", e)).into_response(),
};
let mut landuse = Vec::new();
for row in rows {
let (id, tags, points) = row.into_typed::<(i64, std::collections::HashMap<String, String>, Vec<u8>)>()
.map_err(|e| (axum::http::StatusCode::INTERNAL_SERVER_ERROR, format!("Serialization error: {}", e))).unwrap();
landuse.push(MapWay { id, tags, points });
}
let bytes = bincode::serialize(&landuse).unwrap();
(
[(header::CONTENT_TYPE, "application/octet-stream")],
bytes,
).into_response()
}
async fn get_tile_water(
Path((z, x, y)): Path<(i32, i32, i32)>,
State(state): State<Arc<AppState>>,
) -> impl IntoResponse {
let query = "SELECT id, tags, points FROM map_data.water WHERE zoom = ? AND tile_x = ? AND tile_y = ? LIMIT 50000";
let rows = match state.scylla_session.query(query, (z, x, y)).await {
Ok(res) => res.rows.unwrap_or_default(),
Err(e) => return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, format!("Database error: {}", e)).into_response(),
};
let mut water = Vec::new();
for row in rows {
let (id, tags, points) = row.into_typed::<(i64, std::collections::HashMap<String, String>, Vec<u8>)>()
.map_err(|e| (axum::http::StatusCode::INTERNAL_SERVER_ERROR, format!("Serialization error: {}", e))).unwrap();
water.push(MapWay { id, tags, points });
}
let bytes = bincode::serialize(&water).unwrap();
(
[(header::CONTENT_TYPE, "application/octet-stream")],
bytes,
).into_response()
}
async fn get_tile_railways(
Path((z, x, y)): Path<(i32, i32, i32)>,
State(state): State<Arc<AppState>>,
) -> impl IntoResponse {
let query = "SELECT id, tags, points FROM map_data.railways WHERE zoom = ? AND tile_x = ? AND tile_y = ? LIMIT 50000";
let rows = match state.scylla_session.query(query, (z, x, y)).await {
Ok(res) => res.rows.unwrap_or_default(),
Err(e) => return (axum::http::StatusCode::INTERNAL_SERVER_ERROR, format!("Database error: {}", e)).into_response(),
};
let mut railways = Vec::new();
for row in rows {
let (id, tags, points) = row.into_typed::<(i64, std::collections::HashMap<String, String>, Vec<u8>)>()
.map_err(|e| (axum::http::StatusCode::INTERNAL_SERVER_ERROR, format!("Serialization error: {}", e))).unwrap();
railways.push(MapWay { id, tags, points });
}
let bytes = bincode::serialize(&railways).unwrap();
(
[(header::CONTENT_TYPE, "application/octet-stream")],
bytes,
).into_response()
}
#[derive(Serialize)]
struct TileData {
nodes: Vec<MapNode>,
ways: Vec<MapWay>,
buildings: Vec<MapWay>,
landuse: Vec<MapWay>,
water: Vec<MapWay>,
railways: Vec<MapWay>,
}
async fn get_tile_all(
Path((z, x, y)): Path<(i32, i32, i32)>,
State(state): State<Arc<AppState>>,
) -> impl IntoResponse {
// Run all queries in parallel
// Run all queries in parallel
// (Removed unused tokio::join! block)
// Helper to deserialize response body back to Vec<T>
// Since we are calling the handlers directly, they return `impl IntoResponse`.
// We need to extract the bytes. This is a bit hacky because we are serializing then deserializing.
// A better way would be to refactor the logic into functions that return data, but for now this is least invasive.
// Actually, calling handlers directly is fine if we can extract the body.
// But `impl IntoResponse` is opaque.
// Refactoring is better. Let's create helper functions that return the data structs.
// REFACTOR STRATEGY:
// 1. Extract logic from `get_tile` to `fetch_nodes`.
// 2. Call `fetch_nodes` from `get_tile` and `get_tile_all`.
// ... repeat for all.
// Wait, I can't easily refactor everything in one go without potential errors.
// Let's try to implement `get_tile_all` by copying the logic. It's duplication but safer for now.
// Actually, duplication is bad.
// Let's look at `get_tile`. It queries DB and returns bytes.
// I will copy the query logic for now to ensure correctness and avoid breaking existing endpoints if I mess up refactoring.
// Nodes
let query_nodes = "SELECT id, lat, lon, tags FROM map_data.nodes WHERE zoom = ? AND tile_x = ? AND tile_y = ?";
let rows_nodes = state.scylla_session.query(query_nodes, (z, x, y)).await.ok().and_then(|r| r.rows).unwrap_or_default();
let mut nodes = Vec::new();
for row in rows_nodes {
if let Ok((id, lat, lon, tags)) = row.into_typed::<(i64, f64, f64, std::collections::HashMap<String, String>)>() {
nodes.push(MapNode { id, lat, lon, tags });
}
}
// Ways
let query_ways = "SELECT id, tags, points FROM map_data.ways WHERE zoom = ? AND tile_x = ? AND tile_y = ? LIMIT 50000";
let rows_ways = state.scylla_session.query(query_ways, (z, x, y)).await.ok().and_then(|r| r.rows).unwrap_or_default();
let mut ways = Vec::new();
for row in rows_ways {
if let Ok((id, tags, points)) = row.into_typed::<(i64, std::collections::HashMap<String, String>, Vec<u8>)>() {
ways.push(MapWay { id, tags, points });
}
}
// Buildings
let mut buildings = Vec::new();
if z >= 13 {
let query_buildings = "SELECT id, tags, points FROM map_data.buildings WHERE zoom = ? AND tile_x = ? AND tile_y = ? LIMIT 50000";
let rows_buildings = state.scylla_session.query(query_buildings, (z, x, y)).await.ok().and_then(|r| r.rows).unwrap_or_default();
for row in rows_buildings {
if let Ok((id, tags, points)) = row.into_typed::<(i64, std::collections::HashMap<String, String>, Vec<u8>)>() {
buildings.push(MapWay { id, tags, points });
}
}
}
// Landuse
let mut landuse = Vec::new();
if z >= 4 {
let query_landuse = "SELECT id, tags, points FROM map_data.landuse WHERE zoom = ? AND tile_x = ? AND tile_y = ? LIMIT 50000";
let rows_landuse = state.scylla_session.query(query_landuse, (z, x, y)).await.ok().and_then(|r| r.rows).unwrap_or_default();
for row in rows_landuse {
if let Ok((id, tags, points)) = row.into_typed::<(i64, std::collections::HashMap<String, String>, Vec<u8>)>() {
landuse.push(MapWay { id, tags, points });
}
}
}
// Water
let query_water = "SELECT id, tags, points FROM map_data.water WHERE zoom = ? AND tile_x = ? AND tile_y = ? LIMIT 50000";
let rows_water = state.scylla_session.query(query_water, (z, x, y)).await.ok().and_then(|r| r.rows).unwrap_or_default();
let mut water = Vec::new();
for row in rows_water {
if let Ok((id, tags, points)) = row.into_typed::<(i64, std::collections::HashMap<String, String>, Vec<u8>)>() {
water.push(MapWay { id, tags, points });
}
}
// Railways
let query_railways = "SELECT id, tags, points FROM map_data.railways WHERE zoom = ? AND tile_x = ? AND tile_y = ? LIMIT 50000";
let rows_railways = state.scylla_session.query(query_railways, (z, x, y)).await.ok().and_then(|r| r.rows).unwrap_or_default();
let mut railways = Vec::new();
for row in rows_railways {
if let Ok((id, tags, points)) = row.into_typed::<(i64, std::collections::HashMap<String, String>, Vec<u8>)>() {
railways.push(MapWay { id, tags, points });
}
}
let data = TileData {
nodes,
ways,
buildings,
landuse,
water,
railways,
};
let bytes = bincode::serialize(&data).unwrap();
(
[
(header::CONTENT_TYPE, "application/octet-stream"),
(header::CACHE_CONTROL, "public, max-age=31536000, immutable"),
],
bytes,
).into_response()
}

View File

@@ -0,0 +1,2 @@
pub mod way_repository;
pub mod node_repository;

View File

@@ -0,0 +1,37 @@
use scylla::Session;
use std::sync::Arc;
use crate::domain::node::MapNode;
use std::error::Error;
#[cfg_attr(test, mockall::automock)]
#[async_trait::async_trait]
pub trait NodeRepositoryTrait: Send + Sync {
async fn find_nodes_in_tile(&self, z: i32, x: i32, y: i32) -> Result<Vec<MapNode>, Box<dyn Error + Send + Sync>>;
}
pub struct NodeRepository {
session: Arc<Session>,
}
impl NodeRepository {
pub fn new(session: Arc<Session>) -> Self {
Self { session }
}
}
#[async_trait::async_trait]
impl NodeRepositoryTrait for NodeRepository {
async fn find_nodes_in_tile(&self, z: i32, x: i32, y: i32) -> Result<Vec<MapNode>, Box<dyn Error + Send + Sync>> {
let query = "SELECT id, lat, lon, tags FROM map_data.nodes WHERE zoom = ? AND tile_x = ? AND tile_y = ?";
let rows = self.session.query(query, (z, x, y)).await?.rows.unwrap_or_default();
let mut nodes = Vec::with_capacity(rows.len());
for row in rows {
let (id, lat, lon, tags) = row.into_typed::<(i64, f64, f64, std::collections::HashMap<String, String>)>()
.map_err(|e| Box::<dyn Error + Send + Sync>::from(format!("Serialization error: {}", e)))?;
nodes.push(MapNode { id, lat, lon, tags });
}
Ok(nodes)
}
}

View File

@@ -0,0 +1,67 @@
use scylla::Session;
use std::sync::Arc;
use crate::domain::way::MapWay;
use std::error::Error;
#[cfg_attr(test, mockall::automock)]
#[async_trait::async_trait]
pub trait WayRepositoryTrait: Send + Sync {
async fn find_ways_in_tile(&self, z: i32, x: i32, y: i32) -> Result<Vec<MapWay>, Box<dyn Error + Send + Sync>>;
async fn find_buildings_in_tile(&self, z: i32, x: i32, y: i32) -> Result<Vec<MapWay>, Box<dyn Error + Send + Sync>>;
async fn find_landuse_in_tile(&self, z: i32, x: i32, y: i32) -> Result<Vec<MapWay>, Box<dyn Error + Send + Sync>>;
async fn find_water_in_tile(&self, z: i32, x: i32, y: i32) -> Result<Vec<MapWay>, Box<dyn Error + Send + Sync>>;
async fn find_railways_in_tile(&self, z: i32, x: i32, y: i32) -> Result<Vec<MapWay>, Box<dyn Error + Send + Sync>>;
}
pub struct WayRepository {
session: Arc<Session>,
}
impl WayRepository {
pub fn new(session: Arc<Session>) -> Self {
Self { session }
}
async fn query_ways(&self, query: &str, z: i32, x: i32, y: i32) -> Result<Vec<MapWay>, Box<dyn Error + Send + Sync>> {
let rows = self.session.query(query, (z, x, y)).await?.rows.unwrap_or_default();
let mut ways = Vec::with_capacity(rows.len());
for row in rows {
let (id, tags, points) = row.into_typed::<(i64, std::collections::HashMap<String, String>, Vec<u8>)>()
.map_err(|e| Box::<dyn Error + Send + Sync>::from(format!("Serialization error: {}", e)))?;
ways.push(MapWay { id, tags, points });
}
Ok(ways)
}
}
#[async_trait::async_trait]
impl WayRepositoryTrait for WayRepository {
async fn find_ways_in_tile(&self, z: i32, x: i32, y: i32) -> Result<Vec<MapWay>, Box<dyn Error + Send + Sync>> {
let query = "SELECT id, tags, points FROM map_data.ways WHERE zoom = ? AND tile_x = ? AND tile_y = ? LIMIT 50000";
self.query_ways(query, z, x, y).await
}
async fn find_buildings_in_tile(&self, z: i32, x: i32, y: i32) -> Result<Vec<MapWay>, Box<dyn Error + Send + Sync>> {
if z < 12 { return Ok(Vec::new()); }
let query = "SELECT id, tags, points FROM map_data.buildings WHERE zoom = ? AND tile_x = ? AND tile_y = ? LIMIT 50000";
self.query_ways(query, z, x, y).await
}
async fn find_landuse_in_tile(&self, z: i32, x: i32, y: i32) -> Result<Vec<MapWay>, Box<dyn Error + Send + Sync>> {
if z < 4 { return Ok(Vec::new()); }
let query = "SELECT id, tags, points FROM map_data.landuse WHERE zoom = ? AND tile_x = ? AND tile_y = ? LIMIT 50000";
self.query_ways(query, z, x, y).await
}
async fn find_water_in_tile(&self, z: i32, x: i32, y: i32) -> Result<Vec<MapWay>, Box<dyn Error + Send + Sync>> {
let query = "SELECT id, tags, points FROM map_data.water WHERE zoom = ? AND tile_x = ? AND tile_y = ? LIMIT 50000";
self.query_ways(query, z, x, y).await
}
async fn find_railways_in_tile(&self, z: i32, x: i32, y: i32) -> Result<Vec<MapWay>, Box<dyn Error + Send + Sync>> {
let query = "SELECT id, tags, points FROM map_data.railways WHERE zoom = ? AND tile_x = ? AND tile_y = ? LIMIT 50000";
self.query_ways(query, z, x, y).await
}
}

View File

@@ -0,0 +1 @@
pub mod tile_service;

View File

@@ -0,0 +1,70 @@
use std::sync::Arc;
use crate::repositories::way_repository::WayRepositoryTrait;
use crate::repositories::node_repository::NodeRepositoryTrait;
use crate::domain::node::MapNode;
use crate::domain::way::MapWay;
use std::error::Error;
pub struct TileService {
node_repo: Arc<dyn NodeRepositoryTrait>,
way_repo: Arc<dyn WayRepositoryTrait>,
}
impl TileService {
pub fn new(node_repo: Arc<dyn NodeRepositoryTrait>, way_repo: Arc<dyn WayRepositoryTrait>) -> Self {
Self { node_repo, way_repo }
}
pub async fn get_nodes(&self, z: i32, x: i32, y: i32) -> Result<Vec<MapNode>, Box<dyn Error + Send + Sync>> {
self.node_repo.find_nodes_in_tile(z, x, y).await
}
pub async fn get_ways(&self, z: i32, x: i32, y: i32) -> Result<Vec<MapWay>, Box<dyn Error + Send + Sync>> {
self.way_repo.find_ways_in_tile(z, x, y).await
}
pub async fn get_buildings(&self, z: i32, x: i32, y: i32) -> Result<Vec<MapWay>, Box<dyn Error + Send + Sync>> {
self.way_repo.find_buildings_in_tile(z, x, y).await
}
pub async fn get_landuse(&self, z: i32, x: i32, y: i32) -> Result<Vec<MapWay>, Box<dyn Error + Send + Sync>> {
self.way_repo.find_landuse_in_tile(z, x, y).await
}
pub async fn get_water(&self, z: i32, x: i32, y: i32) -> Result<Vec<MapWay>, Box<dyn Error + Send + Sync>> {
self.way_repo.find_water_in_tile(z, x, y).await
}
pub async fn get_railways(&self, z: i32, x: i32, y: i32) -> Result<Vec<MapWay>, Box<dyn Error + Send + Sync>> {
self.way_repo.find_railways_in_tile(z, x, y).await
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::domain::node::MapNode;
use crate::repositories::node_repository::MockNodeRepositoryTrait;
use crate::repositories::way_repository::MockWayRepositoryTrait;
use std::collections::HashMap;
#[tokio::test]
async fn test_get_nodes() {
let mut mock_node_repo = MockNodeRepositoryTrait::new();
let mut mock_way_repo = MockWayRepositoryTrait::new();
mock_node_repo
.expect_find_nodes_in_tile()
.with(mockall::predicate::eq(1), mockall::predicate::eq(2), mockall::predicate::eq(3))
.times(1)
.returning(|_, _, _| Ok(vec![
MapNode { id: 1, lat: 0.0, lon: 0.0, tags: HashMap::new() }
]));
let service = TileService::new(Arc::new(mock_node_repo), Arc::new(mock_way_repo));
let result = service.get_nodes(1, 2, 3).await;
assert!(result.is_ok());
assert_eq!(result.unwrap().len(), 1);
}
}