use std::sync::Arc;
use crate::ArrowChunk;
use anyhow::{anyhow, Context, Result};
use hypersync_net_types::RollbackGuard;
use polars_arrow::datatypes::SchemaRef;
#[derive(Default, Debug, Clone)]
pub struct ArrowResponseData {
pub blocks: Vec<ArrowBatch>,
pub transactions: Vec<ArrowBatch>,
pub logs: Vec<ArrowBatch>,
pub traces: Vec<ArrowBatch>,
pub decoded_logs: Vec<ArrowBatch>,
}
#[derive(Debug, Clone)]
pub struct QueryResponse<T> {
pub archive_height: Option<u64>,
pub next_block: u64,
pub total_execution_time: u64,
pub data: T,
pub rollback_guard: Option<RollbackGuard>,
}
pub type ArrowResponse = QueryResponse<ArrowResponseData>;
#[derive(Debug, Clone)]
pub struct ArrowBatch {
pub chunk: Arc<ArrowChunk>,
pub schema: SchemaRef,
}
impl ArrowBatch {
pub fn column<T: 'static>(&self, name: &str) -> Result<&T> {
match self
.schema
.fields
.iter()
.enumerate()
.find(|(_, f)| f.name == name)
{
Some((idx, _)) => {
let col = self
.chunk
.columns()
.get(idx)
.context("get column")?
.as_any()
.downcast_ref::<T>()
.with_context(|| anyhow!("cast type of column '{}'", name))?;
Ok(col)
}
None => Err(anyhow!("field {} not found in schema", name)),
}
}
}