Skip to content

Commit

Permalink
Merge pull request #30 from 0xBLCKLPTN/alice_database-dev
Browse files Browse the repository at this point in the history
Log Engine supported now.
  • Loading branch information
0xBLCKLPTN authored Oct 26, 2024
2 parents 8d809b3 + fa6aad0 commit f4e10a7
Show file tree
Hide file tree
Showing 10 changed files with 314 additions and 306 deletions.
2 changes: 1 addition & 1 deletion Plugins/Alice-Database/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "Alice-Database_DBMS"
version = "1.2.2"
version = "1.2.3"
edition = "2021"
include = ["**/*.rs", "Cargo.toml","proto/*.proto", "src/syntax/*.pest", "src/test.decl"]
license = "MIT"
Expand Down
5 changes: 3 additions & 2 deletions Plugins/Alice-Database/proto/instance.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ message Instance {

message CreateInstanceRequest {
string engine_type = 1;
string root_path = 2;
Token token = 3;
string name = 2;
string root_path = 3;
Token token = 4;
}

message CreateInstanceResponse {
Expand Down
2 changes: 2 additions & 0 deletions Plugins/Alice-Database/src/engines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. */

use crate::json_engine::JSONEngine;
use crate::log_engine::LOGEngine;

#[derive(Debug, Clone)]
pub enum Engines {
LOGEngine(LOGEngine),
JSONEngine(JSONEngine),
}
7 changes: 4 additions & 3 deletions Plugins/Alice-Database/src/grpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ impl InstanceService for GRPCInstanceManager
async fn create_instance(
&self, request: Request<CreateInstanceRequest>,
) -> Result<Response<CreateInstanceResponse>, Status> {

let engine_type = request.into_inner().engine_type;
let inner = request.into_inner();
let engine_type = inner.engine_type;
let name = inner.name;
let mut im = self.instance_manager.lock().unwrap();
let id = im.create_instance(&engine_type).unwrap();
let id = im.create_instance(&engine_type, &name).unwrap();

Ok(
Response::new( CreateInstanceResponse { instance: id } )
Expand Down
126 changes: 79 additions & 47 deletions Plugins/Alice-Database/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ use uuid::Uuid;
use std::path::{PathBuf, Path};
use std::fs::File;
use std::io::{self, BufRead};
use crate::JSONEngine;
use crate::{JSONEngine, LOGEngine};
use std::collections::HashMap;
use ring::{rand::{SecureRandom, SystemRandom}, hmac};
use rand::rngs::OsRng;
use crate::IMPestParser;
use pest_derive::Parser;
use pest::Parser;
Expand All @@ -54,51 +52,46 @@ pub struct InstanceManager {
pub authenticated_apps: HashMap<String, String>,
}


impl InstanceManager {
pub fn new(root_path: &PathBuf) -> Self {
let name = Uuid::new_v4().to_string();

let mut instances: Vec<Instance> = vec![];
let mut authenticated_apps: HashMap<String, String> = HashMap::new();
Self {name, instances, root_path: root_path.to_owned(), authenticated_apps}
Self {
name,
instances: vec![],
root_path: root_path.to_owned(),
authenticated_apps: HashMap::new(),
}
}

pub fn create_instance(&mut self, engine_type: &str) -> Result<String, Box<dyn std::error::Error>> {
let instance_name: String = Uuid::new_v4().to_string();

let mut engine = match engine_type {
pub fn create_instance(&mut self, engine_type: &str, name: &str) -> Result<String, Box<dyn std::error::Error>> {
let new_name = name.to_string();
let engine = match engine_type {
"json_engine" => Engines::JSONEngine(JSONEngine::new(&self.root_path)),
"log_engine" => Engines::LOGEngine(LOGEngine::new(&self.root_path)),
_ => {
println!("Engine not found.");
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::InvalidInput, "Engine type not found")));
std::io::ErrorKind::InvalidInput, "Engine type not found")));
}
};
let mut instance = Instance {engine, name: instance_name.clone()};
let instance = Instance { engine, name: new_name.clone() };
self.instances.push(instance);
Ok(instance_name)
Ok(new_name)
}

pub fn get_instance(&self, instance_name: &str) -> Option<&Instance> {
self.instances.iter().find(|i| i.name == instance_name)
}
// Method to mutate the engine of an existing instance

pub fn get_mutable_engine(&mut self, instance_name: &str) -> Option<&mut JSONEngine> {
for instance in &mut self.instances {
if instance.name == instance_name {
if let Engines::JSONEngine(ref mut engine) = instance.engine {
return Some(engine);
}
}
}
None
pub fn get_mutable_engine(&mut self, instance_name: &str) -> Option<&mut Engines> {
self.instances.iter_mut().find(|instance| instance.name == instance_name)
.map(|instance| &mut instance.engine)
}

pub fn sign_up(&mut self, app_name: String) -> String {
let key = Uuid::new_v4().to_string();
&self.authenticated_apps.insert(app_name, key.clone());
return key;
self.authenticated_apps.insert(app_name, key.clone());
key
}

pub fn get_all_apps(&self) {
Expand All @@ -113,22 +106,66 @@ impl InstanceManager {
match inner_pair.as_rule() {
Rule::create_instance => {
let inner = inner_pair.into_inner().as_str().split(" ENGINE ").collect::<Vec<_>>();
let instance_id = &self.create_instance(&inner[1]);
let instance_id = self.create_instance(inner[1], inner[0]);
match instance_id {
Ok(message) => adbprint!("NEW INSTANCE ID: {}", message),
Err(e) => adbprint!("{:#?}",e)
Err(e) => adbprint!("{:#?}", e),
}
},
Rule::get_instance => {
let inner = inner_pair.into_inner().as_str();
adbprint!("{:#?}", inner);
adbprint!("{:#?}", self.get_instance(inner));
},
Rule::get_instances => {
adbprint!("{:#?}", self.instances);
},
Rule::print_addbms => {
adbprint!("{:#?}", self);
},
Rule::create_collection => {
let inner = inner_pair.into_inner().as_str().split(" INSTANCE WITH NAME ").collect::<Vec<_>>();
if let Some(engine) = self.get_mutable_engine(inner[0]) {
match engine {
Engines::JSONEngine(json_engine) => {
json_engine.add_collection(inner[1]); // Call method for JSONEngine
},
Engines::LOGEngine(log_engine) => {
log_engine.add_collection(inner[1]); // Call method for LOGEngine
},
// You can add more engine variants here if needed
}
} else {
adbprint!("Instance not found: {}", inner[0]);
}
}

Rule::create_document => {
let inner = inner_pair.into_inner().as_str().split(" INSTANCE IN COLLECTION ").collect::<Vec<_>>();
let inner_two = inner[1].split(" WITH NAME ").collect::<Vec<_>>();

if let Some(engine) = self.get_mutable_engine(inner[0]) {
match engine {
Engines::JSONEngine(json_engine) => {
if let Some(collection) = json_engine.get_collection_mut(inner_two[0]) {
collection.add_document(inner_two[1], "");
adbprint!("DOCUMENT {} CREATED IN {}.{} C.I", inner_two[1], inner_two[0], inner[0]);
} else {
adbprint!("Collection not found in JSONEngine: {}", inner_two[0]);
}
},
Engines::LOGEngine(log_engine) => {
if let Some(collection) = log_engine.get_collection_mut(inner_two[0]) {
collection.add_document(inner_two[1], "");
adbprint!("DOCUMENT {} CREATED IN {}.{} C.I", inner_two[1], inner_two[0], inner[0]);
} else {
adbprint!("Collection not found in LOGEngine: {}", inner_two[0]);
}
},
}
} else {
adbprint!("Instance not found: {}", inner[0]);
}
},
_ => unreachable!("I don't know this command"),
}
}
Expand All @@ -143,27 +180,22 @@ impl InstanceManager {
}

pub fn wrapped_execute_cmd(&mut self, command: &str) -> Result<(), Box<dyn std::error::Error>> {
match &self.execute_cmd(command) {
Ok(_) => println!(""),
Err(e) => adbprint!("Error! {}", e),
}
Ok(())
self.execute_cmd(command)
.map_err(|e| { adbprint!("Error! {}", e); e })
}

pub fn execute_decl_file<P>(&mut self, filename: P) -> Result<(), io::Error>
where
P: AsRef<Path> {
let file = File::open(filename)?;
let reader = io::BufReader::new(file);
let mut lines: Vec<String> = Vec::new();

for line in reader.lines() {
self.wrapped_execute_cmd(line?.replace("\n", "").as_str());
P: AsRef<Path>,
{
let file = File::open(filename)?;
let reader = io::BufReader::new(file);

for line in reader.lines() {
if let Err(e) = self.wrapped_execute_cmd(&line?.replace("\n", "")) {
adbprint!("Failed to execute line: {}", e);
}
Ok(())
}
Ok(())
}
}




4 changes: 3 additions & 1 deletion Plugins/Alice-Database/src/json_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use serde_json::{json, Value, Result as JsonResult};
use log::{info, error, trace};
use simplelog::*;

use uuid::Uuid;
use chrono::Local;

const ROOT_DIR: &str = "Alice-Database-Data";
Expand Down Expand Up @@ -78,7 +79,8 @@ impl Collection {
let collection_path = Path::new(&self.path()).join(&self.name);
fs::create_dir_all(&collection_path)?;

let doc_path = collection_path.join(name); // Correctly construct the document path
let doc_path = collection_path.join(&name.clone()); // Correctly construct the document path

let mut file = fs::File::create(&doc_path)?;
file.write_all(content.as_bytes())?;

Expand Down
Loading

0 comments on commit f4e10a7

Please sign in to comment.