timeless 0.0.14-alpha

A crate of utilities for storing in-memory timeseries data.
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
//! This is code responsible for possibly chunked data.

#[derive(Clone, Default, Debug)]
struct DataChunk<T> {
    /// The start offset of this chunk, should correspond to the time vector
    /// indices. If that updates, this MUST also update.
    start_offset: usize,

    /// The actual value data!
    data: Vec<T>,
}

impl<T> DataChunk<T> {
    fn push(&mut self, item: T) {
        self.data.push(item)
    }
}

/// An iterator created from a [`ChunkedData`].
pub struct ChunkedDataIter<I: Iterator> {
    iter: I,
    size: usize, // TODO: We can probably store this as a `OnceCell` instead to avoid computing it if we don't need it.
}

impl<T, I: Iterator<Item = T>> Iterator for ChunkedDataIter<I> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next()
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.size, Some(self.size))
    }
}

impl<T, I: Iterator<Item = T>> ExactSizeIterator for ChunkedDataIter<I> {}

impl<T, I: Iterator<Item = T> + DoubleEndedIterator> DoubleEndedIterator for ChunkedDataIter<I> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.iter.next_back()
    }
}

/// A struct representing data that may potentially have breaks.
/// If you expect that you may want to store time values but _not_
/// data values, use this to avoid storing blanks.
#[derive(Clone, Default, Debug)]
pub struct ChunkedData<D> {
    next_index: usize,
    is_active: bool,
    chunks: Vec<DataChunk<D>>,
}

impl<D> ChunkedData<D> {
    /// Returns an iterator of items alongside the associated indices for each item.
    pub fn iter_with_index(&self) -> ChunkedDataIter<impl DoubleEndedIterator<Item = (usize, &D)>> {
        let size = self.chunks.iter().map(|dc| dc.data.len()).sum();
        let iter = self.chunks.iter().flat_map(|dc| {
            let start = dc.start_offset;

            dc.data
                .iter()
                .enumerate()
                .map(move |(offset, datum)| (start + offset, datum))
        });

        ChunkedDataIter { iter, size }
    }

    /// Returns an iterator of items.
    pub fn iter(&self) -> ChunkedDataIter<impl DoubleEndedIterator<Item = &D>> {
        let size = self.chunks.iter().map(|dc| dc.data.len()).sum();
        let iter = self.chunks.iter().flat_map(|dc| dc.data.iter());

        ChunkedDataIter { iter, size }
    }

    /// Returns an iterator of owned items. This consumes the [`ChunkedData`].
    ///
    /// Note this is currently not just `into_iter` due to how it's implemented, this is subject to change.
    pub fn into_owned_iter(self) -> ChunkedDataIter<impl DoubleEndedIterator<Item = D>> {
        let size = self.chunks.iter().map(|dc| dc.data.len()).sum();
        let iter = self.chunks.into_iter().flat_map(|dc| dc.data.into_iter());

        ChunkedDataIter { iter, size }
    }

    /// Given a slice that serves as the "base" yielding items `T`, return an iterator of `(T, D)`, where each `D` from
    /// the [`ChunkedData`] has its index associated with that of `base_slice`.
    ///
    /// This is meant to be used alongside a slice of time values.
    ///
    /// Note this will return the minimum of the number of elements in either the base slice or the [`ChunkedData`].
    pub fn iter_along_base<'a, T>(
        &'a self, base_slice: &'a [T],
    ) -> ChunkedDataIter<impl DoubleEndedIterator<Item = (&'a T, &'a D)>> {
        let size = if base_slice.len() >= self.length() {
            // Happy path. We return at most the number of stored elements!
            self.num_elements()
        } else {
            // Uh oh. We need a bit more logic around this step.

            let mut num_returned_elements = 0;

            for dc in self.chunks.iter() {
                let start = dc.start_offset;
                let end = dc.start_offset + dc.data.len();

                if base_slice.len() > end {
                    num_returned_elements += dc.data.len();
                } else if base_slice.len() < start {
                    break;
                } else {
                    // It's somewhere in the middle.
                    num_returned_elements += base_slice.len() - start;
                }
            }

            num_returned_elements
        };

        let iter = self.chunks.iter().flat_map(move |dc| {
            let start = dc.start_offset;
            let to_take = base_slice.len().saturating_sub(start); // Take at most all the values in the chunk, or up to the number of base element indices.

            dc.data
                .iter()
                .take(to_take)
                .enumerate()
                .map(move |(offset, datum)| {
                    let actual_index = start + offset;
                    let base = &base_slice[actual_index];

                    (base, datum)
                })
        });

        ChunkedDataIter { iter, size }
    }

    /// Return how many elements actually are stored in the [`ChunkedData`].
    pub fn num_elements(&self) -> usize {
        self.chunks.iter().map(|dc| dc.data.len()).sum()
    }

    /// Return the "length" of the [`ChunkedData`], _including_ skipped
    /// elements.
    pub fn length(&self) -> usize {
        self.next_index
    }

    /// Push an element.
    pub fn push(&mut self, item: D) {
        if self.is_active {
            let current_chunk = self
                .chunks
                .last_mut()
                .expect("chunks must be initialized with at least a value if is_active is set");
            current_chunk.push(item);
        } else {
            // Start a new chunk.
            self.chunks.push(DataChunk {
                start_offset: self.next_index,
                data: vec![item],
            });
            self.is_active = true;
        }

        self.next_index += 1;
    }

    /// Manually mark that a break is needed in the chunk.
    pub fn insert_break(&mut self) {
        // "Seal" the latest chunk.
        self.is_active = false;
    }

    /// Push an element. If `item` is [`None`], then it will automatically
    /// insert a break in the chunk if needed.
    pub fn try_push(&mut self, item: Option<D>) {
        match item {
            Some(item) => {
                self.push(item);
            }
            None => {
                self.insert_break();
                self.next_index += 1;
            }
        }
    }

    /// Remove all elements up to (and including) `index`, including "skipped"
    /// elements. This will result in the effective length becoming
    /// `prev_length - index - 1`.
    ///
    /// If `index` goes past the number of elements, this function will return
    /// an error containing the stored index in the [`ChunkedData`].
    pub fn prune(&mut self, index: usize) -> Result<(), usize> {
        if self.next_index == 0 || self.next_index - 1 < index || self.chunks.is_empty() {
            return Err(self.next_index);
        }

        self.next_index -= index + 1;

        let dc_index = match self.chunks.binary_search_by(|c| c.start_offset.cmp(&index)) {
            Ok(result) => result,
            Err(result) => {
                if result > 0 {
                    result - 1
                } else {
                    // Nothing to prune. We still need to change the offsets though.
                    for chunk in &mut self.chunks {
                        chunk.start_offset -= index + 1;
                    }

                    return Ok(());
                }
            }
        };

        // SAFETY: This index must be valid since it was returned from the binary search.
        let curr = unsafe { self.chunks.get_unchecked_mut(dc_index) };
        let to_remove = index - curr.start_offset + 1;

        if to_remove <= curr.data.len() {
            curr.data.drain(..to_remove);
            curr.start_offset = 0;

            // Remove all previous chunks.
            self.chunks.drain(0..dc_index);

            // Update offsets for all following chunks.
            for chunk in self.chunks.iter_mut().skip(1) {
                chunk.start_offset -= to_remove;
            }
        } else {
            // Drain this chunk too.
            self.chunks.drain(0..=dc_index);

            for chunk in &mut self.chunks {
                chunk.start_offset -= to_remove;
            }
        }

        Ok(())
    }

    /// Shrink the [`ChunkedData`] after.
    pub fn shrink_to_fit(&mut self) {
        for chunk in &mut self.chunks {
            chunk.data.shrink_to_fit();
        }

        self.chunks.shrink_to_fit();
    }

    /// Convenience function to prune _and_ shrink the [`ChunkedData`] after.
    pub fn prune_and_shrink_to_fit(&mut self, index: usize) -> Result<(), usize> {
        self.prune(index)?;
        self.shrink_to_fit();

        Ok(())
    }

    /// Try and return the first element.
    pub fn first(&self) -> Option<&D> {
        self.chunks.first().and_then(|chunk| chunk.data.first())
    }

    /// Try and return the last element.
    pub fn last(&self) -> Option<&D> {
        self.chunks.last().and_then(|chunk| chunk.data.last())
    }

    /// Return whether there are zero elements left stored internally.
    pub fn no_elements(&self) -> bool {
        self.num_elements() == 0
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn chunked_push() {
        let mut data = ChunkedData::default();
        assert!(data.no_elements());

        data.try_push(Some(1));

        assert!(!data.chunks.is_empty());
        assert!(data.is_active);
        assert_eq!(data.chunks.last().as_ref().unwrap().data, vec![1]);
        assert_eq!(data.next_index, 1);

        data.try_push(Some(2));
        data.try_push(None);

        assert!(!data.is_active);
        assert_eq!(data.chunks.len(), 1);
        assert_eq!(data.chunks.first().unwrap().data, vec![1, 2]);
        assert_eq!(data.next_index, 3);

        data.try_push(None);
        assert!(!data.is_active);
        assert_eq!(data.next_index, 4);

        data.try_push(Some(3));
        assert!(data.is_active);
        assert_eq!(data.chunks.last().as_ref().unwrap().data, vec![3]);
        assert_eq!(data.next_index, 5);

        assert_eq!(data.length(), 5);
        assert_eq!(data.num_elements(), 3);
    }

    /// Ensure that if we push nothing at first, we don't incorrectly try and
    /// seal nothing.
    #[test]
    fn chunked_empty_initial_push() {
        let mut data: ChunkedData<u64> = ChunkedData::default();

        data.try_push(None);
        assert!(!data.is_active);

        data.try_push(Some(1));
        assert!(data.is_active);
        assert_eq!(data.next_index, 2);
    }

    const POPULATION: [Option<u64>; 10] = [
        Some(1),
        Some(2),
        Some(3),
        None,
        None,
        None,
        Some(7),
        Some(8),
        Some(9),
        Some(10),
    ];

    fn test_populate(data: &mut ChunkedData<u64>) {
        for p in POPULATION {
            data.try_push(p);
        }
    }

    /// Initialize data, prune, and insert.
    #[track_caller]
    fn test_pruning(to_prune_index: usize) {
        // println!("Trying to prune up to index {to_prune_index}...");

        let mut data = ChunkedData::default();
        test_populate(&mut data);

        assert!(data.prune(to_prune_index).is_ok());

        let removed = to_prune_index + 1;
        let result = data
            .iter_with_index()
            .map(|(a, b)| (a, *b))
            .collect::<Vec<_>>();

        let expected = POPULATION
            .into_iter()
            .skip(removed)
            .enumerate()
            .filter_map(|(a, b)| b.map(|b| (a, b)))
            .collect::<Vec<_>>();

        assert_eq!(result, expected);
        assert_eq!(data.next_index, POPULATION.len() - (to_prune_index + 1));
    }

    #[test]
    fn chunked_prune() {
        test_pruning(0);
        test_pruning(1);
        test_pruning(2);
        test_pruning(3);
        test_pruning(4);
        test_pruning(5);
        test_pruning(6);
        test_pruning(7);
        test_pruning(8);
        test_pruning(9);
    }

    /// Handle if we try and prune something empty.
    #[test]
    fn chunked_prune_empty() {
        let mut data: ChunkedData<u64> = ChunkedData::default();
        assert!(data.prune(0).is_err());
    }

    /// Handle if we try to clear an index past the index stored.
    #[test]
    fn chunked_prune_past_index() {
        let mut data = ChunkedData::default();
        test_populate(&mut data);

        assert!(data.prune(10).is_err());
    }

    #[test]
    fn chunked_prune_without_curr() {
        let mut data = ChunkedData::default();
        test_populate(&mut data);
        data.try_push(None);

        assert!(data.prune(10).is_ok());
    }

    #[test]
    fn prune_zero_when_none() {
        let mut data = ChunkedData::default();
        data.try_push(None);
        data.try_push(None);
        data.try_push(None);
        test_populate(&mut data);

        assert!(data.prune(0).is_ok());
        assert_eq!(data.chunks[0].start_offset, 2);
        assert_eq!(data.chunks[1].start_offset, 8);
        assert_eq!(data.next_index, POPULATION.len() + 3 - 1);

        assert!(data.prune(0).is_ok());
        assert_eq!(data.chunks[0].start_offset, 1);
        assert_eq!(data.chunks[1].start_offset, 7);
        assert_eq!(data.next_index, POPULATION.len() + 3 - 2);

        assert!(data.prune(0).is_ok());
        assert_eq!(data.chunks[0].start_offset, 0);
        assert_eq!(data.chunks[1].start_offset, 6);
        assert_eq!(data.next_index, POPULATION.len() + 3 - 3);

        assert!(data.prune(0).is_ok());
        assert_eq!(data.chunks[0].start_offset, 0);
        assert_eq!(data.chunks[0].data.as_slice(), &[2, 3]);
        assert_eq!(data.chunks[1].start_offset, 5);
        assert_eq!(data.next_index, POPULATION.len() + 3 - 4);
    }

    #[test]
    fn first_last() {
        let mut data = ChunkedData::default();
        test_populate(&mut data);

        assert_eq!(data.first(), Some(&1));
        assert_eq!(data.last(), Some(&10));
    }

    #[test]
    fn iter() {
        let mut data = ChunkedData::default();
        test_populate(&mut data);

        assert_eq!(
            data.into_owned_iter().collect::<Vec<_>>(),
            POPULATION.iter().filter_map(|v| *v).collect::<Vec<_>>(),
        );
    }

    #[test]
    fn reverse_iter() {
        let mut data = ChunkedData::default();
        test_populate(&mut data);

        assert_eq!(
            data.into_owned_iter().rev().collect::<Vec<_>>(),
            POPULATION
                .iter()
                .filter_map(|v| *v)
                .rev()
                .collect::<Vec<_>>(),
        );
    }

    #[track_caller]
    fn base_slice_test(
        data: &ChunkedData<u32>, base_slice: &[u32], expected: &[(u32, u32)],
        base_slice_index: usize, expected_slice_index: usize,
    ) {
        let base_slice = &base_slice[0..base_slice_index];

        assert_eq!(
            data.iter_along_base(base_slice).len(),
            expected_slice_index,
            "the returned size of the iterator should match"
        );
        assert_eq!(
            data.iter_along_base(base_slice)
                .map(|(a, b)| (*a, *b))
                .collect::<Vec<_>>(),
            expected[0..expected_slice_index],
            "the actual generated values should match"
        );
    }

    #[test]
    fn base_slice_simple() {
        let mut data = ChunkedData::default();
        data.push(1);
        data.push(2);
        data.push(3);

        let base_slice = [1, 2, 3, 4, 5];
        let expected = [(1, 1), (2, 2), (3, 3)];

        // Test with a larger base slice
        base_slice_test(&data, &base_slice, &expected, 5, 3);

        // Test with an exact base slice
        base_slice_test(&data, &base_slice, &expected, 3, 3);

        // Test with a smaller base slice
        base_slice_test(&data, &base_slice, &expected, 2, 2);
        base_slice_test(&data, &base_slice, &expected, 1, 1);
        base_slice_test(&data, &base_slice, &expected, 0, 0);
    }

    #[test]
    fn base_slice_chunked() {
        let mut data = ChunkedData::default();
        data.push(1);
        data.push(2);
        data.push(3);
        data.try_push(None);
        data.try_push(None);
        data.try_push(None);
        data.push(7);
        data.push(8);
        data.push(9);

        let base_slice = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
        let expected = [(1, 1), (2, 2), (3, 3), (7, 7), (8, 8), (9, 9)];

        // Test with a larger base slice
        base_slice_test(&data, &base_slice, &expected, 10, 6);

        // Test with an exact base slice
        base_slice_test(&data, &base_slice, &expected, 9, 6);

        // Test with a smaller base slice
        base_slice_test(&data, &base_slice, &expected, 8, 5);
        base_slice_test(&data, &base_slice, &expected, 7, 4);
        base_slice_test(&data, &base_slice, &expected, 6, 3);
        base_slice_test(&data, &base_slice, &expected, 5, 3);
        base_slice_test(&data, &base_slice, &expected, 4, 3);
        base_slice_test(&data, &base_slice, &expected, 3, 3);
        base_slice_test(&data, &base_slice, &expected, 2, 2);
        base_slice_test(&data, &base_slice, &expected, 1, 1);
        base_slice_test(&data, &base_slice, &expected, 0, 0);
    }
}