graphlite 0.0.1

GraphLite - A lightweight ISO GQL Graph Database
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
// Copyright (c) 2024-2025 DeepGraph Inc.
// SPDX-License-Identifier: Apache-2.0
//
// Core schema type definitions for ISO GQL Graph Types

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Represents a complete graph type definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphTypeDefinition {
    pub name: String,
    pub version: GraphTypeVersion,
    pub previous_version: Option<GraphTypeVersion>,
    pub node_types: Vec<NodeTypeDefinition>,
    pub edge_types: Vec<EdgeTypeDefinition>,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub created_by: String,
    pub description: Option<String>,
    pub metadata: HashMap<String, serde_json::Value>,
}

/// Version information for a graph type
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct GraphTypeVersion {
    pub major: u32,
    pub minor: u32,
    pub patch: u32,
    pub pre_release: Option<String>,
    pub build_metadata: Option<String>,
}

impl GraphTypeVersion {
    pub fn new(major: u32, minor: u32, patch: u32) -> Self {
        Self {
            major,
            minor,
            patch,
            pre_release: None,
            build_metadata: None,
        }
    }

    pub fn to_string(&self) -> String {
        let mut version = format!("{}.{}.{}", self.major, self.minor, self.patch);
        if let Some(pre) = &self.pre_release {
            version.push_str(&format!("-{}", pre));
        }
        if let Some(build) = &self.build_metadata {
            version.push_str(&format!("+{}", build));
        }
        version
    }

    pub fn parse(version_str: &str) -> Result<Self, String> {
        let parts: Vec<&str> = version_str.split('.').collect();
        if parts.len() != 3 {
            return Err(format!("Invalid version format: {}", version_str));
        }

        let major = parts[0]
            .parse::<u32>()
            .map_err(|_| format!("Invalid major version: {}", parts[0]))?;
        let minor = parts[1]
            .parse::<u32>()
            .map_err(|_| format!("Invalid minor version: {}", parts[1]))?;

        // Handle patch with optional pre-release/build metadata
        let patch_parts: Vec<&str> = parts[2].split('-').collect();
        let patch = patch_parts[0]
            .parse::<u32>()
            .map_err(|_| format!("Invalid patch version: {}", patch_parts[0]))?;

        let mut version = Self::new(major, minor, patch);

        if patch_parts.len() > 1 {
            let pre_build: Vec<&str> = patch_parts[1].split('+').collect();
            version.pre_release = Some(pre_build[0].to_string());
            if pre_build.len() > 1 {
                version.build_metadata = Some(pre_build[1].to_string());
            }
        }

        Ok(version)
    }

    /// Check if this version is compatible with another version
    #[allow(dead_code)] // ROADMAP v0.4.0 - Version compatibility checking for schema evolution
    pub fn is_compatible_with(&self, other: &GraphTypeVersion) -> bool {
        // Same major version = compatible
        // Different major version = potentially incompatible
        self.major == other.major
    }
}

/// Definition of a node type within a graph type
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeTypeDefinition {
    pub label: String,
    pub properties: Vec<PropertyDefinition>,
    pub constraints: Vec<Constraint>,
    pub description: Option<String>,
    pub is_abstract: bool,
    pub extends: Option<String>, // For inheritance
}

/// Definition of an edge type within a graph type
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EdgeTypeDefinition {
    pub type_name: String,
    pub from_node_types: Vec<String>,
    pub to_node_types: Vec<String>,
    pub properties: Vec<PropertyDefinition>,
    pub constraints: Vec<Constraint>,
    pub description: Option<String>,
    pub cardinality: EdgeCardinality,
}

/// Cardinality constraints for edges
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EdgeCardinality {
    pub from_min: Option<u32>,
    pub from_max: Option<u32>,
    pub to_min: Option<u32>,
    pub to_max: Option<u32>,
}

impl Default for EdgeCardinality {
    fn default() -> Self {
        Self {
            from_min: None,
            from_max: None,
            to_min: None,
            to_max: None,
        }
    }
}

/// Definition of a property within a node or edge type
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PropertyDefinition {
    pub name: String,
    pub data_type: DataType,
    pub required: bool,
    pub unique: bool,
    pub default_value: Option<serde_json::Value>,
    pub description: Option<String>,
    pub deprecated: bool,
    pub deprecation_message: Option<String>,
    pub validation_pattern: Option<String>, // Regular expression for validation
    pub constraints: Vec<Constraint>,       // Property-level constraints
}

/// Supported data types in graph schemas
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum DataType {
    // Primitive types
    String,
    Integer,
    BigInt,
    Float,
    Double,
    Boolean,

    // Temporal types
    Date,
    Time,
    DateTime,
    Timestamp,
    Duration,

    // Special types
    UUID,
    Text,  // For full-text indexable content
    Json,  // JSON object
    Bytes, // Binary data

    // Collection types
    Array(Box<DataType>),
    Map(Box<DataType>, Box<DataType>),
    Set(Box<DataType>),
    List(Box<DataType>), // List of elements
    Vector(usize),       // Vector with dimension size

    // User-defined types
    Enum(Vec<String>),
    Reference(String), // Reference to another node type
}

impl DataType {
    #[allow(dead_code)] // ROADMAP v0.4.0 - Type compatibility for schema validation
    pub fn is_compatible_with(&self, other: &DataType) -> bool {
        match (self, other) {
            (DataType::String, DataType::Text) | (DataType::Text, DataType::String) => true,
            (DataType::Integer, DataType::BigInt) => true, // Integer can be promoted to BigInt
            (DataType::Float, DataType::Double) => true,   // Float can be promoted to Double
            (a, b) => a == b,
        }
    }

    #[allow(dead_code)] // ROADMAP v0.4.0 - SQL type mapping for storage layer integration
    pub fn to_sql_type(&self) -> String {
        match self {
            DataType::String => "VARCHAR".to_string(),
            DataType::Integer => "INTEGER".to_string(),
            DataType::BigInt => "BIGINT".to_string(),
            DataType::Float => "REAL".to_string(),
            DataType::Double => "DOUBLE PRECISION".to_string(),
            DataType::Boolean => "BOOLEAN".to_string(),
            DataType::Date => "DATE".to_string(),
            DataType::Time => "TIME".to_string(),
            DataType::DateTime => "TIMESTAMP".to_string(),
            DataType::Timestamp => "TIMESTAMP WITH TIME ZONE".to_string(),
            DataType::Duration => "INTERVAL".to_string(),
            DataType::UUID => "UUID".to_string(),
            DataType::Text => "TEXT".to_string(),
            DataType::Json => "JSONB".to_string(),
            DataType::Bytes => "BYTEA".to_string(),
            DataType::Array(_) => "JSONB".to_string(), // Arrays stored as JSON
            DataType::Map(_, _) => "JSONB".to_string(),
            DataType::Set(_) => "JSONB".to_string(),
            DataType::List(_) => "JSONB".to_string(), // Lists stored as JSON
            DataType::Vector(dim) => format!("VECTOR({})", dim), // Vector with dimension
            DataType::Enum(_) => "VARCHAR".to_string(),
            DataType::Reference(_) => "UUID".to_string(),
        }
    }
}

/// Constraints that can be applied to properties or types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Constraint {
    NotNull,
    Unique,
    PrimaryKey,
    ForeignKey {
        references: String,
        on_delete: ForeignKeyAction,
    },
    Check {
        expression: String,
    },
    MinLength(usize),
    MaxLength(usize),
    MinValue(f64),
    MaxValue(f64),
    Pattern(String),            // Regular expression
    In(Vec<serde_json::Value>), // Value must be in this list
}

/// Foreign key actions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ForeignKeyAction {
    Cascade,
    SetNull,
    SetDefault,
    Restrict,
    NoAction,
}

/// Schema enforcement modes
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
pub enum SchemaEnforcementMode {
    /// Block operations that violate schema
    Strict,
    /// Warn but allow violations
    Advisory,
    /// No schema validation
    Disabled,
}

impl Default for SchemaEnforcementMode {
    fn default() -> Self {
        SchemaEnforcementMode::Advisory
    }
}

/// Schema change for ALTER operations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SchemaChange {
    AddNodeType(NodeTypeDefinition),
    DropNodeType(String),
    AddEdgeType(EdgeTypeDefinition),
    DropEdgeType(String),
    AddProperty {
        type_name: String,
        is_node: bool,
        property: PropertyDefinition,
    },
    DropProperty {
        type_name: String,
        is_node: bool,
        property_name: String,
    },
    AlterProperty {
        type_name: String,
        is_node: bool,
        property_name: String,
        changes: PropertyChanges,
    },
    AddConstraint {
        type_name: String,
        is_node: bool,
        constraint: Constraint,
    },
    DropConstraint {
        type_name: String,
        is_node: bool,
        constraint_type: String,
    },
}

/// Changes that can be made to a property
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PropertyChanges {
    pub new_type: Option<DataType>,
    pub new_default: Option<serde_json::Value>,
    pub new_required: Option<bool>,
    pub new_unique: Option<bool>,
    pub new_description: Option<String>,
    pub mark_deprecated: Option<bool>,
    pub deprecation_message: Option<String>,
}

/// Result of schema compatibility check
#[allow(dead_code)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompatibilityResult {
    pub level: CompatibilityLevel,
    pub issues: Vec<CompatibilityIssue>,
    pub migration_hints: Vec<String>,
}

/// Level of compatibility between schema versions
#[allow(dead_code)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum CompatibilityLevel {
    FullyCompatible,
    CompatibleWithDefaults,
    CompatibleWithTransform,
    Incompatible,
}

/// Specific compatibility issue
#[allow(dead_code)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompatibilityIssue {
    pub severity: IssueSeverity,
    pub type_name: String,
    pub property_name: Option<String>,
    pub description: String,
    pub suggested_fix: Option<String>,
}

/// Severity of compatibility issues
#[allow(dead_code)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum IssueSeverity {
    Error,   // Blocks migration
    Warning, // May cause issues
    Info,    // Informational only
}

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

    #[test]
    fn test_version_parsing() {
        let version = GraphTypeVersion::parse("1.2.3").unwrap();
        assert_eq!(version.major, 1);
        assert_eq!(version.minor, 2);
        assert_eq!(version.patch, 3);
        assert_eq!(version.to_string(), "1.2.3");

        let version_with_pre = GraphTypeVersion::parse("2.0.0-beta+build123").unwrap();
        assert_eq!(version_with_pre.major, 2);
        assert_eq!(version_with_pre.pre_release, Some("beta".to_string()));
        assert_eq!(
            version_with_pre.build_metadata,
            Some("build123".to_string())
        );
    }

    #[test]
    fn test_version_compatibility() {
        let v1 = GraphTypeVersion::new(1, 2, 3);
        let v2 = GraphTypeVersion::new(1, 3, 0);
        let v3 = GraphTypeVersion::new(2, 0, 0);

        assert!(v1.is_compatible_with(&v2)); // Same major version
        assert!(!v1.is_compatible_with(&v3)); // Different major version
    }

    #[test]
    fn test_data_type_compatibility() {
        assert!(DataType::String.is_compatible_with(&DataType::Text));
        assert!(DataType::Integer.is_compatible_with(&DataType::BigInt));
        assert!(DataType::Float.is_compatible_with(&DataType::Double));
        assert!(!DataType::String.is_compatible_with(&DataType::Integer));
    }
}