Skip to content

Commit

Permalink
Added Encoding. Added Database loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Miko0187 committed Aug 2, 2023
1 parent 302ede3 commit 7186fce
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
hashbrown = "0.14"
hashbrown = "0.14"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
zstd = "0.12.4"
16 changes: 16 additions & 0 deletions src/encoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::io::{Read, Write};

use zstd::stream::{read::Decoder, write::Encoder};

pub fn encode(data: &str) -> Vec<u8> {
let mut encoder = Encoder::new(Vec::new(), 0).unwrap();
encoder.write_all(data.as_bytes()).unwrap();
encoder.finish().unwrap()
}

pub fn decode(data: Vec<u8>) -> String {
let mut decoder = Decoder::new(data.as_slice()).unwrap();
let mut decoded = String::new();
decoder.read_to_string(&mut decoded).unwrap();
decoded
}
11 changes: 9 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use std::io::Read;

mod encoding;

mod stores {
pub mod container;
pub mod db;
Expand All @@ -8,11 +12,14 @@ pub mod values {
}

fn main() {
let mut db = stores::db::Database::new("test.db".to_string());
let mut file = std::fs::File::open("test.bin").unwrap();
let decoded = encoding::decode(file.bytes().map(|x| x.unwrap()).collect());
println!("{}", decoded);
/*let mut db = stores::db::Database::new("test.db".to_string());
db.create_container("TestContainer".to_string());
let container = db.get_container("TestContainer");
if container.is_none() {
panic!("Unable to get container");
}
}*/
}
52 changes: 49 additions & 3 deletions src/stores/db.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,70 @@
use std::{fs::File, io::Read, path::Path};

use hashbrown::HashMap;
use std::{fs::File, path::Path};

use crate::{stores::container, values::key::Key};
use crate::encoding;

use super::container::Container;

fn parse_database(data: String) -> Vec<String> {
let mut lines = Vec::new();
let mut block = String::new();
let mut in_block = false;

for line in data.lines() {
if line.starts_with("@CONTAINER") && !in_block {
in_block = true;
block = String::new();
} else if line.starts_with("@CONTAINER") && in_block {
panic!("Invalid database file");
} else if line.starts_with("@END") && in_block {
in_block = false;
lines.push(block.clone());
} else if line.starts_with("@END") && !in_block {
panic!("Invalid database file");
} else if in_block {
block.push_str(line);
block.push('\n');
}
}

lines
}

fn load_containers(file: String) -> Vec<HashMap<String, Container>> {
let parsed = {
let mut file = File::open(file).expect("Unable to open file");
let data = encoding::decode(file.bytes().map(|x| x.unwrap()).collect());
parse_database(data)
};
let mut containers = Vec::new();

for parse in parsed {
containers.push(serde_json::from_str(&parse).unwrap());
}

containers
}

pub struct Database {
pub filename: String,
pub containers: Vec<HashMap<String, Container>>,
}

impl Database {
pub(crate) fn new(filename: String) -> Database {
let containers: Vec<HashMap<String, Container>>;

if !Path::new(&filename).exists() {
File::create(&filename).expect("Unable to Database create file");
containers = Vec::new();
} else {
containers = load_containers(filename.clone());
}

Database {
filename,
containers: Vec::new(),
containers,
}
}

Expand Down

0 comments on commit 7186fce

Please sign in to comment.