Files
unchecked-io/src/lib.rs

73 lines
2.2 KiB
Rust
Raw Normal View History

2025-11-17 14:23:03 -07:00
// --- Declare our new modules ---
mod config;
mod parser;
// --- External Crates ---
use pyo3::prelude::*;
use pyo3::exceptions::PyValueError;
use tokio;
2025-11-18 11:52:28 -07:00
use pyo3::types::{PyModule, PyAny};
use pyo3::Bound;
use pyo3_arrow::PyRecordBatch;
2025-11-18 12:40:08 -07:00
// Use the high-performance mimalloc for better multi-threaded memory allocation.
// We conditionally compile it to avoid issues on MSVC targets.
#[cfg(not(target_env = "msvc"))]
use mimalloc;
#[cfg(not(target_env = "msvc"))]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
2025-11-17 14:23:03 -07:00
// --- Internal Crates ---
use crate::config::{load_and_validate_config, ConnectorConfig};
use crate::parser::run_db_logic;
2025-11-18 12:15:01 -07:00
// --- THE PYTHON-CALLABLE ENTRY POINT ---
2025-11-17 14:23:03 -07:00
#[pyfunction]
2025-11-18 12:40:08 -07:00
#[pyo3(signature = (config_path, blast_radius=312500))]
2025-11-17 14:23:03 -07:00
#[allow(unsafe_code)]
#[allow(unsafe_op_in_unsafe_fn)]
#[allow(rust_2024_compatibility)]
fn load_data_from_config<'py>(
py: Python<'py>,
config_path: String,
2025-11-18 11:52:28 -07:00
blast_radius: i64,
) -> PyResult<Bound<'py, PyAny>> {
2025-11-18 12:15:01 -07:00
// --- Phase 1: Load and Validate Configuration ---
2025-11-17 14:23:03 -07:00
let config: ConnectorConfig = match load_and_validate_config(&config_path) {
Ok(c) => c,
Err(e) => return Err(PyValueError::new_err(format!("Configuration Error: {:?}", e))),
};
println!("--- UncheckedIO: Schema Accepted ---");
println!("Database: {}", config.connection_string);
2025-11-18 12:15:01 -07:00
println!("Columns (in order): {:?}", config.schema.iter().map(|c| &c.column_name).collect::<Vec<_>>());
2025-11-17 14:23:03 -07:00
2025-11-18 12:15:01 -07:00
// --- Phase 2: Run Core Logic ---
let record_batch = py.allow_threads(|| {
2025-11-17 14:23:03 -07:00
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
2025-11-18 12:15:01 -07:00
run_db_logic(config, blast_radius).await
2025-11-17 14:23:03 -07:00
})
}).map_err(|e| PyValueError::new_err(format!("Database/Runtime Error: {:?}", e)))?;
2025-11-18 12:15:01 -07:00
// --- Phase 3: Return Data to Python ---
let py_record_batch = PyRecordBatch::new(record_batch);
py_record_batch.into_pyarrow(py)
2025-11-17 14:23:03 -07:00
}
// --- PYTHON MODULE EXPORT ---
2025-11-18 12:15:01 -07:00
2025-11-17 14:23:03 -07:00
#[pymodule]
fn unchecked_io(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
2025-11-17 14:23:03 -07:00
m.add_function(wrap_pyfunction!(load_data_from_config, m)?)?;
Ok(())
}