refactor: make postgres and mem catalog implementations public
parent
b3ee1032b3
commit
dfe95e1a56
|
@ -7,12 +7,31 @@ use crate::interface::{
|
|||
};
|
||||
use async_trait::async_trait;
|
||||
use std::convert::TryFrom;
|
||||
use std::fmt::Formatter;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
struct MemCatalog {
|
||||
/// In-memory catalog that implements the `RepoCollection` and individual repo traits fromt
|
||||
/// the catalog interface.
|
||||
#[derive(Default)]
|
||||
pub struct MemCatalog {
|
||||
collections: Mutex<MemCollections>,
|
||||
}
|
||||
|
||||
impl MemCatalog {
|
||||
/// return new initialized `MemCatalog`
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for MemCatalog {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
let c = self.collections.lock().expect("mutex poisoned");
|
||||
write!(f, "MemCatalog[ {:?} ]", c)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
struct MemCollections {
|
||||
kafka_topics: Vec<KafkaTopic>,
|
||||
query_pools: Vec<QueryPool>,
|
||||
|
|
|
@ -16,12 +16,19 @@ const IDLE_TIMEOUT: Duration = Duration::from_secs(500);
|
|||
#[allow(dead_code)]
|
||||
const SCHEMA_NAME: &str = "iox_catalog";
|
||||
|
||||
/// Connect to the catalog store.
|
||||
pub async fn connect_catalog_store(
|
||||
/// In-memory catalog that implements the `RepoCollection` and individual repo traits.
|
||||
#[derive(Debug)]
|
||||
pub struct PostgresCatalog {
|
||||
pool: Pool<Postgres>,
|
||||
}
|
||||
|
||||
impl PostgresCatalog {
|
||||
/// Connect to the catalog store.
|
||||
pub async fn connect(
|
||||
app_name: &'static str,
|
||||
schema_name: &'static str,
|
||||
dsn: &str,
|
||||
) -> Result<Pool<Postgres>, sqlx::Error> {
|
||||
) -> Result<Self> {
|
||||
let pool = PgPoolOptions::new()
|
||||
.min_connections(1)
|
||||
.max_connections(MAX_CONNECTIONS)
|
||||
|
@ -40,17 +47,15 @@ pub async fn connect_catalog_store(
|
|||
})
|
||||
})
|
||||
.connect(dsn)
|
||||
.await?;
|
||||
.await
|
||||
.map_err(|e| Error::SqlxError { source: e })?;
|
||||
|
||||
// Log a connection was successfully established and include the application
|
||||
// name for cross-correlation between Conductor logs & database connections.
|
||||
info!(application_name=%app_name, "connected to catalog store");
|
||||
|
||||
Ok(pool)
|
||||
}
|
||||
|
||||
struct PostgresCatalog {
|
||||
pool: Pool<Postgres>,
|
||||
Ok(Self { pool })
|
||||
}
|
||||
}
|
||||
|
||||
impl RepoCollection for Arc<PostgresCatalog> {
|
||||
|
@ -348,6 +353,7 @@ fn is_fk_violation(e: &sqlx::Error) -> bool {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::postgres::PostgresCatalog;
|
||||
use crate::{
|
||||
create_or_get_default_records, interface::NamespaceSchema, validate_or_insert_schema,
|
||||
};
|
||||
|
@ -395,10 +401,11 @@ mod tests {
|
|||
|
||||
async fn setup_db() -> (Arc<PostgresCatalog>, KafkaTopic, QueryPool) {
|
||||
let dsn = std::env::var("DATABASE_URL").unwrap();
|
||||
let pool = connect_catalog_store("test", SCHEMA_NAME, &dsn)
|
||||
let postgres_catalog = Arc::new(
|
||||
PostgresCatalog::connect("test", SCHEMA_NAME, &dsn)
|
||||
.await
|
||||
.unwrap();
|
||||
let postgres_catalog = Arc::new(PostgresCatalog { pool });
|
||||
.unwrap(),
|
||||
);
|
||||
|
||||
let (kafka_topic, query_pool, _) = create_or_get_default_records(2, &postgres_catalog)
|
||||
.await
|
||||
|
|
Loading…
Reference in New Issue