Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Massive update to tables
Browse files Browse the repository at this point in the history
* All table sources now use the same system as functions
* Proper table name escaping
* Prepared statements might speed things up, but this is not certain because we prepare on each get request. At least now we can optimize in one place.
nyurik committed Dec 3, 2022
1 parent 1e62d0a commit b3f384c
Showing 24 changed files with 640 additions and 725 deletions.
6 changes: 3 additions & 3 deletions benches/sources.rs
Original file line number Diff line number Diff line change
@@ -52,7 +52,7 @@ fn main() {}
//
// async fn get_table_source_tile() {
// let source = mock_table_source("public", "table_source").await;
// let xyz = Xyz { z: 0, x: 0, y: 0 };
// let xyz = Xyz::new(0, 0, 0);
// let _tile = source.get_tile(&xyz, &None).await.unwrap();
// }
//
@@ -77,7 +77,7 @@ fn main() {}
// table_sources: vec![points1, points2],
// };
//
// let xyz = Xyz { z: 0, x: 0, y: 0 };
// let xyz = Xyz::new(0, 0, 0);
// let _tile = source.get_tile(&xyz, &None).await.unwrap();
// }
//
@@ -88,7 +88,7 @@ fn main() {}
//
// async fn get_function_source_tile() {
// let source = mock_function_source("public", "function_zxy_query");
// let xyz = Xyz { z: 0, x: 0, y: 0 };
// let xyz = Xyz::new(0, 0, 0);
//
// let _tile = source.get_tile(&xyz, &None).await.unwrap();
// }
8 changes: 4 additions & 4 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use actix_web::dev::Server;
use clap::Parser;
use log::{error, info, warn};
use martin::config::{read_config, ConfigBuilder, ConfigDb};
use martin::config::{read_config, Config, ConfigBuilder};
use martin::pg::config::{PgArgs, PgConfigBuilder};
use martin::pg::db::resolve_pg_data;
use martin::pg::configurator::resolve_pg_data;
use martin::source::IdResolver;
use martin::srv::config::{SrvArgs, SrvConfigBuilder};
use martin::srv::server;
@@ -74,9 +74,9 @@ async fn start(args: Args) -> io::Result<Server> {

let id_resolver = IdResolver::new(RESERVED_KEYWORDS);
let (sources, pg_config, _) = resolve_pg_data(config.pg, id_resolver).await?;
let config = ConfigDb {
srv: config.srv,
let config = Config {
pg: pg_config,
..config
};

if save_config.is_some() {
10 changes: 1 addition & 9 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::io_error;
use crate::pg::config::{PgConfig, PgConfigBuilder, PgConfigDb};
use crate::pg::config::{PgConfig, PgConfigBuilder};
use crate::srv::config::{SrvConfig, SrvConfigBuilder};
use log::warn;
use serde::{Deserialize, Serialize};
@@ -17,14 +17,6 @@ pub struct Config {
pub pg: PgConfig,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct ConfigDb {
#[serde(flatten)]
pub srv: SrvConfig,
#[serde(flatten)]
pub pg: PgConfigDb,
}

#[derive(Debug, PartialEq, Deserialize)]
pub struct ConfigBuilder {
#[serde(flatten)]
106 changes: 49 additions & 57 deletions src/pg/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::config::{report_unrecognized_config, set_option};
use crate::pg::utils::create_tilejson;
use serde::{Deserialize, Serialize};
use serde_yaml::Value;
use std::collections::HashMap;
use std::{env, io};
use tilejson::Bounds;
use tilejson::{Bounds, TileJSON};

pub const POOL_SIZE_DEFAULT: u32 = 20;

@@ -25,11 +26,7 @@ pub struct PgArgs {
pub pool_size: Option<u32>,
}

pub trait FormatId {
fn format_id(&self, db_id: &str) -> String;
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Default)]
pub struct TableInfo {
/// Table schema
pub schema: String,
@@ -85,36 +82,44 @@ pub struct TableInfo {
pub unrecognized: HashMap<String, Value>,
}

impl FormatId for TableInfo {
fn format_id(&self, db_id: &str) -> String {
format!(
"{}.{}.{}.{}",
db_id, self.schema, self.table, self.geometry_column
impl PgInfo for TableInfo {
fn format_id(&self) -> String {
format!("{}.{}.{}", self.schema, self.table, self.geometry_column)
}

fn to_tilejson(&self) -> TileJSON {
create_tilejson(
self.format_id(),
self.minzoom,
self.maxzoom,
self.bounds,
None,
)
}
}

#[derive(Clone, Serialize, Debug, PartialEq, Default)]
pub struct FunctionInfoDbInfo {
#[serde(skip_serializing)]
#[derive(Clone, Debug)]
pub struct PgSqlInfo {
pub query: String,
#[serde(skip_serializing)]
pub has_query_params: bool,
#[serde(skip_serializing)]
pub signature: String,
#[serde(flatten)]
pub info: FunctionInfo,
}

impl FunctionInfoDbInfo {
pub fn with_info(&self, info: &FunctionInfo) -> Self {
impl PgSqlInfo {
pub fn new(query: String, has_query_params: bool, signature: String) -> Self {
Self {
info: info.clone(),
..self.clone()
query,
has_query_params,
signature,
}
}
}

pub trait PgInfo {
fn format_id(&self) -> String;
fn to_tilejson(&self) -> TileJSON;
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Default)]
pub struct FunctionInfo {
/// Schema name
@@ -169,24 +174,31 @@ impl FunctionInfo {
}
}

impl FormatId for FunctionInfo {
fn format_id(&self, db_id: &str) -> String {
format!("{}.{}.{}", db_id, self.schema, self.function)
impl PgInfo for FunctionInfo {
fn format_id(&self) -> String {
format!("{}.{}", self.schema, self.function)
}

fn to_tilejson(&self) -> TileJSON {
create_tilejson(
self.format_id(),
self.minzoom,
self.maxzoom,
self.bounds,
None,
)
}
}

pub type TableInfoSources = HashMap<String, TableInfo>;
pub type TableInfoVec = Vec<TableInfo>;
pub type FuncInfoSources = HashMap<String, FunctionInfo>;
pub type FuncInfoDbSources = HashMap<String, FunctionInfoDbInfo>;
pub type FuncInfoDbMapMap = HashMap<String, HashMap<String, FunctionInfoDbInfo>>;
pub type FuncInfoDbVec = Vec<FunctionInfoDbInfo>;

pub type PgConfig = PgConfigRaw<TableInfoSources, FuncInfoSources>;
pub type PgConfigDb = PgConfigRaw<TableInfoSources, FuncInfoDbSources>;
pub type InfoMap<T> = HashMap<String, T>;
pub type SqlFuncInfoMapMap = HashMap<String, HashMap<String, (PgSqlInfo, FunctionInfo)>>;
pub type SqlTableInfoMapMapMap =
HashMap<String, HashMap<String, HashMap<String, (PgSqlInfo, TableInfo)>>>;

#[derive(Clone, Debug, Serialize, PartialEq)]
pub struct PgConfigRaw<T, F> {
pub struct PgConfig {
pub connection_string: String,
#[cfg(feature = "ssl")]
#[serde(skip_serializing_if = "Option::is_none")]
@@ -196,10 +208,12 @@ pub struct PgConfigRaw<T, F> {
#[serde(skip_serializing_if = "Option::is_none")]
pub default_srid: Option<i32>,
pub pool_size: u32,
#[serde(skip_serializing)]
pub discover_functions: bool,
#[serde(skip_serializing)]
pub discover_tables: bool,
pub table_sources: T,
pub function_sources: F,
pub table_sources: TableInfoSources,
pub function_sources: FuncInfoSources,
}

#[derive(Debug, Default, PartialEq, Deserialize)]
@@ -297,25 +311,3 @@ impl From<(PgArgs, Option<String>)> for PgConfigBuilder {
}
}
}

impl PgConfig {
pub fn to_db(
self,
table_sources: TableInfoSources,
function_sources: FuncInfoDbSources,
) -> PgConfigDb {
PgConfigDb {
connection_string: self.connection_string,
#[cfg(feature = "ssl")]
ca_root_file: self.ca_root_file,
#[cfg(feature = "ssl")]
danger_accept_invalid_certs: self.danger_accept_invalid_certs,
default_srid: self.default_srid,
pool_size: self.pool_size,
discover_functions: self.discover_functions,
discover_tables: self.discover_tables,
table_sources,
function_sources,
}
}
}
Loading

0 comments on commit b3f384c

Please sign in to comment.