Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
InvalidJoker committed Aug 2, 2023
1 parent 27eae2d commit 302ede3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
10 changes: 9 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,12 @@ pub mod values {
pub mod key;
}

fn main() {}
fn main() {
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");
}
}
8 changes: 4 additions & 4 deletions src/stores/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use hashbrown::HashMap;
use crate::values::key::Key;

pub struct Container {
name: String,
documents: Vec<HashMap<String, Key>>,
pub name: String,
pub documents: Vec<HashMap<String, Key>>,
}

/*
Expand Down Expand Up @@ -35,13 +35,13 @@ impl Container {
}
}

pub fn add_document(&mut self, name: String, key: Key) {
pub fn add(&mut self, name: String, key: Key) {
let mut document = HashMap::new();
document.insert(name, key);
self.documents.push(document);
}

pub fn get_document(&self, index: usize) -> Option<&HashMap<String, Key>> {
pub fn get(&self, index: usize) -> Option<&HashMap<String, Key>> {
self.documents.get(index)
}
}
38 changes: 23 additions & 15 deletions src/stores/db.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
use hashbrown::HashMap;
use std::{fs::File, path::Path};

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

use super::container::Container;

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

impl Database {
fn read_collections() -> Vec<container::Container> {
unimplemented!()
}

pub fn create_collection() {
unimplemented!()
}
pub fn delete_collection() {
unimplemented!()
}

pub(crate) fn new(filename: String) -> Database {
if !Path::new(&filename).exists() {
File::create(&filename).expect("Unable to Database create file");
}

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

pub fn get_container(&self, name: &str) -> Option<&Container> {
for container in &self.containers {
if let Some(container) = container.get(name) {
return Some(container);
}
}

None
}

pub fn create_container(&mut self, name: String) {
let mut container = HashMap::new();
container.insert(name.clone(), Container::new(name));
self.containers.push(container);
}
}

0 comments on commit 302ede3

Please sign in to comment.