oxirs-tdb 0.3.1

Apache Jena TDB/TDB2 compatible RDF storage engine with B+Tree indexes
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
//! Range queries on indexed triple components.
//!
//! Provides a three-way `BTreeMap`-based index (SPO / POS / OSP) that supports
//! efficient range scans by subject prefix, exact predicate match, or
//! object lexicographic range.

use std::collections::BTreeMap;

/// A range predicate for triple-component queries.
#[derive(Debug, Clone, Default)]
pub struct TripleRange {
    /// If set, only triples whose subject starts with this prefix are returned.
    pub subject_prefix: Option<String>,
    /// If set, only triples with exactly this predicate are returned.
    pub predicate_exact: Option<String>,
    /// If set, only triples whose object is lexicographically >= this value.
    pub object_min: Option<String>,
    /// If set, only triples whose object is lexicographically <= this value.
    pub object_max: Option<String>,
}

impl TripleRange {
    /// Construct an empty range that matches all triples.
    pub fn new() -> Self {
        Self::default()
    }

    /// Match only triples where the subject starts with the given prefix.
    pub fn with_subject_prefix(mut self, prefix: impl Into<String>) -> Self {
        self.subject_prefix = Some(prefix.into());
        self
    }

    /// Match only triples with exactly this predicate.
    pub fn with_predicate(mut self, predicate: impl Into<String>) -> Self {
        self.predicate_exact = Some(predicate.into());
        self
    }

    /// Match only triples whose object is >= this lower bound.
    pub fn with_object_min(mut self, min: impl Into<String>) -> Self {
        self.object_min = Some(min.into());
        self
    }

    /// Match only triples whose object is <= this upper bound.
    pub fn with_object_max(mut self, max: impl Into<String>) -> Self {
        self.object_max = Some(max.into());
        self
    }

    /// Evaluate whether a `(subject, predicate, object)` triple matches this range.
    fn matches(&self, s: &str, p: &str, o: &str) -> bool {
        if let Some(ref prefix) = self.subject_prefix {
            if !s.starts_with(prefix.as_str()) {
                return false;
            }
        }
        if let Some(ref exact) = self.predicate_exact {
            if p != exact.as_str() {
                return false;
            }
        }
        if let Some(ref min) = self.object_min {
            if o < min.as_str() {
                return false;
            }
        }
        if let Some(ref max) = self.object_max {
            if o > max.as_str() {
                return false;
            }
        }
        true
    }
}

/// A three-index structure for efficient triple range queries.
///
/// Maintains three `BTreeMap`s keyed by (S,P,O), (P,O,S), and (O,S,P)
/// respectively, enabling efficient scans regardless of the query access
/// pattern.
pub struct RangeIndex {
    /// Subject → Predicate → Object
    spo: BTreeMap<(String, String, String), ()>,
    /// Predicate → Object → Subject
    pos: BTreeMap<(String, String, String), ()>,
    /// Object → Subject → Predicate
    osp: BTreeMap<(String, String, String), ()>,
}

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

impl RangeIndex {
    /// Create an empty index.
    pub fn new() -> Self {
        Self {
            spo: BTreeMap::new(),
            pos: BTreeMap::new(),
            osp: BTreeMap::new(),
        }
    }

    /// Insert a triple into all three indexes.
    pub fn insert(&mut self, s: &str, p: &str, o: &str) {
        self.spo
            .insert((s.to_string(), p.to_string(), o.to_string()), ());
        self.pos
            .insert((p.to_string(), o.to_string(), s.to_string()), ());
        self.osp
            .insert((o.to_string(), s.to_string(), p.to_string()), ());
    }

    /// Remove a triple from all three indexes.  Returns `true` if the triple
    /// was present, `false` if it was not found.
    pub fn remove(&mut self, s: &str, p: &str, o: &str) -> bool {
        let key_spo = (s.to_string(), p.to_string(), o.to_string());
        if self.spo.remove(&key_spo).is_none() {
            return false;
        }
        self.pos
            .remove(&(p.to_string(), o.to_string(), s.to_string()));
        self.osp
            .remove(&(o.to_string(), s.to_string(), p.to_string()));
        true
    }

    /// Query triples matching the given range descriptor.
    ///
    /// Internally chooses the best index depending on which constraints are
    /// set, falling back to a full SPO scan with in-memory filtering when no
    /// single-index acceleration is possible.
    pub fn query(&self, range: &TripleRange) -> Vec<(String, String, String)> {
        let mut results = Vec::new();

        // Fast path: predicate-only or predicate + object range — use POS index
        if range.predicate_exact.is_some()
            && range.subject_prefix.is_none()
            && (range.object_min.is_some() || range.object_max.is_some())
        {
            let p = range.predicate_exact.as_deref().unwrap_or("");
            let o_lo: &str = range.object_min.as_deref().unwrap_or("");
            let o_hi: &str = range.object_max.as_deref().unwrap_or("\u{10FFFF}");

            let from = (p.to_string(), o_lo.to_string(), String::new());
            let to = (p.to_string(), o_hi.to_string(), "\u{10FFFF}".to_string());

            for ((kp, ko, ks), _) in self.pos.range(from..=to) {
                if kp != p {
                    break;
                }
                if ko.as_str() < o_lo || ko.as_str() > o_hi {
                    continue;
                }
                if range.matches(ks.as_str(), kp.as_str(), ko.as_str()) {
                    results.push((ks.clone(), kp.clone(), ko.clone()));
                }
            }
            return results;
        }

        // Predicate-only fast path — use POS index
        if let Some(ref p) = range.predicate_exact {
            if range.subject_prefix.is_none() && range.object_min.is_none() && range.object_max.is_none() {
                let from = (p.clone(), String::new(), String::new());
                let to = (p.clone(), "\u{10FFFF}".to_string(), "\u{10FFFF}".to_string());
                for ((kp, ko, ks), _) in self.pos.range(from..=to) {
                    if kp != p.as_str() {
                        break;
                    }
                    results.push((ks.clone(), kp.clone(), ko.clone()));
                }
                return results;
            }
        }

        // General path — iterate SPO and apply filter
        for ((s, p, o), _) in &self.spo {
            if range.matches(s.as_str(), p.as_str(), o.as_str()) {
                results.push((s.clone(), p.clone(), o.clone()));
            }
        }

        results
    }

    /// Return the total number of triples stored.
    pub fn count(&self) -> usize {
        self.spo.len()
    }

    /// Return the distinct subjects associated with the given predicate.
    pub fn subjects_for_predicate(&self, predicate: &str) -> Vec<String> {
        let from = (predicate.to_string(), String::new(), String::new());
        let to = (
            predicate.to_string(),
            "\u{10FFFF}".to_string(),
            "\u{10FFFF}".to_string(),
        );
        let mut subjects: Vec<String> = self
            .pos
            .range(from..=to)
            .filter(|((p, _, _), _)| p.as_str() == predicate)
            .map(|((_, _, s), _)| s.clone())
            .collect();
        subjects.sort();
        subjects.dedup();
        subjects
    }

    /// Return the distinct objects associated with the given predicate.
    pub fn objects_for_predicate(&self, predicate: &str) -> Vec<String> {
        let from = (predicate.to_string(), String::new(), String::new());
        let to = (
            predicate.to_string(),
            "\u{10FFFF}".to_string(),
            "\u{10FFFF}".to_string(),
        );
        let mut objects: Vec<String> = self
            .pos
            .range(from..=to)
            .filter(|((p, _, _), _)| p.as_str() == predicate)
            .map(|((_, o, _), _)| o.clone())
            .collect();
        objects.sort();
        objects.dedup();
        objects
    }

    /// Return the distinct predicates for the given subject.
    pub fn predicates_for_subject(&self, subject: &str) -> Vec<String> {
        let from = (subject.to_string(), String::new(), String::new());
        let to = (
            subject.to_string(),
            "\u{10FFFF}".to_string(),
            "\u{10FFFF}".to_string(),
        );
        let mut predicates: Vec<String> = self
            .spo
            .range(from..=to)
            .filter(|((s, _, _), _)| s.as_str() == subject)
            .map(|((_, p, _), _)| p.clone())
            .collect();
        predicates.sort();
        predicates.dedup();
        predicates
    }

    /// Return all triples where the subject equals `subject`.
    pub fn triples_for_subject(&self, subject: &str) -> Vec<(String, String, String)> {
        let from = (subject.to_string(), String::new(), String::new());
        let to = (
            subject.to_string(),
            "\u{10FFFF}".to_string(),
            "\u{10FFFF}".to_string(),
        );
        self.spo
            .range(from..=to)
            .filter(|((s, _, _), _)| s.as_str() == subject)
            .map(|((s, p, o), _)| (s.clone(), p.clone(), o.clone()))
            .collect()
    }

    /// Return all triples where the predicate equals `predicate`.
    pub fn triples_for_predicate(&self, predicate: &str) -> Vec<(String, String, String)> {
        let from = (predicate.to_string(), String::new(), String::new());
        let to = (
            predicate.to_string(),
            "\u{10FFFF}".to_string(),
            "\u{10FFFF}".to_string(),
        );
        self.pos
            .range(from..=to)
            .filter(|((p, _, _), _)| p.as_str() == predicate)
            .map(|((p, o, s), _)| (s.clone(), p.clone(), o.clone()))
            .collect()
    }
}

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

    fn populated_index() -> RangeIndex {
        let mut idx = RangeIndex::new();
        idx.insert("s:alice", "p:age", "30");
        idx.insert("s:alice", "p:name", "Alice");
        idx.insert("s:bob", "p:age", "25");
        idx.insert("s:bob", "p:name", "Bob");
        idx.insert("s:carol", "p:age", "35");
        idx.insert("s:carol", "p:name", "Carol");
        idx.insert("s:dave", "p:score", "100");
        idx
    }

    // --- insert / count ---
    #[test]
    fn test_insert_and_count() {
        let mut idx = RangeIndex::new();
        assert_eq!(idx.count(), 0);
        idx.insert("s", "p", "o");
        assert_eq!(idx.count(), 1);
        idx.insert("s", "p", "o2");
        assert_eq!(idx.count(), 2);
    }

    #[test]
    fn test_insert_duplicate_no_increase() {
        let mut idx = RangeIndex::new();
        idx.insert("s", "p", "o");
        idx.insert("s", "p", "o"); // duplicate
        assert_eq!(idx.count(), 1);
    }

    // --- remove ---
    #[test]
    fn test_remove_present() {
        let mut idx = RangeIndex::new();
        idx.insert("s", "p", "o");
        assert!(idx.remove("s", "p", "o"));
        assert_eq!(idx.count(), 0);
    }

    #[test]
    fn test_remove_absent() {
        let mut idx = RangeIndex::new();
        assert!(!idx.remove("s", "p", "o"));
    }

    #[test]
    fn test_remove_correct_triple() {
        let mut idx = RangeIndex::new();
        idx.insert("s", "p", "o1");
        idx.insert("s", "p", "o2");
        idx.remove("s", "p", "o1");
        assert_eq!(idx.count(), 1);
        let q = TripleRange::new();
        let results = idx.query(&q);
        assert_eq!(results[0].2, "o2");
    }

    // --- query: no constraints ---
    #[test]
    fn test_query_no_constraints_returns_all() {
        let idx = populated_index();
        let results = idx.query(&TripleRange::new());
        assert_eq!(results.len(), 7);
    }

    // --- query: subject prefix ---
    #[test]
    fn test_query_subject_prefix() {
        let idx = populated_index();
        let range = TripleRange::new().with_subject_prefix("s:alice");
        let results = idx.query(&range);
        assert_eq!(results.len(), 2);
        assert!(results.iter().all(|(s, _, _)| s.starts_with("s:alice")));
    }

    #[test]
    fn test_query_subject_prefix_no_match() {
        let idx = populated_index();
        let range = TripleRange::new().with_subject_prefix("s:zzzz");
        assert!(idx.query(&range).is_empty());
    }

    // --- query: predicate exact ---
    #[test]
    fn test_query_predicate_exact() {
        let idx = populated_index();
        let range = TripleRange::new().with_predicate("p:age");
        let results = idx.query(&range);
        assert_eq!(results.len(), 3);
        assert!(results.iter().all(|(_, p, _)| p == "p:age"));
    }

    #[test]
    fn test_query_predicate_exact_no_match() {
        let idx = populated_index();
        let range = TripleRange::new().with_predicate("p:unknown");
        assert!(idx.query(&range).is_empty());
    }

    // --- query: object range ---
    #[test]
    fn test_query_object_min() {
        let idx = populated_index();
        // ages are "25", "30", "35"
        let range = TripleRange::new()
            .with_predicate("p:age")
            .with_object_min("30");
        let results = idx.query(&range);
        assert_eq!(results.len(), 2); // "30" and "35"
    }

    #[test]
    fn test_query_object_max() {
        let idx = populated_index();
        let range = TripleRange::new()
            .with_predicate("p:age")
            .with_object_max("30");
        let results = idx.query(&range);
        assert_eq!(results.len(), 2); // "25" and "30"
    }

    #[test]
    fn test_query_object_range() {
        let idx = populated_index();
        let range = TripleRange::new()
            .with_predicate("p:age")
            .with_object_min("25")
            .with_object_max("30");
        let results = idx.query(&range);
        assert_eq!(results.len(), 2); // "25" and "30"
    }

    #[test]
    fn test_query_object_range_exclusive() {
        let idx = populated_index();
        let range = TripleRange::new()
            .with_predicate("p:age")
            .with_object_min("26")
            .with_object_max("34");
        let results = idx.query(&range);
        assert_eq!(results.len(), 1); // only "30"
    }

    // --- query: combined constraints ---
    #[test]
    fn test_query_subject_and_predicate() {
        let idx = populated_index();
        let range = TripleRange::new()
            .with_subject_prefix("s:alice")
            .with_predicate("p:age");
        let results = idx.query(&range);
        assert_eq!(results.len(), 1);
        assert_eq!(results[0], ("s:alice".to_string(), "p:age".to_string(), "30".to_string()));
    }

    // --- subjects_for_predicate ---
    #[test]
    fn test_subjects_for_predicate() {
        let idx = populated_index();
        let subs = idx.subjects_for_predicate("p:age");
        assert_eq!(subs.len(), 3);
        assert!(subs.contains(&"s:alice".to_string()));
        assert!(subs.contains(&"s:bob".to_string()));
        assert!(subs.contains(&"s:carol".to_string()));
    }

    #[test]
    fn test_subjects_for_predicate_empty() {
        let idx = populated_index();
        assert!(idx.subjects_for_predicate("p:unknown").is_empty());
    }

    // --- objects_for_predicate ---
    #[test]
    fn test_objects_for_predicate() {
        let idx = populated_index();
        let objs = idx.objects_for_predicate("p:name");
        assert_eq!(objs.len(), 3);
        assert!(objs.contains(&"Alice".to_string()));
        assert!(objs.contains(&"Bob".to_string()));
        assert!(objs.contains(&"Carol".to_string()));
    }

    #[test]
    fn test_objects_for_predicate_empty() {
        let idx = populated_index();
        assert!(idx.objects_for_predicate("p:missing").is_empty());
    }

    // --- predicates_for_subject ---
    #[test]
    fn test_predicates_for_subject() {
        let idx = populated_index();
        let preds = idx.predicates_for_subject("s:alice");
        assert_eq!(preds.len(), 2);
        assert!(preds.contains(&"p:age".to_string()));
        assert!(preds.contains(&"p:name".to_string()));
    }

    #[test]
    fn test_predicates_for_subject_empty() {
        let idx = populated_index();
        assert!(idx.predicates_for_subject("s:unknown").is_empty());
    }

    // --- triples_for_subject ---
    #[test]
    fn test_triples_for_subject() {
        let idx = populated_index();
        let trips = idx.triples_for_subject("s:bob");
        assert_eq!(trips.len(), 2);
        assert!(trips.iter().all(|(s, _, _)| s == "s:bob"));
    }

    #[test]
    fn test_triples_for_subject_empty() {
        let idx = populated_index();
        assert!(idx.triples_for_subject("s:nobody").is_empty());
    }

    // --- triples_for_predicate ---
    #[test]
    fn test_triples_for_predicate() {
        let idx = populated_index();
        let trips = idx.triples_for_predicate("p:score");
        assert_eq!(trips.len(), 1);
        assert_eq!(trips[0].0, "s:dave");
    }

    #[test]
    fn test_triples_for_predicate_multiple() {
        let idx = populated_index();
        let trips = idx.triples_for_predicate("p:age");
        assert_eq!(trips.len(), 3);
    }

    // --- empty index ---
    #[test]
    fn test_empty_index_query() {
        let idx = RangeIndex::new();
        assert!(idx.query(&TripleRange::new()).is_empty());
    }

    #[test]
    fn test_empty_index_count_zero() {
        assert_eq!(RangeIndex::new().count(), 0);
    }

    // --- TripleRange builder ---
    #[test]
    fn test_triple_range_default() {
        let r = TripleRange::new();
        assert!(r.subject_prefix.is_none());
        assert!(r.predicate_exact.is_none());
        assert!(r.object_min.is_none());
        assert!(r.object_max.is_none());
    }

    #[test]
    fn test_triple_range_builders() {
        let r = TripleRange::new()
            .with_subject_prefix("s:")
            .with_predicate("p:")
            .with_object_min("a")
            .with_object_max("z");
        assert_eq!(r.subject_prefix.as_deref(), Some("s:"));
        assert_eq!(r.predicate_exact.as_deref(), Some("p:"));
        assert_eq!(r.object_min.as_deref(), Some("a"));
        assert_eq!(r.object_max.as_deref(), Some("z"));
    }

    // --- remove consistency ---
    #[test]
    fn test_remove_updates_all_indexes() {
        let mut idx = RangeIndex::new();
        idx.insert("s", "p:age", "30");
        idx.remove("s", "p:age", "30");
        assert!(idx.subjects_for_predicate("p:age").is_empty());
        assert!(idx.objects_for_predicate("p:age").is_empty());
        assert!(idx.triples_for_subject("s").is_empty());
    }
}