use selene_core::{Change, DbString, SchemaChange};
use crate::graph::PropertyIndexEntry;
use crate::schema_index_kind::schema_kind_from;
use crate::{GraphError, GraphResult, Mutator, TypedIndexKind};
impl<'tx, 'g> Mutator<'tx, 'g> {
pub fn create_property_index(
&mut self,
label: DbString,
property: DbString,
kind: TypedIndexKind,
) -> GraphResult<()> {
self.create_property_index_named(label, property, kind, None)
}
pub fn create_property_index_named(
&mut self,
label: DbString,
property: DbString,
kind: TypedIndexKind,
name: Option<DbString>,
) -> GraphResult<()> {
if self
.txn
.read()
.property_index
.contains_key(&(label.clone(), property.clone()))
{
return Err(GraphError::PropertyIndexAlreadyExists { label, property });
}
let index = crate::property_index::build_property_index(
self.txn.read(),
label.clone(),
property.clone(),
kind,
)?;
let graph_id = self.txn.read().graph_id();
self.txn.guard_mut().property_index.insert(
(label.clone(), property.clone()),
PropertyIndexEntry::new(index, name.clone()),
);
self.txn.changes.push(Change::SchemaChanged {
graph: graph_id,
change: SchemaChange::PropertyIndexCreatedNamed {
label,
property,
kind: schema_kind_from(kind),
name,
},
});
Ok(())
}
pub fn drop_property_index(&mut self, label: DbString, property: DbString) -> GraphResult<()> {
if !self
.txn
.read()
.property_index
.contains_key(&(label.clone(), property.clone()))
{
return Ok(());
}
let graph_id = self.txn.read().graph_id();
self.txn
.guard_mut()
.property_index
.remove(&(label.clone(), property.clone()));
self.txn.changes.push(Change::SchemaChanged {
graph: graph_id,
change: SchemaChange::PropertyIndexDropped { label, property },
});
Ok(())
}
}
#[cfg(test)]
#[path = "property_index/tests.rs"]
mod tests;