blazegraph-io-core 0.1.2

Core library for semantic document graph processing — parse PDFs into structured, queryable graphs
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
use crate::cache::{self, GraphCacheKey};
use crate::classifier::DocumentClassifier;
use crate::config::ParsingConfig;
use crate::graphs::builder::GraphBuilder;
use crate::graphs::NodeIdGenerator;
use crate::preprocessors::{Preprocessor, TikaPreprocessor};
use crate::rules::RuleEngine;
use crate::storage::{
    calculate_config_hash, calculate_pdf_hash, CacheDefaults, CachePoint, DocumentStorage,
    FileStorage, FreshFrom,
};
use crate::types::*;
use anyhow::Result;
use std::path::Path;
use std::time::{Duration, Instant};

/// Captured intermediate outputs from each pipeline stage
/// Used for testing and diagnostics — lets you inspect/compare each boundary
#[derive(Debug, Clone, serde::Serialize)]
pub struct PipelineStages {
    pub xhtml: String,
    pub text_elements: Vec<PdfTextElement>,
    pub parsed_elements: Vec<ParsedPdfElement>,
    pub graph: DocumentGraph,
}

/// Simple profiler that collects timings for pipeline steps
pub struct StepProfiler {
    enabled: bool,
    timings: Vec<(String, Duration)>,
}

impl StepProfiler {
    pub fn new(enabled: bool) -> Self {
        Self {
            enabled,
            timings: Vec::new(),
        }
    }

    pub fn time_step<F, R>(&mut self, step_name: &str, f: F) -> R
    where
        F: FnOnce() -> R,
    {
        if !self.enabled {
            return f();
        }

        let start = Instant::now();
        let result = f();
        let elapsed = start.elapsed();

        self.timings.push((step_name.to_string(), elapsed));
        println!("⏱️  {}: {:.0}ms", step_name, elapsed.as_millis());

        result
    }

    pub fn print_summary(&self) {
        if !self.enabled || self.timings.is_empty() {
            return;
        }

        println!("\n📊 Performance Summary:");
        let total: Duration = self.timings.iter().map(|(_, d)| *d).sum();

        for (step, duration) in &self.timings {
            let percentage = (duration.as_secs_f64() / total.as_secs_f64()) * 100.0;
            println!(
                "   {:.<35} {:.0}ms ({:.1}%)",
                step,
                duration.as_millis(),
                percentage
            );
        }
        println!("   {:.<35} {:.0}ms", "Total", total.as_millis());
    }
}

pub struct DocumentProcessor {
    preprocessor: Box<dyn Preprocessor>,
    storage: Box<dyn DocumentStorage + Send + Sync>,
    classifier: DocumentClassifier,
    rule_engine: RuleEngine,
    graph_builder: GraphBuilder,
}

impl DocumentProcessor {
    /// Create DocumentProcessor with full dependency injection
    pub fn new_with_dependencies(
        preprocessor: Box<dyn Preprocessor>,
        storage: Box<dyn DocumentStorage + Send + Sync>,
    ) -> Result<Self> {
        Ok(Self {
            preprocessor,
            storage,
            classifier: DocumentClassifier::new(),
            rule_engine: RuleEngine::new()?,
            graph_builder: GraphBuilder::new(),
        })
    }

    /// Convenience constructor for CLI usage with JNI backend (cross-platform)
    #[cfg(feature = "jni-backend")]
    pub fn new_cli_jni(jre_path: &std::path::Path, jar_path: &std::path::Path) -> Result<Self> {
        let preprocessor = Box::new(TikaPreprocessor::new_with_jni(jre_path, jar_path)?);
        let storage = Box::new(FileStorage::new("cache")?);
        Self::new_with_dependencies(preprocessor, storage)
    }

    /// Convenience constructor for CLI with JNI backend and custom cache directory
    #[cfg(feature = "jni-backend")]
    pub fn new_cli_jni_with_cache(
        jre_path: &std::path::Path,
        jar_path: &std::path::Path,
        cache_dir: &str,
    ) -> Result<Self> {
        let preprocessor = Box::new(TikaPreprocessor::new_with_jni(jre_path, jar_path)?);
        let storage = Box::new(FileStorage::new(cache_dir)?);
        Self::new_with_dependencies(preprocessor, storage)
    }

    // =========================================================================
    // Main entry points
    // =========================================================================

    /// Process document with cache point awareness (CR-11).
    /// This is the primary entry point for CLI usage.
    pub fn process_document_with_cache(
        &mut self,
        input_path: &str,
        config: &ParsingConfig,
        fresh_from: FreshFrom,
        cache_defaults: &CacheDefaults,
        enable_profiling: bool,
    ) -> Result<DocumentGraph> {
        let mut profiler = StepProfiler::new(enable_profiling);
        let start_time = Instant::now();

        // Read PDF and calculate hash
        let pdf_bytes = std::fs::read(input_path)?;
        let pdf_hash = calculate_pdf_hash(&pdf_bytes);

        println!("📄 Processing: {}", input_path);

        // --- C3: Graph cache check ---
        if fresh_from.should_use_cache(CachePoint::C3) && cache_defaults.should_write(CachePoint::C3) {
            let config_hash = calculate_config_hash(config)?;
            let cache_key = GraphCacheKey::new(pdf_hash.clone(), config_hash);
            if let Some(cached) = self.storage.get_graph_output(&cache_key)? {
                println!(
                    "🎯 C3 graph cache hit ({:.3}s)",
                    start_time.elapsed().as_secs_f64()
                );
                return Ok(cached.graph);
            }
        }

        // Create deterministic ID generator: version + pdf_hash + config_hash
        let config_hash = calculate_config_hash(config)?;
        let id_gen = NodeIdGenerator::new(
            cache::versions::BLAZEGRAPH_VERSION,
            &pdf_hash,
            &config_hash,
        );

        // --- C2: Preprocessor cache check ---
        let preprocessor_output = if fresh_from.should_use_cache(CachePoint::C2) {
            if let Some(cached) = self.storage.get_preprocessor_output(&pdf_hash)? {
                println!("🎯 C2 preprocessor cache hit — skipping extraction + parsing");
                cached
            } else {
                self.extract_and_parse(input_path, &pdf_bytes, &pdf_hash, &fresh_from, cache_defaults, &mut profiler)?
            }
        } else {
            self.extract_and_parse(input_path, &pdf_bytes, &pdf_hash, &fresh_from, cache_defaults, &mut profiler)?
        };

        // --- Stages 2-5: Classification → Rules → Graph → Post-processing ---
        let graph = self.rules_and_graph(&preprocessor_output, config, &id_gen, &mut profiler)?;

        if enable_profiling {
            profiler.print_summary();
        }
        println!(
            "⏱️  Total: {:.0}ms",
            start_time.elapsed().as_millis()
        );

        Ok(graph)
    }

    /// Simple document processing function using default config (no cache awareness)
    pub fn process_document(&mut self, input_path: &str) -> Result<DocumentGraph> {
        let default_config = ParsingConfig::default();
        self.process_document_with_cache(
            input_path,
            &default_config,
            FreshFrom::None,
            &CacheDefaults::default(),
            false,
        )
    }

    /// Process document with config loaded from file
    pub fn process_document_with_config_file(
        &mut self,
        input_path: &str,
        config_path: &str,
    ) -> Result<DocumentGraph> {
        let config = ParsingConfig::load_from_file(config_path)?;
        self.process_document_with_cache(
            input_path,
            &config,
            FreshFrom::None,
            &CacheDefaults::default(),
            false,
        )
    }

    /// Process document and capture all intermediate stage outputs
    /// Used for pipeline diagnostics (--dump-stages). Always runs fresh.
    pub fn process_document_capture_stages(
        &mut self,
        input_path: &str,
        config: &ParsingConfig,
    ) -> Result<PipelineStages> {
        let input_path_ref = Path::new(input_path);
        let pdf_bytes = std::fs::read(input_path_ref)?;

        // Stage 1a: PDF → XHTML (always fresh for diagnostics)
        let xhtml = self.preprocessor.parse_pdf_to_markup_language(&pdf_bytes)?;
        println!("📋 Stage 1a: XHTML captured ({} bytes)", xhtml.len());

        // Stage 1b: XHTML → TextElements
        let preprocessor_output = self
            .preprocessor
            .parse_markup_to_preprocessor_output(&xhtml)?;
        let text_elements = preprocessor_output.text_elements.clone();
        println!("📋 Stage 1b: {} TextElements captured", text_elements.len());

        // Stage 2: Classification + Rules → ParsedElements
        let classification = self.classifier.classify(&preprocessor_output)?;
        let document_analysis =
            DocumentAnalysis::analyze_text_elements(&preprocessor_output.text_elements);

        let parsed_elements = if config.minimal_parse {
            self.rule_engine
                .convert_text_elements_to_parsed(&preprocessor_output.text_elements)
        } else {
            let font_size_analysis = self.rule_engine.analyze_font_sizes(
                &preprocessor_output.text_elements,
                &preprocessor_output.style_data,
            );
            self.rule_engine.apply_rules_with_config(
                &preprocessor_output.text_elements,
                &classification,
                &document_analysis,
                &font_size_analysis,
                &preprocessor_output.style_data,
                config,
            )?
        };
        println!(
            "📋 Stage 2: {} ParsedElements captured",
            parsed_elements.len()
        );

        // Infer title from content before graph build
        let inferred_title = infer_title(&parsed_elements);

        // Stage 3: ParsedElements → DocumentGraph
        let mut graph = self.graph_builder.build_graph(parsed_elements.clone())?;

        // Wire metadata and compute post-processing
        if let Some(title) = inferred_title {
            graph.document_info.document_metadata.title = Some(title);
        }
        graph
            .document_info
            .document_metadata
            .merge_extracted(preprocessor_output.metadata);
        graph.document_info.document_analysis = document_analysis;
        graph.document_info.bookmark_data = preprocessor_output.bookmark_data;
        graph.compute_structural_profile();
        graph.compute_breadcrumbs();
        crate::graphs::graph_sanity::apply(&mut graph, &config.graph_sanity);

        println!(
            "📋 Stage 3: Graph captured ({} nodes)",
            graph.nodes.len()
        );

        Ok(PipelineStages {
            xhtml,
            text_elements,
            parsed_elements,
            graph,
        })
    }

    // =========================================================================
    // Internal: extraction + parsing with C1/C2 cache awareness
    // =========================================================================

    /// Extract XHTML and parse to PreprocessorOutput, respecting C1 and C2 caches.
    fn extract_and_parse(
        &mut self,
        _input_path: &str,
        pdf_bytes: &[u8],
        pdf_hash: &str,
        fresh_from: &FreshFrom,
        cache_defaults: &CacheDefaults,
        profiler: &mut StepProfiler,
    ) -> Result<PreprocessorOutput> {
        // --- C1: XHTML cache check ---
        let xhtml = if fresh_from.should_use_cache(CachePoint::C1) {
            if let Some(cached) = self.storage.get_xhtml(pdf_hash)? {
                println!("🎯 C1 XHTML cache hit — skipping Tika extraction");
                cached
            } else {
                let markup = profiler.time_step("C1: PDF → XHTML (Tika)", || {
                    self.preprocessor.parse_pdf_to_markup_language(pdf_bytes)
                })?;
                if cache_defaults.should_write(CachePoint::C1) {
                    self.storage.store_xhtml(pdf_hash, &markup)?;
                    println!("💾 C1: XHTML cached ({} bytes)", markup.len());
                }
                markup
            }
        } else {
            // Fresh extraction requested
            let markup = profiler.time_step("C1: PDF → XHTML (Tika, fresh)", || {
                self.preprocessor.parse_pdf_to_markup_language(pdf_bytes)
            })?;
            if cache_defaults.should_write(CachePoint::C1) {
                self.storage.store_xhtml(pdf_hash, &markup)?;
                println!("💾 C1: XHTML cached ({} bytes, refreshed)", markup.len());
            }
            markup
        };

        // --- C2: Parse XHTML → PreprocessorOutput ---
        let output = profiler.time_step("C2: XHTML → PreprocessorOutput", || {
            self.preprocessor
                .parse_markup_to_preprocessor_output(&xhtml)
        })?;

        if cache_defaults.should_write(CachePoint::C2) {
            self.storage
                .store_preprocessor_output(pdf_hash, &output)?;
            println!("💾 C2: PreprocessorOutput cached");
        }

        Ok(output)
    }

    // =========================================================================
    // Internal: classification → rules → graph (shared by all entry points)
    // =========================================================================

    /// Run classification, rules, and graph building on PreprocessorOutput.
    fn rules_and_graph(
        &mut self,
        preprocessor_output: &PreprocessorOutput,
        config: &ParsingConfig,
        id_gen: &NodeIdGenerator,
        profiler: &mut StepProfiler,
    ) -> Result<DocumentGraph> {
        // Classification
        let classification = profiler.time_step("Classification", || {
            self.classifier.classify(preprocessor_output)
        })?;

        // Document analysis (used by rules + stored in DocumentInfo)
        let document_analysis = profiler.time_step("Document Analysis", || {
            DocumentAnalysis::analyze_text_elements(&preprocessor_output.text_elements)
        });

        // Rule processing
        let parsed_elements = if config.minimal_parse {
            println!("🔄 Minimal parse mode — skipping rule processing");
            self.rule_engine
                .convert_text_elements_to_parsed(&preprocessor_output.text_elements)
        } else {
            let font_size_analysis = profiler.time_step("Font Analysis", || {
                self.rule_engine.analyze_font_sizes(
                    &preprocessor_output.text_elements,
                    &preprocessor_output.style_data,
                )
            });

            profiler.time_step("Rules Processing", || {
                self.rule_engine.apply_rules_with_config(
                    &preprocessor_output.text_elements,
                    &classification,
                    &document_analysis,
                    &font_size_analysis,
                    &preprocessor_output.style_data,
                    config,
                )
            })?
        };

        // Infer title before graph build consumes elements
        let inferred_title = infer_title(&parsed_elements);

        // Graph construction (deterministic UUIDv5 node IDs)
        let mut graph = profiler.time_step("Graph Construction", || {
            self.graph_builder.build_graph_deterministic(parsed_elements, id_gen)
        })?;

        // Post-processing: metadata, analysis, breadcrumbs
        if let Some(title) = inferred_title {
            graph.document_info.document_metadata.title = Some(title);
        }
        graph
            .document_info
            .document_metadata
            .merge_extracted(preprocessor_output.metadata.clone());
        graph.document_info.document_analysis = document_analysis;
        graph.document_info.bookmark_data = preprocessor_output.bookmark_data.clone();
        graph.compute_structural_profile();
        graph.compute_breadcrumbs();
        crate::graphs::graph_sanity::apply(&mut graph, &config.graph_sanity);

        Ok(graph)
    }
}