2023-08-10 16:55:31 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use crate::connection::libsql::LibSqlConnection;
|
2023-11-01 10:31:28 +01:00
|
|
|
use crate::connection::write_proxy::{RpcStream, WriteProxyConnection};
|
2023-08-10 16:55:31 +02:00
|
|
|
use crate::connection::{Connection, MakeConnection, TrackedConnection};
|
2023-10-03 16:46:51 +02:00
|
|
|
use crate::replication::{ReplicationLogger, ReplicationLoggerHook};
|
2023-08-10 16:55:31 +02:00
|
|
|
|
|
|
|
pub trait Database: Sync + Send + 'static {
|
|
|
|
/// The connection type of the database
|
|
|
|
type Connection: Connection;
|
|
|
|
|
|
|
|
fn connection_maker(&self) -> Arc<dyn MakeConnection<Connection = Self::Connection>>;
|
2023-09-01 11:33:11 +02:00
|
|
|
fn shutdown(&self);
|
2023-08-10 16:55:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ReplicaDatabase {
|
|
|
|
pub connection_maker:
|
2023-11-01 10:31:28 +01:00
|
|
|
Arc<dyn MakeConnection<Connection = TrackedConnection<WriteProxyConnection<RpcStream>>>>,
|
2023-08-10 16:55:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Database for ReplicaDatabase {
|
2023-11-01 10:31:28 +01:00
|
|
|
type Connection = TrackedConnection<WriteProxyConnection<RpcStream>>;
|
2023-08-10 16:55:31 +02:00
|
|
|
|
|
|
|
fn connection_maker(&self) -> Arc<dyn MakeConnection<Connection = Self::Connection>> {
|
|
|
|
self.connection_maker.clone()
|
|
|
|
}
|
2023-09-01 11:33:11 +02:00
|
|
|
|
|
|
|
fn shutdown(&self) {}
|
2023-08-10 16:55:31 +02:00
|
|
|
}
|
|
|
|
|
2023-10-03 16:46:51 +02:00
|
|
|
pub type PrimaryConnection = TrackedConnection<LibSqlConnection<ReplicationLoggerHook>>;
|
|
|
|
|
2023-08-10 16:55:31 +02:00
|
|
|
pub struct PrimaryDatabase {
|
|
|
|
pub logger: Arc<ReplicationLogger>,
|
2023-10-03 16:46:51 +02:00
|
|
|
pub connection_maker: Arc<dyn MakeConnection<Connection = PrimaryConnection>>,
|
2023-08-10 16:55:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Database for PrimaryDatabase {
|
2023-10-03 16:46:51 +02:00
|
|
|
type Connection = PrimaryConnection;
|
2023-08-10 16:55:31 +02:00
|
|
|
|
|
|
|
fn connection_maker(&self) -> Arc<dyn MakeConnection<Connection = Self::Connection>> {
|
|
|
|
self.connection_maker.clone()
|
|
|
|
}
|
2023-09-01 11:33:11 +02:00
|
|
|
|
|
|
|
fn shutdown(&self) {
|
|
|
|
self.logger.closed_signal.send_replace(true);
|
|
|
|
}
|
2023-08-10 16:55:31 +02:00
|
|
|
}
|