Alice_Database_DBMS/json_engine.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
/* 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::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};
use simplelog::*;
use chrono::Local;
const ROOT_DIR: &str = "Alice-Database-Data";
const ADB_DATA_DIR: &str = "ADB_Data";
const JSON_ENGINE_DIR: &str = "json_engine";
/// A struct representing a document in the database.
///
/// A `Document` contains its name, file path, and its content stored as a JSON `Value`.
#[derive(Debug, Clone)]
pub struct Document {
pub name: String,
pub path: PathBuf,
pub json_value: Option<Value>,
}
/// A struct representing a collection of documents.
///
/// A `Collection` holds a name and a list of associated `Document`s.
#[derive(Debug, Clone)]
pub struct Collection {
pub name: String,
pub documents: Vec<Document>,
}
impl Collection {
// Method to get a document by name
pub fn get_document(&self, name: &str) -> Option<&Document> {
self.documents.iter().find(|doc| doc.name == name)
}
/// Retrieve a mutable reference to a document by its name.
///
/// # Parameters
/// - `name`: The name of the document to retrieve.
///
/// # Returns
/// An `Option` containing a mutable reference to the `Document` if found,
/// or `None` if not found.
pub fn get_document_mut(&mut self, name: &str) -> Option<&mut Document> {
self.documents.iter_mut().find(|doc| doc.name == name)
}
// Method to add a new document to the collection
pub fn add_document(&mut self, name: &str, content: &str) -> io::Result<()> {
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 mut file = fs::File::create(&doc_path)?;
file.write_all(content.as_bytes())?;
// Create a new Document instance
let new_document = Document {
name: name.to_string(),
path: doc_path.clone(),
json_value: parse_json_data(content).ok(),
};
self.documents.push(new_document);
Ok(())
}
// Method to delete a document from the collection
pub fn delete_document(&mut self, name: &str) -> io::Result<()> {
if let Some(doc) = self.documents.iter().find(|doc| doc.name == name) {
fs::remove_file(&doc.path)?;
self.documents.retain(|doc| doc.name != name);
Ok(())
} else {
Err(io::Error::new(io::ErrorKind::NotFound, "Document not found"))
}
}
fn path(&self) -> PathBuf {
let home_dir = env::home_dir().expect("Failed to get home directory");
home_dir.join(ROOT_DIR).join(ADB_DATA_DIR).join(JSON_ENGINE_DIR)
}
}
/// A struct to manage multiple collections of documents.
#[derive(Debug, Clone)]
pub struct JSONEngine {
collections: Vec<Collection>,
}
impl JSONEngine {
/// Create a new `JSONEngine`.
///
/// # Parameters
/// - `root`: The path to the root directory for data storage.
///
/// # Returns
/// A new instance of `JSONEngine`.
pub fn new(root: &Path, ) -> Self {
let collections = get_exists_collections(root);
JSONEngine { collections }
}
/// Retrieve a mutable reference to a collection by its name.
///
/// # Parameters
/// - `name`: The name of the collection to retrieve.
///
/// # Returns
/// An `Option` containing a mutable reference to the `Collection`, if found.
pub fn get_collection_mut(&mut self, name: &str) -> Option<&mut Collection> {
self.collections.iter_mut().find(|col| col.name == name)
}
/// Add a new collection.
///
/// # Parameters
/// - `name`: The name of the collection to create.
///
/// # Returns
/// An `Option` containing a mutable reference to the newly added `Collection`.
pub fn add_collection(&mut self, name: &str) -> Option<&mut Collection> {
let collection_path = Path::new(&self.root_path()).join(name);
fs::create_dir_all(&collection_path).ok()?; // Create the directory for new collection
let new_collection = Collection {
name: name.to_string(),
documents: vec![],
};
self.collections.push(new_collection);
self.collections.last_mut() // Return a mutable reference to the newly added collection
}
/// Get a collection by name.
///
/// # Parameters
/// - `name`: The name of the collection to retrieve.
///
/// # Returns
/// An `Option` containing a reference to the `Collection`, if found.
pub fn get_collection(&self, name: &str) -> Option<&Collection> {
self.collections.iter().find(|col| col.name == name)
}
/// Get a document from a specific collection.
///
/// # Parameters
/// - `collection_name`: The name of the collection the document belongs to.
/// - `document_name`: The name of the document to retrieve.
///
/// # Returns
/// An `Option` containing a reference to the `Document`, if found.
pub fn get_document(&self, collection_name: &str, document_name: &str) -> Option<&Document> {
self.get_collection(collection_name)?.get_document(document_name)
}
fn root_path(&self) -> PathBuf {
let home_dir = env::home_dir().expect("Failed to get home directory");
home_dir.join(ROOT_DIR).join(ADB_DATA_DIR).join(JSON_ENGINE_DIR)
}
}
impl Document {
/// Update a field in the document.
///
/// # Parameters
/// - `key`: The key of the field to update.
/// - `value`: The new value for the field.
///
/// # Returns
/// A result indicating success or failure.
pub fn update_rows(&mut self, key: &str, value: &Value) -> io::Result<()> {
if let Some(json_value) = &mut self.json_value {
if let Some(obj) = json_value.as_object_mut() {
obj.insert(key.to_string(), value.clone());
let updated_content = serde_json::to_string_pretty(json_value)?;
let mut file = fs::File::create(&self.path)?;
file.write_all(updated_content.as_bytes())?;
Ok(())
} else {
Err(io::Error::new(io::ErrorKind::InvalidData, "JSON is not an object"))
}
} else {
Err(io::Error::new(io::ErrorKind::InvalidData, "Document does not contain valid JSON"))
}
}
/// Delete a field in the document.
///
/// # Parameters
/// - `key`: The key of the field to delete.
///
/// # Returns
/// A result indicating success or failure.
pub fn delete_rows(&mut self, key: &str) -> io::Result<()> {
if let Some(json_value) = &mut self.json_value {
if let Some(obj) = json_value.as_object_mut() {
obj.remove(key);
let updated_content = serde_json::to_string_pretty(json_value)?;
let mut file = fs::File::create(&self.path)?;
file.write_all(updated_content.as_bytes())?;
Ok(())
} else {
Err(io::Error::new(io::ErrorKind::InvalidData, "JSON is not an object"))
}
} else {
Err(io::Error::new(io::ErrorKind::InvalidData, "Document does not contain valid JSON"))
}
}
/// Update a field in a nested JSON object.
///
/// # Parameters
/// - `parent_key`: The parent key of the nested field.
/// - `key`: The key of the field to update within the parent key.
/// - `value`: The new value for the nested field.
///
/// # Returns
/// A result indicating success or failure.
pub fn update_nested_field(&mut self, parent_key: &str, key: &str, value: &Value) -> io::Result<()> {
if let Some(json_value) = &mut self.json_value {
if let Some(parent) = json_value.get_mut(parent_key) {
if let Some(obj) = parent.as_object_mut() {
obj.insert(key.to_string(), value.clone());
let updated_content = serde_json::to_string_pretty(json_value)?;
let mut file = fs::File::create(&self.path)?;
file.write_all(updated_content.as_bytes())?;
Ok(())
} else {
Err(io::Error::new(io::ErrorKind::InvalidData, "Parent key is not an object"))
}
} else {
Err(io::Error::new(io::ErrorKind::NotFound, "Parent key not found"))
}
} else {
Err(io::Error::new(io::ErrorKind::InvalidData, "Document does not contain valid JSON"))
}
}
}
// Functions for handling file operations and collections
fn get_documents_in_collection(path: &Path) -> Vec<Document> {
let entries = fs::read_dir(path).unwrap();
let mut documents: Vec<Document> = vec![];
for entry in entries {
let entry = entry.unwrap();
let entry_path = entry.path();
if entry_path.is_file() {
let name = entry_path.file_name().unwrap().to_string_lossy().into_owned();
let data = read_file_data(&entry_path).unwrap_or_default();
let json_value = parse_json_data(&data).ok();
let document = Document {
name,
path: entry_path.clone(),
json_value,
};
documents.push(document);
}
}
documents
}
fn read_file_data(path: &Path) -> io::Result<String> {
let mut file = fs::File::open(path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Ok(contents)
}
fn parse_json_data(data: &str) -> JsonResult<Value> {
serde_json::from_str(data)
}
fn get_exists_collections(path: &Path) -> Vec<Collection> {
let mut collections: Vec<Collection> = vec![];
if path.exists() && path.is_dir() {
let entries = fs::read_dir(path).unwrap();
for entry in entries {
let entry = entry.unwrap();
let entry_path = entry.path();
if entry_path.is_dir() {
let documents = get_documents_in_collection(&entry_path);
let collection_name = entry_path.file_name().unwrap().to_string_lossy().into_owned();
let collection = Collection {
name: collection_name,
documents,
};
collections.push(collection);
}
}
} else {
error!("The specified path does not exist or is not a directory: {:?}", path);
}
collections
}
// Helper method to get a mutable reference to a document
impl JSONEngine {
pub fn get_document_mut(&mut self, collection_name: &str, document_name: &str) -> Option<&mut Document> {
self.get_collection_mut(collection_name)?.documents.iter_mut().find(|doc| doc.name == document_name)
}
}