#[cfg(feature = "diagnostics")]
use crate::db::physical_access::with_physical_access_attribution;
#[cfg(feature = "diagnostics")]
use crate::db::session::sql::measure_sql_stage;
use crate::{
db::{
DbSession, QueryError, executor::StructuralGroupedProjectionResult,
session::sql::SqlStatementResult,
},
traits::CanisterKind,
};
#[cfg(feature = "diagnostics")]
pub(super) fn measure_execute_phase_with_physical_access<T, E>(
run: impl FnOnce() -> Result<T, E>,
) -> ((u64, u64), Result<T, E>) {
let (store_local_instructions, (execute_local_instructions, result)) =
with_physical_access_attribution(|| measure_sql_stage(run));
(
(execute_local_instructions, store_local_instructions),
result,
)
}
pub(super) struct GroupedSqlDiagnosticsCollector<'a> {
#[cfg(feature = "diagnostics")]
response_finalization_local_instructions: &'a mut u64,
#[cfg(not(feature = "diagnostics"))]
_marker: std::marker::PhantomData<&'a mut u64>,
}
impl GroupedSqlDiagnosticsCollector<'_> {
#[cfg(feature = "diagnostics")]
pub(super) const fn new(
response_finalization_local_instructions: &mut u64,
) -> GroupedSqlDiagnosticsCollector<'_> {
GroupedSqlDiagnosticsCollector {
response_finalization_local_instructions,
}
}
pub(super) fn finalize_grouped_sql_statement<C: CanisterKind>(
self,
columns: Vec<String>,
fixed_scales: Vec<Option<u32>>,
result: StructuralGroupedProjectionResult,
) -> Result<SqlStatementResult, QueryError> {
#[cfg(feature = "diagnostics")]
{
let (response_finalization_local_instructions, statement_result) =
measure_sql_stage(|| {
DbSession::<C>::grouped_sql_statement_result_from_result(
columns,
fixed_scales,
result,
)
});
*self.response_finalization_local_instructions =
response_finalization_local_instructions;
statement_result
}
#[cfg(not(feature = "diagnostics"))]
{
let _ = self;
DbSession::<C>::grouped_sql_statement_result_from_result(columns, fixed_scales, result)
}
}
}