first commit

This commit is contained in:
Elias Renman
2023-04-02 13:50:15 +02:00
commit f390bfb0d5
10 changed files with 256 additions and 0 deletions

69
src/database/table.rs Normal file
View File

@@ -0,0 +1,69 @@
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fs};
use super::{index::Index, row::Row};
#[derive(Serialize, Deserialize)]
pub struct Table {
indexes: Vec<Index>,
rows: HashMap<u64, Row>,
name: String,
pk_key: String,
}
impl Table {
pub fn new(name: &str, pk_key: &str) -> Table {
Table { indexes: vec![], rows: HashMap::new(), name: name.to_string(), pk_key: pk_key.to_string() }
}
pub fn from_file(file_path: String) -> Table {
let serialized = fs::read_to_string(file_path);
if serialized.is_err() {
panic!("Failed loading from file");
}
let table: Table = serde_json::from_str(&serialized.unwrap()).unwrap();
return table;
}
pub fn to_file(&self, file_path: String) {
let serialized = serde_json::to_string(self).unwrap();
println!("serialized = {}", serialized);
let result = fs::write(file_path, serialized);
if result.is_err() {
panic!("Failed writing table to file")
}
}
pub fn create_index(&mut self, index: Index) {
self.indexes.push(index)
}
pub fn find_by_pk(&self, value: &u64) -> &Row {
let row = self.rows.get(value);
if row.is_none() {
panic!("Row not found!");
}
return row.unwrap().clone();
}
pub fn insert_row(&mut self, row: Row) {
let key_option = row.columns.get(&self.pk_key);
if key_option.is_none() {
panic!("Primary key not found on row to insert!");
}
let key = key_option.unwrap().as_u64();
if key.is_none() {
panic!("Primary key is not of type u64");
}
self.rows.insert(key.unwrap(), row);
}
}