diff --git a/pgdog/src/frontend/router/parser/cache/cache_impl.rs b/pgdog/src/frontend/router/parser/cache/cache_impl.rs index bc0b5052d..72b282a92 100644 --- a/pgdog/src/frontend/router/parser/cache/cache_impl.rs +++ b/pgdog/src/frontend/router/parser/cache/cache_impl.rs @@ -5,7 +5,6 @@ use pg_query::normalize; use pgdog_config::QueryParserEngine; use std::borrow::Borrow; use std::collections::HashMap; -use std::ops::Deref; use std::time::Duration; use parking_lot::{Mutex, RawMutex}; @@ -45,8 +44,7 @@ impl Stats { } } -/// Newtype wrapper around `Arc` that lets us look up cache entries -/// with any `&str` (e.g. a `QueryWithoutComment`, which derefs to `str`). +/// Newtype wrapper around `Arc` that lets us look up cache entries with any `&str`. /// Stdlib only provides `Arc: Borrow`, not `Arc: Borrow`. #[derive(Clone, Debug, Hash, PartialEq, Eq)] pub(super) struct CacheKey(pub(super) Arc); @@ -130,17 +128,13 @@ impl Cache { ) -> Result { // Separate query from comment, if one is present. let query_and_comment = parse_edge_comment(query.query(), &ctx.sharding_schema)?; - let cache_key = &query_and_comment.query; { let mut guard = self.inner.lock(); - let ast = guard - .queries - .get_mut(cache_key.deref()) // Use the query without comment as the cache key. - .map(|entry| { - entry.stats.lock().hits += 1; // No contention on this. - entry.clone() - }); + let ast = guard.queries.get_mut(query_and_comment.query).map(|entry| { + entry.stats.lock().hits += 1; // No contention on this. + entry.clone() + }); if let Some(mut ast) = ast { guard.stats.hits += 1; ast.comment_role = query_and_comment.role; @@ -154,7 +148,7 @@ impl Cache { let mut entry = Ast::with_context( &AstQuery { original_query: query, - query_without_comment: &query_and_comment.query, + query_without_comment: query_and_comment.query, }, ctx, prepared_statements, @@ -194,7 +188,7 @@ impl Cache { let mut entry = Ast::with_context( &AstQuery { original_query: query, - query_without_comment: &query_and_comment.query, + query_without_comment: query_and_comment.query, }, ctx, prepared_statements, diff --git a/pgdog/src/frontend/router/parser/comment/mod.rs b/pgdog/src/frontend/router/parser/comment/mod.rs index d6c25856f..01768936e 100644 --- a/pgdog/src/frontend/router/parser/comment/mod.rs +++ b/pgdog/src/frontend/router/parser/comment/mod.rs @@ -1,12 +1,9 @@ mod directive; -mod query; mod strip; #[cfg(test)] mod tests; -pub use query::QueryWithoutComment; - use crate::backend::ShardingSchema; use crate::config::database::Role; @@ -16,7 +13,7 @@ use strip::{leading_block_comment, trailing_block_comment}; #[derive(Default, Debug, Clone)] pub struct QueryAndComment<'a> { - pub query: QueryWithoutComment<'a>, + pub query: &'a str, #[cfg(test)] pub comment: String, pub role: Option, @@ -46,7 +43,7 @@ pub fn parse_edge_comment<'a>( if leading.is_none() && trailing.is_none() { return Ok(QueryAndComment { - query: QueryWithoutComment::Original(query), + query, #[cfg(test)] comment: String::new(), ..Default::default() @@ -70,7 +67,7 @@ pub fn parse_edge_comment<'a>( } Ok(QueryAndComment { - query: QueryWithoutComment::Stripped(stripped.to_string()), + query: stripped, #[cfg(test)] comment: match (leading, trailing) { (Some(l), Some(t)) => format!("{} {}", l, t), diff --git a/pgdog/src/frontend/router/parser/comment/query.rs b/pgdog/src/frontend/router/parser/comment/query.rs deleted file mode 100644 index 58f239f87..000000000 --- a/pgdog/src/frontend/router/parser/comment/query.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::borrow::Borrow; -use std::ops::Deref; - -/// A query with any leading/trailing block comment stripped. -/// -/// Borrows the original query when no stripping was needed, and owns the -/// trimmed copy otherwise. -#[derive(Debug, Clone)] -pub enum QueryWithoutComment<'a> { - Original(&'a str), - Stripped(String), -} - -impl PartialEq for QueryWithoutComment<'_> { - fn eq(&self, other: &Self) -> bool { - self.deref() == other.deref() - } -} - -impl PartialEq<&str> for QueryWithoutComment<'_> { - fn eq(&self, other: &&str) -> bool { - self.deref() == *other - } -} - -impl Borrow for QueryWithoutComment<'_> { - fn borrow(&self) -> &str { - self.deref() - } -} - -impl Default for QueryWithoutComment<'_> { - fn default() -> Self { - QueryWithoutComment::Original("") - } -} - -impl<'a> Deref for QueryWithoutComment<'a> { - type Target = str; - - fn deref(&self) -> &Self::Target { - match self { - Self::Original(original) => original, - Self::Stripped(stripped) => stripped.as_str(), - } - } -}