graph_d 1.3.2

A native graph database implementation in Rust with built-in JSON support and SQLite-like simplicity
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
//! Memory-mapped file storage implementation.
//!
//! This module implements persistent storage using memory-mapped files.
//! Note: This is a simplified implementation focused on functionality.
//! Production systems would need more sophisticated record management.

use crate::error::{GraphError, Result};
use crate::graph::{Id, Node, Relationship};
use memmap2::{MmapMut, MmapOptions};
use parking_lot::RwLock;
use std::collections::HashMap;
use std::fs::OpenOptions;
use std::path::Path;
use std::sync::Arc;

// Allow unsafe code only for this module for memory mapping
#[allow(unsafe_code)]
/// Simple file header for the memory-mapped file.
pub struct FileHeader {
    /// Magic bytes for file format identification ("GRPH")
    pub magic: [u8; 4],
    /// File format version number
    pub version: u32,
    /// Number of nodes stored in the file
    pub node_count: u64,
    /// Number of relationships stored in the file
    pub rel_count: u64,
}

impl Default for FileHeader {
    fn default() -> Self {
        Self::new()
    }
}

impl FileHeader {
    const MAGIC: &'static [u8; 4] = b"GRPH";
    const VERSION: u32 = 1;
    const SIZE: usize = 4 + 4 + 8 + 8; // 24 bytes

    /// Create a new file header with default values.
    pub fn new() -> Self {
        FileHeader {
            magic: *Self::MAGIC,
            version: Self::VERSION,
            node_count: 0,
            rel_count: 0,
        }
    }

    /// Check if this header has valid magic bytes and version.
    pub fn is_valid(&self) -> bool {
        self.magic == *Self::MAGIC && self.version == Self::VERSION
    }

    /// Serialize the header to bytes.
    pub fn serialize(&self) -> Vec<u8> {
        let mut data = Vec::with_capacity(Self::SIZE);
        data.extend_from_slice(&self.magic);
        data.extend_from_slice(&self.version.to_le_bytes());
        data.extend_from_slice(&self.node_count.to_le_bytes());
        data.extend_from_slice(&self.rel_count.to_le_bytes());
        data
    }

    /// Deserialize a header from bytes.
    pub fn deserialize(data: &[u8]) -> Result<Self> {
        if data.len() < Self::SIZE {
            return Err(GraphError::Storage("Invalid header size".to_string()));
        }

        let mut magic = [0u8; 4];
        magic.copy_from_slice(&data[0..4]);

        let version = u32::from_le_bytes([data[4], data[5], data[6], data[7]]);
        let node_count = u64::from_le_bytes([
            data[8], data[9], data[10], data[11], data[12], data[13], data[14], data[15],
        ]);
        let rel_count = u64::from_le_bytes([
            data[16], data[17], data[18], data[19], data[20], data[21], data[22], data[23],
        ]);

        Ok(FileHeader {
            magic,
            version,
            node_count,
            rel_count,
        })
    }
}

/// Memory-mapped storage backend.
pub struct MmapStorage {
    /// Memory-mapped file handle
    mmap: Arc<RwLock<MmapMut>>,
    /// Path to the storage file on disk
    file_path: std::path::PathBuf,
    /// In-memory cache for node lookups
    node_cache: Arc<RwLock<HashMap<Id, Node>>>,
    /// In-memory cache for relationship lookups
    relationship_cache: Arc<RwLock<HashMap<Id, Relationship>>>,
    /// Index mapping nodes to their relationship IDs for fast traversal
    node_relationships: Arc<RwLock<HashMap<Id, Vec<Id>>>>,
}

impl MmapStorage {
    /// Create a new memory-mapped storage at the given path.
    pub fn create<P: AsRef<Path>>(path: P) -> Result<Self> {
        let file_path = path.as_ref().to_path_buf();

        // Create the file with initial size
        let initial_size = 1024 * 1024; // 1MB initial size
        let file = OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .truncate(true)
            .open(&file_path)?;

        file.set_len(initial_size)?;

        // Create memory map
        #[allow(unsafe_code)]
        let mut mmap = unsafe { MmapOptions::new().map_mut(&file)? };

        // Initialize file header
        let header = FileHeader::new();
        let header_data = header.serialize();
        mmap[0..FileHeader::SIZE].copy_from_slice(&header_data);

        Ok(MmapStorage {
            mmap: Arc::new(RwLock::new(mmap)),
            file_path,
            node_cache: Arc::new(RwLock::new(HashMap::new())),
            relationship_cache: Arc::new(RwLock::new(HashMap::new())),
            node_relationships: Arc::new(RwLock::new(HashMap::new())),
        })
    }

    /// Open existing memory-mapped storage at the given path.
    pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
        let file_path = path.as_ref().to_path_buf();

        if !file_path.exists() {
            return Self::create(path);
        }

        let file = OpenOptions::new().read(true).write(true).open(&file_path)?;

        #[allow(unsafe_code)]
        let mmap = unsafe { MmapOptions::new().map_mut(&file)? };

        // Validate file header
        if mmap.len() < FileHeader::SIZE {
            return Err(GraphError::Storage(
                "File too small to contain header".to_string(),
            ));
        }

        let header = FileHeader::deserialize(&mmap[0..FileHeader::SIZE])?;
        if !header.is_valid() {
            return Err(GraphError::Storage("Invalid file header".to_string()));
        }

        let storage = MmapStorage {
            mmap: Arc::new(RwLock::new(mmap)),
            file_path,
            node_cache: Arc::new(RwLock::new(HashMap::new())),
            relationship_cache: Arc::new(RwLock::new(HashMap::new())),
            node_relationships: Arc::new(RwLock::new(HashMap::new())),
        };

        // Load existing data into caches
        storage.load_caches()?;

        Ok(storage)
    }

    /// Load existing data from the file into memory caches.
    fn load_caches(&self) -> Result<()> {
        let mmap = self.mmap.read();
        let header = FileHeader::deserialize(&mmap[0..FileHeader::SIZE])?;

        let mut offset = FileHeader::SIZE;

        // Load nodes
        for _ in 0..header.node_count {
            if offset + 8 > mmap.len() {
                break; // Corrupted file or incomplete write
            }

            // Read node data length
            let data_len = u64::from_le_bytes([
                mmap[offset],
                mmap[offset + 1],
                mmap[offset + 2],
                mmap[offset + 3],
                mmap[offset + 4],
                mmap[offset + 5],
                mmap[offset + 6],
                mmap[offset + 7],
            ]) as usize;
            offset += 8;

            if offset + data_len > mmap.len() {
                break; // Incomplete record
            }

            // Deserialize node using JSON
            let node_data = &mmap[offset..offset + data_len];
            if let Ok(json_str) = std::str::from_utf8(node_data) {
                if let Ok(node) = serde_json::from_str::<Node>(json_str) {
                    self.node_cache.write().insert(node.id, node.clone());
                    self.node_relationships.write().entry(node.id).or_default();
                }
            }
            offset += data_len;
        }

        // Load relationships
        for _ in 0..header.rel_count {
            if offset + 8 > mmap.len() {
                break;
            }

            let data_len = u64::from_le_bytes([
                mmap[offset],
                mmap[offset + 1],
                mmap[offset + 2],
                mmap[offset + 3],
                mmap[offset + 4],
                mmap[offset + 5],
                mmap[offset + 6],
                mmap[offset + 7],
            ]) as usize;
            offset += 8;

            if offset + data_len > mmap.len() {
                break;
            }

            let rel_data = &mmap[offset..offset + data_len];
            if let Ok(json_str) = std::str::from_utf8(rel_data) {
                if let Ok(rel) = serde_json::from_str::<Relationship>(json_str) {
                    let from_id = rel.from_id;
                    let to_id = rel.to_id;
                    let rel_id = rel.id;

                    self.relationship_cache.write().insert(rel_id, rel);

                    // Update relationship index
                    self.node_relationships
                        .write()
                        .entry(from_id)
                        .or_default()
                        .push(rel_id);

                    if from_id != to_id {
                        self.node_relationships
                            .write()
                            .entry(to_id)
                            .or_default()
                            .push(rel_id);
                    }
                }
            }
            offset += data_len;
        }

        Ok(())
    }

    /// Store a node in the memory-mapped storage.
    pub fn store_node(&self, node: Node) -> Result<()> {
        // Add to cache first
        self.node_cache.write().insert(node.id, node.clone());

        // Initialize empty relationship list
        self.node_relationships.write().entry(node.id).or_default();

        // TODO: Implement actual persistence to memory-mapped file
        // For now, we'll keep it in cache (in a production system,
        // this would write the node record and properties to the file)

        Ok(())
    }

    /// Retrieve a node by its ID.
    pub fn get_node(&self, id: Id) -> Result<Option<Node>> {
        Ok(self.node_cache.read().get(&id).cloned())
    }

    /// Store a relationship in the memory-mapped storage.
    pub fn store_relationship(&self, relationship: Relationship) -> Result<()> {
        let from_id = relationship.from_id;
        let to_id = relationship.to_id;
        let rel_id = relationship.id;

        // Verify nodes exist
        if !self.node_cache.read().contains_key(&from_id) {
            return Err(GraphError::NotFound(format!("Node {from_id} not found")));
        }
        if !self.node_cache.read().contains_key(&to_id) {
            return Err(GraphError::NotFound(format!("Node {to_id} not found")));
        }

        // Add to cache
        self.relationship_cache.write().insert(rel_id, relationship);

        // Update relationship index
        self.node_relationships
            .write()
            .entry(from_id)
            .or_default()
            .push(rel_id);

        if from_id != to_id {
            self.node_relationships
                .write()
                .entry(to_id)
                .or_default()
                .push(rel_id);
        }

        Ok(())
    }

    /// Retrieve a relationship by its ID.
    pub fn get_relationship(&self, id: Id) -> Result<Option<Relationship>> {
        Ok(self.relationship_cache.read().get(&id).cloned())
    }

    /// Get all relationships for a given node.
    pub fn get_relationships_for_node(&self, node_id: Id) -> Result<Vec<Relationship>> {
        let empty_vec = Vec::new();
        let relationship_ids = self
            .node_relationships
            .read()
            .get(&node_id)
            .unwrap_or(&empty_vec)
            .clone();

        let mut relationships = Vec::new();
        let rel_cache = self.relationship_cache.read();

        for rel_id in relationship_ids {
            if let Some(rel) = rel_cache.get(&rel_id) {
                relationships.push(rel.clone());
            }
        }

        Ok(relationships)
    }

    /// Get the number of nodes in storage.
    pub fn node_count(&self) -> usize {
        self.node_cache.read().len()
    }

    /// Get the number of relationships in storage.
    pub fn relationship_count(&self) -> usize {
        self.relationship_cache.read().len()
    }

    /// Delete a node from storage.
    /// Returns the deleted node if it existed.
    pub fn delete_node(&self, id: Id) -> Result<Option<Node>> {
        // Remove from node_relationships tracking
        self.node_relationships.write().remove(&id);
        // Remove the node
        Ok(self.node_cache.write().remove(&id))
    }

    /// Delete a relationship from storage.
    /// Returns the deleted relationship if it existed.
    pub fn delete_relationship(&self, id: Id) -> Result<Option<Relationship>> {
        if let Some(rel) = self.relationship_cache.write().remove(&id) {
            // Remove from node_relationships tracking
            let mut node_rels = self.node_relationships.write();
            if let Some(rels) = node_rels.get_mut(&rel.from_id) {
                rels.retain(|&r| r != id);
            }
            if rel.from_id != rel.to_id {
                if let Some(rels) = node_rels.get_mut(&rel.to_id) {
                    rels.retain(|&r| r != id);
                }
            }
            Ok(Some(rel))
        } else {
            Ok(None)
        }
    }

    /// Check if a node has any relationships.
    pub fn node_has_relationships(&self, node_id: Id) -> bool {
        self.node_relationships
            .read()
            .get(&node_id)
            .map(|rels| !rels.is_empty())
            .unwrap_or(false)
    }

    /// Flush all cached data to the memory-mapped file.
    pub fn flush(&self) -> Result<()> {
        let mut mmap = self.mmap.write();
        let mut offset = FileHeader::SIZE;

        // Serialize all nodes
        let nodes = self.node_cache.read();
        let relationships = self.relationship_cache.read();

        // Calculate total size needed using JSON serialization
        let mut total_size = FileHeader::SIZE;
        for node in nodes.values() {
            if let Ok(json_str) = serde_json::to_string(node) {
                total_size += 8 + json_str.len(); // 8 bytes for length + data
            }
        }
        for rel in relationships.values() {
            if let Ok(json_str) = serde_json::to_string(rel) {
                total_size += 8 + json_str.len(); // 8 bytes for length + data
            }
        }

        // Resize memory map if needed
        if total_size > mmap.len() {
            drop(mmap); // Release the lock before resizing

            // Resize the file
            let file = OpenOptions::new()
                .read(true)
                .write(true)
                .open(&self.file_path)?;
            file.set_len(total_size as u64 * 2)?; // Double for growth room

            // Recreate memory map
            #[allow(unsafe_code)]
            let new_mmap = unsafe { MmapOptions::new().map_mut(&file)? };
            *self.mmap.write() = new_mmap;
            mmap = self.mmap.write();
        }

        // Write nodes using JSON serialization for reliability with serde_json::Value
        for node in nodes.values() {
            if let Ok(json_str) = serde_json::to_string(node) {
                let serialized = json_str.as_bytes();
                if offset + 8 + serialized.len() > mmap.len() {
                    return Err(GraphError::Storage("Not enough space in mmap".to_string()));
                }

                // Write length
                let len_bytes = (serialized.len() as u64).to_le_bytes();
                mmap[offset..offset + 8].copy_from_slice(&len_bytes);
                offset += 8;

                // Write data
                mmap[offset..offset + serialized.len()].copy_from_slice(serialized);
                offset += serialized.len();
            }
        }

        // Write relationships using JSON serialization
        for rel in relationships.values() {
            if let Ok(json_str) = serde_json::to_string(rel) {
                let serialized = json_str.as_bytes();
                if offset + 8 + serialized.len() > mmap.len() {
                    return Err(GraphError::Storage("Not enough space in mmap".to_string()));
                }

                // Write length
                let len_bytes = (serialized.len() as u64).to_le_bytes();
                mmap[offset..offset + 8].copy_from_slice(&len_bytes);
                offset += 8;

                // Write data
                mmap[offset..offset + serialized.len()].copy_from_slice(serialized);
                offset += serialized.len();
            }
        }

        // Update and write header
        let mut header = FileHeader::new();
        header.node_count = nodes.len() as u64;
        header.rel_count = relationships.len() as u64;

        let header_data = header.serialize();
        mmap[0..FileHeader::SIZE].copy_from_slice(&header_data);

        // Force write to disk
        mmap.flush()?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;
    use tempfile::NamedTempFile;

    #[test]
    fn test_mmap_storage_creation() {
        let temp_file = NamedTempFile::new().unwrap();
        let storage = MmapStorage::create(temp_file.path()).unwrap();

        assert_eq!(storage.node_count(), 0);
        assert_eq!(storage.relationship_count(), 0);
    }

    #[test]
    fn test_mmap_node_operations() {
        let temp_file = NamedTempFile::new().unwrap();
        let storage = MmapStorage::create(temp_file.path()).unwrap();

        let node = Node::new(1, HashMap::new());
        storage.store_node(node.clone()).unwrap();

        let retrieved = storage.get_node(1).unwrap();
        assert_eq!(retrieved, Some(node));
        assert_eq!(storage.node_count(), 1);
    }

    #[test]
    fn test_mmap_persistence() {
        let temp_file = NamedTempFile::new().unwrap();
        let path = temp_file.path().to_path_buf();

        // Create storage and add some data
        {
            let storage = MmapStorage::create(&path).unwrap();
            let mut properties = HashMap::new();
            properties.insert("name".to_string(), serde_json::json!("Alice"));
            properties.insert("age".to_string(), serde_json::json!(30));

            let node = Node::new(1, properties);
            storage.store_node(node).unwrap();

            // Verify data is in cache before flush
            assert_eq!(storage.node_count(), 1);

            storage.flush().unwrap();

            // Verify data is still in cache after flush
            let retrieved = storage.get_node(1).unwrap();
            assert!(retrieved.is_some());
            let retrieved_node = retrieved.unwrap();
            assert_eq!(retrieved_node.id, 1);
            assert_eq!(
                retrieved_node.get_property("name"),
                Some(&serde_json::json!("Alice"))
            );
        }

        // Reopen storage and verify persistence
        {
            let storage = MmapStorage::open(&path).unwrap();
            assert_eq!(storage.node_count(), 1, "Data should persist after reopen");

            let retrieved = storage.get_node(1).unwrap();
            assert!(retrieved.is_some());
            let retrieved_node = retrieved.unwrap();
            assert_eq!(retrieved_node.id, 1);
            assert_eq!(
                retrieved_node.get_property("name"),
                Some(&serde_json::json!("Alice"))
            );
            assert_eq!(
                retrieved_node.get_property("age"),
                Some(&serde_json::json!(30))
            );
        }
    }
}