|
14 | 14 |
|
15 | 15 | //! Common structs and utilities for reading data. |
16 | 16 |
|
| 17 | +use std::sync::Arc; |
| 18 | + |
17 | 19 | use async_trait::async_trait; |
18 | 20 | use common_time::Timestamp; |
19 | | -use datatypes::vectors::VectorRef; |
| 21 | +use datatypes::vectors::{UInt64Vector, UInt8Vector, Vector, VectorRef}; |
| 22 | +use snafu::ensure; |
| 23 | +use store_api::storage::ColumnId; |
20 | 24 |
|
21 | | -use crate::error::Result; |
| 25 | +use crate::error::{InvalidBatchSnafu, Result}; |
22 | 26 | use crate::metadata::RegionMetadataRef; |
23 | 27 |
|
24 | | -/// Storage internal representation of a batch of rows. |
| 28 | +/// Storage internal representation of a batch of rows |
| 29 | +/// for a primary key (time series). |
25 | 30 | /// |
26 | | -/// Now the structure of [Batch] is still unstable, all pub fields may be changed. |
27 | | -#[derive(Debug, Default, PartialEq, Eq, Clone)] |
| 31 | +/// Rows are sorted by primary key, timestamp, sequence desc, op_type desc. |
| 32 | +#[derive(Debug, PartialEq, Clone)] |
28 | 33 | pub struct Batch { |
29 | | - /// Rows organized in columnar format. |
30 | | - pub columns: Vec<VectorRef>, |
| 34 | + /// Primary key encoded in a comparable form. |
| 35 | + primary_key: Vec<u8>, |
| 36 | + /// Timestamps of rows, should be sorted and not null. |
| 37 | + timestamps: VectorRef, |
| 38 | + /// Sequences of rows |
| 39 | + /// |
| 40 | + /// UInt64 type, not null. |
| 41 | + sequences: Arc<UInt64Vector>, |
| 42 | + /// Op types of rows |
| 43 | + /// |
| 44 | + /// UInt8 type, not null. |
| 45 | + op_types: Arc<UInt8Vector>, |
| 46 | + /// Fields organized in columnar format. |
| 47 | + fields: Vec<BatchColumn>, |
31 | 48 | } |
32 | 49 |
|
33 | 50 | impl Batch { |
34 | | - /// Create a new `Batch` from `columns`. |
35 | | - /// |
36 | | - /// # Panics |
37 | | - /// Panics if vectors in `columns` have different length. |
38 | | - pub fn new(columns: Vec<VectorRef>) -> Batch { |
39 | | - Self::assert_columns(&columns); |
| 51 | + /// Creates a new batch. |
| 52 | + pub fn new( |
| 53 | + primary_key: Vec<u8>, |
| 54 | + timestamps: VectorRef, |
| 55 | + sequences: Arc<UInt64Vector>, |
| 56 | + op_types: Arc<UInt8Vector>, |
| 57 | + fields: Vec<BatchColumn>, |
| 58 | + ) -> Result<Batch> { |
| 59 | + BatchBuilder::new(primary_key, timestamps, sequences, op_types) |
| 60 | + .with_fields(fields) |
| 61 | + .build() |
| 62 | + } |
| 63 | + |
| 64 | + /// Returns primary key of the batch. |
| 65 | + pub fn primary_key(&self) -> &[u8] { |
| 66 | + &self.primary_key |
| 67 | + } |
| 68 | + |
| 69 | + /// Returns fields in the batch. |
| 70 | + pub fn fields(&self) -> &[BatchColumn] { |
| 71 | + &self.fields |
| 72 | + } |
40 | 73 |
|
41 | | - Batch { columns } |
| 74 | + /// Returns timestamps of the batch. |
| 75 | + pub fn timestamps(&self) -> &VectorRef { |
| 76 | + &self.timestamps |
42 | 77 | } |
43 | 78 |
|
44 | | - /// Returns number of columns in the batch. |
45 | | - pub fn num_columns(&self) -> usize { |
46 | | - self.columns.len() |
| 79 | + /// Returns sequences of the batch. |
| 80 | + pub fn sequences(&self) -> &Arc<UInt64Vector> { |
| 81 | + &self.sequences |
47 | 82 | } |
48 | 83 |
|
49 | | - /// Returns number of rows in the batch. |
| 84 | + /// Returns op types of the batch. |
| 85 | + pub fn op_types(&self) -> &Arc<UInt8Vector> { |
| 86 | + &self.op_types |
| 87 | + } |
| 88 | + |
| 89 | + /// Returns the number of rows in the batch. |
50 | 90 | pub fn num_rows(&self) -> usize { |
51 | | - self.columns.get(0).map(|v| v.len()).unwrap_or(0) |
| 91 | + // All vectors have the same length so we use |
| 92 | + // the length of timestamps vector. |
| 93 | + self.timestamps.len() |
52 | 94 | } |
53 | 95 |
|
54 | 96 | /// Returns true if the number of rows in the batch is 0. |
55 | 97 | pub fn is_empty(&self) -> bool { |
56 | 98 | self.num_rows() == 0 |
57 | 99 | } |
| 100 | +} |
| 101 | + |
| 102 | +/// A column in a [Batch]. |
| 103 | +#[derive(Debug, PartialEq, Eq, Clone)] |
| 104 | +pub struct BatchColumn { |
| 105 | + /// Id of the column. |
| 106 | + pub column_id: ColumnId, |
| 107 | + /// Data of the column. |
| 108 | + pub data: VectorRef, |
| 109 | +} |
| 110 | + |
| 111 | +/// Builder to build [Batch]. |
| 112 | +pub struct BatchBuilder { |
| 113 | + primary_key: Vec<u8>, |
| 114 | + timestamps: VectorRef, |
| 115 | + sequences: Arc<UInt64Vector>, |
| 116 | + op_types: Arc<UInt8Vector>, |
| 117 | + fields: Vec<BatchColumn>, |
| 118 | +} |
| 119 | + |
| 120 | +impl BatchBuilder { |
| 121 | + /// Creates a new [BatchBuilder]. |
| 122 | + pub fn new( |
| 123 | + primary_key: Vec<u8>, |
| 124 | + timestamps: VectorRef, |
| 125 | + sequences: Arc<UInt64Vector>, |
| 126 | + op_types: Arc<UInt8Vector>, |
| 127 | + ) -> BatchBuilder { |
| 128 | + BatchBuilder { |
| 129 | + primary_key, |
| 130 | + timestamps, |
| 131 | + sequences, |
| 132 | + op_types, |
| 133 | + fields: Vec::new(), |
| 134 | + } |
| 135 | + } |
58 | 136 |
|
59 | | - /// Slice the batch, returning a new batch. |
60 | | - /// |
61 | | - /// # Panics |
62 | | - /// Panics if `offset + length > self.num_rows()`. |
63 | | - pub fn slice(&self, offset: usize, length: usize) -> Batch { |
64 | | - let columns = self |
65 | | - .columns |
66 | | - .iter() |
67 | | - .map(|v| v.slice(offset, length)) |
68 | | - .collect(); |
69 | | - Batch { columns } |
70 | | - } |
71 | | - |
72 | | - fn assert_columns(columns: &[VectorRef]) { |
73 | | - if columns.is_empty() { |
74 | | - return; |
| 137 | + /// Set all field columns. |
| 138 | + pub fn with_fields(mut self, fields: Vec<BatchColumn>) -> Self { |
| 139 | + self.fields = fields; |
| 140 | + self |
| 141 | + } |
| 142 | + |
| 143 | + /// Push a field column. |
| 144 | + pub fn push_field(&mut self, column: BatchColumn) -> &mut Self { |
| 145 | + self.fields.push(column); |
| 146 | + self |
| 147 | + } |
| 148 | + |
| 149 | + /// Builds the [Batch]. |
| 150 | + pub fn build(self) -> Result<Batch> { |
| 151 | + let ts_len = self.timestamps.len(); |
| 152 | + ensure!( |
| 153 | + self.sequences.len() == ts_len, |
| 154 | + InvalidBatchSnafu { |
| 155 | + reason: format!( |
| 156 | + "sequence have different len {} != {}", |
| 157 | + self.sequences.len(), |
| 158 | + ts_len |
| 159 | + ), |
| 160 | + } |
| 161 | + ); |
| 162 | + ensure!( |
| 163 | + self.op_types.len() == ts_len, |
| 164 | + InvalidBatchSnafu { |
| 165 | + reason: format!( |
| 166 | + "op type have different len {} != {}", |
| 167 | + self.op_types.len(), |
| 168 | + ts_len |
| 169 | + ), |
| 170 | + } |
| 171 | + ); |
| 172 | + for column in &self.fields { |
| 173 | + ensure!( |
| 174 | + column.data.len() == ts_len, |
| 175 | + InvalidBatchSnafu { |
| 176 | + reason: format!( |
| 177 | + "column {} has different len {} != {}", |
| 178 | + column.column_id, |
| 179 | + column.data.len(), |
| 180 | + ts_len |
| 181 | + ), |
| 182 | + } |
| 183 | + ); |
75 | 184 | } |
76 | 185 |
|
77 | | - let length = columns[0].len(); |
78 | | - assert!(columns.iter().all(|col| col.len() == length)); |
| 186 | + Ok(Batch { |
| 187 | + primary_key: self.primary_key, |
| 188 | + timestamps: self.timestamps, |
| 189 | + sequences: self.sequences, |
| 190 | + op_types: self.op_types, |
| 191 | + fields: self.fields, |
| 192 | + }) |
79 | 193 | } |
80 | 194 | } |
81 | 195 |
|
@@ -110,6 +224,7 @@ impl Source { |
110 | 224 | unimplemented!() |
111 | 225 | } |
112 | 226 |
|
| 227 | + // TODO(yingwen): Maybe remove this method. |
113 | 228 | /// Returns statisics of fetched batches. |
114 | 229 | pub(crate) fn stats(&self) -> SourceStats { |
115 | 230 | unimplemented!() |
|
0 commit comments