Skip to main content

qualia_core_db/query/graph_accel/
segment.rs

1//! Load one Q42 volume (or volume-set child) and sieve a field. GPU when
2//! eligible; CPU floor always.
3
4use crate::query::graph_accel::sieve::{sieve_eq, SieveOutcome};
5use crate::query::graph_accel::QuinField;
6use crate::NQuin;
7
8/// Read all Quins from `path` (single-file volume) into `scratch`, then compact
9/// matches into `out`. Fails closed if the volume is larger than `scratch`.
10pub fn sieve_volume_file(
11    path: &std::path::Path,
12    field: QuinField,
13    needle: u64,
14    scratch: &mut [NQuin],
15    out: &mut [NQuin],
16) -> Result<(SieveOutcome, usize), String> {
17    let vol = crate::q42_volume::Q42Volume::open(path).map_err(|e| e.to_string())?;
18    let loaded = vol.read_all_quins().map_err(|e| e.to_string())?;
19    if loaded.len() > scratch.len() {
20        return Err(format!(
21            "volume has more Quins than the caller scratch ({} > {})",
22            loaded.len(),
23            scratch.len()
24        ));
25    }
26    let n = loaded.len();
27    scratch[..n].copy_from_slice(&loaded);
28    let outcome = sieve_eq(&scratch[..n], field, needle, out);
29    Ok((outcome, n))
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35    use crate::query::ingest::{streaming_import_rdf_with_mode, IngestMode};
36
37    #[test]
38    fn sieves_ingested_ntriples() {
39        let dir = tempfile::TempDir::new().unwrap();
40        let input = dir.path().join("in.nt");
41        let output = dir.path().join("out.q42");
42        std::fs::write(
43            &input,
44            "<http://ex/s1> <http://ex/p> <http://ex/keep> .\n\
45             <http://ex/s2> <http://ex/p> <http://ex/drop> .\n\
46             <http://ex/s3> <http://ex/p> <http://ex/keep> .\n",
47        )
48        .unwrap();
49        streaming_import_rdf_with_mode(
50            input.to_str().unwrap(),
51            output.to_str().unwrap(),
52            IngestMode::Complete,
53        )
54        .unwrap();
55        let needle = crate::query::ingest_formats::object_iri_hash("http://ex/keep");
56        let mut scratch = vec![NQuin::default(); 16];
57        let mut out = vec![NQuin::default(); 16];
58        let (hit, scanned) =
59            sieve_volume_file(&output, QuinField::Object, needle, &mut scratch, &mut out).unwrap();
60        assert_eq!(scanned, 3);
61        assert_eq!(hit.written, 2);
62    }
63}