diff --git a/src/main.rs b/src/main.rs index 89e5e2b..0fad8e6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,7 @@ fn main() { let mut table = Table::init().unwrap(); - table.insert(peeps).unwrap(); + table.insert_collection(peeps, String::from("Potato")).unwrap(); // let mut table = Table::init().unwrap(); diff --git a/src/table.rs b/src/table.rs index 5b7cc5e..825e70e 100644 --- a/src/table.rs +++ b/src/table.rs @@ -10,32 +10,33 @@ use crate::collection::Collection; #[derive(Serialize, Deserialize, Clone)] struct TableRow { id: usize, - name: Uuid, + key: Uuid, num_entries: usize, path: String, - file_pos: u64 + file_pos: u64, + name: String } impl TableRow { - fn new(id: usize, name: Uuid, entry: Collection, file_pos: u64) -> Result>{ - let path = format!("{}.bson", name); + fn new(id: usize, key: Uuid, entry: Collection, file_pos: u64, name: String) -> Result>{ + let path = format!("{}.bson", key); let file = File::create(&path)?; entry.save(file)?; Ok( - TableRow { id: id, name: name, num_entries: entry.entries.len(), path: path, file_pos: file_pos } + TableRow { id: id, key, num_entries: entry.entries.len(), path: path, file_pos: file_pos, name: name } ) } } pub struct Table{ - rows: Vec + collections: std::collections::HashMap } impl Table { fn new() -> Table{ - Table{ rows:Vec::new()} + Table{ collections: std::collections::HashMap::new()} } pub fn init() -> Result>{ @@ -48,7 +49,9 @@ impl Table { let n = file.metadata()?.len(); let mut file = BufReader::new(file); while file.stream_position()? < n { - table.rows.push(serde_json::from_reader(&mut file)?); + let row : TableRow = serde_json::from_reader(&mut file)?; + let name = row.name.clone(); + table.collections.insert(name, row); } } Ok(table) @@ -57,14 +60,14 @@ impl Table { fn rewrite_table(&mut self) -> Result<(), Box>{ remove_file("metalize.table")?; let file = File::create("metalize.table")?; - for row in self.rows.iter(){ - let x = serde_json::to_value(row)?; + for coll in self.collections.iter(){ + let x = serde_json::to_value(coll.1)?; serde_json::to_writer(&file, &x)?; } Ok(()) } - pub fn insert(&mut self, entry: Collection) -> Result<(), Box> { + pub fn insert_collection(&mut self, entry: Collection, name: String) -> Result<(), Box> { let path = Path::new("metalize.table"); let y = if path.exists() { File::open(path)? @@ -73,20 +76,21 @@ impl Table { }; let new_pos = y.metadata()?.len(); let table_row = TableRow::new( - self.rows.len()+1, + self.collections.len()+1, Uuid::new_v4(), entry, - new_pos + new_pos, + name.clone() )?; let x = serde_json::to_value(&table_row)?; serde_json::to_writer(&y, &x)?; - self.rows.push(table_row); + self.collections.insert(name, table_row); Ok(()) } - pub fn add_to_collection(&mut self, collection_id : usize, object: T) -> Result<(), Box>{ + pub fn add_to_collection(&mut self, name: String, object: T) -> Result<(), Box>{ // TODO: this currently wipes out files and rewrites the entirety of them. Update to only change the sections that need to change. - let selected_row = self.get_row(collection_id); + let selected_row = self.get_row(name).expect("No collection with that name in table."); let mut selected_collection: Collection = Collection::load(File::open(&selected_row.path)?)?; remove_file(&selected_row.path)?; selected_collection.entries.push(object); @@ -96,9 +100,9 @@ impl Table { Ok(()) } - pub fn update(&mut self, id: usize, entry: Collection)-> Result<(), Box>{ + pub fn update(&mut self, name: String, entry: Collection)-> Result<(), Box>{ // TODO: this currently wipes out files and rewrites the entirety of them. Update to only change the sections that need to change. - let selected_row = self.get_row(id); + let selected_row = self.get_row(name).expect("No collection with that name in table."); remove_file(&selected_row.path)?; selected_row.num_entries = entry.entries.len(); entry.save(File::create(&selected_row.path)?)?; @@ -106,15 +110,13 @@ impl Table { Ok(()) } - fn get_row(&mut self, id: usize) -> &mut TableRow{ - let row_index = self.rows.iter().position(move |row |{ - row.id == id - }).expect("Failed to find that row in table."); - &mut self.rows[row_index] + fn get_row(&mut self, name: String) -> Result<&mut TableRow, Box>{ + let row = self.collections.get_mut(&name).expect("No collection with that name in table."); + Ok(row) } - pub fn get(&mut self, id: usize)-> Result, Box>{ - let table_row = self.get_row(id); + pub fn get(&mut self, name:String)-> Result, Box>{ + let table_row = self.get_row(name).expect("No collection with that name in table."); let file = File::open(&table_row.path)?; Collection::load(file) }