surrealdb_sql/statements/
analyze.rsuse crate::ctx::Context;
use crate::dbs::Options;
use crate::dbs::Transaction;
use crate::doc::CursorDoc;
use crate::err::Error;
use crate::iam::{Action, ResourceKind};
use crate::ident::Ident;
use crate::idx::ft::FtIndex;
use crate::idx::trees::mtree::MTreeIndex;
use crate::idx::trees::store::TreeStoreType;
use crate::idx::IndexKeyBase;
use crate::index::Index;
use crate::value::Value;
use crate::Base;
use derive::Store;
use revision::revisioned;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::fmt::{Display, Formatter};
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
#[revisioned(revision = 1)]
pub enum AnalyzeStatement {
Idx(Ident, Ident),
}
impl AnalyzeStatement {
pub(crate) async fn compute(
&self,
_ctx: &Context<'_>,
opt: &Options,
txn: &Transaction,
_doc: Option<&CursorDoc<'_>>,
) -> Result<Value, Error> {
match self {
AnalyzeStatement::Idx(tb, idx) => {
opt.is_allowed(Action::View, ResourceKind::Index, &Base::Db)?;
let ix = txn
.lock()
.await
.get_and_cache_tb_index(opt.ns(), opt.db(), tb.as_str(), idx.as_str())
.await?;
let ikb = IndexKeyBase::new(opt, &ix);
let value: Value = match &ix.index {
Index::Search(p) => {
let ft =
FtIndex::new(opt, txn, p.az.as_str(), ikb, p, TreeStoreType::Traversal)
.await?;
ft.statistics(txn).await?.into()
}
Index::MTree(p) => {
let mut tx = txn.lock().await;
let mt = MTreeIndex::new(&mut tx, ikb, p, TreeStoreType::Traversal).await?;
mt.statistics(&mut tx).await?.into()
}
_ => {
return Err(Error::FeatureNotYetImplemented {
feature: "Statistics on unique and non-unique indexes.".to_string(),
})
}
};
Ok(value)
}
}
}
}
impl Display for AnalyzeStatement {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::Idx(tb, idx) => write!(f, "ANALYZE INDEX {idx} ON {tb}"),
}
}
}