MCPcopy Create free account
hub / github.com/erans/pgsqlite / prepare_and_cache_enhanced

Method prepare_and_cache_enhanced

src/cache/enhanced_statement_pool.rs:85–153  ·  view source on GitHub ↗

Prepare a statement with enhanced caching based on query patterns

(
        &self,
        conn: &'conn Connection,
        query: &str,
    )

Source from the content-addressed store, hash-verified

83
84 /// Prepare a statement with enhanced caching based on query patterns
85 pub fn prepare_and_cache_enhanced<'conn>(
86 &self,
87 conn: &'conn Connection,
88 query: &str,
89 ) -> Result<(Statement<'conn>, StatementMetadata), rusqlite::Error> {
90 debug!("Enhanced statement pool preparing query: {}", query);
91 let start_time = Instant::now();
92
93 // Generate normalized fingerprint for cache key
94 let cache_key = self.generate_cache_key(query);
95
96 // Update total queries stat
97 {
98 let mut stats = self.stats.write().unwrap();
99 stats.total_queries += 1;
100 }
101
102 // Check cache first
103 if let Some(metadata) = self.get_cached_metadata(&cache_key) {
104 // Cache hit - prepare statement with cached metadata
105 let stmt = conn.prepare(query)?;
106 self.record_cache_hit(&cache_key);
107 debug!("Statement cache hit for query: {}", cache_key);
108 return Ok((stmt, metadata));
109 }
110
111 // Cache miss - analyze query pattern and prepare statement
112 let (pattern, hints) = {
113 let mut optimizer = self.pattern_optimizer.write().unwrap();
114 optimizer.analyze_query(query)
115 };
116
117 // Decide whether to cache based on optimization hints
118 let should_cache = self.should_cache_query(&pattern, &hints);
119
120 if should_cache {
121 debug!("Preparing and caching statement for pattern: {:?}", pattern);
122
123 // Prepare statement and extract metadata
124 let stmt = conn.prepare(query)?;
125 let metadata = self.extract_enhanced_metadata(&stmt, query, &pattern, &hints)?;
126
127 // Cache the statement metadata
128 self.cache_statement_metadata(cache_key.clone(), metadata.clone(), pattern.clone(), hints);
129
130 // Record preparation time
131 let preparation_time = start_time.elapsed();
132 {
133 let mut stats = self.stats.write().unwrap();
134 stats.cache_misses += 1;
135 stats.total_preparation_time_ms += preparation_time.as_millis() as u64;
136 }
137
138 info!("Cached new statement for pattern {:?} in {}ms", pattern, preparation_time.as_millis());
139 Ok((stmt, metadata))
140 } else {
141 // Don't cache this query - just prepare it
142 debug!("Not caching query due to optimization hints: {:?}", hints);

Callers 2

Calls 9

get_cached_metadataMethod · 0.80
record_cache_hitMethod · 0.80
should_cache_queryMethod · 0.80
generate_cache_keyMethod · 0.45
analyze_queryMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected