Skip to content

Commit eea1d8d

Browse files
authored
minor: add constants for metadata key values (#11811)
This PR adds constants for metadata keys we use throughout the repository. The purpose is to reduce opportunities for errors due to mangled constants. There are no functional changes in this PR.
1 parent 5c88280 commit eea1d8d

File tree

14 files changed

+133
-74
lines changed

14 files changed

+133
-74
lines changed

crates/store/re_protos/src/v1alpha1/rerun.cloud.v1alpha1.ext.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -189,19 +189,25 @@ impl QueryDatasetResponse {
189189
pub fn field_chunk_id() -> FieldRef {
190190
lazy_field_ref!(
191191
Field::new(Self::FIELD_CHUNK_ID, DataType::FixedSizeBinary(16), false).with_metadata(
192-
[("rerun:kind".to_owned(), "control".to_owned())]
193-
.into_iter()
194-
.collect(),
192+
[(
193+
re_sorbet::metadata::RERUN_KIND.to_owned(),
194+
"control".to_owned()
195+
)]
196+
.into_iter()
197+
.collect(),
195198
)
196199
)
197200
}
198201

199202
pub fn field_chunk_partition_id() -> FieldRef {
200203
lazy_field_ref!(
201204
Field::new(Self::FIELD_CHUNK_PARTITION_ID, DataType::Utf8, false).with_metadata(
202-
[("rerun:kind".to_owned(), "control".to_owned())]
203-
.into_iter()
204-
.collect(),
205+
[(
206+
re_sorbet::metadata::RERUN_KIND.to_owned(),
207+
"control".to_owned()
208+
)]
209+
.into_iter()
210+
.collect(),
205211
)
206212
)
207213
}
@@ -221,19 +227,25 @@ impl QueryDatasetResponse {
221227
pub fn field_chunk_entity_path() -> FieldRef {
222228
lazy_field_ref!(
223229
Field::new(Self::FIELD_CHUNK_ENTITY_PATH, DataType::Utf8, false).with_metadata(
224-
[("rerun:kind".to_owned(), "control".to_owned())]
225-
.into_iter()
226-
.collect(),
230+
[(
231+
re_sorbet::metadata::RERUN_KIND.to_owned(),
232+
"control".to_owned()
233+
)]
234+
.into_iter()
235+
.collect(),
227236
)
228237
)
229238
}
230239

231240
pub fn field_chunk_is_static() -> FieldRef {
232241
lazy_field_ref!(
233242
Field::new(Self::FIELD_CHUNK_IS_STATIC, DataType::Boolean, false).with_metadata(
234-
[("rerun:kind".to_owned(), "control".to_owned())]
235-
.into_iter()
236-
.collect(),
243+
[(
244+
re_sorbet::metadata::RERUN_KIND.to_owned(),
245+
"control".to_owned()
246+
)]
247+
.into_iter()
248+
.collect(),
237249
)
238250
)
239251
}

crates/store/re_sorbet/src/column_kind.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl TryFrom<&ArrowField> for ColumnKind {
4444
"We should have migrated to 'rerun:kind'"
4545
);
4646

47-
let Some(kind) = field.get_opt("rerun:kind") else {
47+
let Some(kind) = field.get_opt(crate::metadata::RERUN_KIND) else {
4848
return Ok(Self::default());
4949
};
5050
match kind {

crates/store/re_sorbet/src/component_column_descriptor.rs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,22 @@ impl ComponentColumnDescriptor {
199199
} = self;
200200

201201
let mut metadata = std::collections::HashMap::from([
202-
("rerun:kind".to_owned(), ColumnKind::Component.to_string()),
203-
("rerun:component".to_owned(), component.to_string()),
202+
(
203+
crate::metadata::RERUN_KIND.to_owned(),
204+
ColumnKind::Component.to_string(),
205+
),
206+
(
207+
re_types_core::FIELD_METADATA_KEY_COMPONENT.to_owned(),
208+
component.to_string(),
209+
),
204210
]);
205211

206212
match batch_type {
207213
BatchType::Dataframe => {
208-
metadata.insert("rerun:entity_path".to_owned(), entity_path.to_string());
214+
metadata.insert(
215+
crate::metadata::SORBET_ENTITY_PATH.to_owned(),
216+
entity_path.to_string(),
217+
);
209218
}
210219
BatchType::Chunk => {
211220
// The whole chunk is for the same entity, which is set in the record batch metadata.
@@ -215,14 +224,14 @@ impl ComponentColumnDescriptor {
215224

216225
if let Some(archetype_name) = archetype_name {
217226
metadata.insert(
218-
"rerun:archetype".to_owned(),
227+
re_types_core::FIELD_METADATA_KEY_ARCHETYPE.to_owned(),
219228
archetype_name.full_name().to_owned(),
220229
);
221230
}
222231

223232
if let Some(component_type) = component_type {
224233
metadata.insert(
225-
"rerun:component_type".to_owned(),
234+
re_types_core::FIELD_METADATA_KEY_COMPONENT_TYPE.to_owned(),
226235
component_type.full_name().to_owned(),
227236
);
228237
}
@@ -294,26 +303,32 @@ impl ComponentColumnDescriptor {
294303
/// `chunk_entity_path`: if this column is part of a chunk batch,
295304
/// what is its entity path (so we can set [`ComponentColumnDescriptor::entity_path`])?
296305
pub fn from_arrow_field(chunk_entity_path: Option<&EntityPath>, field: &ArrowField) -> Self {
297-
let entity_path = if let Some(entity_path) = field.get_opt("rerun:entity_path") {
298-
EntityPath::parse_forgiving(entity_path)
299-
} else if let Some(chunk_entity_path) = chunk_entity_path {
300-
chunk_entity_path.clone()
301-
} else {
302-
EntityPath::root() // TODO(#8744): make entity_path optional for general sorbet batches
303-
};
304-
305-
let component = if let Some(component) = field.get_opt("rerun:component") {
306-
ComponentIdentifier::from(component)
307-
} else {
308-
ComponentIdentifier::new(field.name()) // fallback
309-
};
306+
let entity_path =
307+
if let Some(entity_path) = field.get_opt(crate::metadata::SORBET_ENTITY_PATH) {
308+
EntityPath::parse_forgiving(entity_path)
309+
} else if let Some(chunk_entity_path) = chunk_entity_path {
310+
chunk_entity_path.clone()
311+
} else {
312+
EntityPath::root() // TODO(#8744): make entity_path optional for general sorbet batches
313+
};
314+
315+
let component =
316+
if let Some(component) = field.get_opt(re_types_core::FIELD_METADATA_KEY_COMPONENT) {
317+
ComponentIdentifier::from(component)
318+
} else {
319+
ComponentIdentifier::new(field.name()) // fallback
320+
};
310321

311322
let schema = Self {
312323
store_datatype: field.data_type().clone(),
313324
entity_path,
314-
archetype: field.get_opt("rerun:archetype").map(Into::into),
325+
archetype: field
326+
.get_opt(re_types_core::FIELD_METADATA_KEY_ARCHETYPE)
327+
.map(Into::into),
315328
component,
316-
component_type: field.get_opt("rerun:component_type").map(Into::into),
329+
component_type: field
330+
.get_opt(re_types_core::FIELD_METADATA_KEY_COMPONENT_TYPE)
331+
.map(Into::into),
317332
is_static: field.get_bool("rerun:is_static"),
318333
is_tombstone: field.get_bool("rerun:is_tombstone"),
319334
is_semantically_empty: field.get_bool("rerun:is_semantically_empty"),

crates/store/re_sorbet/src/index_column_descriptor.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,13 @@ impl IndexColumnDescriptor {
106106

107107
let mut metadata = std::collections::HashMap::from([
108108
(
109-
"rerun:kind".to_owned(),
109+
crate::metadata::RERUN_KIND.to_owned(),
110110
crate::ColumnKind::Index.to_string(),
111111
),
112-
("rerun:index_name".to_owned(), timeline.name().to_string()),
112+
(
113+
crate::metadata::SORBET_INDEX_NAME.to_owned(),
114+
timeline.name().to_string(),
115+
),
113116
]);
114117
if *is_sorted {
115118
metadata.insert("rerun:is_sorted".to_owned(), "true".to_owned());
@@ -134,7 +137,7 @@ impl TryFrom<&ArrowField> for IndexColumnDescriptor {
134137
type Error = UnsupportedTimeType;
135138

136139
fn try_from(field: &ArrowField) -> Result<Self, Self::Error> {
137-
let name = if let Some(name) = field.metadata().get("rerun:index_name") {
140+
let name = if let Some(name) = field.metadata().get(crate::metadata::SORBET_INDEX_NAME) {
138141
name.to_owned()
139142
} else {
140143
re_log::debug_once!(

crates/store/re_sorbet/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ mod component_column_descriptor;
2121
mod error;
2222
mod index_column_descriptor;
2323
mod ipc;
24-
mod metadata;
24+
pub mod metadata;
2525
mod migrations;
2626
mod row_id_column_descriptor;
2727
mod schema_builder;

crates/store/re_sorbet/src/metadata.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@ use std::collections::HashMap;
22

33
use arrow::datatypes::Field as ArrowField;
44

5+
// The following constants are used as metadata keys. See also
6+
// [`re_types_core::component_descriptor`] for additional constants.
7+
8+
/// The key used to identify the index name in field-level metadata.
9+
pub const SORBET_INDEX_NAME: &str = "rerun:index_name";
10+
11+
/// The key used to identify the entity path in field-level metadata.
12+
pub const SORBET_ENTITY_PATH: &str = "rerun:entity_path";
13+
14+
/// The key used to identify the [`crate::column_kind::ColumnKind`] in
15+
/// field-level metadata.
16+
pub const RERUN_KIND: &str = "rerun:kind";
17+
518
/// Arrow metadata for an arrow record batch.
619
pub type ArrowBatchMetadata = HashMap<String, String>;
720

crates/store/re_sorbet/src/migrations/v0_0_2__to__v0_1_0.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ fn rewire_tagged_components(batch: &RecordBatch) -> RecordBatch {
6161
}
6262

6363
// These metadata fields don't require value changes.
64-
rename_key(&mut metadata, "rerun.index_name", "rerun:index_name");
65-
rename_key(&mut metadata, "rerun.entity_path", "rerun:entity_path");
66-
rename_key(&mut metadata, "rerun.kind", "rerun:kind");
64+
rename_key(&mut metadata, "rerun.index_name", crate::metadata::SORBET_INDEX_NAME);
65+
rename_key(&mut metadata, "rerun.entity_path", crate::metadata::SORBET_ENTITY_PATH);
66+
rename_key(&mut metadata, "rerun.kind", crate::metadata::RERUN_KIND);
6767
rename_key(&mut metadata, "rerun.is_static", "rerun:is_static");
6868
rename_key(&mut metadata, "rerun.is_indicator", "rerun:is_indicator");
6969
rename_key(&mut metadata, "rerun.is_tombstone", "rerun:is_tombstone");
@@ -83,13 +83,13 @@ fn rewire_tagged_components(batch: &RecordBatch) -> RecordBatch {
8383
component.ends_with("Indicator"),
8484
"Expected component to end with 'Indicator', got: {component:?}"
8585
);
86-
metadata.insert("rerun:component".to_owned(), component);
86+
metadata.insert(re_types_core::FIELD_METADATA_KEY_COMPONENT.to_owned(), component);
8787
} else if field_name.starts_with("rerun.") {
8888
// Long name
89-
metadata.insert("rerun:component".to_owned(), field_name.clone());
89+
metadata.insert(re_types_core::FIELD_METADATA_KEY_COMPONENT.to_owned(), field_name.clone());
9090
} else {
9191
// Short name: expand it to be long
92-
metadata.insert("rerun:component".to_owned(), format!("rerun.components.{field_name}"));
92+
metadata.insert(re_types_core::FIELD_METADATA_KEY_COMPONENT.to_owned(), format!("rerun.components.{field_name}"));
9393
}
9494

9595
// Remove everything else.
@@ -137,11 +137,11 @@ fn rewire_tagged_components(batch: &RecordBatch) -> RecordBatch {
137137
};
138138

139139
if let Some(archetype) = archetype {
140-
metadata.insert("rerun:archetype".to_owned(), archetype);
140+
metadata.insert(re_types_core::FIELD_METADATA_KEY_ARCHETYPE.to_owned(), archetype);
141141
}
142-
metadata.insert("rerun:component".to_owned(), component);
142+
metadata.insert(re_types_core::FIELD_METADATA_KEY_COMPONENT.to_owned(), component);
143143
if let Some(component_type) = component_type {
144-
metadata.insert("rerun:component_type".to_owned(), component_type);
144+
metadata.insert(re_types_core::FIELD_METADATA_KEY_COMPONENT_TYPE.to_owned(), component_type);
145145
}
146146
}
147147

@@ -192,7 +192,9 @@ fn port_recording_info(batch: &mut RecordBatch) {
192192

193193
// We renamed `RecordingProperties` to `RecordingInfo`,
194194
// and moved it from `/__properties/recording` to `/__properties`.
195-
if let Some(entity_path) = batch.schema_metadata_mut().get_mut("rerun:entity_path")
195+
if let Some(entity_path) = batch
196+
.schema_metadata_mut()
197+
.get_mut(crate::metadata::SORBET_ENTITY_PATH)
196198
&& entity_path == "/__properties/recording"
197199
{
198200
*entity_path = "/__properties".to_owned();
@@ -220,7 +222,9 @@ fn port_recording_info(batch: &mut RecordBatch) {
220222
.with_metadata(field.metadata().clone());
221223

222224
// Migrate per-column entity paths (if any):
223-
if let Some(entity_path) = field.metadata_mut().get_mut("rerun:entity_path")
225+
if let Some(entity_path) = field
226+
.metadata_mut()
227+
.get_mut(crate::metadata::SORBET_ENTITY_PATH)
224228
&& entity_path == "/__properties/recording"
225229
{
226230
*entity_path = "/__properties".to_owned();

crates/store/re_sorbet/src/migrations/v0_1_0__to__v0_1_1.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,17 @@ fn drop_indicators(batch: RecordBatch) -> RecordBatch {
4242
);
4343
Some(i) // Keep
4444
}
45-
} else if field.metadata().get("rerun:component").is_some_and(|val| {
46-
val.starts_with("rerun.components.") && val.ends_with("Indicator")
47-
}) {
48-
let Some(indicator) = field.metadata().get("rerun:component") else {
45+
} else if field
46+
.metadata()
47+
.get(re_types_core::FIELD_METADATA_KEY_COMPONENT)
48+
.is_some_and(|val| {
49+
val.starts_with("rerun.components.") && val.ends_with("Indicator")
50+
})
51+
{
52+
let Some(indicator) = field
53+
.metadata()
54+
.get(re_types_core::FIELD_METADATA_KEY_COMPONENT)
55+
else {
4956
debug_assert!(
5057
false,
5158
"missing 'rerun:component' entry that should be present"

crates/store/re_sorbet/src/row_id_column_descriptor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl RowIdColumnDescriptor {
4242

4343
let mut metadata = std::collections::HashMap::from([
4444
(
45-
"rerun:kind".to_owned(),
45+
crate::metadata::RERUN_KIND.to_owned(),
4646
crate::ColumnKind::RowId.to_string(),
4747
),
4848
(

crates/store/re_sorbet/src/sorbet_schema.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ impl SorbetSchema {
5959
}
6060

6161
pub fn entity_path_metadata(entity_path: &EntityPath) -> (String, String) {
62-
("rerun:entity_path".to_owned(), entity_path.to_string())
62+
(
63+
crate::metadata::SORBET_ENTITY_PATH.to_owned(),
64+
entity_path.to_string(),
65+
)
6366
}
6467

6568
pub fn partition_id_metadata(partition_id: impl AsRef<str>) -> (String, String) {
@@ -136,7 +139,7 @@ impl SorbetSchema {
136139
let ArrowSchema { metadata, fields } = arrow_schema;
137140

138141
let entity_path = metadata
139-
.get("rerun:entity_path")
142+
.get(crate::metadata::SORBET_ENTITY_PATH)
140143
.map(|s| EntityPath::parse_forgiving(s));
141144

142145
let columns = SorbetColumnDescriptors::try_from_arrow_fields(entity_path.as_ref(), fields)?;

0 commit comments

Comments
 (0)