irox_stats/
streams.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
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
// SPDX-License-Identifier: MIT
// Copyright 2025 IROX Contributors
//

//!
//! Streaming data encoders and decoders

extern crate alloc;
use crate::cfg_feature_miniz;
use alloc::boxed::Box;
use alloc::collections::BTreeMap;
use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::fmt::UpperHex;
use core::ops::{Add, BitXor, DerefMut, Sub};
use irox_bits::{BitsWrapper, Error, MutBits, SharedROCounter, WriteToBEBits};
use irox_tools::codec::{EncodeVByteTo, ZagZig, ZigZag};
use irox_tools::{ToSigned, ToUnsigned};
use irox_types::NumberSigned;

pub trait Stream<T: Streamable> {
    fn write_value(&mut self, value: T) -> Result<usize, Error>;
    fn flush(&mut self) -> Result<(), Error> {
        Ok(())
    }
    fn written_stats(&self) -> String {
        String::new()
    }
}
pub trait Decoder<T> {
    fn next(&mut self) -> Result<Option<T>, Error>;
}

pub trait Streamable: Sized + Default + Copy + WriteToBEBits {}
impl<T> Streamable for T where T: Sized + Default + Copy + WriteToBEBits {}
pub trait StreamableVByte:
    Sized + Default + Copy + Sub<Output: EncodeVByteTo + UpperHex> + EncodeVByteTo + WriteToBEBits + Sub
{
}
impl<T> StreamableVByte for T where
    T: Sized
        + Default
        + Copy
        + Sub<Output: EncodeVByteTo + UpperHex>
        + EncodeVByteTo
        + WriteToBEBits
        + Sub
{
}
pub trait ValueOperation<'a, T> {
    fn encode(&'a mut self, value: &T) -> Result<T, Error>;
}
pub struct CompositeStream<'a, T: Streamable, B: MutBits> {
    writer: &'a mut B,
    operations: Vec<Box<dyn ValueOperation<'a, T>>>,
}
impl<'a, T: Streamable, B: MutBits> CompositeStream<'a, T, B> {
    pub fn new(writer: &'a mut B) -> CompositeStream<'a, T, B> {
        Self {
            writer,
            operations: Vec::new(),
        }
    }
    pub fn and_then<V: ValueOperation<'a, T> + 'static>(&mut self, value: Box<V>) {
        self.operations.push(value);
    }
    pub fn write_value(&'a mut self, value: T) -> Result<usize, Error> {
        let mut v = value;
        for op in &mut self.operations {
            v = op.encode(&v)?;
        }
        WriteToBEBits::write_be_to(&value, self.writer)
    }
}

pub struct DeltaOperation<T> {
    last_value: T,
}
impl<'a, T: Sub<T, Output = T> + Copy> ValueOperation<'a, T> for DeltaOperation<T> {
    fn encode(&'a mut self, value: &T) -> Result<T, Error> {
        let out = *value - self.last_value;
        self.last_value = out;
        Ok(out)
    }
}
pub struct VByteOperation;
impl<'a, T: Sub<T, Output = T> + Copy> ValueOperation<'a, T> for VByteOperation {
    fn encode(&'a mut self, _value: &T) -> Result<T, Error> {
        todo!()
    }
}

///
/// A stream impl that writes the difference between the last value and the current
/// value to the provided [`MutBits`] writer.  The previous value is initialized to 0.
pub struct DeltaStream<T: Streamable> {
    last_value: T,
    writer: Box<dyn Stream<T>>,
}

impl<T: Streamable> DeltaStream<T> {
    ///
    /// Create a new stream impl
    pub fn new(writer: Box<dyn Stream<T>>) -> Self {
        DeltaStream {
            last_value: Default::default(),
            writer,
        }
    }
}
impl<S: Streamable + ToSigned<Output = T>, T: Streamable + NumberSigned + Sub<Output = T>> Stream<S>
    for DeltaStream<T>
{
    ///
    /// Deltifies the value against the previous value and writes it out.
    fn write_value(&mut self, value: S) -> Result<usize, Error> {
        let value = ToSigned::to_signed(value);
        let delta = value - self.last_value;
        self.last_value = value;
        self.writer.write_value(delta)
    }

    fn flush(&mut self) -> Result<(), Error> {
        self.writer.flush()
    }
    fn written_stats(&self) -> String {
        self.writer.written_stats()
    }
}
pub struct AddingDecoder<T: Streamable> {
    last_value: T,
    reader: Box<dyn Decoder<T>>,
}
impl<T: Streamable> AddingDecoder<T> {
    pub fn new(reader: Box<dyn Decoder<T>>) -> Self {
        AddingDecoder {
            last_value: Default::default(),
            reader,
        }
    }
}
impl<T: Streamable + ToUnsigned<Output = R> + Add<Output = T>, R: Streamable> Decoder<R>
    for AddingDecoder<T>
{
    fn next(&mut self) -> Result<Option<R>, Error> {
        let a = self.reader.next()?;
        let Some(a) = a else {
            return Ok(None);
        };
        let next = a + self.last_value;
        self.last_value = next;
        let next = ToUnsigned::to_unsigned(next);
        Ok(Some(next))
    }
}

pub struct ZigZagStream<T> {
    writer: Box<dyn Stream<T>>,
}
impl<T: Streamable> ZigZagStream<T> {
    pub fn new(writer: Box<dyn Stream<T>>) -> Self {
        Self { writer }
    }
}
impl<D: Streamable, S: Streamable + ZigZag<Output = D>> Stream<S> for ZigZagStream<D> {
    fn write_value(&mut self, value: S) -> Result<usize, Error> {
        self.writer.write_value(ZigZag::zigzag(value))
    }
}

pub struct ZigZagDecoder<T> {
    reader: Box<dyn Decoder<T>>,
}
impl<T: Streamable> ZigZagDecoder<T> {
    pub fn new(reader: Box<dyn Decoder<T>>) -> Self {
        Self { reader }
    }
}
impl<D: Streamable, S: Streamable + ZagZig<Output = D>> Decoder<D> for ZigZagDecoder<S> {
    fn next(&mut self) -> Result<Option<D>, Error> {
        let a = self.reader.next()?;
        let Some(a) = a else {
            return Ok(None);
        };
        let a = ZagZig::zagzig(a);
        Ok(Some(a))
    }
}

///
/// Basic stream to convert from a [`f64`] to a [`u64`]
pub struct F64ToU64Stream {
    writer: Box<dyn Stream<u64>>,
}
impl F64ToU64Stream {
    pub fn new(writer: Box<dyn Stream<u64>>) -> Self {
        Self { writer }
    }
}
impl Stream<f64> for F64ToU64Stream {
    fn write_value(&mut self, value: f64) -> Result<usize, Error> {
        self.writer.write_value(value.to_bits())
    }
}
pub struct U64ToF64Decoder {
    reader: Box<dyn Decoder<u64>>,
}
impl U64ToF64Decoder {
    pub fn new(reader: Box<dyn Decoder<u64>>) -> Self {
        Self { reader }
    }
}
impl Decoder<f64> for U64ToF64Decoder {
    fn next(&mut self) -> Result<Option<f64>, Error> {
        let Some(val) = self.reader.next()? else {
            return Ok(None);
        };
        Ok(Some(f64::from_bits(val)))
    }
}

pub struct XorDeltaStream<T> {
    last_value: T,
    writer: Box<dyn Stream<T>>,
}
impl<T: Sized + Default> XorDeltaStream<T> {
    pub fn new(writer: Box<dyn Stream<T>>) -> Self {
        Self {
            writer,
            last_value: Default::default(),
        }
    }
}
impl<T: Streamable + BitXor<Output = T> + Copy> Stream<T> for XorDeltaStream<T> {
    fn write_value(&mut self, value: T) -> Result<usize, Error> {
        let out = BitXor::bitxor(self.last_value, value);
        self.last_value = value;
        self.writer.write_value(out)
    }
    fn flush(&mut self) -> Result<(), Error> {
        self.writer.flush()
    }

    fn written_stats(&self) -> String {
        self.writer.written_stats()
    }
}
impl Stream<f64> for XorDeltaStream<u64> {
    fn write_value(&mut self, value: f64) -> Result<usize, Error> {
        let value = value.to_bits();
        self.write_value(value)
    }

    fn flush(&mut self) -> Result<(), Error> {
        self.writer.flush()
    }

    fn written_stats(&self) -> String {
        self.writer.written_stats()
    }
}

pub struct VByteIntStream<'a, B: MutBits> {
    writer: BitsWrapper<'a, B>,
}
impl<'a, B: MutBits> VByteIntStream<'a, B> {
    pub fn new(writer: BitsWrapper<'a, B>) -> Self {
        Self { writer }
    }
}
impl<'a, B: MutBits, T: StreamableVByte + WriteToBEBits> Stream<T> for VByteIntStream<'a, B> {
    fn write_value(&mut self, value: T) -> Result<usize, Error> {
        EncodeVByteTo::encode_vbyte_to(&value, self.writer.deref_mut())
    }
}
macro_rules! impl_mutbits_for_stream {
    () => {
        fn write_u8(&mut self, val: u8) -> Result<(), Error> {
            self.write_value(val)?;
            Ok(())
        }

        fn write_be_u16(&mut self, val: u16) -> Result<(), Error> {
            self.write_value(val)?;
            Ok(())
        }

        fn write_be_u32(&mut self, val: u32) -> Result<(), Error> {
            self.write_value(val)?;
            Ok(())
        }

        fn write_be_u64(&mut self, val: u64) -> Result<(), Error> {
            self.write_value(val)?;
            Ok(())
        }

        fn write_be_u128(&mut self, val: u128) -> Result<(), Error> {
            self.write_value(val)?;
            Ok(())
        }
    };
}
impl<'a, B: MutBits> MutBits for VByteIntStream<'a, B> {
    impl_mutbits_for_stream!();
}

///
/// A stream impl that writes the varint-encoded difference between the last
/// value and the current value to the provided [`MutBits`] writer.  The previous
/// value is initialized to 0.
pub struct VByteDeltaIntStream<'a, T, B: MutBits> {
    last_value: T,
    writer: VByteIntStream<'a, B>,
}

impl<'a, T: Streamable, B: MutBits> VByteDeltaIntStream<'a, T, B> {
    /// Creates a new stream
    pub fn new(writer: BitsWrapper<'a, B>) -> VByteDeltaIntStream<T, B> {
        VByteDeltaIntStream {
            last_value: Default::default(),
            writer: VByteIntStream::new(writer),
        }
    }
}
impl<
        'a,
        T: Streamable + Sub<Output = T> + EncodeVByteTo + UpperHex + Sub<T> + NumberSigned,
        B: MutBits,
    > Stream<T> for VByteDeltaIntStream<'a, T, B>
{
    fn write_value(&mut self, value: T) -> Result<usize, Error> {
        let delta = value - self.last_value;
        self.last_value = value;
        self.writer.write_value(delta)
    }
}

cfg_feature_miniz! {
    use miniz_oxide::deflate::core::{compress_to_output, CompressorOxide, TDEFLFlush, TDEFLStatus};
    use miniz_oxide::deflate::CompressionLevel;
    use miniz_oxide::DataFormat;
    use alloc::collections::VecDeque;
    use irox_bits::{Bits, ErrorKind};
    use irox_tools::buf::{Buffer, RoundU8Buffer};

    pub struct CompressStream<'a, T: MutBits> {
        writer: BitsWrapper<'a, T>,
        inbuf: VecDeque<u8>,
        compressor: CompressorOxide,
        written: u64,
    }
    impl<'a, T: MutBits> CompressStream<'a, T> {
        pub fn new(writer: BitsWrapper<'a, T>) -> Self {
            let mut compressor = CompressorOxide::default();
            compressor.set_format_and_level(DataFormat::Raw, CompressionLevel::DefaultCompression as u8);
            Self {
                writer,
                inbuf: VecDeque::with_capacity(32768),
                compressor,
                written: 0,
            }
        }

        pub fn write_value<V: WriteToBEBits+Copy>(
                &mut self, value: V) -> Result<(), Error> {
            // println!("writing {value:08X}");
            WriteToBEBits::write_be_to(&value, &mut self.inbuf)?;
            if self.inbuf.len() < 32768 {
                return Ok(())
            }
            let (a,b) = self.inbuf.as_slices();
            let v = if a.is_empty() {
                b
            } else  {
                a
            };

            let (status, size) = compress_to_output(&mut self.compressor, v, TDEFLFlush::None, |out| {
                self.written = self.written.wrapping_add(out.len() as u64);
                self.writer.write_all_bytes(out).is_ok()
            });
            if status != TDEFLStatus::Okay {
                return Err(ErrorKind::BrokenPipe.into());
            }
            self.inbuf.drain(0..size);
            Ok(())
        }

         pub fn flush(&mut self) -> Result<(), Error> {
            loop {
                let v = self.inbuf.make_contiguous();
                let (status, size) = compress_to_output(&mut self.compressor, v, TDEFLFlush::Finish, |out| {
                    self.written = self.written.wrapping_add(out.len() as u64);
                    self.writer.write_all_bytes(out).is_ok()
                });
                self.inbuf.drain(0..size);
                return match status {
                    TDEFLStatus::BadParam => {
                        Err(ErrorKind::InvalidInput.into())
                    }
                    TDEFLStatus::PutBufFailed => {
                        Err(ErrorKind::BrokenPipe.into())
                    }
                    TDEFLStatus::Okay => {
                        continue;
                    }
                    TDEFLStatus::Done => {
                        break;
                    }
                }
            }
            Ok(())
        }
        pub fn written(&self) -> u64 {
            self.written
        }
    }
    impl<'a, B: MutBits> Drop for CompressStream<'a, B> {
        /// Make sure the buffer is fully flushed on drop
        fn drop(&mut self) {
            let _ = self.flush();
        }
    }
    impl<'a, B: MutBits> MutBits for CompressStream<'a, B> {
        impl_mutbits_for_stream!();
    }

    impl<'a, B: MutBits, T: Streamable> Stream<T> for CompressStream<'a, B> {
        fn write_value(&mut self, value: T) -> Result<usize, Error> {
            WriteToBEBits::write_be_to(&value, self)
        }

        fn flush(&mut self) -> Result<(), Error> {
            Self::flush(self)
        }

        fn written_stats(&self) -> String {
            format!("{}", self.written)
        }

    }

    ///
    /// A stream impl that writes the deflated, varint-encoded difference between
    /// the last value and the current value to the provided [`MutBits`] writer.
    /// The previous value is initialized to 0.
    pub struct DeltaCompressStream<'a, T: Streamable+Copy, B: MutBits> {
        last_value: T,
        compressor: CompressStream<'a, B>
    }
    impl<'a, T: Streamable+Copy, B: MutBits> DeltaCompressStream<'a, T, B> {
        /// Create a new stream
        pub fn new(writer: BitsWrapper<'a, B>) -> DeltaCompressStream<'a, T, B> {
            DeltaCompressStream {
                last_value: Default::default(),
                compressor: CompressStream::new(writer),
            }
        }

        ///
        /// Encodes & writes the value out.
        pub fn write_value(&mut self, value: T) -> Result<(), Error> {

            let delta = value; //value.wrapping_sub(self.last_value);
            self.last_value = value;
            // println!("Delta: {delta:08X}");
            // EncodeVByteTo::encode_vbyte_to(&delta, &mut self.compressor)?;
            self.compressor.write_value(delta)?;
            Ok(())
        }

        pub fn flush(&mut self) -> Result<(), Error> {
            self.compressor.flush()
        }

        pub fn written(&self) -> u64 {
            self.compressor.written()
        }

    }
    impl<'a, T: Streamable+Copy, B: MutBits> Drop for DeltaCompressStream<'a, T, B> {
        /// Make sure the buffer is fully flushed on drop
        fn drop(&mut self) {
            let _ = self.flush();
        }
    }

    pub struct InflateStream<'a, B: irox_bits::BufBits> {
        reader: BitsWrapper<'a, B>,
        out_buffer: RoundU8Buffer<4096>,
        inflater: miniz_oxide::inflate::stream::InflateState,
    }
    impl<'a, B: irox_bits::BufBits> InflateStream<'a, B> {
        pub fn new(reader: BitsWrapper<'a, B>) -> Self {
            let inflater = miniz_oxide::inflate::stream::InflateState::new(DataFormat::Raw);
            let out_buffer = RoundU8Buffer::<4096>::default();
            Self {
                reader,
                out_buffer,
                inflater
            }
        }
        pub fn has_more(&mut self) -> Result<bool, Error> {
            if self.out_buffer.is_empty() {
                self.try_fill_buf()?;
            }
            Ok(!self.out_buffer.is_empty())
        }
        fn try_fill_buf(&mut self) -> Result<(), Error> {
            if !self.out_buffer.is_empty() {
                return Ok(());
            }
            self.out_buffer.clear();

            let outbuf = self.out_buffer.as_ref_mut_available();
            let inbuf = self.reader.fill_buf()?;
            let res = miniz_oxide::inflate::stream::inflate(&mut self.inflater, inbuf, outbuf, miniz_oxide::MZFlush::None);
            self.reader.consume(res.bytes_consumed);
            self.out_buffer.mark_some_used(res.bytes_written)?;
            Ok(())
        }
    }
    impl<'a, B: irox_bits::BufBits> Bits for InflateStream<'a, B> {
        fn next_u8(&mut self) -> Result<Option<u8>, Error> {
            if let Some(v) = self.out_buffer.pop_front() {
                return Ok(Some(v));
            }
            self.try_fill_buf()?;

            Ok(self.out_buffer.pop_front())
        }

    }
}
#[derive(Default)]
pub struct StreamStageStats {
    stats: BTreeMap<String, Box<dyn Fn() -> String>>,
}
impl StreamStageStats {
    pub fn stage(&mut self, name: &str, value: Box<dyn Fn() -> String>) {
        self.stats.insert(name.to_string(), value);
    }
    pub fn stage_counting(&mut self, name: &str, value: SharedROCounter) {
        self.stage(name, Box::new(move || value.get_count().to_string()))
    }
    pub fn stats(&self) -> Vec<String> {
        self.stats
            .iter()
            .map(|(k, v)| format!("{k}: {}", v()))
            .collect::<Vec<String>>()
    }
}

#[cfg(all(test, feature = "miniz", feature = "std"))]
mod test {
    use crate::streams::{BitsWrapper, DeltaCompressStream};
    use irox_bits::Error;
    use irox_time::Time64;
    use irox_units::units::duration::Duration;
    use std::time::Instant;

    ///
    /// Writes out 8*1M = 8MB to the underlying stream.
    #[test]
    pub fn test1() -> Result<(), Error> {
        let mut buf = Vec::with_capacity(32768);
        let mut input = 0;
        let start = Instant::now();
        let written = {
            let wrapper = BitsWrapper::Borrowed(&mut buf);
            let mut vbout = DeltaCompressStream::<u64, _>::new(wrapper);

            for i in 0..4_000_000 {
                input += 8;
                vbout.write_value(i)?;
            }
            vbout.flush()?;
            drop(vbout);
            buf.len()
        };
        let end = start.elapsed();
        // irox_tools::hex::HexDump::hexdump(&buf);
        let ratio = 1. - (written as f64 / input as f64);
        let ratio = ratio * 100.;
        let ubps = input as f64 / end.as_secs_f64() / 1e6;
        println!("Turned {input} bytes into {written} = {ratio:02.}% reduction = {ubps:02.02}MB/s");
        Ok(())
    }

    #[test]
    pub fn test_nanos() -> Result<(), Error> {
        let mut buf = Vec::with_capacity(32768);
        let mut input = Time64::now();
        let incr = Duration::from_millis(100);
        let start = Instant::now();
        let count = 2_000_000;
        let written = {
            let wrapper = BitsWrapper::Borrowed(&mut buf);
            let mut vbout = DeltaCompressStream::new(wrapper);

            for _ in 0..count {
                input += incr;
                vbout.write_value(input.as_u64())?;
            }
            vbout.flush()?;
            drop(vbout);
            buf.len()
        };
        let count = count * 8;
        let end = start.elapsed();
        // irox_tools::hex::HexDump::hexdump(&buf);
        let ratio = 1. - (written as f64 / count as f64);
        let ratio = ratio * 100.;
        let ubps = count as f64 / end.as_secs_f64() / 1e6;
        println!("Turned {count} bytes into {written} = {ratio:02.}% reduction = {ubps:02.02}MB/s");

        Ok(())
    }
}