From e7603974949bf924501fc7d996dd8e2bf5512bde Mon Sep 17 00:00:00 2001 From: Liam Fitzpatrick Date: Fri, 12 Apr 2024 08:29:45 -0400 Subject: [PATCH] created fn for add and update rows in table --- src/main.rs | 2 +- src/table.rs | 59 +++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5c57294..8dcba53 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,7 @@ fn main() { let mut table = Table::init().unwrap(); - let mut peeps: Collection = table.get(1).unwrap(); + table.addToCollection(1, Potato{bob: 66.0}).unwrap(); println!("blah"); } \ No newline at end of file diff --git a/src/table.rs b/src/table.rs index cf3a553..1194963 100644 --- a/src/table.rs +++ b/src/table.rs @@ -1,29 +1,30 @@ -use std::{fs::File, io::{Seek, BufReader}}; +use core::fmt; +use std::{fs::{File,remove_file}, io::{Seek, BufReader}}; use serde::{de::DeserializeOwned, Deserialize, Serialize}; use std::error::Error; use uuid::Uuid; use std::path::Path; use serde_json; - - use crate::collection::Collection; -#[derive(Serialize, Deserialize)] + +#[derive(Serialize, Deserialize, Clone)] struct TableRow { id: usize, name: Uuid, num_entries: usize, - path: String + path: String, + file_pos: u64 } impl TableRow { - fn new(id: usize, name: Uuid, entry: Collection) -> Result>{ + fn new(id: usize, name: Uuid, entry: Collection, file_pos: u64) -> Result>{ let path = format!("{}.bson", name); let mut file = File::create(&path)?; entry.save(file)?; Ok( - TableRow { id: id, name: name, num_entries: entry.entries.len(), path: path } + TableRow { id: id, name: name, num_entries: entry.entries.len(), path: path, file_pos: file_pos } ) } } @@ -55,26 +56,54 @@ impl Table { } pub fn insert(&mut self, entry: Collection) -> Result<(), Box> { - let table_row = TableRow::new( - self.rows.len()+1, - Uuid::new_v4(), - entry - )?; let path = Path::new("metalize.table"); - let y = if path.exists() { File::open(path)? } else { File::create(path)? }; + let new_pos = y.metadata()?.len(); + let table_row = TableRow::new( + self.rows.len()+1, + Uuid::new_v4(), + entry, + new_pos + )?; let x = serde_json::to_value(&table_row)?; serde_json::to_writer(&y, &x)?; self.rows.push(table_row); Ok(()) } - pub fn get(&self, id: usize)-> Result, Box>{ - let table_row = self.rows.get(id-1).expect("Could not access that id."); + pub fn addToCollection(&mut self, collection_id : usize, 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.getRow(collection_id); + let mut selected_collection: Collection = Collection::load(File::open(&selected_row.path)?)?; + remove_file(&selected_row.path)?; + selected_collection.entries.push(object); + selected_row.num_entries = selected_collection.entries.len(); + selected_collection.save(File::create(&selected_row.path)?)?; + Ok(()) + } + + pub fn update(&mut self, id: usize, 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.getRow(id); + remove_file(&selected_row.path)?; + selected_row.num_entries = entry.entries.len(); + entry.save(File::create(&selected_row.path)?)?; + Ok(()) + } + + fn getRow(&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] + } + + pub fn get(&mut self, id: usize)-> Result, Box>{ + let table_row = self.getRow(id); let mut file = File::open(&table_row.path)?; Collection::load(file) }