Skip to main content

selene_graph/mutator/
text_index.rs

1//! Text-index mutation methods for the transaction mutator.
2
3use selene_core::{Change, DbString, SchemaChange};
4
5use crate::graph::TextIndexEntry;
6use crate::{GraphError, GraphResult, Mutator, TextIndex};
7
8impl<'tx, 'g> Mutator<'tx, 'g> {
9    /// Register a durable node text index in the active write transaction.
10    ///
11    /// # Errors
12    ///
13    /// Returns [`GraphError::TextIndexAlreadyExists`] if the pair already
14    /// exists, or [`GraphError::Inconsistent`] if index construction observes
15    /// corrupt graph columns.
16    pub fn create_text_index(&mut self, label: DbString, property: DbString) -> GraphResult<()> {
17        self.create_text_index_named(label, property, None)
18    }
19
20    /// Register a durable node text index with optional catalog name.
21    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    /// Drop a durable node text index from the active write transaction.
53    ///
54    /// The operation is idempotent. Dropping an absent index succeeds and emits
55    /// no WAL change.
56    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;