fix the initialization problem over the shared indexes with documents

This commit is contained in:
Tamo 2024-07-30 10:47:58 +02:00
parent d262b1df32
commit b53a019b07
2 changed files with 20 additions and 26 deletions

View File

@ -12,6 +12,7 @@ use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[allow(unused)] #[allow(unused)]
pub use server::{default_settings, Server}; pub use server::{default_settings, Server};
use tokio::sync::OnceCell;
use crate::common::index::Index; use crate::common::index::Index;
@ -192,25 +193,21 @@ pub static DOCUMENTS: Lazy<Value> = Lazy::new(|| {
}); });
pub async fn shared_index_with_documents() -> &'static Index<'static, Shared> { pub async fn shared_index_with_documents() -> &'static Index<'static, Shared> {
// We cannot store a `Shared` index directly because we need to do more initialization work later on static INDEX: OnceCell<Index<'static, Shared>> = OnceCell::const_new();
static INDEX: Lazy<Index<'static, Shared>> = Lazy::new(|| { INDEX.get_or_init(|| async {
let server = Server::new_shared(); let server = Server::new_shared();
server._index("SHARED_DOCUMENTS").to_shared() let index = server._index("SHARED_DOCUMENTS").to_shared();
});
let index = Lazy::get(&INDEX);
// That means the lazy has never been initialized, we need to create the index and index the documents
if index.is_none() {
let documents = DOCUMENTS.clone(); let documents = DOCUMENTS.clone();
let (response, _code) = INDEX._add_documents(documents, None).await; let (response, _code) = index._add_documents(documents, None).await;
INDEX.wait_task(response.uid()).await.succeeded(); index.wait_task(response.uid()).await.succeeded();
let (response, _code) = INDEX let (response, _code) = index
._update_settings( ._update_settings(
json!({"filterableAttributes": ["id", "title"], "sortableAttributes": ["id", "title"]}), json!({"filterableAttributes": ["id", "title"], "sortableAttributes": ["id", "title"]}),
) )
.await; .await;
INDEX.wait_task(response.uid()).await.succeeded(); index.wait_task(response.uid()).await.succeeded();
} index
&INDEX }).await
} }
pub static SCORE_DOCUMENTS: Lazy<Value> = Lazy::new(|| { pub static SCORE_DOCUMENTS: Lazy<Value> = Lazy::new(|| {
@ -298,24 +295,21 @@ pub static NESTED_DOCUMENTS: Lazy<Value> = Lazy::new(|| {
}); });
pub async fn shared_index_with_nested_documents() -> &'static Index<'static, Shared> { pub async fn shared_index_with_nested_documents() -> &'static Index<'static, Shared> {
static INDEX: Lazy<Index<'static, Shared>> = Lazy::new(|| { static INDEX: OnceCell<Index<'static, Shared>> = OnceCell::const_new();
INDEX.get_or_init(|| async {
let server = Server::new_shared(); let server = Server::new_shared();
server._index("SHARED_NESTED_DOCUMENTS").to_shared() let index = server._index("SHARED_NESTED_DOCUMENTS").to_shared();
});
let index = Lazy::get(&INDEX);
// That means the lazy has never been initialized, we need to create the index and index the documents
if index.is_none() {
let documents = NESTED_DOCUMENTS.clone(); let documents = NESTED_DOCUMENTS.clone();
let (response, _code) = INDEX._add_documents(documents, None).await; let (response, _code) = index._add_documents(documents, None).await;
INDEX.wait_task(response.uid()).await.succeeded(); index.wait_task(response.uid()).await.succeeded();
let (response, _code) = INDEX let (response, _code) = index
._update_settings( ._update_settings(
json!({"filterableAttributes": ["father", "doggos"], "sortableAttributes": ["doggos"]}), json!({"filterableAttributes": ["father", "doggos"], "sortableAttributes": ["doggos"]}),
) )
.await; .await;
INDEX.wait_task(response.uid()).await.succeeded(); index.wait_task(response.uid()).await.succeeded();
} index
&INDEX }).await
} }
pub static FRUITS_DOCUMENTS: Lazy<Value> = Lazy::new(|| { pub static FRUITS_DOCUMENTS: Lazy<Value> = Lazy::new(|| {

View File

@ -1,6 +1,6 @@
use meili_snap::*; use meili_snap::*;
use crate::common::{shared_does_not_exists_index, Server}; use crate::common::Server;
use crate::json; use crate::json;
#[actix_rt::test] #[actix_rt::test]