From c6676c6681d697650181779bdc3544b3992cf476 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Sejkora?= Date: Sun, 21 Feb 2021 00:01:38 +0100 Subject: [PATCH] Allow loading the DB without loading the merge bounds. There are so many that it's a waste of memory and time to load those if they are not needed. --- src/combine-layouts.rs | 2 +- src/db.rs | 17 +++++++++++++++-- src/import-logs.rs | 2 +- src/main.rs | 2 +- src/optimize-subcity.rs | 2 +- src/upload-bot.rs | 2 +- 6 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/combine-layouts.rs b/src/combine-layouts.rs index 81e8297..08e8e5f 100644 --- a/src/combine-layouts.rs +++ b/src/combine-layouts.rs @@ -8,7 +8,7 @@ mod db; mod combine; fn main() { - let mut db = SqliteLayoutDB::from_file("layouts.sqlite").expect("Failed to load the DB"); + let mut db = SqliteLayoutDB::from_file("layouts.sqlite", true).expect("Failed to load the DB"); eprintln!("Loaded the DB, {} stored layouts", db.layouts().len()); let city = City::read_from_file("01.in", city::INPUT_CITY_WIDTH, city::INPUT_CITY_HEIGHT); diff --git a/src/db.rs b/src/db.rs index a276154..c691b8c 100644 --- a/src/db.rs +++ b/src/db.rs @@ -5,6 +5,7 @@ use std::collections::HashMap; pub struct SqliteLayoutDB { connection: Connection, memory_db: MemoryLayoutDB, + bounds_accessible: bool } pub struct MemoryLayoutDB { @@ -135,7 +136,7 @@ impl LayoutDB for SqliteLayoutDB { } impl SqliteLayoutDB { - pub fn from_file(filename: &str) -> Result { + pub fn from_file(filename: &str, read_bounds: bool) -> Result { let connection = Connection::open(filename)?; let mut layouts: HashMap> = HashMap::new(); @@ -153,6 +154,7 @@ impl SqliteLayoutDB { } let mut merges = HashMap::new(); + if read_bounds { let mut stmt = connection.prepare("SELECT left_layout_id, right_layout_id, axis, price FROM merge_lower_bounds")?; let mut rows = stmt.query(NO_PARAMS)?; @@ -169,6 +171,8 @@ impl SqliteLayoutDB { price, }); } + } else { + } let layouts = layouts.into_iter().map(|(id, xy_pairs)| @@ -180,7 +184,7 @@ impl SqliteLayoutDB { ).collect(); let memory_db = MemoryLayoutDB::from_collections(layouts, merges); - Ok(SqliteLayoutDB { connection, memory_db}) + Ok(SqliteLayoutDB { connection, memory_db, bounds_accessible: read_bounds}) } pub fn memory_db(&self) -> &MemoryLayoutDB { @@ -206,10 +210,16 @@ impl SqliteLayoutDB { } pub fn get_merge_lower_bound(&self, left_layout: &SavedLayout, right_layout: &SavedLayout, y_axis: bool) -> Option { + if !self.bounds_accessible { + panic!("Bounds are not loaded!") + } self.memory_db.get_merge_lower_bound(left_layout, right_layout, y_axis) } pub fn add_merge_lower_bound(&mut self, lower_bound: MergeLowerBound) -> Result<()> { + if !self.bounds_accessible { + panic!("Bounds are not loaded!") + } let transaction = self.connection.transaction()?; transaction.execute("INSERT INTO merge_lower_bounds (left_layout_id, right_layout_id, axis, price) VALUES (?1, ?2, ?3, ?4) ON CONFLICT(left_layout_id, right_layout_id) DO UPDATE SET price = ?4", params![lower_bound.left_layout_id as u32, @@ -224,6 +234,9 @@ impl SqliteLayoutDB { } pub fn add_merge_lower_bounds(&mut self, lower_bounds: Vec) -> Result<()> { + if !self.bounds_accessible { + panic!("Bounds are not loaded!") + } let transaction = self.connection.transaction()?; for lower_bound in &lower_bounds { transaction.execute("INSERT INTO merge_lower_bounds (left_layout_id, right_layout_id, axis, price) VALUES (?1, ?2, ?3, ?4) ON CONFLICT(left_layout_id, right_layout_id, axis) DO UPDATE SET price = ?4", diff --git a/src/import-logs.rs b/src/import-logs.rs index 6e1b430..0844df4 100644 --- a/src/import-logs.rs +++ b/src/import-logs.rs @@ -14,7 +14,7 @@ enum State { } fn main() { - let mut db = SqliteLayoutDB::from_file("layouts.sqlite").expect("Failed to load the DB"); + let mut db = SqliteLayoutDB::from_file("layouts.sqlite", false).expect("Failed to load the DB"); let stdin = io::stdin(); let price_regex = Regex::new("^Price ([0-9]*), seed ([0-9]*)$").unwrap(); diff --git a/src/main.rs b/src/main.rs index 65cf214..1e2161a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,7 @@ enum RunType { } fn main() { - let mut db = SqliteLayoutDB::from_file("layouts.sqlite").expect("Failed to load the DB"); + let mut db = SqliteLayoutDB::from_file("layouts.sqlite", false).expect("Failed to load the DB"); eprintln!("Loaded the DB, {} stored layouts", db.layouts().len()); let city = City::read_from_file("01.in", city::INPUT_CITY_WIDTH, city::INPUT_CITY_HEIGHT); diff --git a/src/optimize-subcity.rs b/src/optimize-subcity.rs index 7abf957..5fddf01 100644 --- a/src/optimize-subcity.rs +++ b/src/optimize-subcity.rs @@ -13,7 +13,7 @@ mod optimization; mod population; fn main() { - let mut sqlite_db = SqliteLayoutDB::from_file("layouts.sqlite").expect("Failed to load the DB"); + let mut sqlite_db = SqliteLayoutDB::from_file("layouts.sqlite", false).expect("Failed to load the DB"); eprintln!("Loaded the DB, {} stored layouts", sqlite_db.layouts().len()); let city = City::read_from_file("01.in", city::INPUT_CITY_WIDTH, city::INPUT_CITY_HEIGHT); diff --git a/src/upload-bot.rs b/src/upload-bot.rs index f0b767f..9782145 100644 --- a/src/upload-bot.rs +++ b/src/upload-bot.rs @@ -23,7 +23,7 @@ fn main() { } }; - let db = SqliteLayoutDB::from_file("layouts.sqlite").expect("Failed to load the DB"); + let db = SqliteLayoutDB::from_file("layouts.sqlite", false).expect("Failed to load the DB"); eprintln!("Loaded the DB, {} stored layouts", db.layouts().len()); let city = City::read_from_file("01.in", city::INPUT_CITY_WIDTH, city::INPUT_CITY_HEIGHT);