hypersync_client/
types.rs1use std::sync::Arc;
2
3use crate::{
4 simple_types::{Block, Event, Log, Trace, Transaction},
5 ArrowChunk, FromArrow,
6};
7use anyhow::{anyhow, Context, Result};
8use hypersync_net_types::RollbackGuard;
9use polars_arrow::datatypes::SchemaRef;
10
11#[derive(Default, Debug, Clone)]
13pub struct ArrowResponseData {
14 pub blocks: Vec<ArrowBatch>,
16 pub transactions: Vec<ArrowBatch>,
18 pub logs: Vec<ArrowBatch>,
20 pub traces: Vec<ArrowBatch>,
22 pub decoded_logs: Vec<ArrowBatch>,
26}
27
28#[derive(Default, Debug, Clone)]
30pub struct ResponseData {
31 pub blocks: Vec<Vec<Block>>,
33 pub transactions: Vec<Vec<Transaction>>,
35 pub logs: Vec<Vec<Log>>,
37 pub traces: Vec<Vec<Trace>>,
39}
40
41impl From<&'_ ArrowResponse> for EventResponse {
42 fn from(arrow_response: &'_ ArrowResponse) -> Self {
43 let r: QueryResponse = arrow_response.into();
44 Self {
45 archive_height: r.archive_height,
46 next_block: r.next_block,
47 total_execution_time: r.total_execution_time,
48 data: vec![r.data.into()],
49 rollback_guard: r.rollback_guard,
50 }
51 }
52}
53
54impl From<&'_ ArrowResponse> for QueryResponse {
55 fn from(arrow_response: &ArrowResponse) -> Self {
56 let blocks = arrow_response
57 .data
58 .blocks
59 .iter()
60 .map(Block::from_arrow)
61 .collect();
62 let transactions = arrow_response
63 .data
64 .transactions
65 .iter()
66 .map(Transaction::from_arrow)
67 .collect();
68 let logs = arrow_response
69 .data
70 .logs
71 .iter()
72 .map(Log::from_arrow)
73 .collect();
74 let traces = arrow_response
75 .data
76 .traces
77 .iter()
78 .map(Trace::from_arrow)
79 .collect();
80
81 QueryResponse {
82 archive_height: arrow_response.archive_height,
83 next_block: arrow_response.next_block,
84 total_execution_time: arrow_response.total_execution_time,
85 data: ResponseData {
86 blocks,
87 transactions,
88 logs,
89 traces,
90 },
91 rollback_guard: arrow_response.rollback_guard.clone(),
92 }
93 }
94}
95
96#[derive(Debug, Clone)]
99pub struct QueryResponse<T = ResponseData> {
100 pub archive_height: Option<u64>,
102 pub next_block: u64,
106 pub total_execution_time: u64,
108 pub data: T,
110 pub rollback_guard: Option<RollbackGuard>,
112}
113
114pub type ArrowResponse = QueryResponse<ArrowResponseData>;
116pub type EventResponse = QueryResponse<Vec<Vec<Event>>>;
118
119#[derive(Debug, Clone)]
121pub struct ArrowBatch {
122 pub chunk: Arc<ArrowChunk>,
124 pub schema: SchemaRef,
126}
127
128impl ArrowBatch {
129 pub fn optional_column<T: 'static>(&self, name: &str) -> Result<Option<&T>> {
131 match self
132 .schema
133 .fields
134 .iter()
135 .enumerate()
136 .find(|(_, f)| f.name == name)
137 {
138 Some((idx, _)) => {
139 let col = self
140 .chunk
141 .columns()
142 .get(idx)
143 .context("get column using index")?;
144 let col = col.as_any().downcast_ref::<T>().with_context(|| {
145 anyhow!(
146 "cast type of column '{}', it was {:?}",
147 name,
148 col.data_type()
149 )
150 })?;
151 Ok(Some(col))
152 }
153 None => Ok(None),
154 }
155 }
156 pub fn column<T: 'static>(&self, name: &str) -> Result<&T> {
158 self.optional_column(name)?
159 .with_context(|| anyhow!("field {} not found in schema", name))
160 }
161}