mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-26 23:04:26 +01:00
simple search unit test
This commit is contained in:
parent
4b4ebad9a9
commit
85b5260d9d
@ -16,10 +16,10 @@ use uuid::Uuid;
|
|||||||
use crate::index_controller::update_file_store::UpdateFileStore;
|
use crate::index_controller::update_file_store::UpdateFileStore;
|
||||||
use crate::EnvSizer;
|
use crate::EnvSizer;
|
||||||
|
|
||||||
use super::{Checked, Settings};
|
|
||||||
use super::error::IndexError;
|
use super::error::IndexError;
|
||||||
use super::update_handler::UpdateHandler;
|
|
||||||
use super::error::Result;
|
use super::error::Result;
|
||||||
|
use super::update_handler::UpdateHandler;
|
||||||
|
use super::{Checked, Settings};
|
||||||
|
|
||||||
pub type Document = Map<String, Value>;
|
pub type Document = Map<String, Value>;
|
||||||
|
|
||||||
@ -284,4 +284,3 @@ impl Index {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
pub use search::{default_crop_length, SearchQuery, SearchResult, DEFAULT_SEARCH_LIMIT};
|
pub use search::{default_crop_length, SearchQuery, SearchResult, DEFAULT_SEARCH_LIMIT};
|
||||||
pub use updates::{apply_settings_to_builder, Checked, Facets, Settings, Unchecked};
|
pub use updates::{apply_settings_to_builder, Checked, Facets, Settings, Unchecked};
|
||||||
|
|
||||||
pub mod error;
|
|
||||||
pub mod update_handler;
|
|
||||||
mod dump;
|
mod dump;
|
||||||
|
pub mod error;
|
||||||
mod search;
|
mod search;
|
||||||
|
pub mod update_handler;
|
||||||
mod updates;
|
mod updates;
|
||||||
|
|
||||||
|
#[allow(clippy::module_inception)]
|
||||||
mod index;
|
mod index;
|
||||||
|
|
||||||
pub use index::{Document, IndexMeta, IndexStats};
|
pub use index::{Document, IndexMeta, IndexStats};
|
||||||
@ -31,11 +33,10 @@ pub mod test {
|
|||||||
use crate::index_controller::update_file_store::UpdateFileStore;
|
use crate::index_controller::update_file_store::UpdateFileStore;
|
||||||
use crate::index_controller::updates::status::{Failed, Processed, Processing};
|
use crate::index_controller::updates::status::{Failed, Processed, Processing};
|
||||||
|
|
||||||
use super::{Checked, IndexMeta, IndexStats, SearchQuery, SearchResult, Settings};
|
|
||||||
use super::index::Index;
|
|
||||||
use super::error::Result;
|
use super::error::Result;
|
||||||
|
use super::index::Index;
|
||||||
use super::update_handler::UpdateHandler;
|
use super::update_handler::UpdateHandler;
|
||||||
|
use super::{Checked, IndexMeta, IndexStats, SearchQuery, SearchResult, Settings};
|
||||||
|
|
||||||
pub struct Stub<A, R> {
|
pub struct Stub<A, R> {
|
||||||
name: String,
|
name: String,
|
||||||
@ -54,11 +55,19 @@ pub mod test {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<A, R> Stub<A, R> {
|
||||||
|
fn invalidate(&mut self) {
|
||||||
|
self.invalidated = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<A: UnwindSafe, R> Stub<A, R> {
|
impl<A: UnwindSafe, R> Stub<A, R> {
|
||||||
fn call(&mut self, args: A) -> R {
|
fn call(&mut self, args: A) -> R {
|
||||||
match self.times {
|
match self.times {
|
||||||
Some(0) => panic!("{} called to many times", self.name),
|
Some(0) => panic!("{} called to many times", self.name),
|
||||||
Some(ref mut times) => { *times -= 1; },
|
Some(ref mut times) => {
|
||||||
|
*times -= 1;
|
||||||
|
}
|
||||||
None => (),
|
None => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,7 +82,7 @@ pub mod test {
|
|||||||
match std::panic::catch_unwind(|| (stub.0)(args)) {
|
match std::panic::catch_unwind(|| (stub.0)(args)) {
|
||||||
Ok(r) => r,
|
Ok(r) => r,
|
||||||
Err(panic) => {
|
Err(panic) => {
|
||||||
self.invalidated = true;
|
self.invalidate();
|
||||||
std::panic::resume_unwind(panic);
|
std::panic::resume_unwind(panic);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -82,7 +91,7 @@ pub mod test {
|
|||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
struct StubStore {
|
struct StubStore {
|
||||||
inner: Arc<Mutex<HashMap<String, Box<dyn Any + Sync + Send>>>>
|
inner: Arc<Mutex<HashMap<String, Box<dyn Any + Sync + Send>>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StubStore {
|
impl StubStore {
|
||||||
@ -107,7 +116,7 @@ pub mod test {
|
|||||||
name: String,
|
name: String,
|
||||||
store: &'a StubStore,
|
store: &'a StubStore,
|
||||||
times: Option<usize>,
|
times: Option<usize>,
|
||||||
_f: std::marker::PhantomData<fn(A) -> R>
|
_f: std::marker::PhantomData<fn(A) -> R>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, A: 'static, R: 'static> StubBuilder<'a, A, R> {
|
impl<'a, A: 'static, R: 'static> StubBuilder<'a, A, R> {
|
||||||
@ -159,7 +168,11 @@ pub mod test {
|
|||||||
pub fn get<'a, A, R>(&'a self, name: &str) -> &'a mut Stub<A, R> {
|
pub fn get<'a, A, R>(&'a self, name: &str) -> &'a mut Stub<A, R> {
|
||||||
match self.store.get_mut(name) {
|
match self.store.get_mut(name) {
|
||||||
Some(stub) => stub,
|
Some(stub) => stub,
|
||||||
None => panic!("unexpected call to {}", name),
|
None => {
|
||||||
|
// TODO: this can cause nested panics, because stubs are dropped and panic
|
||||||
|
// themselves in their drops.
|
||||||
|
panic!("unexpected call to {}", name)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -237,7 +250,9 @@ pub mod test {
|
|||||||
attributes_to_retrieve: Option<Vec<S>>,
|
attributes_to_retrieve: Option<Vec<S>>,
|
||||||
) -> Result<Vec<Map<String, Value>>> {
|
) -> Result<Vec<Map<String, Value>>> {
|
||||||
match self {
|
match self {
|
||||||
MockIndex::Vrai(index) => index.retrieve_documents(offset, limit, attributes_to_retrieve),
|
MockIndex::Vrai(index) => {
|
||||||
|
index.retrieve_documents(offset, limit, attributes_to_retrieve)
|
||||||
|
}
|
||||||
MockIndex::Faux(_) => todo!(),
|
MockIndex::Faux(_) => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -263,9 +278,7 @@ pub mod test {
|
|||||||
pub fn snapshot(&self, path: impl AsRef<Path>) -> Result<()> {
|
pub fn snapshot(&self, path: impl AsRef<Path>) -> Result<()> {
|
||||||
match self {
|
match self {
|
||||||
MockIndex::Vrai(index) => index.snapshot(path),
|
MockIndex::Vrai(index) => index.snapshot(path),
|
||||||
MockIndex::Faux(faux) => {
|
MockIndex::Faux(faux) => faux.get("snapshot").call(path.as_ref()),
|
||||||
faux.get("snapshot").call(path.as_ref())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -285,7 +298,7 @@ pub mod test {
|
|||||||
pub fn perform_search(&self, query: SearchQuery) -> Result<SearchResult> {
|
pub fn perform_search(&self, query: SearchQuery) -> Result<SearchResult> {
|
||||||
match self {
|
match self {
|
||||||
MockIndex::Vrai(index) => index.perform_search(query),
|
MockIndex::Vrai(index) => index.perform_search(query),
|
||||||
MockIndex::Faux(_) => todo!(),
|
MockIndex::Faux(faux) => faux.get("perform_search").call(query),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -300,12 +313,9 @@ pub mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_faux_index() {
|
fn test_faux_index() {
|
||||||
let faux = Mocker::default();
|
let faux = Mocker::default();
|
||||||
faux
|
faux.when("snapshot")
|
||||||
.when("snapshot")
|
|
||||||
.times(2)
|
.times(2)
|
||||||
.then(|_: &Path| -> Result<()> {
|
.then(|_: &Path| -> Result<()> { Ok(()) });
|
||||||
Ok(())
|
|
||||||
});
|
|
||||||
|
|
||||||
let index = MockIndex::faux(faux);
|
let index = MockIndex::faux(faux);
|
||||||
|
|
||||||
@ -330,8 +340,7 @@ pub mod test {
|
|||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn test_faux_panic() {
|
fn test_faux_panic() {
|
||||||
let faux = Mocker::default();
|
let faux = Mocker::default();
|
||||||
faux
|
faux.when("snapshot")
|
||||||
.when("snapshot")
|
|
||||||
.times(2)
|
.times(2)
|
||||||
.then(|_: &Path| -> Result<()> {
|
.then(|_: &Path| -> Result<()> {
|
||||||
panic!();
|
panic!();
|
||||||
|
@ -13,13 +13,13 @@ use serde_json::{json, Value};
|
|||||||
|
|
||||||
use crate::index::error::FacetError;
|
use crate::index::error::FacetError;
|
||||||
|
|
||||||
use super::error::{Result, IndexError};
|
use super::error::{IndexError, Result};
|
||||||
use super::index::Index;
|
use super::index::Index;
|
||||||
|
|
||||||
pub type Document = IndexMap<String, Value>;
|
pub type Document = IndexMap<String, Value>;
|
||||||
type MatchesInfo = BTreeMap<String, Vec<MatchInfo>>;
|
type MatchesInfo = BTreeMap<String, Vec<MatchInfo>>;
|
||||||
|
|
||||||
#[derive(Serialize, Debug, Clone)]
|
#[derive(Serialize, Debug, Clone, PartialEq)]
|
||||||
pub struct MatchInfo {
|
pub struct MatchInfo {
|
||||||
start: usize,
|
start: usize,
|
||||||
length: usize,
|
length: usize,
|
||||||
@ -35,7 +35,7 @@ pub const fn default_crop_length() -> usize {
|
|||||||
DEFAULT_CROP_LENGTH
|
DEFAULT_CROP_LENGTH
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug, Clone, PartialEq)]
|
||||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||||
pub struct SearchQuery {
|
pub struct SearchQuery {
|
||||||
pub q: Option<String>,
|
pub q: Option<String>,
|
||||||
@ -55,7 +55,7 @@ pub struct SearchQuery {
|
|||||||
pub facets_distribution: Option<Vec<String>>,
|
pub facets_distribution: Option<Vec<String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize)]
|
#[derive(Debug, Clone, Serialize, PartialEq)]
|
||||||
pub struct SearchHit {
|
pub struct SearchHit {
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
pub document: Document,
|
pub document: Document,
|
||||||
@ -65,7 +65,7 @@ pub struct SearchHit {
|
|||||||
pub matches_info: Option<MatchesInfo>,
|
pub matches_info: Option<MatchesInfo>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Debug)]
|
#[derive(Serialize, Debug, Clone, PartialEq)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct SearchResult {
|
pub struct SearchResult {
|
||||||
pub hits: Vec<SearchHit>,
|
pub hits: Vec<SearchHit>,
|
||||||
|
@ -10,9 +10,9 @@ use tokio::sync::{mpsc, oneshot, RwLock};
|
|||||||
|
|
||||||
use super::error::{DumpActorError, Result};
|
use super::error::{DumpActorError, Result};
|
||||||
use super::{DumpInfo, DumpMsg, DumpStatus, DumpTask};
|
use super::{DumpInfo, DumpMsg, DumpStatus, DumpTask};
|
||||||
use crate::index_controller::index_resolver::IndexResolver;
|
|
||||||
use crate::index_controller::index_resolver::index_store::IndexStore;
|
use crate::index_controller::index_resolver::index_store::IndexStore;
|
||||||
use crate::index_controller::index_resolver::uuid_store::UuidStore;
|
use crate::index_controller::index_resolver::uuid_store::UuidStore;
|
||||||
|
use crate::index_controller::index_resolver::IndexResolver;
|
||||||
use crate::index_controller::updates::UpdateSender;
|
use crate::index_controller::updates::UpdateSender;
|
||||||
|
|
||||||
pub const CONCURRENT_DUMP_MSG: usize = 10;
|
pub const CONCURRENT_DUMP_MSG: usize = 10;
|
||||||
|
@ -53,6 +53,7 @@ impl Metadata {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
|
#[cfg_attr(test, mockall::automock)]
|
||||||
pub trait DumpActorHandle {
|
pub trait DumpActorHandle {
|
||||||
/// Start the creation of a dump
|
/// Start the creation of a dump
|
||||||
/// Implementation: [handle_impl::DumpActorHandleImpl::create_dump]
|
/// Implementation: [handle_impl::DumpActorHandleImpl::create_dump]
|
||||||
@ -278,13 +279,13 @@ mod test {
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::index::error::Result as IndexResult;
|
||||||
use crate::index::test::Mocker;
|
use crate::index::test::Mocker;
|
||||||
use crate::index::Index;
|
use crate::index::Index;
|
||||||
|
use crate::index_controller::index_resolver::error::IndexResolverError;
|
||||||
use crate::index_controller::index_resolver::index_store::MockIndexStore;
|
use crate::index_controller::index_resolver::index_store::MockIndexStore;
|
||||||
use crate::index_controller::index_resolver::uuid_store::MockUuidStore;
|
use crate::index_controller::index_resolver::uuid_store::MockUuidStore;
|
||||||
use crate::index_controller::updates::create_update_handler;
|
use crate::index_controller::updates::create_update_handler;
|
||||||
use crate::index::error::Result as IndexResult;
|
|
||||||
use crate::index_controller::index_resolver::error::IndexResolverError;
|
|
||||||
|
|
||||||
fn setup() {
|
fn setup() {
|
||||||
static SETUP: Lazy<()> = Lazy::new(|| {
|
static SETUP: Lazy<()> = Lazy::new(|| {
|
||||||
@ -323,15 +324,15 @@ mod test {
|
|||||||
assert!(uuids_clone.contains(&uuid));
|
assert!(uuids_clone.contains(&uuid));
|
||||||
uuid
|
uuid
|
||||||
});
|
});
|
||||||
mocker.when::<&Path, IndexResult<()>>("dump").once().then(move |_| {
|
mocker
|
||||||
Ok(())
|
.when::<&Path, IndexResult<()>>("dump")
|
||||||
});
|
.once()
|
||||||
|
.then(move |_| Ok(()));
|
||||||
Box::pin(ok(Some(Index::faux(mocker))))
|
Box::pin(ok(Some(Index::faux(mocker))))
|
||||||
});
|
});
|
||||||
|
|
||||||
let index_resolver = Arc::new(IndexResolver::new(uuid_store, index_store));
|
let index_resolver = Arc::new(IndexResolver::new(uuid_store, index_store));
|
||||||
|
|
||||||
|
|
||||||
let update_sender =
|
let update_sender =
|
||||||
create_update_handler(index_resolver.clone(), tmp.path(), 4096 * 100).unwrap();
|
create_update_handler(index_resolver.clone(), tmp.path(), 4096 * 100).unwrap();
|
||||||
|
|
||||||
|
@ -30,7 +30,9 @@ use error::Result;
|
|||||||
|
|
||||||
use self::dump_actor::load_dump;
|
use self::dump_actor::load_dump;
|
||||||
use self::index_resolver::error::IndexResolverError;
|
use self::index_resolver::error::IndexResolverError;
|
||||||
use self::index_resolver::HardStateIndexResolver;
|
use self::index_resolver::index_store::{IndexStore, MapIndexStore};
|
||||||
|
use self::index_resolver::uuid_store::{HeedUuidStore, UuidStore};
|
||||||
|
use self::index_resolver::IndexResolver;
|
||||||
use self::updates::status::UpdateStatus;
|
use self::updates::status::UpdateStatus;
|
||||||
use self::updates::UpdateMsg;
|
use self::updates::UpdateMsg;
|
||||||
|
|
||||||
@ -41,6 +43,10 @@ mod snapshot;
|
|||||||
pub mod update_file_store;
|
pub mod update_file_store;
|
||||||
pub mod updates;
|
pub mod updates;
|
||||||
|
|
||||||
|
/// Concrete implementation of the IndexController, exposed by meilisearch-lib
|
||||||
|
pub type MeiliSearch =
|
||||||
|
IndexController<HeedUuidStore, MapIndexStore, dump_actor::DumpActorHandleImpl>;
|
||||||
|
|
||||||
pub type Payload = Box<
|
pub type Payload = Box<
|
||||||
dyn Stream<Item = std::result::Result<Bytes, PayloadError>> + Send + Sync + 'static + Unpin,
|
dyn Stream<Item = std::result::Result<Bytes, PayloadError>> + Send + Sync + 'static + Unpin,
|
||||||
>;
|
>;
|
||||||
@ -62,13 +68,6 @@ pub struct IndexSettings {
|
|||||||
pub primary_key: Option<String>,
|
pub primary_key: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct IndexController {
|
|
||||||
index_resolver: Arc<HardStateIndexResolver>,
|
|
||||||
update_sender: updates::UpdateSender,
|
|
||||||
dump_handle: dump_actor::DumpActorHandleImpl,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum DocumentAdditionFormat {
|
pub enum DocumentAdditionFormat {
|
||||||
Json,
|
Json,
|
||||||
@ -129,7 +128,7 @@ impl IndexControllerBuilder {
|
|||||||
self,
|
self,
|
||||||
db_path: impl AsRef<Path>,
|
db_path: impl AsRef<Path>,
|
||||||
indexer_options: IndexerOpts,
|
indexer_options: IndexerOpts,
|
||||||
) -> anyhow::Result<IndexController> {
|
) -> anyhow::Result<MeiliSearch> {
|
||||||
let index_size = self
|
let index_size = self
|
||||||
.max_index_size
|
.max_index_size
|
||||||
.ok_or_else(|| anyhow::anyhow!("Missing index size"))?;
|
.ok_or_else(|| anyhow::anyhow!("Missing index size"))?;
|
||||||
@ -178,6 +177,8 @@ impl IndexControllerBuilder {
|
|||||||
update_store_size,
|
update_store_size,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
let dump_handle = Arc::new(dump_handle);
|
||||||
|
|
||||||
if self.schedule_snapshot {
|
if self.schedule_snapshot {
|
||||||
let snapshot_service = SnapshotService::new(
|
let snapshot_service = SnapshotService::new(
|
||||||
index_resolver.clone(),
|
index_resolver.clone(),
|
||||||
@ -266,7 +267,21 @@ impl IndexControllerBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IndexController {
|
// Using derivative to derive clone here, to ignore U and I bounds.
|
||||||
|
#[derive(derivative::Derivative)]
|
||||||
|
#[derivative(Clone(bound = ""))]
|
||||||
|
pub struct IndexController<U, I, D> {
|
||||||
|
index_resolver: Arc<IndexResolver<U, I>>,
|
||||||
|
update_sender: updates::UpdateSender,
|
||||||
|
dump_handle: Arc<D>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<U, I, D> IndexController<U, I, D>
|
||||||
|
where
|
||||||
|
U: UuidStore + Sync + Send + 'static,
|
||||||
|
I: IndexStore + Sync + Send + 'static,
|
||||||
|
D: DumpActorHandle + Send + Sync,
|
||||||
|
{
|
||||||
pub fn builder() -> IndexControllerBuilder {
|
pub fn builder() -> IndexControllerBuilder {
|
||||||
IndexControllerBuilder::default()
|
IndexControllerBuilder::default()
|
||||||
}
|
}
|
||||||
@ -286,7 +301,7 @@ impl IndexController {
|
|||||||
if create_index {
|
if create_index {
|
||||||
let index = self.index_resolver.create_index(name, None).await?;
|
let index = self.index_resolver.create_index(name, None).await?;
|
||||||
let update_result =
|
let update_result =
|
||||||
UpdateMsg::update(&self.update_sender, index.uuid, update).await?;
|
UpdateMsg::update(&self.update_sender, index.uuid(), update).await?;
|
||||||
Ok(update_result)
|
Ok(update_result)
|
||||||
} else {
|
} else {
|
||||||
Err(IndexResolverError::UnexistingIndex(name).into())
|
Err(IndexResolverError::UnexistingIndex(name).into())
|
||||||
@ -497,3 +512,117 @@ pub async fn get_arc_ownership_blocking<T>(mut item: Arc<T>) -> T {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses the v1 version of the Asc ranking rules `asc(price)`and returns the field name.
|
||||||
|
pub fn asc_ranking_rule(text: &str) -> Option<&str> {
|
||||||
|
text.split_once("asc(")
|
||||||
|
.and_then(|(_, tail)| tail.rsplit_once(")"))
|
||||||
|
.map(|(field, _)| field)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parses the v1 version of the Desc ranking rules `desc(price)`and returns the field name.
|
||||||
|
pub fn desc_ranking_rule(text: &str) -> Option<&str> {
|
||||||
|
text.split_once("desc(")
|
||||||
|
.and_then(|(_, tail)| tail.rsplit_once(")"))
|
||||||
|
.map(|(field, _)| field)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use futures::future::ok;
|
||||||
|
use mockall::predicate::eq;
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
|
use crate::index::error::Result as IndexResult;
|
||||||
|
use crate::index::test::Mocker;
|
||||||
|
use crate::index::Index;
|
||||||
|
use crate::index_controller::dump_actor::MockDumpActorHandle;
|
||||||
|
use crate::index_controller::index_resolver::index_store::MockIndexStore;
|
||||||
|
use crate::index_controller::index_resolver::uuid_store::MockUuidStore;
|
||||||
|
|
||||||
|
use super::updates::UpdateSender;
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
impl<D: DumpActorHandle> IndexController<MockUuidStore, MockIndexStore, D> {
|
||||||
|
pub fn mock(
|
||||||
|
index_resolver: IndexResolver<MockUuidStore, MockIndexStore>,
|
||||||
|
update_sender: UpdateSender,
|
||||||
|
dump_handle: D,
|
||||||
|
) -> Self {
|
||||||
|
IndexController {
|
||||||
|
index_resolver: Arc::new(index_resolver),
|
||||||
|
update_sender,
|
||||||
|
dump_handle: Arc::new(dump_handle),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_search_simple() {
|
||||||
|
let index_uid = "test";
|
||||||
|
let index_uuid = Uuid::new_v4();
|
||||||
|
let query = SearchQuery {
|
||||||
|
q: Some(String::from("hello world")),
|
||||||
|
offset: Some(10),
|
||||||
|
limit: 0,
|
||||||
|
attributes_to_retrieve: Some(vec!["string".to_owned()].into_iter().collect()),
|
||||||
|
attributes_to_crop: None,
|
||||||
|
crop_length: 18,
|
||||||
|
attributes_to_highlight: None,
|
||||||
|
matches: true,
|
||||||
|
filter: None,
|
||||||
|
sort: None,
|
||||||
|
facets_distribution: None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let result = SearchResult {
|
||||||
|
hits: vec![],
|
||||||
|
nb_hits: 29,
|
||||||
|
exhaustive_nb_hits: true,
|
||||||
|
query: "hello world".to_string(),
|
||||||
|
limit: 24,
|
||||||
|
offset: 0,
|
||||||
|
processing_time_ms: 50,
|
||||||
|
facets_distribution: None,
|
||||||
|
exhaustive_facets_count: Some(true),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut uuid_store = MockUuidStore::new();
|
||||||
|
uuid_store
|
||||||
|
.expect_get_uuid()
|
||||||
|
.with(eq(index_uid.to_owned()))
|
||||||
|
.returning(move |s| Box::pin(ok((s, Some(index_uuid)))));
|
||||||
|
|
||||||
|
let mut index_store = MockIndexStore::new();
|
||||||
|
let result_clone = result.clone();
|
||||||
|
let query_clone = query.clone();
|
||||||
|
index_store
|
||||||
|
.expect_get()
|
||||||
|
.with(eq(index_uuid))
|
||||||
|
.returning(move |_uuid| {
|
||||||
|
let result = result_clone.clone();
|
||||||
|
let query = query_clone.clone();
|
||||||
|
let mocker = Mocker::default();
|
||||||
|
mocker
|
||||||
|
.when::<SearchQuery, IndexResult<SearchResult>>("perform_search")
|
||||||
|
.once()
|
||||||
|
.then(move |q| {
|
||||||
|
assert_eq!(&q, &query);
|
||||||
|
Ok(result.clone())
|
||||||
|
});
|
||||||
|
let index = Index::faux(mocker);
|
||||||
|
Box::pin(ok(Some(index)))
|
||||||
|
});
|
||||||
|
|
||||||
|
let index_resolver = IndexResolver::new(uuid_store, index_store);
|
||||||
|
let (update_sender, _) = mpsc::channel(1);
|
||||||
|
let dump_actor = MockDumpActorHandle::new();
|
||||||
|
let index_controller = IndexController::mock(index_resolver, update_sender, dump_actor);
|
||||||
|
|
||||||
|
let r = index_controller
|
||||||
|
.search(index_uid.to_owned(), query.clone())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(r, result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -11,9 +11,9 @@ use tokio::time::sleep;
|
|||||||
use crate::compression::from_tar_gz;
|
use crate::compression::from_tar_gz;
|
||||||
use crate::index_controller::updates::UpdateMsg;
|
use crate::index_controller::updates::UpdateMsg;
|
||||||
|
|
||||||
use super::index_resolver::IndexResolver;
|
|
||||||
use super::index_resolver::index_store::IndexStore;
|
use super::index_resolver::index_store::IndexStore;
|
||||||
use super::index_resolver::uuid_store::UuidStore;
|
use super::index_resolver::uuid_store::UuidStore;
|
||||||
|
use super::index_resolver::IndexResolver;
|
||||||
use super::updates::UpdateSender;
|
use super::updates::UpdateSender;
|
||||||
|
|
||||||
pub struct SnapshotService<U, I> {
|
pub struct SnapshotService<U, I> {
|
||||||
@ -25,7 +25,7 @@ pub struct SnapshotService<U, I> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<U, I> SnapshotService<U, I>
|
impl<U, I> SnapshotService<U, I>
|
||||||
where
|
where
|
||||||
U: UuidStore + Sync + Send + 'static,
|
U: UuidStore + Sync + Send + 'static,
|
||||||
I: IndexStore + Sync + Send + 'static,
|
I: IndexStore + Sync + Send + 'static,
|
||||||
{
|
{
|
||||||
@ -142,11 +142,11 @@ mod test {
|
|||||||
|
|
||||||
use crate::index::error::IndexError;
|
use crate::index::error::IndexError;
|
||||||
use crate::index::test::Mocker;
|
use crate::index::test::Mocker;
|
||||||
use crate::index::{Index, error::Result as IndexResult};
|
use crate::index::{error::Result as IndexResult, Index};
|
||||||
use crate::index_controller::index_resolver::IndexResolver;
|
|
||||||
use crate::index_controller::index_resolver::error::IndexResolverError;
|
use crate::index_controller::index_resolver::error::IndexResolverError;
|
||||||
use crate::index_controller::index_resolver::uuid_store::MockUuidStore;
|
|
||||||
use crate::index_controller::index_resolver::index_store::MockIndexStore;
|
use crate::index_controller::index_resolver::index_store::MockIndexStore;
|
||||||
|
use crate::index_controller::index_resolver::uuid_store::MockUuidStore;
|
||||||
|
use crate::index_controller::index_resolver::IndexResolver;
|
||||||
use crate::index_controller::updates::create_update_handler;
|
use crate::index_controller::updates::create_update_handler;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
@ -183,7 +183,10 @@ mod test {
|
|||||||
|
|
||||||
let mut indexes = uuids.clone().into_iter().map(|uuid| {
|
let mut indexes = uuids.clone().into_iter().map(|uuid| {
|
||||||
let mocker = Mocker::default();
|
let mocker = Mocker::default();
|
||||||
mocker.when("snapshot").times(1).then(|_: &Path| -> IndexResult<()> { Ok(()) });
|
mocker
|
||||||
|
.when("snapshot")
|
||||||
|
.times(1)
|
||||||
|
.then(|_: &Path| -> IndexResult<()> { Ok(()) });
|
||||||
mocker.when("uuid").then(move |_: ()| uuid);
|
mocker.when("uuid").then(move |_: ()| uuid);
|
||||||
Index::faux(mocker)
|
Index::faux(mocker)
|
||||||
});
|
});
|
||||||
@ -199,7 +202,8 @@ mod test {
|
|||||||
let index_resolver = Arc::new(IndexResolver::new(uuid_store, index_store));
|
let index_resolver = Arc::new(IndexResolver::new(uuid_store, index_store));
|
||||||
|
|
||||||
let dir = tempfile::tempdir().unwrap();
|
let dir = tempfile::tempdir().unwrap();
|
||||||
let update_sender = create_update_handler(index_resolver.clone(), dir.path(), 4096 * 100).unwrap();
|
let update_sender =
|
||||||
|
create_update_handler(index_resolver.clone(), dir.path(), 4096 * 100).unwrap();
|
||||||
|
|
||||||
let snapshot_path = tempfile::tempdir().unwrap();
|
let snapshot_path = tempfile::tempdir().unwrap();
|
||||||
let snapshot_service = SnapshotService::new(
|
let snapshot_service = SnapshotService::new(
|
||||||
@ -224,14 +228,13 @@ mod test {
|
|||||||
.returning(move |_| Box::pin(err(IndexResolverError::IndexAlreadyExists)));
|
.returning(move |_| Box::pin(err(IndexResolverError::IndexAlreadyExists)));
|
||||||
|
|
||||||
let mut index_store = MockIndexStore::new();
|
let mut index_store = MockIndexStore::new();
|
||||||
index_store
|
index_store.expect_get().never();
|
||||||
.expect_get()
|
|
||||||
.never();
|
|
||||||
|
|
||||||
let index_resolver = Arc::new(IndexResolver::new(uuid_store, index_store));
|
let index_resolver = Arc::new(IndexResolver::new(uuid_store, index_store));
|
||||||
|
|
||||||
let dir = tempfile::tempdir().unwrap();
|
let dir = tempfile::tempdir().unwrap();
|
||||||
let update_sender = create_update_handler(index_resolver.clone(), dir.path(), 4096 * 100).unwrap();
|
let update_sender =
|
||||||
|
create_update_handler(index_resolver.clone(), dir.path(), 4096 * 100).unwrap();
|
||||||
|
|
||||||
let snapshot_path = tempfile::tempdir().unwrap();
|
let snapshot_path = tempfile::tempdir().unwrap();
|
||||||
let snapshot_service = SnapshotService::new(
|
let snapshot_service = SnapshotService::new(
|
||||||
@ -261,7 +264,9 @@ mod test {
|
|||||||
let mut indexes = uuids.clone().into_iter().map(|uuid| {
|
let mut indexes = uuids.clone().into_iter().map(|uuid| {
|
||||||
let mocker = Mocker::default();
|
let mocker = Mocker::default();
|
||||||
// index returns random error
|
// index returns random error
|
||||||
mocker.when("snapshot").then(|_: &Path| -> IndexResult<()> { Err(IndexError::ExistingPrimaryKey) });
|
mocker
|
||||||
|
.when("snapshot")
|
||||||
|
.then(|_: &Path| -> IndexResult<()> { Err(IndexError::ExistingPrimaryKey) });
|
||||||
mocker.when("uuid").then(move |_: ()| uuid);
|
mocker.when("uuid").then(move |_: ()| uuid);
|
||||||
Index::faux(mocker)
|
Index::faux(mocker)
|
||||||
});
|
});
|
||||||
@ -277,7 +282,8 @@ mod test {
|
|||||||
let index_resolver = Arc::new(IndexResolver::new(uuid_store, index_store));
|
let index_resolver = Arc::new(IndexResolver::new(uuid_store, index_store));
|
||||||
|
|
||||||
let dir = tempfile::tempdir().unwrap();
|
let dir = tempfile::tempdir().unwrap();
|
||||||
let update_sender = create_update_handler(index_resolver.clone(), dir.path(), 4096 * 100).unwrap();
|
let update_sender =
|
||||||
|
create_update_handler(index_resolver.clone(), dir.path(), 4096 * 100).unwrap();
|
||||||
|
|
||||||
let snapshot_path = tempfile::tempdir().unwrap();
|
let snapshot_path = tempfile::tempdir().unwrap();
|
||||||
let snapshot_service = SnapshotService::new(
|
let snapshot_service = SnapshotService::new(
|
||||||
|
@ -3,7 +3,11 @@ use std::fmt;
|
|||||||
|
|
||||||
use meilisearch_error::{Code, ErrorCode};
|
use meilisearch_error::{Code, ErrorCode};
|
||||||
|
|
||||||
use crate::{document_formats::DocumentFormatError, index::error::IndexError, index_controller::{update_file_store::UpdateFileStoreError, DocumentAdditionFormat}};
|
use crate::{
|
||||||
|
document_formats::DocumentFormatError,
|
||||||
|
index::error::IndexError,
|
||||||
|
index_controller::{update_file_store::UpdateFileStoreError, DocumentAdditionFormat},
|
||||||
|
};
|
||||||
|
|
||||||
pub type Result<T> = std::result::Result<T, UpdateLoopError>;
|
pub type Result<T> = std::result::Result<T, UpdateLoopError>;
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ pub fn create_update_handler<U, I>(
|
|||||||
db_path: impl AsRef<Path>,
|
db_path: impl AsRef<Path>,
|
||||||
update_store_size: usize,
|
update_store_size: usize,
|
||||||
) -> anyhow::Result<UpdateSender>
|
) -> anyhow::Result<UpdateSender>
|
||||||
where
|
where
|
||||||
U: UuidStore + Sync + Send + 'static,
|
U: UuidStore + Sync + Send + 'static,
|
||||||
I: IndexStore + Sync + Send + 'static,
|
I: IndexStore + Sync + Send + 'static,
|
||||||
{
|
{
|
||||||
|
@ -532,8 +532,7 @@ impl UpdateStore {
|
|||||||
..
|
..
|
||||||
} = pending.decode()?
|
} = pending.decode()?
|
||||||
{
|
{
|
||||||
self.update_file_store
|
self.update_file_store.snapshot(content_uuid, &path)?;
|
||||||
.snapshot(content_uuid, &path)?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -576,8 +575,8 @@ mod test {
|
|||||||
|
|
||||||
use crate::index::error::IndexError;
|
use crate::index::error::IndexError;
|
||||||
use crate::index::test::Mocker;
|
use crate::index::test::Mocker;
|
||||||
use crate::index_controller::index_resolver::uuid_store::MockUuidStore;
|
|
||||||
use crate::index_controller::index_resolver::index_store::MockIndexStore;
|
use crate::index_controller::index_resolver::index_store::MockIndexStore;
|
||||||
|
use crate::index_controller::index_resolver::uuid_store::MockUuidStore;
|
||||||
use crate::index_controller::updates::status::{Failed, Processed};
|
use crate::index_controller::updates::status::{Failed, Processed};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
@ -675,7 +674,6 @@ mod test {
|
|||||||
let uuid_store = MockUuidStore::new();
|
let uuid_store = MockUuidStore::new();
|
||||||
let index_resolver = Arc::new(IndexResolver::new(uuid_store, index_store));
|
let index_resolver = Arc::new(IndexResolver::new(uuid_store, index_store));
|
||||||
|
|
||||||
|
|
||||||
let update_file_store = UpdateFileStore::new(dir.path()).unwrap();
|
let update_file_store = UpdateFileStore::new(dir.path()).unwrap();
|
||||||
let mut options = EnvOpenOptions::new();
|
let mut options = EnvOpenOptions::new();
|
||||||
options.map_size(4096 * 100);
|
options.map_size(4096 * 100);
|
||||||
@ -700,7 +698,6 @@ mod test {
|
|||||||
.put(&mut txn, &(0, index_uuid, 0), &update)
|
.put(&mut txn, &(0, index_uuid, 0), &update)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
|
||||||
txn.commit().unwrap();
|
txn.commit().unwrap();
|
||||||
|
|
||||||
// Process the pending, and check that it has been moved to the update databases, and
|
// Process the pending, and check that it has been moved to the update databases, and
|
||||||
@ -742,7 +739,6 @@ mod test {
|
|||||||
let uuid_store = MockUuidStore::new();
|
let uuid_store = MockUuidStore::new();
|
||||||
let index_resolver = Arc::new(IndexResolver::new(uuid_store, index_store));
|
let index_resolver = Arc::new(IndexResolver::new(uuid_store, index_store));
|
||||||
|
|
||||||
|
|
||||||
let update_file_store = UpdateFileStore::new(dir.path()).unwrap();
|
let update_file_store = UpdateFileStore::new(dir.path()).unwrap();
|
||||||
let mut options = EnvOpenOptions::new();
|
let mut options = EnvOpenOptions::new();
|
||||||
options.map_size(4096 * 100);
|
options.map_size(4096 * 100);
|
||||||
@ -767,7 +763,6 @@ mod test {
|
|||||||
.put(&mut txn, &(0, index_uuid, 0), &update)
|
.put(&mut txn, &(0, index_uuid, 0), &update)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
|
||||||
txn.commit().unwrap();
|
txn.commit().unwrap();
|
||||||
|
|
||||||
// Process the pending, and check that it has been moved to the update databases, and
|
// Process the pending, and check that it has been moved to the update databases, and
|
||||||
|
@ -5,7 +5,8 @@ pub mod options;
|
|||||||
pub mod index;
|
pub mod index;
|
||||||
pub mod index_controller;
|
pub mod index_controller;
|
||||||
|
|
||||||
pub use index_controller::{updates::store::Update, IndexController as MeiliSearch};
|
pub use index_controller::updates::store::Update;
|
||||||
|
pub use index_controller::MeiliSearch;
|
||||||
|
|
||||||
pub use milli;
|
pub use milli;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user