interstellar 0.2.0

A high-performance graph database with Gremlin-style traversals and GQL query language
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Index specification types and builder.
//!
//! This module provides the types needed to define and create property indexes.

use crate::index::error::IndexError;
use crate::value::Value;

/// Element type for indexing.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum ElementType {
    /// Index applies to vertices.
    Vertex,
    /// Index applies to edges.
    Edge,
}

/// Type of index structure.
#[derive(
    Clone, Copy, Debug, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize,
)]
pub enum IndexType {
    /// B+ tree for range queries and ordered iteration.
    /// Supports efficient `<`, `<=`, `>`, `>=`, `BETWEEN` predicates.
    #[default]
    BTree,
    /// Hash-based index with uniqueness constraint.
    /// Provides O(1) exact match lookup and enforces unique values.
    Unique,
    /// R-tree spatial index for geospatial queries.
    /// Supports efficient `within_distance`, `intersects`, `contained_by`, `bbox` predicates.
    RTree,
}

/// Specification for creating an index.
///
/// This struct defines all the parameters needed to create a property index.
/// Use [`IndexBuilder`] for a fluent API to construct specifications.
///
/// # Example
///
/// ```rust,ignore
/// use interstellar::index::{IndexBuilder, IndexType};
///
/// let spec = IndexBuilder::vertex()
///     .label("person")
///     .property("age")
///     .build()
///     .unwrap();
///
/// assert_eq!(spec.property, "age");
/// assert_eq!(spec.index_type, IndexType::BTree);
/// ```
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct IndexSpec {
    /// Unique name for this index.
    pub name: String,

    /// What element type to index (Vertex or Edge).
    pub element_type: ElementType,

    /// Label filter - only index elements with this label.
    /// None means index all elements regardless of label.
    pub label: Option<String>,

    /// Property key to index.
    pub property: String,

    /// Index type (BTree or Unique).
    pub index_type: IndexType,
}

impl IndexSpec {
    /// Generate an automatic name for this index.
    fn auto_name(
        element_type: ElementType,
        label: Option<&str>,
        property: &str,
        index_type: IndexType,
    ) -> String {
        let prefix = match index_type {
            IndexType::BTree => "idx",
            IndexType::Unique => "uniq",
            IndexType::RTree => "rtree",
        };
        let elem = match element_type {
            ElementType::Vertex => "v",
            ElementType::Edge => "e",
        };
        match label {
            Some(l) => format!("{}_{}_{}{}", prefix, l, property, elem),
            None => format!("{}_{}{}", prefix, property, elem),
        }
    }
}

/// Fluent builder for index creation.
///
/// # Example
///
/// ```rust,ignore
/// use interstellar::index::IndexBuilder;
///
/// // B+ tree index on person.age
/// let spec = IndexBuilder::vertex()
///     .label("person")
///     .property("age")
///     .build()?;
///
/// // Unique index on user.email
/// let spec = IndexBuilder::vertex()
///     .label("user")
///     .property("email")
///     .unique()
///     .build()?;
///
/// // Edge index
/// let spec = IndexBuilder::edge()
///     .label("purchased")
///     .property("amount")
///     .build()?;
/// ```
#[derive(Clone, Debug)]
pub struct IndexBuilder {
    element_type: ElementType,
    label: Option<String>,
    property: Option<String>,
    index_type: IndexType,
    name: Option<String>,
}

impl IndexBuilder {
    /// Start building a vertex index.
    pub fn vertex() -> Self {
        Self {
            element_type: ElementType::Vertex,
            label: None,
            property: None,
            index_type: IndexType::BTree,
            name: None,
        }
    }

    /// Start building an edge index.
    pub fn edge() -> Self {
        Self {
            element_type: ElementType::Edge,
            label: None,
            property: None,
            index_type: IndexType::BTree,
            name: None,
        }
    }

    /// Set the label filter (only index elements with this label).
    pub fn label(mut self, label: impl Into<String>) -> Self {
        self.label = Some(label.into());
        self
    }

    /// Set the property to index (required).
    pub fn property(mut self, property: impl Into<String>) -> Self {
        self.property = Some(property.into());
        self
    }

    /// Make this a unique index (default is B+ tree).
    pub fn unique(mut self) -> Self {
        self.index_type = IndexType::Unique;
        self
    }

    /// Make this an R-tree spatial index for geospatial data.
    pub fn rtree(mut self) -> Self {
        self.index_type = IndexType::RTree;
        self
    }

    /// Set explicit index name (default: auto-generated).
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }

    /// Build the index specification.
    ///
    /// # Errors
    ///
    /// Returns [`IndexError::MissingProperty`] if the property was not set.
    pub fn build(self) -> Result<IndexSpec, IndexError> {
        let property = self.property.ok_or(IndexError::MissingProperty)?;

        let name = self.name.unwrap_or_else(|| {
            IndexSpec::auto_name(
                self.element_type,
                self.label.as_deref(),
                &property,
                self.index_type,
            )
        });

        Ok(IndexSpec {
            name,
            element_type: self.element_type,
            label: self.label,
            property,
            index_type: self.index_type,
        })
    }
}

/// Predicates that can use indexes.
///
/// These predicates represent the comparison operations that can be
/// accelerated using property indexes.
#[derive(Clone, Debug, PartialEq)]
pub enum IndexPredicate {
    /// Exact equality: property = value
    Eq(Value),

    /// Inequality: property <> value
    Neq(Value),

    /// Less than: property < value
    Lt(Value),

    /// Less than or equal: property <= value
    Lte(Value),

    /// Greater than: property > value
    Gt(Value),

    /// Greater than or equal: property >= value
    Gte(Value),

    /// Range: start <= property < end (or inclusive)
    Between {
        /// Start of range.
        start: Value,
        /// End of range.
        end: Value,
        /// Whether start is inclusive.
        start_inclusive: bool,
        /// Whether end is inclusive.
        end_inclusive: bool,
    },

    /// Membership: property IN [values]
    Within(Vec<Value>),
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn index_builder_vertex_basic() {
        let spec = IndexBuilder::vertex()
            .label("person")
            .property("age")
            .build()
            .unwrap();

        assert_eq!(spec.element_type, ElementType::Vertex);
        assert_eq!(spec.label, Some("person".to_string()));
        assert_eq!(spec.property, "age");
        assert_eq!(spec.index_type, IndexType::BTree);
        assert!(spec.name.starts_with("idx_"));
    }

    #[test]
    fn index_builder_edge_basic() {
        let spec = IndexBuilder::edge()
            .label("knows")
            .property("since")
            .build()
            .unwrap();

        assert_eq!(spec.element_type, ElementType::Edge);
        assert_eq!(spec.label, Some("knows".to_string()));
        assert_eq!(spec.property, "since");
        assert_eq!(spec.index_type, IndexType::BTree);
    }

    #[test]
    fn index_builder_unique() {
        let spec = IndexBuilder::vertex()
            .label("user")
            .property("email")
            .unique()
            .build()
            .unwrap();

        assert_eq!(spec.index_type, IndexType::Unique);
        assert!(spec.name.starts_with("uniq_"));
    }

    #[test]
    fn index_builder_custom_name() {
        let spec = IndexBuilder::vertex()
            .label("person")
            .property("age")
            .name("my_custom_index")
            .build()
            .unwrap();

        assert_eq!(spec.name, "my_custom_index");
    }

    #[test]
    fn index_builder_no_label() {
        let spec = IndexBuilder::vertex()
            .property("created_at")
            .build()
            .unwrap();

        assert_eq!(spec.label, None);
        assert_eq!(spec.property, "created_at");
    }

    #[test]
    fn index_builder_missing_property() {
        let result = IndexBuilder::vertex().label("person").build();

        assert!(matches!(result, Err(IndexError::MissingProperty)));
    }

    #[test]
    fn index_spec_auto_name_btree_with_label() {
        let name =
            IndexSpec::auto_name(ElementType::Vertex, Some("person"), "age", IndexType::BTree);
        assert_eq!(name, "idx_person_agev");
    }

    #[test]
    fn index_spec_auto_name_unique_with_label() {
        let name = IndexSpec::auto_name(
            ElementType::Vertex,
            Some("user"),
            "email",
            IndexType::Unique,
        );
        assert_eq!(name, "uniq_user_emailv");
    }

    #[test]
    fn index_spec_auto_name_without_label() {
        let name = IndexSpec::auto_name(ElementType::Edge, None, "weight", IndexType::BTree);
        assert_eq!(name, "idx_weighte");
    }

    #[test]
    fn element_type_equality() {
        assert_eq!(ElementType::Vertex, ElementType::Vertex);
        assert_eq!(ElementType::Edge, ElementType::Edge);
        assert_ne!(ElementType::Vertex, ElementType::Edge);
    }

    #[test]
    fn index_type_equality() {
        assert_eq!(IndexType::BTree, IndexType::BTree);
        assert_eq!(IndexType::Unique, IndexType::Unique);
        assert_ne!(IndexType::BTree, IndexType::Unique);
    }

    #[test]
    fn index_type_default_is_btree() {
        assert_eq!(IndexType::default(), IndexType::BTree);
    }

    #[test]
    fn index_predicate_eq() {
        let pred = IndexPredicate::Eq(Value::Int(42));
        assert!(matches!(pred, IndexPredicate::Eq(Value::Int(42))));
    }

    #[test]
    fn index_predicate_between() {
        let pred = IndexPredicate::Between {
            start: Value::Int(10),
            end: Value::Int(20),
            start_inclusive: true,
            end_inclusive: false,
        };
        match pred {
            IndexPredicate::Between {
                start,
                end,
                start_inclusive,
                end_inclusive,
            } => {
                assert_eq!(start, Value::Int(10));
                assert_eq!(end, Value::Int(20));
                assert!(start_inclusive);
                assert!(!end_inclusive);
            }
            _ => panic!("Expected Between"),
        }
    }

    #[test]
    fn index_predicate_within() {
        let pred = IndexPredicate::Within(vec![Value::Int(1), Value::Int(2), Value::Int(3)]);
        match pred {
            IndexPredicate::Within(values) => {
                assert_eq!(values.len(), 3);
            }
            _ => panic!("Expected Within"),
        }
    }

    #[test]
    fn index_spec_clone() {
        let spec = IndexBuilder::vertex()
            .label("person")
            .property("age")
            .build()
            .unwrap();

        let cloned = spec.clone();
        assert_eq!(spec, cloned);
    }

    #[test]
    fn index_builder_clone() {
        let builder = IndexBuilder::vertex().label("person").property("age");

        let cloned = builder.clone();
        let spec1 = builder.build().unwrap();
        let spec2 = cloned.build().unwrap();

        assert_eq!(spec1, spec2);
    }
}