2022-05-14 12:35:08 +00:00
|
|
|
use crate::ctx::Context;
|
2022-02-06 01:14:56 +00:00
|
|
|
use crate::dbs::Statement;
|
2023-07-06 14:57:42 +00:00
|
|
|
use crate::dbs::{Options, Transaction};
|
|
|
|
use crate::doc::{CursorDoc, Document};
|
2022-02-06 01:14:56 +00:00
|
|
|
use crate::err::Error;
|
2023-07-11 18:22:31 +00:00
|
|
|
use crate::idx::btree::store::BTreeStoreType;
|
2023-05-29 11:46:41 +00:00
|
|
|
use crate::idx::ft::FtIndex;
|
|
|
|
use crate::idx::IndexKeyBase;
|
2022-04-09 09:09:01 +00:00
|
|
|
use crate::sql::array::Array;
|
2023-05-29 11:46:41 +00:00
|
|
|
use crate::sql::index::Index;
|
|
|
|
use crate::sql::scoring::Scoring;
|
|
|
|
use crate::sql::statements::DefineIndexStatement;
|
2023-07-06 14:57:42 +00:00
|
|
|
use crate::sql::{Ident, Thing};
|
2023-05-29 11:46:41 +00:00
|
|
|
use crate::{key, kvs};
|
2022-01-13 07:00:50 +00:00
|
|
|
|
2022-02-13 19:03:00 +00:00
|
|
|
impl<'a> Document<'a> {
|
2022-02-06 01:14:56 +00:00
|
|
|
pub async fn index(
|
|
|
|
&self,
|
2022-05-14 12:35:08 +00:00
|
|
|
ctx: &Context<'_>,
|
2022-04-09 09:09:01 +00:00
|
|
|
opt: &Options,
|
2023-07-06 14:57:42 +00:00
|
|
|
txn: &Transaction,
|
2022-05-13 20:46:56 +00:00
|
|
|
_stm: &Statement<'_>,
|
2022-02-06 01:14:56 +00:00
|
|
|
) -> Result<(), Error> {
|
2023-07-05 21:26:13 +00:00
|
|
|
// Check indexes
|
2022-08-25 13:49:33 +00:00
|
|
|
if !opt.indexes {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2022-04-09 09:09:01 +00:00
|
|
|
// Check if forced
|
|
|
|
if !opt.force && !self.changed() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
// Check if the table is a view
|
2023-07-06 14:57:42 +00:00
|
|
|
if self.tb(opt, txn).await?.drop {
|
2022-04-09 09:09:01 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
// Get the record id
|
|
|
|
let rid = self.id.as_ref().unwrap();
|
|
|
|
// Loop through all index statements
|
2023-07-06 14:57:42 +00:00
|
|
|
for ix in self.ix(opt, txn).await?.iter() {
|
2022-04-09 09:09:01 +00:00
|
|
|
// Calculate old values
|
2023-07-06 14:57:42 +00:00
|
|
|
let o = Self::build_opt_array(ctx, opt, txn, ix, &self.initial).await?;
|
2023-05-29 11:46:41 +00:00
|
|
|
|
2022-04-09 09:09:01 +00:00
|
|
|
// Calculate new values
|
2023-07-06 14:57:42 +00:00
|
|
|
let n = Self::build_opt_array(ctx, opt, txn, ix, &self.current).await?;
|
2023-05-29 11:46:41 +00:00
|
|
|
|
2022-04-09 09:09:01 +00:00
|
|
|
// Update the index entries
|
|
|
|
if opt.force || o != n {
|
2023-05-29 11:46:41 +00:00
|
|
|
// Claim transaction
|
|
|
|
let mut run = txn.lock().await;
|
|
|
|
|
|
|
|
// Store all the variable and parameters required by the index operation
|
2023-06-21 18:31:15 +00:00
|
|
|
let ic = IndexOperation::new(opt, ix, o, n, rid);
|
2023-05-29 11:46:41 +00:00
|
|
|
|
|
|
|
// Index operation dispatching
|
|
|
|
match &ix.index {
|
|
|
|
Index::Uniq => ic.index_unique(&mut run).await?,
|
|
|
|
Index::Idx => ic.index_non_unique(&mut run).await?,
|
|
|
|
Index::Search {
|
|
|
|
az,
|
|
|
|
sc,
|
|
|
|
hl,
|
2023-06-19 18:41:13 +00:00
|
|
|
order,
|
2023-06-21 18:31:15 +00:00
|
|
|
} => ic.index_full_text(&mut run, az, *order, sc, *hl).await?,
|
2022-04-09 09:09:01 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Carry on
|
2022-02-06 01:14:56 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2023-05-29 11:46:41 +00:00
|
|
|
|
2023-06-19 18:41:13 +00:00
|
|
|
/// Extract from the given document, the values required by the index and put then in an array.
|
|
|
|
/// Eg. IF the index is composed of the columns `name` and `instrument`
|
|
|
|
/// Given this doc: { "id": 1, "instrument":"piano", "name":"Tobie" }
|
|
|
|
/// It will return: ["Tobie", "piano"]
|
2023-05-29 11:46:41 +00:00
|
|
|
async fn build_opt_array(
|
|
|
|
ctx: &Context<'_>,
|
|
|
|
opt: &Options,
|
2023-07-06 14:57:42 +00:00
|
|
|
txn: &Transaction,
|
2023-05-29 11:46:41 +00:00
|
|
|
ix: &DefineIndexStatement,
|
2023-07-06 14:57:42 +00:00
|
|
|
doc: &CursorDoc<'_>,
|
2023-05-29 11:46:41 +00:00
|
|
|
) -> Result<Option<Array>, Error> {
|
2023-07-06 14:57:42 +00:00
|
|
|
if !doc.doc.is_some() {
|
2023-05-29 11:46:41 +00:00
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
let mut o = Array::with_capacity(ix.cols.len());
|
|
|
|
for i in ix.cols.iter() {
|
2023-07-06 14:57:42 +00:00
|
|
|
let v = i.compute(ctx, opt, txn, Some(doc)).await?;
|
2023-05-29 11:46:41 +00:00
|
|
|
o.push(v);
|
|
|
|
}
|
|
|
|
Ok(Some(o))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct IndexOperation<'a> {
|
|
|
|
opt: &'a Options,
|
|
|
|
ix: &'a DefineIndexStatement,
|
|
|
|
/// The old value (if existing)
|
|
|
|
o: Option<Array>,
|
|
|
|
/// The new value (if existing)
|
|
|
|
n: Option<Array>,
|
|
|
|
rid: &'a Thing,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> IndexOperation<'a> {
|
|
|
|
fn new(
|
|
|
|
opt: &'a Options,
|
|
|
|
ix: &'a DefineIndexStatement,
|
|
|
|
o: Option<Array>,
|
|
|
|
n: Option<Array>,
|
|
|
|
rid: &'a Thing,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
opt,
|
|
|
|
ix,
|
|
|
|
o,
|
|
|
|
n,
|
|
|
|
rid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_non_unique_index_key(&self, v: &Array) -> key::index::Index {
|
2023-07-10 08:24:47 +00:00
|
|
|
key::index::Index::new(
|
2023-05-29 11:46:41 +00:00
|
|
|
self.opt.ns(),
|
|
|
|
self.opt.db(),
|
|
|
|
&self.ix.what,
|
|
|
|
&self.ix.name,
|
2023-07-10 08:24:47 +00:00
|
|
|
v.to_owned(),
|
|
|
|
Some(self.rid.id.to_owned()),
|
2023-05-29 11:46:41 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn index_non_unique(&self, run: &mut kvs::Transaction) -> Result<(), Error> {
|
|
|
|
// Delete the old index data
|
|
|
|
if let Some(o) = &self.o {
|
|
|
|
let key = self.get_non_unique_index_key(o);
|
|
|
|
let _ = run.delc(key, Some(self.rid)).await; // Ignore this error
|
|
|
|
}
|
|
|
|
// Create the new index data
|
|
|
|
if let Some(n) = &self.n {
|
|
|
|
let key = self.get_non_unique_index_key(n);
|
|
|
|
if run.putc(key, self.rid, None).await.is_err() {
|
|
|
|
return self.err_index_exists(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_unique_index_key(&self, v: &Array) -> key::index::Index {
|
2023-07-10 08:24:47 +00:00
|
|
|
key::index::Index::new(
|
|
|
|
self.opt.ns(),
|
|
|
|
self.opt.db(),
|
|
|
|
&self.ix.what,
|
|
|
|
&self.ix.name,
|
|
|
|
v.to_owned(),
|
|
|
|
None,
|
|
|
|
)
|
2023-05-29 11:46:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn index_unique(&self, run: &mut kvs::Transaction) -> Result<(), Error> {
|
|
|
|
// Delete the old index data
|
|
|
|
if let Some(o) = &self.o {
|
|
|
|
let key = self.get_unique_index_key(o);
|
|
|
|
let _ = run.delc(key, Some(self.rid)).await; // Ignore this error
|
|
|
|
}
|
|
|
|
// Create the new index data
|
|
|
|
if let Some(n) = &self.n {
|
|
|
|
let key = self.get_unique_index_key(n);
|
|
|
|
if run.putc(key, self.rid, None).await.is_err() {
|
|
|
|
return self.err_index_exists(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn err_index_exists(&self, n: &Array) -> Result<(), Error> {
|
|
|
|
Err(Error::IndexExists {
|
|
|
|
thing: self.rid.to_string(),
|
|
|
|
index: self.ix.name.to_string(),
|
|
|
|
value: match n.len() {
|
|
|
|
1 => n.first().unwrap().to_string(),
|
|
|
|
_ => n.to_string(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-06-21 18:31:15 +00:00
|
|
|
async fn index_full_text(
|
2023-05-29 11:46:41 +00:00
|
|
|
&self,
|
|
|
|
run: &mut kvs::Transaction,
|
2023-06-19 18:41:13 +00:00
|
|
|
az: &Ident,
|
|
|
|
order: u32,
|
2023-06-21 18:31:15 +00:00
|
|
|
scoring: &Scoring,
|
|
|
|
hl: bool,
|
2023-05-29 11:46:41 +00:00
|
|
|
) -> Result<(), Error> {
|
|
|
|
let ikb = IndexKeyBase::new(self.opt, self.ix);
|
2023-06-19 18:41:13 +00:00
|
|
|
let az = run.get_az(self.opt.ns(), self.opt.db(), az.as_str()).await?;
|
2023-07-11 18:22:31 +00:00
|
|
|
let mut ft = FtIndex::new(run, az, ikb, order, scoring, hl, BTreeStoreType::Write).await?;
|
2023-05-29 11:46:41 +00:00
|
|
|
if let Some(n) = &self.n {
|
2023-07-11 18:22:31 +00:00
|
|
|
ft.index_document(run, self.rid, n).await?;
|
2023-05-29 11:46:41 +00:00
|
|
|
} else {
|
2023-07-11 18:22:31 +00:00
|
|
|
ft.remove_document(run, self.rid).await?;
|
2023-05-29 11:46:41 +00:00
|
|
|
}
|
2023-07-11 18:22:31 +00:00
|
|
|
ft.finish(run).await
|
2023-05-29 11:46:41 +00:00
|
|
|
}
|
2022-02-06 01:14:56 +00:00
|
|
|
}
|