graph_api_lib/index.rs
1use std::any::TypeId;
2use std::fmt::{Debug, Display};
3use std::hash::Hash;
4
5/// The type of index
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7#[non_exhaustive]
8pub enum IndexType {
9 /// A hash index that supports lookup by value.
10 Hash,
11 /// A range index that supports range search
12 Range,
13 /// A full text index
14 FullText,
15}
16
17impl Display for IndexType {
18 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19 match self {
20 IndexType::Hash => write!(f, "hash"),
21 IndexType::Range => write!(f, "range"),
22 IndexType::FullText => write!(f, "full text"),
23 }
24 }
25}
26
27/// An Index is a fast lookup mechanism for graph elements.
28///
29/// Indexes allow for efficient querying of graph elements based on different criteria.
30/// Each index has a type and can be identified by an ordinal within its type.
31pub trait Index
32where
33 Self: Sized + Copy + Eq + Hash + Debug,
34{
35 /// Returns the TypeId of the element being indexed.
36 ///
37 /// Supported types are graph dependent, but all graph implementations support
38 /// basic Rust types and strings.
39 fn ty(&self) -> TypeId;
40
41 /// Returns the ordinal number of this index.
42 ///
43 /// The ordinal uniquely identifies an index within its index type.
44 fn ordinal(&self) -> usize;
45
46 /// Returns the type of this index (Hash, Range, or FullText).
47 fn index_type(&self) -> IndexType;
48}
49
50impl Index for () {
51 fn ty(&self) -> TypeId {
52 unimplemented!("index not implemented")
53 }
54
55 fn ordinal(&self) -> usize {
56 unimplemented!("index not implemented")
57 }
58
59 fn index_type(&self) -> IndexType {
60 unimplemented!("index not implemented")
61 }
62}