selene_graph/mutator/
text_index.rs1use selene_core::{Change, DbString, SchemaChange};
4
5use crate::graph::TextIndexEntry;
6use crate::{GraphError, GraphResult, Mutator, TextIndex};
7
8impl<'tx, 'g> Mutator<'tx, 'g> {
9 pub fn create_text_index(&mut self, label: DbString, property: DbString) -> GraphResult<()> {
17 self.create_text_index_named(label, property, None)
18 }
19
20 pub fn create_text_index_named(
22 &mut self,
23 label: DbString,
24 property: DbString,
25 name: Option<DbString>,
26 ) -> GraphResult<()> {
27 if self
28 .txn
29 .read()
30 .text_index
31 .contains_key(&(label.clone(), property.clone()))
32 {
33 return Err(GraphError::TextIndexAlreadyExists { label, property });
34 }
35 let index = TextIndex::build(self.txn.read(), label.clone(), property.clone())?;
36 let graph_id = self.txn.read().graph_id();
37 self.txn.guard_mut().text_index.insert(
38 (label.clone(), property.clone()),
39 TextIndexEntry::new(index, name.clone()),
40 );
41 self.txn.changes.push(Change::SchemaChanged {
42 graph: graph_id,
43 change: SchemaChange::TextIndexCreated {
44 label,
45 property,
46 name,
47 },
48 });
49 Ok(())
50 }
51
52 pub fn drop_text_index(&mut self, label: DbString, property: DbString) -> GraphResult<()> {
57 if !self
58 .txn
59 .read()
60 .text_index
61 .contains_key(&(label.clone(), property.clone()))
62 {
63 return Ok(());
64 }
65 let graph_id = self.txn.read().graph_id();
66 self.txn
67 .guard_mut()
68 .text_index
69 .remove(&(label.clone(), property.clone()));
70 self.txn.changes.push(Change::SchemaChanged {
71 graph: graph_id,
72 change: SchemaChange::TextIndexDropped { label, property },
73 });
74 Ok(())
75 }
76}
77
78#[cfg(test)]
79#[path = "text_index/tests.rs"]
80mod tests;