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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use arrow2::array::Array as ArrowArray;
use arrow2::chunk::Chunk as ArrowChunk;
use arrow2::datatypes::Schema as ArrowSchema;
use arrow2::error::Error as ArrowError;
use arrow2::io::ipc::{read, write};
use re_dataframe::TransportChunk;

use crate::v0::{EncoderVersion, RecordingMetadata};

#[derive(Debug, thiserror::Error)]
pub enum CodecError {
    #[error("Arrow serialization error: {0}")]
    ArrowSerialization(ArrowError),

    #[error("Failed to decode message header {0}")]
    HeaderDecoding(std::io::Error),

    #[error("Failed to encode message header {0}")]
    HeaderEncoding(std::io::Error),

    #[error("Missing record batch")]
    MissingRecordBatch,

    #[error("Unexpected stream state")]
    UnexpectedStreamState,

    #[error("Unknown message header")]
    UnknownMessageHeader,

    #[error("Invalid argument: {0}")]
    InvalidArgument(String),
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct MessageHader(pub u8);

impl MessageHader {
    pub const NO_DATA: Self = Self(1);
    pub const RECORD_BATCH: Self = Self(2);

    pub const SIZE_BYTES: usize = 1;
}

impl MessageHader {
    fn decode(read: &mut impl std::io::Read) -> Result<Self, CodecError> {
        let mut buffer = [0_u8; Self::SIZE_BYTES];
        read.read_exact(&mut buffer)
            .map_err(CodecError::HeaderDecoding)?;

        let header = u8::from_le(buffer[0]);

        Ok(Self(header))
    }

    fn encode(&self, write: &mut impl std::io::Write) -> Result<(), CodecError> {
        write
            .write_all(&[self.0])
            .map_err(CodecError::HeaderEncoding)?;

        Ok(())
    }
}

#[derive(Debug)]
pub enum TransportMessageV0 {
    NoData,
    RecordBatch(TransportChunk),
}

impl TransportMessageV0 {
    fn to_bytes(&self) -> Result<Vec<u8>, CodecError> {
        match self {
            Self::NoData => {
                let mut data: Vec<u8> = Vec::new();
                MessageHader::NO_DATA.encode(&mut data)?;
                Ok(data)
            }
            Self::RecordBatch(chunk) => {
                let mut data: Vec<u8> = Vec::new();
                MessageHader::RECORD_BATCH.encode(&mut data)?;

                write_arrow_to_bytes(&mut data, &chunk.schema, &chunk.data)?;

                Ok(data)
            }
        }
    }

    fn from_bytes(data: &[u8]) -> Result<Self, CodecError> {
        let mut reader = std::io::Cursor::new(data);
        let header = MessageHader::decode(&mut reader)?;

        match header {
            MessageHader::NO_DATA => Ok(Self::NoData),
            MessageHader::RECORD_BATCH => {
                let (schema, data) = read_arrow_from_bytes(&mut reader)?;

                let tc = TransportChunk {
                    schema: schema.clone(),
                    data,
                };

                Ok(Self::RecordBatch(tc))
            }
            _ => Err(CodecError::UnknownMessageHeader),
        }
    }
}

// TODO(zehiko) add support for separately encoding schema from the record batch to get rid of overhead
// of sending schema in each transport message for the same stream of batches. This will require codec
// to become stateful and keep track if schema was sent / received.
/// Encode a transport chunk into a byte stream.
pub fn encode(version: EncoderVersion, chunk: TransportChunk) -> Result<Vec<u8>, CodecError> {
    match version {
        EncoderVersion::V0 => TransportMessageV0::RecordBatch(chunk).to_bytes(),
    }
}

/// Encode a `NoData` message into a byte stream. This can be used by the remote store
/// (i.e. data producer) to signal back to the client that there's no data available.
pub fn no_data(version: EncoderVersion) -> Result<Vec<u8>, CodecError> {
    match version {
        EncoderVersion::V0 => TransportMessageV0::NoData.to_bytes(),
    }
}

/// Decode transport data from a byte stream - if there's a record batch present, return it, otherwise return `None`.
pub fn decode(version: EncoderVersion, data: &[u8]) -> Result<Option<TransportChunk>, CodecError> {
    match version {
        EncoderVersion::V0 => {
            let msg = TransportMessageV0::from_bytes(data)?;
            match msg {
                TransportMessageV0::RecordBatch(chunk) => Ok(Some(chunk)),
                TransportMessageV0::NoData => Ok(None),
            }
        }
    }
}

impl RecordingMetadata {
    /// Create `RecordingMetadata` from `TransportChunk`. We rely on `TransportChunk` until
    /// we migrate from arrow2 to arrow.
    pub fn try_from(
        version: EncoderVersion,
        metadata: &TransportChunk,
    ) -> Result<Self, CodecError> {
        if metadata.data.len() != 1 {
            return Err(CodecError::InvalidArgument(format!(
                "metadata record batch can only have a single row, batch with {} rows given",
                metadata.data.len()
            )));
        };

        match version {
            EncoderVersion::V0 => {
                let mut data: Vec<u8> = Vec::new();
                write_arrow_to_bytes(&mut data, &metadata.schema, &metadata.data)?;

                Ok(Self {
                    encoder_version: version as i32,
                    payload: data,
                })
            }
        }
    }

    /// Get metadata as arrow data
    pub fn data(&self) -> Result<TransportChunk, CodecError> {
        let mut reader = std::io::Cursor::new(self.payload.clone());

        let encoder_version = EncoderVersion::try_from(self.encoder_version)
            .map_err(|err| CodecError::InvalidArgument(err.to_string()))?;

        match encoder_version {
            EncoderVersion::V0 => {
                let (schema, data) = read_arrow_from_bytes(&mut reader)?;
                Ok(TransportChunk { schema, data })
            }
        }
    }

    /// Returns unique id of the recording
    pub fn id(&self) -> Result<re_log_types::StoreId, CodecError> {
        let metadata = self.data()?;
        let id_pos = metadata
            .schema
            .fields
            .iter()
            // TODO(zehiko) we need to figure out where mandatory fields live
            .position(|field| field.name == "id")
            .ok_or_else(|| CodecError::InvalidArgument("missing id field in schema".to_owned()))?;

        use arrow2::array::Utf8Array as ArrowUtf8Array;

        let id = metadata.data.columns()[id_pos]
            .as_any()
            .downcast_ref::<ArrowUtf8Array<i32>>()
            .ok_or_else(|| {
                CodecError::InvalidArgument(format!(
                    "Unexpected type for id with position {id_pos} in schema: {:?}",
                    metadata.schema
                ))
            })?
            .value(0);

        Ok(re_log_types::StoreId::from_string(
            re_log_types::StoreKind::Recording,
            id.to_owned(),
        ))
    }
}

/// Helper function that serializes given arrow schema and record batch into bytes
/// using Arrow IPC format.
fn write_arrow_to_bytes<W: std::io::Write>(
    writer: &mut W,
    schema: &ArrowSchema,
    data: &ArrowChunk<Box<dyn ArrowArray>>,
) -> Result<(), CodecError> {
    let options = write::WriteOptions { compression: None };
    let mut sw = write::StreamWriter::new(writer, options);

    sw.start(schema, None)
        .map_err(CodecError::ArrowSerialization)?;
    sw.write(data, None)
        .map_err(CodecError::ArrowSerialization)?;
    sw.finish().map_err(CodecError::ArrowSerialization)?;

    Ok(())
}

/// Helper function that deserializes raw bytes into arrow schema and record batch
/// using Arrow IPC format.
fn read_arrow_from_bytes<R: std::io::Read>(
    reader: &mut R,
) -> Result<(ArrowSchema, ArrowChunk<Box<dyn ArrowArray>>), CodecError> {
    let metadata = read::read_stream_metadata(reader).map_err(CodecError::ArrowSerialization)?;
    let mut stream = read::StreamReader::new(reader, metadata, None);

    let schema = stream.schema().clone();
    // there should be at least one record batch in the stream
    let stream_state = stream
        .next()
        .ok_or(CodecError::MissingRecordBatch)?
        .map_err(CodecError::ArrowSerialization)?;

    match stream_state {
        read::StreamState::Waiting => Err(CodecError::UnexpectedStreamState),
        read::StreamState::Some(chunk) => Ok((schema, chunk)),
    }
}

#[cfg(test)]
mod tests {

    use arrow2::array::Utf8Array as ArrowUtf8Array;
    use arrow2::chunk::Chunk as ArrowChunk;
    use arrow2::{
        array::Int32Array as ArrowInt32Array, datatypes::Field as ArrowField,
        datatypes::Schema as ArrowSchema,
    };
    use re_dataframe::external::re_chunk::{Chunk, RowId};
    use re_dataframe::TransportChunk;
    use re_log_types::StoreId;
    use re_log_types::{example_components::MyPoint, Timeline};

    use crate::v0::RecordingMetadata;
    use crate::{
        codec::{decode, encode, CodecError, TransportMessageV0},
        v0::EncoderVersion,
    };

    fn get_test_chunk() -> Chunk {
        let row_id1 = RowId::new();
        let row_id2 = RowId::new();

        let timepoint1 = [
            (Timeline::log_time(), 100),
            (Timeline::new_sequence("frame"), 1),
        ];
        let timepoint2 = [
            (Timeline::log_time(), 104),
            (Timeline::new_sequence("frame"), 1),
        ];

        let points1 = &[MyPoint::new(1.0, 1.0)];
        let points2 = &[MyPoint::new(2.0, 2.0)];

        Chunk::builder("mypoints".into())
            .with_component_batches(row_id1, timepoint1, [points1 as _])
            .with_component_batches(row_id2, timepoint2, [points2 as _])
            .build()
            .unwrap()
    }

    #[test]
    fn test_message_v0_no_data() {
        let msg = TransportMessageV0::NoData;
        let data = msg.to_bytes().unwrap();
        let decoded = TransportMessageV0::from_bytes(&data).unwrap();
        assert!(matches!(decoded, TransportMessageV0::NoData));
    }

    #[test]
    fn test_message_v0_record_batch() {
        let expected_chunk = get_test_chunk();

        let msg = TransportMessageV0::RecordBatch(expected_chunk.clone().to_transport().unwrap());
        let data = msg.to_bytes().unwrap();
        let decoded = TransportMessageV0::from_bytes(&data).unwrap();

        #[allow(clippy::match_wildcard_for_single_variants)]
        match decoded {
            TransportMessageV0::RecordBatch(transport) => {
                let decoded_chunk = Chunk::from_transport(&transport).unwrap();
                assert_eq!(expected_chunk, decoded_chunk);
            }
            _ => panic!("unexpected message type"),
        }
    }

    #[test]
    fn test_invalid_batch_data() {
        let data = vec![2, 3, 4]; // '1' is NO_DATA message header
        let decoded = TransportMessageV0::from_bytes(&data);

        assert!(matches!(
            decoded.err().unwrap(),
            CodecError::ArrowSerialization(_)
        ));
    }

    #[test]
    fn test_unknown_header() {
        let data = vec![3];
        let decoded = TransportMessageV0::from_bytes(&data);
        assert!(decoded.is_err());

        assert!(matches!(
            decoded.err().unwrap(),
            CodecError::UnknownMessageHeader
        ));
    }

    #[test]
    fn test_v0_codec() {
        let expected_chunk = get_test_chunk();

        let encoded = encode(
            EncoderVersion::V0,
            expected_chunk.clone().to_transport().unwrap(),
        )
        .unwrap();
        let decoded = decode(EncoderVersion::V0, &encoded).unwrap().unwrap();
        let decoded_chunk = Chunk::from_transport(&decoded).unwrap();

        assert_eq!(expected_chunk, decoded_chunk);
    }

    #[test]
    fn test_recording_metadata_serialization() {
        let expected_schema = ArrowSchema::from(vec![
            ArrowField::new("id", arrow2::datatypes::DataType::Utf8, false),
            ArrowField::new("my_int", arrow2::datatypes::DataType::Int32, false),
        ]);

        let id = ArrowUtf8Array::<i32>::from_slice(["some_id"]);
        let my_ints = ArrowInt32Array::from_slice([42]);
        let expected_chunk = ArrowChunk::new(vec![Box::new(id) as _, Box::new(my_ints) as _]);
        let metadata_tc = TransportChunk {
            schema: expected_schema.clone(),
            data: expected_chunk.clone(),
        };

        let metadata = RecordingMetadata::try_from(EncoderVersion::V0, &metadata_tc).unwrap();
        assert_eq!(
            StoreId::from_string(re_log_types::StoreKind::Recording, "some_id".to_owned()),
            metadata.id().unwrap()
        );

        let tc = metadata.data().unwrap();

        assert_eq!(expected_schema, tc.schema);
        assert_eq!(expected_chunk, tc.data);
    }

    #[test]
    fn test_recording_metadata_fails_with_non_unit_batch() {
        let expected_schema = ArrowSchema::from(vec![ArrowField::new(
            "my_int",
            arrow2::datatypes::DataType::Int32,
            false,
        )]);
        // more than 1 row in the batch
        let my_ints = ArrowInt32Array::from_slice([41, 42]);

        let expected_chunk = ArrowChunk::new(vec![Box::new(my_ints) as _]);
        let metadata_tc = TransportChunk {
            schema: expected_schema.clone(),
            data: expected_chunk,
        };

        let metadata = RecordingMetadata::try_from(EncoderVersion::V0, &metadata_tc);

        assert!(matches!(
            metadata.err().unwrap(),
            CodecError::InvalidArgument(_)
        ));
    }
}