From a22defdab181c1dd1d075e1f35f15cc3cc8a3c41 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Thu, 2 Mar 2023 13:24:35 +0530 Subject: [PATCH] feat: index words on user and repo creation and rm from index when they are deleted --- db/db-core/src/tests.rs | 7 ++++++- db/db-sqlx-sqlite/src/lib.rs | 33 +++++++++++++++++++++++++++++++++ db/db-sqlx-sqlite/src/tests.rs | 4 ++-- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/db/db-core/src/tests.rs b/db/db-core/src/tests.rs index bf4b1af..5f41df3 100644 --- a/db/db-core/src/tests.rs +++ b/db/db-core/src/tests.rs @@ -66,6 +66,7 @@ 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()); // add repository db.create_repository(&add_repo_msg).await.unwrap(); @@ -74,6 +75,10 @@ pub async fn adding_forge_works<'a, T: SCDatabase>( .repository_exists(add_repo_msg.name, add_repo_msg.owner, &add_repo_msg.url) .await .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.get_all_repositories(00, 1000).await.unwrap().len() >= 1); let repo_search = db.search_repository(add_repo_msg.name).await.unwrap(); @@ -137,5 +142,5 @@ pub async fn mini_index_helper(db: &T) { } let mini_index = db.export_mini_index().await.unwrap(); - assert_eq!(mini_index, expected_mini_index); + assert!(mini_index.contains(expected_mini_index)); } diff --git a/db/db-sqlx-sqlite/src/lib.rs b/db/db-sqlx-sqlite/src/lib.rs index 1ba9043..7903711 100644 --- a/db/db-sqlx-sqlite/src/lib.rs +++ b/db/db-sqlx-sqlite/src/lib.rs @@ -273,6 +273,7 @@ impl SCDatabase for Database { .execute(&self.pool) .await .map_err(|e| DBError::DBError(Box::new(e)))?; + self.rm_word_from_mini_index(&url).await?; Ok(()) } @@ -318,6 +319,8 @@ impl SCDatabase for Database { .map_err(map_register_err)?; } + self.add_word_to_mini_index(&url).await?; + Ok(()) } @@ -478,6 +481,7 @@ impl SCDatabase for Database { .await .map_err(map_register_err)?; self.new_fts_user(u.username).await?; + self.add_word_to_mini_index(u.username).await?; Ok(()) } @@ -600,6 +604,13 @@ impl SCDatabase for Database { self.new_fts_repositories(r.name, r.description, r.website, r.html_link) .await?; + if let Some(description) = r.description { + self.add_word_to_mini_index(description).await?; + } + self.add_word_to_mini_index(r.name).await?; + if let Some(website) = r.website { + self.add_word_to_mini_index(website).await?; + } if let Some(topics) = &r.tags { for topic in topics.iter() { @@ -612,6 +623,7 @@ impl SCDatabase for Database { .map_err(map_register_err)?; self.new_fts_topic(topic).await?; + self.add_word_to_mini_index(topic).await?; sqlx::query!( " @@ -634,8 +646,12 @@ impl SCDatabase for Database { /// delete user async fn delete_user(&self, username: &str, url: &Url) -> DBResult<()> { + let user = self.get_user(username, &url).await?; + self.rm_word_from_mini_index(&user.username).await?; + let url = db_core::clean_url(url); // TODO fts delete user + sqlx::query!( " DELETE FROM starchart_users WHERE username = $1 AND hostname_id = (SELECT ID FROM starchart_forges WHERE hostname = $2)", @@ -652,6 +668,23 @@ impl SCDatabase for Database { async fn delete_repository(&self, owner: &str, name: &str, url: &Url) -> DBResult<()> { let url = db_core::clean_url(url); // TODO fts delete repo + + let repo = self.search_repository(&url).await?; + if !repo.is_empty() { + if let Some(r) = repo + .iter() + .find(|r| r.username == owner && r.name == name && r.url == url) + { + if let Some(description) = &r.description { + self.add_word_to_mini_index(description).await?; + } + self.add_word_to_mini_index(&r.name).await?; + if let Some(website) = &r.website { + self.add_word_to_mini_index(website).await?; + } + } + } + sqlx::query!( " DELETE FROM starchart_repositories WHERE diff --git a/db/db-sqlx-sqlite/src/tests.rs b/db/db-sqlx-sqlite/src/tests.rs index cadcef4..3a96b06 100644 --- a/db/db-sqlx-sqlite/src/tests.rs +++ b/db/db-sqlx-sqlite/src/tests.rs @@ -73,8 +73,8 @@ async fn everything_works() { name: REPO_NAME, tags: Some(TAGS.into()), owner: USERNAME, - website: None, - description: None, + website: "https://starcahrt-sqlite-test.example.org".into(), + description: "starchart sqlite test repo sescription".into(), url, import: false, };