use selene_core::{Change, DbString, SchemaChange};
use crate::graph::TextIndexEntry;
use crate::{GraphError, GraphResult, Mutator, TextIndex};
impl<'tx, 'g> Mutator<'tx, 'g> {
pub fn create_text_index(&mut self, label: DbString, property: DbString) -> GraphResult<()> {
self.create_text_index_named(label, property, None)
}
pub fn create_text_index_named(
&mut self,
label: DbString,
property: DbString,
name: Option<DbString>,
) -> GraphResult<()> {
if self
.txn
.read()
.text_index
.contains_key(&(label.clone(), property.clone()))
{
return Err(GraphError::TextIndexAlreadyExists { label, property });
}
let index = TextIndex::build(self.txn.read(), label.clone(), property.clone())?;
let graph_id = self.txn.read().graph_id();
self.txn.guard_mut().text_index.insert(
(label.clone(), property.clone()),
TextIndexEntry::new(index, name.clone()),
);
self.txn.changes.push(Change::SchemaChanged {
graph: graph_id,
change: SchemaChange::TextIndexCreated {
label,
property,
name,
},
});
Ok(())
}
pub fn drop_text_index(&mut self, label: DbString, property: DbString) -> GraphResult<()> {
if !self
.txn
.read()
.text_index
.contains_key(&(label.clone(), property.clone()))
{
return Ok(());
}
let graph_id = self.txn.read().graph_id();
self.txn
.guard_mut()
.text_index
.remove(&(label.clone(), property.clone()));
self.txn.changes.push(Change::SchemaChanged {
graph: graph_id,
change: SchemaChange::TextIndexDropped { label, property },
});
Ok(())
}
}
#[cfg(test)]
#[path = "text_index/tests.rs"]
mod tests;