2022-02-06 01:14:56 +00:00
|
|
|
use crate::dbs::Options;
|
|
|
|
use crate::dbs::Runtime;
|
|
|
|
use crate::dbs::Statement;
|
2022-02-15 01:00:30 +00:00
|
|
|
use crate::dbs::Transaction;
|
2022-02-06 01:14:56 +00:00
|
|
|
use crate::doc::Document;
|
|
|
|
use crate::err::Error;
|
2022-04-09 09:09:01 +00:00
|
|
|
use crate::sql::array::Array;
|
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-04-09 09:09:01 +00:00
|
|
|
ctx: &Runtime,
|
|
|
|
opt: &Options,
|
|
|
|
txn: &Transaction,
|
2022-02-26 23:30:19 +00:00
|
|
|
_stm: &Statement,
|
2022-02-06 01:14:56 +00:00
|
|
|
) -> Result<(), Error> {
|
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
|
|
|
|
if self.tb(opt, txn).await?.drop {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
// Get the record id
|
|
|
|
let rid = self.id.as_ref().unwrap();
|
|
|
|
// Loop through all index statements
|
|
|
|
for ix in self.ix(opt, txn).await?.iter() {
|
|
|
|
// Calculate old values
|
|
|
|
let mut o = Array::with_capacity(ix.cols.len());
|
|
|
|
for i in ix.cols.iter() {
|
|
|
|
let v = i.compute(ctx, opt, txn, Some(&self.current)).await?;
|
2022-05-04 16:14:40 +00:00
|
|
|
o.push(v);
|
2022-04-09 09:09:01 +00:00
|
|
|
}
|
|
|
|
// Calculate new values
|
|
|
|
let mut n = Array::with_capacity(ix.cols.len());
|
|
|
|
for i in ix.cols.iter() {
|
|
|
|
let v = i.compute(ctx, opt, txn, Some(&self.current)).await?;
|
2022-05-04 16:14:40 +00:00
|
|
|
n.push(v);
|
2022-04-09 09:09:01 +00:00
|
|
|
}
|
|
|
|
// Clone transaction
|
|
|
|
let run = txn.clone();
|
|
|
|
// Claim transaction
|
|
|
|
let mut run = run.lock().await;
|
|
|
|
// Update the index entries
|
|
|
|
if opt.force || o != n {
|
|
|
|
match ix.uniq {
|
|
|
|
true => {
|
|
|
|
// Delete the old index data
|
|
|
|
if self.initial.is_some() {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let key = crate::key::index::new(opt.ns(), opt.db(), &ix.what, &ix.name, o);
|
|
|
|
run.delc(key, Some(rid)).await?;
|
|
|
|
}
|
|
|
|
// Create the new index data
|
|
|
|
if self.current.is_some() {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let key = crate::key::index::new(opt.ns(), opt.db(), &ix.what, &ix.name, n);
|
|
|
|
if run.putc(key, rid, None).await.is_err() {
|
|
|
|
return Err(Error::IndexExists {
|
2022-05-06 22:09:08 +00:00
|
|
|
index: ix.name.to_string(),
|
|
|
|
thing: rid.to_string(),
|
2022-04-09 09:09:01 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false => {
|
|
|
|
// Delete the old index data
|
|
|
|
if self.initial.is_some() {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let key = crate::key::point::new(opt.ns(), opt.db(), &ix.what, &ix.name, o, &rid.id);
|
|
|
|
run.delc(key, Some(rid)).await?;
|
|
|
|
}
|
|
|
|
// Create the new index data
|
|
|
|
if self.current.is_some() {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let key = crate::key::point::new(opt.ns(), opt.db(), &ix.what, &ix.name, n, &rid.id);
|
|
|
|
if run.putc(key, rid, None).await.is_err() {
|
|
|
|
return Err(Error::IndexExists {
|
2022-05-06 22:09:08 +00:00
|
|
|
index: ix.name.to_string(),
|
|
|
|
thing: rid.to_string(),
|
2022-04-09 09:09:01 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Carry on
|
2022-02-06 01:14:56 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|