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.
This commit is contained in:
parent
012b079e46
commit
c6676c6681
6 changed files with 20 additions and 7 deletions
|
@ -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);
|
||||
|
|
17
src/db.rs
17
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<Self> {
|
||||
pub fn from_file(filename: &str, read_bounds: bool) -> Result<Self> {
|
||||
let connection = Connection::open(filename)?;
|
||||
let mut layouts: HashMap<u32, Vec<(u32, u32)>> = 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<u32> {
|
||||
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<MergeLowerBound>) -> 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",
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue