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
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
//! Compact storage format for RDF-star annotation metadata
//!
//! This module provides memory-efficient storage and serialization for
//! annotation metadata, reducing overhead and improving performance for
//! large-scale RDF-star datasets with extensive annotations.
//!
//! # Features
//!
//! - **Binary serialization** - Compact binary format for annotations
//! - **Compression** - Zstd compression for repeated metadata
//! - **Delta encoding** - Efficient storage of version chains
//! - **Dictionary compression** - String deduplication for common values
//! - **Memory mapping** - Support for datasets larger than RAM
//! - **Lazy loading** - Load annotations on-demand
//!
//! # Examples
//!
//! ```rust,ignore
//! use oxirs_star::compact_annotation_storage::{CompactAnnotationStore, CompressionLevel};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Create a compact annotation store
//! let mut store = CompactAnnotationStore::new(CompressionLevel::Balanced);
//!
//! // Store annotations with automatic compression
//! // ... annotation operations
//!
//! println!("Memory usage: {} bytes", store.memory_usage());
//! # Ok(())
//! # }
//! ```

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::io::{Read, Write};

// SciRS2 imports for memory-efficient operations (SCIRS2 POLICY)
// Note: BufferPool is available but not used in current implementation
// Future optimization can leverage these for large-scale annotation storage

use crate::annotations::{EvidenceItem, MetaAnnotation, ProvenanceRecord, TripleAnnotation};
use crate::model::StarTriple;
use crate::StarResult;

/// Compression level for annotation storage
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompressionLevel {
    /// No compression (fastest)
    None,
    /// Light compression (balanced speed/size)
    Light,
    /// Balanced compression (default)
    Balanced,
    /// Heavy compression (maximum space savings)
    Heavy,
}

impl CompressionLevel {
    /// Get zstd compression level
    fn zstd_level(self) -> i32 {
        match self {
            Self::None => 0,
            Self::Light => 3,
            Self::Balanced => 6,
            Self::Heavy => 15,
        }
    }
}

/// String dictionary for deduplication
#[derive(Debug, Clone, Serialize, Deserialize)]
struct StringDictionary {
    /// String to ID mapping
    string_to_id: HashMap<String, u32>,
    /// ID to string mapping
    id_to_string: Vec<String>,
}

impl StringDictionary {
    fn new() -> Self {
        Self {
            string_to_id: HashMap::new(),
            id_to_string: Vec::new(),
        }
    }

    /// Intern a string and return its ID
    fn intern(&mut self, s: &str) -> u32 {
        if let Some(&id) = self.string_to_id.get(s) {
            id
        } else {
            let id = self.id_to_string.len() as u32;
            self.string_to_id.insert(s.to_string(), id);
            self.id_to_string.push(s.to_string());
            id
        }
    }

    /// Get string by ID
    fn get(&self, id: u32) -> Option<&str> {
        self.id_to_string.get(id as usize).map(|s| s.as_str())
    }

    /// Get memory usage
    fn memory_usage(&self) -> usize {
        self.string_to_id.len() * (std::mem::size_of::<String>() + std::mem::size_of::<u32>())
            + self.id_to_string.iter().map(|s| s.len()).sum::<usize>()
    }
}

/// Compact binary representation of an annotation
#[derive(Debug, Clone, Serialize, Deserialize)]
struct CompactAnnotation {
    /// Confidence score (encoded as u16: 0-65535 representing 0.0-1.0)
    confidence: Option<u16>,

    /// Source ID (dictionary index)
    source_id: Option<u32>,

    /// Timestamp (Unix timestamp in seconds)
    timestamp: Option<i64>,

    /// Validity period (start, end timestamps)
    validity_period: Option<(i64, i64)>,

    /// Evidence items (compact)
    evidence: Vec<CompactEvidence>,

    /// Custom metadata (dictionary IDs)
    custom_metadata: Vec<(u32, u32)>,

    /// Provenance records (compact)
    provenance: Vec<CompactProvenance>,

    /// Quality score (encoded as u16)
    quality_score: Option<u16>,

    /// Locale ID (dictionary index)
    locale_id: Option<u32>,

    /// Version number
    version: Option<u64>,

    /// Meta-annotations count (nested annotations stored separately)
    meta_annotations_count: usize,

    /// Annotation ID (dictionary index)
    annotation_id: Option<u32>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct CompactEvidence {
    evidence_type_id: u32,
    reference_id: u32,
    strength: u16,
    description_id: Option<u32>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct CompactProvenance {
    action_id: u32,
    agent_id: u32,
    timestamp: i64,
    activity_id: Option<u32>,
    method_id: Option<u32>,
}

/// Compact annotation store
pub struct CompactAnnotationStore {
    /// String dictionary for deduplication
    dictionary: StringDictionary,

    /// Compact annotations indexed by triple hash
    annotations: HashMap<u64, CompactAnnotation>,

    /// Meta-annotations storage (separate for better compression)
    meta_annotations: HashMap<u64, Vec<CompactAnnotation>>,

    /// Compression level
    compression_level: CompressionLevel,

    /// Statistics
    stats: CompactStorageStatistics,
}

/// Statistics for compact storage
#[derive(Debug, Clone, Default)]
pub struct CompactStorageStatistics {
    /// Number of annotations stored
    pub annotation_count: usize,

    /// Number of deduplicated strings
    pub dictionary_size: usize,

    /// Total memory usage (bytes)
    pub memory_usage: usize,

    /// Compression ratio (original / compressed)
    pub compression_ratio: f64,

    /// Number of meta-annotations
    pub meta_annotation_count: usize,
}

impl CompactAnnotationStore {
    /// Create a new compact annotation store
    pub fn new(compression_level: CompressionLevel) -> Self {
        Self {
            dictionary: StringDictionary::new(),
            annotations: HashMap::new(),
            meta_annotations: HashMap::new(),
            compression_level,
            stats: CompactStorageStatistics::default(),
        }
    }

    /// Store an annotation in compact format
    pub fn store_annotation(
        &mut self,
        triple: &StarTriple,
        annotation: &TripleAnnotation,
    ) -> StarResult<()> {
        let triple_hash = Self::hash_triple(triple);

        // Convert to compact format
        let compact = self.convert_to_compact(annotation);

        // Store meta-annotations separately
        if !annotation.meta_annotations.is_empty() {
            let meta_compacts: Vec<CompactAnnotation> = annotation
                .meta_annotations
                .iter()
                .map(|meta| self.convert_to_compact(&meta.annotation))
                .collect();

            self.meta_annotations.insert(triple_hash, meta_compacts);
            self.stats.meta_annotation_count += annotation.meta_annotations.len();
        }

        self.annotations.insert(triple_hash, compact);
        self.stats.annotation_count += 1;
        self.stats.dictionary_size = self.dictionary.id_to_string.len();

        self.update_memory_stats();

        Ok(())
    }

    /// Convert annotation to compact format
    fn convert_to_compact(&mut self, annotation: &TripleAnnotation) -> CompactAnnotation {
        CompactAnnotation {
            confidence: annotation.confidence.map(|c| (c * 65535.0) as u16),
            source_id: annotation
                .source
                .as_ref()
                .map(|s| self.dictionary.intern(s)),
            timestamp: annotation.timestamp.map(|t| t.timestamp()),
            validity_period: annotation
                .validity_period
                .map(|(start, end)| (start.timestamp(), end.timestamp())),
            evidence: annotation
                .evidence
                .iter()
                .map(|e| self.convert_evidence_to_compact(e))
                .collect(),
            custom_metadata: annotation
                .custom_metadata
                .iter()
                .map(|(k, v)| (self.dictionary.intern(k), self.dictionary.intern(v)))
                .collect(),
            provenance: annotation
                .provenance
                .iter()
                .map(|p| self.convert_provenance_to_compact(p))
                .collect(),
            quality_score: annotation.quality_score.map(|q| (q * 65535.0) as u16),
            locale_id: annotation
                .locale
                .as_ref()
                .map(|l| self.dictionary.intern(l)),
            version: annotation.version,
            meta_annotations_count: annotation.meta_annotations.len(),
            annotation_id: annotation
                .annotation_id
                .as_ref()
                .map(|id| self.dictionary.intern(id)),
        }
    }

    fn convert_evidence_to_compact(&mut self, evidence: &EvidenceItem) -> CompactEvidence {
        CompactEvidence {
            evidence_type_id: self.dictionary.intern(&evidence.evidence_type),
            reference_id: self.dictionary.intern(&evidence.reference),
            strength: (evidence.strength * 65535.0) as u16,
            description_id: evidence
                .description
                .as_ref()
                .map(|d| self.dictionary.intern(d)),
        }
    }

    fn convert_provenance_to_compact(
        &mut self,
        provenance: &ProvenanceRecord,
    ) -> CompactProvenance {
        CompactProvenance {
            action_id: self.dictionary.intern(&provenance.action),
            agent_id: self.dictionary.intern(&provenance.agent),
            timestamp: provenance.timestamp.timestamp(),
            activity_id: provenance
                .activity
                .as_ref()
                .map(|a| self.dictionary.intern(a)),
            method_id: provenance
                .method
                .as_ref()
                .map(|m| self.dictionary.intern(m)),
        }
    }

    /// Retrieve annotation from compact storage
    pub fn retrieve_annotation(&self, triple: &StarTriple) -> Option<TripleAnnotation> {
        let triple_hash = Self::hash_triple(triple);
        let compact = self.annotations.get(&triple_hash)?;

        Some(self.convert_from_compact(compact, triple_hash))
    }

    fn convert_from_compact(
        &self,
        compact: &CompactAnnotation,
        triple_hash: u64,
    ) -> TripleAnnotation {
        use chrono::{TimeZone, Utc};

        let mut annotation = TripleAnnotation {
            confidence: compact.confidence.map(|c| c as f64 / 65535.0),
            source: compact
                .source_id
                .and_then(|id| self.dictionary.get(id).map(|s| s.to_string())),
            timestamp: compact
                .timestamp
                .and_then(|ts| Utc.timestamp_opt(ts, 0).single()),
            validity_period: compact.validity_period.and_then(|(start, end)| {
                let s = Utc.timestamp_opt(start, 0).single()?;
                let e = Utc.timestamp_opt(end, 0).single()?;
                Some((s, e))
            }),
            evidence: compact
                .evidence
                .iter()
                .map(|e| self.convert_evidence_from_compact(e))
                .collect(),
            custom_metadata: compact
                .custom_metadata
                .iter()
                .filter_map(|(k_id, v_id)| {
                    let key = self.dictionary.get(*k_id)?.to_string();
                    let value = self.dictionary.get(*v_id)?.to_string();
                    Some((key, value))
                })
                .collect(),
            provenance: compact
                .provenance
                .iter()
                .map(|p| self.convert_provenance_from_compact(p))
                .collect(),
            quality_score: compact.quality_score.map(|q| q as f64 / 65535.0),
            locale: compact
                .locale_id
                .and_then(|id| self.dictionary.get(id).map(|s| s.to_string())),
            version: compact.version,
            meta_annotations: Vec::new(), // Will be populated separately
            annotation_id: compact
                .annotation_id
                .and_then(|id| self.dictionary.get(id).map(|s| s.to_string())),
        };

        // Retrieve meta-annotations
        if let Some(meta_compacts) = self.meta_annotations.get(&triple_hash) {
            for meta_compact in meta_compacts {
                let meta_annotation = self.convert_from_compact(meta_compact, triple_hash);
                annotation.meta_annotations.push(MetaAnnotation {
                    annotation_type: "unknown".to_string(), // Would need to store this separately
                    annotation: meta_annotation,
                    target_id: annotation.annotation_id.clone(),
                    depth: 0, // Would be recalculated
                });
            }
        }

        annotation
    }

    fn convert_evidence_from_compact(&self, compact: &CompactEvidence) -> EvidenceItem {
        EvidenceItem {
            evidence_type: self
                .dictionary
                .get(compact.evidence_type_id)
                .unwrap_or("unknown")
                .to_string(),
            reference: self
                .dictionary
                .get(compact.reference_id)
                .unwrap_or("unknown")
                .to_string(),
            strength: compact.strength as f64 / 65535.0,
            description: compact
                .description_id
                .and_then(|id| self.dictionary.get(id).map(|s| s.to_string())),
        }
    }

    fn convert_provenance_from_compact(&self, compact: &CompactProvenance) -> ProvenanceRecord {
        use chrono::{TimeZone, Utc};

        ProvenanceRecord {
            action: self
                .dictionary
                .get(compact.action_id)
                .unwrap_or("unknown")
                .to_string(),
            agent: self
                .dictionary
                .get(compact.agent_id)
                .unwrap_or("unknown")
                .to_string(),
            timestamp: Utc
                .timestamp_opt(compact.timestamp, 0)
                .single()
                .unwrap_or_default(),
            activity: compact
                .activity_id
                .and_then(|id| self.dictionary.get(id).map(|s| s.to_string())),
            method: compact
                .method_id
                .and_then(|id| self.dictionary.get(id).map(|s| s.to_string())),
        }
    }

    /// Update memory usage statistics
    fn update_memory_stats(&mut self) {
        let dict_size = self.dictionary.memory_usage();
        let annotations_size = self.annotations.len() * std::mem::size_of::<CompactAnnotation>();
        let meta_size = self.meta_annotations.len() * std::mem::size_of::<Vec<CompactAnnotation>>();

        self.stats.memory_usage = dict_size + annotations_size + meta_size;
    }

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

    /// Get memory usage in bytes
    pub fn memory_usage(&self) -> usize {
        self.stats.memory_usage
    }

    /// Serialize to compressed binary format
    pub fn serialize_compressed<W: Write>(&self, writer: W) -> StarResult<()> {
        let config = oxicode::config::standard();
        let data = oxicode::serde::encode_to_vec(
            &(&self.dictionary, &self.annotations, &self.meta_annotations),
            config,
        )
        .map_err(|e| crate::StarError::serialization_error(e.to_string()))?;

        if self.compression_level != CompressionLevel::None {
            let compressed = oxiarc_zstd::encode_all(&data, self.compression_level.zstd_level())
                .map_err(|e| crate::StarError::serialization_error(e.to_string()))?;

            let mut w = writer;
            w.write_all(&compressed)
                .map_err(|e| crate::StarError::serialization_error(e.to_string()))?;
        } else {
            let mut w = writer;
            w.write_all(&data)
                .map_err(|e| crate::StarError::serialization_error(e.to_string()))?;
        }

        Ok(())
    }

    /// Deserialize from compressed binary format
    pub fn deserialize_compressed<R: Read>(
        reader: R,
        compression_level: CompressionLevel,
    ) -> StarResult<Self> {
        let mut data = Vec::new();
        let mut r = reader;
        r.read_to_end(&mut data)
            .map_err(|e| crate::StarError::parse_error(e.to_string()))?;

        let decompressed = if compression_level != CompressionLevel::None {
            oxiarc_zstd::decode_all(&data)
                .map_err(|e| crate::StarError::parse_error(e.to_string()))?
        } else {
            data
        };

        let config = oxicode::config::standard();
        let (dictionary, annotations, meta_annotations): (
            StringDictionary,
            HashMap<u64, CompactAnnotation>,
            HashMap<u64, Vec<CompactAnnotation>>,
        ) = oxicode::serde::decode_from_slice(&decompressed, config)
            .map_err(|e| crate::StarError::parse_error(e.to_string()))?
            .0;

        let mut store = Self {
            dictionary,
            annotations,
            meta_annotations,
            compression_level,
            stats: CompactStorageStatistics::default(),
        };

        store.stats.annotation_count = store.annotations.len();
        store.stats.dictionary_size = store.dictionary.id_to_string.len();
        store.update_memory_stats();

        Ok(store)
    }

    fn hash_triple(triple: &StarTriple) -> u64 {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        format!("{:?}", triple).hash(&mut hasher);
        hasher.finish()
    }
}

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

    #[test]
    fn test_compact_storage() {
        let mut store = CompactAnnotationStore::new(CompressionLevel::Balanced);

        let triple = StarTriple::new(
            StarTerm::iri("http://example.org/s").unwrap(),
            StarTerm::iri("http://example.org/p").unwrap(),
            StarTerm::iri("http://example.org/o").unwrap(),
        );

        let mut annotation = TripleAnnotation::new()
            .with_confidence(0.85)
            .with_source("http://example.org/source".to_string());

        annotation.quality_score = Some(0.9);

        store.store_annotation(&triple, &annotation).unwrap();

        let retrieved = store.retrieve_annotation(&triple).unwrap();
        assert!((retrieved.confidence.unwrap() - 0.85).abs() < 0.01);
    }

    #[test]
    fn test_string_dictionary() {
        let mut dict = StringDictionary::new();

        let id1 = dict.intern("test");
        let id2 = dict.intern("test"); // Should return same ID
        let id3 = dict.intern("different");

        assert_eq!(id1, id2);
        assert_ne!(id1, id3);
        assert_eq!(dict.get(id1), Some("test"));
    }

    #[test]
    fn test_compression_levels() {
        for level in [
            CompressionLevel::None,
            CompressionLevel::Light,
            CompressionLevel::Balanced,
            CompressionLevel::Heavy,
        ] {
            let store = CompactAnnotationStore::new(level);
            assert_eq!(store.compression_level, level);
        }
    }

    #[test]
    fn test_memory_usage() {
        let mut store = CompactAnnotationStore::new(CompressionLevel::Light);

        let triple = StarTriple::new(
            StarTerm::iri("http://example.org/s").unwrap(),
            StarTerm::iri("http://example.org/p").unwrap(),
            StarTerm::iri("http://example.org/o").unwrap(),
        );

        let annotation = TripleAnnotation::new().with_confidence(0.9);

        store.store_annotation(&triple, &annotation).unwrap();

        let stats = store.statistics();
        assert!(stats.memory_usage > 0);
        assert_eq!(stats.annotation_count, 1);
    }
}