|
|
@ -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", |
|
|
|