sim-lib-stream-combinators 0.1.3

Lazy in-memory combinators for SIM stream packets.
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
use std::ops::RangeBounds;
use std::sync::{Arc, Mutex};

use sim_kernel::{
    Cx, Error, Event, EventKind, EventLedger, Ref, Result, Severity, Symbol, Tick, value_from_ref,
};
use sim_lib_stream_core::{
    StreamCassette, StreamDiagnostic, StreamItem, StreamMetadata, StreamPacket, StreamStats,
    TransportProfile,
};

use crate::stream::{Stream, StreamNode};

/// A fully captured stream: its metadata plus every packet it produced.
///
/// A recording is the materialized, replayable form of a finished stream. It is
/// produced by draining a [`Stream`] to `done` and can be replayed any number
/// of times, seeked into, or serialized to a transport cassette.
///
/// # Examples
///
/// ```
/// use sim_kernel::{Expr, Symbol};
/// use sim_lib_stream_core::{
///     BufferOverflowPolicy, BufferPolicy, StreamDirection, StreamItem, StreamMedia,
///     StreamMetadata, StreamPacket,
/// };
/// use sim_lib_stream_combinators::{record_bang, Stream};
///
/// let metadata = StreamMetadata::new(
///     Symbol::qualified("stream", "doc"),
///     StreamMedia::Data,
///     StreamDirection::Source,
///     Symbol::qualified("clock", "doc"),
///     BufferPolicy::bounded_with_overflow(8, BufferOverflowPolicy::DropNewest).unwrap(),
/// );
/// let item = StreamItem::new(StreamPacket::data(
///     Symbol::qualified("stream/data", "model-event"),
///     Expr::Nil,
/// ));
/// let stream = Stream::pull(metadata, vec![item.clone()]);
///
/// let recording = record_bang(&stream).unwrap();
/// assert_eq!(recording.len(), 1);
/// assert_eq!(recording.replay().take_packets(8).unwrap(), vec![item]);
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StreamRecording {
    metadata: StreamMetadata,
    items: Vec<StreamItem>,
}

impl StreamRecording {
    /// Builds a recording from explicit metadata and captured packets.
    pub fn new(metadata: StreamMetadata, items: Vec<StreamItem>) -> Self {
        Self { metadata, items }
    }

    /// Returns the metadata of the recorded stream.
    pub fn metadata(&self) -> &StreamMetadata {
        &self.metadata
    }

    /// Returns the captured packets in their recorded order.
    pub fn items(&self) -> &[StreamItem] {
        &self.items
    }

    /// Returns the number of captured packets.
    pub fn len(&self) -> usize {
        self.items.len()
    }

    /// Reports whether the recording captured no packets.
    pub fn is_empty(&self) -> bool {
        self.items.is_empty()
    }

    /// Returns a fresh stream that replays the captured packets.
    pub fn replay(&self) -> Stream {
        replay(self)
    }

    /// Replays the recording from the first packet matching `target`.
    pub fn seek(&self, target: SeekTarget) -> Stream {
        seek(self.replay(), target)
    }

    /// Serializes the recording into a transport cassette for `profile`.
    pub fn cassette(&self, profile: TransportProfile) -> Result<StreamCassette> {
        StreamCassette::from_items(
            self.metadata.clone(),
            self.items.clone(),
            profile,
            StreamStats {
                yielded: self.items.len() as u64,
                ..StreamStats::default()
            },
        )
    }
}

/// Where a [`seek`] should begin replaying within a recorded stream.
///
/// # Examples
///
/// ```
/// use sim_lib_stream_combinators::SeekTarget;
///
/// let by_index = SeekTarget::packet_index(2);
/// assert_eq!(by_index, SeekTarget::PacketIndex(2));
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SeekTarget {
    /// Start at the packet at this zero-based position in the stream.
    PacketIndex(usize),
    /// Start at the first packet bearing `index` on the named `clock`.
    ClockIndex {
        /// The clock whose tick index is matched.
        clock: Symbol,
        /// The tick index on `clock` to seek to.
        index: Ref,
    },
}

impl SeekTarget {
    /// Builds a [`SeekTarget::PacketIndex`] for the given position.
    pub fn packet_index(index: usize) -> Self {
        Self::PacketIndex(index)
    }

    /// Builds a [`SeekTarget::ClockIndex`] for the given clock and tick index.
    pub fn clock_index(clock: Symbol, index: Ref) -> Self {
        Self::ClockIndex { clock, index }
    }
}

/// Default ceiling on the packets captured by the unbounded recorders.
///
/// [`record_bang`] and [`record_cassette_bang`] drain a source to `done`; a live
/// or unbounded source never reaches `done`, so they cap capture at this many
/// packets and error rather than looping forever. Use [`record_bang_bounded`] /
/// [`record_cassette_bang_bounded`] to choose an explicit bound for a source
/// that may not terminate.
pub const DEFAULT_RECORD_ITEM_LIMIT: usize = 1 << 20;

/// Drains `source` to `done` and captures it as a [`StreamRecording`].
///
/// Errors if the stream is exhausted without reaching its terminal `done`, or if
/// it yields more than [`DEFAULT_RECORD_ITEM_LIMIT`] packets (a guard against a
/// live or unbounded source that never reaches `done`).
pub fn record_bang(source: &Stream) -> Result<StreamRecording> {
    record_bang_bounded(source, DEFAULT_RECORD_ITEM_LIMIT)
}

/// Drains `source` to `done`, capturing at most `max_items` packets.
///
/// Like [`record_bang`] but with a caller-chosen bound. A live or unbounded
/// source never reaches `done`; recording stops and returns an error once it has
/// pulled `max_items` packets, so the call cannot loop forever. Prefer this over
/// [`record_bang`] whenever the source may not terminate.
pub fn record_bang_bounded(source: &Stream, max_items: usize) -> Result<StreamRecording> {
    let mut items = Vec::new();
    while let Some(item) = source.next_packet()? {
        if items.len() >= max_items {
            return Err(Error::Eval(format!(
                "cannot record more than {max_items} packets; source may be live or unbounded"
            )));
        }
        items.push(item);
    }
    if !source.is_done()? {
        return Err(Error::Eval(
            "cannot record a stream that has not reached done".to_owned(),
        ));
    }
    Ok(StreamRecording::new(source.metadata().clone(), items))
}

/// Returns a fresh stream replaying every packet of `recording`.
pub fn replay(recording: &StreamRecording) -> Stream {
    Stream::pull(recording.metadata.clone(), recording.items.clone())
}

/// Records `source` to completion and serializes it to a cassette for `profile`.
///
/// Capture is bounded by [`DEFAULT_RECORD_ITEM_LIMIT`]; see [`record_bang`].
pub fn record_cassette_bang(source: &Stream, profile: TransportProfile) -> Result<StreamCassette> {
    record_bang(source)?.cassette(profile)
}

/// Records at most `max_items` packets of `source` and serializes them.
///
/// Bounded twin of [`record_cassette_bang`] for a live or unbounded source; see
/// [`record_bang_bounded`].
pub fn record_cassette_bang_bounded(
    source: &Stream,
    profile: TransportProfile,
    max_items: usize,
) -> Result<StreamCassette> {
    record_bang_bounded(source, max_items)?.cassette(profile)
}

/// Rebuilds a replayable stream from a serialized transport `cassette`.
pub fn replay_cassette(cassette: &StreamCassette) -> Result<Stream> {
    Ok(Stream::from_value(Arc::new(
        cassette.replay_stream_value()?,
    )))
}

/// Returns a stream that skips ahead in `source` to the first packet at `target`.
///
/// The stream then continues from that packet. If a live source is temporarily
/// empty before the target arrives, seeking stays pending; if the source reaches
/// terminal `done` without a match, the seek stream is empty.
pub fn seek(source: Stream, target: SeekTarget) -> Stream {
    Stream::new(SeekNode {
        source,
        target,
        state: Mutex::new(SeekState::Pending { skipped: 0 }),
    })
}

/// Reconstructs a recording from all of `run`'s events in `ledger`.
///
/// Convenience wrapper over [`record_events`] for an entire run.
pub fn record_ledger_run(
    cx: &mut Cx,
    metadata: StreamMetadata,
    ledger: &EventLedger,
    run: &Ref,
) -> Result<StreamRecording> {
    record_events(cx, metadata, ledger.events_for_run(run))
}

/// Reconstructs a recording from the events of `run` within `seq_range`.
///
/// Like [`record_ledger_run`] but limited to events whose sequence number
/// falls inside `seq_range`.
pub fn record_ledger_slice<R>(
    cx: &mut Cx,
    metadata: StreamMetadata,
    ledger: &EventLedger,
    run: &Ref,
    seq_range: R,
) -> Result<StreamRecording>
where
    R: RangeBounds<u64>,
{
    record_events(
        cx,
        metadata,
        ledger
            .events_for_run(run)
            .iter()
            .filter(|event| seq_range.contains(&event.seq)),
    )
}

/// Reconstructs a recording from an arbitrary sequence of kernel `events`.
///
/// Chunk events are decoded back into stream packets and diagnostic events into
/// diagnostic packets; a `done` event ends capture, a `failed` event errors,
/// and other event kinds are ignored.
pub fn record_events<'a>(
    cx: &mut Cx,
    metadata: StreamMetadata,
    events: impl IntoIterator<Item = &'a Event>,
) -> Result<StreamRecording> {
    let mut items = Vec::new();
    for event in events {
        match &event.kind {
            EventKind::Chunk { payload } => {
                items.push(item_from_payload(cx, payload, event.ticks.clone())?);
            }
            EventKind::Diagnostic(diagnostic) => {
                items.push(StreamItem::new(StreamPacket::Diagnostic(
                    diagnostic_packet(diagnostic),
                )));
            }
            EventKind::Done => break,
            EventKind::Failed(_) => {
                return Err(Error::Eval(
                    "cannot record a failed stream event slice".to_owned(),
                ));
            }
            EventKind::Started { .. }
            | EventKind::Claim { .. }
            | EventKind::Trace(_)
            | EventKind::EffectRequested { .. }
            | EventKind::EffectResolved { .. }
            | EventKind::Capture { .. }
            | EventKind::Card { .. }
            | EventKind::Final(_) => {}
        }
    }
    Ok(StreamRecording::new(metadata, items))
}

fn item_from_payload(cx: &mut Cx, payload: &Ref, ticks: Vec<Tick>) -> Result<StreamItem> {
    let value = value_from_ref(cx, payload)?;
    let packet = StreamPacket::try_from(value.object().as_expr(cx)?)?;
    StreamItem::with_ticks(packet, ticks)
}

fn diagnostic_packet(diagnostic: &sim_kernel::Diagnostic) -> StreamDiagnostic {
    let kind = diagnostic
        .code
        .clone()
        .unwrap_or_else(|| Symbol::qualified("stream/combinator", "Diagnostic"));
    let prefix = match diagnostic.severity {
        Severity::Error => "error",
        Severity::Warning => "warning",
        Severity::Info => "info",
        Severity::Note => "note",
    };
    StreamDiagnostic::new(kind, format!("{prefix}: {}", diagnostic.message))
}

struct SeekNode {
    source: Stream,
    target: SeekTarget,
    state: Mutex<SeekState>,
}

enum SeekState {
    Pending { skipped: usize },
    Ready,
    Drained,
}

enum SeekPoll {
    Found(StreamItem),
    Pending,
    Drained,
}

impl StreamNode for SeekNode {
    fn metadata(&self) -> &StreamMetadata {
        self.source.metadata()
    }

    fn next_packet(&self) -> Result<Option<StreamItem>> {
        let mut state = self
            .state
            .lock()
            .map_err(|_| Error::PoisonedLock("seek stream"))?;
        match *state {
            SeekState::Ready => self.source.next_packet(),
            SeekState::Drained => Ok(None),
            SeekState::Pending { ref mut skipped } => {
                let poll = seek_first(&self.source, &self.target, skipped)?;
                match poll {
                    SeekPoll::Found(item) => {
                        *state = SeekState::Ready;
                        Ok(Some(item))
                    }
                    SeekPoll::Pending => Ok(None),
                    SeekPoll::Drained => {
                        *state = SeekState::Drained;
                        Ok(None)
                    }
                }
            }
        }
    }

    fn is_done(&self) -> Result<bool> {
        let state = self
            .state
            .lock()
            .map_err(|_| Error::PoisonedLock("seek stream"))?;
        match *state {
            SeekState::Drained => Ok(true),
            SeekState::Pending { .. } | SeekState::Ready => self.source.is_done(),
        }
    }
}

fn seek_first(source: &Stream, target: &SeekTarget, skipped: &mut usize) -> Result<SeekPoll> {
    match target {
        SeekTarget::PacketIndex(index) => {
            while *skipped < *index {
                match source.next_packet()? {
                    Some(_) => *skipped += 1,
                    None => {
                        return if source.is_done()? {
                            Ok(SeekPoll::Drained)
                        } else {
                            Ok(SeekPoll::Pending)
                        };
                    }
                }
            }
            match source.next_packet()? {
                Some(item) => Ok(SeekPoll::Found(item)),
                None => {
                    if source.is_done()? {
                        Ok(SeekPoll::Drained)
                    } else {
                        Ok(SeekPoll::Pending)
                    }
                }
            }
        }
        SeekTarget::ClockIndex { clock, index } => loop {
            match source.next_packet()? {
                Some(item) => {
                    if item
                        .ticks()
                        .iter()
                        .any(|tick| &tick.clock == clock && &tick.index == index)
                    {
                        return Ok(SeekPoll::Found(item));
                    }
                }
                None => {
                    return if source.is_done()? {
                        Ok(SeekPoll::Drained)
                    } else {
                        Ok(SeekPoll::Pending)
                    };
                }
            }
        },
    }
}