Skip to main content

qualia_core_db/query/
ingestion.rs

1use crate::{query_compiler::QueryCompiler, NQuin};
2
3/// A zero-allocation stream processor trait that iterates over lines or chunks
4/// of bytes/strings without allocating to the heap.
5pub trait ZeroCopyStream<'a> {
6    type Item;
7    fn stream_parse(&self) -> impl Iterator<Item = Self::Item> + 'a;
8}
9
10/// The Zero-Allocation Ingestion Pipeline.
11/// Designed for extremely high-throughput ingestion of N-Triples*, N-Quads*,
12/// JSON-LD*, and N3Logic direct to hardware Quins.
13pub struct IngestionPipeline<'a> {
14    payload: &'a str,
15}
16
17impl<'a> IngestionPipeline<'a> {
18    pub fn new(payload: &'a str) -> Self {
19        Self { payload }
20    }
21
22    /// Parses a single line natively, determining hardware routing based on the syntax.
23    pub fn parse_line(line: &str) -> Option<NQuin> {
24        let trimmed = line.trim();
25        if trimmed.is_empty() || trimmed.starts_with('#') {
26            return None; // Skip empty lines and comments
27        }
28
29        let mut metadata: u64 = 0;
30
31        // 1. N3Logic Detection -> Route to Core 1 (Prolog Webizen)
32        if trimmed.contains("=>") || trimmed.contains("@forAll") || trimmed.contains("@forSome") {
33            // N3Logic Implication / Quantification requires deep inference
34            // Routing Tier: 0b10 (Bits 61-62)
35            metadata |= 0b10 << 61;
36            // Embedded N3Logic identifier payload (example mask)
37            metadata |= 0x00A3;
38        }
39        // 2. RDF-Star Nesting (<< >>) -> Route based on complexity
40        else if trimmed.contains("<<") && trimmed.contains(">>") {
41            if trimmed.contains("qualia:guardian") || trimmed.contains("SPIN:") {
42                // Nested identity logic -> Core 1
43                metadata |= 0b10 << 61;
44            } else {
45                // Standard structural nesting -> Core 2 (SIMD)
46                // Routing Tier: 0b00 (Bits 61-62)
47                metadata |= 0b00 << 61;
48            }
49        }
50        // 3. Fallback to the QueryCompiler logic gates for other semantics
51        else if let Some(quin) = QueryCompiler::compile_to_quin(trimmed) {
52            return Some(quin);
53        } else {
54            // Standard Passthrough
55            metadata |= 0b00 << 61;
56        }
57
58        Some(NQuin {
59            subject: 0,
60            predicate: 0,
61            object: 0,
62            context: 0,
63            metadata,
64            parity: 0,
65        })
66    }
67}
68
69impl<'a> ZeroCopyStream<'a> for IngestionPipeline<'a> {
70    type Item = NQuin;
71
72    /// Iterates over the string payload line-by-line, compiling immediately.
73    fn stream_parse(&self) -> impl Iterator<Item = Self::Item> + 'a {
74        self.payload
75            .lines()
76            .filter_map(|line| Self::parse_line(line))
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn test_n3logic_routing() {
86        let n3_rule = "{ ?x a :Man } => { ?x a :Mortal } .";
87        let quin = IngestionPipeline::parse_line(n3_rule).unwrap();
88
89        // Expected route: 0b10 (Core 1 Prolog Webizen)
90        let expected_routing = 0b10 << 61;
91        assert_eq!(
92            quin.metadata & (0b11 << 61),
93            expected_routing,
94            "N3Logic implication failed to route to Core 1"
95        );
96    }
97
98    #[test]
99    fn test_rdf_star_nesting() {
100        let nested_triple = "<< :AgentX :prescribed :MedicationY >> :assertedBy :DoctorZ .";
101        let quin = IngestionPipeline::parse_line(nested_triple).unwrap();
102
103        // Expected route: 0b00 (Core 2 SIMD matcher) for structural nesting
104        let expected_routing = 0b00 << 61;
105        assert_eq!(
106            quin.metadata & (0b11 << 61),
107            expected_routing,
108            "RDF-Star nesting failed to route correctly"
109        );
110    }
111}