From 69a1f79677676f922cf7136a871def1674a56df3 Mon Sep 17 00:00:00 2001 From: SIMRAN MAKHIJA Date: Wed, 28 Feb 2024 02:00:44 -0500 Subject: [PATCH 1/7] add namespace functions --- src/database/database.rs | 16 ++++++++++- src/main.rs | 45 +++++++++++++++++++++++++++-- src/repository/mod.rs | 1 + src/repository/namespace.rs | 57 +++++++++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 src/repository/mod.rs create mode 100644 src/repository/namespace.rs diff --git a/src/database/database.rs b/src/database/database.rs index 6b76732..5c1205a 100644 --- a/src/database/database.rs +++ b/src/database/database.rs @@ -1,4 +1,4 @@ -use rocksdb::{ColumnFamilyDescriptor, Options, DB}; +use rocksdb::{ColumnFamilyDescriptor, IteratorMode, Options, DB}; use serde::{Deserialize, Serialize}; use std::io::{self, ErrorKind}; use std::path::Path; @@ -29,6 +29,20 @@ impl Database { Ok(Self { db }) } + + pub fn list_all_keys(&self, cf: &str) -> Result, io::Error> { + let cf_handle = self.db.cf_handle(cf).ok_or_else(|| io::Error::new(ErrorKind::NotFound, format!( "Column family {} not found", cf)))?; + let mut keys = Vec::new(); + let iter = self.db.iterator_cf(cf_handle, IteratorMode::Start); + for item in iter { + let (key, _) = item.map_err(|e| io::Error::new(ErrorKind::Other, e.to_string()))?; + let key_str = String::from_utf8(key.to_vec()).map_err(|e| io::Error::new(ErrorKind::Other, e.to_string()))?; + keys.push(key_str); + } + Ok(keys) + } + + pub fn insert(&self, cf: &str, key: &str, value: &V) -> Result<(), io::Error> { let cf_handle = self.db.cf_handle(cf).ok_or_else(|| { io::Error::new( diff --git a/src/main.rs b/src/main.rs index 03faeb4..0647c35 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,15 @@ mod database; mod dto; +mod repository; -use crate::database::database::Database; +// use crate::database::database::Database; use dto::namespace_data::NamespaceData; -use dto::operator_statistics::OperatorStatistics; -use dto::table_data::TableData; +// use dto::operator_statistics::OperatorStatistics; +// use dto::table_data::TableData; use serde_json::json; +use repository::namespace::NamespaceRepository; +/* fn main() -> Result<(), std::io::Error> { let db = Database::open("rocksdb")?; @@ -85,3 +88,39 @@ fn main() -> Result<(), std::io::Error> { Ok(()) } +*/ + +fn main() -> Result<(), std::io::Error> { + let repo = NamespaceRepository::new("rocksdb")?; + + // Create some data + let namespace_data = NamespaceData { + name: "my_namespace".to_string(), + properties: json!({"key": "value"}), + }; + + + // Insert the data + repo.create_namespace(&namespace_data.name, Some(namespace_data.properties.clone()))?; + + // List all namespaces + let namespaces = repo.list_all_namespaces()?; + println!("Namespaces: {:?}", namespaces); + + // Load the metadata properties for a namespace + let namespace_data = repo.load_namespace(&namespace_data.name)?; + println!("NamespaceData: {:?}", namespace_data); + + if let Some(namespace_data) = namespace_data { + // Check if a namespace exists + let exists = repo.namespace_exists(&namespace_data.name)?; + println!("Namespace exists: {}", exists); + + // Delete a namespace + repo.delete_namespace(&namespace_data.name)?; + } + + + Ok(()) +} + diff --git a/src/repository/mod.rs b/src/repository/mod.rs new file mode 100644 index 0000000..a47bb07 --- /dev/null +++ b/src/repository/mod.rs @@ -0,0 +1 @@ +pub mod namespace; \ No newline at end of file diff --git a/src/repository/namespace.rs b/src/repository/namespace.rs new file mode 100644 index 0000000..beb5a76 --- /dev/null +++ b/src/repository/namespace.rs @@ -0,0 +1,57 @@ + +use crate::database::database::Database; +use crate::dto::namespace_data::NamespaceData; +use serde_json::{Value, json}; +use std::io; + +pub struct NamespaceRepository { + db: Database, +} + +impl NamespaceRepository { + pub fn new(db_path: &str) -> io::Result { + let db = Database::open(db_path)?; + Ok(Self { db }) + } + + pub fn list_all_namespaces(&self) -> io::Result> { + // This function depends on the specific implementation of MyDB + // and whether it supports listing all keys in a column family. + self.db.list_all_keys("NamespaceData").map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string())) + } + + pub fn create_namespace(&self, name: &str, properties: Option) -> io::Result<()> { + let namespace_data = NamespaceData { + name: name.to_string(), + properties: properties.unwrap_or_else(|| json!({"last_modified_time": current_time()})), + }; + self.db.insert("NamespaceData", name, &namespace_data) + } + + pub fn load_namespace(&self, name: &str) -> io::Result> { + self.db.get("NamespaceData", name) + } + + pub fn namespace_exists(&self, name: &str) -> io::Result { + self.db.get::("NamespaceData", name).map(|data| data.is_some()) + } + + pub fn delete_namespace(&self, name: &str) -> io::Result<()> { + self.db.delete("NamespaceData", name) + } + + pub fn set_namespace_properties(&self, name: &str, properties: Value) -> io::Result<()> { + if let Some(mut namespace_data) = self.load_namespace(name)? { + namespace_data.properties = properties; + self.db.update("NamespaceData", name, &namespace_data) + } else { + Err(io::Error::new(io::ErrorKind::NotFound, "Namespace not found")) + } + } +} + +fn current_time() -> String { + // This function returns the current time as a string. + // It's a placeholder and should be replaced with your actual implementation. + "current_time".to_string() +} From 93d3d2117b37e272e6ac462a14bb4a3bfcc9e12d Mon Sep 17 00:00:00 2001 From: SIMRAN MAKHIJA Date: Wed, 28 Feb 2024 02:02:00 -0500 Subject: [PATCH 2/7] clear main --- src/main.rs | 126 +--------------------------------------------------- 1 file changed, 2 insertions(+), 124 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0647c35..09525bc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,126 +1,4 @@ -mod database; -mod dto; -mod repository; - -// use crate::database::database::Database; -use dto::namespace_data::NamespaceData; -// use dto::operator_statistics::OperatorStatistics; -// use dto::table_data::TableData; -use serde_json::json; -use repository::namespace::NamespaceRepository; - -/* -fn main() -> Result<(), std::io::Error> { - let db = Database::open("rocksdb")?; - - // Create some data - let namespace_data = NamespaceData { - name: "my_namespace".to_string(), - properties: json!({"key": "value"}), - }; - - let table_data = TableData { - name: "MyTable".to_string(), - num_columns: 5, - read_properties: serde_json::json!({"key": "value"}), - write_properties: serde_json::json!({"key": "value"}), - file_urls: vec!["url1".to_string(), "url2".to_string()], - columns: vec![vec!["column1".to_string(), "column2".to_string()]], - aggregates: serde_json::json!({"key": "value"}), - value_range: (1, 100), - is_strong_key: true, - is_weak_key: false, - primary_key_col_name: "id".to_string(), - }; - - let operator_statistics = OperatorStatistics { - operator_string: "my_operator".to_string(), - cardinality_prev_result: 10, - }; - - // Insert the data - db.insert("NamespaceData", &namespace_data.name, &namespace_data)?; - db.insert("TableData", &table_data.name, &table_data)?; - db.insert( - "OperatorStatistics", - &operator_statistics.operator_string, - &operator_statistics, - )?; - - // Get the data - let namespace_data: Option = db.get("NamespaceData", &namespace_data.name)?; - let table_data: Option = db.get("TableData", &table_data.name)?; - let operator_statistics: Option = - db.get("OperatorStatistics", &operator_statistics.operator_string)?; - - println!("NamespaceData: {:?}", namespace_data); - println!("TableData: {:?}", table_data); - println!("OperatorStatistics: {:?}", operator_statistics); - - // Update the data - let updated_namespace_data = NamespaceData { - name: "my_namespace".to_string(), - properties: json!({"key": "new_value"}), - }; - db.update( - "NamespaceData", - &updated_namespace_data.name, - &updated_namespace_data, - )?; - - // Get the updated data - let namespace_data: Option = - db.get("NamespaceData", &updated_namespace_data.name)?; - println!("NamespaceData: {:?}", namespace_data); - - // Delete the data - if let Some(namespace_data) = namespace_data { - db.delete("NamespaceData", &namespace_data.name)?; - } - if let Some(table_data) = table_data { - db.delete("NamespaceData", &table_data.name)?; - } - if let Some(operator_statistics) = operator_statistics { - db.delete("NamespaceData", &operator_statistics.operator_string)?; - } - // Close the database - db.close(); - - Ok(()) -} -*/ - -fn main() -> Result<(), std::io::Error> { - let repo = NamespaceRepository::new("rocksdb")?; - - // Create some data - let namespace_data = NamespaceData { - name: "my_namespace".to_string(), - properties: json!({"key": "value"}), - }; - - - // Insert the data - repo.create_namespace(&namespace_data.name, Some(namespace_data.properties.clone()))?; - - // List all namespaces - let namespaces = repo.list_all_namespaces()?; - println!("Namespaces: {:?}", namespaces); - - // Load the metadata properties for a namespace - let namespace_data = repo.load_namespace(&namespace_data.name)?; - println!("NamespaceData: {:?}", namespace_data); - - if let Some(namespace_data) = namespace_data { - // Check if a namespace exists - let exists = repo.namespace_exists(&namespace_data.name)?; - println!("Namespace exists: {}", exists); - - // Delete a namespace - repo.delete_namespace(&namespace_data.name)?; - } - - - Ok(()) +fn main() { + println!("Hello World!"); } From 621d6e655ef9db45ecdc868292156c0371428305 Mon Sep 17 00:00:00 2001 From: SIMRAN MAKHIJA Date: Wed, 28 Feb 2024 11:40:50 -0500 Subject: [PATCH 3/7] repo using db object input --- src/database/database.rs | 13 +++++++++-- src/main.rs | 44 ++++++++++++++++++++++++++++++++++--- src/repository/namespace.rs | 8 +++---- 3 files changed, 56 insertions(+), 9 deletions(-) diff --git a/src/database/database.rs b/src/database/database.rs index 5c1205a..520acd5 100644 --- a/src/database/database.rs +++ b/src/database/database.rs @@ -2,9 +2,18 @@ use rocksdb::{ColumnFamilyDescriptor, IteratorMode, Options, DB}; use serde::{Deserialize, Serialize}; use std::io::{self, ErrorKind}; use std::path::Path; +use std::sync::Arc; pub struct Database { - db: DB, + db: Arc, +} + +impl Clone for Database { + fn clone(&self) -> Self { + Self { + db: Arc::clone(&self.db), + } + } } impl Database { @@ -27,7 +36,7 @@ impl Database { let db = DB::open_cf_descriptors(&opts, path, cfs_vec) .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?; - Ok(Self { db }) + Ok(Self { db : db.into() }) } pub fn list_all_keys(&self, cf: &str) -> Result, io::Error> { diff --git a/src/main.rs b/src/main.rs index 09525bc..3cbb279 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,42 @@ -fn main() { - println!("Hello World!"); -} +mod database; +mod dto; +mod repository; + +use crate::database::database::Database; +// use dto::namespace_data::NamespaceData; +// use dto::operator_statistics::OperatorStatistics; +// use dto::table_data::TableData; +use serde_json::json; +use repository::namespace::NamespaceRepository; +use std::sync::Arc; + +fn main() -> std::io::Result<()> { + let db_path = "rocksdb"; + let db = Arc::new(Database::open(db_path)?); + let namespace_repo = NamespaceRepository::new(db.clone()); + + // Test creating a namespace + let properties = Some(json!({"property1": "value1", "property2": "value2"})); + namespace_repo.create_namespace("test_namespace", properties)?; + + // Test listing all namespaces + let namespaces = namespace_repo.list_all_namespaces()?; + println!("Namespaces: {:?}", namespaces); + // Test loading a namespace + let namespace_data = namespace_repo.load_namespace("test_namespace")?; + println!("Namespace data: {:?}", namespace_data); + + // Test checking if a namespace exists + let exists = namespace_repo.namespace_exists("test_namespace")?; + println!("Namespace exists: {}", exists); + + // Test setting namespace properties + let new_properties = json!({"property1": "new_value1", "property3": "value3"}); + namespace_repo.set_namespace_properties("test_namespace", new_properties)?; + + // Test deleting a namespace + namespace_repo.delete_namespace("test_namespace")?; + + Ok(()) +} diff --git a/src/repository/namespace.rs b/src/repository/namespace.rs index beb5a76..10acd6c 100644 --- a/src/repository/namespace.rs +++ b/src/repository/namespace.rs @@ -3,15 +3,15 @@ use crate::database::database::Database; use crate::dto::namespace_data::NamespaceData; use serde_json::{Value, json}; use std::io; +use std::sync::Arc; pub struct NamespaceRepository { - db: Database, + db: Arc, } impl NamespaceRepository { - pub fn new(db_path: &str) -> io::Result { - let db = Database::open(db_path)?; - Ok(Self { db }) + pub fn new(db: Arc) -> Self { + Self { db } } pub fn list_all_namespaces(&self) -> io::Result> { From 46e8973e1ae2165c82de6f804a2c0ab39c42eeb0 Mon Sep 17 00:00:00 2001 From: SIMRAN MAKHIJA Date: Wed, 28 Feb 2024 11:41:18 -0500 Subject: [PATCH 4/7] fmt --- src/database/database.rs | 13 +++++++++---- src/main.rs | 2 +- src/repository/mod.rs | 2 +- src/repository/namespace.rs | 16 +++++++++++----- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/database/database.rs b/src/database/database.rs index 520acd5..e0b6051 100644 --- a/src/database/database.rs +++ b/src/database/database.rs @@ -36,21 +36,26 @@ impl Database { let db = DB::open_cf_descriptors(&opts, path, cfs_vec) .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?; - Ok(Self { db : db.into() }) + Ok(Self { db: db.into() }) } pub fn list_all_keys(&self, cf: &str) -> Result, io::Error> { - let cf_handle = self.db.cf_handle(cf).ok_or_else(|| io::Error::new(ErrorKind::NotFound, format!( "Column family {} not found", cf)))?; + let cf_handle = self.db.cf_handle(cf).ok_or_else(|| { + io::Error::new( + ErrorKind::NotFound, + format!("Column family {} not found", cf), + ) + })?; let mut keys = Vec::new(); let iter = self.db.iterator_cf(cf_handle, IteratorMode::Start); for item in iter { let (key, _) = item.map_err(|e| io::Error::new(ErrorKind::Other, e.to_string()))?; - let key_str = String::from_utf8(key.to_vec()).map_err(|e| io::Error::new(ErrorKind::Other, e.to_string()))?; + let key_str = String::from_utf8(key.to_vec()) + .map_err(|e| io::Error::new(ErrorKind::Other, e.to_string()))?; keys.push(key_str); } Ok(keys) } - pub fn insert(&self, cf: &str, key: &str, value: &V) -> Result<(), io::Error> { let cf_handle = self.db.cf_handle(cf).ok_or_else(|| { diff --git a/src/main.rs b/src/main.rs index 3cbb279..ebbbbaf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,8 +6,8 @@ use crate::database::database::Database; // use dto::namespace_data::NamespaceData; // use dto::operator_statistics::OperatorStatistics; // use dto::table_data::TableData; -use serde_json::json; use repository::namespace::NamespaceRepository; +use serde_json::json; use std::sync::Arc; fn main() -> std::io::Result<()> { diff --git a/src/repository/mod.rs b/src/repository/mod.rs index a47bb07..a0cb07f 100644 --- a/src/repository/mod.rs +++ b/src/repository/mod.rs @@ -1 +1 @@ -pub mod namespace; \ No newline at end of file +pub mod namespace; diff --git a/src/repository/namespace.rs b/src/repository/namespace.rs index 10acd6c..1e533eb 100644 --- a/src/repository/namespace.rs +++ b/src/repository/namespace.rs @@ -1,7 +1,6 @@ - use crate::database::database::Database; use crate::dto::namespace_data::NamespaceData; -use serde_json::{Value, json}; +use serde_json::{json, Value}; use std::io; use std::sync::Arc; @@ -17,7 +16,9 @@ impl NamespaceRepository { pub fn list_all_namespaces(&self) -> io::Result> { // This function depends on the specific implementation of MyDB // and whether it supports listing all keys in a column family. - self.db.list_all_keys("NamespaceData").map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string())) + self.db + .list_all_keys("NamespaceData") + .map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string())) } pub fn create_namespace(&self, name: &str, properties: Option) -> io::Result<()> { @@ -33,7 +34,9 @@ impl NamespaceRepository { } pub fn namespace_exists(&self, name: &str) -> io::Result { - self.db.get::("NamespaceData", name).map(|data| data.is_some()) + self.db + .get::("NamespaceData", name) + .map(|data| data.is_some()) } pub fn delete_namespace(&self, name: &str) -> io::Result<()> { @@ -45,7 +48,10 @@ impl NamespaceRepository { namespace_data.properties = properties; self.db.update("NamespaceData", name, &namespace_data) } else { - Err(io::Error::new(io::ErrorKind::NotFound, "Namespace not found")) + Err(io::Error::new( + io::ErrorKind::NotFound, + "Namespace not found", + )) } } } From 384b46006a6ac1e1dc5a3cc8ca436f36717831ab Mon Sep 17 00:00:00 2001 From: SIMRAN MAKHIJA Date: Wed, 28 Feb 2024 11:43:17 -0500 Subject: [PATCH 5/7] cleanup --- src/database/database.rs | 3 --- src/main.rs | 3 --- 2 files changed, 6 deletions(-) diff --git a/src/database/database.rs b/src/database/database.rs index e0b6051..3e28285 100644 --- a/src/database/database.rs +++ b/src/database/database.rs @@ -124,7 +124,4 @@ impl Database { Ok(()) } - pub fn close(self) { - drop(self); - } } diff --git a/src/main.rs b/src/main.rs index ebbbbaf..f4fc936 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,9 +3,6 @@ mod dto; mod repository; use crate::database::database::Database; -// use dto::namespace_data::NamespaceData; -// use dto::operator_statistics::OperatorStatistics; -// use dto::table_data::TableData; use repository::namespace::NamespaceRepository; use serde_json::json; use std::sync::Arc; From 58dbdc2386880c3f1adc47910e92229a25870eb0 Mon Sep 17 00:00:00 2001 From: SIMRAN MAKHIJA Date: Wed, 28 Feb 2024 11:43:52 -0500 Subject: [PATCH 6/7] clear main --- src/main.rs | 42 +++--------------------------------------- 1 file changed, 3 insertions(+), 39 deletions(-) diff --git a/src/main.rs b/src/main.rs index f4fc936..92d7195 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,39 +1,3 @@ -mod database; -mod dto; -mod repository; - -use crate::database::database::Database; -use repository::namespace::NamespaceRepository; -use serde_json::json; -use std::sync::Arc; - -fn main() -> std::io::Result<()> { - let db_path = "rocksdb"; - let db = Arc::new(Database::open(db_path)?); - let namespace_repo = NamespaceRepository::new(db.clone()); - - // Test creating a namespace - let properties = Some(json!({"property1": "value1", "property2": "value2"})); - namespace_repo.create_namespace("test_namespace", properties)?; - - // Test listing all namespaces - let namespaces = namespace_repo.list_all_namespaces()?; - println!("Namespaces: {:?}", namespaces); - - // Test loading a namespace - let namespace_data = namespace_repo.load_namespace("test_namespace")?; - println!("Namespace data: {:?}", namespace_data); - - // Test checking if a namespace exists - let exists = namespace_repo.namespace_exists("test_namespace")?; - println!("Namespace exists: {}", exists); - - // Test setting namespace properties - let new_properties = json!({"property1": "new_value1", "property3": "value3"}); - namespace_repo.set_namespace_properties("test_namespace", new_properties)?; - - // Test deleting a namespace - namespace_repo.delete_namespace("test_namespace")?; - - Ok(()) -} +fn main(){ + println!("Hello world!"); +} \ No newline at end of file From a78a4aeb882fe293b8dd850584649a5c86944d24 Mon Sep 17 00:00:00 2001 From: SIMRAN MAKHIJA Date: Thu, 14 Mar 2024 18:43:36 -0400 Subject: [PATCH 7/7] comment cleanup --- src/repository/namespace.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/repository/namespace.rs b/src/repository/namespace.rs index 1e533eb..32f4959 100644 --- a/src/repository/namespace.rs +++ b/src/repository/namespace.rs @@ -14,8 +14,6 @@ impl NamespaceRepository { } pub fn list_all_namespaces(&self) -> io::Result> { - // This function depends on the specific implementation of MyDB - // and whether it supports listing all keys in a column family. self.db .list_all_keys("NamespaceData") .map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string())) @@ -57,7 +55,5 @@ impl NamespaceRepository { } fn current_time() -> String { - // This function returns the current time as a string. - // It's a placeholder and should be replaced with your actual implementation. - "current_time".to_string() + "current_time".to_string() }