feat: import and search federating mini indices
This commit is contained in:
parent
a22defdab1
commit
c5a36ca685
4 changed files with 113 additions and 3 deletions
|
@ -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<String>;
|
||||
|
||||
/// 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<Vec<String>>;
|
||||
}
|
||||
|
||||
/// Trait to clone SCDatabase
|
||||
|
|
|
@ -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<T: SCDatabase>(db: &T) {
|
|||
|
||||
/// test if all instance introducer methods work
|
||||
pub async fn instance_introducer_helper<T: SCDatabase>(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<T: SCDatabase>(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
|
||||
|
|
|
@ -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
|
||||
);
|
|
@ -108,6 +108,35 @@ struct FTSRepository {
|
|||
}
|
||||
|
||||
impl Database {
|
||||
async fn get_federated_mini_index_matrches(&self, query: &str) -> DBResult<Vec<String>> {
|
||||
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<Vec<FTSRepository>> {
|
||||
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<Vec<String>> {
|
||||
self.get_federated_mini_index_matrches(query).await
|
||||
}
|
||||
}
|
||||
|
||||
fn now_unix_time_stamp() -> i64 {
|
||||
|
|
Loading…
Reference in a new issue