From c5a36ca68531647822e7b8e759e6f9c6dc8550da Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Thu, 2 Mar 2023 15:39:43 +0530 Subject: [PATCH] feat: import and search federating mini indices --- db/db-core/src/lib.rs | 13 ++++ db/db-core/src/tests.rs | 23 +++++- ...2094417_starchart_federated_mini_index.sql | 5 ++ db/db-sqlx-sqlite/src/lib.rs | 75 +++++++++++++++++++ 4 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 db/db-sqlx-sqlite/migrations/20230302094417_starchart_federated_mini_index.sql diff --git a/db/db-core/src/lib.rs b/db/db-core/src/lib.rs index ba5bc6d..f0c16bb 100644 --- a/db/db-core/src/lib.rs +++ b/db/db-core/src/lib.rs @@ -258,6 +258,19 @@ pub trait SCDatabase: std::marker::Send + std::marker::Sync + CloneSPDatabase { /// consolidate and export mini index async fn export_mini_index(&self) -> DBResult; + + /// Import mini-index + async fn import_mini_index( + &self, + starchart_instance_url: &Url, + mini_index: &str, + ) -> DBResult<()>; + + /// Delete imported mini-index + async fn rm_imported_mini_index(&self, starchart_instance_url: &Url) -> DBResult<()>; + + /// Search mini index + async fn search_mini_index(&self, query: &str) -> DBResult>; } /// Trait to clone SCDatabase diff --git a/db/db-core/src/tests.rs b/db/db-core/src/tests.rs index 5f41df3..dd8016d 100644 --- a/db/db-core/src/tests.rs +++ b/db/db-core/src/tests.rs @@ -66,7 +66,10 @@ pub async fn adding_forge_works<'a, T: SCDatabase>( .user_exists(add_user_msg.username, Some(&add_user_msg.url)) .await .unwrap()); - assert!(db.is_word_mini_indexed(&add_user_msg2.username).await.unwrap()); + assert!(db + .is_word_mini_indexed(&add_user_msg2.username) + .await + .unwrap()); // add repository db.create_repository(&add_repo_msg).await.unwrap(); @@ -77,8 +80,14 @@ pub async fn adding_forge_works<'a, T: SCDatabase>( .unwrap()); assert!(db.is_word_mini_indexed(&add_repo_msg.owner).await.unwrap()); assert!(db.is_word_mini_indexed(&add_repo_msg.name).await.unwrap()); - assert!(db.is_word_mini_indexed(&add_repo_msg.description.unwrap()).await.unwrap()); - assert!(db.is_word_mini_indexed(&add_repo_msg.website.unwrap()).await.unwrap()); + assert!(db + .is_word_mini_indexed(&add_repo_msg.description.unwrap()) + .await + .unwrap()); + assert!(db + .is_word_mini_indexed(&add_repo_msg.website.unwrap()) + .await + .unwrap()); assert!(db.get_all_repositories(00, 1000).await.unwrap().len() >= 1); let repo_search = db.search_repository(add_repo_msg.name).await.unwrap(); @@ -116,6 +125,7 @@ pub async fn forge_type_exists_helper(db: &T) { /// test if all instance introducer methods work pub async fn instance_introducer_helper(db: &T, instance_url: &Url) { + const MINI_INDEX: &str = "instance_introducer_helper test mini index uniquerq2342"; db.add_starchart_to_introducer(instance_url).await.unwrap(); let instances = db .get_all_introduced_starchart_instances(0, 100) @@ -124,6 +134,13 @@ pub async fn instance_introducer_helper(db: &T, instance_url: &Ur assert!(instances .iter() .any(|i| i.instance_url == instance_url.as_str())); + + db.import_mini_index(instance_url, MINI_INDEX) + .await + .unwrap(); + let matching_instances = db.search_mini_index("uniquerq2342").await.unwrap(); + assert_eq!(matching_instances.len(), 1); + assert_eq!(matching_instances.first().unwrap(), instance_url.as_str()); } /// test if all instance introducer methods work diff --git a/db/db-sqlx-sqlite/migrations/20230302094417_starchart_federated_mini_index.sql b/db/db-sqlx-sqlite/migrations/20230302094417_starchart_federated_mini_index.sql new file mode 100644 index 0000000..1cbed38 --- /dev/null +++ b/db/db-sqlx-sqlite/migrations/20230302094417_starchart_federated_mini_index.sql @@ -0,0 +1,5 @@ +CREATE VIRTUAL TABLE IF NOT EXISTS starchart_federated_mini_index USING fts4 ( + starchart_instance INTEGER REFERENCES starchart_introducer(ID) ON DELETE CASCADE, + mini_index TEXT NOT NULL, + ID INTEGER PRIMARY KEY NOT NULL +); diff --git a/db/db-sqlx-sqlite/src/lib.rs b/db/db-sqlx-sqlite/src/lib.rs index 7903711..a902e53 100644 --- a/db/db-sqlx-sqlite/src/lib.rs +++ b/db/db-sqlx-sqlite/src/lib.rs @@ -108,6 +108,35 @@ struct FTSRepository { } impl Database { + async fn get_federated_mini_index_matrches(&self, query: &str) -> DBResult> { + struct Match { + instance_url: String, + } + let mut fts_mini_index_matches = sqlx::query_as!( + Match, + "SELECT + starchart_introducer.instance_url + FROM + starchart_federated_mini_index + INNER JOIN + starchart_introducer + ON + starchart_introducer.ID = starchart_instance + WHERE + mini_index MATCH $1", + query + ) + .fetch_all(&self.pool) + .await + .map_err(|e| DBError::DBError(Box::new(e)))?; + + let mut res = Vec::with_capacity(fts_mini_index_matches.len()); + fts_mini_index_matches + .drain(0..) + .for_each(|m| res.push(m.instance_url)); + + Ok(res) + } async fn get_fts_repository(&self, query: &str) -> DBResult> { let fts_repos = sqlx::query_as_unchecked!( FTSRepository, @@ -943,6 +972,52 @@ impl SCDatabase for Database { }); Ok(mini_index) } + + /// Import mini-index + async fn import_mini_index( + &self, + starchart_instance_url: &Url, + mini_index: &str, + ) -> DBResult<()> { + // delete old index before importing fresh index + let _ = self.rm_imported_mini_index(&starchart_instance_url).await; + let url = db_core::clean_url(starchart_instance_url); + sqlx::query!( + "INSERT OR IGNORE INTO + starchart_federated_mini_index + (mini_index, starchart_instance) + VALUES ($1, (SELECT ID FROM starchart_introducer WHERE instance_url = $2));", + mini_index, + url + ) + .execute(&self.pool) + .await + .map_err(map_register_err)?; + Ok(()) + } + + /// Delete imported mini-index + async fn rm_imported_mini_index(&self, starchart_instance_url: &Url) -> DBResult<()> { + let url = db_core::clean_url(starchart_instance_url); + sqlx::query!( + " + DELETE FROM starchart_federated_mini_index + WHERE + starchart_instance = ( + SELECT ID FROM starchart_introducer + WHERE instance_url = $1 + )", + url + ) + .execute(&self.pool) + .await + .map_err(|e| DBError::DBError(Box::new(e)))?; + Ok(()) + } + /// Search mini index + async fn search_mini_index(&self, query: &str) -> DBResult> { + self.get_federated_mini_index_matrches(query).await + } } fn now_unix_time_stamp() -> i64 {