Resolve blocking commit deadlock and clippy warnings
This commit is contained in:
1 parent
afc7cbdad5
commit
cbd3d5801c
5 files changed
+55
-32
No files matched your search
+14
-6
@@ -3,6 +3,9 @@ use std::sync::{Arc, Mutex};
|
||||
use tantivy::schema::*;
|
||||
use tantivy::{Index, IndexReader, IndexWriter, ReloadPolicy, doc};
|
||||
|
||||
pub type SearchResultTuple = (String, String, String, String, f32);
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct MemoryIndex {
|
||||
index: Index,
|
||||
reader: IndexReader,
|
||||
@@ -81,17 +84,22 @@ impl MemoryIndex {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn commit(&self) -> tantivy::Result<()> {
|
||||
let mut writer = self.writer.lock().unwrap();
|
||||
writer.commit()?;
|
||||
Ok(())
|
||||
pub async fn commit(&self) -> tantivy::Result<()> {
|
||||
let writer = Arc::clone(&self.writer);
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let mut writer = writer.lock().unwrap();
|
||||
writer.commit()?;
|
||||
Ok(())
|
||||
})
|
||||
.await
|
||||
.unwrap_or_else(|_| Err(tantivy::TantivyError::SystemError("Commit task panicked".to_string())))
|
||||
}
|
||||
|
||||
pub fn search(
|
||||
&self,
|
||||
query: &str,
|
||||
namespace: Option<&str>,
|
||||
) -> tantivy::Result<Vec<(String, String, String, String, f32)>> {
|
||||
) -> tantivy::Result<Vec<SearchResultTuple>> {
|
||||
let searcher = self.reader.searcher();
|
||||
let query_parser = tantivy::query::QueryParser::for_index(
|
||||
&self.index,
|
||||
@@ -226,7 +234,7 @@ mod tests {
|
||||
};
|
||||
index.index_adr(&adr).await.unwrap();
|
||||
|
||||
index.commit().unwrap();
|
||||
index.commit().await.unwrap();
|
||||
index.reader.reload().unwrap();
|
||||
|
||||
// Test search
|
||||
|
||||
Reference in new issue
Block a user