2015-08-08 22:33:08 +08:00
|
|
|
//! Prepared statements cache for faster execution.
|
2015-08-02 18:07:49 +08:00
|
|
|
|
2015-12-17 03:10:31 +08:00
|
|
|
use std::cell::RefCell;
|
2015-12-18 03:02:49 +08:00
|
|
|
use std::collections::VecDeque;
|
2015-12-13 18:23:54 +08:00
|
|
|
use {Result, Connection, Statement};
|
2015-08-02 18:07:49 +08:00
|
|
|
|
2015-08-08 22:33:08 +08:00
|
|
|
/// Prepared statements cache.
|
2015-08-02 18:07:49 +08:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct StatementCache<'conn> {
|
2015-12-17 02:42:03 +08:00
|
|
|
conn: &'conn Connection,
|
2015-12-18 03:02:49 +08:00
|
|
|
cache: VecDeque<Statement<'conn>>, // back = LRU
|
2015-08-02 18:07:49 +08:00
|
|
|
}
|
|
|
|
|
2015-12-17 02:42:03 +08:00
|
|
|
pub struct CachedStatement<'conn> {
|
2015-12-18 03:02:49 +08:00
|
|
|
stmt: Option<Statement<'conn>>,
|
2015-12-17 03:10:31 +08:00
|
|
|
cache: RefCell<StatementCache<'conn>>,
|
2015-12-18 03:02:49 +08:00
|
|
|
pub cacheable: bool,
|
2015-12-17 02:42:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'conn> Drop for CachedStatement<'conn> {
|
|
|
|
#[allow(unused_must_use)]
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if self.cacheable {
|
2015-12-18 03:02:49 +08:00
|
|
|
self.cache.borrow_mut().release(self.stmt.take().unwrap());
|
2015-12-17 02:42:03 +08:00
|
|
|
} else {
|
2015-12-18 03:02:49 +08:00
|
|
|
self.stmt.take().unwrap().finalize();
|
2015-12-17 02:42:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-02 18:07:49 +08:00
|
|
|
impl<'conn> StatementCache<'conn> {
|
2015-08-08 22:33:08 +08:00
|
|
|
/// Create a statement cache.
|
2015-12-13 18:23:54 +08:00
|
|
|
pub fn new(conn: &'conn Connection, capacity: usize) -> StatementCache<'conn> {
|
|
|
|
StatementCache {
|
|
|
|
conn: conn,
|
2015-12-18 03:02:49 +08:00
|
|
|
cache: VecDeque::with_capacity(capacity),
|
2015-12-13 18:23:54 +08:00
|
|
|
}
|
2015-08-02 18:07:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Search the cache for a prepared-statement object that implements `sql`.
|
|
|
|
// If no such prepared-statement can be found, allocate and prepare a new one.
|
2015-12-07 02:57:20 +08:00
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Will return `Err` if no cached statement can be found and the underlying SQLite prepare call fails.
|
2015-12-13 18:23:54 +08:00
|
|
|
pub fn get(&mut self, sql: &str) -> Result<Statement<'conn>> {
|
2015-12-18 03:02:49 +08:00
|
|
|
match self.cache.iter().rposition(|entry| entry.eq(sql)) {
|
|
|
|
Some(index) => Ok(self.cache.swap_remove_front(index).unwrap()), // FIXME Not LRU compliant
|
2015-12-13 18:23:54 +08:00
|
|
|
_ => self.conn.prepare(sql),
|
2015-08-02 18:07:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If `discard` is true, then the statement is deleted immediately.
|
|
|
|
/// Otherwise it is added to the LRU list and may be returned
|
|
|
|
/// by a subsequent call to `get()`.
|
2015-12-07 02:57:20 +08:00
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Will return `Err` if `stmt` (or the already cached statement implementing the same SQL) statement is `discard`ed
|
|
|
|
/// and the underlying SQLite finalize call fails.
|
2015-12-18 03:02:49 +08:00
|
|
|
fn release(&mut self, mut stmt: Statement<'conn>) {
|
|
|
|
if self.cache.capacity() == self.cache.len() { // is full
|
|
|
|
self.cache.pop_back(); // LRU dropped
|
2015-08-02 18:07:49 +08:00
|
|
|
}
|
2015-12-07 02:57:20 +08:00
|
|
|
stmt.reset_if_needed();
|
2015-12-16 04:49:59 +08:00
|
|
|
stmt.clear_bindings();
|
2015-12-18 03:02:49 +08:00
|
|
|
self.cache.push_front(stmt)
|
2015-08-02 18:07:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Flush the prepared statement cache
|
2015-12-16 04:49:59 +08:00
|
|
|
pub fn clear(&mut self) {
|
2015-08-02 18:07:49 +08:00
|
|
|
self.cache.clear();
|
|
|
|
}
|
|
|
|
|
2015-12-16 04:49:59 +08:00
|
|
|
/// Return current cache size.
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.cache.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return maximum cache size.
|
|
|
|
pub fn capacity(&self) -> usize {
|
|
|
|
self.cache.capacity()
|
2015-08-02 18:07:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2015-12-13 18:23:54 +08:00
|
|
|
use Connection;
|
2015-08-02 18:07:49 +08:00
|
|
|
use super::StatementCache;
|
|
|
|
|
2015-12-13 18:23:54 +08:00
|
|
|
#[test]
|
2015-08-02 18:07:49 +08:00
|
|
|
fn test_cache() {
|
2015-12-13 18:23:54 +08:00
|
|
|
let db = Connection::open_in_memory().unwrap();
|
2015-12-18 03:02:49 +08:00
|
|
|
let mut cache = StatementCache::new(&db, 15);
|
2015-12-16 04:49:59 +08:00
|
|
|
assert_eq!(0, cache.len());
|
2015-12-18 03:02:49 +08:00
|
|
|
assert_eq!(15, cache.capacity());
|
2015-12-16 04:49:59 +08:00
|
|
|
|
2015-08-02 18:07:49 +08:00
|
|
|
let sql = "PRAGMA schema_version";
|
|
|
|
let mut stmt = cache.get(sql).unwrap();
|
2015-12-16 04:49:59 +08:00
|
|
|
assert_eq!(0, cache.len());
|
2015-12-18 03:02:49 +08:00
|
|
|
assert_eq!(0,
|
|
|
|
stmt.query(&[]).unwrap().get_expected_row().unwrap().get::<i64>(0));
|
2015-12-16 04:49:59 +08:00
|
|
|
|
2015-12-13 18:23:54 +08:00
|
|
|
// println!("NEW {:?}", stmt);
|
2015-12-18 03:02:49 +08:00
|
|
|
cache.release(stmt);
|
2015-12-16 04:49:59 +08:00
|
|
|
assert_eq!(1, cache.len());
|
|
|
|
|
2015-08-02 18:07:49 +08:00
|
|
|
stmt = cache.get(sql).unwrap();
|
2015-12-16 04:49:59 +08:00
|
|
|
assert_eq!(0, cache.len());
|
2015-12-18 03:02:49 +08:00
|
|
|
assert_eq!(0,
|
|
|
|
stmt.query(&[]).unwrap().get_expected_row().unwrap().get::<i64>(0));
|
2015-12-16 04:49:59 +08:00
|
|
|
|
2015-12-13 18:23:54 +08:00
|
|
|
// println!("CACHED {:?}", stmt);
|
2015-12-18 03:02:49 +08:00
|
|
|
cache.release(stmt);
|
|
|
|
assert_eq!(1, cache.len());
|
2015-12-16 04:49:59 +08:00
|
|
|
|
|
|
|
cache.clear();
|
|
|
|
assert_eq!(0, cache.len());
|
2015-12-18 03:02:49 +08:00
|
|
|
assert_eq!(15, cache.capacity());
|
2015-08-02 18:07:49 +08:00
|
|
|
}
|
|
|
|
}
|