tstorage 0.1.0

Embedded time-series database
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
use crate::metric::{DataPoint, Row};
use anyhow::Result;
use std::path::Path;

use super::{disk::flush, Boundary, Partition, PartitionError, PointPartitionOrdering};

#[derive(Debug)]
pub struct MemoryPartition {
    pub map: dashmap::DashMap<String, MetricEntry>,
    partition_boundary: Boundary,
}

impl Partition for MemoryPartition {
    fn select(&self, name: &str, start: i64, end: i64) -> Result<Vec<DataPoint>> {
        Ok(match self.partition_boundary.contains_range(start, end) {
            true => match self.map.get(name) {
                Some(entry) => entry.select(start, end),
                None => vec![],
            },
            false => vec![],
        })
    }

    fn insert(&self, row: &Row) -> Result<(), PartitionError> {
        if !self
            .partition_boundary
            .contains_point(row.data_point.timestamp)
        {
            return Err(PartitionError::OutOfBounds);
        }
        match self.map.get_mut(row.metric) {
            Some(mut m) => {
                m.insert(row.data_point);
            }
            None => {
                self.map
                    .insert(row.metric.to_string(), MetricEntry::new(row.data_point));
            }
        };
        Ok(())
    }

    fn ordering(&self, row: &Row) -> PointPartitionOrdering {
        self.partition_boundary.ordering(row.data_point.timestamp)
    }

    fn flush(
        &self,
        dir_path: &Path,
        encode_strategy: crate::EncodeStrategy,
    ) -> Result<(), PartitionError> {
        flush(self, dir_path, encode_strategy).map_err(|_| PartitionError::Flush)
    }

    fn boundary(&self) -> Boundary {
        self.partition_boundary
    }

    fn clean(&self) -> Result<(), PartitionError> {
        // Memory held by this partition gets automatically removed
        // when the variable is dropped.
        Ok(())
    }
}

impl MemoryPartition {
    pub fn new(partition_duration: i64, min_timestamp: i64) -> Self {
        let partition_boundary = Boundary {
            min_timestamp,
            max_timestamp: min_timestamp + partition_duration,
        };

        Self {
            map: dashmap::DashMap::new(),
            partition_boundary,
        }
    }
}

#[derive(Debug)]
pub struct MetricEntry {
    pub data_points: Vec<DataPoint>,
}

impl MetricEntry {
    pub fn new(data_point: DataPoint) -> Self {
        Self {
            data_points: vec![data_point],
        }
    }

    pub fn min_timestamp(&self) -> i64 {
        if self.data_points.is_empty() {
            0
        } else {
            self.data_points[0].timestamp
        }
    }

    pub fn max_timestamp(&self) -> i64 {
        if self.data_points.is_empty() {
            0
        } else {
            self.data_points[self.data_points.len() - 1].timestamp
        }
    }

    pub fn select(&self, start: i64, end: i64) -> Vec<DataPoint> {
        if self.data_points.is_empty() {
            return vec![];
        }

        let min_timestamp = self.data_points[0].timestamp;
        let max_timestamp = self.data_points[self.data_points.len() - 1].timestamp;
        if min_timestamp > end || max_timestamp < start {
            // Out of range
            return vec![];
        }

        let start_idx = if start <= min_timestamp {
            0
        } else {
            self.data_points
                .binary_search_by(|dp| {
                    if dp.timestamp >= start {
                        std::cmp::Ordering::Greater
                    } else {
                        std::cmp::Ordering::Less
                    }
                })
                .unwrap_or_else(|i| i)
        };

        let end_idx = if end >= max_timestamp {
            self.data_points.len()
        } else {
            self.data_points
                .binary_search_by(|dp| {
                    if dp.timestamp > end {
                        std::cmp::Ordering::Greater
                    } else {
                        std::cmp::Ordering::Less
                    }
                })
                .unwrap_or_else(|i| i)
        };

        return self.data_points[start_idx..end_idx].to_vec();
    }

    pub fn insert(&mut self, data_point: DataPoint) {
        match self.data_points.is_empty() {
            true => self.data_points.push(data_point),
            false => {
                let max_timestamp = self.data_points[self.data_points.len() - 1].timestamp;
                if data_point.timestamp >= max_timestamp {
                    self.data_points.push(data_point)
                } else {
                    // Out-of-order insert
                    let pos = self
                        .data_points
                        .binary_search_by_key(&data_point.timestamp, |d| d.timestamp)
                        .unwrap_or_else(|i| i);
                    self.data_points.insert(pos, data_point);
                }
            }
        }
    }
}

#[cfg(test)]
pub mod tests {
    use crate::{
        metric::{DataPoint, Row},
        partition::{memory::Partition, Boundary, PartitionError},
    };

    use super::MemoryPartition;

    fn create_partition_with_rows(partition_duration: i64, rows: &[Row]) -> MemoryPartition {
        assert!(!rows.is_empty());
        let partition = MemoryPartition::new(partition_duration, rows[0].data_point.timestamp);
        for row in rows {
            partition.insert(row).unwrap();
        }
        partition
    }

    #[test]
    fn test_partition_boundary() {
        let boundaries = Boundary {
            min_timestamp: 1000,
            max_timestamp: 2000,
        };
        assert!(boundaries.contains_range(1500, 1800));
        assert!(boundaries.contains_range(0, 3000));
        assert!(boundaries.contains_range(1999, 2000));

        assert!(!boundaries.contains_range(2000, 2001));
        assert!(!boundaries.contains_range(0, 999));
    }

    #[test]
    fn test_min_max_timestamp() {
        let partition = MemoryPartition::new(1000, 1234);
        assert_eq!(partition.boundary().min_timestamp(), 1234);
        assert_eq!(partition.boundary().max_timestamp(), 2234);
    }

    #[test]
    fn test_simple_select_in_range() {
        let metric = "hello";
        let data_point = DataPoint {
            timestamp: 1234,
            value: 4.20,
        };
        let row = Row { metric, data_point };
        let partition = MemoryPartition::new(60000, data_point.timestamp);
        partition.insert(&row).unwrap();
        let result = partition.select(metric, 1000, 2000).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(data_point, result[0]);
    }

    #[test]
    fn test_multiple_metrics() {
        let metric_a = "hello";
        let data_point_a = DataPoint {
            timestamp: 1000,
            value: 4.20,
        };
        let row_a = &Row {
            metric: metric_a,
            data_point: data_point_a,
        };

        let metric_b = "world";
        let data_point_b = DataPoint {
            timestamp: 1001,
            value: 1.50,
        };
        let row_b = &Row {
            metric: metric_b,
            data_point: data_point_b,
        };

        let partition = MemoryPartition::new(60000, row_a.data_point.timestamp);
        partition.insert(row_a).unwrap();
        partition.insert(row_b).unwrap();

        let result = partition.select(metric_a, 1000, 2000).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(data_point_a, result[0]);

        let result = partition.select(metric_b, 1000, 2000).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(data_point_b, result[0]);
    }

    #[test]
    fn test_simple_select_out_of_range() {
        let metric = "hello";
        let row = Row {
            metric,
            data_point: DataPoint {
                timestamp: 1234,
                value: 4.20,
            },
        };
        let partition = MemoryPartition::new(60000, row.data_point.timestamp);
        partition.insert(&row).unwrap();
        let result = partition.select(metric, 0, 1000).unwrap();
        assert_eq!(result.len(), 0);
    }

    #[test]
    fn test_select_boundaries() {
        let metric = "hello";
        let data_points = [
            DataPoint {
                timestamp: 100,
                value: 0.0,
            },
            DataPoint {
                timestamp: 200,
                value: 0.0,
            },
            DataPoint {
                timestamp: 200,
                value: 1.0,
            },
            DataPoint {
                timestamp: 300,
                value: 0.0,
            },
            DataPoint {
                timestamp: 400,
                value: 0.0,
            },
            DataPoint {
                timestamp: 400,
                value: 1.0,
            },
            DataPoint {
                timestamp: 400,
                value: 2.0,
            },
            DataPoint {
                timestamp: 500,
                value: 0.0,
            },
        ];
        let rows: Vec<Row> = data_points
            .iter()
            .map(|dp| Row {
                metric,
                data_point: *dp,
            })
            .collect();

        let partition = create_partition_with_rows(60000, &rows);

        let result = partition.select(metric, 200, 400).unwrap();
        assert_eq!(result, data_points[1..7]);
    }

    #[test]
    fn test_complex_select() {
        let metric = "hello";
        let data_points = [
            DataPoint {
                timestamp: 100,
                value: 0.0,
            },
            DataPoint {
                timestamp: 200,
                value: 0.0,
            },
            DataPoint {
                timestamp: 200,
                value: 1.0,
            },
            DataPoint {
                timestamp: 300,
                value: 0.0,
            },
        ];

        let rows: Vec<Row> = data_points
            .iter()
            .map(|dp| Row {
                metric,
                data_point: *dp,
            })
            .collect();

        let partition = create_partition_with_rows(60000, &rows);

        let result = partition.select(metric, 101, 300).unwrap();
        assert_eq!(result, data_points[1..]);
    }

    #[test]
    fn test_past_writes() {
        let metric = "hello";
        let data_points = [
            DataPoint {
                timestamp: 200, // sets the timestamp baseline for the partition
                value: 2.0,
            },
            DataPoint {
                timestamp: 100, // should belong to a previous partition
                value: 1.0,
            },
            DataPoint {
                timestamp: 300,
                value: 3.0,
            },
        ];

        let rows: Vec<Row> = data_points
            .iter()
            .map(|dp| Row {
                metric,
                data_point: *dp,
            })
            .collect();

        let partition = MemoryPartition::new(60000, rows[0].data_point.timestamp);
        partition.insert(&rows[0]).unwrap();
        assert_eq!(
            partition.insert(&rows[1]).err(),
            Some(PartitionError::OutOfBounds)
        );
        partition.insert(&rows[2]).unwrap();

        let result = partition.select(metric, 0, 1000).unwrap();
        assert_eq!(result, vec![data_points[0], data_points[2]]);
    }

    #[test]
    fn test_out_of_order_writes() {
        let metric = "hello";
        let mut data_points = [
            DataPoint {
                timestamp: 100,
                value: 0.0,
            },
            DataPoint {
                timestamp: 300,
                value: 0.0,
            },
            DataPoint {
                timestamp: 200, // out of order
                value: 0.0,
            },
            DataPoint {
                timestamp: 400,
                value: 0.0,
            },
        ];

        let rows: Vec<Row> = data_points
            .iter()
            .map(|dp| Row {
                metric,
                data_point: *dp,
            })
            .collect();

        let partition = create_partition_with_rows(60000, &rows);
        data_points.sort_by_key(|d| d.timestamp);

        let result = partition.select(metric, 0, 1000).unwrap();
        assert_eq!(result, data_points);
    }
}