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

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

89
Cargo.lock generated Normal file
View File

@@ -0,0 +1,89 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "db"
version = "0.1.0"
dependencies = [
"serde",
"serde_json",
]
[[package]]
name = "itoa"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6"
[[package]]
name = "proc-macro2"
version = "1.0.55"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d0dd4be24fcdcfeaa12a432d588dc59bbad6cad3510c67e74a2b6b2fc950564"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc"
dependencies = [
"proc-macro2",
]
[[package]]
name = "ryu"
version = "1.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041"
[[package]]
name = "serde"
version = "1.0.159"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c04e8343c3daeec41f58990b9d77068df31209f2af111e059e9fe9646693065"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.159"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c614d17805b093df4b147b51339e7e44bf05ef59fba1e45d83500bcfb4d8585"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d721eca97ac802aa7777b701877c8004d950fc142651367300d21c1cc0194744"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "syn"
version = "2.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c9da457c5285ac1f936ebd076af6dac17a61cfe7826f2076b4d015cf47bc8ec"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4"

10
Cargo.toml Normal file
View File

@@ -0,0 +1,10 @@
[package]
name = "db"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

24
infrastructure.md Normal file
View File

@@ -0,0 +1,24 @@
# A High level overview
Database >
Tables -> has many rows
### Strucutres
#### Table
* Row[]
* Index[]
* pk-key
* name: Str
#### Row
* Key
* Value
#### Index
* pk-key
* key
* direction, asc - desc

1
readme.md Normal file
View File

@@ -0,0 +1 @@
# A homemade database with indexing.

11
src/database/index.rs Normal file
View File

@@ -0,0 +1,11 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct Index {
pk: u64,
key: String,
asd_sort: bool,
unique: bool,
}

3
src/database/mod.rs Normal file
View File

@@ -0,0 +1,3 @@
pub mod index;
pub mod table;
pub mod row;

14
src/database/row.rs Normal file
View File

@@ -0,0 +1,14 @@
use serde_json::Value;
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct Row {
pub columns: HashMap<String, Value>
}
impl Row {
pub fn new(cols: HashMap<String, Value>) -> Row {
Row {columns: cols}
}
}

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);
}
}

34
src/main.rs Normal file
View File

@@ -0,0 +1,34 @@
use serde_json::Value;
macro_rules! hashmap {
($( $key: expr => $val: expr ),*) => {{
let mut map: ::std::collections::HashMap<String, Value> = ::std::collections::HashMap::new();
$( map.insert($key.to_string(), serde_json::to_value($val).unwrap()); )*
map
}}
}
use database::{table::Table, row::Row};
mod database;
fn main() {
let mut table: Table = Table::new("Cats", "id");
table.insert_row(Row::new(hashmap!["id" => 1, "name" => "Ozzy"]));
table.insert_row(Row::new(hashmap!["id" => 2, "name" => "Simon"]));
let row = table.find_by_pk(&1u64);
let pretty_print = serde_json::to_string_pretty(row);
println!("Found Row: {}", pretty_print.unwrap());
let row = table.find_by_pk(&2u64);
let pretty_print = serde_json::to_string_pretty(row);
println!("Found Row: {}", pretty_print.unwrap());
let row = table.find_by_pk(&3u64);
let pretty_print = serde_json::to_string_pretty(row);
println!("Failed to Find Row: {}", pretty_print.unwrap());
}