12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- use std:: hash:: Hash ;
16
-
17
- use api:: v1:: value:: ValueData ;
18
- use api:: v1:: { ColumnDataType , ColumnSchema , Row , Rows , SemanticType } ;
15
+ use api:: v1:: { Rows , WriteHint } ;
19
16
use common_telemetry:: { error, info} ;
20
17
use snafu:: { ensure, OptionExt } ;
21
- use store_api:: metric_engine_consts:: {
22
- DATA_SCHEMA_TABLE_ID_COLUMN_NAME , DATA_SCHEMA_TSID_COLUMN_NAME ,
23
- } ;
18
+ use store_api:: codec:: PrimaryKeyEncoding ;
24
19
use store_api:: region_request:: { AffectedRows , RegionPutRequest } ;
25
20
use store_api:: storage:: { RegionId , TableId } ;
26
21
@@ -30,11 +25,9 @@ use crate::error::{
30
25
PhysicalRegionNotFoundSnafu , Result ,
31
26
} ;
32
27
use crate :: metrics:: { FORBIDDEN_OPERATION_COUNT , MITO_OPERATION_ELAPSED } ;
28
+ use crate :: row_modifier:: RowsIter ;
33
29
use crate :: utils:: to_data_region_id;
34
30
35
- // A random number
36
- const TSID_HASH_SEED : u32 = 846793005 ;
37
-
38
31
impl MetricEngineInner {
39
32
/// Dispatch region put request
40
33
pub async fn put_region (
@@ -82,8 +75,21 @@ impl MetricEngineInner {
82
75
83
76
// write to data region
84
77
78
+ // TODO(weny): retrieve the encoding from the metadata region.
79
+ let encoding = PrimaryKeyEncoding :: Dense ;
80
+
85
81
// TODO: retrieve table name
86
- self . modify_rows ( logical_region_id. table_id ( ) , & mut request. rows ) ?;
82
+ self . modify_rows (
83
+ physical_region_id,
84
+ logical_region_id. table_id ( ) ,
85
+ & mut request. rows ,
86
+ encoding,
87
+ ) ?;
88
+ if encoding == PrimaryKeyEncoding :: Sparse {
89
+ request. hint = Some ( WriteHint {
90
+ primary_key_encoding : api:: v1:: PrimaryKeyEncoding :: Sparse . into ( ) ,
91
+ } ) ;
92
+ }
87
93
self . data_region . write_data ( data_region_id, request) . await
88
94
}
89
95
@@ -133,67 +139,28 @@ impl MetricEngineInner {
133
139
/// Perform metric engine specific logic to incoming rows.
134
140
/// - Add table_id column
135
141
/// - Generate tsid
136
- fn modify_rows ( & self , table_id : TableId , rows : & mut Rows ) -> Result < ( ) > {
137
- // gather tag column indices
138
- let tag_col_indices = rows
139
- . schema
140
- . iter ( )
141
- . enumerate ( )
142
- . filter_map ( |( idx, col) | {
143
- if col. semantic_type == SemanticType :: Tag as i32 {
144
- Some ( ( idx, col. column_name . clone ( ) ) )
145
- } else {
146
- None
147
- }
148
- } )
149
- . collect :: < Vec < _ > > ( ) ;
150
-
151
- // add table_name column
152
- rows. schema . push ( ColumnSchema {
153
- column_name : DATA_SCHEMA_TABLE_ID_COLUMN_NAME . to_string ( ) ,
154
- datatype : ColumnDataType :: Uint32 as i32 ,
155
- semantic_type : SemanticType :: Tag as _ ,
156
- datatype_extension : None ,
157
- options : None ,
158
- } ) ;
159
- // add tsid column
160
- rows. schema . push ( ColumnSchema {
161
- column_name : DATA_SCHEMA_TSID_COLUMN_NAME . to_string ( ) ,
162
- datatype : ColumnDataType :: Uint64 as i32 ,
163
- semantic_type : SemanticType :: Tag as _ ,
164
- datatype_extension : None ,
165
- options : None ,
166
- } ) ;
167
-
168
- // fill internal columns
169
- for row in & mut rows. rows {
170
- Self :: fill_internal_columns ( table_id, & tag_col_indices, row) ;
171
- }
172
-
173
- Ok ( ( ) )
174
- }
175
-
176
- /// Fills internal columns of a row with table name and a hash of tag values.
177
- fn fill_internal_columns (
142
+ fn modify_rows (
143
+ & self ,
144
+ physical_region_id : RegionId ,
178
145
table_id : TableId ,
179
- tag_col_indices : & [ ( usize , String ) ] ,
180
- row : & mut Row ,
181
- ) {
182
- let mut hasher = mur3 :: Hasher128 :: with_seed ( TSID_HASH_SEED ) ;
183
- for ( idx , name ) in tag_col_indices {
184
- let tag = row . values [ * idx ] . clone ( ) ;
185
- name . hash ( & mut hasher ) ;
186
- // The type is checked before. So only null is ignored.
187
- if let Some ( ValueData :: StringValue ( string ) ) = tag . value_data {
188
- string . hash ( & mut hasher ) ;
189
- }
190
- }
191
- // TSID is 64 bits, simply truncate the 128 bits hash
192
- let ( hash , _ ) = hasher . finish128 ( ) ;
193
-
194
- // fill table id and tsid
195
- row . values . push ( ValueData :: U32Value ( table_id ) . into ( ) ) ;
196
- row . values . push ( ValueData :: U64Value ( hash ) . into ( ) ) ;
146
+ rows : & mut Rows ,
147
+ encoding : PrimaryKeyEncoding ,
148
+ ) -> Result < ( ) > {
149
+ let input = std :: mem :: take ( rows ) ;
150
+ let iter = {
151
+ let state = self . state . read ( ) . unwrap ( ) ;
152
+ let name_to_id = state
153
+ . physical_region_states ( )
154
+ . get ( & physical_region_id )
155
+ . with_context ( || PhysicalRegionNotFoundSnafu {
156
+ region_id : physical_region_id ,
157
+ } ) ?
158
+ . physical_columns ( ) ;
159
+ RowsIter :: new ( input , name_to_id )
160
+ } ;
161
+ let output = self . row_modifier . modify_rows ( iter , table_id , encoding ) ? ;
162
+ * rows = output ;
163
+ Ok ( ( ) )
197
164
}
198
165
}
199
166
@@ -217,6 +184,7 @@ mod tests {
217
184
let rows = test_util:: build_rows ( 1 , 5 ) ;
218
185
let request = RegionRequest :: Put ( RegionPutRequest {
219
186
rows : Rows { schema, rows } ,
187
+ hint : None ,
220
188
} ) ;
221
189
222
190
// write data
@@ -290,6 +258,7 @@ mod tests {
290
258
let rows = test_util:: build_rows ( 3 , 100 ) ;
291
259
let request = RegionRequest :: Put ( RegionPutRequest {
292
260
rows : Rows { schema, rows } ,
261
+ hint : None ,
293
262
} ) ;
294
263
295
264
// write data
@@ -311,6 +280,7 @@ mod tests {
311
280
let rows = test_util:: build_rows ( 1 , 100 ) ;
312
281
let request = RegionRequest :: Put ( RegionPutRequest {
313
282
rows : Rows { schema, rows } ,
283
+ hint : None ,
314
284
} ) ;
315
285
316
286
engine
@@ -330,6 +300,7 @@ mod tests {
330
300
let rows = test_util:: build_rows ( 1 , 100 ) ;
331
301
let request = RegionRequest :: Put ( RegionPutRequest {
332
302
rows : Rows { schema, rows } ,
303
+ hint : None ,
333
304
} ) ;
334
305
335
306
engine
0 commit comments