Alice_Database_DBMS/
lib.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
/*                          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::path::{PathBuf, Path};
use serde_json::{json, Value, Result as JsonResult};
use log::{info, error, trace};
use simplelog::*;
use chrono::Local;
use std::env; // Import this for getting the home directory

pub mod json_engine;
use json_engine::*;

// Define constants for the root path and log path
const ROOT_DIR: &str = "Alice-Database-Data";
const ADB_DATA_DIR: &str = "ADB_Data";
const JSON_ENGINE_DIR: &str = "json_engine";
const ADB_LOGS_DIR: &str = "ADB_Logs";

fn prepare() -> std::io::Result<PathBuf> {
    // Get the home directory
    let home_dir = env::home_dir().expect("Failed to get home directory");
    let base_path = home_dir.join(ROOT_DIR);
    let adb_data_path = base_path.join(ADB_DATA_DIR);
    let adb_logs_path = base_path.join(ADB_LOGS_DIR);

    // Ensure the base and log directories exist
    fs::create_dir_all(&adb_data_path).expect("Failed to create ADB_Data directory");
    fs::create_dir_all(&adb_logs_path).expect("Failed to create ADB_Logs directory");

    // Define the data path for JSON documents
    let root_path = adb_data_path.join(JSON_ENGINE_DIR);

    // Ensure the JSON engine directory exists
    fs::create_dir_all(&root_path).expect("Failed to create json_engine directory");
    // Generate a unique log filename using timestamp
    let timestamp = Local::now().format("%Y-%m-%d_%H-%M-%S").to_string();
    let log_file_path = format!("{}/{}.adb.log", adb_logs_path.display(), timestamp);

    // Set up logging configuration
    let log_config = ConfigBuilder::new().build();

    CombinedLogger::init(
        vec![
            TermLogger::new(LevelFilter::Trace, log_config.clone(), TerminalMode::Mixed, ColorChoice::Auto),
                            WriteLogger::new(LevelFilter::Trace, log_config.clone(), fs::File::create(log_file_path).unwrap()),
        ]
    ).unwrap();
    Ok(root_path.clone())
    
}

fn print_ascii() {
    println!(r"
    @---------------------------------------------------------------@
    |        ______     __         __     ______     ______         |
    |       /\  __ \   /\ \       /\ \   /\  ___\   /\  ___\        |
    |       \ \  __ \  \ \ \____  \ \ \  \ \ \____  \ \  __\        |
    |        \ \_\ \_\  \ \_____\  \ \_\  \ \_____\  \ \_____\      |
    |         \/_/\/_/   \/_____/   \/_/   \/_____/   \/_____/      |
    |                                                               |
    |                     _____     ______                          |
    |                    /\  __-.  /\  == \                         |
    |                    \ \ \/\ \ \ \  __<                         |
    |                     \ \____-  \ \_____\                       |
    |                      \/____/   \/_____/                       |
    |                                                               |
    @---------------------------------------------------------------@
    ")
}


/// The main entry point for the application.
fn main() -> std::io::Result<()> {
    print_ascii();
    let root_path = match prepare() {
        Ok(k) => k,
        _ => panic!("Errors in prepare function."),
    };
    
    trace!("Starting Collection Manager...");

    let mut manager = JSONEngine::new(&root_path);

    // Create a new collection
    let collection_name = "collection1"; // Example collection name
    if let Some(_) = manager.add_collection(collection_name) {
        trace!("Created collection: {}", collection_name);
    }

    // Create a new document within the created collection
    let document_name = "document5.json"; // Example document name
    let document_content = json!({
        "code": 200,
        "success": true,
        "payload": {
            "features": [
                "serde",
                "json"
            ],
            "homepage": null
        }
    });

    // Convert JSON Value to String
    let document_content_str = serde_json::to_string(&document_content).expect("Failed to convert JSON to string");

    if let Some(collection) = manager.get_collection_mut(collection_name) {
        if let Err(e) = collection.add_document(document_name, &document_content_str) {
            error!("Failed to add document: {}", e);
        } else {
            trace!("Created document: {} in collection: {}", document_name, collection_name);
        }
    }

    // Example of accessing the document
    if let Some(doc) = manager.get_document(collection_name, document_name) {
        println!("Found document: {:?}", doc);
        trace!("Successfully found document: {}", doc.name);
    } else {
        println!("Document '{}' not found in collection '{}'", document_name, collection_name);
        error!("Document '{}' not found in collection '{}'", document_name, collection_name);
    }

    // Example of updating 'homepage' in the 'payload' field
    if let Some(doc) = manager.get_document_mut(collection_name, document_name) {
        // New value for 'payload.homepage'
        let new_homepage_value = json!("https://new-homepage-url.com");

        if let Err(e) = doc.update_nested_field("payload", "homepage", &new_homepage_value) {
            error!("Failed to update 'homepage' in 'payload': {}", e);
        } else {
            trace!("Updated 'homepage' in document: {}", document_name);
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::env;
    use std::fs;
    use std::path::{Path, PathBuf};
    use serde_json::json;
    const TEST_ROOT_DIR: &str = "Alice-Database-Data-Test";
    const TEST_ADB_DATA_DIR: &str = "ADB_Data";
    const TEST_JSON_ENGINE_DIR: &str = "json_engine";

    // Setup a temporary test directory
    fn setup_test_dir() -> PathBuf {
        let home_dir = env::temp_dir();
        let test_dir = home_dir.join(TEST_ROOT_DIR);
        fs::create_dir_all(&test_dir).expect("Failed to create test directory");
        test_dir
    }

    #[test]
    fn test_create_and_add_document() {
        let root_path = setup_test_dir();
        let mut manager = JSONEngine::new(&root_path);

        // Create a collection
        manager.add_collection("test_collection").unwrap();
        let collection = manager.get_collection_mut("test_collection").unwrap();

        // Add a document
        let doc_content = json!({"code": 200, "success": true}).to_string();
        let result = collection.add_document("test_document.json", &doc_content);
        assert!(result.is_ok());

        // Verify the document is added
        assert_eq!(collection.documents.len(), 1);
        assert_eq!(collection.documents[0].name, "test_document.json");
    }

    #[test]
    fn test_update_document() {
        let root_path = setup_test_dir();
        let mut manager = JSONEngine::new(&root_path);

        // Create a collection and add a document
        manager.add_collection("test_collection").unwrap();
        let collection = manager.get_collection_mut("test_collection").unwrap();
        let doc_content = json!({
            "code": 200,
            "success": true,
            "payload": {
                "homepage": null
            }
        }).to_string();
        collection.add_document("test_document.json", &doc_content).unwrap();

        // Update the homepage field
        let doc = collection.get_document_mut("test_document.json").unwrap();
        let new_homepage_value = json!("https://new-homepage-url.com");

        let update_result = doc.update_nested_field("payload", "homepage", &new_homepage_value);
        assert!(update_result.is_ok());

        // Check the updated value
        assert_eq!(doc.json_value.as_ref().unwrap()["payload"]["homepage"], new_homepage_value);
    }

    #[test]
    fn test_delete_document() {
        let root_path = setup_test_dir();
        let mut manager = JSONEngine::new(&root_path);

        // Create a collection and add a document
        manager.add_collection("test_collection").unwrap();
        let collection = manager.get_collection_mut("test_collection").unwrap();
        collection.add_document("test_document.json", "{\"key\":\"value\"}").unwrap();

        // Ensure the document exists before deletion
        assert_eq!(collection.documents.len(), 1);

        // Delete the document
        let delete_result = collection.delete_document("test_document.json");
        assert!(delete_result.is_ok());

        // Verify the document was deleted
        assert_eq!(collection.documents.len(), 0);
    }

}