sqlitegraph 2.0.7

Embedded graph database with full ACID transactions, HNSW vector search, dual backend support, and comprehensive graph algorithms library
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
//! Validation and error mapping utilities for native graph backend.

use super::types::*;
use crate::SqliteGraphError;
use crate::backend::{EdgeSpec, NodeSpec};
use crate::graph::GraphEntity;

/// Error mapping from NativeBackendError to SqliteGraphError
pub fn map_to_graph_error(err: NativeBackendError) -> SqliteGraphError {
    match err {
        NativeBackendError::Io(e) => SqliteGraphError::connection(e.to_string()),
        NativeBackendError::SerializationError { context } => {
            SqliteGraphError::connection(format!("Serialization error: {}", context))
        }
        NativeBackendError::DeserializationError { context } => {
            SqliteGraphError::connection(format!("Deserialization error: {}", context))
        }
        NativeBackendError::InvalidNodeId { id, max_id } => {
            SqliteGraphError::query(format!("Invalid node ID: {} (max: {})", id, max_id))
        }
        NativeBackendError::InvalidEdgeId { id, max_id } => {
            SqliteGraphError::query(format!("Invalid edge ID: {} (max: {})", id, max_id))
        }
        NativeBackendError::CorruptNodeRecord { node_id, reason } => {
            SqliteGraphError::connection(format!("Corrupt node record {}: {}", node_id, reason))
        }
        NativeBackendError::CorruptEdgeRecord { edge_id, reason } => {
            SqliteGraphError::connection(format!("Corrupt edge record {}: {}", edge_id, reason))
        }
        NativeBackendError::FileTooSmall { size, min_size } => {
            SqliteGraphError::connection(format!("File too small: {} < {}", size, min_size))
        }
        NativeBackendError::RecordTooLarge { size, max_size } => {
            SqliteGraphError::connection(format!("Record too large: {} > {}", size, max_size))
        }
        NativeBackendError::InconsistentAdjacency {
            node_id,
            count,
            direction,
            file_count,
        } => SqliteGraphError::connection(format!(
            "Inconsistent adjacency for node {}: {} {} != {} in file",
            node_id, direction, count, file_count
        )),
        NativeBackendError::InvalidMagic { expected, found } => {
            SqliteGraphError::connection(format!(
                "Invalid magic number: expected {:#x}, got {:#x}",
                expected, found
            ))
        }
        NativeBackendError::UnsupportedVersion {
            version,
            supported_version,
        } => SqliteGraphError::connection(format!(
            "Unsupported version: {} (supported: {})",
            version, supported_version
        )),
        NativeBackendError::InvalidHeader { field, reason } => {
            SqliteGraphError::connection(format!("Invalid header field '{}': {}", field, reason))
        }
        NativeBackendError::InvalidChecksum { expected, found } => {
            SqliteGraphError::connection(format!(
                "Invalid checksum: expected {:#x}, got {:#x}",
                expected, found
            ))
        }
        NativeBackendError::Utf8Error(e) => SqliteGraphError::connection(e.to_string()),
        NativeBackendError::JsonError(e) => SqliteGraphError::connection(e.to_string()),
        NativeBackendError::BincodeError(e) => SqliteGraphError::connection(e.to_string()),
        NativeBackendError::InvalidUtf8(e) => SqliteGraphError::connection(e.to_string()),
        NativeBackendError::BufferTooSmall { size, min_size } => {
            SqliteGraphError::connection(format!("Buffer too small: {} < {}", size, min_size))
        }
        NativeBackendError::InvalidStringOffset { offset } => {
            SqliteGraphError::connection(format!("Invalid string table offset: {}", offset))
        }
        NativeBackendError::CorruptStringTable { reason } => {
            SqliteGraphError::connection(format!("Corrupt string table: {}", reason))
        }
        NativeBackendError::InvalidMagicBytes { found } => {
            SqliteGraphError::connection(format!("Invalid magic bytes: {:?}", found))
        }
        NativeBackendError::ValidationFailed {
            metric,
            expected,
            actual,
        } => SqliteGraphError::connection(format!(
            "Validation failed for {}: expected {}, got {}",
            metric, expected, actual
        )),
        NativeBackendError::OutOfSpace => {
            SqliteGraphError::connection("Out of space in file".to_string())
        }
        NativeBackendError::CorruptFreeSpace { reason } => {
            SqliteGraphError::connection(format!("Corrupt free space: {}", reason))
        }
        NativeBackendError::TransactionRolledBack(reason) => {
            SqliteGraphError::connection(format!("Transaction rolled back: {}", reason))
        }
        NativeBackendError::NodeNotFound { node_id, operation } => {
            SqliteGraphError::query(format!("Node {} not found during {}", node_id, operation))
        }
        NativeBackendError::InvalidParameter { context, .. } => {
            SqliteGraphError::query(format!("Invalid parameter: {}", context))
        }
        NativeBackendError::InvalidState { context, .. } => {
            SqliteGraphError::connection(format!("Invalid state: {}", context))
        }
        NativeBackendError::CorruptionDetected { context, .. } => {
            SqliteGraphError::connection(format!("Corruption detected: {}", context))
        }
        NativeBackendError::InvalidConfiguration { parameter, reason } => {
            SqliteGraphError::InvalidInput(format!("Invalid {}: {}", parameter, reason))
        }
        NativeBackendError::VersionMismatch {
            expected, found, ..
        } => SqliteGraphError::connection(format!(
            "Version mismatch: expected {}, found {}",
            expected, found
        )),
        // New V2 WAL error variants
        NativeBackendError::NodeExists { node_id } => {
            SqliteGraphError::query(format!("Node {} already exists", node_id))
        }
        NativeBackendError::EdgeExists { edge_id } => {
            SqliteGraphError::query(format!("Edge {} already exists", edge_id))
        }
        NativeBackendError::EdgeNotFound { edge_id } => {
            SqliteGraphError::query(format!("Edge {} not found", edge_id))
        }
        NativeBackendError::TransactionNotFound { tx_id } => {
            SqliteGraphError::connection(format!("Transaction {} not found", tx_id))
        }
        NativeBackendError::SavepointNotFound { savepoint_id } => {
            SqliteGraphError::connection(format!("Savepoint {} not found", savepoint_id))
        }
        NativeBackendError::DeadlockDetected { tx_id, .. } => {
            SqliteGraphError::connection(format!("Deadlock detected for transaction {}", tx_id))
        }
        NativeBackendError::InvalidTransaction { tx_id, reason } => {
            SqliteGraphError::connection(format!("Invalid transaction {}: {}", tx_id, reason))
        }
        NativeBackendError::IoError { context, .. } => {
            SqliteGraphError::connection(format!("I/O error: {}", context))
        }
        NativeBackendError::InvalidTransactionState { tx_id, state } => {
            SqliteGraphError::connection(format!("Invalid transaction {} state: {}", tx_id, state))
        }
        NativeBackendError::Recovery(message) => {
            SqliteGraphError::connection(format!("Recovery error: {}", message))
        }
        NativeBackendError::MigrationFailed(message) => {
            SqliteGraphError::connection(format!("Migration failed: {}", message))
        }
        NativeBackendError::TransactionIdExhaustion {
            current_id,
            remaining,
        } => SqliteGraphError::connection(format!(
            "Transaction ID exhaustion at ID {}: {} remaining",
            current_id, remaining
        )),
        NativeBackendError::WalContiguityViolation(msg) => {
            SqliteGraphError::connection(format!("WAL contiguity violation: {}", msg))
        }
        NativeBackendError::SerializationError { context } => {
            SqliteGraphError::connection(format!("Serialization error: {}", context))
        }
        NativeBackendError::DeserializationError { context } => {
            SqliteGraphError::connection(format!("Deserialization error: {}", context))
        }
        NativeBackendError::LockError { context } => {
            SqliteGraphError::connection(format!("Lock error: {}", context))
        }
        NativeBackendError::InvalidOperation { context } => {
            SqliteGraphError::connection(format!("Invalid operation: {}", context))
        }
    }
}

/// Convert NodeSpec to NodeRecord for storage
pub fn node_spec_to_record(spec: NodeSpec, node_id: NativeNodeId) -> NodeRecord {
    NodeRecord::new(node_id, spec.kind, spec.name, spec.data)
}

/// Convert NodeSpec to NodeRecordV2 for storage, preserving cluster metadata
///
/// This is used by update_node to modify node data while preserving adjacency
/// cluster offsets. The cluster metadata MUST be preserved or all edges
/// connected to this node would be lost.
///
/// # Arguments
/// * `spec` - New node specification with updated kind/name/data
/// * `node_id` - ID of the node being updated
/// * `old_record` - Existing record to preserve cluster metadata from
///
/// # Returns
/// A new NodeRecordV2 with updated data but preserved cluster offsets
pub fn node_spec_to_v2_record(
    spec: NodeSpec,
    node_id: NativeNodeId,
    old_record: &crate::backend::native::v2::node_record_v2::NodeRecordV2,
) -> Result<crate::backend::native::v2::node_record_v2::NodeRecordV2, SqliteGraphError> {
    Ok(crate::backend::native::v2::node_record_v2::NodeRecordV2 {
        id: node_id as i64,
        flags: old_record.flags,
        kind: spec.kind,
        name: spec.name,
        data: spec.data,
        // Preserve cluster metadata - this is critical!
        outgoing_cluster_offset: old_record.outgoing_cluster_offset,
        outgoing_cluster_size: old_record.outgoing_cluster_size,
        outgoing_edge_count: old_record.outgoing_edge_count,
        incoming_cluster_offset: old_record.incoming_cluster_offset,
        incoming_cluster_size: old_record.incoming_cluster_size,
        incoming_edge_count: old_record.incoming_edge_count,
    })
}

/// Convert NodeRecord from storage to GraphEntity
pub fn node_record_to_entity(record: NodeRecord) -> GraphEntity {
    GraphEntity {
        id: record.id as i64,
        kind: record.kind,
        name: record.name,
        file_path: None, // Native backend doesn't store file_path
        data: record.data,
    }
}

/// Convert EdgeSpec to EdgeRecord for storage
pub fn edge_spec_to_record(spec: EdgeSpec, edge_id: NativeEdgeId) -> EdgeRecord {
    // DEBUG: Print what EdgeSpec contains before conversion to EdgeRecord
    if std::env::var("EDGE_DEBUG").is_ok() {
        println!(
            "[EDGE_DEBUG] edge_spec_to_record: from={}, to={}, edge_type={}",
            spec.from, spec.to, spec.edge_type
        );
    }

    EdgeRecord::new(
        edge_id,
        spec.from as NativeNodeId,
        spec.to as NativeNodeId,
        spec.edge_type,
        spec.data,
    )
}

/// Validate node exists and is accessible
pub fn validate_node_exists(
    graph_file: &mut super::graph_file::GraphFile,
    node_id: NativeNodeId,
) -> Result<(), NativeBackendError> {
    let mut node_store = super::node_store::NodeStore::new(graph_file);

    // Try to read the node - this will return an error if node doesn't exist
    node_store.read_node(node_id)?;

    Ok(())
}

/// Validate edge exists and is accessible
pub fn validate_edge_exists(
    graph_file: &mut super::graph_file::GraphFile,
    edge_id: NativeEdgeId,
) -> Result<(), NativeBackendError> {
    let mut edge_store = super::edge_store::EdgeStore::new(graph_file);

    // Try to read the edge - this will return an error if edge doesn't exist
    edge_store.read_edge(edge_id)?;

    Ok(())
}

/// Validate node ID is in valid range
pub fn validate_node_id_range(
    graph_file: &super::graph_file::GraphFile,
    node_id: NativeNodeId,
) -> Result<(), NativeBackendError> {
    let header = graph_file.persistent_header();

    // Check lower bound (must be positive)
    if node_id <= 0 {
        return Err(NativeBackendError::InvalidNodeId {
            id: node_id,
            max_id: header.node_count as NativeNodeId,
        });
    }

    // For upper bound, allow both existing nodes and reasonable future allocation
    // Allow up to 100,000 OR current node count + space for 1000 more nodes
    let max_allowed = std::cmp::max(100_000, header.node_count + 1000);
    if node_id > max_allowed as NativeNodeId {
        return Err(NativeBackendError::InvalidNodeId {
            id: node_id,
            max_id: max_allowed as NativeNodeId,
        });
    }

    Ok(())
}

/// Validate edge ID is in valid range
pub fn validate_edge_id_range(
    graph_file: &super::graph_file::GraphFile,
    edge_id: NativeEdgeId,
) -> Result<(), NativeBackendError> {
    let header = graph_file.persistent_header();

    if edge_id <= 0 || edge_id > header.edge_count as NativeEdgeId {
        return Err(NativeBackendError::InvalidEdgeId {
            id: edge_id,
            max_id: header.edge_count as NativeEdgeId,
        });
    }

    Ok(())
}

/// Check if file operations are in a consistent state
pub fn check_file_consistency(
    graph_file: &super::graph_file::GraphFile,
) -> Result<(), NativeBackendError> {
    let header = graph_file.persistent_header();

    // Basic header validation
    #[allow(clippy::absurd_extreme_comparisons)]
    if header.node_count < 0 || header.edge_count < 0 {
        return Err(NativeBackendError::CorruptNodeRecord {
            node_id: 0,
            reason: "Negative counts in header".to_string(),
        });
    }

    // Check for reasonable limits
    if header.node_count > 1_000_000 || header.edge_count > 10_000_000 {
        return Err(NativeBackendError::CorruptNodeRecord {
            node_id: 0,
            reason: "Counts exceed reasonable limits".to_string(),
        });
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::super::graph_file::GraphFile;
    use super::*;
    use tempfile::NamedTempFile;

    #[test]
    fn test_error_mapping() {
        let node_error = NativeBackendError::InvalidNodeId { id: 0, max_id: 10 };
        let mapped = map_to_graph_error(node_error);

        match mapped {
            SqliteGraphError::QueryError(msg) => {
                assert!(msg.contains("Invalid node ID"));
                assert!(msg.contains("0"));
                assert!(msg.contains("10"));
            }
            _ => panic!("Expected QueryError"),
        }
    }

    #[test]
    fn test_node_spec_to_record() {
        let spec = NodeSpec {
            kind: "Test".to_string(),
            name: "test_node".to_string(),
            file_path: Some("/path/to/file".to_string()),
            data: serde_json::json!({"key": "value"}),
        };

        let record = node_spec_to_record(spec, 5);
        assert_eq!(record.id, 5);
        assert_eq!(record.kind, "Test");
        assert_eq!(record.name, "test_node");
        assert_eq!(record.data, serde_json::json!({"key": "value"}));
    }

    #[test]
    fn test_node_record_to_entity() {
        let record = NodeRecord::new(
            42,
            "Test".to_string(),
            "test_node".to_string(),
            serde_json::json!({"key": "value"}),
        );

        let entity = node_record_to_entity(record);
        assert_eq!(entity.id, 42);
        assert_eq!(entity.kind, "Test");
        assert_eq!(entity.name, "test_node");
        assert_eq!(entity.data, serde_json::json!({"key": "value"}));
    }

    #[test]
    fn test_validate_node_id_range() {
        let temp_file = NamedTempFile::new().unwrap();
        let path = temp_file.path();
        let graph_file = GraphFile::create(path).unwrap();

        // Valid node ID should pass (even though node doesn't exist yet)
        assert!(validate_node_id_range(&graph_file, 1).is_ok());

        // Invalid node IDs should fail
        assert!(validate_node_id_range(&graph_file, 0).is_err());
        assert!(validate_node_id_range(&graph_file, -1).is_err());
        assert!(validate_node_id_range(&graph_file, 1000000).is_err());
    }

    #[test]
    fn test_check_file_consistency() {
        let temp_file = NamedTempFile::new().unwrap();
        let path = temp_file.path();
        let graph_file = GraphFile::create(path).unwrap();

        // Fresh file should be consistent
        assert!(check_file_consistency(&graph_file).is_ok());
    }
}