use anyhow::Result;
use std::path::Path;
use super::ScrapeConfig;
pub mod database;
pub mod extract_v2;
pub mod extracted_data;
pub mod languages;
pub mod types;
pub fn initialize(config: &ScrapeConfig) -> Result<()> {
println!("🗄️ Initializing optimized knowledge database...");
if let Some(parent) = Path::new(&config.db_path).parent() {
std::fs::create_dir_all(parent)?;
}
if Path::new(&config.db_path).exists() {
std::fs::remove_file(&config.db_path)?;
}
initialize_database(&config.db_path)?;
println!("✅ Database initialized at {}", config.db_path);
Ok(())
}
pub fn run(config: ScrapeConfig) -> Result<super::ScrapeStats> {
println!("🔄 Extracting semantic code information...");
let start = std::time::Instant::now();
let work_dir = std::env::current_dir()?;
println!("📂 Working directory: {}", work_dir.display());
if !Path::new(&config.db_path).exists() || config.force {
initialize_database(&config.db_path)?;
}
let items_processed =
extract_v2::extract_code_metadata_v2(&config.db_path, &work_dir, config.force)?;
println!("📝 Building FTS5 lexical index...");
let conn = rusqlite::Connection::open(&config.db_path)?;
let fts_count = super::database::populate_fts5(&conn)?;
println!(" Indexed {} symbols", fts_count);
let metadata = std::fs::metadata(&config.db_path)?;
let database_size_kb = metadata.len() / 1024;
Ok(super::ScrapeStats {
items_processed,
time_elapsed: start.elapsed(),
database_size_kb,
})
}
fn initialize_database(db_path: &str) -> Result<()> {
if let Some(parent) = Path::new(db_path).parent() {
std::fs::create_dir_all(parent)?;
}
if Path::new(db_path).exists() {
std::fs::remove_file(db_path)?;
}
super::database::initialize(Path::new(db_path))?;
let mut db = database::Database::open(db_path)?;
db.init_schema()?;
Ok(())
}