blazegraph-io-core 0.1.0

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
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
use crate::cache::{GraphCacheKey, GraphCacheValue};
use crate::classifier::DocumentClassifier;
use crate::config::ParsingConfig;
use crate::graphs::builder::GraphBuilder;
use crate::preprocessors::{Preprocessor, TikaPreprocessor};
use crate::rules::{engine::DebugConfig, RuleEngine};
use crate::storage::{calculate_config_hash, calculate_pdf_hash, DocumentStorage, FileStorage};
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)
    ///
    /// # Arguments
    /// * `jre_path` - Path to JRE directory
    /// * `jar_path` - Path to blazing-tika.jar
    #[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)
    }

    // Future: Convenience constructor for API usage (server Tika + database storage)
    // This will be implemented when server-based Tika preprocessor is available
    // pub fn new_api(server_url: &str, db_config: &DatabaseConfig) -> Result<Self> {
    //     let preprocessor = Box::new(TikaPreprocessor::new_with_server(server_url)?);
    //     let storage = Box::new(DatabaseStorage::new(db_config)?);
    //     Self::new_with_dependencies(preprocessor, storage)
    // }

    /// Process document with specific config and profiling (pure function approach)
    /// This is the main method implementing PDF + Config → Graph with Level 2 caching
    pub fn process_document_with_config_and_profiling(
        &mut self,
        input_path: &str,
        config: &ParsingConfig,
        enable_profiling: bool,
        skip_cache: bool,
    ) -> Result<DocumentGraph> {
        if enable_profiling {
            self.process_document_with_config_and_profiler(
                input_path,
                config,
                StepProfiler::new(true),
                skip_cache,
            )
        } else if skip_cache {
            // Skip cache without profiling - use no-op profiler
            self.process_document_with_config_and_profiler(
                input_path,
                config,
                StepProfiler::new(false),
                skip_cache,
            )
        } else {
            self.process_document_with_config(input_path, config)
        }
    }

    /// Process document with specific config (pure function approach)
    /// This is the main method implementing PDF + Config → Graph with Level 2 caching
    pub fn process_document_with_config(
        &mut self,
        input_path: &str,
        config: &ParsingConfig,
    ) -> Result<DocumentGraph> {
        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);

        // Calculate config hash for Level 2 cache
        let config_hash = calculate_config_hash(config)?;
        let cache_key = GraphCacheKey::new(pdf_hash.clone(), config_hash);

        // Check Level 2 cache: Config + PDF → Graph
        if let Some(cached) = self.storage.get_graph_output(&cache_key)? {
            println!("🎯 Cache hit: Found graph for PDF + config combination");
            println!(
                "⏱️  Total processing time: {:.3}s (cached)",
                start_time.elapsed().as_secs_f64()
            );
            return Ok(cached.graph);
        }

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

        // Process with config flow
        let graph = self.process_with_config_flow(input_path, config)?;

        // Store in Level 2 cache
        let processing_time = start_time.elapsed().as_millis() as u64;
        let cache_value = GraphCacheValue::new(graph.clone(), processing_time);
        self.storage.store_graph_output(&cache_key, &cache_value)?;

        println!(
            "⏱️  Total processing time: {:.3}s",
            start_time.elapsed().as_secs_f64()
        );
        Ok(graph)
    }

    /// Process document with profiler for detailed timing
    fn process_document_with_config_and_profiler(
        &mut self,
        input_path: &str,
        config: &ParsingConfig,
        mut profiler: StepProfiler,
        skip_cache: bool,
    ) -> Result<DocumentGraph> {
        let start_time = Instant::now();

        // Check cache first (timed)
        let (_pdf_hash, cache_key) = profiler.time_step("Cache Key Generation", || {
            let pdf_bytes = std::fs::read(input_path)?;
            let pdf_hash = calculate_pdf_hash(&pdf_bytes);
            let config_hash = calculate_config_hash(config)?;
            let cache_key = GraphCacheKey::new(pdf_hash.clone(), config_hash);
            Ok::<(String, GraphCacheKey), anyhow::Error>((pdf_hash, cache_key))
        })?;

        let cached_result = if skip_cache {
            println!("🚫 Skipping cache lookup (--skip-cache enabled)");
            None
        } else {
            profiler.time_step("Cache Lookup", || self.storage.get_graph_output(&cache_key))?
        };

        if let Some(cached) = cached_result {
            println!("🎯 Cache hit: Found graph for PDF + config combination");
            profiler.print_summary();
            println!(
                "⏱️  Total processing time: {:.0}ms (cached)",
                start_time.elapsed().as_millis()
            );
            return Ok(cached.graph);
        }

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

        // Process with detailed profiling
        let graph =
            self.process_with_config_flow_and_profiler(input_path, config, &mut profiler)?;

        // Store in cache (timed) unless skipping cache
        if !skip_cache {
            profiler.time_step("Cache Storage", || {
                let processing_time = start_time.elapsed().as_millis() as u64;
                let cache_value = GraphCacheValue::new(graph.clone(), processing_time);
                self.storage.store_graph_output(&cache_key, &cache_value)
            })?;
        } else {
            println!("🚫 Skipping cache storage (--skip-cache enabled)");
        }

        profiler.print_summary();
        println!(
            "⏱️  Total processing time: {:.0}ms",
            start_time.elapsed().as_millis()
        );
        Ok(graph)
    }

    /// Internal processing with config flow through all pipeline stages
    fn process_with_config_flow(
        &mut self,
        input_path: &str,
        config: &ParsingConfig,
    ) -> Result<DocumentGraph> {
        let stage1_start = Instant::now();

        // Stage 1: Preprocessing (PDF → TextElements)
        let input_path = Path::new(input_path);
        let preprocessor_output = self.preprocessor.process_file(input_path)?;
        println!(
            "⏱️  Preprocessing: {:.3}s",
            stage1_start.elapsed().as_secs_f64()
        );

        let stage2_start = Instant::now();

        // Stage 2: Classification
        let classification = self.classifier.classify(&preprocessor_output)?;
        println!("📋 Document classified as: {:?}", classification);
        println!(
            "⏱️  Classification: {:.3}s",
            stage2_start.elapsed().as_secs_f64()
        );

        let stage3_start = Instant::now();

        // Compute document analysis once (used by rules and stored in DocumentInfo)
        let document_analysis =
            DocumentAnalysis::analyze_text_elements(&preprocessor_output.text_elements);

        // Stage 3: Rule processing with config (TextElements + Config → ParsedElements)
        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 = self.rule_engine.analyze_font_sizes(
                &preprocessor_output.text_elements,
                &preprocessor_output.style_data,
            );

            // Apply rules with config guiding behavior
            self.rule_engine.apply_rules_with_config(
                &preprocessor_output.text_elements,
                &classification,
                &document_analysis,
                &font_size_analysis,
                &preprocessor_output.style_data,
                config, // Config flows through rule engine
            )?
        };

        println!(
            "⏱️  Rule processing: {:.3}s",
            stage3_start.elapsed().as_secs_f64()
        );

        let stage4_start = Instant::now();

        // Infer title from content before elements are consumed by graph builder
        let inferred_title = infer_title(&parsed_elements);

        // Stage 4: Graph building (ParsedElements + Config → Graph)
        let mut graph = self.graph_builder.build_graph(parsed_elements)?;
        println!(
            "⏱️  Graph construction: {:.3}s",
            stage4_start.elapsed().as_secs_f64()
        );

        // Stage 5: 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.compute_structural_profile();
        graph.compute_breadcrumbs();

        Ok(graph)
    }

    /// Internal processing with detailed profiling
    fn process_with_config_flow_and_profiler(
        &mut self,
        input_path: &str,
        config: &ParsingConfig,
        profiler: &mut StepProfiler,
    ) -> Result<DocumentGraph> {
        // Stage 1: Preprocessing with sub-steps
        let input_path = Path::new(input_path);
        let pdf_bytes = std::fs::read(input_path)?;
        let markup = profiler.time_step("1. PDF → Markup", || {
            self.preprocessor.parse_pdf_to_markup_language(&pdf_bytes)
        })?;

        let preprocessor_output = profiler.time_step("2. Markup → TextElements", || {
            self.preprocessor
                .parse_markup_to_preprocessor_output(&markup)
        })?;

        // Stage 2: Classification
        let classification = profiler.time_step("3. Classification", || {
            self.classifier.classify(&preprocessor_output)
        })?;

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

        // Stage 3: Rule processing with detailed timing
        let parsed_elements = if config.minimal_parse {
            profiler.time_step("4. Minimal Parse", || {
                self.rule_engine
                    .convert_text_elements_to_parsed(&preprocessor_output.text_elements)
            })
        } else {
            let font_size_analysis = profiler.time_step("4b. Font Analysis", || {
                self.rule_engine.analyze_font_sizes(
                    &preprocessor_output.text_elements,
                    &preprocessor_output.style_data,
                )
            });

            profiler.time_step("4c. 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 from content before elements are consumed by graph builder
        let inferred_title = infer_title(&parsed_elements);

        // Stage 4: Graph building
        let mut graph = profiler.time_step("5. Graph Construction", || {
            self.graph_builder.build_graph(parsed_elements)
        })?;

        // Stage 5: 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.compute_structural_profile();
        graph.compute_breadcrumbs();

        Ok(graph)
    }

    /// Main document processing function with all options
    pub fn process_document_with_options(
        &mut self,
        input_path: &str,
        include_raw_tika: bool,
        output_dir: Option<&str>,
        debug_output: bool,
        debug_filters: &[String],
        minimal_parse: Option<bool>,
    ) -> Result<DocumentGraph> {
        let start_time = Instant::now();
        println!("📄 Processing document: {}", input_path);

        // Step 1: Use preprocessor to extract and parse document
        let preprocessor_output = if include_raw_tika || output_dir.is_some() {
            // For now, handle raw output options by doing two-step process manually
            let input_path = Path::new(input_path);
            let pdf_bytes = std::fs::read(input_path)?;
            let markup = self.preprocessor.parse_pdf_to_markup_language(&pdf_bytes)?;

            // Save raw markup if requested
            if include_raw_tika {
                if let Some(output_dir) = output_dir {
                    use std::fs;
                    let raw_path = format!("{}/raw_tika_output.html", output_dir);
                    if let Err(e) = fs::write(&raw_path, &markup) {
                        println!("⚠️  Failed to save raw markup to {}: {}", raw_path, e);
                    } else {
                        println!("💾 Saved raw markup to {}", raw_path);
                    }
                }
            }

            self.preprocessor
                .parse_markup_to_preprocessor_output(&markup)?
        } else {
            // Standard processing - use the convenience method
            let input_path = Path::new(input_path);
            self.preprocessor.process_file(input_path)?
        };

        println!(
            "⏱️  Preprocessing complete: {:.3}s",
            start_time.elapsed().as_secs_f64()
        );

        let step2_start = Instant::now();

        // Step 2: Document classification
        let classification = self.classifier.classify(&preprocessor_output)?;
        println!("📋 Document classified as: {:?}", classification);

        // Step 3: Get text elements (already parsed by preprocessor)
        println!(
            "⏱️  Text parsing: {:.3}s",
            step2_start.elapsed().as_secs_f64()
        );

        let step3_start = Instant::now();

        // Compute document analysis once (used by rules and stored in DocumentInfo)
        let document_analysis =
            DocumentAnalysis::analyze_text_elements(&preprocessor_output.text_elements);

        // Step 4: Apply rules (skip if minimal parse requested)
        let parsed_elements = if minimal_parse.unwrap_or(false) {
            println!("🔄 Minimal parse mode - skipping rule processing");
            // Convert text elements to parsed elements without processing
            self.rule_engine
                .convert_text_elements_to_parsed(&preprocessor_output.text_elements)
        } else {
            // Set up debug config
            if debug_output {
                let debug_config = DebugConfig {
                    enabled: true,
                    filter_patterns: debug_filters.to_vec(),
                };
                self.rule_engine.set_debug_config(debug_config);
            }

            let font_size_analysis = self.rule_engine.analyze_font_sizes(
                &preprocessor_output.text_elements,
                &preprocessor_output.style_data,
            );

            // Apply rules to get processed elements
            self.rule_engine.apply_rules(
                &preprocessor_output.text_elements,
                &classification,
                &document_analysis,
                &font_size_analysis,
                &preprocessor_output.style_data,
            )?
        };

        println!(
            "⏱️  Rule processing: {:.3}s",
            step3_start.elapsed().as_secs_f64()
        );

        let step4_start = Instant::now();

        // Infer title from content before elements are consumed by graph builder
        let inferred_title = infer_title(&parsed_elements);

        // Step 5: Build graph from processed elements
        let mut graph = self.graph_builder.build_graph(parsed_elements)?;

        // Step 6: 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.compute_structural_profile();
        graph.compute_breadcrumbs();

        println!(
            "⏱️  Graph construction: {:.3}s",
            step4_start.elapsed().as_secs_f64()
        );
        println!(
            "⏱️  Total processing time: {:.3}s",
            start_time.elapsed().as_secs_f64()
        );

        Ok(graph)
    }

    /// Process document and capture all intermediate stage outputs
    /// Used for pipeline diagnostics and testing stage boundaries
    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
        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.compute_structural_profile();
        graph.compute_breadcrumbs();

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

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

    /// Simple document processing function using default config
    pub fn process_document(&mut self, input_path: &str) -> Result<DocumentGraph> {
        let default_config = ParsingConfig::default();
        self.process_document_with_config(input_path, &default_config)
    }

    /// 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_config(input_path, &config)
    }
}