bring back the IndexMeta and IndexStats in meilisearch-http

This commit is contained in:
Tamo 2022-09-27 19:52:06 +02:00 committed by Clément Renault
parent f4ecf75cda
commit 0d0b2a9ac1
No known key found for this signature in database
GPG key ID: 92ADA4E935E71FA4
6 changed files with 208 additions and 130 deletions

View file

@ -22,49 +22,6 @@ use super::{Checked, Settings};
pub type Document = Map<String, Value>;
// @kero, what is this structure? Shouldn't it move entirely to milli?
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct IndexMeta {
#[serde(with = "time::serde::rfc3339")]
pub created_at: OffsetDateTime,
#[serde(with = "time::serde::rfc3339")]
pub updated_at: OffsetDateTime,
pub primary_key: Option<String>,
}
impl IndexMeta {
pub fn new(index: &Index) -> Result<Self> {
let txn = index.read_txn()?;
Self::new_txn(index, &txn)
}
pub fn new_txn(index: &Index, txn: &milli::heed::RoTxn) -> Result<Self> {
let created_at = index.created_at(txn)?;
let updated_at = index.updated_at(txn)?;
let primary_key = index.primary_key(txn)?.map(String::from);
Ok(Self {
created_at,
updated_at,
primary_key,
})
}
}
// @kero Maybe this should be entirely generated somewhere else since it doesn't really concern the index?
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct IndexStats {
#[serde(skip)]
pub size: u64,
pub number_of_documents: u64,
/// Whether the current index is performing an update. It is initially `None` when the
/// index returns it, since it is the `UpdateStore` that knows what index is currently indexing. It is
/// later set to either true or false, we we retrieve the information from the `UpdateStore`
pub is_indexing: Option<bool>,
pub field_distribution: FieldDistribution,
}
#[derive(Clone, derivative::Derivative)]
#[derivative(Debug)]
pub struct Index {
@ -115,20 +72,16 @@ impl Index {
Ok(())
}
pub fn stats(&self) -> Result<IndexStats> {
pub fn number_of_documents(&self) -> Result<u64> {
let rtxn = self.read_txn()?;
Ok(IndexStats {
size: self.size()?,
number_of_documents: self.number_of_documents(&rtxn)?,
is_indexing: None,
field_distribution: self.field_distribution(&rtxn)?,
})
Ok(self.inner.number_of_documents(&rtxn)?)
}
pub fn meta(&self) -> Result<IndexMeta> {
IndexMeta::new(self)
pub fn field_distribution(&self) -> Result<FieldDistribution> {
let rtxn = self.read_txn()?;
Ok(self.inner.field_distribution(&rtxn)?)
}
pub fn settings(&self) -> Result<Settings<Checked>> {
let txn = self.read_txn()?;
self.settings_txn(&txn)
@ -261,7 +214,7 @@ impl Index {
};
documents.push(document);
}
let number_of_documents = self.number_of_documents(&rtxn)?;
let number_of_documents = self.inner.number_of_documents(&rtxn)?;
Ok((number_of_documents, documents))
}
@ -315,6 +268,21 @@ impl Index {
}))
}
pub fn created_at(&self) -> Result<OffsetDateTime> {
let rtxn = self.read_txn()?;
Ok(self.inner.created_at(&rtxn)?)
}
pub fn updated_at(&self) -> Result<OffsetDateTime> {
let rtxn = self.read_txn()?;
Ok(self.inner.updated_at(&rtxn)?)
}
pub fn primary_key(&self) -> Result<Option<String>> {
let rtxn = self.read_txn()?;
Ok(self.inner.primary_key(&rtxn)?.map(str::to_string))
}
pub fn size(&self) -> Result<u64> {
Ok(self.inner.on_disk_size()?)
}

View file

@ -12,7 +12,7 @@ pub mod updates;
#[allow(clippy::module_inception)]
mod index;
pub use self::index::{Document, IndexMeta, IndexStats};
pub use self::index::Document;
#[cfg(not(test))]
pub use self::index::Index;
@ -30,13 +30,15 @@ pub mod test {
use milli::update::{
DocumentAdditionResult, DocumentDeletionResult, IndexDocumentsMethod, IndexerConfig,
};
use milli::FieldDistribution;
use nelson::Mocker;
use time::OffsetDateTime;
use uuid::Uuid;
use super::error::Result;
use super::index::Index;
use super::Document;
use super::{Checked, IndexMeta, IndexStats, SearchQuery, SearchResult, Settings};
use super::{Checked, SearchQuery, SearchResult, Settings};
use file_store::FileStore;
#[derive(Clone)]
@ -71,19 +73,6 @@ pub mod test {
}
*/
pub fn stats(&self) -> Result<IndexStats> {
match self {
MockIndex::Real(index) => index.stats(),
MockIndex::Mock(m) => unsafe { m.get("stats").call(()) },
}
}
pub fn meta(&self) -> Result<IndexMeta> {
match self {
MockIndex::Real(index) => index.meta(),
MockIndex::Mock(_) => todo!(),
}
}
pub fn settings(&self) -> Result<Settings<Checked>> {
match self {
MockIndex::Real(index) => index.settings(),
@ -144,6 +133,20 @@ pub mod test {
}
}
pub fn number_of_documents(&self) -> Result<u64> {
match self {
MockIndex::Real(index) => index.number_of_documents(),
MockIndex::Mock(m) => unsafe { m.get("number_of_documents").call(()) },
}
}
pub fn field_distribution(&self) -> Result<FieldDistribution> {
match self {
MockIndex::Real(index) => index.field_distribution(),
MockIndex::Mock(m) => unsafe { m.get("field_distribution").call(()) },
}
}
pub fn perform_search(&self, query: SearchQuery) -> Result<SearchResult> {
match self {
MockIndex::Real(index) => index.perform_search(query),
@ -151,15 +154,6 @@ pub mod test {
}
}
/*
pub fn dump(&self, path: impl AsRef<Path>) -> Result<()> {
match self {
MockIndex::Real(index) => index.dump(path),
MockIndex::Mock(m) => unsafe { m.get("dump").call(path.as_ref()) },
}
}
*/
pub fn update_documents(
&self,
method: IndexDocumentsMethod,
@ -186,7 +180,7 @@ pub mod test {
}
}
pub fn update_primary_key(&self, primary_key: String) -> Result<IndexMeta> {
pub fn update_primary_key(&self, primary_key: String) -> Result<()> {
match self {
MockIndex::Real(index) => index.update_primary_key(primary_key),
MockIndex::Mock(m) => unsafe { m.get("update_primary_key").call(primary_key) },
@ -206,6 +200,27 @@ pub mod test {
MockIndex::Mock(m) => unsafe { m.get("clear_documents").call(()) },
}
}
pub fn created_at(&self) -> Result<OffsetDateTime> {
match self {
MockIndex::Real(index) => index.created_at(),
MockIndex::Mock(m) => unsafe { m.get("created_ad").call(()) },
}
}
pub fn updated_at(&self) -> Result<OffsetDateTime> {
match self {
MockIndex::Real(index) => index.updated_at(),
MockIndex::Mock(m) => unsafe { m.get("updated_ad").call(()) },
}
}
pub fn primary_key(&self) -> Result<Option<String>> {
match self {
MockIndex::Real(index) => index.primary_key(),
MockIndex::Mock(m) => unsafe { m.get("primary_key").call(()) },
}
}
}
#[test]

View file

@ -12,7 +12,7 @@ use serde::{Deserialize, Serialize, Serializer};
use uuid::Uuid;
use super::error::{IndexError, Result};
use super::index::{Index, IndexMeta};
use super::index::Index;
use file_store::FileStore;
fn serialize_with_wildcard<S>(
@ -251,21 +251,18 @@ impl Index {
&'a self,
txn: &mut milli::heed::RwTxn<'a, 'b>,
primary_key: String,
) -> Result<IndexMeta> {
) -> Result<()> {
let mut builder = milli::update::Settings::new(txn, self, self.indexer_config.as_ref());
builder.set_primary_key(primary_key);
builder.execute(|_| ())?;
let meta = IndexMeta::new_txn(self, txn)?;
Ok(meta)
Ok(())
}
pub fn update_primary_key(&self, primary_key: String) -> Result<IndexMeta> {
pub fn update_primary_key(&self, primary_key: String) -> Result<()> {
let mut txn = self.write_txn()?;
let res = self.update_primary_key_txn(&mut txn, primary_key)?;
self.update_primary_key_txn(&mut txn, primary_key)?;
txn.commit()?;
Ok(res)
Ok(())
}
/// Deletes `ids` from the index, and returns how many documents were deleted.
@ -304,7 +301,7 @@ impl Index {
let mut txn = self.write_txn()?;
if let Some(primary_key) = primary_key {
if self.primary_key(&txn)?.is_none() {
if self.inner.primary_key(&txn)?.is_none() {
self.update_primary_key_txn(&mut txn, primary_key)?;
}
}