taos_query/iter/mod.rs
1use super::*;
2
3// type DeserializeIter<'b, B, T> =
4// std::iter::Map<RowsIter<'b, B>, fn(RowInBlock<'b, B>) -> Result<T, DeError>>;
5
6/// Trait to define a data `Block` to fetch records bulky.
7///
8/// If query performance is not your main concern, you can just use the deserialize method from result set.
9pub trait BlockExt: Debug + Sized {
10 /// A block should container number of rows.
11 fn num_of_rows(&self) -> usize;
12
13 /// Fields can be queried from a block.
14 fn fields(&self) -> &[Field];
15
16 /// Number of fields.
17 fn field_count(&self) -> usize {
18 self.fields().len()
19 }
20
21 fn precision(&self) -> Precision;
22
23 fn is_null(&self, row: usize, col: usize) -> bool;
24
25 /// Get field without column index check.
26 ///
27 /// # Safety
28 ///
29 /// This should not be called manually, please use [get_field](#method.get_field).
30 unsafe fn get_field_unchecked(&self, col: usize) -> &Field {
31 self.fields().get_unchecked(col)
32 }
33
34 /// Get field of one column.
35 fn get_field(&self, col: usize) -> Option<&Field> {
36 self.fields().get(col)
37 }
38
39 /// # Safety
40 ///
41 /// **DO NOT** call it directly.
42 unsafe fn cell_unchecked(&self, row: usize, col: usize) -> (&Field, BorrowedValue);
43
44 /// # Safety
45 /// **DO NOT** call it directly.
46 unsafe fn get_col_unchecked(&self, col: usize) -> &ColumnView;
47
48 /// Query by rows.
49 // fn iter_rows(&self) -> RowsIter<'_, Self> {
50 // RowsIter::new(self)
51 // }
52
53 /// Consume self into rows.
54 // fn into_iter_rows(self) -> IntoRowsIter<Self> {
55 // IntoRowsIter::new(self)
56 // }
57
58 /// Columns iterator with borrowed data from block.
59 fn columns_iter(&self) -> ColsIter<'_, Self> {
60 ColsIter::new(self)
61 }
62
63 // fn to_records(&self) -> Vec<Vec<Value>> {
64 // self.iter_rows()
65 // .map(|row| row.into_iter().map(|(f, v)| v.into_value()).collect_vec())
66 // .collect_vec()
67 // }
68
69 // / Deserialize a row to a record type(primitive type or a struct).
70 // /
71 // / Any record could borrow data from the block, so that &[u8], &[str] could be used as record element (if valid).
72 // fn deserialize<'b, T>(&'b self) -> DeserializeIter<'b, Self, T>
73 // where
74 // T: serde::de::Deserialize<'b>,
75 // {
76 // self.iter_rows().map(|row| {
77 // let de = de::RecordDeserializer::from(row);
78 // T::deserialize(de)
79 // })
80 // }
81
82 // / Deserialize a row to a record type(primitive type or a struct).
83 // /
84 // / Any record could borrow data from the block, so that &[u8], &[str] could be used as record element (if valid).
85 // fn deserialize_into<T>(
86 // self,
87 // ) -> std::iter::Map<IntoRowsIter<Self>, fn(QueryRowIter<Self>) -> Result<T, DeError>>
88 // where
89 // T: serde::de::DeserializeOwned,
90 // {
91 // self.into_iter_rows().map(|row| {
92 // let de = de::RecordDeserializer::from(&row);
93 // T::deserialize(de)
94 // })
95 // }
96
97 // / Shortcut version to `.deserialize_into.collect::<Vec<T>>()`
98 //
99 // fn deserialize_into_vec<T>(self) -> Vec<Result<T, DeError>>
100 // where
101 // T: serde::de::DeserializeOwned,
102 // {
103 // self.deserialize_into().collect()
104 // }
105
106 // #[cfg(feature = "async")]
107 // /// Rows as [futures::stream::Stream].
108 // fn rows_stream(&self) -> futures::stream::Iter<RowsIter<'_, Self>> {
109 // futures::stream::iter(Self::iter_rows(self))
110 // }
111
112 // #[cfg(feature = "async")]
113 // /// Owned version to rows stream.
114 // fn into_rows_stream(self) -> futures::stream::Iter<IntoRowsIter<Self>> {
115 // futures::stream::iter(Self::into_iter_rows(self))
116 // }
117
118 // #[cfg(feature = "async")]
119 // /// Rows stream to deserialized record.
120 // fn deserialize_stream<'b, T>(&'b self) -> futures::stream::Iter<DeserializeIter<'b, Self, T>>
121 // where
122 // T: serde::de::Deserialize<'b>,
123 // {
124 // futures::stream::iter(Self::deserialize(self))
125 // }
126}
127
128pub struct CellIter<'b, T: BlockExt> {
129 block: &'b T,
130 row: usize,
131 col: usize,
132}
133
134impl<'b, T: BlockExt> Iterator for CellIter<'b, T> {
135 type Item = (&'b Field, BorrowedValue<'b>);
136
137 fn next(&mut self) -> Option<Self::Item> {
138 let col = self.col;
139 if col < self.block.field_count() {
140 self.col += 1;
141 Some(unsafe { self.block.cell_unchecked(self.row, col) })
142 } else {
143 None
144 }
145 }
146}
147
148#[derive(Debug)]
149pub struct RowInBlock<'b, T: BlockExt> {
150 block: &'b T,
151 row: usize,
152}
153
154impl<'b, T> IntoIterator for RowInBlock<'b, T>
155where
156 T: BlockExt,
157{
158 type Item = (&'b Field, BorrowedValue<'b>);
159
160 type IntoIter = CellIter<'b, T>;
161
162 fn into_iter(self) -> Self::IntoIter {
163 CellIter {
164 block: self.block,
165 row: self.row,
166 col: 0,
167 }
168 }
169}
170
171#[derive(Debug)]
172pub struct RowsIter<'b, T: BlockExt> {
173 block: &'b T,
174 row: usize,
175}
176
177// impl<'b, T> RowsIter<'b, T>
178// where
179// T: BlockExt,
180// {
181// pub(crate) fn new(block: &'b T) -> Self {
182// Self { block, row: 0 }
183// }
184// }
185
186impl<'b, T> Iterator for RowsIter<'b, T>
187where
188 T: BlockExt,
189{
190 type Item = RowInBlock<'b, T>;
191
192 fn next(&mut self) -> Option<Self::Item> {
193 let row = self.row;
194
195 if row < self.block.num_of_rows() {
196 self.row += 1;
197 Some(RowInBlock {
198 block: self.block,
199 row,
200 })
201 } else {
202 None
203 }
204 }
205}
206
207#[derive(Debug)]
208pub struct IntoRowsIter<T: BlockExt> {
209 block: Rc<T>,
210 row: usize,
211}
212
213// impl<T> IntoRowsIter<T>
214// where
215// T: BlockExt,
216// {
217// pub(crate) fn new(block: T) -> Self {
218// Self {
219// block: Rc::new(block),
220// row: 0,
221// }
222// }
223// }
224
225pub struct QueryRowIter<T: BlockExt> {
226 block: Rc<T>,
227 row: usize,
228}
229
230impl<'b, T> IntoIterator for &'b QueryRowIter<T>
231where
232 T: BlockExt,
233{
234 type Item = (&'b Field, BorrowedValue<'b>);
235
236 type IntoIter = CellIter<'b, T>;
237
238 fn into_iter(self) -> Self::IntoIter {
239 CellIter {
240 block: &self.block,
241 row: self.row,
242 col: 0,
243 }
244 }
245}
246
247impl<T> Iterator for IntoRowsIter<T>
248where
249 T: BlockExt,
250{
251 type Item = QueryRowIter<T>;
252
253 fn next(&mut self) -> Option<Self::Item> {
254 let row = self.row;
255
256 if row < self.block.num_of_rows() {
257 self.row += 1;
258 Some(QueryRowIter {
259 block: self.block.clone(),
260 row,
261 })
262 } else {
263 None
264 }
265 }
266}
267
268pub struct ColsIter<'b, T: BlockExt> {
269 block: &'b T,
270 col: usize,
271}
272impl<'b, T> ColsIter<'b, T>
273where
274 T: BlockExt,
275{
276 pub(crate) fn new(block: &'b T) -> Self {
277 Self { block, col: 0 }
278 }
279}
280
281impl<'b, T> Iterator for ColsIter<'b, T>
282where
283 T: BlockExt,
284{
285 type Item = &'b ColumnView;
286
287 fn next(&mut self) -> Option<Self::Item> {
288 if self.col >= self.block.field_count() {
289 return None;
290 }
291
292 let v = unsafe { self.block.get_col_unchecked(self.col) };
293 self.col += 1;
294 Some(v)
295 }
296}