#[macro_export]
macro_rules! measure_time {
(@unit [$unit:literal, $as_unit:ident]; $tag:expr, $expr:expr) => {
{
let start = ::std::time::Instant::now();
let value = $expr;
$crate::re::tracing::debug!(
::core::concat!("{} in {} ", $unit),
$tag,
start.elapsed().$as_unit(),
);
value
}
};
(@auto $tag:expr, $expr:expr) => {
{
let start = ::std::time::Instant::now();
let value = $expr;
$crate::re::tracing::debug!(
"{} in {}",
$tag,
$crate::_macro_support::AutoUnitDuration::from(start),
);
value
}
};
($tag:expr, $expr:expr) => { $crate::measure_time!(@auto $tag, $expr) };
(MILLI, $tag:expr, $expr:expr) => { $crate::measure_time!(@unit ["ms", as_millis]; $tag, $expr) };
(MICRO, $tag:expr, $expr:expr) => { $crate::measure_time!(@unit ["µs", as_micros]; $tag, $expr) };
(NANO, $tag:expr, $expr:expr) => { $crate::measure_time!(@unit ["ns", as_nanos]; $tag, $expr) };
(SEC, $tag:expr, $expr:expr) => { $crate::measure_time!(@unit ["s", as_secs]; $tag, $expr) };
}
#[macro_export]
macro_rules! postgres_query {
($($body:tt)+) => {
$crate::postgres_query_internal! {$($body)+}
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! postgres_query_internal {
(
@query_impl
($query_fn:ident, $execute_fn:ident -> $from_row:ty),
$sql:expr,
$(#[$fn_meta:meta])*
$fn_vis:vis async fn $fn_name:ident ($($field:tt),* $(,)?)
) => {
$(#[$fn_meta])*
$fn_vis async fn $fn_name<'c, E>(&self, executor: E) -> ::sqlx::Result<$from_row>
where
E: ::sqlx::Executor<'c, Database = ::sqlx::Postgres>,
{
use $crate::re::once_cell::sync::Lazy;
use $crate::sql::SqlTrimBoxed;
static __BOXED_QUERY__: Lazy<Box<str>> = Lazy::new(|| {
$sql.sql_trim_boxed()
});
::sqlx::$query_fn(&**__BOXED_QUERY__)
$(.bind(&self.$field))*
.$execute_fn(executor)
.await
}
};
(
@query
($query_fn:ident, $execute_fn:ident -> $from_row:ty),
$sql:expr,
$(#[$fn_meta:meta])*
$fn_vis:vis async fn $fn_name:ident {$($field:ident),* $(,)?}
) => {
$crate::postgres_query_internal! {
@query_impl
($query_fn, $execute_fn -> $from_row),
$sql,
$(#[$fn_meta])*
$fn_vis async fn $fn_name ($($field),*)
}
};
(
@query
($query_fn:ident, $execute_fn:ident -> $from_row:ty),
$sql:expr,
$(#[$fn_meta:meta])*
$fn_vis:vis async fn $fn_name:ident ($($field:tt),* $(,)?)
) => {
$crate::postgres_query_internal! {
@query_impl
($query_fn, $execute_fn -> $from_row),
$sql,
$(#[$fn_meta])*
$fn_vis async fn $fn_name ($($field),*)
}
};
(
fetch_one($sql:expr) -> $from_row:ty,
$($fn_spec:tt)*
) => {
$crate::postgres_query_internal! {
@query
(query_as, fetch_one -> $from_row),
$sql,
$($fn_spec)*
}
};
(
fetch_one_scalar($sql:expr) -> $from_row:ty,
$($fn_spec:tt)*
) => {
$crate::postgres_query_internal! {
@query
(query_scalar, fetch_one -> $from_row),
$sql,
$($fn_spec)*
}
};
(
fetch_optional($sql:expr) -> $from_row:ty,
$($fn_spec:tt)*
) => {
$crate::postgres_query_internal! {
@query
(query_as, fetch_optional -> ::std::option::Option<$from_row>),
$sql,
$($fn_spec)*
}
};
(
fetch_optional_scalar($sql:expr) -> $from_row:ty,
$($fn_spec:tt)*
) => {
$crate::sqlite_query_internal! {
@query
(query_scalar, fetch_optional -> ::std::option::Option<$from_row>),
$sql,
$($fn_spec)*
}
};
(
fetch_all($sql:expr) -> $from_row:ty,
$($fn_spec:tt)*
) => {
$crate::postgres_query_internal! {
@query
(query_as, fetch_all -> ::std::vec::Vec<$from_row>),
$sql,
$($fn_spec)*
}
};
(
execute($sql:expr),
$($fn_spec:tt)*
) => {
$crate::postgres_query_internal! {
@query
(query, execute -> ::sqlx::postgres::PgQueryResult),
$sql,
$($fn_spec)*
}
};
}
#[macro_export]
macro_rules! with_builder {
($builder:ty => $ty:ty) => {
impl $ty {
pub fn builder() -> $builder {
<$builder as ::core::default::Default>::default()
}
}
};
}
#[macro_export]
macro_rules! generate_read_jemalloc_raw_data {
($vis:vis fn $name:ident) => {
$vis fn $name() -> ::core::option::Option<$crate::jemalloc::info::JemallocRawData> {
use ::std::prelude::*;
use tikv_jemalloc_ctl::{arenas, background_thread, epoch, max_background_threads, stats};
use $crate::jemalloc::info::{JemallocRawData, BackgroundThread};
fn read_background_thread() -> Option<BackgroundThread> {
Some(BackgroundThread {
max: max_background_threads::read().ok()?,
enabled: background_thread::read().ok()?,
})
}
epoch::advance().ok()?;
let value = JemallocRawData {
background_thread: read_background_thread(),
number_of_arenas: arenas::narenas::read().ok()?,
active_bytes: stats::active::read().ok()?,
allocated_bytes: stats::allocated::read().ok()?,
mapped_bytes: stats::mapped::read().ok()?,
metadata_bytes: stats::metadata::read().ok()?,
resident_bytes: stats::resident::read().ok()?,
retained_bytes: stats::retained::read().ok()?,
};
Some(value)
}
};
}