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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
//! Indexing, bulk insertion, optimization, and supporting infrastructure.
//!
//! This sibling module of `crate::store` contains the maintenance side of
//! [`StarStore`]: index construction, bulk-insert pipelines, cache integration,
//! detailed statistics, memory-mapped storage hooks, and the connection-pool
//! factory.
use std::collections::BTreeSet;
use std::thread;
use std::time::Instant;
use tracing::{debug, info, span, Level};
use crate::model::{StarTerm, StarTriple};
use crate::store::index::{IndexStatistics, QuotedTripleIndex};
use crate::store::{bulk_insert_mod, cache_mod, pool_mod, StarStore};
use crate::{StarError, StarResult, StarStatistics};
use bulk_insert_mod::BulkInsertConfig;
use cache_mod::CacheStatistics;
use pool_mod::ConnectionPool;
impl StarStore {
/// Build index entries for quoted triples in a given triple using B-tree indices
pub(crate) fn index_quoted_triples(
&self,
triple: &StarTriple,
triple_index: usize,
index: &mut QuotedTripleIndex,
) {
self.index_quoted_triples_recursive(triple, triple_index, index);
// Index by nesting depth for performance optimization
let depth = triple.nesting_depth();
index
.nesting_depth_index
.entry(depth)
.or_default()
.insert(triple_index);
}
/// Recursively index quoted triples with multi-dimensional indexing
pub(crate) fn index_quoted_triples_recursive(
&self,
triple: &StarTriple,
triple_index: usize,
index: &mut QuotedTripleIndex,
) {
// Index quoted triples in subject
if let StarTerm::QuotedTriple(qt) = &triple.subject {
let signature = self.quoted_triple_key(qt);
index
.signature_to_indices
.entry(signature)
.or_default()
.insert(triple_index);
// Index by subject signature for S?? queries
let subject_key = format!("SUBJ:{}", qt.subject);
index
.subject_index
.entry(subject_key)
.or_default()
.insert(triple_index);
// Recursively index nested quoted triples
self.index_quoted_triples_recursive(qt, triple_index, index);
}
// Index quoted triples in predicate (rare but possible in some extensions)
if let StarTerm::QuotedTriple(qt) = &triple.predicate {
let signature = self.quoted_triple_key(qt);
index
.signature_to_indices
.entry(signature)
.or_default()
.insert(triple_index);
// Index by predicate signature for ?P? queries
let predicate_key = format!("PRED:{}", qt.predicate);
index
.predicate_index
.entry(predicate_key)
.or_default()
.insert(triple_index);
// ALSO index the subject and object of the quoted triple found in predicate position
let qt_subject_key = format!("SUBJ:{}", qt.subject);
index
.subject_index
.entry(qt_subject_key)
.or_default()
.insert(triple_index);
let qt_object_key = format!("OBJ:{}", qt.object);
index
.object_index
.entry(qt_object_key)
.or_default()
.insert(triple_index);
// Recursively index nested quoted triples
self.index_quoted_triples_recursive(qt, triple_index, index);
}
// Index quoted triples in object
if let StarTerm::QuotedTriple(qt) = &triple.object {
let signature = self.quoted_triple_key(qt);
index
.signature_to_indices
.entry(signature)
.or_default()
.insert(triple_index);
// Index by object signature for ??O queries
let object_key = format!("OBJ:{}", qt.object);
index
.object_index
.entry(object_key)
.or_default()
.insert(triple_index);
// ALSO index the subject and predicate of the quoted triple found in object position
// This allows finding triples like "bob believes <<alice age 25>>" when searching for alice
let qt_subject_key = format!("SUBJ:{}", qt.subject);
index
.subject_index
.entry(qt_subject_key)
.or_default()
.insert(triple_index);
let qt_predicate_key = format!("PRED:{}", qt.predicate);
index
.predicate_index
.entry(qt_predicate_key)
.or_default()
.insert(triple_index);
// Recursively index nested quoted triples
self.index_quoted_triples_recursive(qt, triple_index, index);
}
}
/// Generate a key for indexing quoted triples
pub(crate) fn quoted_triple_key(&self, triple: &StarTriple) -> String {
format!("{}|{}|{}", triple.subject, triple.predicate, triple.object)
}
/// Update indices after removing an item at position `pos`
/// This efficiently updates all indices > pos by decrementing them
pub(crate) fn update_indices_after_removal(indices: &mut BTreeSet<usize>, pos: usize) {
// Remove the item at pos
indices.remove(&pos);
// Create a new set with updated indices
let updated: BTreeSet<usize> = indices
.iter()
.map(|&idx| if idx > pos { idx - 1 } else { idx })
.collect();
// Replace the old set with the updated one
*indices = updated;
}
/// Count quoted triples within a single triple
#[allow(clippy::only_used_in_recursion)]
pub(crate) fn count_quoted_triples_in_triple(&self, triple: &StarTriple) -> usize {
let mut count = 0;
if triple.subject.is_quoted_triple() {
count += 1;
if let StarTerm::QuotedTriple(qt) = &triple.subject {
count += self.count_quoted_triples_in_triple(qt);
}
}
if triple.predicate.is_quoted_triple() {
count += 1;
if let StarTerm::QuotedTriple(qt) = &triple.predicate {
count += self.count_quoted_triples_in_triple(qt);
}
}
if triple.object.is_quoted_triple() {
count += 1;
if let StarTerm::QuotedTriple(qt) = &triple.object {
count += self.count_quoted_triples_in_triple(qt);
}
}
count
}
/// Optimize the store by rebuilding indices
pub fn optimize(&self) -> StarResult<()> {
let span = span!(Level::INFO, "optimize_store");
let _enter = span.enter();
let star_triples = self.star_triples.read().unwrap_or_else(|e| e.into_inner());
let mut index = self
.quoted_triple_index
.write()
.unwrap_or_else(|e| e.into_inner());
// Rebuild the quoted triple index with all new B-tree structures
index.clear();
for (i, triple) in star_triples.iter().enumerate() {
if triple.contains_quoted_triples() {
self.index_quoted_triples(triple, i, &mut index);
}
}
// Compact the indices by removing empty entries
index
.signature_to_indices
.retain(|_, indices| !indices.is_empty());
index.subject_index.retain(|_, indices| !indices.is_empty());
index
.predicate_index
.retain(|_, indices| !indices.is_empty());
index.object_index.retain(|_, indices| !indices.is_empty());
index
.nesting_depth_index
.retain(|_, indices| !indices.is_empty());
info!(
"Store optimization completed - rebuilt {} index entries",
index.signature_to_indices.len()
+ index.subject_index.len()
+ index.predicate_index.len()
+ index.object_index.len()
+ index.nesting_depth_index.len()
);
Ok(())
}
/// Bulk insert triples with optimized performance
pub fn bulk_insert(&self, triples: &[StarTriple], config: &BulkInsertConfig) -> StarResult<()> {
let span = span!(Level::INFO, "bulk_insert", count = triples.len());
let _enter = span.enter();
info!("Starting bulk insertion of {} triples", triples.len());
let start_time = Instant::now();
// Enable bulk mode
{
let mut bulk_state = self
.bulk_insert_state
.write()
.unwrap_or_else(|e| e.into_inner());
bulk_state.active = true;
bulk_state.pending_triples.clear();
bulk_state.current_memory_usage = 0;
bulk_state.batch_count = 0;
}
if config.parallel_processing && triples.len() > config.batch_size {
self.bulk_insert_parallel(triples, config)?;
} else {
self.bulk_insert_sequential(triples, config)?;
}
// Finalize bulk insertion
self.finalize_bulk_insert(config)?;
let elapsed = start_time.elapsed();
info!(
"Bulk insertion completed in {:?} for {} triples",
elapsed,
triples.len()
);
// Update statistics
{
let mut stats = self.statistics.write().unwrap_or_else(|e| e.into_inner());
stats.processing_time_us += elapsed.as_micros() as u64;
}
Ok(())
}
/// Sequential bulk insertion implementation
pub(crate) fn bulk_insert_sequential(
&self,
triples: &[StarTriple],
config: &BulkInsertConfig,
) -> StarResult<()> {
for batch in triples.chunks(config.batch_size) {
for triple in batch {
// Validate the triple
triple.validate()?;
// Insert based on triple type
if triple.contains_quoted_triples() {
if config.defer_index_updates {
// Add to pending list for later indexing
let mut bulk_state = self
.bulk_insert_state
.write()
.unwrap_or_else(|e| e.into_inner());
bulk_state.pending_triples.push(triple.clone());
bulk_state.current_memory_usage += self.estimate_triple_memory_size(triple);
} else {
self.insert_star_triple(triple)?;
}
} else {
self.insert_regular_triple(triple)?;
}
}
// Check memory threshold
{
let bulk_state = self
.bulk_insert_state
.read()
.unwrap_or_else(|e| e.into_inner());
if bulk_state.current_memory_usage >= config.memory_threshold {
drop(bulk_state);
self.flush_pending_triples(config)?;
}
}
// Update batch count
{
let mut bulk_state = self
.bulk_insert_state
.write()
.unwrap_or_else(|e| e.into_inner());
bulk_state.batch_count += 1;
}
}
Ok(())
}
/// Parallel bulk insertion implementation
pub(crate) fn bulk_insert_parallel(
&self,
triples: &[StarTriple],
config: &BulkInsertConfig,
) -> StarResult<()> {
// Guard against config.worker_threads > triples.len() (or 0 worker
// threads), which would otherwise compute a chunk_size of 0 and make
// `.chunks(0)` panic.
let worker_threads = config.worker_threads.max(1);
let chunk_size = ((triples.len() + worker_threads - 1) / worker_threads).max(1);
let mut handles = Vec::new();
for chunk in triples.chunks(chunk_size) {
let chunk = chunk.to_vec();
let store_clone = self.clone();
let config_clone = config.clone();
let handle =
thread::spawn(move || store_clone.bulk_insert_sequential(&chunk, &config_clone));
handles.push(handle);
}
// Wait for all threads to complete
for handle in handles {
handle
.join()
.map_err(|e| StarError::query_error(format!("Thread join error: {e:?}")))??;
}
Ok(())
}
/// Flush pending triples and rebuild indices
pub(crate) fn flush_pending_triples(&self, config: &BulkInsertConfig) -> StarResult<()> {
let pending_triples = {
let mut bulk_state = self
.bulk_insert_state
.write()
.unwrap_or_else(|e| e.into_inner());
let triples = bulk_state.pending_triples.clone();
bulk_state.pending_triples.clear();
bulk_state.current_memory_usage = 0;
triples
};
if !pending_triples.is_empty() {
debug!("Flushing {} pending triples", pending_triples.len());
// Insert all pending triples into storage
{
let mut star_triples = self.star_triples.write().unwrap_or_else(|e| e.into_inner());
let base_index = star_triples.len();
star_triples.extend(pending_triples.clone());
// Build indices for the new triples
if !config.defer_index_updates {
let mut index = self
.quoted_triple_index
.write()
.unwrap_or_else(|e| e.into_inner());
for (i, triple) in pending_triples.iter().enumerate() {
self.index_quoted_triples(triple, base_index + i, &mut index);
}
}
}
}
Ok(())
}
/// Finalize bulk insertion by rebuilding indices if needed
pub(crate) fn finalize_bulk_insert(&self, config: &BulkInsertConfig) -> StarResult<()> {
// Flush any remaining pending triples
self.flush_pending_triples(config)?;
// Rebuild indices if they were deferred
if config.defer_index_updates {
info!("Rebuilding indices after bulk insertion");
self.optimize()?;
}
// Reset bulk state
{
let mut bulk_state = self
.bulk_insert_state
.write()
.unwrap_or_else(|e| e.into_inner());
bulk_state.active = false;
bulk_state.pending_triples.clear();
bulk_state.current_memory_usage = 0;
bulk_state.batch_count = 0;
}
Ok(())
}
/// Estimate memory size of a triple for memory tracking
pub(crate) fn estimate_triple_memory_size(&self, triple: &StarTriple) -> usize {
// Rough estimation based on string lengths and structure
let subject_size = match &triple.subject {
StarTerm::NamedNode(nn) => nn.iri.len(),
StarTerm::BlankNode(bn) => bn.id.len(),
StarTerm::Literal(lit) => lit.value.len(),
StarTerm::QuotedTriple(_) => 200, // Estimated overhead
StarTerm::Variable(var) => var.name.len(),
};
let predicate_size = match &triple.predicate {
StarTerm::NamedNode(nn) => nn.iri.len(),
_ => 50, // Default estimate
};
let object_size = match &triple.object {
StarTerm::NamedNode(nn) => nn.iri.len(),
StarTerm::BlankNode(bn) => bn.id.len(),
StarTerm::Literal(lit) => lit.value.len(),
StarTerm::QuotedTriple(_) => 200, // Estimated overhead
StarTerm::Variable(var) => var.name.len(),
};
subject_size + predicate_size + object_size + 100 // Base overhead
}
/// Enable memory-mapped storage.
///
/// `StarStore` keeps its triples in an in-process `Vec<StarTriple>` plus
/// B-tree indices; there is currently no disk-backed representation to
/// map into memory, so this cannot honestly reduce RAM usage for large
/// datasets. Rather than flip an `enabled` flag while silently keeping
/// everything fully resident in memory (misleading for memory-constrained
/// deployments), this fails loudly until real mmap-backed storage is
/// implemented.
///
/// For genuine disk-backed / memory-mapped storage today, use
/// [`crate::storage_integration::StarStorageBackend::memory_mapped`],
/// which constructs a separate backend with real on-disk persistence
/// hooks, instead of retrofitting an existing in-memory `StarStore`.
pub fn enable_memory_mapping(
&self,
file_path: &str,
_enable_compression: bool,
) -> StarResult<()> {
let span = span!(Level::INFO, "enable_memory_mapping");
let _enter = span.enter();
Err(StarError::configuration_error(format!(
"Memory-mapped storage is not implemented for StarStore (requested path: \
'{file_path}'): triples are always held fully in memory. Use \
storage_integration::StarStorageBackend::memory_mapped for a disk-backed \
backend instead of enable_memory_mapping()."
)))
}
/// Get optimized triples using cache
pub fn get_triples_cached(&self, pattern: &str) -> Vec<StarTriple> {
let span = span!(Level::DEBUG, "get_triples_cached");
let _enter = span.enter();
// Check cache first
if let Some(cached_results) = self.cache.get(pattern) {
debug!("Cache hit for pattern: {}", pattern);
return cached_results;
}
// Cache miss - compute results
debug!("Cache miss for pattern: {}", pattern);
let results = self.compute_pattern_results(pattern);
// Store in cache
self.cache.put(pattern.to_string(), results.clone());
results
}
/// Compute pattern results by parsing `pattern` into subject/predicate/
/// object components and delegating to the store's (indexed) [`query`](Self::query).
///
/// Supported syntax: three whitespace-separated terms
/// `SUBJECT PREDICATE OBJECT`, where each term is either `?` (wildcard,
/// matches anything) or an N-Triples-style term:
/// - `<http://example.org/iri>` for an IRI
/// - `_:label` for a blank node
/// - `"value"`, `"value"@lang`, or `"value"^^<datatype-iri>` for a literal
///
/// The bare keyword `quoted` is retained for backward compatibility and
/// returns all triples containing at least one quoted triple. Patterns
/// that cannot be parsed fall back to a full scan (logged at debug
/// level) rather than silently returning the wrong result set.
pub(crate) fn compute_pattern_results(&self, pattern: &str) -> Vec<StarTriple> {
let trimmed = pattern.trim();
if trimmed == "quoted" {
return self.find_triples_by_nesting_depth(1, None);
}
match Self::parse_triple_pattern(trimmed) {
Some((subject, predicate, object)) => self
.query(subject.as_ref(), predicate.as_ref(), object.as_ref())
.unwrap_or_default(),
None => {
debug!(
"Could not parse triple pattern '{}'; falling back to full scan",
pattern
);
self.triples()
}
}
}
/// Parse a `"SUBJECT PREDICATE OBJECT"` pattern string into optional
/// [`StarTerm`] filters (`None` = wildcard `?`). Returns `None` if the
/// pattern does not tokenize into exactly three well-formed terms.
fn parse_triple_pattern(
pattern: &str,
) -> Option<(Option<StarTerm>, Option<StarTerm>, Option<StarTerm>)> {
let tokens = Self::tokenize_pattern(pattern)?;
if tokens.len() != 3 {
return None;
}
let subject = Self::parse_pattern_term(&tokens[0])?;
let predicate = Self::parse_pattern_term(&tokens[1])?;
let object = Self::parse_pattern_term(&tokens[2])?;
Some((subject, predicate, object))
}
/// Split a pattern string into whitespace-separated terms, treating
/// `<...>` IRIs and `"..."` literals (with optional `@lang`/`^^<dt>`
/// suffix) as atomic tokens even if they cannot contain whitespace
/// themselves (they never legally do for IRIs/simple literals here).
fn tokenize_pattern(pattern: &str) -> Option<Vec<String>> {
let chars: Vec<char> = pattern.chars().collect();
let mut tokens = Vec::new();
let mut i = 0;
while i < chars.len() {
while i < chars.len() && chars[i].is_whitespace() {
i += 1;
}
if i >= chars.len() {
break;
}
let start = i;
match chars[i] {
'<' => {
while i < chars.len() && chars[i] != '>' {
i += 1;
}
if i >= chars.len() {
return None; // Unterminated IRI
}
i += 1; // include '>'
}
'"' => {
i += 1;
while i < chars.len() && chars[i] != '"' {
if chars[i] == '\\' && i + 1 < chars.len() {
i += 1;
}
i += 1;
}
if i >= chars.len() {
return None; // Unterminated literal
}
i += 1; // include closing quote
if i < chars.len() && chars[i] == '@' {
i += 1;
while i < chars.len() && !chars[i].is_whitespace() {
i += 1;
}
} else if i + 1 < chars.len() && chars[i] == '^' && chars[i + 1] == '^' {
i += 2;
if i < chars.len() && chars[i] == '<' {
while i < chars.len() && chars[i] != '>' {
i += 1;
}
if i >= chars.len() {
return None; // Unterminated datatype IRI
}
i += 1;
}
}
}
_ => {
while i < chars.len() && !chars[i].is_whitespace() {
i += 1;
}
}
}
tokens.push(chars[start..i].iter().collect());
}
Some(tokens)
}
/// Parse a single pattern token into an optional [`StarTerm`] filter.
/// Returns `Some(None)` for the wildcard `?`, `Some(Some(term))` for a
/// successfully parsed term, and `None` if the token is malformed.
fn parse_pattern_term(token: &str) -> Option<Option<StarTerm>> {
if token == "?" {
return Some(None);
}
if let Some(iri) = token.strip_prefix('<').and_then(|s| s.strip_suffix('>')) {
return StarTerm::iri(iri).ok().map(Some);
}
if let Some(label) = token.strip_prefix("_:") {
return StarTerm::blank_node(label).ok().map(Some);
}
if token.starts_with('"') {
return Self::parse_literal_token(token).map(Some);
}
None
}
/// Parse a literal token (`"value"`, `"value"@lang`, or
/// `"value"^^<datatype-iri>`) into a [`StarTerm::Literal`].
fn parse_literal_token(token: &str) -> Option<StarTerm> {
let chars: Vec<char> = token.chars().collect();
if chars.first() != Some(&'"') {
return None;
}
let mut i = 1;
while i < chars.len() && chars[i] != '"' {
if chars[i] == '\\' && i + 1 < chars.len() {
i += 1;
}
i += 1;
}
if i >= chars.len() {
return None;
}
let value: String = chars[1..i].iter().collect();
let rest: String = chars[i + 1..].iter().collect();
if let Some(lang) = rest.strip_prefix('@') {
return StarTerm::literal_with_language(&value, lang).ok();
}
if let Some(dt) = rest.strip_prefix("^^") {
let dt_iri = dt.strip_prefix('<').and_then(|s| s.strip_suffix('>'))?;
return StarTerm::literal_with_datatype(&value, dt_iri).ok();
}
if rest.is_empty() {
return StarTerm::literal(&value).ok();
}
None
}
/// Get comprehensive storage statistics
pub fn get_detailed_statistics(&self) -> DetailedStorageStatistics {
let base_stats = self.statistics();
let cache_stats = self.cache.get_statistics();
let index_stats = {
let index = self
.quoted_triple_index
.read()
.unwrap_or_else(|e| e.into_inner());
index.get_statistics()
};
let bulk_state = self
.bulk_insert_state
.read()
.unwrap_or_else(|e| e.into_inner());
let mm_state = self.memory_mapped.read().unwrap_or_else(|e| e.into_inner());
DetailedStorageStatistics {
basic_stats: base_stats,
cache_stats,
index_stats,
bulk_insert_active: bulk_state.active,
bulk_pending_count: bulk_state.pending_triples.len(),
bulk_memory_usage: bulk_state.current_memory_usage,
memory_mapped_enabled: mm_state.enabled,
memory_mapped_path: mm_state.file_path.clone(),
}
}
/// Create a connection pool for this store type
pub fn create_connection_pool(
max_connections: usize,
config: crate::StarConfig,
) -> ConnectionPool {
ConnectionPool::new(max_connections, config)
}
/// Compress the store's serialized data and report the real number of
/// bytes saved.
///
/// This serializes the current triple set with `oxicode` (per the
/// COOLJAPAN no-bincode policy) and compresses it with `oxiarc-zstd`
/// (per the COOLJAPAN compression policy), returning the actual
/// measured difference between the uncompressed and compressed byte
/// lengths rather than a fabricated estimate. This does not persist the
/// compressed bytes anywhere; it is a storage-savings measurement, not a
/// durable compaction operation.
pub fn compress_storage(&self) -> StarResult<usize> {
let span = span!(Level::INFO, "compress_storage");
let _enter = span.enter();
let triples = self.triples();
let serialized = oxicode::serde::encode_to_vec(&triples, oxicode::config::standard())
.map_err(|e| {
StarError::query_error(format!("Failed to serialize triples for compression: {e}"))
})?;
let compressed = oxiarc_zstd::encode_all(&serialized, 3).map_err(|e| {
StarError::query_error(format!("Failed to compress serialized triples: {e}"))
})?;
let space_saved = serialized.len().saturating_sub(compressed.len());
info!(
"Compressed storage for {} triples: {} bytes -> {} bytes ({} bytes saved)",
triples.len(),
serialized.len(),
compressed.len(),
space_saved
);
Ok(space_saved)
}
}
/// Comprehensive storage statistics including optimizations
#[derive(Debug, Clone)]
pub struct DetailedStorageStatistics {
pub basic_stats: StarStatistics,
pub cache_stats: CacheStatistics,
pub index_stats: IndexStatistics,
pub bulk_insert_active: bool,
pub bulk_pending_count: usize,
pub bulk_memory_usage: usize,
pub memory_mapped_enabled: bool,
pub memory_mapped_path: Option<String>,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::{StarTerm, StarTriple};
use crate::store::StarStore;
use crate::StarResult;
/// Regression test: bulk_insert_parallel must not panic on
/// `.chunks(0)` when worker_threads exceeds the number of triples (or
/// is otherwise larger than what would produce a non-zero chunk size).
#[test]
fn test_bulk_insert_parallel_does_not_panic_with_few_triples() -> StarResult<()> {
let store = StarStore::new();
let triples: Vec<StarTriple> = (0..3)
.map(|i| {
StarTriple::new(
StarTerm::iri(&format!("http://example.org/s{i}")).unwrap(),
StarTerm::iri("http://example.org/p").unwrap(),
StarTerm::literal(&format!("o{i}")).unwrap(),
)
})
.collect();
let config = BulkInsertConfig {
batch_size: 1,
defer_index_updates: false,
memory_threshold: usize::MAX,
parallel_processing: true,
// More worker threads than triples: chunk_size used to compute
// to 3 / 16 == 0, which made `.chunks(0)` panic.
worker_threads: 16,
};
store.bulk_insert_parallel(&triples, &config)?;
assert_eq!(store.len(), 3);
Ok(())
}
#[test]
fn test_compute_pattern_results_parses_bound_pattern() -> StarResult<()> {
let store = StarStore::new();
let alice = StarTerm::iri("http://example.org/alice")?;
let knows = StarTerm::iri("http://example.org/knows")?;
let bob = StarTerm::iri("http://example.org/bob")?;
let matching = StarTriple::new(alice.clone(), knows.clone(), bob.clone());
let non_matching = StarTriple::new(
bob,
StarTerm::iri("http://example.org/likes")?,
alice.clone(),
);
store.insert(&matching)?;
store.insert(&non_matching)?;
// Bound subject+predicate, wildcard object: only `matching` triple.
let pattern = "<http://example.org/alice> <http://example.org/knows> ?";
let results = store.compute_pattern_results(pattern);
assert_eq!(results, vec![matching]);
Ok(())
}
#[test]
fn test_compute_pattern_results_quoted_keyword_backward_compat() -> StarResult<()> {
let store = StarStore::new();
let inner = StarTriple::new(
StarTerm::iri("http://example.org/s1")?,
StarTerm::iri("http://example.org/p1")?,
StarTerm::literal("o1")?,
);
let quoted_triple = StarTriple::new(
StarTerm::quoted_triple(inner),
StarTerm::iri("http://example.org/certainty")?,
StarTerm::literal("0.9")?,
);
store.insert("ed_triple)?;
let results = store.compute_pattern_results("quoted");
assert_eq!(results, vec![quoted_triple]);
Ok(())
}
#[test]
fn test_compute_pattern_results_unparseable_falls_back_to_full_scan() -> StarResult<()> {
let store = StarStore::new();
let triple = StarTriple::new(
StarTerm::iri("http://example.org/s")?,
StarTerm::iri("http://example.org/p")?,
StarTerm::literal("o")?,
);
store.insert(&triple)?;
// Malformed pattern (unterminated IRI): falls back to a full scan
// instead of silently returning an empty / wrong result set.
let results = store.compute_pattern_results("<http://example.org/s ? ?");
assert_eq!(results, vec![triple]);
Ok(())
}
#[test]
fn test_compress_storage_reports_real_measurement() -> StarResult<()> {
let store = StarStore::new();
for i in 0..20 {
store.insert(&StarTriple::new(
StarTerm::iri(&format!("http://example.org/s{i}"))?,
StarTerm::iri("http://example.org/p")?,
StarTerm::literal("a moderately repetitive literal value for compression")?,
))?;
}
// Highly repetitive data should compress to a non-trivial number of
// saved bytes (previously this was always `triple_count * 50`
// regardless of actual content).
let space_saved = store.compress_storage()?;
assert!(space_saved > 0);
Ok(())
}
#[test]
fn test_enable_memory_mapping_fails_loudly() {
let store = StarStore::new();
let result = store.enable_memory_mapping("/tmp/does-not-matter", false);
assert!(result.is_err());
}
}