Skip to main content

ferrochart_core/data/
ohlc.rs

1//! Storage of one OHLC(V) series: `f64` columns, LOD pyramid, generation and
2//! change log, with validation at the API boundary.
3//!
4//! The timestamps live in the global [`TimeIndex`] and are not stored again
5//! here: bar `p` is logical index `first + p`, and slots of the axis the
6//! series has no bar for hold gap bars (all prices NaN). Every operation validates first and only then
7//! mutates, so an error leaves both the series and the index unchanged.
8//!
9
10use super::lod::{ANCHOR, Lod, Raw};
11use super::{ChangeLog, DeqColumn, IdxSpan, OhlcColumns, TimeIndex, Timestamp};
12use crate::error::{Error, Result};
13
14/// How bars that violate `low <= min(open, close) <= max(open, close) <= high` are
15/// handled. The default is the same in debug and release builds.
16///
17/// # Examples
18///
19/// ```
20/// use ferrochart_core::data::ValidationMode;
21///
22/// assert_eq!(ValidationMode::default(), ValidationMode::Reject);
23/// ```
24#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
25pub enum ValidationMode {
26    /// Return [`Error::InvalidOhlc`].
27    #[default]
28    Reject,
29    /// Widen `high`/`low` to include `open`/`close` and report the number of
30    /// repaired bars in a `ChartEvent::Warning`. Non-finite values are still
31    /// rejected.
32    Repair,
33}
34
35/// A data change, as seen by downstream consumers (LOD, indicators).
36///
37///
38/// # Examples
39///
40/// ```
41/// use ferrochart_core::data::DataChange;
42///
43/// let change = DataChange::Append { count: 2 };
44/// assert_ne!(change, DataChange::UpdateLast);
45/// ```
46#[derive(Clone, Copy, Debug, PartialEq, Eq)]
47pub enum DataChange {
48    /// Everything was replaced.
49    Replace,
50    /// `count` bars were added at the end.
51    Append {
52        /// Number of new bars.
53        count: usize,
54    },
55    /// The last bar was modified (the main real-time path).
56    UpdateLast,
57    /// `count` bars were added at the beginning (history loading).
58    Prepend {
59        /// Number of new bars.
60        count: usize,
61    },
62}
63
64/// A real-time update for `Chart::update_last`.
65///
66/// If `time` equals the time of the last bar, that bar is replaced; if it is
67/// later, a new bar is appended.
68///
69/// # Examples
70///
71/// ```
72/// use ferrochart_core::data::BarUpdate;
73///
74/// let tick = BarUpdate { time: 60_000, open: 10.0, high: 11.0, low: 9.5, close: 10.5, volume: Some(3.0) };
75/// assert!(tick.low <= tick.close);
76/// ```
77#[derive(Clone, Copy, Debug, PartialEq)]
78pub struct BarUpdate {
79    /// Bar open time (UTC milliseconds).
80    pub time: Timestamp,
81    /// Open price.
82    pub open: f64,
83    /// High price.
84    pub high: f64,
85    /// Low price.
86    pub low: f64,
87    /// Close price.
88    pub close: f64,
89    /// Volume; must be present exactly when the series has volume.
90    pub volume: Option<f64>,
91}
92
93/// A read-only column of one LOD level: `f64` at level 0, `f32` above.
94///
95/// # Examples
96///
97/// ```
98/// use ferrochart_core::data::Column;
99///
100/// let col = Column::F32(&[1.5, 2.5]);
101/// assert_eq!(col.get(1), Some(2.5));
102/// assert_eq!(col.len(), 2);
103/// ```
104#[derive(Clone, Copy, Debug)]
105pub enum Column<'a> {
106    /// Level 0: original data.
107    F64(&'a [f64]),
108    /// LOD levels: `f32` with directed rounding.
109    F32(&'a [f32]),
110}
111
112impl Column<'_> {
113    /// Number of values.
114    pub fn len(&self) -> usize {
115        match self {
116            Self::F64(s) => s.len(),
117            Self::F32(s) => s.len(),
118        }
119    }
120
121    /// Whether the column is empty.
122    pub fn is_empty(&self) -> bool {
123        self.len() == 0
124    }
125
126    /// Value at position `i` (not a logical index), widened to `f64`.
127    pub fn get(&self, i: usize) -> Option<f64> {
128        match self {
129            Self::F64(s) => s.get(i).copied(),
130            Self::F32(s) => s.get(i).copied().map(f64::from),
131        }
132    }
133}
134
135/// Read-only view of one level of an OHLC series.
136///
137/// At level 0 element `p` has logical index `first + p`; at level `k >= 2` it is
138/// bucket number `first + p`, covering logical indices
139/// `ANCHOR + b * 2^k .. ANCHOR + (b + 1) * 2^k` (see [`ANCHOR`](super::ANCHOR)).
140///
141/// # Examples
142///
143/// ```
144/// use ferrochart_core::data::{Column, OhlcLevelView};
145///
146/// let v = OhlcLevelView {
147///     level: 0, first: 10,
148///     open: Column::F64(&[1.0]), high: Column::F64(&[2.0]),
149///     low: Column::F64(&[0.5]), close: Column::F64(&[1.5]), volume: None,
150/// };
151/// assert_eq!((v.len(), v.end()), (1, 11));
152/// ```
153#[derive(Clone, Copy, Debug)]
154pub struct OhlcLevelView<'a> {
155    /// LOD level (0 = original data).
156    pub level: u8,
157    /// Logical index (level 0) or bucket number (level >= 2) of the first element.
158    pub first: i64,
159    /// Open values.
160    pub open: Column<'a>,
161    /// High values.
162    pub high: Column<'a>,
163    /// Low values.
164    pub low: Column<'a>,
165    /// Close values.
166    pub close: Column<'a>,
167    /// Volumes (summed in LOD levels), if the series has volume.
168    pub volume: Option<Column<'a>>,
169}
170
171impl OhlcLevelView<'_> {
172    /// Number of elements.
173    pub fn len(&self) -> usize {
174        self.open.len()
175    }
176
177    /// Whether the view is empty.
178    pub fn is_empty(&self) -> bool {
179        self.open.is_empty()
180    }
181
182    /// One past the last element's index.
183    pub fn end(&self) -> i64 {
184        self.first + i64::try_from(self.len()).unwrap_or(i64::MAX)
185    }
186}
187
188/// Logical indices are kept within `±2^39` so that `index - ANCHOR` never
189/// overflows and stays non-negative.
190const INDEX_LIMIT: i64 = 1 << 39;
191
192#[derive(Clone, Copy, Debug)]
193struct ExtremesCache {
194    from: i64,
195    to: i64,
196    generation: u64,
197    result: Option<(f64, f64)>,
198}
199
200/// Storage of one OHLC series (gap bars allowed).
201#[derive(Clone, Debug, Default)]
202pub(crate) struct OhlcStore {
203    first: i64,
204    open: DeqColumn<f64>,
205    high: DeqColumn<f64>,
206    low: DeqColumn<f64>,
207    close: DeqColumn<f64>,
208    volume: Option<DeqColumn<f64>>,
209    lod: Lod,
210    log: ChangeLog,
211    extremes_cache: Option<ExtremesCache>,
212}
213
214macro_rules! raw {
215    ($s:expr) => {
216        Raw {
217            first: $s.first,
218            open: &$s.open,
219            high: &$s.high,
220            low: &$s.low,
221            close: &$s.close,
222            volume: $s.volume.as_deref(),
223        }
224    };
225}
226
227/// All columns must have the length of `open`; `time` too unless it is empty
228/// (secondary series, whose times the caller has already mapped).
229fn check_lengths(c: &OhlcColumns) -> Result<()> {
230    let n = c.open.len();
231    if !c.time.is_empty() && c.time.len() != n {
232        return Err(Error::LengthMismatch { column: "open" });
233    }
234    let cols = [
235        ("open", c.open.len()),
236        ("high", c.high.len()),
237        ("low", c.low.len()),
238        ("close", c.close.len()),
239        ("volume", c.volume.as_ref().map_or(n, Vec::len)),
240    ];
241    match cols.iter().find(|(_, len)| *len != n) {
242        Some(&(column, _)) => Err(Error::LengthMismatch { column }),
243        None => Ok(()),
244    }
245}
246
247/// Whether a bar is a gap: open, high, low and close all NaN (a slot of the
248/// time axis that the series does not cover).
249pub(crate) fn is_gap_bar(o: f64, h: f64, l: f64, c: f64) -> bool {
250    o.is_nan() && h.is_nan() && l.is_nan() && c.is_nan()
251}
252
253/// Validates (and in `Repair` mode fixes) the bars. Returns the number of
254/// repaired bars. Gap bars are allowed; their volume is set to 0. In `Reject`
255/// mode only gap volumes are modified.
256fn check_bars(
257    o: &[f64],
258    h: &mut [f64],
259    l: &mut [f64],
260    c: &[f64],
261    mut v: Option<&mut [f64]>,
262    mode: ValidationMode,
263) -> Result<usize> {
264    let mut repaired = 0;
265    for i in 0..o.len() {
266        if is_gap_bar(o[i], h[i], l[i], c[i]) {
267            if let Some(v) = v.as_deref_mut() {
268                v[i] = 0.0;
269            }
270            continue;
271        }
272        let vol_ok = v.as_deref().is_none_or(|v| v[i].is_finite());
273        if !(o[i].is_finite() && h[i].is_finite() && l[i].is_finite() && c[i].is_finite() && vol_ok)
274        {
275            return Err(Error::InvalidOhlc { index: i });
276        }
277        let top = o[i].max(c[i]);
278        let bottom = o[i].min(c[i]);
279        if h[i] < top || l[i] > bottom {
280            match mode {
281                ValidationMode::Reject => return Err(Error::InvalidOhlc { index: i }),
282                ValidationMode::Repair => {
283                    h[i] = h[i].max(top);
284                    l[i] = l[i].min(bottom);
285                    repaired += 1;
286                }
287            }
288        }
289    }
290    Ok(repaired)
291}
292
293fn to_i64(n: usize) -> i64 {
294    i64::try_from(n).unwrap_or(i64::MAX)
295}
296
297/// Checks the column lengths and validates (in `Repair` mode fixes) the bars,
298/// before they are rearranged. Returns the number of repaired bars.
299pub(crate) fn check_columns(cols: &mut OhlcColumns, mode: ValidationMode) -> Result<usize> {
300    check_lengths(cols)?;
301    check_bars(
302        &cols.open,
303        &mut cols.high,
304        &mut cols.low,
305        &cols.close,
306        cols.volume.as_deref_mut(),
307        mode,
308    )
309}
310
311impl OhlcStore {
312    pub fn len(&self) -> usize {
313        self.open.len()
314    }
315
316    pub fn first(&self) -> i64 {
317        self.first
318    }
319
320    pub fn end(&self) -> i64 {
321        self.first + to_i64(self.len())
322    }
323
324    pub fn has_volume(&self) -> bool {
325        self.volume.is_some()
326    }
327
328    pub fn log(&self) -> &ChangeLog {
329        &self.log
330    }
331
332    pub fn lod(&self) -> &Lod {
333        &self.lod
334    }
335
336    /// View of level `level` (0 or a built LOD level).
337    pub fn level_view(&self, level: u8) -> Option<OhlcLevelView<'_>> {
338        if level == 0 {
339            return Some(OhlcLevelView {
340                level,
341                first: self.first,
342                open: Column::F64(&self.open),
343                high: Column::F64(&self.high),
344                low: Column::F64(&self.low),
345                close: Column::F64(&self.close),
346                volume: self.volume.as_deref().map(Column::F64),
347            });
348        }
349        let l = self.lod.level(level)?;
350        Some(OhlcLevelView {
351            level,
352            first: l.first,
353            open: Column::F32(&l.open),
354            high: Column::F32(&l.high),
355            low: Column::F32(&l.low),
356            close: Column::F32(&l.close),
357            volume: l.volume.as_deref().map(Column::F32),
358        })
359    }
360
361    /// Replaces all data and the time index. Returns the number of repaired bars.
362    pub fn replace(
363        &mut self,
364        index: &mut TimeIndex,
365        mut cols: OhlcColumns,
366        mode: ValidationMode,
367    ) -> Result<usize> {
368        check_lengths(&cols)?;
369        if cols.time.len() != cols.open.len() {
370            return Err(Error::LengthMismatch { column: "open" });
371        }
372        let new_index = TimeIndex::from_times(core::mem::take(&mut cols.time))?;
373        let repaired = self.replace_at(new_index.first_index(), cols, mode)?;
374        *index = new_index;
375        index.shrink_to_fit();
376        Ok(repaired)
377    }
378
379    /// Replaces all data of a secondary series without touching the time
380    /// index: bar `p` goes to logical index `first + p` (`cols.time` is
381    /// ignored; the caller has mapped it). Returns the number of repaired bars.
382    pub fn replace_at(
383        &mut self,
384        first: i64,
385        mut cols: OhlcColumns,
386        mode: ValidationMode,
387    ) -> Result<usize> {
388        check_lengths(&cols)?;
389        if to_i64(cols.len()) >= INDEX_LIMIT {
390            return Err(Error::InvalidArgument("too many bars"));
391        }
392        let repaired = check_bars(
393            &cols.open,
394            &mut cols.high,
395            &mut cols.low,
396            &cols.close,
397            cols.volume.as_deref_mut(),
398            mode,
399        )?;
400        let mut next = Self {
401            first,
402            open: DeqColumn::from_vec(cols.open),
403            high: DeqColumn::from_vec(cols.high),
404            low: DeqColumn::from_vec(cols.low),
405            close: DeqColumn::from_vec(cols.close),
406            volume: cols.volume.map(DeqColumn::from_vec),
407            lod: Lod::default(),
408            log: core::mem::take(&mut self.log),
409            extremes_cache: None,
410        };
411        next.lod = match Lod::build(&raw!(next)) {
412            Ok(lod) => lod,
413            Err(e) => {
414                self.log = next.log;
415                return Err(e);
416            }
417        };
418        for c in [
419            &mut next.open,
420            &mut next.high,
421            &mut next.low,
422            &mut next.close,
423        ] {
424            c.shrink_to_fit();
425        }
426        if let Some(v) = next.volume.as_mut() {
427            v.shrink_to_fit();
428        }
429        next.log.replace();
430        *self = next;
431        Ok(repaired)
432    }
433
434    fn check_volume_presence(&self, has: bool) -> Result<()> {
435        if has == self.has_volume() {
436            Ok(())
437        } else {
438            Err(Error::LengthMismatch { column: "volume" })
439        }
440    }
441
442    fn columns_mut(&mut self) -> impl Iterator<Item = &mut DeqColumn<f64>> {
443        [
444            Some(&mut self.open),
445            Some(&mut self.high),
446            Some(&mut self.low),
447            Some(&mut self.close),
448            self.volume.as_mut(),
449        ]
450        .into_iter()
451        .flatten()
452    }
453
454    /// Appends bars after the last one. Returns the number of repaired bars.
455    /// With `index`, the bars' times are appended to it;
456    /// without, the caller has checked that they occupy the slots right after
457    /// the last bar.
458    pub fn append(
459        &mut self,
460        index: Option<&mut TimeIndex>,
461        mut cols: OhlcColumns,
462        mode: ValidationMode,
463    ) -> Result<usize> {
464        if self.len() == 0 {
465            return match index {
466                Some(index) => self.replace(index, cols, mode),
467                None => self.replace_at(self.first, cols, mode),
468            };
469        }
470        check_lengths(&cols)?;
471        self.check_volume_presence(cols.volume.is_some())?;
472        let m = cols.open.len();
473        if index.is_some() && cols.time.len() != m {
474            return Err(Error::LengthMismatch { column: "open" });
475        }
476        let new_end = self.end().saturating_add(to_i64(m));
477        if new_end >= INDEX_LIMIT {
478            return Err(Error::InvalidArgument("too many bars"));
479        }
480        let repaired = check_bars(
481            &cols.open,
482            &mut cols.high,
483            &mut cols.low,
484            &cols.close,
485            cols.volume.as_deref_mut(),
486            mode,
487        )?;
488        for c in self.columns_mut() {
489            c.reserve_back(m)?;
490        }
491        self.lod.reserve(self.first, new_end)?;
492        if let Some(index) = index {
493            index.reserve_back(m)?;
494            index.append(&cols.time)?; // validates the times, atomic
495        }
496
497        // Reserved above: these cannot fail.
498        self.open.extend_back(&cols.open)?;
499        self.high.extend_back(&cols.high)?;
500        self.low.extend_back(&cols.low)?;
501        self.close.extend_back(&cols.close)?;
502        if let (Some(v), Some(src)) = (self.volume.as_mut(), cols.volume.as_deref()) {
503            v.extend_back(src)?;
504        }
505        let span = IdxSpan {
506            start: new_end - to_i64(m),
507            end: new_end,
508        };
509        self.lod.update(&raw!(self), span);
510        self.log.record(span);
511        Ok(repaired)
512    }
513
514    /// Inserts bars before the first one. Returns the number of repaired bars.
515    /// `index` as in [`append`](Self::append).
516    pub fn prepend(
517        &mut self,
518        index: Option<&mut TimeIndex>,
519        mut cols: OhlcColumns,
520        mode: ValidationMode,
521    ) -> Result<usize> {
522        if self.len() == 0 {
523            if let Some(index) = index {
524                return self.replace(index, cols, mode);
525            }
526            let first = self.first - to_i64(cols.open.len());
527            return self.replace_at(first, cols, mode);
528        }
529        check_lengths(&cols)?;
530        self.check_volume_presence(cols.volume.is_some())?;
531        let m = cols.open.len();
532        if index.is_some() && cols.time.len() != m {
533            return Err(Error::LengthMismatch { column: "open" });
534        }
535        let new_first = self.first.saturating_sub(to_i64(m));
536        if new_first <= -INDEX_LIMIT || new_first <= ANCHOR {
537            return Err(Error::InvalidArgument("too many bars"));
538        }
539        let repaired = check_bars(
540            &cols.open,
541            &mut cols.high,
542            &mut cols.low,
543            &cols.close,
544            cols.volume.as_deref_mut(),
545            mode,
546        )?;
547        for c in self.columns_mut() {
548            c.reserve_front(m)?;
549        }
550        self.lod.reserve(new_first, self.end())?;
551        if let Some(index) = index {
552            index.reserve_front(m)?;
553            index.prepend(&cols.time)?; // validates the times, atomic
554        }
555
556        self.open.extend_front(&cols.open)?;
557        self.high.extend_front(&cols.high)?;
558        self.low.extend_front(&cols.low)?;
559        self.close.extend_front(&cols.close)?;
560        if let (Some(v), Some(src)) = (self.volume.as_mut(), cols.volume.as_deref()) {
561            v.extend_front(src)?;
562        }
563        let span = IdxSpan {
564            start: new_first,
565            end: self.first,
566        };
567        self.first = new_first;
568        self.lod.update(&raw!(self), span);
569        self.log.record(span);
570        Ok(repaired)
571    }
572
573    /// Validates (and in `Repair` mode fixes) one bar. Returns the bar and
574    /// whether it was repaired.
575    pub(crate) fn check_one(bar: BarUpdate, mode: ValidationMode) -> Result<(BarUpdate, usize)> {
576        let one = |x| [x];
577        let mut high = one(bar.high);
578        let mut low = one(bar.low);
579        let mut volume = bar.volume.map(one);
580        let repaired = check_bars(
581            &one(bar.open),
582            &mut high,
583            &mut low,
584            &one(bar.close),
585            volume.as_mut().map(<[f64; 1]>::as_mut_slice),
586            mode,
587        )?;
588        Ok((
589            BarUpdate {
590                high: high[0],
591                low: low[0],
592                volume: volume.map(|v| v[0]),
593                ..bar
594            },
595            repaired,
596        ))
597    }
598
599    /// Overwrites the last bar in place (no allocation); `bar.time` is not
600    /// checked. Returns the number of repaired bars (0 or 1).
601    pub fn set_last(&mut self, bar: BarUpdate, mode: ValidationMode) -> Result<usize> {
602        self.set_at(self.end() - 1, bar, mode)
603    }
604
605    /// Overwrites the bar at logical index `j` in place (no allocation);
606    /// `bar.time` is not checked. Returns the number of repaired bars (0 or 1).
607    pub fn set_at(&mut self, j: i64, bar: BarUpdate, mode: ValidationMode) -> Result<usize> {
608        let (bar, repaired) = Self::check_one(bar, mode)?;
609        self.check_volume_presence(bar.volume.is_some())?;
610        let Some(p) = j
611            .checked_sub(self.first)
612            .and_then(|p| usize::try_from(p).ok())
613            .filter(|&p| p < self.len())
614        else {
615            return Err(Error::InvalidArgument("no bar at this index"));
616        };
617        self.open[p] = bar.open;
618        self.high[p] = bar.high;
619        self.low[p] = bar.low;
620        self.close[p] = bar.close;
621        if let (Some(v), Some(x)) = (self.volume.as_mut(), bar.volume) {
622            v[p] = x;
623        }
624        let span = IdxSpan {
625            start: j,
626            end: j + 1,
627        };
628        self.lod.update(&raw!(self), span);
629        self.log.record(span);
630        Ok(repaired)
631    }
632
633    /// Removes the last `n` bars (all if fewer), keeping the capacity; never
634    /// allocates. The new last bar is recorded as changed (its LOD buckets
635    /// were recomputed).
636    pub fn truncate_back(&mut self, n: usize) {
637        let n = n.min(self.len());
638        if n == 0 {
639            return;
640        }
641        for c in self.columns_mut() {
642            c.truncate_ends(0, n);
643        }
644        self.lod.truncate(&raw!(self));
645        self.extremes_cache = None;
646        let end = self.end();
647        // Empty: only the generation moves.
648        let start = if self.len() == 0 { end } else { end - 1 };
649        self.log.record(IdxSpan { start, end });
650    }
651
652    /// One bar as columns (for appending it).
653    pub fn bar_columns(bar: BarUpdate, with_time: bool) -> OhlcColumns {
654        OhlcColumns {
655            time: if with_time {
656                vec![bar.time]
657            } else {
658                Vec::new()
659            },
660            open: vec![bar.open],
661            high: vec![bar.high],
662            low: vec![bar.low],
663            close: vec![bar.close],
664            volume: bar.volume.map(|v| vec![v]),
665        }
666    }
667
668    /// Real-time update: replaces the last bar if `bar.time` equals its time
669    /// (no allocation), appends a bar if it is later. Returns the change that was
670    /// applied and the number of repaired bars (0 or 1). The chart goes
671    /// through `series::Op::update_last` instead, which handles the union axis.
672    #[cfg(test)]
673    pub fn update_last(
674        &mut self,
675        index: &mut TimeIndex,
676        bar: BarUpdate,
677        mode: ValidationMode,
678    ) -> Result<(DataChange, usize)> {
679        let last_time = index.times().last().copied();
680        match last_time {
681            Some(t) if bar.time == t && self.len() > 0 => {
682                let repaired = self.set_last(bar, mode)?;
683                Ok((DataChange::UpdateLast, repaired))
684            }
685            Some(t) if bar.time < t => Err(Error::TimeNotIncreasing { index: 0 }),
686            _ => {
687                let (bar, repaired) = Self::check_one(bar, mode)?;
688                // Already validated/repaired; Reject cannot fail again.
689                self.append(
690                    Some(index),
691                    Self::bar_columns(bar, true),
692                    ValidationMode::Reject,
693                )?;
694                Ok((DataChange::Append { count: 1 }, repaired))
695            }
696        }
697    }
698
699    /// Values of the bar at logical index `j`; `None` outside the data and
700    /// for gap bars.
701    pub fn bar_at(&self, j: i64) -> Option<[f64; 4]> {
702        let p = usize::try_from(j.checked_sub(self.first)?).ok()?;
703        let bar = [
704            *self.open.get(p)?,
705            *self.high.get(p)?,
706            *self.low.get(p)?,
707            *self.close.get(p)?,
708        ];
709        (!is_gap_bar(bar[0], bar[1], bar[2], bar[3])).then_some(bar)
710    }
711
712    /// Volume of the bar at logical index `j`.
713    pub fn volume_at(&self, j: i64) -> Option<f64> {
714        let p = usize::try_from(j.checked_sub(self.first)?).ok()?;
715        self.volume.as_ref()?.get(p).copied()
716    }
717
718    /// Minimum low and maximum high over logical indices `from..to`, cached by
719    /// `(range, generation)`.
720    pub fn extremes(&mut self, from: i64, to: i64) -> Option<(f64, f64)> {
721        let generation = self.log.generation();
722        if let Some(c) = self.extremes_cache
723            && (c.from, c.to, c.generation) == (from, to, generation)
724        {
725            return c.result;
726        }
727        // Ranges of gap bars only have no extremes.
728        let result = self
729            .lod
730            .extremes(&raw!(self), from, to)
731            .filter(|(lo, hi)| lo.is_finite() && hi.is_finite());
732        self.extremes_cache = Some(ExtremesCache {
733            from,
734            to,
735            generation,
736            result,
737        });
738        result
739    }
740
741    /// Heap bytes held by the series (columns and LOD).
742    pub fn heap_bytes(&self) -> usize {
743        let cols = 4 + usize::from(self.has_volume());
744        cols * self.len() * core::mem::size_of::<f64>() + self.lod.heap_bytes()
745    }
746}
747
748#[cfg(test)]
749#[allow(clippy::float_cmp)] // exact values are the point of these tests
750mod tests {
751    use super::*;
752
753    fn cols(times: &[i64], closes: &[f64]) -> OhlcColumns {
754        OhlcColumns {
755            time: times.to_vec(),
756            open: closes.to_vec(),
757            high: closes.iter().map(|c| c + 1.0).collect(),
758            low: closes.iter().map(|c| c - 1.0).collect(),
759            close: closes.to_vec(),
760            volume: Some(vec![1.0; closes.len()]),
761        }
762    }
763
764    /// Every value of every level of `a` and `b`, as bits.
765    fn levels(s: &OhlcStore) -> Vec<(u8, i64, Vec<u64>)> {
766        let top = s.lod().max_level().unwrap_or(0);
767        (0..=top)
768            .filter_map(|k| s.level_view(k))
769            .map(|v| {
770                let mut all = Vec::new();
771                for c in [&v.open, &v.high, &v.low, &v.close] {
772                    all.extend((0..c.len()).map(|i| c.get(i).unwrap_or(0.0).to_bits()));
773                }
774                if let Some(c) = &v.volume {
775                    all.extend((0..c.len()).map(|i| c.get(i).unwrap_or(0.0).to_bits()));
776                }
777                (v.level, v.first, all)
778            })
779            .collect()
780    }
781
782    #[test]
783    fn truncate_back_equals_a_fresh_store() {
784        let closes: Vec<f64> = (0..300).map(|i| 100.0 + f64::from(i % 37) * 0.75).collect();
785        let times: Vec<i64> = (0..300).collect();
786        for keep in [1, 2, 3, 4, 5, 63, 64, 65, 128, 200, 299] {
787            let mut s = OhlcStore::default();
788            s.replace_at(10, cols(&times, &closes), ValidationMode::Reject)
789                .unwrap();
790            let gen0 = s.log().generation();
791            s.truncate_back(300 - keep);
792            assert!(s.log().generation() > gen0);
793            let mut fresh = OhlcStore::default();
794            fresh
795                .replace_at(
796                    10,
797                    cols(&times[..keep], &closes[..keep]),
798                    ValidationMode::Reject,
799                )
800                .unwrap();
801            assert_eq!(s.len(), keep);
802            for (a, b) in levels(&s).iter().zip(levels(&fresh).iter()) {
803                assert_eq!(a, b, "keep {keep}");
804            }
805            assert_eq!(
806                s.extremes(10, 10 + keep as i64),
807                fresh.extremes(10, 10 + keep as i64)
808            );
809            // Writing over the end again works.
810            let bar = BarUpdate {
811                time: 0,
812                open: 1.0,
813                high: 500.0,
814                low: 0.5,
815                close: 2.0,
816                volume: Some(1.0),
817            };
818            s.append(
819                None,
820                OhlcStore::bar_columns(bar, false),
821                ValidationMode::Reject,
822            )
823            .unwrap();
824            assert_eq!(s.extremes(10, 11 + keep as i64).map(|e| e.1), Some(500.0));
825        }
826        let mut s = OhlcStore::default();
827        s.replace_at(0, cols(&times, &closes), ValidationMode::Reject)
828            .unwrap();
829        s.truncate_back(1000);
830        assert_eq!(s.len(), 0);
831        assert_eq!(s.lod().max_level(), None);
832    }
833
834    fn setup() -> (OhlcStore, TimeIndex) {
835        let mut s = OhlcStore::default();
836        let mut index = TimeIndex::new();
837        s.replace(
838            &mut index,
839            cols(&[10, 20, 30], &[1.0, 2.0, 3.0]),
840            ValidationMode::Reject,
841        )
842        .unwrap();
843        (s, index)
844    }
845
846    fn snapshot(s: &OhlcStore, index: &TimeIndex) -> (Vec<f64>, Vec<i64>, i64, u64) {
847        (
848            s.close.to_vec(),
849            index.times().to_vec(),
850            s.first,
851            s.log.generation(),
852        )
853    }
854
855    #[test]
856    fn validation_errors_leave_store_unchanged() {
857        let (mut s, mut index) = setup();
858        let before = snapshot(&s, &index);
859        let mut bad = cols(&[40], &[4.0]);
860        bad.high[0] = 3.0; // below close
861        assert_eq!(
862            s.append(Some(&mut index), bad.clone(), ValidationMode::Reject),
863            Err(Error::InvalidOhlc { index: 0 })
864        );
865        let mut nan = cols(&[40], &[4.0]);
866        nan.open[0] = f64::NAN;
867        assert_eq!(
868            s.append(Some(&mut index), nan, ValidationMode::Repair),
869            Err(Error::InvalidOhlc { index: 0 })
870        );
871        let mut short = cols(&[40, 50], &[4.0, 5.0]);
872        short.low.pop();
873        assert_eq!(
874            s.append(Some(&mut index), short, ValidationMode::Reject),
875            Err(Error::LengthMismatch { column: "low" })
876        );
877        let mut novol = cols(&[40], &[4.0]);
878        novol.volume = None;
879        assert_eq!(
880            s.append(Some(&mut index), novol, ValidationMode::Reject),
881            Err(Error::LengthMismatch { column: "volume" })
882        );
883        assert_eq!(
884            s.append(
885                Some(&mut index),
886                cols(&[30], &[4.0]),
887                ValidationMode::Reject
888            ),
889            Err(Error::TimeNotIncreasing { index: 0 })
890        );
891        assert_eq!(
892            s.prepend(
893                Some(&mut index),
894                cols(&[5, 10], &[4.0, 5.0]),
895                ValidationMode::Reject
896            ),
897            Err(Error::TimeNotIncreasing { index: 1 })
898        );
899        assert_eq!(snapshot(&s, &index), before);
900        // Repair mode fixes the bar and reports it.
901        assert_eq!(
902            s.append(Some(&mut index), bad, ValidationMode::Repair),
903            Ok(1)
904        );
905        assert_eq!(s.high[3], 4.0);
906    }
907
908    #[test]
909    fn update_last_and_append_and_prepend() {
910        let (mut s, mut index) = setup();
911        let mut tick = BarUpdate {
912            time: 30,
913            open: 3.0,
914            high: 9.0,
915            low: 2.0,
916            close: 8.0,
917            volume: Some(2.0),
918        };
919        assert_eq!(
920            s.update_last(&mut index, tick, ValidationMode::Reject),
921            Ok((DataChange::UpdateLast, 0))
922        );
923        assert_eq!(s.close[2], 8.0);
924        tick.time = 40;
925        assert_eq!(
926            s.update_last(&mut index, tick, ValidationMode::Reject),
927            Ok((DataChange::Append { count: 1 }, 0))
928        );
929        tick.time = 35;
930        assert_eq!(
931            s.update_last(&mut index, tick, ValidationMode::Reject),
932            Err(Error::TimeNotIncreasing { index: 0 })
933        );
934        s.prepend(
935            Some(&mut index),
936            cols(&[1, 2], &[0.5, 0.6]),
937            ValidationMode::Reject,
938        )
939        .unwrap();
940        assert_eq!(s.first(), -2);
941        assert_eq!(index.first_index(), -2);
942        assert_eq!(s.len(), 6);
943        assert_eq!(s.extremes(-10, 10), Some((-0.5, 9.0)));
944        assert_eq!(s.extremes(-10, 10), Some((-0.5, 9.0))); // cached
945    }
946
947    #[test]
948    fn replace_resets_change_log() {
949        let (mut s, mut index) = setup();
950        let synced = s.log().generation();
951        s.replace(&mut index, cols(&[1], &[1.0]), ValidationMode::Reject)
952            .unwrap();
953        assert_eq!(s.log().changes_since(synced), super::super::DataDelta::Full);
954    }
955}