Insert plan into cache
(
&self,
key: PlanCacheKey,
logical_plan: LogicalPlan,
physical_plan: PhysicalPlan,
trace: Option<PlanTrace>,
compilation_time: Duration,
)
| 179 | |
| 180 | /// Insert plan into cache |
| 181 | pub fn insert( |
| 182 | &self, |
| 183 | key: PlanCacheKey, |
| 184 | logical_plan: LogicalPlan, |
| 185 | physical_plan: PhysicalPlan, |
| 186 | trace: Option<PlanTrace>, |
| 187 | compilation_time: Duration, |
| 188 | ) { |
| 189 | let estimated_cost = physical_plan.estimated_cost; |
| 190 | let estimated_rows = physical_plan.estimated_rows; |
| 191 | |
| 192 | let entry = PlanCacheEntry { |
| 193 | logical_plan, |
| 194 | physical_plan, |
| 195 | trace, |
| 196 | compilation_time, |
| 197 | estimated_cost, |
| 198 | estimated_rows, |
| 199 | metadata: CacheEntryMetadata::new(0, CacheLevel::L1).with_ttl(self.default_ttl), |
| 200 | usage_count: 0, |
| 201 | last_used: Instant::now(), |
| 202 | }; |
| 203 | |
| 204 | let size = entry.size_bytes(); |
| 205 | |
| 206 | // Check if we need to evict entries |
| 207 | self.evict_if_needed(size); |
| 208 | |
| 209 | // Insert entry |
| 210 | { |
| 211 | let mut entries = self.entries.write().unwrap(); |
| 212 | entries.insert(key, entry); |
| 213 | } |
| 214 | |
| 215 | { |
| 216 | let mut current_memory = self.current_memory.write().unwrap(); |
| 217 | *current_memory += size; |
| 218 | } |
| 219 | |
| 220 | // Update stats |
| 221 | { |
| 222 | let mut stats = self.stats.write().unwrap(); |
| 223 | stats.current_entries = self.entries.read().unwrap().len(); |
| 224 | stats.current_memory_bytes = *self.current_memory.read().unwrap(); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | fn evict_if_needed(&self, incoming_size: usize) { |
| 229 | let current_memory = *self.current_memory.read().unwrap(); |