|
| 1 | +use std::collections::HashSet; |
| 2 | + |
| 3 | +use arrow::array::AsArray as _; |
| 4 | + |
| 5 | +use re_log_types::LogMsg; |
| 6 | +use re_types::reflection::Reflection; |
| 7 | + |
| 8 | +use crate::commands::read_rrd_streams_from_file_or_stdin; |
| 9 | + |
| 10 | +// --- |
| 11 | + |
| 12 | +#[derive(Debug, Clone, clap::Parser)] |
| 13 | +pub struct VerifyCommand { |
| 14 | + /// Paths to read from. Reads from standard input if none are specified. |
| 15 | + path_to_input_rrds: Vec<String>, |
| 16 | +} |
| 17 | + |
| 18 | +impl VerifyCommand { |
| 19 | + pub fn run(&self) -> anyhow::Result<()> { |
| 20 | + let mut verifier = Verifier::new()?; |
| 21 | + |
| 22 | + let Self { path_to_input_rrds } = self; |
| 23 | + |
| 24 | + // TODO(cmc): might want to make this configurable at some point. |
| 25 | + let version_policy = re_log_encoding::VersionPolicy::Warn; |
| 26 | + let (rx, _) = read_rrd_streams_from_file_or_stdin(version_policy, path_to_input_rrds); |
| 27 | + |
| 28 | + let mut seen_files = std::collections::HashSet::new(); |
| 29 | + |
| 30 | + for (source, res) in rx { |
| 31 | + verifier.verify_log_msg(&source.to_string(), res?); |
| 32 | + seen_files.insert(source); |
| 33 | + } |
| 34 | + |
| 35 | + if verifier.errors.is_empty() { |
| 36 | + if seen_files.len() == 1 { |
| 37 | + eprintln!("1 file verified without error."); |
| 38 | + } else { |
| 39 | + eprintln!("{} files verified without error.", seen_files.len()); |
| 40 | + } |
| 41 | + Ok(()) |
| 42 | + } else { |
| 43 | + for err in &verifier.errors { |
| 44 | + eprintln!("{err}"); |
| 45 | + } |
| 46 | + Err(anyhow::anyhow!( |
| 47 | + "Verification failed with {} errors", |
| 48 | + verifier.errors.len() |
| 49 | + )) |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +struct Verifier { |
| 55 | + reflection: Reflection, |
| 56 | + errors: HashSet<String>, |
| 57 | +} |
| 58 | + |
| 59 | +impl Verifier { |
| 60 | + fn new() -> anyhow::Result<Self> { |
| 61 | + Ok(Self { |
| 62 | + reflection: re_types::reflection::generate_reflection()?, |
| 63 | + errors: HashSet::new(), |
| 64 | + }) |
| 65 | + } |
| 66 | + |
| 67 | + fn verify_log_msg(&mut self, source: &str, msg: LogMsg) { |
| 68 | + match msg { |
| 69 | + LogMsg::SetStoreInfo { .. } | LogMsg::BlueprintActivationCommand { .. } => {} |
| 70 | + |
| 71 | + LogMsg::ArrowMsg(_store_id, arrow_msg) => { |
| 72 | + self.verify_record_batch(source, &arrow_msg.batch); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + fn verify_record_batch(&mut self, source: &str, batch: &arrow::array::RecordBatch) { |
| 78 | + match re_sorbet::ChunkBatch::try_from(batch) { |
| 79 | + Ok(chunk_batch) => self.verify_chunk_batch(source, &chunk_batch), |
| 80 | + Err(err) => { |
| 81 | + self.errors |
| 82 | + .insert(format!("{source}: Failed to parse batch: {err}")); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + fn verify_chunk_batch(&mut self, source: &str, chunk_batch: &re_sorbet::ChunkBatch) { |
| 88 | + for (component_descriptor, column) in chunk_batch.component_columns() { |
| 89 | + if let Err(err) = self.verify_component_column(component_descriptor, column) { |
| 90 | + self.errors.insert(format!( |
| 91 | + "{source}: Failed to deserialize column {}: {}", |
| 92 | + component_descriptor.component_name, |
| 93 | + re_error::format(err) |
| 94 | + )); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + fn verify_component_column( |
| 100 | + &self, |
| 101 | + component_descriptor: &re_sorbet::ComponentColumnDescriptor, |
| 102 | + column: &dyn arrow::array::Array, |
| 103 | + ) -> anyhow::Result<()> { |
| 104 | + let re_sorbet::ComponentColumnDescriptor { |
| 105 | + component_name, |
| 106 | + archetype_name, |
| 107 | + archetype_field_name, |
| 108 | + .. |
| 109 | + } = component_descriptor; |
| 110 | + |
| 111 | + if !component_name.full_name().starts_with("rerun.") { |
| 112 | + re_log::debug_once!("Ignoring non-Rerun component {component_name:?}"); |
| 113 | + return Ok(()); |
| 114 | + } |
| 115 | + |
| 116 | + if component_name.is_indicator_component() { |
| 117 | + // Lacks reflection and data |
| 118 | + } else { |
| 119 | + // Verify data |
| 120 | + let component_reflection = self |
| 121 | + .reflection |
| 122 | + .components |
| 123 | + .get(component_name) |
| 124 | + .ok_or_else(|| anyhow::anyhow!("Unknown component"))?; |
| 125 | + |
| 126 | + let list_array = column.as_list_opt::<i32>().ok_or_else(|| { |
| 127 | + anyhow::anyhow!("Expected list array, found {:?}", column.data_type()) |
| 128 | + })?; |
| 129 | + |
| 130 | + assert_eq!(column.len() + 1, list_array.offsets().len()); |
| 131 | + |
| 132 | + for i in 0..column.len() { |
| 133 | + let cell = list_array.value(i); |
| 134 | + (component_reflection.verify_arrow_array)(cell.as_ref())?; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + if let Some(archetype_name) = archetype_name { |
| 139 | + if archetype_name.full_name().starts_with("rerun.") { |
| 140 | + // Verify archetype. |
| 141 | + // We may want to have a flag to allow some of this? |
| 142 | + let archetype_reflection = self |
| 143 | + .reflection |
| 144 | + .archetypes |
| 145 | + .get(archetype_name) |
| 146 | + .ok_or_else(|| anyhow::anyhow!("Unknown archetype: {archetype_name:?}"))?; |
| 147 | + |
| 148 | + if let Some(archetype_field_name) = archetype_field_name { |
| 149 | + // Verify archetype field. |
| 150 | + // We may want to have a flag to allow some of this? |
| 151 | + let archetype_field_reflection = archetype_reflection |
| 152 | + .get_field(archetype_field_name) |
| 153 | + .ok_or_else(|| { |
| 154 | + anyhow::anyhow!( |
| 155 | + "Input column referred to the archetype field name {archetype_field_name:?} of {archetype_name:?}, which only has the fields: {:?}", |
| 156 | + archetype_reflection.fields.iter().map(|field| field.name) |
| 157 | + ) |
| 158 | + })?; |
| 159 | + |
| 160 | + let expected_component_name = &archetype_field_reflection.component_name; |
| 161 | + if component_name != expected_component_name { |
| 162 | + return Err(anyhow::anyhow!( |
| 163 | + "Archetype field {archetype_field_name:?} of {archetype_name:?} has component {expected_component_name:?} in this version of Rerun, but the data column has component {component_name:?}" |
| 164 | + )); |
| 165 | + } |
| 166 | + } |
| 167 | + } else { |
| 168 | + re_log::debug_once!("Ignoring non-Rerun archetype {archetype_name:?}"); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + Ok(()) |
| 173 | + } |
| 174 | +} |
0 commit comments