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
impl PlanCache
Sourcepub fn new(capacity: usize) -> Self
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.
Sourcepub fn invalidate_schema(&self)
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.
Sourcepub fn plan(&self, sql: &str) -> Result<Arc<LogicalPlan>, OxiSqlError>
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.
Sourcepub fn plan_with(
&self,
sql: &str,
opts: &PlannerOptions,
) -> Result<Arc<LogicalPlan>, OxiSqlError>
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.