1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//! Query Plan Cache
//!
//! LRU cache for compiled query plans with TTL validation.
//!
//! # Features
//!
//! - LRU eviction policy
//! - TTL-based invalidation
//! - Thread-safe access
//! - Statistics tracking
use std::collections::HashMap;
use std::time::{Duration, Instant};
use super::{CacheStats, QueryPlan};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CacheEntryState {
Inactive,
Active,
}
/// A cached query plan with metadata
#[derive(Debug, Clone)]
pub struct CachedPlan {
/// The compiled query plan
pub plan: QueryPlan,
/// When this plan was cached
pub cached_at: Instant,
/// Number of times this plan was accessed
pub access_count: u64,
/// Last access time
pub last_accessed: Instant,
/// Query shape key used for parameter-insensitive cache grouping.
pub shape_key: Option<String>,
/// Last exact query string stored in this slot.
pub exact_query: Option<std::sync::Arc<str>>,
/// Runtime activation state inspired by Mongo's active/inactive plan cache.
pub state: CacheEntryState,
/// Moving expectation for storage reads (`rows_scanned`) on this shape.
pub expected_rows_scanned: Option<u64>,
/// Last observed runtime reads for the shape.
pub last_observed_rows_scanned: Option<u64>,
/// Number of literal binds expected by the cached shape skeleton.
pub parameter_count: usize,
/// When true, the next cache lookup forces a fresh replan.
pub replan_pending: bool,
/// Monotonic recency stamp assigned by the owning [`PlanCache`] on every
/// insert and hit. The least-recently-used entry is the one with the
/// smallest stamp; this replaces the old `Vec<String>` scan so promotion is
/// O(1) and allocation-free on the cache-hit hot path.
lru_seq: u64,
}
impl CachedPlan {
/// Create a new cached plan
pub fn new(plan: QueryPlan) -> Self {
let now = Instant::now();
Self {
plan,
cached_at: now,
access_count: 0,
last_accessed: now,
shape_key: None,
exact_query: None,
state: CacheEntryState::Inactive,
expected_rows_scanned: None,
last_observed_rows_scanned: None,
parameter_count: 0,
replan_pending: false,
lru_seq: 0,
}
}
pub fn with_shape_key(mut self, shape_key: impl Into<String>) -> Self {
self.shape_key = Some(shape_key.into());
self
}
pub fn with_exact_query(mut self, query: impl Into<String>) -> Self {
let s: String = query.into();
self.exact_query = Some(std::sync::Arc::<str>::from(s));
self
}
pub fn with_parameter_count(mut self, parameter_count: usize) -> Self {
self.parameter_count = parameter_count;
self
}
/// Check if the plan has expired
pub fn is_expired(&self, ttl: Duration) -> bool {
self.cached_at.elapsed() > ttl
}
/// Record an access
pub fn touch(&mut self) {
self.access_count += 1;
self.last_accessed = Instant::now();
}
pub fn matches_exact_query(&self, query: &str) -> bool {
self.exact_query.as_deref() == Some(query)
}
pub fn needs_replan(&self) -> bool {
self.replan_pending
}
pub fn record_observation(&mut self, rows_scanned: u64) {
self.last_observed_rows_scanned = Some(rows_scanned);
match (self.state, self.expected_rows_scanned) {
(_, None) => {
self.expected_rows_scanned = Some(rows_scanned.max(1));
self.replan_pending = false;
}
(CacheEntryState::Inactive, Some(expected)) => {
if rows_scanned <= expected {
self.state = CacheEntryState::Active;
self.expected_rows_scanned = Some(rows_scanned.max(1));
self.replan_pending = false;
} else {
self.expected_rows_scanned = Some(rows_scanned.min(expected.saturating_mul(2)));
}
}
(CacheEntryState::Active, Some(expected)) => {
if rows_scanned > expected.saturating_mul(10).max(10) {
self.state = CacheEntryState::Inactive;
self.expected_rows_scanned = Some(rows_scanned.max(1));
self.replan_pending = true;
} else if rows_scanned < expected {
self.expected_rows_scanned = Some(rows_scanned.max(1));
self.replan_pending = false;
}
}
}
}
}
/// LRU cache for query plans
///
/// Recency is tracked by a monotonic `clock` counter stamped onto each entry's
/// `lru_seq` on insert and hit, rather than a `Vec<String>` that had to be
/// linearly scanned (`position()` + `remove()`) on every promotion and removal.
/// Promotion (the cache-hit hot path) is now O(1) and allocation-free; eviction
/// picks the entry with the smallest stamp, which is the exact same victim the
/// old front-of-`Vec` policy would have chosen for any given access sequence.
pub struct PlanCache {
/// Cached plans by key
entries: HashMap<String, CachedPlan>,
/// Monotonic recency clock; larger means more recently used.
clock: u64,
/// Maximum cache size
capacity: usize,
/// Time-to-live for entries
ttl: Duration,
/// Cache statistics
hits: u64,
misses: u64,
}
impl PlanCache {
/// Create a new plan cache with the given capacity
///
/// A capacity of 0 is meaningless and unreachable from every call site
/// today. If one ever appears, the LRU keeps a single entry rather than
/// evicting forever (the pre-#2012 code looped indefinitely on it).
pub fn new(capacity: usize) -> Self {
debug_assert!(capacity > 0, "plan cache capacity must be positive");
Self {
entries: HashMap::with_capacity(capacity),
clock: 0,
capacity,
ttl: Duration::from_secs(3600), // 1 hour default TTL
hits: 0,
misses: 0,
}
}
/// Advance the recency clock and return the fresh stamp.
fn next_seq(&mut self) -> u64 {
self.clock += 1;
self.clock
}
/// Set the TTL for cache entries
pub fn with_ttl(mut self, ttl: Duration) -> Self {
self.ttl = ttl;
self
}
/// Read-only cache probe — no LRU promotion, no mutation.
///
/// Use this with a `read()` lock in the hot query path so concurrent
/// readers don't serialize on a write lock. Falls back to `None` when
/// the entry is expired or has a pending replan; the caller must then
/// re-acquire a `write()` lock and call `get()` to handle eviction and
/// miss insertion.
pub fn peek(&self, key: &str) -> Option<&CachedPlan> {
let entry = self.entries.get(key)?;
if entry.needs_replan() || entry.is_expired(self.ttl) {
return None;
}
Some(entry)
}
/// Get a cached plan by key
pub fn get(&mut self, key: &str) -> Option<&CachedPlan> {
if self
.entries
.get(key)
.is_some_and(|entry| entry.needs_replan())
{
self.remove(key);
self.misses += 1;
return None;
}
// Check if entry exists and is not expired
let expired = match self.entries.get(key) {
Some(entry) => entry.is_expired(self.ttl),
None => {
self.misses += 1;
return None;
}
};
if expired {
// Remove expired entry
self.remove(key);
self.misses += 1;
return None;
}
// Hit: stamp the entry as most-recently-used. O(1), no allocation.
let seq = self.next_seq();
if let Some(entry) = self.entries.get_mut(key) {
entry.touch();
entry.lru_seq = seq;
}
self.hits += 1;
self.entries.get(key)
}
/// Insert a plan into the cache
pub fn insert(&mut self, key: String, mut plan: CachedPlan) {
// Remove existing entry if present
if self.entries.contains_key(&key) {
self.remove(&key);
}
// Evict if at capacity
while self.entries.len() >= self.capacity && !self.entries.is_empty() {
self.evict_lru();
}
// Insert new entry, stamped as most-recently-used.
plan.lru_seq = self.next_seq();
self.entries.insert(key, plan);
}
/// Remove an entry from the cache
pub fn remove(&mut self, key: &str) -> Option<CachedPlan> {
self.entries.remove(key)
}
/// Invalidate entries matching a predicate
pub fn invalidate<F>(&mut self, predicate: F)
where
F: Fn(&str) -> bool,
{
let keys_to_remove: Vec<String> = self
.entries
.keys()
.filter(|k| predicate(k))
.cloned()
.collect();
for key in keys_to_remove {
self.remove(&key);
}
}
/// Clear all entries
pub fn clear(&mut self) {
self.entries.clear();
}
/// Get cache statistics
pub fn stats(&self) -> CacheStats {
CacheStats {
hits: self.hits,
misses: self.misses,
size: self.entries.len(),
capacity: self.capacity,
}
}
/// Evict the least recently used entry (smallest recency stamp).
fn evict_lru(&mut self) {
let victim = self
.entries
.iter()
.min_by_key(|(_, entry)| entry.lru_seq)
.map(|(key, _)| key.clone());
if let Some(key) = victim {
self.remove(&key);
}
}
/// Prune expired entries
pub fn prune_expired(&mut self) {
let expired: Vec<String> = self
.entries
.iter()
.filter(|(_, v)| v.is_expired(self.ttl))
.map(|(k, _)| k.clone())
.collect();
for key in expired {
self.remove(&key);
}
}
pub fn record_observation(&mut self, key: &str, rows_scanned: u64) {
if let Some(entry) = self.entries.get_mut(key) {
entry.record_observation(rows_scanned);
}
}
}
impl Default for PlanCache {
fn default() -> Self {
Self::new(1000)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::storage::query::ast::{Projection, QueryExpr, TableQuery};
use crate::storage::query::planner::cost::PlanCost;
fn make_test_plan() -> QueryPlan {
QueryPlan::new(
QueryExpr::Table(TableQuery {
table: "test".to_string(),
source: None,
alias: None,
select_items: Vec::new(),
columns: vec![Projection::All],
where_expr: None,
filter: None,
group_by_exprs: Vec::new(),
group_by: Vec::new(),
having_expr: None,
having: None,
order_by: vec![],
limit: None,
limit_param: None,
offset: None,
offset_param: None,
expand: None,
as_of: None,
sessionize: None,
distinct: false,
}),
QueryExpr::Table(TableQuery {
table: "test".to_string(),
source: None,
alias: None,
select_items: Vec::new(),
columns: vec![Projection::All],
where_expr: None,
filter: None,
group_by_exprs: Vec::new(),
group_by: Vec::new(),
having_expr: None,
having: None,
order_by: vec![],
limit: None,
limit_param: None,
offset: None,
offset_param: None,
expand: None,
as_of: None,
sessionize: None,
distinct: false,
}),
PlanCost::default(),
)
}
#[test]
fn test_cache_insert_and_get() {
let mut cache = PlanCache::new(10);
let plan = CachedPlan::new(make_test_plan());
cache.insert("query1".to_string(), plan);
assert!(cache.get("query1").is_some());
assert!(cache.get("query2").is_none());
}
#[test]
fn test_cache_lru_eviction() {
let mut cache = PlanCache::new(2);
cache.insert("q1".to_string(), CachedPlan::new(make_test_plan()));
cache.insert("q2".to_string(), CachedPlan::new(make_test_plan()));
// Access q1 to make it most recently used
let _ = cache.get("q1");
// Insert q3 - should evict q2 (LRU)
cache.insert("q3".to_string(), CachedPlan::new(make_test_plan()));
assert!(cache.get("q1").is_some());
assert!(cache.get("q2").is_none()); // Evicted
assert!(cache.get("q3").is_some());
}
#[test]
fn test_cache_stats() {
let mut cache = PlanCache::new(10);
cache.insert("q1".to_string(), CachedPlan::new(make_test_plan()));
let _ = cache.get("q1"); // Hit
let _ = cache.get("q2"); // Miss
let _ = cache.get("q1"); // Hit
let stats = cache.stats();
assert_eq!(stats.hits, 2);
assert_eq!(stats.misses, 1);
}
#[test]
fn test_cache_invalidation() {
let mut cache = PlanCache::new(10);
cache.insert(
"hosts_query1".to_string(),
CachedPlan::new(make_test_plan()),
);
cache.insert(
"hosts_query2".to_string(),
CachedPlan::new(make_test_plan()),
);
cache.insert("users_query".to_string(), CachedPlan::new(make_test_plan()));
// Invalidate all hosts queries
cache.invalidate(|k| k.starts_with("hosts_"));
assert!(cache.get("hosts_query1").is_none());
assert!(cache.get("hosts_query2").is_none());
assert!(cache.get("users_query").is_some());
}
#[test]
fn lru_eviction_picks_least_recently_used_victim() {
// Regression guard for the #2011 recency-counter LRU: eviction must pick
// the same victim the old front-of-`Vec` policy would have, for a given
// access sequence. `peek` is used for verification so it does not itself
// perturb recency.
let mut cache = PlanCache::new(3);
for k in ["a", "b", "c"] {
cache.insert(k.to_string(), CachedPlan::new(make_test_plan()));
}
// Touch a and c, leaving b as the least-recently-used entry.
assert!(cache.get("a").is_some());
assert!(cache.get("c").is_some());
// Inserting a fourth entry at capacity evicts b.
cache.insert("d".to_string(), CachedPlan::new(make_test_plan()));
assert!(
cache.peek("b").is_none(),
"b was least-recently-used and must be the eviction victim"
);
assert!(cache.peek("a").is_some());
assert!(cache.peek("c").is_some());
assert!(cache.peek("d").is_some());
}
#[test]
fn active_entry_forces_replan_after_large_regression() {
let mut cache = PlanCache::new(10);
cache.insert("q1".to_string(), CachedPlan::new(make_test_plan()));
cache.record_observation("q1", 10);
cache.record_observation("q1", 10);
cache.record_observation("q1", 500);
assert!(cache.get("q1").is_none());
}
}