Skip to main content

PlanCache

Struct PlanCache 

Source
pub struct PlanCache { /* private fields */ }
Expand description

Thread-safe LRU cache that maps parameterized SQL templates to Arc<LogicalPlan> values.

§Schema invalidation

Call PlanCache::invalidate_schema whenever the schema changes (e.g. a table is created or dropped). This increments an internal generation counter; all existing cache entries are associated with the previous generation and will never be returned again, making them eligible for eviction.

§Example

use oxisql_parse::PlanCache;

let cache = PlanCache::new(64);
let plan1 = cache.plan("SELECT * FROM t WHERE id = 1").unwrap();
let plan2 = cache.plan("SELECT * FROM t WHERE id = 2").unwrap();
// Both queries parameterize to the same template, so only one cache entry
// was created.
assert_eq!(cache.len(), 1);

Implementations§

Source§

impl PlanCache

Source

pub fn new(capacity: usize) -> Self

Create a new cache with the given LRU capacity.

If capacity is 0 it is silently promoted to 1 so the cache is always functional.

Source

pub fn invalidate_schema(&self)

Invalidate all existing cache entries by incrementing the schema generation counter.

Existing entries are not removed immediately; they simply belong to the previous generation and will never be returned, so they age out of the LRU naturally.

Source

pub fn plan(&self, sql: &str) -> Result<Arc<LogicalPlan>, OxiSqlError>

Parse, plan, optimize and cache sql using the default crate::PlannerOptions (decorrelation enabled).

Returns a shared Arc<LogicalPlan> that may be cloned cheaply for concurrent readers.

§Errors

Returns OxiSqlError if parsing or planning fails.

Source

pub fn plan_with( &self, sql: &str, opts: &PlannerOptions, ) -> Result<Arc<LogicalPlan>, OxiSqlError>

Parse, plan, optimize and cache sql using the supplied crate::PlannerOptions.

Two calls with the same sql structure but different literal values will map to the same cache key and share one entry.

§Errors

Returns OxiSqlError if parsing or planning fails.

Source

pub fn len(&self) -> usize

Return the number of currently cached entries.

Source

pub fn is_empty(&self) -> bool

Return true if the cache holds no entries.

Source

pub fn clear(&self)

Evict all cached entries.

Trait Implementations§

Source§

impl Debug for PlanCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.