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
use crate::{Error, Point, Result};
use std::collections::BTreeMap;

pub struct Store {
    last_t: u32,
    val_len: u8,
    all: BTreeMap<u32, Vec<u16>>,
}

impl Store {
    pub fn new(val_len: u8) -> Store {
        Store {
            last_t: 0,
            val_len,
            all: BTreeMap::new(),
        }
    }

    pub fn ingest(&mut self, ps: &[Point]) -> Result<()> {
        for p in ps {
            if p.t <= self.last_t {
                return Err(Error::String("t <= last_t".to_owned()));
            }
            self.last_t = p.t;

            assert!(p.vs.len() == self.val_len as usize);
            self.all.insert(p.t, p.vs.clone());
        }

        trace!("ingest all.len={} last_t={}", self.all.len(), self.last_t);

        Ok(())
    }

    pub fn discard(&mut self, t0: u32, t1: u32) -> Result<()> {
        for t in self.all.range(t0..t1).map(|(t,_vs)| *t).collect::<Vec<u32>>() {
            self.all.remove(&t);
        }
        Ok(())
    }

    /// Returns a Vec of the points with t >= t0, < t1.
    pub fn query_range(&self, t0: u32, t1: u32) -> Result<Vec<Point>> {
        let rv: Vec<Point> =
            self.all.range(t0..t1)
                .map(|(t,vs)| Point { t: *t, vs: vs.clone() })
                .collect();
        trace!("query t0={} t1={} rv.len={}", t0, t1, rv.len());
        Ok(rv)
    }

    /// Returns the first point with t >= given t.
    pub fn query_point(&self, t: u32) -> Result<Option<Point>> {
        let rv = self.all.range(t..)
                     .map(|(t,vs)| Point { t: *t, vs: vs.clone() })
                     .next();
        Ok(rv)
    }

    pub fn last_t(&self) -> u32 {
        self.last_t
    }

    pub fn first_t(&self) -> u32 {
        self.query_point(0).unwrap()
                           .map_or(0, |pt| pt.t)
    }

    pub fn val_len(&self) -> u8 {
        self.val_len
    }
}