operates with a String key instead of usize id for get
This commit is contained in:
parent
d3600c46da
commit
1b46f9a604
@ -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();
|
||||
|
||||
|
||||
52
src/table.rs
52
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<T:Serialize>(id: usize, name: Uuid, entry: Collection<T>, file_pos: u64) -> Result<TableRow, Box<dyn Error>>{
|
||||
let path = format!("{}.bson", name);
|
||||
fn new<T:Serialize>(id: usize, key: Uuid, entry: Collection<T>, file_pos: u64, name: String) -> Result<TableRow, Box<dyn Error>>{
|
||||
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<TableRow>
|
||||
collections: std::collections::HashMap<String, TableRow>
|
||||
}
|
||||
|
||||
impl Table {
|
||||
|
||||
fn new() -> Table{
|
||||
Table{ rows:Vec::new()}
|
||||
Table{ collections: std::collections::HashMap::new()}
|
||||
}
|
||||
|
||||
pub fn init() -> Result<Table, Box<dyn Error>>{
|
||||
@ -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<dyn Error>>{
|
||||
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<T:Serialize>(&mut self, entry: Collection<T>) -> Result<(), Box<dyn Error>> {
|
||||
pub fn insert_collection<T:Serialize>(&mut self, entry: Collection<T>, name: String) -> Result<(), Box<dyn Error>> {
|
||||
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<T:Serialize + DeserializeOwned>(&mut self, collection_id : usize, object: T) -> Result<(), Box<dyn Error>>{
|
||||
pub fn add_to_collection<T:Serialize + DeserializeOwned>(&mut self, name: String, object: T) -> Result<(), Box<dyn Error>>{
|
||||
// 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<T> = 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<T:Serialize>(&mut self, id: usize, entry: Collection<T>)-> Result<(), Box<dyn Error>>{
|
||||
pub fn update<T:Serialize>(&mut self, name: String, entry: Collection<T>)-> Result<(), Box<dyn Error>>{
|
||||
// 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<dyn Error>>{
|
||||
let row = self.collections.get_mut(&name).expect("No collection with that name in table.");
|
||||
Ok(row)
|
||||
}
|
||||
|
||||
pub fn get<T:DeserializeOwned>(&mut self, id: usize)-> Result<Collection<T>, Box<dyn Error>>{
|
||||
let table_row = self.get_row(id);
|
||||
pub fn get<T:DeserializeOwned>(&mut self, name:String)-> Result<Collection<T>, Box<dyn Error>>{
|
||||
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)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user