refactor: address feedback

pull/24376/head
Edd Robinson 2022-01-14 10:41:27 +00:00
parent 9283432a0f
commit cdb4f43d62
3 changed files with 15 additions and 8 deletions

View File

@ -242,7 +242,7 @@ impl QueryDatabase for QueryCatalogAccess {
// When the query token is dropped the query entry's completion time
// will be set.
let entry = self.query_log.push(query_type, query_text);
QueryCompletedToken::new(move || self.query_log.set_completed(Arc::clone(&entry)))
QueryCompletedToken::new(move || self.query_log.set_completed(entry))
}
}

View File

@ -9,6 +9,9 @@ use std::{
use parking_lot::Mutex;
use time::{Time, TimeProvider};
// The query duration used for queries still running.
const UNCOMPLETED_DURATION: i64 = -1;
/// Information about a single query that was executed
#[derive(Debug)]
pub struct QueryLogEntry {
@ -33,7 +36,7 @@ impl QueryLogEntry {
query_type,
query_text,
issue_time,
query_completed_duration: (-1_i64).into(),
query_completed_duration: UNCOMPLETED_DURATION.into(),
}
}
@ -42,7 +45,7 @@ impl QueryLogEntry {
.query_completed_duration
.load(atomic::Ordering::Relaxed)
{
-1 => None,
UNCOMPLETED_DURATION => None,
d => Some(Duration::from_nanos(d as u64)),
}
}
@ -54,7 +57,7 @@ impl QueryLogEntry {
}
}
/// Stores a fixed number `QueryExcutions` -- handles locking
/// Stores a fixed number `QueryExecutions` -- handles locking
/// internally so can be shared across multiple
#[derive(Debug)]
pub struct QueryLog {

View File

@ -81,7 +81,7 @@ pub trait QueryChunkMeta: Sized {
/// a `QueryDatabase`. It is used to trigger side-effects (such as query timing)
/// on query completion.
pub struct QueryCompletedToken<'a> {
f: Box<dyn Fn() + Send + 'a>,
f: Option<Box<dyn FnOnce() + Send + 'a>>,
}
impl<'a> Debug for QueryCompletedToken<'a> {
@ -91,14 +91,18 @@ impl<'a> Debug for QueryCompletedToken<'a> {
}
impl<'a> QueryCompletedToken<'a> {
pub fn new(f: impl Fn() + Send + 'a) -> Self {
Self { f: Box::new(f) }
pub fn new(f: impl FnOnce() + Send + 'a) -> Self {
Self {
f: Some(Box::new(f)),
}
}
}
impl<'a> Drop for QueryCompletedToken<'a> {
fn drop(&mut self) {
(self.f)()
if let Some(f) = self.f.take() {
(f)()
}
}
}