re_log_encoding/
legacy.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//! Legacy types for old `MsgPack` .rrd loader
//!
//! The types looks the same as in 0.22, to make sure their serde works the same.

use std::{collections::BTreeMap, sync::Arc};

use arrow::array::RecordBatch as ArrowRecordBatch;

use re_chunk::{TimeInt, TimelineName};
use re_log_types::{external::re_tuid::Tuid, ApplicationId, TimeCell};

// -------------------------------------------------------------

/// Command used for activating a blueprint once it has been fully transmitted.
///
/// This command serves two purposes:
/// - It is important that a blueprint is never activated before it has been fully
///   transmitted. Displaying, or allowing a user to modify, a half-transmitted
///   blueprint can cause confusion and bad interactions with the view heuristics.
/// - Additionally, this command allows fine-tuning the activation behavior itself
///   by specifying whether the blueprint should be immediately activated, or only
///   become the default for future activations.
#[derive(Clone, Debug, serde::Deserialize)]
pub struct LegacyBlueprintActivationCommand {
    pub blueprint_id: LegacyStoreId,
    pub make_active: bool,
    pub make_default: bool,
}

impl LegacyBlueprintActivationCommand {
    fn migrate(self) -> re_log_types::BlueprintActivationCommand {
        let Self {
            blueprint_id,
            make_active,
            make_default,
        } = self;
        re_log_types::BlueprintActivationCommand {
            blueprint_id: blueprint_id.migrate(),
            make_active,
            make_default,
        }
    }
}

#[derive(Clone, Debug, serde::Deserialize)]
pub struct LegacyStoreId {
    pub kind: LegacyStoreKind,
    pub id: Arc<String>,
}

impl LegacyStoreId {
    fn migrate(self) -> re_log_types::StoreId {
        let Self { kind, id } = self;
        re_log_types::StoreId {
            kind: match kind {
                LegacyStoreKind::Recording => re_log_types::StoreKind::Recording,
                LegacyStoreKind::Blueprint => re_log_types::StoreKind::Blueprint,
            },
            id,
        }
    }
}

#[derive(Copy, Clone, Debug, serde::Deserialize)]
pub enum LegacyStoreKind {
    /// A recording of user-data.
    Recording,

    /// Data associated with the blueprint state.
    Blueprint,
}

// -------------------------------------------------------------

#[derive(Clone, Debug, serde::Deserialize)]
#[allow(clippy::large_enum_variant)]
pub enum LegacyLogMsg {
    SetStoreInfo(LegacySetStoreInfo),
    ArrowMsg(LegacyStoreId, LegacyArrowMsg),
    BlueprintActivationCommand(LegacyBlueprintActivationCommand),
}

impl LegacyLogMsg {
    pub fn migrate(self) -> re_log_types::LogMsg {
        match self {
            Self::SetStoreInfo(legacy_set_store_info) => {
                let LegacySetStoreInfo { row_id, info } = legacy_set_store_info;
                let LegacyStoreInfo {
                    application_id,
                    store_id,
                    cloned_from,
                    started,
                } = info;

                re_log_types::LogMsg::SetStoreInfo(re_log_types::SetStoreInfo {
                    row_id: row_id.migrate(),
                    info: re_log_types::StoreInfo {
                        application_id,
                        store_id: store_id.migrate(),
                        cloned_from: cloned_from.map(|id| id.migrate()),
                        store_source: re_log_types::StoreSource::Unknown,
                        started: Some(started),
                        store_version: None,
                    },
                })
            }

            Self::ArrowMsg(store_id, arrow_msg) => {
                re_log_types::LogMsg::ArrowMsg(store_id.migrate(), arrow_msg.migrate())
            }

            Self::BlueprintActivationCommand(legacy_blueprint_activation_command) => {
                re_log_types::LogMsg::BlueprintActivationCommand(
                    legacy_blueprint_activation_command.migrate(),
                )
            }
        }
    }
}

// -------------------------------------------------------------

#[must_use]
#[derive(Clone, Debug, serde::Deserialize)]
pub struct LegacySetStoreInfo {
    pub row_id: LegacyTuid,

    pub info: LegacyStoreInfo,
}

// -------------------------------------------------------------

/// Message containing an Arrow payload
#[derive(Clone, Debug)]
#[must_use]
pub struct LegacyArrowMsg {
    /// Unique identifier for the chunk in this message.
    pub chunk_id: LegacyTuid,

    /// The maximum values for all timelines across the entire batch of data.
    ///
    /// Used to timestamp the batch as a whole for e.g. latency measurements without having to
    /// deserialize the arrow payload.
    pub timepoint_max: LegacyTimePoint,

    /// Schema and data for all control & data columns.
    pub batch: ArrowRecordBatch,
}

impl LegacyArrowMsg {
    fn migrate(self) -> re_log_types::ArrowMsg {
        let Self {
            chunk_id,
            timepoint_max,
            batch,
        } = self;
        re_log_types::ArrowMsg {
            chunk_id: chunk_id.migrate(),
            timepoint_max: timepoint_max.migrate(),
            batch,
            on_release: None,
        }
    }
}

impl<'de> serde::Deserialize<'de> for LegacyArrowMsg {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        struct FieldVisitor;

        impl<'de> serde::de::Visitor<'de> for FieldVisitor {
            type Value = LegacyArrowMsg;

            fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                formatter.write_str("(table_id, timepoint, buf)")
            }

            fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
            where
                A: serde::de::SeqAccess<'de>,
            {
                re_tracing::profile_scope!("LegacyArrowMsg::deserialize");

                let table_id: Option<LegacyTuid> = seq.next_element()?;
                let timepoint_max: Option<LegacyTimePoint> = seq.next_element()?;
                let ipc_bytes: Option<serde_bytes::ByteBuf> = seq.next_element()?;

                if let (Some(chunk_id), Some(timepoint_max), Some(buf)) =
                    (table_id, timepoint_max, ipc_bytes)
                {
                    let batch = arrow_from_ipc(&buf)
                        .map_err(|err| serde::de::Error::custom(format!("IPC decoding: {err}")))?;

                    Ok(LegacyArrowMsg {
                        chunk_id,
                        timepoint_max,
                        batch,
                    })
                } else {
                    Err(serde::de::Error::custom(
                        "Expected (table_id, timepoint, buf)",
                    ))
                }
            }
        }

        deserializer
            .deserialize_tuple(3, FieldVisitor)
            .map_err(|err| serde::de::Error::custom(format!("ArrowMsg: {err}")))
    }
}

fn arrow_from_ipc(buf: &[u8]) -> Result<ArrowRecordBatch, String> {
    use arrow::ipc::reader::StreamReader;
    let stream = StreamReader::try_new(std::io::Cursor::new(buf), None)
        .map_err(|err| format!("Arrow StreamReader error: {err}"))?;
    let batches: Result<Vec<_>, _> = stream.collect();
    let batches = batches.map_err(|err| format!("Arrow error: {err}"))?;
    if batches.is_empty() {
        return Err("No RecordBatch in stream".to_owned());
    }
    if batches.len() > 1 {
        return Err(format!(
            "Found {} batches in stream - expected just one.",
            batches.len()
        ));
    }
    #[allow(clippy::unwrap_used)] // is_empty check above
    let batch = batches.into_iter().next().unwrap();
    Ok(batch)
}

// -------------------------------------------------------------

#[derive(Clone, Debug, serde::Deserialize)]
pub struct LegacyTimePoint(BTreeMap<LegacyTimeline, TimeInt>);

impl LegacyTimePoint {
    fn migrate(self) -> re_chunk::TimePoint {
        self.0
            .into_iter()
            .map(|(timeline, time_int)| {
                let LegacyTimeline { name, typ } = timeline;
                let typ = match typ {
                    LegacyTimeType::Time => {
                        if name == TimelineName::log_time() {
                            re_log_types::TimeType::TimestampNs
                        } else {
                            re_log_types::TimeType::DurationNs
                        }
                    }
                    LegacyTimeType::Sequence => re_log_types::TimeType::Sequence,
                };
                (name, TimeCell::new(typ, time_int))
            })
            .collect::<BTreeMap<_, _>>()
            .into()
    }
}

// -------------------------------------------------------------

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, serde::Deserialize)]
pub struct LegacyTimeline {
    name: TimelineName,

    typ: LegacyTimeType,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, serde::Deserialize)]
pub enum LegacyTimeType {
    Time,
    Sequence,
}

// -------------------------------------------------------------

#[derive(Clone, Copy, Debug, Hash, serde::Deserialize)]
pub struct LegacyTuid {
    pub time_ns: u64,
    pub inc: u64,
}

impl LegacyTuid {
    fn migrate(&self) -> Tuid {
        Tuid::from_nanos_and_inc(self.time_ns, self.inc)
    }
}

// -------------------------------------------------------------

/// Information about a recording or blueprint.
#[derive(Clone, Debug, serde::Deserialize)]
pub struct LegacyStoreInfo {
    pub application_id: ApplicationId,
    pub store_id: LegacyStoreId,
    pub cloned_from: Option<LegacyStoreId>,
    // pub is_official_example: bool,
    pub started: re_log_types::Time,
}