Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
remove get_nb_cities get_address from CompatibleDB trait
Browse files Browse the repository at this point in the history
This was a debugging feature that was not even used in the main binary
and which was not even valid since addresses to delete are computed on
the fly.
  • Loading branch information
remi-dupre committed Mar 8, 2022
1 parent 53a5be8 commit fb657f5
Show file tree
Hide file tree
Showing 8 changed files with 7 additions and 141 deletions.
1 change: 1 addition & 0 deletions deduplicator/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ fn main() -> rusqlite::Result<()> {

tprintln!("Deduplication...");
deduplication.compute_duplicates()?;
deduplication.cleanup_database()?;

// --- Dump CSV

Expand Down
41 changes: 2 additions & 39 deletions deduplicator/src/lib/db_hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub struct DbHashes {

impl DbHashes {
/// Instantiate a new database from a path to an SQLite file.
/// The handler optionally contains a buffer for addresses that should be removed, which
/// reflects the output of get_addresses()?.iter().
///
/// If the file is not created yet or if the schema is not already set up, this will be done.
///
Expand Down Expand Up @@ -95,28 +97,6 @@ impl DbHashes {
))
}

/// Return a list of addresses matching an input house number and street name.
pub fn get_addresses_by_street(
&self,
housenumber: i32,
street: &str,
) -> rusqlite::Result<Vec<Address>> {
let conn = self.get_conn()?;
let mut stmt = conn
.prepare(&format!(
"SELECT * FROM {TABLE_ADDRESSES} WHERE number=?1 AND street=?2;"
))
.expect("failed to prepare statement");

let mut addr_iter =
stmt.query_map(&[&housenumber, &street as &dyn ToSql], |row| row.try_into())?;

addr_iter.try_fold(Vec::new(), |mut acc, addr| {
acc.push(addr?);
Ok(acc)
})
}

/// Returns the number of rows of a table.
fn count_table_entries(&self, table: &str) -> rusqlite::Result<i64> {
self.get_conn()?.query_row(
Expand Down Expand Up @@ -165,23 +145,6 @@ impl DbHashes {
self.to_delete.len()
}

/// Returns the number of cities in the database.
///
/// # Example
/// ```no_run
/// use deduplicator::db_hashes::*;
///
/// let db = DbHashes::new("sqlite.db".into(), None).unwrap();
/// assert_eq!(db.count_cities(), Ok(0));
/// ```
pub fn count_cities(&self) -> rusqlite::Result<i64> {
self.get_conn()?.query_row(
&format!("SELECT COUNT(DISTINCT city) FROM {TABLE_ADDRESSES};"),
[],
|row: &rusqlite::Row| row.get(0),
)
}

/// Count the number of pairs (address, hash) that are in collision with another.
///
/// # Example
Expand Down
14 changes: 1 addition & 13 deletions deduplicator/src/lib/deduplicator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl Deduplicator {
}

/// Swipe hashes from database and output stats
pub fn apply_deletions(&self) -> rusqlite::Result<()> {
pub fn cleanup_database(&self) -> rusqlite::Result<()> {
self.db.cleanup_database()?;
let count_to_delete = self.db.count_to_delete() as i64;

Expand Down Expand Up @@ -476,24 +476,12 @@ where
.expect("failed sending address: channel may have closed too early")
}

fn get_nb_cities(&mut self) -> i64 {
self.borrow_db(|db| db.count_cities())
.map_err(|err| eprintln!("Failed counting cities: '{}'", err))
.unwrap_or(0)
}

fn get_nb_addresses(&mut self) -> i64 {
self.flush()
.expect("failed flushing before counting addresses");
self.count_addresses
}

fn get_address(&mut self, housenumber: i32, street: &str) -> Vec<Address> {
self.borrow_db(|db| db.get_addresses_by_street(housenumber, street))
.map_err(|err| eprintln!("Error while retrieving addresses by street: '{}'", err))
.unwrap_or_default()
}

// Current implementation for the deduplication actually doesn't log errors.
fn get_nb_errors(&mut self) -> i64 {
0
Expand Down
3 changes: 1 addition & 2 deletions importers/bano/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ fn main() {
bano::import_addresses(&args[1], &mut db);

tprintln!(
"Got {} addresses in {} cities (and {} errors)",
"Got {} addresses (and {} errors)",
db.get_nb_addresses(),
db.get_nb_cities(),
db.get_nb_errors(),
);

Expand Down
3 changes: 1 addition & 2 deletions importers/openaddresses/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ fn main() {
openaddresses::import_addresses(&args[1], &mut db);

tprintln!(
"Got {} addresses in {} cities (and {} errors)",
"Got {} addresses (and {} errors)",
db.get_nb_addresses(),
db.get_nb_cities(),
db.get_nb_errors(),
);

Expand Down
3 changes: 0 additions & 3 deletions importers/osm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,6 @@ mod tests {
import_addresses(pbf_file.as_ref(), &mut db);
assert_eq!(db.get_nb_addresses(), 361);

let addr = db.get_address(2, "Place de la Forêt de Cruye");
assert_eq!(addr.len(), 1);

let _ = std::fs::remove_file(db_file); // we ignore any potential error
}
}
3 changes: 1 addition & 2 deletions importers/osm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ fn main() {
let mut db = DB::new("addresses.db", 1000, true).expect("Failed to create DB");
osm::import_addresses(args[1].as_ref(), &mut db);
tprintln!(
"Got {} addresses in {} cities (and {} errors)",
"Got {} addresses (and {} errors)",
db.get_nb_addresses(),
db.get_nb_cities(),
db.get_nb_errors(),
);

Expand Down
80 changes: 0 additions & 80 deletions tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,29 +350,6 @@ pub trait CompatibleDB {
/// });
/// ```
fn insert(&mut self, addr: Address);
/// Counts the number of different inserted cities.
///
/// Example:
///
/// ```no_run
/// use tools::{Address, CompatibleDB, DB};
///
/// let mut db = DB::new("addresses.db", 10000, true).expect("failed to create DB");
/// assert_eq!(db.get_nb_cities(), 0);
/// db.insert(Address {
/// lat: 0.,
/// lon: 0.,
/// number: Some("12".into()),
/// street: Some("rue des champignons".into()),
/// unit: None,
/// city: Some("Paris".into()),
/// district: None,
/// region: None,
/// postcode: None,
/// });
/// assert_eq!(db.get_nb_cities(), 1);
/// ```
fn get_nb_cities(&mut self) -> i64;
/// Counts the number of inserted addresses.
///
/// Example:
Expand Down Expand Up @@ -445,40 +422,6 @@ pub trait CompatibleDB {
/// assert_eq!(db.get_nb_by_errors_kind(), vec![("Missing mandataory field".into(), 1)]);
/// ```
fn get_nb_by_errors_kind(&mut self) -> Vec<(std::string::String, i64)>;
/// Returns a list of addresses matching the given housenumber and street name.
///
/// Example:
///
/// ```no_run
/// use tools::{Address, CompatibleDB, DB};
///
/// let mut db = DB::new("addresses.db", 10000, true).expect("failed to create DB");
/// assert_eq!(db.get_nb_addresses(), 0);
/// db.insert(Address {
/// lat: 0.,
/// lon: 0.,
/// number: Some("12".into()),
/// street: Some("rue des champignons".into()),
/// unit: None,
/// city: None,
/// district: None,
/// region: None,
/// postcode: None,
/// });
/// assert_eq!(db.get_address(12, "rue des champignons"),
/// vec![Address {
/// lat: 0.,
/// lon: 0.,
/// number: Some("12".into()),
/// street: Some("rue des champignons".into()),
/// unit: None,
/// city: None,
/// district: None,
/// region: None,
/// postcode: None,
/// }]);
/// ```
fn get_address(&mut self, housenumber: i32, street: &str) -> Vec<Address>;
}

impl CompatibleDB for DB {
Expand All @@ -492,18 +435,6 @@ impl CompatibleDB for DB {
}
}

fn get_nb_cities(&mut self) -> i64 {
self.flush();
let mut stmt = self
.conn
.prepare("SELECT COUNT(DISTINCT city) FROM addresses;")
.expect("failed to prepare");
let mut iter = stmt
.query_map([], |row| row.get(0))
.expect("query_map failed");
iter.next().expect("no count???").expect("failed")
}

fn get_nb_addresses(&mut self) -> i64 {
self.flush();
let mut stmt = self
Expand Down Expand Up @@ -540,17 +471,6 @@ impl CompatibleDB for DB {
.map(|x| x.expect("failed"))
.collect()
}

fn get_address(&mut self, housenumber: i32, street: &str) -> Vec<Address> {
self.flush();
let mut stmt = self.conn
.prepare("SELECT lat, lon, number, street, unit, city, district, region, postcode FROM addresses WHERE number=?1 AND street=?2")
.expect("failed to prepare statement");
stmt.query_map(&[&housenumber as &dyn ToSql, &street], |row| row.try_into())
.expect("failed to insert into errors")
.map(|x| x.expect("failed parsing address"))
.collect()
}
}

impl Drop for DB {
Expand Down

0 comments on commit fb657f5

Please sign in to comment.