Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions pgdog/src/frontend/router/parser/cache/cache_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -45,8 +44,7 @@ impl Stats {
}
}

/// Newtype wrapper around `Arc<String>` that lets us look up cache entries
/// with any `&str` (e.g. a `QueryWithoutComment`, which derefs to `str`).
/// Newtype wrapper around `Arc<String>` that lets us look up cache entries with any `&str`.
/// Stdlib only provides `Arc<T>: Borrow<T>`, not `Arc<String>: Borrow<str>`.
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub(super) struct CacheKey(pub(super) Arc<String>);
Expand Down Expand Up @@ -130,17 +128,13 @@ impl Cache {
) -> Result<Ast, Error> {
// 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;
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 3 additions & 6 deletions pgdog/src/frontend/router/parser/comment/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<Role>,
Expand Down Expand Up @@ -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()
Expand All @@ -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),
Expand Down
47 changes: 0 additions & 47 deletions pgdog/src/frontend/router/parser/comment/query.rs

This file was deleted.

Loading