Skip to main content

orbok_workers/
chunk_and_index.rs

1//! Chunk-and-index worker (RFC-006 §12): loads an extraction result
2//! from the cache, chunks it, and atomically inserts chunks + FTS index
3//! into the catalog (one transaction).
4
5use orbok_cache::{CacheService, EngineOptions, OrbokCacheNamespace};
6use orbok_core::{ErrorCategory, ExtractionId, FileId, OrbokError, OrbokResult};
7use orbok_db::Catalog;
8use orbok_db::repo::{ChunkRepository, FileRepository, SourceRepository};
9use orbok_extract::{ExtractOutput, chunk};
10use orbok_fs::{GuardedSource, PathGuard};
11use rusqlite::params;
12use std::path::Path;
13
14/// Chunk-and-index worker.
15pub struct ChunkAndIndexWorker<'a> {
16    catalog: &'a Catalog,
17    cache: &'a CacheService,
18}
19
20impl<'a> ChunkAndIndexWorker<'a> {
21    pub fn new(catalog: &'a Catalog, cache: &'a CacheService) -> Self {
22        Self { catalog, cache }
23    }
24
25    /// Load the extraction cache for a file, chunk, and index.
26    pub fn run(&self, file_id: &FileId) -> OrbokResult<()> {
27        let files = FileRepository::new(self.catalog);
28        let record = files
29            .get_by_id(file_id)?
30            .ok_or(OrbokError::FileNotFound)?;
31        let sources = SourceRepository::new(self.catalog);
32        let source = sources
33            .get(&record.source_id)?
34            .ok_or(OrbokError::SourceNotFound)?;
35
36        let guard = PathGuard::new(vec![GuardedSource::from_record(&source)]);
37        let validated = guard.validate(Path::new(&record.canonical_path))?;
38
39        let engine = self.cache.engine::<ExtractOutput>(
40            self.catalog,
41            &OrbokCacheNamespace::ExtractSegments,
42            EngineOptions::default(),
43        )?;
44        let output = CacheService::get_fresh(&engine, &validated)?.ok_or_else(|| {
45            OrbokError::Extraction {
46                category: ErrorCategory::ParserError,
47                message: "extraction cache miss: run extraction first".into(),
48            }
49        })?;
50
51        // Find the most recent succeeded extraction record for this file.
52        let extraction_id = self.latest_extraction_id(file_id)?;
53
54        let file_name = Path::new(&record.display_path)
55            .file_name()
56            .map(|n| n.to_string_lossy().into_owned())
57            .unwrap_or_else(|| record.display_path.clone());
58
59        let specs = chunk(&output, &file_name);
60        if specs.is_empty() || (specs.len() == 1 && specs[0].normalized_text.is_empty()) {
61            return Ok(());
62        }
63
64        ChunkRepository::new(self.catalog).insert_bundle(file_id, &extraction_id, &specs)?;
65        Ok(())
66    }
67
68    fn latest_extraction_id(&self, file_id: &FileId) -> OrbokResult<ExtractionId> {
69        let conn = self.catalog.lock();
70        let id: String = conn
71            .query_row(
72                "SELECT extraction_id FROM extraction_records \
73                 WHERE file_id = ?1 AND status = 'succeeded' \
74                 ORDER BY completed_at DESC LIMIT 1",
75                params![file_id.as_str()],
76                |row| row.get(0),
77            )
78            .map_err(|e| OrbokError::Database(format!("no extraction record: {e}")))?;
79        Ok(ExtractionId::from_string(id))
80    }
81}