meathook-rs 0.4.0

A polling runtime with composable, durable sinks
Documentation
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
//! Pluggable window encoders: an [`Encoder`] turns one window of records
//! into the bytes of a single file. Records stay plain
//! `#[derive(Serialize)]` structs — no format-specific builders — and a
//! terminal sink's wire format is swapped by choosing an encoder.
//! [`ParquetEncoder`] (feature `parquet`) stays the `HfSink` default;
//! [`JsonEncoder`] is always available; [`CsvEncoder`] is gated behind the
//! `csv` feature.

use std::error;
#[cfg(feature = "parquet")]
use std::marker::PhantomData;

#[cfg(feature = "parquet")]
use arrow::datatypes::FieldRef;
#[cfg(feature = "parquet")]
use parquet::arrow::ArrowWriter;
#[cfg(feature = "parquet")]
use parquet::basic::{Compression, ZstdLevel};
#[cfg(feature = "parquet")]
use parquet::errors::ParquetError;
#[cfg(feature = "parquet")]
use parquet::file::properties::WriterProperties;
use serde::Serialize;
use serde::de::DeserializeOwned;
#[cfg(feature = "parquet")]
use serde_arrow::schema::{SchemaLike, TracingOptions};

/// Encodes one window of records into the bytes of a single file.
///
/// Implementations must succeed on an empty slice (a valid empty file),
/// since callers may hand over drained-but-empty windows.
pub trait Encoder: Send + Sync + 'static {
    /// Error produced when encoding fails.
    type Error: error::Error + Send + Sync + 'static;

    /// File extension (no leading dot) for files this encoder produces,
    /// e.g. `"parquet"`.
    const EXT: &'static str;

    /// Encode records into an in-memory file.
    ///
    /// # Errors
    ///
    /// Returns the encoder's error if serialization fails.
    fn encode<R: Serialize + DeserializeOwned>(
        &self,
        records: &[R],
    ) -> Result<Vec<u8>, Self::Error>;
}

/// Encodes a window as one JSON array per file.
#[derive(Debug, Clone, Copy, Default)]
pub struct JsonEncoder;

impl Encoder for JsonEncoder {
    type Error = serde_json::Error;
    const EXT: &'static str = "json";

    fn encode<R: Serialize + DeserializeOwned>(
        &self,
        records: &[R],
    ) -> Result<Vec<u8>, Self::Error> {
        serde_json::to_vec(records)
    }
}

/// Error encoding records to parquet.
#[cfg(feature = "parquet")]
#[derive(Debug, thiserror::Error)]
pub enum ParquetEncodeError {
    #[error("failed to derive arrow schema from record type: {0}")]
    Schema(#[source] serde_arrow::Error),
    #[error("failed to build record batch: {0}")]
    Batch(#[source] serde_arrow::Error),
    #[error("failed to write parquet: {0}")]
    Parquet(#[from] ParquetError),
}

#[cfg(feature = "parquet")]
mod private {
    pub trait Sealed {}
}

/// A type-level parquet compression policy.
///
/// This trait is sealed. The built-in policies are [`Uncompressed`] and
/// [`Zstd<LEVEL>`](Zstd), where only levels `1..=22` implement this trait.
#[cfg(feature = "parquet")]
pub trait ParquetCompression: private::Sealed + Send + Sync + 'static {
    #[doc(hidden)]
    fn parquet_compression() -> Result<Compression, ParquetError>;
}

/// Type-level policy for uncompressed parquet output.
#[cfg(feature = "parquet")]
#[derive(Debug, Clone, Copy, Default)]
pub struct Uncompressed;

#[cfg(feature = "parquet")]
impl private::Sealed for Uncompressed {}

#[cfg(feature = "parquet")]
impl ParquetCompression for Uncompressed {
    fn parquet_compression() -> Result<Compression, ParquetError> {
        Ok(Compression::UNCOMPRESSED)
    }
}

/// Type-level policy for zstd-compressed parquet output.
///
/// `LEVEL` defaults to `1`. Only levels `1..=22` implement
/// [`ParquetCompression`], so an encoder with an invalid level cannot be
/// constructed.
///
/// ```compile_fail
/// use meathook::{ParquetEncoder, Zstd};
///
/// let encoder = ParquetEncoder::<Zstd<23>>::new();
/// ```
#[cfg(feature = "parquet")]
#[derive(Debug, Clone, Copy, Default)]
pub struct Zstd<const LEVEL: u8 = 1>;

#[cfg(feature = "parquet")]
macro_rules! impl_zstd_levels {
    ($($level:literal),* $(,)?) => {
        $(
            impl private::Sealed for Zstd<$level> {}

            impl ParquetCompression for Zstd<$level> {
                fn parquet_compression() -> Result<Compression, ParquetError> {
                    ZstdLevel::try_new($level).map(Compression::ZSTD)
                }
            }
        )*
    };
}

#[cfg(feature = "parquet")]
impl_zstd_levels!(
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
);

/// Encodes a window into a parquet file held in memory.
///
/// The arrow schema is derived from `R` itself (not sampled from values, so
/// an empty slice still produces a valid zero-row file), which is why
/// `DeserializeOwned` is required alongside `Serialize`.
///
/// The default compression policy is [`Uncompressed`]. Select zstd and its
/// level in the encoder type:
///
/// ```
/// use meathook::{ParquetEncoder, Zstd};
///
/// let uncompressed = ParquetEncoder::default();
/// let zstd_1 = ParquetEncoder::<Zstd>::new();
/// let zstd_3 = ParquetEncoder::<Zstd<3>>::new();
/// ```
#[cfg(feature = "parquet")]
#[derive(Debug, Clone, Copy)]
pub struct ParquetEncoder<C = Uncompressed> {
    _compression: PhantomData<C>,
}

#[cfg(feature = "parquet")]
impl<C: ParquetCompression> ParquetEncoder<C> {
    /// Creates an encoder using the compression policy `C`.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            _compression: PhantomData,
        }
    }

    fn writer_properties() -> Result<WriterProperties, ParquetError> {
        Ok(WriterProperties::builder()
            .set_compression(C::parquet_compression()?)
            .build())
    }
}

#[cfg(feature = "parquet")]
impl Default for ParquetEncoder {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "parquet")]
impl<C: ParquetCompression> Encoder for ParquetEncoder<C> {
    type Error = ParquetEncodeError;
    const EXT: &'static str = "parquet";

    /// # Errors
    ///
    /// Returns an error if schema derivation, record batch construction, or
    /// parquet writing fails.
    fn encode<R: Serialize + DeserializeOwned>(
        &self,
        records: &[R],
    ) -> Result<Vec<u8>, Self::Error> {
        let fields = Vec::<FieldRef>::from_type::<R>(TracingOptions::default())
            .map_err(ParquetEncodeError::Schema)?;
        let batch =
            serde_arrow::to_record_batch(&fields, &records).map_err(ParquetEncodeError::Batch)?;

        let mut buf = vec![];
        let mut writer =
            ArrowWriter::try_new(&mut buf, batch.schema(), Some(Self::writer_properties()?))?;
        writer.write(&batch)?;
        writer.close()?;
        Ok(buf)
    }
}

/// Error encoding records to CSV.
#[cfg(feature = "csv")]
#[derive(Debug, thiserror::Error)]
pub enum CsvError {
    #[error("failed to serialize record to csv: {0}")]
    Serialize(#[from] csv::Error),
    #[error("failed to flush csv writer: {0}")]
    Flush(#[from] std::io::Error),
}

/// Encodes a window as one CSV file.
///
/// The header row is derived from the record's field names (the csv
/// crate's default); records must be flat — nested structs fail with
/// [`CsvError::Serialize`]. An empty slice encodes to an empty file, since
/// headers are only written together with the first record.
#[cfg(feature = "csv")]
#[derive(Debug, Clone, Copy, Default)]
pub struct CsvEncoder;

#[cfg(feature = "csv")]
impl Encoder for CsvEncoder {
    type Error = CsvError;
    const EXT: &'static str = "csv";

    fn encode<R: Serialize + DeserializeOwned>(
        &self,
        records: &[R],
    ) -> Result<Vec<u8>, Self::Error> {
        let mut writer = csv::Writer::from_writer(Vec::new());
        for record in records {
            writer.serialize(record)?;
        }
        writer
            .into_inner()
            .map_err(|e| CsvError::Flush(e.into_error()))
    }
}

#[cfg(test)]
mod tests {
    use serde::Deserialize;

    use super::*;

    #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
    struct Sample {
        station_id: String,
        timestamp: String,
        value: f64,
    }

    fn samples() -> Vec<Sample> {
        vec![
            Sample {
                station_id: "S100".into(),
                timestamp: "2026-06-12T08:00:00+08:00".into(),
                value: 29.4,
            },
            Sample {
                station_id: "S117".into(),
                timestamp: "2026-06-12T08:00:00+08:00".into(),
                value: 30.1,
            },
        ]
    }

    #[cfg(feature = "parquet")]
    mod parquet_encoder {
        use arrow::array::RecordBatch;
        use parquet::arrow::arrow_reader::ParquetRecordBatchReaderBuilder;
        use parquet::basic::{Compression, ZstdLevel};
        use parquet::file::metadata::{ColumnChunkMetaData, RowGroupMetaData};

        use super::*;

        fn compressions(bytes: Vec<u8>) -> Vec<Compression> {
            let builder =
                ParquetRecordBatchReaderBuilder::try_new(bytes::Bytes::from(bytes)).unwrap();
            builder
                .metadata()
                .row_groups()
                .iter()
                .flat_map(RowGroupMetaData::columns)
                .map(ColumnChunkMetaData::compression)
                .collect()
        }

        #[test]
        fn parquet_round_trip() {
            let records = samples();

            let encoder = ParquetEncoder::default();
            let bytes = encoder.encode(&records).unwrap();

            assert!(
                compressions(bytes.clone())
                    .iter()
                    .all(|compression| *compression == Compression::UNCOMPRESSED)
            );

            let reader = ParquetRecordBatchReaderBuilder::try_new(bytes::Bytes::from(bytes))
                .unwrap()
                .build()
                .unwrap();
            let batches: Vec<_> = reader.collect::<Result<_, _>>().unwrap();
            assert_eq!(batches.iter().map(RecordBatch::num_rows).sum::<usize>(), 2);

            let round_tripped = serde_arrow::from_record_batch::<Vec<Sample>>(&batches[0]).unwrap();

            assert_eq!(round_tripped, records);
        }

        #[test]
        fn default_policy_uses_uncompressed_codec() {
            let bytes = ParquetEncoder::default().encode(&samples()).unwrap();

            assert!(
                compressions(bytes)
                    .iter()
                    .all(|compression| *compression == Compression::UNCOMPRESSED)
            );
        }

        #[test]
        fn empty_slice_encodes_zero_row_file() {
            let encoded = [
                ParquetEncoder::default().encode::<Sample>(&[]).unwrap(),
                ParquetEncoder::<Zstd>::new().encode::<Sample>(&[]).unwrap(),
            ];

            for bytes in encoded {
                let reader = ParquetRecordBatchReaderBuilder::try_new(bytes::Bytes::from(bytes))
                    .unwrap()
                    .build()
                    .unwrap();
                let rows: usize = reader.map(|b| b.unwrap().num_rows()).sum();
                assert_eq!(rows, 0);
            }
        }

        #[test]
        fn zstd_default_level_round_trips() {
            let records = samples();
            let bytes = ParquetEncoder::<Zstd>::new().encode(&records).unwrap();

            assert!(
                compressions(bytes.clone())
                    .iter()
                    .all(|compression| { *compression == Compression::ZSTD(ZstdLevel::default()) })
            );

            let reader = ParquetRecordBatchReaderBuilder::try_new(bytes::Bytes::from(bytes))
                .unwrap()
                .build()
                .unwrap();
            let batches: Vec<_> = reader.collect::<Result<_, _>>().unwrap();
            let round_tripped = serde_arrow::from_record_batch::<Vec<Sample>>(&batches[0]).unwrap();

            assert_eq!(round_tripped, records);
        }

        #[test]
        fn zstd_explicit_level_is_applied() {
            let level = ZstdLevel::try_new(7).unwrap();
            let encoder = ParquetEncoder::<Zstd<7>>::new();
            assert_eq!(
                Zstd::<7>::parquet_compression().unwrap(),
                Compression::ZSTD(level)
            );

            let bytes = encoder.encode(&samples()).unwrap();

            assert!(
                compressions(bytes.clone())
                    .iter()
                    .all(|compression| matches!(compression, Compression::ZSTD(_)))
            );
            ParquetRecordBatchReaderBuilder::try_new(bytes::Bytes::from(bytes))
                .unwrap()
                .build()
                .unwrap()
                .collect::<Result<Vec<_>, _>>()
                .unwrap();
        }

        #[test]
        fn zstd_boundary_levels_are_applied() {
            let policies = [
                Zstd::<1>::parquet_compression().unwrap(),
                Zstd::<22>::parquet_compression().unwrap(),
            ];

            assert_eq!(
                policies,
                [
                    Compression::ZSTD(ZstdLevel::try_new(1).unwrap()),
                    Compression::ZSTD(ZstdLevel::try_new(22).unwrap()),
                ]
            );
        }

        #[test]
        fn zstd_shrinks_compressible_records() {
            let records: Vec<_> = (0..4_096)
                .map(|index| Sample {
                    station_id: format!("weather-station-{index:08}"),
                    timestamp: format!("2026-07-13T12:{:02}:{:02}+08:00", index / 60, index % 60),
                    value: 29.0 + f64::from(index % 10) / 10.0,
                })
                .collect();

            let uncompressed = ParquetEncoder::default().encode(&records).unwrap();
            let compressed = ParquetEncoder::<Zstd>::new().encode(&records).unwrap();

            assert!(compressed.len() < uncompressed.len());
        }
    }

    mod json_encoder {
        use super::*;

        #[test]
        fn json_round_trip() {
            let records = samples();
            let bytes = JsonEncoder.encode(&records).unwrap();
            let round_tripped: Vec<Sample> = serde_json::from_slice(&bytes).unwrap();
            assert_eq!(round_tripped, records);
        }

        #[test]
        fn empty_slice_encodes_empty_array() {
            assert_eq!(JsonEncoder.encode::<Sample>(&[]).unwrap(), b"[]");
        }
    }

    #[cfg(feature = "csv")]
    mod csv_encoder {
        use super::*;

        #[test]
        fn csv_round_trip_with_headers() {
            let records = samples();
            let bytes = CsvEncoder.encode(&records).unwrap();
            let round_tripped = ::csv::Reader::from_reader(bytes.as_slice())
                .deserialize()
                .collect::<Result<Vec<Sample>, _>>()
                .unwrap();
            assert_eq!(round_tripped, records);
        }

        #[test]
        fn empty_slice_encodes_empty_output() {
            assert!(CsvEncoder.encode::<Sample>(&[]).unwrap().is_empty());
        }
    }
}