tinystore 0.12.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
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
//! What a page takes of one snapshot: a read's series one after another, or
//! an aggregate's groups, each part of a series copied only once the page
//! reaches it, and where the page stopped.
//!
//! A read stops before the part that would pass its `limit` or a bound, and
//! passes over a part whose samples its window falls between. An
//! aggregate's page holds whole groups, chosen from every series the query
//! finds, as many series as a query finds at most, so that pages go past
//! that bound a group at a time. A group shares what the page has left among
//! its series, takes each as far as its share goes, and ends at the start of
//! the bucket the first of them stopped in, so that every bucket it answers
//! counts every series of the group; or, sooner, at the first bucket with
//! samples past the page's `limit`, which counts no bucket without any. A
//! group with no sample in the range answers nothing and is left out, and a
//! page whose groups all were goes on past them, as a walk of `next` would.
//!
//! ```text
//! a group's series   a ──────────╳            a stopped inside 10:05's bucket
//!                    b ─────────────────╳
//! buckets            │10:00│10:05│10:10│
//! the page           [10:00 ……)               10:00's bucket, whole; next from 10:05
//! ```

use std::collections::BTreeMap;

use rusqlite::{Connection, OptionalExtension, params};

use super::chosen::{Choice, Chosen};
use super::sampled::Sampled;
use super::{
    GROUPS, GROUPS_AND_BEFORE, HEAD, Holds, Part, Reading, State, Taken, TakenHead, Want, Window, from_the_chunk_before,
};
use crate::metrics::aggregate::{Op, Shape};
use crate::metrics::budget::Budget;
use crate::metrics::clock::{self, Times};
use crate::metrics::directory::{Group, Row};
use crate::metrics::engine::Engine;
use crate::metrics::head::{self, Chunk};
use crate::metrics::labels;
use crate::metrics::matching::{self, Found};
use crate::metrics::page::{GroupPlace, Paging, Place, Start};
use crate::metrics::query::Asked;
use crate::metrics::series::Labels;
use crate::sqlite::sql_error;
use crate::{Error, Result};

/// One group of an aggregate as a page took it: its series, each up to where
/// the page ends the group, and whether the page cut it there.
#[derive(Debug)]
pub(in crate::metrics) struct TakenGroup {
    pub(in crate::metrics) name: String,
    pub(in crate::metrics) labels: Labels,
    pub(in crate::metrics) members: Vec<Taken>,
    pub(in crate::metrics) more: bool,
}

/// What a page of an aggregate takes: its groups, and where the page after
/// it begins; or the name and labels of its first group alone, of more
/// series than a page holds, which an `op` that adds reads in parts.
#[derive(Debug)]
pub(in crate::metrics) enum PageTaken {
    Groups(Vec<TakenGroup>, Option<GroupPlace>),
    Wide((String, Labels)),
}

/// The stretch of a group of more series than a page holds that a part of
/// its series is taken over.
#[derive(Clone, Copy, Debug)]
pub(in crate::metrics) struct Wide<'a> {
    pub(in crate::metrics) group: &'a (String, Labels),
    /// Where the page begins in the group.
    pub(in crate::metrics) at: i64,
    pub(in crate::metrics) to: i64,
    /// The buckets with samples the page may hold, each series taken as far
    /// as its share of a page's bounds goes; none takes the stretch whole.
    pub(in crate::metrics) left: Option<usize>,
}

/// One part of such a group: its series among those found from an id on,
/// each up to `end`, the id the next part's series are found from, and the
/// first group after it met among them.
#[derive(Debug)]
pub(in crate::metrics) struct WidePart {
    pub(in crate::metrics) members: Vec<Taken>,
    pub(in crate::metrics) end: i64,
    pub(in crate::metrics) beyond: Option<i64>,
    pub(in crate::metrics) after: Option<(String, Labels)>,
}

impl Holds for (Vec<Taken>, Option<Place>) {
    fn taken(&mut self) -> Vec<&mut Taken> {
        self.0.iter_mut().collect()
    }
}

impl Holds for PageTaken {
    fn taken(&mut self) -> Vec<&mut Taken> {
        match self {
            PageTaken::Groups(groups, _) => groups.iter_mut().flat_map(|group| group.members.iter_mut()).collect(),
            PageTaken::Wide(_) => Vec::new(),
        }
    }
}

impl Holds for WidePart {
    fn taken(&mut self) -> Vec<&mut Taken> {
        self.members.iter_mut().collect()
    }
}

/// How far a page lets a series go: a part is taken while what the page
/// decoded is under `decoded` and what it copies stays within `bytes`, and
/// always within the query's bounds; on an aggregate's page, while it starts
/// before the end the buckets `sampled` give the page.
#[derive(Debug)]
struct Room<'p, 's> {
    decoded: usize,
    bytes: usize,
    sampled: Option<&'p mut Sampled<'s>>,
    /// The series' first part on the page is still to come. Its times are
    /// read even when it is dense, so that the first bucket the page holds
    /// has samples and no page comes back empty.
    first: bool,
}

impl Room<'_, '_> {
    /// Where a series' window ends on the page: at `to`, or before where the
    /// buckets met so far end the page.
    fn end(&self, to: i64) -> i64 {
        self.sampled.as_ref().map_or(to, |sampled| sampled.end().min(to))
    }
}

/// One group of an aggregate's page: its series, where they end, and how
/// many buckets with samples it holds, which the page's `limit` counts.
struct Ended {
    members: Vec<Taken>,
    end: i64,
    buckets: usize,
}

/// What a page took of a group: its buckets up to where they end, or none,
/// since it stopped at the start of the bucket it could not take.
enum Paged {
    Ended(Ended),
    Stopped(i64),
}

impl Engine {
    /// What a page of a read takes, as [`Engine::take`] takes a read's, and
    /// where it stopped: the place the next page begins, none when it took
    /// the rest of the range.
    pub(in crate::metrics) fn take_page(
        &self,
        asked: &Asked,
        paging: &Paging,
        budget: &mut Budget,
    ) -> Result<(Vec<Taken>, Option<Place>)> {
        let from = paging.series_from();
        self.snapshot(asked, Want::Samples, budget, |reading, asked| {
            let (found, beyond) = matching::find_from(reading.connection, asked, from.series, reading.budget)?;
            reading.page(self, found, asked, paging.limit, from).map(|(taken, stopped)| {
                let past = beyond.map(|series| Place { series, at: i64::MIN });
                (taken, stopped.or(past))
            })
        })
    }

    /// What a page of an aggregate takes: its groups in their order from
    /// where the page begins, and where the page after it begins. When the
    /// groups one query finds hold no sample in the range, the page goes on
    /// past them from its `next`, from a snapshot of its own each time, so
    /// that only the last page takes no group.
    pub(in crate::metrics) fn take_groups(
        &self,
        asked: &Asked,
        shape: &Shape,
        paging: &Paging,
        budget: &mut Budget,
    ) -> Result<PageTaken> {
        let mut paging = paging.clone();
        loop {
            match self.groups_once(asked, shape, &paging, budget)? {
                PageTaken::Groups(page, Some(next)) if page.is_empty() => {
                    crate::cancel::check(|| "an aggregate".to_owned())?;
                    // the page holds nothing of the groups it passed over
                    budget.reset();
                    paging.from = Start::Group(next);
                }
                taken => return Ok(taken),
            }
        }
    }

    /// One part of a group of more series than a page holds: the series of
    /// the group among as many as a query finds from the id `from` on, from
    /// a snapshot of its own, so that no read lasts longer than a page's.
    pub(in crate::metrics) fn take_wide(
        &self,
        asked: &Asked,
        shape: &Shape,
        wide: Wide<'_>,
        from: i64,
        budget: &mut Budget,
    ) -> Result<WidePart> {
        self.snapshot(asked, Want::Buckets(shape), budget, |reading, asked| {
            let (found, beyond) = matching::find_from(reading.connection, asked, from, reading.budget)?;
            let (mut members, mut after) = (Vec::new(), None);
            for series in found {
                shape.check(&series)?;
                let group = shape.group_of(&series).map_err(|error| error.within(&series.kept))?;
                if group == *wide.group {
                    members.push(series);
                } else if group > *wide.group && after.as_ref().is_none_or(|after| group < *after) {
                    after = Some(group);
                }
            }
            let asked = Asked { to: wide.to, ..asked.clone() };
            let (members, end) = reading.wide_members(self, &members, &asked, shape, wide)?;
            Ok(WidePart { members, end, beyond, after })
        })
    }

    /// The groups one snapshot gives a page of an aggregate, and where the
    /// page after it begins.
    fn groups_once(&self, asked: &Asked, shape: &Shape, paging: &Paging, budget: &mut Budget) -> Result<PageTaken> {
        self.snapshot(asked, Want::Buckets(shape), budget, |reading, asked| {
            let most = reading.budget.series_limit();
            let (groups, beyond) = match chosen(reading.connection, asked, shape, paging, most)? {
                Choice::Groups(groups, beyond) => (groups, beyond),
                Choice::TooMany(group, _) if shape.adds() => return Ok(PageTaken::Wide(group)),
                Choice::TooMany(group, series) => return Err(reading.budget.too_many(series).within(named(&group))),
            };
            reading.budget.take_group(groups.values().map(Vec::len).sum())?;
            let beyond = beyond.map(|group| GroupPlace { group, at: i64::MIN });
            let (page, next) = reading.groups_page(self, &groups, asked, shape, paging, beyond)?;
            Ok(PageTaken::Groups(page, next))
        })
    }
}

/// The groups of an aggregate's page, from every series its query finds:
/// each is checked against the `op`, and those of groups before the one
/// where the page begins are left out.
fn chosen(
    connection: &Connection,
    asked: &Asked,
    shape: &Shape,
    paging: &Paging,
    most: usize,
) -> Result<Choice<(String, Labels), Found>> {
    let begins = paging.group_from().map(|place| &place.group);
    let mut chosen = Chosen::new(most);
    matching::find_every(connection, asked, |found| {
        for series in found {
            shape.check(&series)?;
            let group = shape.group_of(&series).map_err(|error| error.within(&series.kept))?;
            if begins.is_none_or(|begins| group >= *begins) {
                chosen.add(group, series);
            }
        }
        Ok(())
    })?;
    Ok(chosen.choice())
}

/// A group as Prometheus prints a series: `rpc_ms{fn="lookup"}`.
fn named((name, labels): &(String, Labels)) -> String {
    let mut named = String::new();
    labels::format(&mut named, name, labels.iter()).ok();
    named
}

impl Reading<'_, '_> {
    /// The series of a read's page in the order of their ids, each taken as
    /// far as the page has room; and where it stopped, before the part that
    /// would pass `limit` or a bound of the query.
    fn page(
        &mut self,
        engine: &Engine,
        found: Vec<Found>,
        asked: &Asked,
        limit: usize,
        from: Place,
    ) -> Result<(Vec<Taken>, Option<Place>)> {
        let mut room = Room { decoded: limit, bytes: usize::MAX, sampled: None, first: false };
        let mut taken = Vec::with_capacity(found.len());
        for found in found {
            let at = if found.id == from.series { from.at } else { i64::MIN };
            let mut series = Taken::of(engine, found, asked, at);
            let stopped = self.fill_lazily(&mut series, asked.now, Want::Samples, &mut room);
            let stopped = stopped.map_err(|error| self.met(&series.found, error))?;
            let id = series.found.id;
            taken.push(series);
            if let Some(at) = stopped {
                return Ok((taken, Some(Place { series: id, at })));
            }
        }
        Ok((taken, None))
    }

    /// The groups chosen for an aggregate's page, from the one where it
    /// begins: each whole while the page has room for it, and the one it
    /// stops in up to the start of a bucket; past them the page ends at
    /// `beyond`, the first group it could not hold. A group with no sample
    /// in the range is left out, since it answers no bucket.
    fn groups_page(
        &mut self,
        engine: &Engine,
        groups: &BTreeMap<(String, Labels), Vec<Found>>,
        asked: &Asked,
        shape: &Shape,
        paging: &Paging,
        beyond: Option<GroupPlace>,
    ) -> Result<(Vec<TakenGroup>, Option<GroupPlace>)> {
        let begins = paging.group_from();
        let (mut page, mut left) = (Vec::new(), paging.limit);
        for (group, found) in groups {
            let at = begins.filter(|place| place.group == *group).map_or(i64::MIN, |place| place.at);
            let place = |at| Some(GroupPlace { group: group.clone(), at });
            if left == 0 {
                return Ok((page, place(at)));
            }
            let ended = match self.group_page(engine, found, asked, shape, (at, left))? {
                Paged::Ended(ended) => ended,
                Paged::Stopped(_) if !page.is_empty() => return Ok((page, place(at))),
                Paged::Stopped(_) => self.one_bucket(engine, found, asked, shape, at)?,
            };
            if ended.buckets == 0 {
                // it ended with the range, and met no sample on the way
                continue;
            }
            left = left.saturating_sub(ended.buckets);
            let (name, labels) = group.clone();
            let more = ended.end < asked.to;
            page.push(TakenGroup { name, labels, members: ended.members, more });
            if more {
                return Ok((page, place(ended.end)));
            }
        }
        Ok((page, beyond))
    }

    /// One group of an aggregate's page from `at`, as many as `left` buckets
    /// with samples: its series each taken as far as its share of what the
    /// page has left, ending at the first bucket with samples past `left`, at
    /// the range's end, or at the start of the bucket the first of them
    /// stopped in; stopped there when that leaves no bucket with samples.
    fn group_page(
        &mut self,
        engine: &Engine,
        found: &[Found],
        asked: &Asked,
        shape: &Shape,
        (at, left): (i64, usize),
    ) -> Result<Paged> {
        let mut members = self.members(engine, found, asked, shape, at);
        let from = members.iter().map(|series| series.window.from).min().unwrap_or(asked.to);
        if from >= asked.to {
            // nothing of the group is left in the range
            return Ok(Paged::Ended(Ended { members: ended_at(members, asked.to), end: asked.to, buckets: 0 }));
        }
        let count = members.len();
        let share = (self.budget.decoded_left() / count, self.budget.bytes_left() / count);
        let mut sampled = Sampled::new(shape, from, left, asked.to);
        let mut stopped_at = asked.to;
        for series in &mut members {
            series.window.to = sampled.end().min(stopped_at);
            let (decoded, bytes) = (self.budget.decoded + share.0, self.budget.bytes + share.1);
            let mut room = Room { decoded, bytes, sampled: Some(&mut sampled), first: true };
            let stopped = self.fill_lazily(series, asked.now, Want::Buckets(shape), &mut room);
            if let Some(stopped) = stopped.map_err(|error| self.met(&series.found, error))? {
                stopped_at = stopped_at.min(shape.bucket_of(stopped, from));
            }
        }
        let end = sampled.end().min(stopped_at);
        let buckets = sampled.before(end);
        if buckets == 0 && end < asked.to {
            return Ok(Paged::Stopped(end));
        }
        Ok(Paged::Ended(Ended { members: ended_at(members, end), end, buckets }))
    }

    /// The series of one part of a group of more series than a page holds,
    /// each up to where the part ends: as a page takes a group, by its share
    /// of the page's bounds, the part ending with none where they held no
    /// bucket with samples; or whole, where a series past a bound is `limit`.
    fn wide_members(
        &mut self,
        engine: &Engine,
        found: &[Found],
        asked: &Asked,
        shape: &Shape,
        wide: Wide<'_>,
    ) -> Result<(Vec<Taken>, i64)> {
        let Some(left) = wide.left else {
            let mut members = self.members(engine, found, asked, shape, wide.at);
            for series in &mut members {
                let filled = self.fill(series, asked.now, Want::Buckets(shape));
                filled.map_err(|error| self.met(&series.found, error))?;
            }
            return Ok((ended_at(members, asked.to), asked.to));
        };
        Ok(match self.group_page(engine, found, asked, shape, (wide.at, left))? {
            Paged::Ended(ended) => (ended.members, ended.end),
            Paged::Stopped(end) => (Vec::new(), end),
        })
    }

    /// A group's series, in the order its buckets join them, each over the
    /// query's window from `at` on.
    fn members(&self, engine: &Engine, found: &[Found], asked: &Asked, shape: &Shape, at: i64) -> Vec<Taken> {
        let mut found = found.to_vec();
        found.sort_by_cached_key(|series| shape.order_of(series));
        found.into_iter().map(|series| Taken::of(engine, series, asked, at)).collect()
    }

    /// The first bucket with samples of a group alone on a page, taken with
    /// the query's whole bounds; when even they stop before it, it is taken
    /// whole, which passes one and is `limit`, since a bucket is never split.
    fn one_bucket(&mut self, engine: &Engine, found: &[Found], asked: &Asked, shape: &Shape, at: i64) -> Result<Ended> {
        self.budget.reset();
        let stopped = match self.group_page(engine, found, asked, shape, (at, 1))? {
            Paged::Ended(ended) => return Ok(ended),
            Paged::Stopped(stopped) => stopped,
        };
        self.budget.reset();
        let end = shape.after_buckets(stopped, 1, asked.to);
        let mut members = self.members(engine, found, asked, shape, at);
        for series in &mut members {
            series.window.to = end;
            let filled = self.fill(series, asked.now, Want::Buckets(shape));
            filled.map_err(|error| self.met(&series.found, error))?;
        }
        Ok(Ended { members: ended_at(members, end), end, buckets: 1 })
    }

    /// A series' blocks and head a page takes for `want`, and the time it
    /// stopped at when it could not take them all.
    fn fill_lazily(&mut self, taken: &mut Taken, now: i64, want: Want<'_>, room: &mut Room) -> Result<Option<i64>> {
        let (id, window) = (taken.found.id, taken.window);
        let state = self.state(id, window, now, -1)?;
        taken.trimmed = state.trimmed;
        // every block is older than the head, so a head that ends before the
        // window leaves it nothing
        if state.extent.count > 0 && state.extent.end < window.from {
            return Ok(None);
        }
        let (shape, before) = match want {
            Want::Buckets(shape) => {
                // a head that starts before the range holds the sample a first step begins at
                let in_head = state.extent.count > 0 && state.extent.start < window.from;
                let op = shape.series_op(&taken.found);
                (Some((shape, op)), Shape::steps(op) && window.from > window.cutoff && !in_head)
            }
            Want::Samples | Want::Newest => (None, false),
        };
        let stopped = match self.blocks_lazily(taken, shape, before, room)? {
            Some(at) => Some(at),
            None => self.head_lazily(taken, &state, want, room)?,
        };
        taken.until = stopped.unwrap_or(window.to);
        Ok(stopped)
    }

    /// Whether a page lets a series take a part of these costs.
    fn admits(&self, room: &Room, bytes: usize, decoded: usize) -> bool {
        let within = self.budget.decoded < room.decoded && self.budget.bytes.saturating_add(bytes) <= room.bytes;
        within && self.budget.fits(bytes, decoded)
    }

    /// Whether a page lets a series take a group: its directory, and its
    /// clock, read here when it fits what the series has left and paid as
    /// long as it is, so that one that does not fit ends the page and never
    /// fails it. A clock the snapshot holds decoded costs nothing more. Paid
    /// at a clock's most, 64 KiB, no series of a group past 256 had a share
    /// that held one, and each of its pages was one bucket.
    fn admits_group(&mut self, room: &Room, directory: usize, clock: i64) -> Result<bool> {
        if !self.admits(room, directory, 0) {
            return Ok(false);
        }
        if self.clocks.iter().any(|(kept, _)| *kept == clock) {
            return Ok(true);
        }
        let spent = self.budget.bytes.saturating_add(directory);
        let left = room.bytes.saturating_sub(spent).min(self.budget.bytes_left().saturating_sub(directory));
        let (length, body) = clock::load(self.connection, clock, left)?;
        let Some(body) = body else { return Ok(false) };
        self.keep_clock(clock, length, &body).map(|_| true)
    }

    /// The live blocks of a series in its window, as [`Reading::blocks`]
    /// takes them, a group's directory and clock read only once the page
    /// reaches the group; and the time of the first block it could not take.
    fn blocks_lazily(
        &mut self,
        taken: &mut Taken,
        shape: Option<(&Shape, Op)>,
        before: bool,
        room: &mut Room,
    ) -> Result<Option<i64>> {
        let (id, window) = (taken.found.id, taken.window);
        let connection = self.connection;
        let failed = |error| sql_error("read a series' groups", error);
        let mut select = connection.prepare_cached(if before { GROUPS_AND_BEFORE } else { GROUPS }).map_err(failed)?;
        let room_left = self.budget.bytes_left() as i64;
        let mut rows = select.query(params![id, window.from, window.to, window.cutoff, room_left]).map_err(failed)?;
        while let Some(row) = rows.next().map_err(failed)? {
            let start: i64 = row.get(0).map_err(failed)?;
            if start >= room.end(window.to) {
                break;
            }
            let length = usize::try_from(row.get::<_, i64>(3).map_err(failed)?).unwrap_or(usize::MAX);
            if !self.admits_group(room, length, row.get(2).map_err(failed)?)? {
                return Ok(Some(start.max(window.from)));
            }
            self.budget.take_bytes(length)?;
            let directory: Option<Vec<u8>> = row.get(4).map_err(failed)?;
            let directory = directory.ok_or_else(|| Error::corrupt("a group's directory past its length"))?;
            let (end, clock) = (row.get(1).map_err(failed)?, row.get(2).map_err(failed)?);
            let row = Row { series: id, start, end, clock, directory };
            if let Some(at) = self.group_lazily(taken, &row, shape, before, room)? {
                return Ok(Some(at));
            }
        }
        Ok(None)
    }

    /// The live blocks of one group a page takes, the newest before the
    /// window first when `before` asks for it; and the start of the first it
    /// could not take.
    fn group_lazily(
        &mut self,
        taken: &mut Taken,
        row: &Row,
        shape: Option<(&Shape, Op)>,
        before: bool,
        room: &mut Room,
    ) -> Result<Option<i64>> {
        let window = taken.window;
        let clock = self.clock(row.clock)?;
        let group = Group::decode(self.zstd, row, clock)?;
        for (slot, block) in group.blocks.into_iter().enumerate() {
            if group.live >> slot & 1 == 0 || block.times.start >= room.end(window.to) {
                continue;
            }
            if block.times.end < window.from {
                if before && block.times.end >= window.cutoff {
                    carry(taken, Part { block, whole: true });
                }
                continue;
            }
            // a read takes no block that holds none of its window's samples
            if shape.is_none() && falls_between(&block.times, window)? {
                continue;
            }
            let whole = shape.is_some_and(|(shape, op)| shape.holds(op, &block, window));
            let bytes = if !whole && block.external() { block.body_len } else { 0 };
            let decoded = if whole { 0 } else { block.times.count };
            if !self.admits(room, bytes, decoded) {
                return Ok(Some(block.times.start.max(window.from)));
            }
            if let Some(sampled) = room.sampled.as_deref_mut() {
                let (from, end, count) = (block.times.start.max(window.from), block.times.end, block.times.count);
                // a block answered whole lies in one bucket of the window
                if whole {
                    sampled.meet(block.times.start);
                } else if room.first || !sampled.meet_run(from, end, count) {
                    // a sample past the window may lie in the bucket the range's end cuts
                    block.times.walk(|_, at| {
                        if (window.from..window.to).contains(&at) {
                            sampled.meet(at)
                        }
                    })?;
                }
                room.first = false;
            }
            taken.blocks.push(self.part(block, whole)?);
        }
        Ok(None)
    }

    /// A series' head a page takes for `want`, chunk by chunk, and the start
    /// of the first chunk it could not take. Its bytes are copied whole, once
    /// the page has room for them.
    fn head_lazily(
        &mut self,
        taken: &mut Taken,
        state: &State,
        want: Want<'_>,
        room: &mut Room,
    ) -> Result<Option<i64>> {
        let (id, extent, window) = (taken.found.id, state.extent, taken.window);
        // a head past where the page's buckets end it is not copied at all
        if extent.count == 0 || extent.end < window.from || extent.start >= room.end(window.to) {
            return Ok(None);
        }
        if !self.admits(room, state.head_len, 0) {
            return Ok(Some(extent.start.max(window.from)));
        }
        self.budget.take_bytes(state.head_len)?;
        let bytes = self.head_bytes(id, state.head_len)?;
        let chunks = head::parse(id, extent, &bytes)?;
        let from = match want {
            Want::Buckets(shape) if Shape::steps(shape.series_op(&taken.found)) => {
                from_the_chunk_before(&chunks, window)
            }
            Want::Samples | Want::Buckets(_) | Want::Newest => window.from,
        };
        let mut stopped = None;
        for chunk in chunks.iter().filter(|chunk| chunk.overlaps(from, window.to)) {
            if chunk.head.start >= room.end(window.to) {
                break;
            }
            if matches!(want, Want::Samples) && self.chunk_falls_between(chunk, window)? {
                // the one chunk the window touches: the head holds none of a read's samples
                return Ok(None);
            }
            if !self.admits(room, 0, chunk.head.count) {
                stopped = Some(chunk.head.start.max(window.from));
                break;
            }
            self.budget.take_decoded(chunk.head.count)?;
            let in_window = chunk.head.end >= window.from;
            if let Some(sampled) = room.sampled.as_deref_mut().filter(|_| in_window) {
                let (from, end, count) = (chunk.head.start.max(window.from), chunk.head.end, chunk.head.count);
                if room.first || !sampled.meet_run(from, end, count) {
                    // a chunk's times alone, its values decoded after the read
                    let mut times = Vec::with_capacity(count);
                    chunk.times(self.zstd, &mut times)?;
                    let within = times.iter().filter(|point| (window.from..window.to).contains(&point.at));
                    within.for_each(|point| sampled.meet(point.at));
                }
                room.first = false;
            }
        }
        drop(chunks);
        taken.head = Some(TakenHead { extent, bytes, from });
        Ok(stopped)
    }

    /// Whether a read's window falls between two samples of a head's chunk
    /// that reaches across it, as [`falls_between`] asks of a block.
    fn chunk_falls_between(&mut self, chunk: &Chunk<'_>, window: Window) -> Result<bool> {
        if chunk.head.start >= window.from || chunk.head.end < window.to {
            return Ok(false);
        }
        let mut times = Vec::with_capacity(chunk.head.count);
        chunk.times(self.zstd, &mut times)?;
        Ok(!times.iter().any(|point| (window.from..window.to).contains(&point.at)))
    }

    /// A head's bytes, read apart from its state's row for a page, which
    /// copies them only once it reaches them.
    fn head_bytes(&self, id: i64, length: usize) -> Result<Vec<u8>> {
        let asked = params![id, i64::try_from(length).unwrap_or(i64::MAX)];
        let body: Option<Vec<u8>> = self
            .connection
            .prepare_cached(HEAD)
            .and_then(|mut select| select.query_row(asked, |row| row.get(0)).optional())
            .map_err(|error| sql_error("read a series' head", error))?;
        body.filter(|body| body.len() == length).ok_or_else(|| Error::corrupt("a head its state says it has"))
    }
}

/// Whether a read's window falls between two samples of a block it touches
/// that reaches across it, from before it to its end or past, so that the
/// block holds none of the window's samples. One that starts or ends inside
/// the window holds a sample there, its first or its last.
fn falls_between(times: &Times, window: Window) -> Result<bool> {
    if times.start >= window.from || times.end < window.to {
        return Ok(false);
    }
    let mut inside = false;
    times.walk(|_, at| inside |= (window.from..window.to).contains(&at))?;
    Ok(!inside)
}

/// Keeps the newest block before a series' window first among its parts,
/// in place of an older one: only its last sample counts, where the first
/// step of the window begins.
fn carry(taken: &mut Taken, part: Part) {
    match taken.blocks.last_mut() {
        Some(last) if last.block.times.end < taken.window.from => *last = part,
        _ => taken.blocks.push(part),
    }
}

/// A group's series ended at `end`, the start of a bucket: what each took
/// from there on is the next page's.
fn ended_at(mut members: Vec<Taken>, end: i64) -> Vec<Taken> {
    for series in &mut members {
        let from = series.window.from;
        series.blocks.retain(|part| part.block.times.start < end || part.block.times.end < from);
        (series.window.to, series.until) = (end, end);
    }
    members
}