Skip to content

Commit 55829df

Browse files
committed
use BTreeMap
1 parent d194a5c commit 55829df

File tree

1 file changed

+76
-27
lines changed

1 file changed

+76
-27
lines changed

proxy/src/opentsdb/types.rs

+76-27
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414

1515
use std::{
16-
collections::{HashMap, HashSet},
16+
collections::{BTreeMap, HashMap, HashSet},
1717
default::Default,
1818
fmt::Debug,
1919
};
@@ -220,13 +220,13 @@ pub struct QueryResponse {
220220
/// A list of tags only returned when the results are for a single time
221221
/// series. If results are aggregated, this value may be null or an empty
222222
/// map
223-
pub(crate) tags: HashMap<String, String>,
223+
pub(crate) tags: BTreeMap<String, String>,
224224
/// If more than one timeseries were included in the result set, i.e. they
225225
/// were aggregated, this will display a list of tag names that were found
226226
/// in common across all time series.
227227
#[serde(rename = "aggregatedTags")]
228228
pub(crate) aggregated_tags: Vec<String>,
229-
pub(crate) dps: HashMap<String, f64>,
229+
pub(crate) dps: BTreeMap<String, f64>,
230230
}
231231

232232
#[derive(Default)]
@@ -237,10 +237,10 @@ struct QueryConverter {
237237
// (column_name, index)
238238
tags_idx: Vec<(String, usize)>,
239239
aggregated_tags: Vec<String>,
240-
// (tags_key, (tagk, tagv))
241-
tags: HashMap<String, HashMap<String, String>>,
242-
// (tags_key, (timestamp, value))
243-
values: HashMap<String, HashMap<String, f64>>,
240+
// (time_series, (tagk, tagv))
241+
tags: BTreeMap<String, BTreeMap<String, String>>,
242+
// (time_series, (timestamp, value))
243+
values: BTreeMap<String, BTreeMap<String, f64>>,
244244

245245
resp: Vec<QueryResponse>,
246246
}
@@ -249,18 +249,18 @@ impl QueryConverter {
249249
fn try_new(
250250
schema: &RecordSchema,
251251
metric: String,
252-
timestamp_col_name: &str,
253-
field_col_name: &str,
252+
timestamp_col_name: String,
253+
field_col_name: String,
254254
tags: Vec<String>,
255255
aggregated_tags: Vec<String>,
256256
) -> Result<Self> {
257257
let timestamp_idx = schema
258-
.index_of(timestamp_col_name)
258+
.index_of(&timestamp_col_name)
259259
.context(InternalNoCause {
260260
msg: "Timestamp column is missing in query response",
261261
})?;
262262

263-
let value_idx = schema.index_of(field_col_name).context(InternalNoCause {
263+
let value_idx = schema.index_of(&field_col_name).context(InternalNoCause {
264264
msg: "Value column is missing in query response",
265265
})?;
266266

@@ -316,7 +316,8 @@ impl QueryConverter {
316316
fn add_batch(&mut self, record_batch: RecordBatch) -> Result<()> {
317317
let row_num = record_batch.num_rows();
318318
for row_idx in 0..row_num {
319-
let mut tags = HashMap::with_capacity(self.tags_idx.len());
319+
let mut tags = BTreeMap::new();
320+
// tags_key is used to identify a time series
320321
let mut tags_key = String::new();
321322
for (tag_key, idx) in &self.tags_idx {
322323
let tag_value = record_batch
@@ -404,8 +405,8 @@ pub(crate) fn convert_output_to_response(
404405
QueryConverter::try_new(
405406
record_schema,
406407
metric,
407-
&timestamp_col_name,
408-
&field_col_name,
408+
timestamp_col_name,
409+
field_col_name,
409410
tags,
410411
aggregated_tags,
411412
)?
@@ -467,19 +468,30 @@ mod tests {
467468
.unwrap(),
468469
)
469470
.unwrap()
471+
.add_normal_column(
472+
column_schema::Builder::new("tag2".to_string(), DatumKind::String)
473+
.is_tag(true)
474+
.build()
475+
.unwrap(),
476+
)
477+
.unwrap()
470478
.build()
471479
.unwrap()
472480
}
473481

474482
fn build_record_batch(schema: &Schema) -> RecordBatchVec {
475-
let tsid: ArrayRef = Arc::new(UInt64Array::from(vec![1]));
476-
let timestamp: ArrayRef = Arc::new(TimestampMillisecondArray::from(vec![11111111]));
477-
let values: ArrayRef = Arc::new(Float64Array::from(vec![100.0]));
478-
let tag1: ArrayRef = Arc::new(StringArray::from(vec!["a"]));
483+
let tsid: ArrayRef = Arc::new(UInt64Array::from(vec![1, 1, 2, 3, 3]));
484+
let timestamp: ArrayRef = Arc::new(TimestampMillisecondArray::from(vec![
485+
11111111, 11111112, 11111113, 11111111, 11111112,
486+
]));
487+
let values: ArrayRef =
488+
Arc::new(Float64Array::from(vec![100.0, 101.0, 200.0, 300.0, 301.0]));
489+
let tag1: ArrayRef = Arc::new(StringArray::from(vec!["a", "a", "b", "c", "c"]));
490+
let tag2: ArrayRef = Arc::new(StringArray::from(vec!["x", "x", "y", "z", "z"]));
479491

480492
let batch = ArrowRecordBatch::try_new(
481493
schema.to_arrow_schema_ref(),
482-
vec![tsid, timestamp, values, tag1],
494+
vec![tsid, timestamp, values, tag1, tag2],
483495
)
484496
.unwrap();
485497

@@ -489,7 +501,7 @@ mod tests {
489501
#[test]
490502
fn test_convert_output_to_response() {
491503
let metric = "metric".to_string();
492-
let tags = vec!["tag1".to_string()];
504+
let tags = vec!["tag1".to_string(), "tag2".to_string()];
493505
let schema = build_schema();
494506
let record_batch = build_record_batch(&schema);
495507
let result = convert_output_to_response(
@@ -498,19 +510,56 @@ mod tests {
498510
DEFAULT_FIELD.to_string(),
499511
TIMESTAMP_COLUMN.to_string(),
500512
tags,
501-
vec!["tag1".to_string()],
513+
vec![],
502514
)
503515
.unwrap();
504516

505517
assert_eq!(
506-
vec![QueryResponse {
507-
metric: metric.clone(),
508-
tags: vec![("tag1".to_string(), "a".to_string()),]
518+
vec![
519+
QueryResponse {
520+
metric: metric.clone(),
521+
tags: vec![
522+
("tag1".to_string(), "a".to_string()),
523+
("tag2".to_string(), "x".to_string()),
524+
]
525+
.into_iter()
526+
.collect(),
527+
aggregated_tags: vec![],
528+
dps: vec![
529+
("11111111".to_string(), 100.0),
530+
("11111112".to_string(), 101.0),
531+
]
532+
.into_iter()
533+
.collect(),
534+
},
535+
QueryResponse {
536+
metric: metric.clone(),
537+
tags: vec![
538+
("tag1".to_string(), "b".to_string()),
539+
("tag2".to_string(), "y".to_string()),
540+
]
541+
.into_iter()
542+
.collect(),
543+
aggregated_tags: vec![],
544+
dps: vec![("11111113".to_string(), 200.0),].into_iter().collect(),
545+
},
546+
QueryResponse {
547+
metric: metric.clone(),
548+
tags: vec![
549+
("tag1".to_string(), "c".to_string()),
550+
("tag2".to_string(), "z".to_string()),
551+
]
552+
.into_iter()
553+
.collect(),
554+
aggregated_tags: vec![],
555+
dps: vec![
556+
("11111111".to_string(), 300.0),
557+
("11111112".to_string(), 301.0),
558+
]
509559
.into_iter()
510560
.collect(),
511-
aggregated_tags: vec!["tag1".to_string()],
512-
dps: vec![("11111111".to_string(), 100.0),].into_iter().collect(),
513-
}],
561+
},
562+
],
514563
result
515564
);
516565
}

0 commit comments

Comments
 (0)