Skip to main content

linkrs_storage/
types.rs

1//! Types of data operations at the storage level
2
3use linkrs_core::types::PropertyDef as CorePropertyDef;
4use linkrs_core::DataType;
5use linkrs_core::Value;
6use std::sync::Arc;
7
8/// Property ID - a compact identifier for properties within a schema.
9/// Replaces string-based property lookups with numeric indices.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
11#[repr(transparent)]
12pub struct PropertyId(pub u16);
13
14impl PropertyId {
15    pub const NONE: PropertyId = PropertyId(u16::MAX);
16
17    #[inline]
18    pub fn new(id: u16) -> Self {
19        Self(id)
20    }
21
22    #[inline]
23    pub fn as_u16(&self) -> u16 {
24        self.0
25    }
26
27    #[inline]
28    pub fn as_usize(&self) -> usize {
29        self.0 as usize
30    }
31
32    #[inline]
33    pub fn is_none(&self) -> bool {
34        self.0 == u16::MAX
35    }
36}
37
38impl From<u16> for PropertyId {
39    #[inline]
40    fn from(id: u16) -> Self {
41        Self(id)
42    }
43}
44
45impl From<PropertyId> for u16 {
46    #[inline]
47    fn from(id: PropertyId) -> Self {
48        id.0
49    }
50}
51
52impl std::fmt::Display for PropertyId {
53    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54        write!(f, "p{}", self.0)
55    }
56}
57
58/// Edge offset - identifies an edge within a vertex's adjacency list.
59/// Replaces the global EdgeId counter with a CSR-native offset-based approach.
60#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
61#[repr(transparent)]
62pub struct EdgeOffset(pub i32);
63
64impl EdgeOffset {
65    pub const NONE: EdgeOffset = EdgeOffset(-1);
66
67    #[inline]
68    pub fn new(offset: i32) -> Self {
69        Self(offset)
70    }
71
72    #[inline]
73    pub fn as_i32(&self) -> i32 {
74        self.0
75    }
76
77    #[inline]
78    pub fn as_usize(&self) -> usize {
79        self.0 as usize
80    }
81
82    #[inline]
83    pub fn is_none(&self) -> bool {
84        self.0 < 0
85    }
86}
87
88impl From<i32> for EdgeOffset {
89    #[inline]
90    fn from(offset: i32) -> Self {
91        Self(offset)
92    }
93}
94
95impl From<EdgeOffset> for i32 {
96    #[inline]
97    fn from(offset: EdgeOffset) -> Self {
98        offset.0
99    }
100}
101
102impl std::fmt::Display for EdgeOffset {
103    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
104        write!(f, "eo{}", self.0)
105    }
106}
107
108/// Storage-level property definition.
109/// Combines features from both vertex and edge property definitions.
110#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
111pub struct StoragePropertyDef {
112    pub name: Arc<str>,
113    pub data_type: DataType,
114    pub nullable: bool,
115    pub default_value: Option<Value>,
116}
117
118impl StoragePropertyDef {
119    pub fn new(name: Arc<str>, data_type: DataType) -> Self {
120        Self {
121            name,
122            data_type,
123            nullable: false,
124            default_value: None,
125        }
126    }
127
128    pub fn from_core(prop: &CorePropertyDef) -> Self {
129        Self {
130            name: Arc::from(prop.name.as_str()),
131            data_type: prop.data_type.clone(),
132            nullable: prop.nullable,
133            default_value: prop.default.clone(),
134        }
135    }
136}
137
138impl From<CorePropertyDef> for StoragePropertyDef {
139    fn from(prop: CorePropertyDef) -> Self {
140        Self {
141            name: prop.name.into(),
142            data_type: prop.data_type,
143            nullable: prop.nullable,
144            default_value: prop.default,
145        }
146    }
147}
148
149impl From<&CorePropertyDef> for StoragePropertyDef {
150    fn from(prop: &CorePropertyDef) -> Self {
151        Self {
152            name: prop.name.as_str().into(),
153            data_type: prop.data_type.clone(),
154            nullable: prop.nullable,
155            default_value: prop.default.clone(),
156        }
157    }
158}