-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Julius de Bruijn
committed
Jun 24, 2020
1 parent
72c4e04
commit 54d6828
Showing
14 changed files
with
173 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use futures_core::future::BoxFuture; | ||
|
||
use crate::error::Error; | ||
|
||
/// A connection that is capable of caching prepared statements. | ||
pub trait CachingConnection: Send { | ||
/// The number of statements currently cached in the connection. | ||
fn cached_statements_count(&self) -> usize; | ||
|
||
/// Removes all statements from the cache, closing them on the server if | ||
/// needed. | ||
fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod statement_cache; | ||
|
||
pub(crate) use statement_cache::StatementCache; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use lru_cache::LruCache; | ||
|
||
/// A cache for prepared statements. When full, the least recently used | ||
/// statement gets removed. | ||
#[derive(Debug)] | ||
pub struct StatementCache { | ||
inner: LruCache<String, u32>, | ||
} | ||
|
||
impl StatementCache { | ||
/// Create a new cache with the given capacity. | ||
pub fn new(capacity: usize) -> Self { | ||
Self { | ||
inner: LruCache::new(capacity), | ||
} | ||
} | ||
|
||
/// Returns a mutable reference to the value corresponding to the given key | ||
/// in the cache, if any. | ||
pub fn get_mut(&mut self, k: &str) -> Option<&mut u32> { | ||
self.inner.get_mut(k) | ||
} | ||
|
||
/// Inserts a new statement to the cache, returning the least recently used | ||
/// statement id if the cache is full, or if inserting with an existing key, | ||
/// the replaced existing statement. | ||
pub fn insert(&mut self, k: &str, v: u32) -> Option<u32> { | ||
let mut lru_item = None; | ||
|
||
if self.inner.capacity() == self.len() && !self.inner.contains_key(k) { | ||
lru_item = self.remove_lru(); | ||
} else if self.inner.contains_key(k) { | ||
lru_item = self.inner.remove(k); | ||
} | ||
|
||
self.inner.insert(k.into(), v); | ||
|
||
lru_item | ||
} | ||
|
||
/// The number of statements in the cache. | ||
pub fn len(&self) -> usize { | ||
self.inner.len() | ||
} | ||
|
||
/// Removes the least recently used item from the cache. | ||
pub fn remove_lru(&mut self) -> Option<u32> { | ||
self.inner.remove_lru().map(|(_, v)| v) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use crate::io::Encode; | ||
use crate::mysql::protocol::Capabilities; | ||
|
||
// https://dev.mysql.com/doc/internals/en/com-stmt-close.html | ||
|
||
#[derive(Debug)] | ||
pub struct StmtClose { | ||
pub statement: u32, | ||
} | ||
|
||
impl Encode<'_, Capabilities> for StmtClose { | ||
fn encode_with(&self, buf: &mut Vec<u8>, _: Capabilities) { | ||
buf.push(0x19); // COM_STMT_CLOSE | ||
buf.extend(&self.statement.to_le_bytes()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters