Skip to content

Commit

Permalink
Caching methods in Connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Julius de Bruijn committed Jun 25, 2020
1 parent eba82e3 commit aebe8ac
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 73 deletions.
13 changes: 0 additions & 13 deletions sqlx-core/src/caching_connection.rs

This file was deleted.

1 change: 1 addition & 0 deletions sqlx-core/src/common/statement_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl<T> StatementCache<T> {
}

/// Clear all cached statements from the cache.
#[cfg(any(feature = "postgres", feature = "sqlite"))]
pub fn clear(&mut self) {
self.inner.clear();
}
Expand Down
19 changes: 18 additions & 1 deletion sqlx-core/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::str::FromStr;
use futures_core::future::BoxFuture;
use futures_core::Future;

use crate::database::Database;
use crate::database::{Database, HasStatementCache};
use crate::error::{BoxDynError, Error};
use crate::transaction::Transaction;

Expand Down Expand Up @@ -64,6 +64,23 @@ pub trait Connection: Send {
})
}

/// The number of statements currently cached in the connection.
fn cached_statements_size(&self) -> usize
where
Self::Database: HasStatementCache,
{
0
}

/// Removes all statements from the cache, closing them on the server if
/// needed.
fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>>
where
Self::Database: HasStatementCache,
{
Box::pin(async move { Ok(()) })
}

#[doc(hidden)]
fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>>;

Expand Down
2 changes: 2 additions & 0 deletions sqlx-core/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,5 @@ pub trait HasArguments<'q> {
/// The concrete type used as a buffer for arguments while encoding.
type ArgumentBuffer: Default;
}

pub trait HasStatementCache {}
1 change: 0 additions & 1 deletion sqlx-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ pub mod arguments;
#[macro_use]
pub mod pool;

pub mod caching_connection;
pub mod connection;

#[macro_use]
Expand Down
31 changes: 14 additions & 17 deletions sqlx-core/src/mysql/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use futures_core::future::BoxFuture;
use futures_util::FutureExt;
use hashbrown::HashMap;

use crate::caching_connection::CachingConnection;
use crate::common::StatementCache;
use crate::connection::{Connect, Connection};
use crate::error::Error;
Expand Down Expand Up @@ -46,22 +45,6 @@ pub struct MySqlConnection {
scratch_row_column_names: Arc<HashMap<UStr, usize>>,
}

impl CachingConnection for MySqlConnection {
fn cached_statements_count(&self) -> usize {
self.cache_statement.len()
}

fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> {
Box::pin(async move {
while let Some(statement) = self.cache_statement.remove_lru() {
self.stream.send_packet(StmtClose { statement }).await?;
}

Ok(())
})
}
}

impl Debug for MySqlConnection {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("MySqlConnection").finish()
Expand Down Expand Up @@ -94,6 +77,20 @@ impl Connection for MySqlConnection {
self.stream.wait_until_ready().boxed()
}

fn cached_statements_size(&self) -> usize {
self.cache_statement.len()
}

fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> {
Box::pin(async move {
while let Some(statement) = self.cache_statement.remove_lru() {
self.stream.send_packet(StmtClose { statement }).await?;
}

Ok(())
})
}

#[doc(hidden)]
fn should_flush(&self) -> bool {
!self.stream.wbuf.is_empty()
Expand Down
4 changes: 3 additions & 1 deletion sqlx-core/src/mysql/database.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::database::{Database, HasArguments, HasValueRef};
use crate::database::{Database, HasArguments, HasStatementCache, HasValueRef};
use crate::mysql::value::{MySqlValue, MySqlValueRef};
use crate::mysql::{
MySqlArguments, MySqlConnection, MySqlRow, MySqlTransactionManager, MySqlTypeInfo,
Expand Down Expand Up @@ -33,3 +33,5 @@ impl HasArguments<'_> for MySql {

type ArgumentBuffer = Vec<u8>;
}

impl HasStatementCache for MySql {}
25 changes: 11 additions & 14 deletions sqlx-core/src/postgres/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use futures_core::future::BoxFuture;
use futures_util::{FutureExt, TryFutureExt};
use hashbrown::HashMap;

use crate::caching_connection::CachingConnection;
use crate::common::StatementCache;
use crate::connection::{Connect, Connection};
use crate::error::Error;
Expand Down Expand Up @@ -98,19 +97,6 @@ impl Debug for PgConnection {
}
}

impl CachingConnection for PgConnection {
fn cached_statements_count(&self) -> usize {
self.cache_statement.len()
}

fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> {
Box::pin(async move {
self.cache_statement.clear();
Ok(())
})
}
}

impl Connection for PgConnection {
type Database = Postgres;

Expand All @@ -134,6 +120,17 @@ impl Connection for PgConnection {
self.execute("/* SQLx ping */").map_ok(|_| ()).boxed()
}

fn cached_statements_size(&self) -> usize {
self.cache_statement.len()
}

fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> {
Box::pin(async move {
self.cache_statement.clear();
Ok(())
})
}

#[doc(hidden)]
fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>> {
self.wait_until_ready().boxed()
Expand Down
4 changes: 3 additions & 1 deletion sqlx-core/src/postgres/database.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::database::{Database, HasArguments, HasValueRef};
use crate::database::{Database, HasArguments, HasStatementCache, HasValueRef};
use crate::postgres::arguments::PgArgumentBuffer;
use crate::postgres::value::{PgValue, PgValueRef};
use crate::postgres::{PgArguments, PgConnection, PgRow, PgTransactionManager, PgTypeInfo};
Expand Down Expand Up @@ -32,3 +32,5 @@ impl HasArguments<'_> for Postgres {

type ArgumentBuffer = PgArgumentBuffer;
}

impl HasStatementCache for Postgres {}
25 changes: 11 additions & 14 deletions sqlx-core/src/sqlite/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use futures_util::future;
use hashbrown::HashMap;
use libsqlite3_sys::sqlite3;

use crate::caching_connection::CachingConnection;
use crate::common::StatementCache;
use crate::connection::{Connect, Connection};
use crate::error::Error;
Expand Down Expand Up @@ -49,19 +48,6 @@ impl Debug for SqliteConnection {
}
}

impl CachingConnection for SqliteConnection {
fn cached_statements_count(&self) -> usize {
self.statements.len()
}

fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> {
Box::pin(async move {
self.statements.clear();
Ok(())
})
}
}

impl Connection for SqliteConnection {
type Database = Sqlite;

Expand All @@ -75,6 +61,17 @@ impl Connection for SqliteConnection {
Box::pin(future::ok(()))
}

fn cached_statements_size(&self) -> usize {
self.statements.len()
}

fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>> {
Box::pin(async move {
self.statements.clear();
Ok(())
})
}

#[doc(hidden)]
fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>> {
// For SQLite, FLUSH does effectively nothing
Expand Down
4 changes: 3 additions & 1 deletion sqlx-core/src/sqlite/database.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::database::{Database, HasArguments, HasValueRef};
use crate::database::{Database, HasArguments, HasStatementCache, HasValueRef};
use crate::sqlite::{
SqliteArgumentValue, SqliteArguments, SqliteConnection, SqliteRow, SqliteTransactionManager,
SqliteTypeInfo, SqliteValue, SqliteValueRef,
Expand Down Expand Up @@ -33,3 +33,5 @@ impl<'q> HasArguments<'q> for Sqlite {

type ArgumentBuffer = Vec<SqliteArgumentValue<'q>>;
}

impl HasStatementCache for Sqlite {}
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![cfg_attr(docsrs, feature(doc_cfg))]

pub use sqlx_core::arguments::{Arguments, IntoArguments};
pub use sqlx_core::caching_connection::CachingConnection;
pub use sqlx_core::connection::{Connect, Connection};
pub use sqlx_core::database::{self, Database};
pub use sqlx_core::executor::{Execute, Executor};
Expand Down
6 changes: 3 additions & 3 deletions tests/mysql/mysql.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use futures::TryStreamExt;
use sqlx::mysql::{MySql, MySqlPool, MySqlRow};
use sqlx::{CachingConnection, Connection, Executor, Row};
use sqlx::{Connection, Executor, Row};
use sqlx_test::new;

#[sqlx_macros::test]
Expand Down Expand Up @@ -193,9 +193,9 @@ async fn it_caches_statements() -> anyhow::Result<()> {
assert_eq!(i, val);
}

assert_eq!(1, conn.cached_statements_count());
assert_eq!(1, conn.cached_statements_size());
conn.clear_cached_statements().await?;
assert_eq!(0, conn.cached_statements_count());
assert_eq!(0, conn.cached_statements_size());

Ok(())
}
5 changes: 2 additions & 3 deletions tests/postgres/postgres.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use futures::TryStreamExt;
use sqlx::postgres::PgRow;
use sqlx::postgres::{PgDatabaseError, PgErrorPosition, PgSeverity};
use sqlx::CachingConnection;
use sqlx::{postgres::Postgres, Connection, Executor, PgPool, Row};
use sqlx_test::new;
use std::time::Duration;
Expand Down Expand Up @@ -504,9 +503,9 @@ async fn it_caches_statements() -> anyhow::Result<()> {
assert_eq!(i, val);
}

assert_eq!(1, conn.cached_statements_count());
assert_eq!(1, conn.cached_statements_size());
conn.clear_cached_statements().await?;
assert_eq!(0, conn.cached_statements_count());
assert_eq!(0, conn.cached_statements_size());

Ok(())
}
6 changes: 3 additions & 3 deletions tests/sqlite/sqlite.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use futures::TryStreamExt;
use sqlx::{
query, sqlite::Sqlite, CachingConnection, Connect, Connection, Executor, Row, SqliteConnection,
query, sqlite::Sqlite, Connect, Connection, Executor, Row, SqliteConnection,
SqlitePool,
};
use sqlx_test::new;
Expand Down Expand Up @@ -286,9 +286,9 @@ async fn it_caches_statements() -> anyhow::Result<()> {
assert_eq!(i, val);
}

assert_eq!(1, conn.cached_statements_count());
assert_eq!(1, conn.cached_statements_size());
conn.clear_cached_statements().await?;
assert_eq!(0, conn.cached_statements_count());
assert_eq!(0, conn.cached_statements_size());

Ok(())
}

0 comments on commit aebe8ac

Please sign in to comment.