Simplify the SerializableItem::cleanup implementation (#29567)
Release Notes: - N/A --------- Co-authored-by: Julia Ryan <juliaryan3.14@gmail.com>
This commit is contained in:
parent
17703310ae
commit
1d7c86bf0d
13 changed files with 120 additions and 200 deletions
|
@ -27,21 +27,22 @@ static QUEUES: LazyLock<RwLock<HashMap<Arc<str>, WriteQueue>>> = LazyLock::new(D
|
|||
/// Thread safe connection to a given database file or in memory db. This can be cloned, shared, static,
|
||||
/// whatever. It derefs to a synchronous connection by thread that is read only. A write capable connection
|
||||
/// may be accessed by passing a callback to the `write` function which will queue the callback
|
||||
pub struct ThreadSafeConnection<M: Migrator + 'static = ()> {
|
||||
#[derive(Clone)]
|
||||
pub struct ThreadSafeConnection {
|
||||
uri: Arc<str>,
|
||||
persistent: bool,
|
||||
connection_initialize_query: Option<&'static str>,
|
||||
connections: Arc<ThreadLocal<Connection>>,
|
||||
_migrator: PhantomData<*mut M>,
|
||||
}
|
||||
|
||||
unsafe impl<M: Migrator> Send for ThreadSafeConnection<M> {}
|
||||
unsafe impl<M: Migrator> Sync for ThreadSafeConnection<M> {}
|
||||
unsafe impl Send for ThreadSafeConnection {}
|
||||
unsafe impl Sync for ThreadSafeConnection {}
|
||||
|
||||
pub struct ThreadSafeConnectionBuilder<M: Migrator + 'static = ()> {
|
||||
db_initialize_query: Option<&'static str>,
|
||||
write_queue_constructor: Option<WriteQueueConstructor>,
|
||||
connection: ThreadSafeConnection<M>,
|
||||
connection: ThreadSafeConnection,
|
||||
_migrator: PhantomData<*mut M>,
|
||||
}
|
||||
|
||||
impl<M: Migrator> ThreadSafeConnectionBuilder<M> {
|
||||
|
@ -72,7 +73,7 @@ impl<M: Migrator> ThreadSafeConnectionBuilder<M> {
|
|||
self
|
||||
}
|
||||
|
||||
pub async fn build(self) -> anyhow::Result<ThreadSafeConnection<M>> {
|
||||
pub async fn build(self) -> anyhow::Result<ThreadSafeConnection> {
|
||||
self.connection
|
||||
.initialize_queues(self.write_queue_constructor);
|
||||
|
||||
|
@ -111,7 +112,7 @@ impl<M: Migrator> ThreadSafeConnectionBuilder<M> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<M: Migrator> ThreadSafeConnection<M> {
|
||||
impl ThreadSafeConnection {
|
||||
fn initialize_queues(&self, write_queue_constructor: Option<WriteQueueConstructor>) -> bool {
|
||||
if !QUEUES.read().contains_key(&self.uri) {
|
||||
let mut queues = QUEUES.write();
|
||||
|
@ -125,7 +126,7 @@ impl<M: Migrator> ThreadSafeConnection<M> {
|
|||
false
|
||||
}
|
||||
|
||||
pub fn builder(uri: &str, persistent: bool) -> ThreadSafeConnectionBuilder<M> {
|
||||
pub fn builder<M: Migrator>(uri: &str, persistent: bool) -> ThreadSafeConnectionBuilder<M> {
|
||||
ThreadSafeConnectionBuilder::<M> {
|
||||
db_initialize_query: None,
|
||||
write_queue_constructor: None,
|
||||
|
@ -134,8 +135,8 @@ impl<M: Migrator> ThreadSafeConnection<M> {
|
|||
persistent,
|
||||
connection_initialize_query: None,
|
||||
connections: Default::default(),
|
||||
_migrator: PhantomData,
|
||||
},
|
||||
_migrator: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -200,7 +201,7 @@ impl<M: Migrator> ThreadSafeConnection<M> {
|
|||
}
|
||||
}
|
||||
|
||||
impl ThreadSafeConnection<()> {
|
||||
impl ThreadSafeConnection {
|
||||
/// Special constructor for ThreadSafeConnection which disallows db initialization and migrations.
|
||||
/// This allows construction to be infallible and not write to the db.
|
||||
pub fn new(
|
||||
|
@ -214,7 +215,6 @@ impl ThreadSafeConnection<()> {
|
|||
persistent,
|
||||
connection_initialize_query,
|
||||
connections: Default::default(),
|
||||
_migrator: PhantomData,
|
||||
};
|
||||
|
||||
connection.initialize_queues(write_queue_constructor);
|
||||
|
@ -222,19 +222,7 @@ impl ThreadSafeConnection<()> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<M: Migrator> Clone for ThreadSafeConnection<M> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
uri: self.uri.clone(),
|
||||
persistent: self.persistent,
|
||||
connection_initialize_query: self.connection_initialize_query,
|
||||
connections: self.connections.clone(),
|
||||
_migrator: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<M: Migrator> Deref for ThreadSafeConnection<M> {
|
||||
impl Deref for ThreadSafeConnection {
|
||||
type Target = Connection;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
|
@ -301,7 +289,7 @@ mod test {
|
|||
for _ in 0..100 {
|
||||
handles.push(thread::spawn(|| {
|
||||
let builder =
|
||||
ThreadSafeConnection::<TestDomain>::builder("annoying-test.db", false)
|
||||
ThreadSafeConnection::builder::<TestDomain>("annoying-test.db", false)
|
||||
.with_db_initialization_query("PRAGMA journal_mode=WAL")
|
||||
.with_connection_initialize_query(indoc! {"
|
||||
PRAGMA synchronous=NORMAL;
|
||||
|
@ -353,7 +341,7 @@ mod test {
|
|||
}
|
||||
|
||||
let builder =
|
||||
ThreadSafeConnection::<TestWorkspace>::builder("wild_zed_lost_failure", false)
|
||||
ThreadSafeConnection::builder::<TestWorkspace>("wild_zed_lost_failure", false)
|
||||
.with_connection_initialize_query("PRAGMA FOREIGN_KEYS=true");
|
||||
|
||||
smol::block_on(builder.build()).unwrap();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue