use std::collections::HashMap;
use std::sync::{Arc, LazyLock, RwLock};
use fv_mac::{Bindings, Taxonomy};
use crate::config::ConnectorConfig;
use crate::write::{write_and_promote_mysql, write_and_promote_postgres};
use crate::{read_mysql_with_mac, read_with_mac, ConnectError, Row};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Caps {
pub read: bool,
pub write: bool,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ScanLeg {
Polled,
Log,
Snapshots,
}
pub type LabelColumn<'a> = &'a (dyn Fn(&str) -> String + Sync);
#[allow(clippy::too_many_arguments)]
#[async_trait::async_trait]
pub trait Source: Send + Sync {
fn capabilities(&self) -> Caps;
fn scan_leg(&self) -> ScanLeg {
ScanLeg::Polled
}
async fn read(
&self,
config: &ConnectorConfig,
password: &str,
table: &str,
taxonomy: &Taxonomy,
grants: &Bindings,
label_column: LabelColumn<'_>,
limit: Option<usize>,
) -> Result<Vec<Row>, ConnectError>;
async fn write_and_promote(
&self,
config: &ConnectorConfig,
password: &str,
table: &str,
columns: &[String],
rows: &[Row],
) -> Result<(), ConnectError>;
}
struct PostgresSource;
#[async_trait::async_trait]
impl Source for PostgresSource {
fn capabilities(&self) -> Caps {
Caps {
read: true,
write: true,
}
}
async fn read(
&self,
config: &ConnectorConfig,
password: &str,
table: &str,
taxonomy: &Taxonomy,
grants: &Bindings,
label_column: LabelColumn<'_>,
limit: Option<usize>,
) -> Result<Vec<Row>, ConnectError> {
read_with_mac(
&config.postgres_dsn(password),
table,
taxonomy,
grants,
label_column,
limit,
)
.await
}
async fn write_and_promote(
&self,
config: &ConnectorConfig,
password: &str,
table: &str,
columns: &[String],
rows: &[Row],
) -> Result<(), ConnectError> {
write_and_promote_postgres(&config.postgres_dsn(password), table, columns, rows).await
}
}
struct MysqlSource;
#[async_trait::async_trait]
impl Source for MysqlSource {
fn capabilities(&self) -> Caps {
Caps {
read: true,
write: true,
}
}
async fn read(
&self,
config: &ConnectorConfig,
password: &str,
table: &str,
taxonomy: &Taxonomy,
grants: &Bindings,
label_column: LabelColumn<'_>,
limit: Option<usize>,
) -> Result<Vec<Row>, ConnectError> {
read_mysql_with_mac(
&config.mysql_url(password),
table,
taxonomy,
grants,
label_column,
limit,
)
.await
}
async fn write_and_promote(
&self,
config: &ConnectorConfig,
password: &str,
table: &str,
columns: &[String],
rows: &[Row],
) -> Result<(), ConnectError> {
write_and_promote_mysql(&config.mysql_url(password), table, columns, rows).await
}
}
struct SqliteSource;
#[async_trait::async_trait]
impl Source for SqliteSource {
fn capabilities(&self) -> Caps {
Caps {
read: true,
write: true,
}
}
async fn read(
&self,
config: &ConnectorConfig,
_password: &str,
table: &str,
taxonomy: &Taxonomy,
grants: &Bindings,
label_column: LabelColumn<'_>,
limit: Option<usize>,
) -> Result<Vec<Row>, ConnectError> {
let (path, table, taxonomy, grants) = (
config.database.clone(),
table.to_string(),
taxonomy.clone(),
grants.clone(),
);
let label: Vec<(String, String)> = taxonomy
.axes
.iter()
.map(|a| (a.name.clone(), label_column(&a.name)))
.collect();
tokio::task::spawn_blocking(move || {
let map: std::collections::HashMap<String, String> = label.into_iter().collect();
crate::sqlite::read_sqlite_with_mac_sync(
&path,
&table,
&taxonomy,
&grants,
|a| map.get(a).cloned().unwrap_or_else(|| a.to_string()),
limit,
)
})
.await
.map_err(|e| ConnectError(format!("sqlite read task: {e}")))?
}
async fn write_and_promote(
&self,
config: &ConnectorConfig,
_password: &str,
table: &str,
columns: &[String],
rows: &[Row],
) -> Result<(), ConnectError> {
let (path, table, columns, rows) = (
config.database.clone(),
table.to_string(),
columns.to_vec(),
rows.to_vec(),
);
tokio::task::spawn_blocking(move || {
crate::sqlite::write_and_promote_sqlite_sync(&path, &table, &columns, &rows)
})
.await
.map_err(|e| ConnectError(format!("sqlite write task: {e}")))?
}
}
struct LegOnly(ScanLeg);
#[async_trait::async_trait]
impl Source for LegOnly {
fn capabilities(&self) -> Caps {
Caps {
read: false,
write: false,
}
}
fn scan_leg(&self) -> ScanLeg {
self.0
}
async fn read(
&self,
config: &ConnectorConfig,
_: &str,
_: &str,
_: &Taxonomy,
_: &Bindings,
_: LabelColumn<'_>,
_: Option<usize>,
) -> Result<Vec<Row>, ConnectError> {
Err(ConnectError(format!(
"connector type '{}' is scan-leg driven ({:?}); it has no relational read",
config.kind, self.0
)))
}
async fn write_and_promote(
&self,
config: &ConnectorConfig,
_: &str,
_: &str,
_: &[String],
_: &[Row],
) -> Result<(), ConnectError> {
Err(ConnectError(format!(
"connector type '{}' has no write capability",
config.kind
)))
}
}
static REGISTRY: LazyLock<RwLock<HashMap<String, Arc<dyn Source>>>> = LazyLock::new(|| {
let mut m: HashMap<String, Arc<dyn Source>> = HashMap::new();
m.insert("POSTGRES".into(), Arc::new(PostgresSource));
m.insert("MYSQL".into(), Arc::new(MysqlSource));
m.insert("SQLITE".into(), Arc::new(SqliteSource));
m.insert("KAFKA".into(), Arc::new(LegOnly(ScanLeg::Log)));
m.insert("ICEBERG".into(), Arc::new(LegOnly(ScanLeg::Snapshots)));
m.insert("ICEBERG_REST".into(), Arc::new(LegOnly(ScanLeg::Snapshots)));
m.insert("REST".into(), Arc::new(crate::rest::RestSource));
m.insert("REST_POLL".into(), Arc::new(crate::rest::RestSource));
RwLock::new(m)
});
pub fn register_source(kind: &str, source: Arc<dyn Source>) {
REGISTRY.write().unwrap().insert(kind.to_ascii_uppercase(), source);
}
pub fn source_for(kind: &str) -> Option<Arc<dyn Source>> {
REGISTRY.read().unwrap().get(&kind.to_ascii_uppercase()).cloned()
}
pub fn registered_kinds() -> Vec<String> {
let mut kinds: Vec<String> = REGISTRY.read().unwrap().keys().cloned().collect();
kinds.sort();
kinds
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn builtins_are_registered_with_full_caps() {
for kind in ["POSTGRES", "postgres", "MYSQL", "mysql"] {
let s = source_for(kind).expect(kind);
assert_eq!(
s.capabilities(),
Caps {
read: true,
write: true
}
);
}
assert!(source_for("ORACLE").is_none());
let kinds = registered_kinds();
assert!(kinds.contains(&"MYSQL".to_string()) && kinds.contains(&"POSTGRES".to_string()));
}
#[test]
fn a_registered_custom_driver_is_dispatchable() {
struct ReadOnly;
#[async_trait::async_trait]
impl Source for ReadOnly {
fn capabilities(&self) -> Caps {
Caps {
read: true,
write: false,
}
}
async fn read(
&self,
_: &ConnectorConfig,
_: &str,
_: &str,
_: &Taxonomy,
_: &Bindings,
_: LabelColumn<'_>,
_: Option<usize>,
) -> Result<Vec<Row>, ConnectError> {
Ok(vec![vec![("rid".into(), fv_value::Value::Str("r-1".into()))]])
}
async fn write_and_promote(
&self,
_: &ConnectorConfig,
_: &str,
_: &str,
_: &[String],
_: &[Row],
) -> Result<(), ConnectError> {
Err(ConnectError("read-only".into()))
}
}
register_source("TESTKIND", Arc::new(ReadOnly));
let s = source_for("testkind").expect("registered");
assert!(s.capabilities().read && !s.capabilities().write);
}
}