ondas 2.1.0

Read-only, format-independent waveform analysis library
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
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
mod body;
mod hierarchy;
mod identity;
mod index;
#[cfg(test)]
#[path = "../../../tests/support/ghw_bytes.rs"]
mod test_bytes;
mod types;
mod validated;
mod wire;

use super::Input;
use crate::{BitsRef, Encoding, Hierarchy, Metadata, Result, Signal, Time, ValueRef};
use identity::Identity;
use std::ops::{ControlFlow, Range};
use std::sync::Arc;
use types::Types;
use validated::{CACHE_BYTES, Validated};
use wire::Wire;

/// Why one logical open runs. A metadata-only request must not leave a parked
/// validated hierarchy behind, so it never parks one of its own and releases the
/// one it reused.
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum Purpose {
    Waveform,
    Metadata,
}

/// One parked validated source, shared by every logical open of this backend.
struct Cache {
    slot: std::sync::Mutex<Option<Arc<Validated>>>,
    #[cfg(test)]
    hits: std::sync::atomic::AtomicUsize,
    #[cfg(test)]
    parses: std::sync::atomic::AtomicUsize,
    #[cfg(test)]
    #[allow(dead_code)]
    skipped: std::sync::atomic::AtomicUsize,
}

static CACHE: Cache = Cache::new();

impl Cache {
    const fn new() -> Self {
        Self {
            slot: std::sync::Mutex::new(None),
            #[cfg(test)]
            hits: std::sync::atomic::AtomicUsize::new(0),
            #[cfg(test)]
            parses: std::sync::atomic::AtomicUsize::new(0),
            #[cfg(test)]
            skipped: std::sync::atomic::AtomicUsize::new(0),
        }
    }

    fn slot(&self) -> std::sync::MutexGuard<'_, Option<Arc<Validated>>> {
        self.slot
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
    }

    /// Identity of the parked source, if any.
    fn parked_identity(&self) -> Option<identity::ContentIdentity> {
        self.slot()
            .as_ref()
            .and_then(|validated| validated.identity)
    }

    /// The parked source when its content identity matches.
    fn take_matching(&self, identity: &identity::ContentIdentity) -> Option<Arc<Validated>> {
        self.slot()
            .as_ref()
            .filter(|validated| validated.identity == Some(*identity))
            .map(Arc::clone)
    }

    /// Park one validated source. A source above the byte bound is not parked, and
    /// a replaced source is dropped outside the lock.
    fn park(&self, candidate: Arc<Validated>) {
        if candidate.retained_bytes > CACHE_BYTES {
            return;
        }
        let replaced = self.slot().replace(candidate);
        drop(replaced);
    }

    /// Release the parked source.
    fn clear(&self) {
        let parked = self.slot().take();
        drop(parked);
    }
}

pub(crate) enum Notification<'v> {
    Value {
        index: usize,
        time: Time,
        value: ValueRef<'v>,
    },
    Tick(Time),
}

pub(crate) struct Reader {
    wire: Wire,
    validated: Arc<Validated>,
    #[cfg(test)]
    pub(crate) records_read: usize,
    #[cfg(test)]
    pub(crate) assembly_bytes: usize,
    #[cfg(test)]
    pub(crate) indexed_reads: usize,
    #[cfg(test)]
    pub(crate) walk_reads: usize,
}

struct Buffer {
    index: usize,
    digits: Vec<u8>,
    real: f64,
    required: usize,
    known: usize,
    wkt: u8,
    vector: bool,
}

struct Dependency {
    id: u32,
    slot: usize,
    position: usize,
    seen: bool,
}

/// Dependency range of one source ID in the sorted dependency list.
fn dependency_range(dependencies: &[Dependency], id: u32) -> Range<usize> {
    let start = dependencies.partition_point(|dependency| dependency.id < id);
    let mut end = start;
    while dependencies
        .get(end)
        .is_some_and(|dependency| dependency.id == id)
    {
        end += 1;
    }
    start..end
}

/// Dependency range of one scalar event.
///
/// A broad read indexes ranges by rank, an indexed read supplies the route of the
/// selected scalar, and everything else — the sequential walker, a route the
/// current table does not cover — falls back to the searched lookup.
fn event_range(
    event: &body::Event,
    broad: Option<&[Range<usize>]>,
    selected: Option<&[Range<usize>]>,
    dependencies: &[Dependency],
) -> Range<usize> {
    let body::Event::Scalar {
        id, rank, route, ..
    } = *event
    else {
        return 0..0;
    };
    if let Some(routes) = broad {
        return routes
            .get(rank)
            .cloned()
            .unwrap_or_else(|| dependency_range(dependencies, id));
    }
    if let Some(routes) = selected
        && route != body::NO_ROUTE
        && let Some(range) = routes.get(route as usize)
    {
        return range.clone();
    }
    dependency_range(dependencies, id)
}

/// Ranges of the selected source IDs, in the same ascending order in which the
/// indexed reader assigns route indices to selected ranks.
fn selected_ranges(dependencies: &[Dependency], ids: &[u32]) -> Option<Vec<Range<usize>>> {
    let mut ranges = Vec::new();
    ranges.try_reserve_exact(ids.len()).ok()?;
    for &id in ids {
        ranges.push(dependency_range(dependencies, id));
    }
    Some(ranges)
}

// The active table and dependencies both use ascending source IDs.
fn dependency_ranges(active: &[(u32, usize)], dependencies: &[Dependency]) -> Vec<Range<usize>> {
    let mut cursor = 0;
    let ranges = active
        .iter()
        .map(|&(id, _)| {
            let start = cursor;
            while dependencies.get(cursor).is_some_and(|d| d.id == id) {
                cursor += 1;
            }
            start..cursor
        })
        .collect();
    debug_assert_eq!(cursor, dependencies.len());
    ranges
}

impl Buffer {
    #[inline(always)]
    fn update(&mut self, position: usize, value: body::Scalar, first: bool) {
        if first {
            self.known += 1;
        }
        let bits = match value {
            body::Scalar::Real(bits) => {
                self.real = f64::from_bits(bits);
                return;
            }
            body::Scalar::Ordinal(ordinal) if self.wkt != 0 => {
                let alphabet: &[u8] = if self.wkt == 3 { b"ux01zwlh-" } else { b"01" };
                self.digits[if self.vector { position } else { 0 }] = alphabet[ordinal as usize];
                return;
            }
            body::Scalar::Ordinal(ordinal) => u64::from(ordinal),
            body::Scalar::Integer(value) => value as u64,
        };
        for (shift, digit) in self.digits.iter_mut().rev().enumerate() {
            *digit = b'0' + ((bits >> shift) & 1) as u8;
        }
    }

    fn value(&self) -> ValueRef<'_> {
        if self.digits.is_empty() {
            ValueRef::Real(self.real)
        } else {
            ValueRef::Bits(BitsRef::from_validated_ascii(&self.digits))
        }
    }
}

impl Reader {
    pub(crate) fn open(
        input: Box<dyn Input>,
        source_name: String,
    ) -> Result<(Self, Hierarchy, Metadata)> {
        Self::open_with(input, source_name, Purpose::Waveform, &CACHE)
    }

    /// Read owned source metadata with the same validation and reuse path, but
    /// without leaving a parked validated hierarchy behind.
    pub(crate) fn metadata(input: Box<dyn Input>, source_name: String) -> Result<Metadata> {
        let (reader, _, metadata) = Self::open_with(input, source_name, Purpose::Metadata, &CACHE)?;
        drop(reader);
        Ok(metadata)
    }

    fn open_with(
        input: Box<dyn Input>,
        source_name: String,
        purpose: Purpose,
        cache: &Cache,
    ) -> Result<(Self, Hierarchy, Metadata)> {
        let mut input = input;
        if let Some(parked) = cache.parked_identity() {
            // A parked source is only usable for byte-identical content, and the
            // length is part of that identity: a different length is a certain miss
            // and needs no scan. Otherwise the scan reads the whole current input; a
            // failure here is an optional miss and the parser below reports whatever
            // error it reaches.
            let length = input.seek(std::io::SeekFrom::End(0))?;
            input.seek(std::io::SeekFrom::Start(0))?;
            if length == parked.length
                && identity::scan(&mut *input).is_ok_and(|current| current == parked)
                && let Some(validated) = cache.take_matching(&parked)
            {
                let mut wire = Wire::new(input)?;
                if wire.len == parked.length {
                    #[cfg(test)]
                    cache
                        .hits
                        .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                    wire.version = validated.version;
                    wire.big_endian = validated.big_endian;
                    let metadata = Metadata {
                        source_name,
                        timescale: None,
                        time_span: validated.time_span,
                        writer: None,
                        date: None,
                        comments: Vec::new(),
                    };
                    let hierarchy = validated.hierarchy.reopened();
                    let reader = Self {
                        wire,
                        validated,
                        #[cfg(test)]
                        records_read: 0,
                        #[cfg(test)]
                        assembly_bytes: 0,
                        #[cfg(test)]
                        indexed_reads: 0,
                        #[cfg(test)]
                        walk_reads: 0,
                    };
                    if purpose == Purpose::Metadata {
                        // A metadata-only request must not keep a parked hierarchy.
                        cache.clear();
                    }
                    return Ok((reader, hierarchy, metadata));
                }
                // The length changed under the identity, so this is a miss.
                input = wire.into_input();
            }
        }
        Self::parse(input, source_name, purpose, cache)
    }

    /// Cold or cache-miss parse: validation runs over a watched input, so the
    /// identity describes exactly the bytes this parse consumed.
    fn parse(
        input: Box<dyn Input>,
        source_name: String,
        purpose: Purpose,
        cache: &Cache,
    ) -> Result<(Self, Hierarchy, Metadata)> {
        #[cfg(test)]
        cache
            .parses
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        let identity = Identity::new();
        let mut wire = Wire::new(identity.watch(input))?;
        wire.header()?;
        let strings = wire.strings()?;
        let mut types = Types::read(&mut wire, strings)?;
        let mut marker = wire.fixed::<4>()?;
        if &marker == b"WKT\0" {
            types.wkt(&mut wire)?;
            marker = wire.fixed()?;
        }
        if &marker != b"HIE\0" {
            return Err(wire.malformed("expected HIE section"));
        }
        let declarations = hierarchy::read(&mut wire, &types)?;
        wire.marker(b"EOH\0")?;
        let body_start = wire.offset;
        let mut index = index::Index::new(declarations.active.len());
        let (_, time_span) = body::walk(
            &mut wire,
            &types,
            &declarations.active,
            Time::from_ticks(u64::MAX),
            |event| {
                let Some(positions) = &mut index else {
                    return ControlFlow::<()>::Continue(());
                };
                match event {
                    body::Event::Scalar {
                        rank,
                        time,
                        offset,
                        cyclic,
                        ..
                    } => {
                        if !positions.push(rank, time, offset, cyclic) {
                            index = None;
                        }
                    }
                    body::Event::ListEnd(_) => positions.list_end(),
                    body::Event::Tick(_) => {}
                }
                ControlFlow::<()>::Continue(())
            },
        )?;
        let complete = match &mut index {
            Some(positions) => positions.finish(),
            None => true,
        };
        if !complete {
            // An incomplete representation is discarded: the sequential walker
            // answers instead, and opening validation already finished.
            index = None;
        }
        let version = wire.version;
        let big_endian = wire.big_endian;
        let hierarchy = declarations.hierarchy;
        let recipes = declarations.recipes;
        let recipe_slots = recipes.capacity();
        let active = declarations.active;
        let active_slots = active.capacity();
        // The parser consumes the whole input, so the proof covers what it validated.
        let proven = identity.finish(wire.len);
        let retained_bytes = validated::retained_bytes(
            &types,
            &recipes,
            recipe_slots,
            active_slots,
            &hierarchy,
            index.as_ref(),
        );
        let validated = Arc::new(Validated {
            identity: proven,
            hierarchy: hierarchy.clone(),
            types,
            recipes,
            active,
            body_start,
            version,
            big_endian,
            time_span,
            index,
            retained_bytes,
        });
        if proven.is_some() && purpose == Purpose::Waveform {
            cache.park(Arc::clone(&validated));
        } else if purpose == Purpose::Metadata {
            // A metadata-only request must not keep a parked hierarchy.
            cache.clear();
        }
        let metadata = Metadata {
            source_name,
            timescale: None,
            time_span: validated.time_span,
            writer: None,
            date: None,
            comments: Vec::new(),
        };
        let reader = Self {
            wire,
            validated,
            #[cfg(test)]
            records_read: 0,
            #[cfg(test)]
            assembly_bytes: 0,
            #[cfg(test)]
            indexed_reads: 0,
            #[cfg(test)]
            walk_reads: 0,
        };
        Ok((reader, hierarchy, metadata))
    }

    /// Query diagnostics of the completed index, for focused tests.
    #[cfg(test)]
    pub(super) fn work(&self) -> Option<index::Work> {
        self.validated.index.as_ref().map(index::Index::work)
    }

    pub(crate) fn read<B>(
        &mut self,
        signals: &[Signal],
        end: Time,
        mut visitor: impl for<'v> FnMut(Notification<'v>) -> ControlFlow<B>,
    ) -> Result<ControlFlow<B>> {
        if signals.is_empty() {
            return Ok(ControlFlow::Continue(()));
        }
        self.wire.seek(self.validated.body_start)?;
        let mut buffers = Vec::new();
        let mut dependencies = Vec::new();
        for signal in signals {
            let recipe = self
                .validated
                .recipes
                .get(signal.index())
                .ok_or_else(|| self.wire.malformed("invalid private recipe index"))?;
            let (_, _, wkt) = self
                .validated
                .types
                .scalar(recipe.scalar_type)
                .ok_or_else(|| self.wire.malformed("non-scalar recipe encoding"))?;
            let slot = buffers.len();
            for (position, &id) in recipe.ids.iter().enumerate() {
                dependencies.push(Dependency {
                    id,
                    slot,
                    position,
                    seen: false,
                });
            }
            let width = match recipe.encoding {
                Encoding::Bits { width } => width as usize,
                Encoding::Real => 0,
                _ => return Err(self.wire.unsupported("recipe value class")),
            };
            buffers.push(Buffer {
                index: signal.index(),
                digits: vec![b'0'; width],
                real: 0.0,
                required: recipe.ids.len(),
                known: 0,
                wkt,
                vector: recipe.vector,
            });
        }
        dependencies.sort_unstable_by_key(|dependency| dependency.id);
        #[cfg(test)]
        {
            self.assembly_bytes = buffers
                .iter()
                .map(|b| b.digits.len() + usize::from(b.digits.is_empty()) * 8)
                .sum();
        }
        let mut marks = vec![false; buffers.len()];
        let mut touched = Vec::new();
        let mut ids = dependencies
            .iter()
            .map(|dependency| dependency.id)
            .collect::<Vec<_>>();
        ids.dedup(); // Dependencies are already sorted by source ID.
        let broad = ids.len() >= self.validated.active.len().div_ceil(2);
        let routes = broad.then(|| dependency_ranges(&self.validated.active, &dependencies));
        // A narrow indexed read resolves each selected range once and receives the
        // matching route index with every event.
        let selected = (!broad && self.validated.index.is_some())
            .then(|| selected_ranges(&dependencies, &ids))
            .flatten();
        // The block permits the closure attribute on MSRV. Force scalar-event
        // specialization in the walker instead of a per-record callback.
        let mut consume = {
            #[inline(always)]
            |event| {
                match event {
                    body::Event::Scalar { value, .. } => {
                        #[cfg(test)]
                        {
                            self.records_read += 1;
                        }
                        let range = event_range(
                            &event,
                            routes.as_deref(),
                            selected.as_deref(),
                            &dependencies,
                        );
                        if !range.is_empty() {
                            // The first entry owns this source scalar's seen flag.
                            let first = !dependencies[range.start].seen;
                            if first {
                                dependencies[range.start].seen = true;
                            }
                            for dependency in &dependencies[range] {
                                let slot = dependency.slot;
                                buffers[slot].update(dependency.position, value, first);
                                if !marks[slot] {
                                    marks[slot] = true;
                                    touched.push(slot);
                                }
                            }
                        }
                    }
                    body::Event::ListEnd(time) => {
                        for slot in touched.drain(..) {
                            marks[slot] = false;
                            let buffer = &buffers[slot];
                            if buffer.known == buffer.required
                                && let ControlFlow::Break(value) = visitor(Notification::Value {
                                    index: buffer.index,
                                    time,
                                    value: buffer.value(),
                                })
                            {
                                return ControlFlow::Break(value);
                            }
                        }
                    }
                    body::Event::Tick(time) => {
                        if let ControlFlow::Break(value) = visitor(Notification::Tick(time)) {
                            return ControlFlow::Break(value);
                        }
                    }
                }
                ControlFlow::Continue(())
            }
        };
        if let Some(index) = &self.validated.index
            && !broad
            && let Some(flow) = index.read(
                &mut self.wire,
                &self.validated.types,
                &self.validated.active,
                &ids,
                end,
                &mut consume,
            )?
        {
            #[cfg(test)]
            {
                self.indexed_reads += 1;
            }
            return Ok(flow);
        }
        #[cfg(test)]
        {
            self.walk_reads += 1;
        }
        let (flow, _) = body::walk(
            &mut self.wire,
            &self.validated.types,
            &self.validated.active,
            end,
            consume,
        )?;
        Ok(flow)
    }
}

#[cfg(test)]
mod reuse_tests;
#[cfg(test)]
mod tests;