tinystore 0.2.0

A small storage runtime for applications on SQLite: SQL, key-value state, durable jobs, files, search, logs and metrics in one directory
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
//! A query: the series of a name, of labels under conditions, or of both,
//! over a range of time. `read` gives their samples, `aggregate` their
//! buckets, `latest` the newest sample of each, and `explain` what a read or
//! an aggregate would cost.
//!
//! ```no_run
//! # use std::time::Duration;
//! # fn f(stats: tinystore::metrics::Metrics) -> tinystore::Result<()> {
//! use tinystore::metrics::{Not, Prefix};
//! let failing = stats
//!     .query()
//!     .name("http_requests_total")
//!     .since(Duration::from_hours(1))
//!     .filter("status", ["500", "502"])
//!     .filter("env", Not("dev"))
//!     .filter("host", Prefix("api-"))
//!     .read()?;
//! # Ok(())
//! # }
//! ```

use std::fmt;
use std::sync::Arc;
use std::time::{Duration, SystemTime};

use super::aggregate::{Aggregate, Asking, Op, Shape};
use super::engine::Engine;
use super::explain::Plan;
use super::labels::{MOST_LABELS, MOST_NAME_BYTES, MOST_VALUE_BYTES, NAME};
use super::latest::Latest;
use super::series::Series;
use crate::calendar::Calendar;
use crate::{Error, Result, unix_millis};

/// The values a list of a condition holds at most.
const MOST_VALUES: usize = 1000;

/// What a label's value must be for a query to take a series.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Condition {
    /// One of these values.
    OneOf(Vec<String>),
    /// None of these values, or no such label at all.
    NoneOf(Vec<String>),
    /// A value that starts with this text.
    Prefix(String),
}

/// A label equal to none of these values, or absent: `Not("dev")`,
/// `Not(["dev", "test"])`.
#[derive(Clone, Copy, Debug)]
pub struct Not<T>(pub T);

/// A label that starts with this text: `Prefix("api-")`.
#[derive(Clone, Copy, Debug)]
pub struct Prefix<T>(pub T);

/// The values a condition lists: text, or a number or a boolean as its text,
/// as a label keeps them.
pub trait Values {
    fn values(self) -> Vec<String>;
}

impl Values for &str {
    fn values(self) -> Vec<String> {
        vec![self.to_owned()]
    }
}

impl Values for String {
    fn values(self) -> Vec<String> {
        vec![self]
    }
}

impl Values for &String {
    fn values(self) -> Vec<String> {
        vec![self.clone()]
    }
}

impl<T: Values, const N: usize> Values for [T; N] {
    fn values(self) -> Vec<String> {
        self.into_iter().flat_map(Values::values).collect()
    }
}

impl<T: Values> Values for Vec<T> {
    fn values(self) -> Vec<String> {
        self.into_iter().flat_map(Values::values).collect()
    }
}

impl<T: Values + Clone> Values for &[T] {
    fn values(self) -> Vec<String> {
        self.iter().cloned().flat_map(Values::values).collect()
    }
}

macro_rules! values_as_text {
    ($($kind:ty),*) => {$(
        impl Values for $kind {
            fn values(self) -> Vec<String> {
                vec![self.to_string()]
            }
        }
    )*};
}
values_as_text!(i8, i16, i32, i64, u8, u16, u32, u64, usize, bool);

impl<T: Values> From<T> for Condition {
    fn from(values: T) -> Condition {
        Condition::OneOf(values.values())
    }
}

impl<T: Values> From<Not<T>> for Condition {
    fn from(Not(values): Not<T>) -> Condition {
        Condition::NoneOf(values.values())
    }
}

impl<T: Into<String>> From<Prefix<T>> for Condition {
    fn from(Prefix(prefix): Prefix<T>) -> Condition {
        Condition::Prefix(prefix.into())
    }
}

/// A call of a query checked, to run: what `read`, `aggregate`, `latest` or
/// `explain` gives once its query passed its checks.
pub(crate) type Checked<T> = Box<dyn FnOnce() -> Result<T> + Send>;

/// A query being asked: its name, conditions and range, and for an
/// aggregate its buckets and groups.
#[derive(Clone)]
pub struct Query {
    pub(super) engine: Arc<Engine>,
    name: Option<String>,
    conditions: Vec<(String, Condition)>,
    since: Option<Duration>,
    from: Option<SystemTime>,
    to: Option<SystemTime>,
    every: Option<Duration>,
    calendar: Option<(Calendar, String)>,
    by: Option<Vec<String>>,
    without: Option<Vec<String>>,
    estimate: bool,
}

impl fmt::Debug for Query {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Query")
            .field("name", &self.name)
            .field("conditions", &self.conditions)
            .field("since", &self.since)
            .field("from", &self.from)
            .field("to", &self.to)
            .field("every", &self.every)
            .field("calendar", &self.calendar)
            .field("by", &self.by)
            .field("without", &self.without)
            .field("estimate", &self.estimate)
            .finish()
    }
}

/// A query checked: its conditions on labels, the name among them as
/// `__name__`, and its range in unix milliseconds, `[from, to)`.
#[derive(Clone, Debug)]
pub(super) struct Asked {
    /// The name it asked for, which a timer's query reads as the series the
    /// timer writes.
    pub(super) name: Option<String>,
    pub(super) conditions: Vec<(String, Condition)>,
    pub(super) from: i64,
    pub(super) to: i64,
    /// The store's clock, read once a query.
    pub(super) now: i64,
}

impl Query {
    pub(super) fn of(engine: Arc<Engine>) -> Query {
        Query {
            engine,
            name: None,
            conditions: Vec::new(),
            since: None,
            from: None,
            to: None,
            every: None,
            calendar: None,
            by: None,
            without: None,
            estimate: false,
        }
    }

    /// The series of this name.
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }

    /// A label's value under a condition: a value, a list of them, a
    /// [`Not`] or a [`Prefix`].
    pub fn filter(mut self, label: impl Into<String>, condition: impl Into<Condition>) -> Self {
        self.conditions.push((label.into(), condition.into()));
        self
    }

    /// The span before now the range covers; or `from` and `to`.
    pub fn since(mut self, span: Duration) -> Self {
        self.since = Some(span);
        self
    }

    /// The range's start, which is in it.
    pub fn from(mut self, at: SystemTime) -> Self {
        self.from = Some(at);
        self
    }

    /// The range's end, which is not in it; now unless given, a sample taken
    /// now in the range.
    pub fn to(mut self, at: SystemTime) -> Self {
        self.to = Some(at);
        self
    }

    /// An aggregate's buckets: spans this long, each beginning at a whole
    /// number of them since 1970 in UTC, the range cutting the first and the
    /// last. Without it the range is one bucket.
    pub fn every(mut self, span: Duration) -> Self {
        self.every = Some(span);
        self
    }

    /// An aggregate's buckets on the calendar of a time zone, its IANA name:
    /// `calendar(Calendar::Day, "Europe/Moscow")` begins each at a local
    /// midnight, a day 23 or 25 hours long when the clock moves. A bucket's
    /// `at` is the instant it begins, and a rate counts its own seconds.
    pub fn calendar(mut self, unit: Calendar, zone: &str) -> Self {
        self.calendar = Some((unit, zone.to_owned()));
        self
    }

    /// The labels an aggregate keeps: the series of a name that share them
    /// are one group. Without `by` and `without`, every series of a name is
    /// one group.
    pub fn by(mut self, labels: &[&str]) -> Self {
        self.by.get_or_insert_default().extend(labels.iter().map(|label| (*label).to_owned()));
        self
    }

    /// The labels an aggregate leaves out, keeping every other: `&[]` keeps
    /// every series apart.
    pub fn without(mut self, labels: &[&str]) -> Self {
        self.without.get_or_insert_default().extend(labels.iter().map(|label| (*label).to_owned()));
        self
    }

    /// A timer's percentile also gives a point between the bounds of the
    /// bucket of its spread that holds the rank, interpolated as Prometheus'
    /// `histogram_quantile` does: an estimate, which no measurement gave.
    pub fn estimate(mut self) -> Self {
        self.estimate = true;
        self
    }

    /// The samples of each series the query finds, in its range: a series
    /// without one is left out. Past a bound of a read it is a `limit`
    /// error, naming the bound, never fewer series or samples.
    pub fn read(&self) -> Result<Vec<Series>> {
        self.checked_read().and_then(|read| read())
    }

    /// `read` in its two steps: the query checked now, its `since` an
    /// absolute start from the store's clock, and the read it gives, which
    /// the async API runs on the store's pool.
    pub(crate) fn checked_read(&self) -> Result<Checked<Vec<Series>>> {
        let checked = self.plain("read").and_then(|()| self.asked());
        self.once_checked(checked, |engine, asked| engine.read(&asked))
    }

    /// The buckets of each group of the series the query finds, each one
    /// exact value of `op`, rounded once: a group without a sample in the
    /// range is left out, and so is a bucket without one. An `op` a series'
    /// kind has not is `invalid`, and so is a bucket over a NaN, an infinity
    /// or a counter below zero; past a bound it is a `limit` error, naming
    /// the bound, never fewer buckets.
    pub fn aggregate(&self, op: Op) -> Result<Vec<Aggregate>> {
        self.checked_aggregate(op).and_then(|aggregate| aggregate())
    }

    /// `aggregate` in its two steps, as [`Query::checked_read`] is `read`.
    pub(crate) fn checked_aggregate(&self, op: Op) -> Result<Checked<Vec<Aggregate>>> {
        let checked = self.shape(op).and_then(|shape| Ok((self.asked()?, shape)));
        self.once_checked(checked, |engine, (asked, shape)| engine.aggregate(&asked, &shape))
    }

    /// The newest sample of each series the query finds, when it has one in
    /// the range: a series that stopped writing leaves the answer rather
    /// than show its last value for ever.
    pub fn latest(&self) -> Result<Vec<Latest>> {
        self.checked_latest().and_then(|latest| latest())
    }

    /// `latest` in its two steps, as [`Query::checked_read`] is `read`.
    pub(crate) fn checked_latest(&self) -> Result<Checked<Vec<Latest>>> {
        let checked = self.plain("latest").and_then(|()| self.asked());
        self.once_checked(checked, |engine, asked| engine.latest(&asked))
    }

    /// What `read`, or `aggregate` when given its `op`, would spend of each
    /// bound of a query, and the bound it would stop at. It decodes no
    /// sample.
    ///
    /// ```no_run
    /// # use std::time::Duration;
    /// # fn f(stats: tinystore::metrics::Metrics) -> tinystore::Result<()> {
    /// use tinystore::metrics::Op;
    /// let month = stats.query().name("cpu").since(Duration::from_hours(30 * 24));
    /// let plan = month.clone().every(Duration::from_hours(1)).explain(Op::Avg)?;
    /// assert!(plan.exceeds.is_none(), "whole hours answer from their summaries");
    /// let read = month.explain(None)?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn explain(&self, op: impl Into<Option<Op>>) -> Result<Plan> {
        self.checked_explain(op.into()).and_then(|explain| explain())
    }

    /// `explain` in its two steps, as [`Query::checked_read`] is `read`.
    pub(crate) fn checked_explain(&self, op: Option<Op>) -> Result<Checked<Plan>> {
        let shape = match op {
            Some(op) => self.shape(op).map(Some),
            None => self.plain("a read").map(|()| None),
        };
        let checked = shape.and_then(|shape| Ok((self.asked()?, shape)));
        self.once_checked(checked, |engine, (asked, shape)| engine.explain(&asked, shape.as_ref()))
    }

    /// What runs a query once `checked` passed, on the engine, its errors
    /// and those of the check named as the engine's.
    fn once_checked<C: Send + 'static, T: 'static>(
        &self,
        checked: Result<C>,
        run: impl FnOnce(&Engine, C) -> Result<T> + Send + 'static,
    ) -> Result<Checked<T>> {
        let checked = checked.map_err(|error| error.within("metrics"))?;
        let engine = Arc::clone(&self.engine);
        Ok(Box::new(move || run(&engine, checked).map_err(|error| error.within("metrics"))))
    }

    /// An aggregate's buckets and groups, checked.
    fn shape(&self, op: Op) -> Result<Shape> {
        let asking = Asking {
            every: self.every,
            calendar: self.calendar.as_ref().map(|(unit, zone)| (*unit, zone.as_str())),
            by: self.by.as_deref(),
            without: self.without.as_deref(),
            name: self.name.as_deref(),
            estimate: self.estimate,
        };
        Shape::of(op, asking)
    }

    /// Refuses what only an aggregate takes, for a call that is none.
    fn plain(&self, call: &str) -> Result<()> {
        let asks = self.every.is_some() || self.calendar.is_some() || self.by.is_some() || self.without.is_some();
        if asks || self.estimate {
            return Err(Error::invalid(format!(
                "`every`, `calendar`, `by`, `without` and `estimate` are an aggregate's: {call} takes none"
            )));
        }
        Ok(())
    }

    /// The query checked against what a read may ask, at the store's time.
    pub(super) fn asked(&self) -> Result<Asked> {
        let now = self.engine.now();
        let from = match (self.since, self.from) {
            (Some(_), Some(_)) => {
                return Err(Error::invalid("a query starts `since` before now or `from` a time, not both"));
            }
            (Some(span), None) => now.saturating_sub(crate::clock::millis(span)),
            (None, Some(from)) => unix_millis(from),
            (None, None) => i64::MIN,
        };
        // a range that starts after now and says no end is empty, not inverted
        let to = self.to.map_or(now.saturating_add(1).max(from), unix_millis);
        if to < from {
            return Err(Error::invalid("a query whose range ends before it starts"));
        }
        let mut conditions = Vec::with_capacity(self.conditions.len() + 1);
        if let Some(name) = &self.name {
            conditions.push((NAME.to_owned(), Condition::OneOf(vec![name.clone()])));
        }
        for (label, condition) in &self.conditions {
            conditions.push((label.clone(), check(label, condition)?));
        }
        if !conditions.iter().any(|(_, condition)| !matches!(condition, Condition::NoneOf(_))) {
            return Err(Error::invalid(
                "a query finds its series by a name, or a label's value, list or prefix: `Not` alone would read every \
                 series",
            ));
        }
        if conditions.len() > MOST_LABELS {
            return Err(Error::invalid(format!("a query of more than {MOST_LABELS} conditions")));
        }
        Ok(Asked { name: self.name.clone(), conditions, from, to, now })
    }
}

/// A condition as a query keeps it: its values sorted and each once; one on
/// a label of the store's own, of no values or of too many is refused.
fn check(label: &str, condition: &Condition) -> Result<Condition> {
    let condition_of = || format!("label {label:?}");
    if super::labels::is_own(label) {
        return Err(Error::invalid(format!("{}: a name beginning with __ is the store's own", condition_of())));
    }
    if label.is_empty() || label.len() > MOST_NAME_BYTES {
        return Err(Error::invalid(format!("{}: a name of 1 to {MOST_NAME_BYTES} bytes", condition_of())));
    }
    let checked = |values: &Vec<String>| -> Result<Vec<String>> {
        if values.is_empty() || values.len() > MOST_VALUES {
            return Err(Error::invalid(format!("{}: a condition lists 1 to {MOST_VALUES} values", condition_of())));
        }
        if values.iter().any(|value| value.len() > MOST_VALUE_BYTES) {
            return Err(Error::invalid(format!("{}: a value of {MOST_VALUE_BYTES} bytes at most", condition_of())));
        }
        let mut values = values.clone();
        values.sort_unstable();
        values.dedup();
        Ok(values)
    };
    Ok(match condition {
        Condition::OneOf(values) => Condition::OneOf(checked(values)?),
        Condition::NoneOf(values) => Condition::NoneOf(checked(values)?),
        Condition::Prefix(prefix) if prefix.is_empty() || prefix.len() > MOST_VALUE_BYTES => {
            return Err(Error::invalid(format!("{}: a prefix of 1 to {MOST_VALUE_BYTES} bytes", condition_of())));
        }
        Condition::Prefix(prefix) => Condition::Prefix(prefix.clone()),
    })
}

#[cfg(test)]
#[path = "query_tests.rs"]
mod tests;