oxirs-star 0.2.4

RDF-star and SPARQL-star grammar support for quoted triples
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
//! Memory-efficient storage for large RDF-star graphs using scirs2-core
//!
//! This module provides disk-backed, memory-mapped storage for RDF-star triples
//! enabling processing of datasets larger than available RAM.

use crate::{StarError, StarResult, StarTerm, StarTriple};
use scirs2_core::memory_efficient::{create_mmap, AccessMode, MemoryMappedArray};
use scirs2_core::ndarray_ext::Array1;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};

/// Memory-efficient RDF-star store for large datasets
///
/// Uses memory-mapped files and lazy evaluation to handle graphs
/// larger than available RAM.
pub struct MemoryEfficientStore {
    /// Base directory for storage
    base_path: PathBuf,
    /// Memory-mapped triple storage
    triple_data: Option<MemoryMappedArray<u8>>,
    /// Chunked index for efficient queries
    index: ChunkedIndex,
    /// Statistics
    stats: StoreStatistics,
    /// Configuration
    config: StoreConfig,
}

/// Configuration for memory-efficient storage
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StoreConfig {
    /// Chunk size for processing (in number of triples)
    pub chunk_size: usize,
    /// Maximum memory usage (in bytes)
    pub max_memory: usize,
    /// Enable compression
    pub enable_compression: bool,
    /// Access mode for memory mapping
    pub access_mode: MappedAccessMode,
}

/// Access mode for memory-mapped files
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MappedAccessMode {
    /// Read-only access
    ReadOnly,
    /// Read-write access
    ReadWrite,
    /// Copy-on-write access
    CopyOnWrite,
}

impl Default for StoreConfig {
    fn default() -> Self {
        Self {
            chunk_size: 10000,
            max_memory: 1 << 30, // 1GB
            enable_compression: true,
            access_mode: MappedAccessMode::ReadWrite,
        }
    }
}

/// Statistics for memory-efficient store
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct StoreStatistics {
    /// Total triples stored
    pub total_triples: usize,
    /// Memory usage (bytes)
    pub memory_usage: usize,
    /// Disk usage (bytes)
    pub disk_usage: usize,
    /// Cache hits
    pub cache_hits: usize,
    /// Cache misses
    pub cache_misses: usize,
}

/// Chunked index for efficient queries
struct ChunkedIndex {
    /// Subject index chunks
    subject_chunks: Vec<HashMap<String, Vec<usize>>>,
    /// Predicate index chunks
    predicate_chunks: Vec<HashMap<String, Vec<usize>>>,
    /// Object index chunks
    object_chunks: Vec<HashMap<String, Vec<usize>>>,
}

impl ChunkedIndex {
    fn new() -> Self {
        Self {
            subject_chunks: Vec::new(),
            predicate_chunks: Vec::new(),
            object_chunks: Vec::new(),
        }
    }

    fn add_chunk(&mut self) {
        self.subject_chunks.push(HashMap::new());
        self.predicate_chunks.push(HashMap::new());
        self.object_chunks.push(HashMap::new());
    }
}

impl MemoryEfficientStore {
    /// Create a new memory-efficient store
    pub fn new<P: AsRef<Path>>(path: P) -> StarResult<Self> {
        Self::with_config(path, StoreConfig::default())
    }

    /// Create with custom configuration
    pub fn with_config<P: AsRef<Path>>(path: P, config: StoreConfig) -> StarResult<Self> {
        let base_path = path.as_ref().to_path_buf();

        // Create directory if it doesn't exist
        if !base_path.exists() {
            std::fs::create_dir_all(&base_path).map_err(|e| {
                StarError::resource_error(format!("Failed to create directory: {}", e))
            })?;
        }

        Ok(Self {
            base_path,
            triple_data: None,
            index: ChunkedIndex::new(),
            stats: StoreStatistics::default(),
            config,
        })
    }

    /// Insert triples in batches for efficient disk writes
    pub fn insert_batch(&mut self, triples: &[StarTriple]) -> StarResult<()> {
        // Serialize triples to bytes
        let serialized = self.serialize_triples(triples)?;

        // Write to memory-mapped file
        let data_path = self.base_path.join("triples.bin");

        if self.triple_data.is_none() {
            // Create initial memory map
            let array = Array1::from_vec(serialized.clone());
            let mmap = create_mmap(&array, &data_path, AccessMode::ReadWrite, 0).map_err(|e| {
                StarError::resource_error(format!("Failed to create memory map: {}", e))
            })?;
            self.triple_data = Some(mmap);
        } else {
            // Append to existing memory map
            self.append_data(&serialized)?;
        }

        // Update indices in chunks
        self.update_indices_chunked(triples)?;

        // Update statistics
        self.stats.total_triples += triples.len();
        self.stats.disk_usage += serialized.len();

        Ok(())
    }

    /// Query triples by subject with chunked processing
    pub fn query_by_subject_chunked<F>(
        &self,
        subject: &StarTerm,
        mut processor: F,
    ) -> StarResult<()>
    where
        F: FnMut(&StarTriple) -> StarResult<()>,
    {
        let subject_key = self.term_to_key(subject);

        // Query each chunk
        for (chunk_id, chunk_index) in self.index.subject_chunks.iter().enumerate() {
            if let Some(indices) = chunk_index.get(&subject_key) {
                for &idx in indices {
                    let triple = self.load_triple_from_chunk(chunk_id, idx)?;
                    processor(&triple)?;
                }
            }
        }

        Ok(())
    }

    /// Process entire store in chunks (for large operations)
    pub fn process_all_chunks<F>(&self, mut processor: F) -> StarResult<()>
    where
        F: FnMut(&[StarTriple]) -> StarResult<()>,
    {
        let total_chunks = self.index.subject_chunks.len();

        for chunk_id in 0..total_chunks {
            let triples = self.load_chunk(chunk_id)?;
            processor(&triples)?;
        }

        Ok(())
    }

    /// Optimize storage (compact, reindex, garbage collect)
    pub fn optimize(&mut self) -> StarResult<()> {
        // Compact fragmented data
        self.compact_storage()?;

        // Rebuild indices
        self.rebuild_indices()?;

        // Update statistics
        self.update_statistics()?;

        Ok(())
    }

    /// Get store statistics
    pub fn statistics(&self) -> &StoreStatistics {
        &self.stats
    }

    /// Serialize triples to bytes
    fn serialize_triples(&self, triples: &[StarTriple]) -> StarResult<Vec<u8>> {
        // Use bincode 2.0 for efficient serialization
        oxicode::serde::encode_to_vec(&triples, oxicode::config::standard())
            .map_err(|e| StarError::serialization_error(format!("Serialization failed: {}", e)))
    }

    /// Deserialize triples from bytes
    #[allow(dead_code)]
    fn deserialize_triples(&self, data: &[u8]) -> StarResult<Vec<StarTriple>> {
        oxicode::serde::decode_from_slice(data, oxicode::config::standard())
            .map(|(triples, _)| triples)
            .map_err(|e| StarError::parse_error(format!("Deserialization failed: {}", e)))
    }

    /// Append data to existing memory map
    fn append_data(&mut self, data: &[u8]) -> StarResult<()> {
        // For now, we'll recreate the memory map with appended data
        // In production, this should use a more sophisticated append mechanism
        let mut current_data = Vec::new();

        // Note: MemoryMappedArray doesn't expose raw data directly
        // In practice, we'd need to read from the file or use a different approach

        current_data.extend_from_slice(data);

        let data_path = self.base_path.join("triples.bin");
        let array = Array1::from_vec(current_data);
        let mmap = create_mmap(&array, &data_path, AccessMode::ReadWrite, 0).map_err(|e| {
            StarError::resource_error(format!("Failed to recreate memory map: {}", e))
        })?;

        self.triple_data = Some(mmap);

        Ok(())
    }

    /// Update indices for new triples in chunks
    fn update_indices_chunked(&mut self, triples: &[StarTriple]) -> StarResult<()> {
        let chunk_id = self.index.subject_chunks.len();

        if chunk_id == 0 || triples.len() >= self.config.chunk_size {
            self.index.add_chunk();
        }

        let current_chunk_id = self.index.subject_chunks.len() - 1;

        // Collect keys first to avoid borrow issues
        let keys: Vec<(String, String, String)> = triples
            .iter()
            .map(|triple| {
                (
                    Self::term_to_key_static(&triple.subject),
                    Self::term_to_key_static(&triple.predicate),
                    Self::term_to_key_static(&triple.object),
                )
            })
            .collect();

        // Now update indices
        let subject_chunk = &mut self.index.subject_chunks[current_chunk_id];
        let predicate_chunk = &mut self.index.predicate_chunks[current_chunk_id];
        let object_chunk = &mut self.index.object_chunks[current_chunk_id];

        for (idx, (subject_key, predicate_key, object_key)) in keys.into_iter().enumerate() {
            subject_chunk.entry(subject_key).or_default().push(idx);
            predicate_chunk.entry(predicate_key).or_default().push(idx);
            object_chunk.entry(object_key).or_default().push(idx);
        }

        Ok(())
    }

    /// Convert term to indexable key
    fn term_to_key(&self, term: &StarTerm) -> String {
        Self::term_to_key_static(term)
    }

    /// Static version for avoiding borrow issues
    fn term_to_key_static(term: &StarTerm) -> String {
        format!("{:?}", term)
    }

    /// Load a specific triple from a chunk
    fn load_triple_from_chunk(&self, _chunk_id: usize, _idx: usize) -> StarResult<StarTriple> {
        // Simplified implementation - would need actual chunk-based loading
        Err(StarError::processing_error("Not implemented"))
    }

    /// Load entire chunk
    fn load_chunk(&self, _chunk_id: usize) -> StarResult<Vec<StarTriple>> {
        // Simplified implementation
        Ok(Vec::new())
    }

    /// Compact storage to reduce fragmentation
    fn compact_storage(&mut self) -> StarResult<()> {
        // Implementation would rewrite data without gaps
        Ok(())
    }

    /// Rebuild all indices
    fn rebuild_indices(&mut self) -> StarResult<()> {
        self.index = ChunkedIndex::new();
        // Would reload and reindex all data
        Ok(())
    }

    /// Update storage statistics
    fn update_statistics(&mut self) -> StarResult<()> {
        if let Some(ref mmap) = self.triple_data {
            // Estimate memory usage from mmap size
            self.stats.memory_usage = mmap.size;
        }
        Ok(())
    }
}

/// Streaming iterator for memory-efficient traversal
pub struct ChunkedIterator<'a> {
    store: &'a MemoryEfficientStore,
    current_chunk: usize,
    chunk_offset: usize,
    total_chunks: usize,
}

impl<'a> ChunkedIterator<'a> {
    fn new(store: &'a MemoryEfficientStore) -> Self {
        let total_chunks = store.index.subject_chunks.len();
        Self {
            store,
            current_chunk: 0,
            chunk_offset: 0,
            total_chunks,
        }
    }
}

impl<'a> Iterator for ChunkedIterator<'a> {
    type Item = StarResult<StarTriple>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.current_chunk >= self.total_chunks {
            return None;
        }

        // Load current triple
        match self
            .store
            .load_triple_from_chunk(self.current_chunk, self.chunk_offset)
        {
            Ok(triple) => {
                self.chunk_offset += 1;
                Some(Ok(triple))
            }
            Err(_) => {
                // Move to next chunk
                self.current_chunk += 1;
                self.chunk_offset = 0;
                self.next()
            }
        }
    }
}

impl MemoryEfficientStore {
    /// Get chunked iterator for streaming access
    pub fn iter_chunked(&self) -> ChunkedIterator<'_> {
        ChunkedIterator::new(self)
    }
}

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

    #[test]
    fn test_store_creation() {
        let temp_path = temp_dir().join("oxirs_star_test_store");
        let store = MemoryEfficientStore::new(&temp_path);
        assert!(store.is_ok());

        // Cleanup
        let _ = std::fs::remove_dir_all(&temp_path);
    }

    #[test]
    fn test_config_default() {
        let config = StoreConfig::default();
        assert_eq!(config.chunk_size, 10000);
        assert!(config.enable_compression);
    }

    #[test]
    fn test_statistics() {
        let temp_path = temp_dir().join("oxirs_star_test_stats");
        let store = MemoryEfficientStore::new(&temp_path).unwrap();
        let stats = store.statistics();
        assert_eq!(stats.total_triples, 0);

        // Cleanup
        let _ = std::fs::remove_dir_all(&temp_path);
    }
}