Skip to content

Commit

Permalink
Minor refactoring cleanup
Browse files Browse the repository at this point in the history
Rename `found_*` to `db_*` and similar for PG sources.

Naming guide:
* `db_*` -- was discovered by querying a PG connection
* `cfg_*` -- came from a configuration file
  • Loading branch information
nyurik committed Aug 3, 2023
1 parent 74c84fa commit 8d678db
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
48 changes: 24 additions & 24 deletions martin/src/pg/configurator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl PgBuilder {
}

pub async fn instantiate_tables(&self) -> Result<(Sources, TableInfoSources)> {
let mut found_pg_tables = query_available_tables(&self.pool).await?;
let mut db_tables_info = query_available_tables(&self.pool).await?;

// Match configured sources with the discovered ones and add them to the pending list.
let mut used = HashSet::<(&str, &str, &str)>::new();
Expand All @@ -89,15 +89,15 @@ impl PgBuilder {
}
}

let Some(found_schemas) = find_info(&found_pg_tables, &cfg_inf.schema, "schema", id) else { continue };
let Some(found_tables) = find_info(found_schemas, &cfg_inf.table, "table", id) else { continue };
let Some(found_inf) = find_info(found_tables, &cfg_inf.geometry_column, "geometry column", id) else { continue };
let Some(db_schemas) = find_info(&db_tables_info, &cfg_inf.schema, "schema", id) else { continue };
let Some(db_tables) = find_info(db_schemas, &cfg_inf.table, "table", id) else { continue };
let Some(db_infos) = find_info(db_tables, &cfg_inf.geometry_column, "geometry column", id) else { continue };

let dup = !used.insert((&cfg_inf.schema, &cfg_inf.table, &cfg_inf.geometry_column));
let dup = if dup { "duplicate " } else { "" };

let id2 = self.resolve_id(id, cfg_inf);
let Some(cfg_inf) = merge_table_info(self.default_srid, &id2, cfg_inf, found_inf) else { continue };
let Some(cfg_inf) = merge_table_info(self.default_srid, &id2, cfg_inf, db_infos) else { continue };
warn_on_rename(id, &id2, "Table");
info!("Configured {dup}source {id2} from {}", summary(&cfg_inf));
pending.push(table_to_query(
Expand All @@ -115,12 +115,12 @@ impl PgBuilder {
.schemas
.as_ref()
.cloned()
.unwrap_or_else(|| found_pg_tables.keys().cloned().collect());
.unwrap_or_else(|| db_tables_info.keys().cloned().collect());
for schema in schemas.iter().sorted() {
let Some(schema) = normalize_key(&found_pg_tables, schema, "schema", "") else { continue };
let found_tables = found_pg_tables.remove(&schema).unwrap();
for (table, geoms) in found_tables.into_iter().sorted_by(by_key) {
for (column, mut found_tbl) in geoms.into_iter().sorted_by(by_key) {
let Some(schema) = normalize_key(&db_tables_info, schema, "schema", "") else { continue };
let db_tables = db_tables_info.remove(&schema).unwrap();
for (table, geoms) in db_tables.into_iter().sorted_by(by_key) {
for (column, mut db_table_inf) in geoms.into_iter().sorted_by(by_key) {
if used.contains(&(schema.as_str(), table.as_str(), column.as_str())) {
continue;
}
Expand All @@ -129,13 +129,13 @@ impl PgBuilder {
.replace("{schema}", &schema)
.replace("{table}", &table)
.replace("{column}", &column);
let id2 = self.resolve_id(&source_id, &found_tbl);
let Some(srid) = calc_srid(&found_tbl.format_id(), &id2, found_tbl.srid, 0, self.default_srid) else { continue };
found_tbl.srid = srid;
info!("Discovered source {id2} from {}", summary(&found_tbl));
let id2 = self.resolve_id(&source_id, &db_table_inf);
let Some(srid) = calc_srid(&db_table_inf.format_id(), &id2, db_table_inf.srid, 0, self.default_srid) else { continue };
db_table_inf.srid = srid;
info!("Discovered source {id2} from {}", summary(&db_table_inf));
pending.push(table_to_query(
id2,
found_tbl,
db_table_inf,
self.pool.clone(),
self.disable_bounds,
self.max_feature_count,
Expand Down Expand Up @@ -166,13 +166,13 @@ impl PgBuilder {
}

pub async fn instantiate_functions(&self) -> Result<(Sources, FuncInfoSources)> {
let mut found_pg_funcs = query_available_function(&self.pool).await?;
let mut db_funcs_info = query_available_function(&self.pool).await?;
let mut res = Sources::default();
let mut info_map = FuncInfoSources::new();
let mut used = HashSet::<(&str, &str)>::new();

for (id, cfg_inf) in &self.functions {
let Some(schemas) = find_info(&found_pg_funcs, &cfg_inf.schema, "schema", id) else { continue };
let Some(schemas) = find_info(&db_funcs_info, &cfg_inf.schema, "schema", id) else { continue };
if schemas.is_empty() {
warn!("No functions found in schema {}. Only functions like (z,x,y) -> bytea and similar are considered. See README.md", cfg_inf.schema);
continue;
Expand All @@ -197,24 +197,24 @@ impl PgBuilder {
.schemas
.as_ref()
.cloned()
.unwrap_or_else(|| found_pg_funcs.keys().cloned().collect());
.unwrap_or_else(|| db_funcs_info.keys().cloned().collect());

for schema in schemas.iter().sorted() {
let Some(schema) = normalize_key(&found_pg_funcs, schema, "schema", "") else { continue; };
let found_funcs = found_pg_funcs.remove(&schema).unwrap();
for (func, (pg_sql, src_inf)) in found_funcs.into_iter().sorted_by(by_key) {
let Some(schema) = normalize_key(&db_funcs_info, schema, "schema", "") else { continue; };
let db_funcs = db_funcs_info.remove(&schema).unwrap();
for (func, (pg_sql, db_inf)) in db_funcs.into_iter().sorted_by(by_key) {
if used.contains(&(schema.as_str(), func.as_str())) {
continue;
}
let source_id = auto_funcs
.source_id_format
.replace("{schema}", &schema)
.replace("{function}", &func);
let id2 = self.resolve_id(&source_id, &src_inf);
self.add_func_src(&mut res, id2.clone(), &src_inf, pg_sql.clone());
let id2 = self.resolve_id(&source_id, &db_inf);
self.add_func_src(&mut res, id2.clone(), &db_inf, pg_sql.clone());
info!("Discovered source {id2} from function {}", pg_sql.signature);
debug!("{}", pg_sql.query);
info_map.insert(id2, src_inf);
info_map.insert(id2, db_inf);
}
}
}
Expand Down
26 changes: 13 additions & 13 deletions martin/src/pg/table_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,31 +198,31 @@ pub fn merge_table_info(
default_srid: Option<i32>,
new_id: &String,
cfg_inf: &TableInfo,
src_inf: &TableInfo,
db_inf: &TableInfo,
) -> Option<TableInfo> {
// Assume cfg_inf and src_inf have the same schema/table/geometry_column
let table_id = src_inf.format_id();
// Assume cfg_inf and db_inf have the same schema/table/geometry_column
let table_id = db_inf.format_id();
let mut inf = TableInfo {
// These values must match the database exactly
schema: src_inf.schema.clone(),
table: src_inf.table.clone(),
geometry_column: src_inf.geometry_column.clone(),
geometry_index: src_inf.geometry_index,
is_view: src_inf.is_view,
srid: calc_srid(&table_id, new_id, src_inf.srid, cfg_inf.srid, default_srid)?,
schema: db_inf.schema.clone(),
table: db_inf.table.clone(),
geometry_column: db_inf.geometry_column.clone(),
geometry_index: db_inf.geometry_index,
is_view: db_inf.is_view,
srid: calc_srid(&table_id, new_id, db_inf.srid, cfg_inf.srid, default_srid)?,
prop_mapping: HashMap::new(),
..cfg_inf.clone()
};

match (&src_inf.geometry_type, &cfg_inf.geometry_type) {
match (&db_inf.geometry_type, &cfg_inf.geometry_type) {
(Some(src), Some(cfg)) if src != cfg => {
warn!(r#"Table {table_id} has geometry type={src}, but source {new_id} has {cfg}"#);
}
_ => {}
}

let empty = HashMap::new();
let props = src_inf.properties.as_ref().unwrap_or(&empty);
let props = db_inf.properties.as_ref().unwrap_or(&empty);

if let Some(id_column) = &cfg_inf.id_column {
let prop = normalize_key(props, id_column.as_str(), "id_column", new_id)?;
Expand All @@ -243,11 +243,11 @@ pub fn merge_table_info(
pub fn calc_srid(
table_id: &str,
new_id: &str,
src_srid: i32,
db_srid: i32,
cfg_srid: i32,
default_srid: Option<i32>,
) -> Option<i32> {
match (src_srid, cfg_srid, default_srid) {
match (db_srid, cfg_srid, default_srid) {
(0, 0, Some(default_srid)) => {
info!("Table {table_id} has SRID=0, using provided default SRID={default_srid}");
Some(default_srid)
Expand Down

0 comments on commit 8d678db

Please sign in to comment.