Skip to main content

linkrs_query/metadata/
types.rs

1//! Metadata Type Definitions
2//!
3//! This module defines the core metadata types used throughout the query planning
4//! and execution process.
5
6use linkrs_core::types::DataType;
7use linkrs_core::Value;
8use serde::{Deserialize, Serialize};
9use std::collections::HashMap;
10
11/// Index metadata
12#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct IndexMetadata {
14    pub index_id: u64,
15    pub index_name: String,
16    pub space_id: u64,
17    pub tag_name: String,
18    pub field_name: String,
19    pub index_type: IndexType,
20    /// Whether this index belongs to an edge type (true) or a tag (false).
21    pub is_edge: bool,
22    pub properties: HashMap<String, Value>,
23}
24
25impl IndexMetadata {
26    pub fn new(
27        index_name: String,
28        space_id: u64,
29        tag_name: String,
30        field_name: String,
31        index_type: IndexType,
32    ) -> Self {
33        Self {
34            index_id: 0,
35            index_name,
36            space_id,
37            tag_name,
38            field_name,
39            index_type,
40            is_edge: false,
41            properties: HashMap::new(),
42        }
43    }
44}
45
46/// Index type enumeration
47#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
48pub enum IndexType {
49    Vector,
50    Fulltext,
51    Property,
52    Composite,
53    Native,
54}
55
56impl std::fmt::Display for IndexType {
57    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58        match self {
59            IndexType::Vector => write!(f, "VECTOR"),
60            IndexType::Fulltext => write!(f, "FULLTEXT"),
61            IndexType::Property => write!(f, "PROPERTY"),
62            IndexType::Composite => write!(f, "COMPOSITE"),
63            IndexType::Native => write!(f, "NATIVE"),
64        }
65    }
66}
67
68/// Tag metadata
69#[derive(Debug, Clone, Serialize, Deserialize)]
70pub struct TagMetadata {
71    /// Numeric tag id as assigned by the storage schema (`TagInfo.tag_id`).
72    /// 0 when the schema manager did not provide one.
73    pub tag_id: u32,
74    pub tag_name: String,
75    pub space_id: u64,
76    pub properties: Vec<PropertyDefinition>,
77    pub indexes: Vec<String>,
78}
79
80impl TagMetadata {
81    pub fn new(tag_name: String, space_id: u64) -> Self {
82        Self {
83            tag_id: 0,
84            tag_name,
85            space_id,
86            properties: Vec::new(),
87            indexes: Vec::new(),
88        }
89    }
90
91    /// Set the numeric tag id (used to wire `IndexScanNode.tag_id`).
92    pub fn with_tag_id(mut self, tag_id: u32) -> Self {
93        self.tag_id = tag_id;
94        self
95    }
96}
97
98/// Edge type metadata
99#[derive(Debug, Clone, Serialize, Deserialize)]
100pub struct EdgeTypeMetadata {
101    pub edge_type: String,
102    pub space_id: u64,
103    pub properties: Vec<PropertyDefinition>,
104    pub indexes: Vec<String>,
105}
106
107impl EdgeTypeMetadata {
108    pub fn new(edge_type: String, space_id: u64) -> Self {
109        Self {
110            edge_type,
111            space_id,
112            properties: Vec::new(),
113            indexes: Vec::new(),
114        }
115    }
116}
117
118/// Property definition
119#[derive(Debug, Clone, Serialize, Deserialize)]
120pub struct PropertyDefinition {
121    pub name: String,
122    pub data_type: PropertyType,
123    pub nullable: bool,
124    pub default_value: Option<Value>,
125}
126
127impl PropertyDefinition {
128    pub fn new(name: String, data_type: PropertyType) -> Self {
129        Self {
130            name,
131            data_type,
132            nullable: true,
133            default_value: None,
134        }
135    }
136}
137
138/// Property type enumeration
139#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
140pub enum PropertyType {
141    Bool,
142    Int,
143    Float,
144    String,
145    Vector,
146    Date,
147    DateTime,
148    List,
149    Map,
150    Vertex,
151    Edge,
152    Path,
153    Geography,
154}
155
156impl From<DataType> for PropertyType {
157    fn from(dt: DataType) -> Self {
158        use DataType as D;
159        match dt {
160            D::Bool => Self::Bool,
161            D::SmallInt | D::Int | D::BigInt => Self::Int,
162            D::Float | D::Double | D::Decimal128 => Self::Float,
163            D::String | D::FixedString(_) | D::Json | D::JsonB | D::Uuid | D::Blob => Self::String,
164            D::Date => Self::Date,
165            D::Time | D::DateTime | D::Interval => Self::DateTime,
166            D::Vertex => Self::Vertex,
167            D::Edge => Self::Edge,
168            D::Path => Self::Path,
169            D::List(_) | D::Set(_) | D::DataSet => Self::List,
170            D::Map(_) => Self::Map,
171            D::Geography => Self::Geography,
172            D::Vector | D::VectorDense(_) | D::VectorSparse(_) => Self::Vector,
173            D::Struct(_) | D::Array(_) | D::Union(_) => Self::List,
174            D::Decimal { .. } => Self::Float,
175            D::Empty | D::Null | D::Unknown => Self::String,
176        }
177    }
178}
179
180impl std::fmt::Display for PropertyType {
181    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
182        match self {
183            PropertyType::Bool => write!(f, "BOOL"),
184            PropertyType::Int => write!(f, "INT"),
185            PropertyType::Float => write!(f, "FLOAT"),
186            PropertyType::String => write!(f, "STRING"),
187            PropertyType::Vector => write!(f, "VECTOR"),
188            PropertyType::Date => write!(f, "DATE"),
189            PropertyType::DateTime => write!(f, "DATETIME"),
190            PropertyType::List => write!(f, "LIST"),
191            PropertyType::Map => write!(f, "MAP"),
192            PropertyType::Vertex => write!(f, "VERTEX"),
193            PropertyType::Edge => write!(f, "EDGE"),
194            PropertyType::Path => write!(f, "PATH"),
195            PropertyType::Geography => write!(f, "GEOGRAPHY"),
196        }
197    }
198}