Skip to content

Commit

Permalink
Merge pull request #31 from 0xBLCKLPTN/alice_database-dev
Browse files Browse the repository at this point in the history
New lib
  • Loading branch information
0xBLCKLPTN authored Oct 26, 2024
2 parents 8ff84f5 + f8f7c3d commit dbcc1d4
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 50 deletions.
4 changes: 2 additions & 2 deletions 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.3"
name = "AliceDBMS"
version = "1.2.8"
edition = "2021"
include = ["**/*.rs", "Cargo.toml","proto/*.proto", "src/syntax/*.pest", "src/test.decl"]
license = "MIT"
Expand Down
21 changes: 5 additions & 16 deletions Plugins/Alice-Database/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ To set up Alice DBMS, ensure you have Rust and Cargo installed. You can then add

```toml
[dependencies]
Alice-Database_DBMS = "=0.1.0"
AliceDBMS = "^1.2.7"
```

Then, run:
Expand All @@ -44,22 +44,11 @@ cargo build
Here's a brief example of how to use the Alice DBMS module:

```rust
use AliceDBMS::prelude::*;

fn main() -> std::io::Result<()> {
// Define the root path for data storage
let root_path = Path::new("path_to_your_database");
let mut manager = CollectionManager::new(&root_path);

// Add a new collection
manager.add_collection("example_collection").unwrap();

// Prepare document content
let doc_content = json!({"key": "value"}).to_string();

// Add a new document to the collection
manager.get_collection_mut("example_collection").unwrap()
.add_document("example_doc.json", &doc_content).unwrap();

println!("Document added successfully!");
let mut instance_manager = InstanceManager::new(&get_root_path());
cli(&mut instance_manager);
Ok(())
}
```
Expand Down
21 changes: 21 additions & 0 deletions Plugins/Alice-Database/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
/* MIT License
Copyright (c) 2024 Daniil Ermolaev
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. */

fn main() {
tonic_build::compile_protos("proto/instance.proto").unwrap();
Expand Down
26 changes: 24 additions & 2 deletions Plugins/Alice-Database/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
use std::io::{self, Write};
/* MIT License
Copyright (c) 2024 Daniil Ermolaev
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. */

use std::io::{ self, Write };
use crate::instance::*;

pub fn cli(instance_manager: &mut InstanceManager) {
loop {
print!("[ Instance Manager ] (type 'exit' to quit)=: ");
print!("[ INSTANCE MANAGER ] (type 'exit' to quit)=: ");
io::stdout().flush().unwrap();

let mut input = String::new();
Expand Down
4 changes: 4 additions & 0 deletions Plugins/Alice-Database/src/engines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ SOFTWARE. */
use crate::json_engine::JSONEngine;
use crate::log_engine::LOGEngine;

use std::fmt;

#[derive(Debug, Clone)]
pub enum Engines {
LOGEngine(LOGEngine),
JSONEngine(JSONEngine),
}


27 changes: 20 additions & 7 deletions Plugins/Alice-Database/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. */

use crate::Engines;
use crate::engines::Engines;
use uuid::Uuid;
use std::path::{PathBuf, Path};
use std::fs::File;
use std::io::{self, BufRead};
use crate::{JSONEngine, LOGEngine};

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

use std::collections::HashMap;
use crate::IMPestParser;
use crate::command_executor::IMPestParser;
use pest_derive::Parser;
use pest::Parser;
use crate::Rule;
use crate::command_executor::Rule;

macro_rules! adbprint {
($($arg:tt)*) => {
Expand Down Expand Up @@ -122,6 +125,9 @@ impl InstanceManager {
Rule::print_addbms => {
adbprint!("{:#?}", self);
},
Rule::supported_engines => {
adbprint!("LOGEngine (log_engine)\nJSONEngine (json_engine)");
},
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]) {
Expand Down Expand Up @@ -183,16 +189,23 @@ impl InstanceManager {
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>,
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", "")) {
let line = line?;

// Check if the line starts with '#', continue if it does
if line.trim_start().starts_with('#') {
continue; // Skip this line
}

// Execute the command after removing newline characters
if let Err(e) = self.wrapped_execute_cmd(&line.replace("\n", "")) {
adbprint!("Failed to execute line: {}", e);
}
}
Expand Down
1 change: 1 addition & 0 deletions Plugins/Alice-Database/src/json_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use std::fs;
use std::io::{self, Read, Write};
use std::env;
use std::path::{PathBuf, Path};

use serde_json::{json, Value, Result as JsonResult};

use log::{info, error, trace};
Expand Down
12 changes: 12 additions & 0 deletions Plugins/Alice-Database/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pub mod json_engine;
pub mod log_engine;

pub mod engines;

pub mod grpc_server;
pub mod instance;
pub mod utils;
pub mod command_executor;
pub mod cli;

pub mod prelude;
23 changes: 23 additions & 0 deletions Plugins/Alice-Database/src/log_engine.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
/* MIT License
Copyright (c) 2024 Daniil Ermolaev
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. */

// I want to replace this code to universal file.

use std::fs::*;
use std::io::{self, Read, Write};
Expand Down
25 changes: 3 additions & 22 deletions Plugins/Alice-Database/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/* MIT License
Copyright (c) 2024 Daniil Ermolaev
Expand All @@ -21,18 +20,6 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. */

use std::{ env, fs };
use std::io::{ self, Read, Write };
use std::path::{ PathBuf, Path };
use std::sync::{ Arc, Mutex };

use serde_json::{ json, Value, Result as JsonResult };

use log::{ info, error, trace };
use simplelog::*;

use chrono::Local;

pub mod json_engine;
pub mod log_engine;
pub mod engines;
Expand All @@ -42,14 +29,9 @@ pub mod utils;
pub mod command_executor;
pub mod cli;

use json_engine::*;
use log_engine::*;
use engines::*;
use grpc_server::*;
use instance::*;
use utils::*;
use command_executor::*;
use cli::cli;
pub mod prelude;
use prelude::*;

/* gRPC
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand All @@ -73,7 +55,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let root_path: PathBuf = get_root_path();

let mut im = InstanceManager::new(&root_path);
//let k = im.execute_decl_file(Path::new("./test.decl"));
cli(&mut im);
Expand Down
63 changes: 63 additions & 0 deletions Plugins/Alice-Database/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* MIT License
Copyright (c) 2024 Daniil Ermolaev
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. */

pub use std::{ env, fs, fmt };
pub use std::io::{ self, Read, Write, BufRead };
pub use std::path::{ PathBuf, Path };
pub use std::sync::{ Arc, Mutex };

pub use pest_derive::Parser;
pub use pest::Parser;

pub use serde_json::{ json, Value, Result as JsonResult };

pub use log::{ info, error, trace };
pub use simplelog::*;

pub use chrono::Local;

/*
pub mod json_engine;
pub mod log_engine;
pub mod engines;
pub mod grpc_server;
pub mod instance;
pub mod utils;
pub mod command_executor;
pub mod cli;
*/

pub use crate::json_engine::*;
pub use crate::log_engine::*;

pub use crate::engines::*;
pub use crate::grpc_server::*;
pub use crate::instance::*;
pub use crate::utils::*;
pub use crate::command_executor::*;
pub use crate::cli::cli;

pub use tonic::{ transport::Server, Request, Response, Status };

pub use uuid::Uuid;
pub use std::collections::HashMap;

5 changes: 4 additions & 1 deletion Plugins/Alice-Database/src/syntax/instance_manager.pest
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ print_addbms = { "PRINTALLDDBMS" }
create_collection = { "CREATE COLLECTION IN " ~ ident ~ " INSTANCE WITH NAME " ~ ident }
create_document = { "CREATE DOCUMENT IN " ~ ident ~ " INSTANCE IN COLLECTION " ~ ident ~ " WITH NAME " ~ ident}

supported_engines = { "GET SUPPORTED ENGINES" }


sql_statement = {
create_instance | get_instance | get_instances | delete_instance | print_addbms |
create_collection | create_document
create_collection | create_document | supported_engines
}

sql_statements = _{ sql_statement ~ ((";" ~ sql_statement) | (";" ~ sql_statement))* ~ ";"? }

0 comments on commit dbcc1d4

Please sign in to comment.