Skip to content

Commit

Permalink
Sorted fields
Browse files Browse the repository at this point in the history
  • Loading branch information
smklein committed Oct 12, 2023
1 parent 929e761 commit 2e91139
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
23 changes: 14 additions & 9 deletions oximeter/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,17 +328,20 @@ pub struct TimeseriesPageSelector {
pub(crate) type TimeseriesKey = u64;

pub(crate) fn timeseries_key(sample: &Sample) -> TimeseriesKey {
timeseries_key_for(sample.target_fields(), sample.metric_fields())
timeseries_key_for(
sample.sorted_target_fields(),
sample.sorted_metric_fields(),
)
}

pub(crate) fn timeseries_key_for<'a>(
target_fields: impl Iterator<Item = &'a Field>,
metric_fields: impl Iterator<Item = &'a Field>,
fn timeseries_key_for<'a>(
target_fields: &BTreeMap<String, Field>,
metric_fields: &BTreeMap<String, Field>,
) -> TimeseriesKey {
use highway::HighwayHasher;
use std::hash::{Hash, Hasher};
let mut hasher = HighwayHasher::default();
for field in target_fields.chain(metric_fields) {
for field in target_fields.values().chain(metric_fields.values()) {
bcs::to_bytes(&field)
.expect("Failed to serialized field to bytes")
.hash(&mut hasher);
Expand Down Expand Up @@ -460,10 +463,12 @@ mod tests {

let mut output = vec![];
for (name, value) in values {
let key = timeseries_key_for(
[&Field { name: name.to_string(), value }].into_iter(),
[].into_iter(),
);
let target_fields = BTreeMap::from([(
"field".to_string(),
Field { name: name.to_string(), value },
)]);
let metric_fields = BTreeMap::new();
let key = timeseries_key_for(&target_fields, &metric_fields);
output.push(format!("{name} -> {key}"));
}

Expand Down
10 changes: 10 additions & 0 deletions oximeter/oximeter/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,11 @@ impl Sample {
self.target.fields.values()
}

/// Return the sorted fields of this sample's target.
pub fn sorted_target_fields(&self) -> &BTreeMap<String, Field> {
&self.target.fields
}

/// Return the name of this sample's metric.
pub fn metric_name(&self) -> &str {
&self.metric.name
Expand All @@ -779,6 +784,11 @@ impl Sample {
self.metric.fields.values()
}

/// Return the sorted fields of this sample's metric
pub fn sorted_metric_fields(&self) -> &BTreeMap<String, Field> {
&self.metric.fields
}

// Check validity of field names for the target and metric. Currently this
// just verifies there are no duplicate names between them.
fn verify_field_names(
Expand Down

0 comments on commit 2e91139

Please sign in to comment.