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