use super::*;
pub use crate::proto::com::arcadedb::grpc::QueryResult as QueryResultBatch;
#[derive(Debug)]
pub struct QueryResult {
pub records: Vec<GrpcRecord>,
pub execution_time_ms: i64,
}
impl QueryResult {
pub fn len(&self) -> usize {
self.records.len()
}
pub fn is_empty(&self) -> bool {
self.records.is_empty()
}
pub fn iter(&self) -> Iter<'_, GrpcRecord> {
self.records.iter()
}
pub fn rows<'r, T>(&'r self) -> impl Iterator<Item = Result<T, RecordDecodeError>> + 'r
where
T: TryFrom<&'r GrpcRecord, Error = RecordDecodeError> + 'r,
{
self.records.iter().map(T::try_from)
}
pub fn first<T>(&self) -> std::result::Result<Option<T>, RecordDecodeError>
where
T: for<'r> TryFrom<&'r GrpcRecord, Error = RecordDecodeError>,
{
self.records.first().map(T::try_from).transpose()
}
pub fn one<T>(&self) -> Result<T>
where
T: for<'r> TryFrom<&'r GrpcRecord, Error = RecordDecodeError>,
{
self.first()
.map_err(|e| ArcadeDbError::Decode {
what: "first row".into(),
source: e,
})?
.ok_or_else(|| ArcadeDbError::NotFound {
operation: "query".into(),
detail: "result set was empty".into(),
})
}
#[cfg(feature = "rayon")]
pub fn par_rows<T>(&self) -> Result<Vec<T>, RecordDecodeError>
where
T: for<'r> TryFrom<&'r GrpcRecord, Error = RecordDecodeError> + Send,
{
use rayon::prelude::*;
if self.records.len() < par_min_records() {
return self.records.iter().map(T::try_from).collect();
}
self.records
.par_iter()
.map(T::try_from)
.collect::<Result<Vec<_>, _>>()
}
pub fn rows_all<T>(&self) -> std::result::Result<Vec<T>, RecordDecodeError>
where
T: for<'r> TryFrom<&'r GrpcRecord, Error = RecordDecodeError>,
{
self.records.iter().map(T::try_from).collect()
}
}
#[cfg(feature = "rayon")]
pub const DEFAULT_PAR_MIN_RECORDS: usize = 512;
#[cfg(feature = "rayon")]
pub fn par_min_records() -> usize {
static OVERRIDE: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
*OVERRIDE.get_or_init(|| {
std::env::var("ARCADEDB_PAR_MIN_RECORDS")
.ok()
.and_then(|v| v.parse().ok())
.filter(|n| *n > 0)
.unwrap_or(DEFAULT_PAR_MIN_RECORDS)
})
}
impl Deref for QueryResult {
type Target = Vec<GrpcRecord>;
fn deref(&self) -> &Self::Target {
&self.records
}
}
impl IntoIterator for QueryResult {
type Item = GrpcRecord;
type IntoIter = IntoIter<GrpcRecord>;
fn into_iter(self) -> Self::IntoIter {
self.records.into_iter()
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct StreamSummary {
pub batches: u32,
pub rows: usize,
pub running_total_emitted: i64,
pub is_last_batch: bool,
pub truncated: bool,
}