tinystore 0.8.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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
//! An index's settings: its fields, each with its weight and a number of
//! its own for good, its filters, numbered the same way, whether a misspelt
//! word is read as the nearest, the keyboard layouts its readers type with,
//! its vectors' dimensions, and how a search by words and a vector at once
//! combines them, and for a table's index the table it follows. They are
//! kept as JSON in the index's row, and an open that asks for others
//! changes them. An index without layouts keeps no `layouts`, and a
//! release before them reads past the key.
//!
//! ```text
//! {"fields":[{"path":"title","weight":2.0,"number":0}],"next_field":1,"typo_tolerance":true,"analysis":1,
//!  "filters":[{"path":"category","number":0}],"next_filter":1,"layouts":["ru","en"],
//!  "vectors":{"dimensions":1024,"recall":null},"hybrid":{"vector_weight":0.5,"rerank":false},
//!  "table":{"name":"articles","key":"id","vector":"embedding"}}
//! ```

use serde_json::{Value, json};

use super::analyzer;
use super::layouts::{self, Layout};
use super::vectors::MOST_DIMENSIONS;
use crate::limits::VECTOR_DIMENSIONS;
use crate::{Error, Result};

/// A field: where its text lies in a document, what a word found there
/// weighs, and its number, which names its parts and is never given again.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct Field {
    pub(crate) path: String,
    pub(crate) weight: f64,
    pub(crate) number: u32,
}

/// A filter: where its values lie in a document, and its number, which
/// names its part and is never given again.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct Filter {
    pub(crate) path: String,
    pub(crate) number: u32,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct Settings {
    pub(crate) fields: Vec<Field>,
    pub(crate) next_field: u32,
    pub(crate) filters: Vec<Filter>,
    pub(crate) next_filter: u32,
    pub(crate) typo_tolerance: bool,
    pub(crate) layouts: Vec<Layout>,
    pub(crate) analysis: u32,
    pub(crate) vectors: Option<Vectors>,
    pub(crate) hybrid: Hybrid,
    pub(crate) table: Option<Table>,
}

/// The table of a database that a table's index follows: its rows are the
/// index's documents by `key`, a column of unique values, and their vectors
/// are the column `vector`'s, when it keeps them.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Table {
    pub(crate) name: String,
    pub(crate) key: String,
    pub(crate) vector: Option<String>,
}

impl Table {
    fn to_json(&self) -> Value {
        json!({ "name": self.name, "key": self.key, "vector": self.vector })
    }

    /// A table as its settings keep it, none for an index of documents;
    /// none outer when it does not read.
    fn from_json(table: &Value) -> Option<Option<Table>> {
        if table.is_null() {
            return Some(None);
        }
        let name = table["name"].as_str()?.to_owned();
        let key = table["key"].as_str()?.to_owned();
        Some(Some(Table { name, key, vector: table["vector"].as_str().map(str::to_owned) }))
    }
}

/// The vectors an index keeps: of how many dimensions each, and the share
/// of the exact ten a search by one finds from clusters, when it is not
/// exact.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct Vectors {
    pub(crate) dimensions: usize,
    pub(crate) recall: Option<f64>,
}

/// How a search by words and a vector at once combines them, unless it says:
/// their scores summed at `vector_weight`, or the words' hits ordered by the
/// vector.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct Hybrid {
    pub(crate) vector_weight: f64,
    pub(crate) rerank: bool,
}

/// What an open asks for: the fields by their paths and weights, none for a
/// document that is its own text.
#[derive(Clone, Debug)]
pub(crate) struct Wanted {
    pub(crate) fields: Vec<(String, f64)>,
    pub(crate) filters: Vec<String>,
    pub(crate) typo_tolerance: bool,
    /// The layouts' names, as the open gave them.
    pub(crate) layouts: Vec<String>,
    pub(crate) dimensions: Option<usize>,
    pub(crate) recall: Option<f64>,
    pub(crate) vector_weight: Option<f64>,
    pub(crate) rerank: bool,
    /// Whether a change that needs the index read again waits for `reindex`.
    pub(crate) manual: bool,
    pub(crate) table: Option<Table>,
}

impl Default for Wanted {
    /// An index of a document that is its own text, typos forgiven.
    fn default() -> Self {
        Wanted {
            fields: Vec::new(),
            filters: Vec::new(),
            typo_tolerance: true,
            layouts: Vec::new(),
            dimensions: None,
            recall: None,
            vector_weight: None,
            rerank: false,
            manual: false,
            table: None,
        }
    }
}

/// What a change of settings makes of an index.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct Changed {
    /// The settings that change at once.
    pub(crate) settings: Settings,
    pub(crate) said: Vec<String>,
    pub(crate) rebuild: Option<Asked>,
}

/// A rebuild a change asks for: why, as the log says it, and whether the
/// index needs it to serve what was asked, or only to drop what it keeps
/// no longer, which an index rebuilt by hand leaves for its next rebuild.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct Asked {
    pub(crate) why: String,
    pub(crate) needed: bool,
}

/// The rebuild a change gathers, reason by reason.
#[derive(Default)]
struct Rebuild {
    needed: Vec<String>,
    cleaning: Vec<String>,
}

impl Rebuild {
    fn needs(&mut self, why: String) {
        self.needed.push(why);
    }

    fn cleans(&mut self, why: String, documents: u64) {
        if documents > 0 {
            self.cleaning.push(why);
        }
    }

    fn asked(self) -> Option<Asked> {
        let needed = !self.needed.is_empty();
        let why: Vec<String> = self.needed.into_iter().chain(self.cleaning).collect();
        (!why.is_empty()).then(|| Asked { why: why.join(", "), needed })
    }
}

/// The weight of a vector a search by both gives unless its index or it
/// says: search-hybrid found no weight that won both its collections.
pub(crate) const VECTOR_WEIGHT: f64 = 0.5;

/// The heaviest weight a field may have: a thousand to one is far past what
/// orders hits, and a score of a search of 32 words stays within its points.
const MOST_WEIGHT: f64 = 1000.0;

impl Wanted {
    /// The fields asked for, the document itself when none was.
    fn fields(&self) -> Vec<(String, f64)> {
        if self.fields.is_empty() { vec![(String::new(), 1.0)] } else { self.fields.clone() }
    }

    pub(crate) fn check(&self) -> Result<()> {
        for (at, (path, weight)) in self.fields.iter().enumerate() {
            check_path(path)?;
            if !(weight.is_finite() && *weight > 0.0 && *weight <= MOST_WEIGHT) {
                return Err(Error::invalid(format!(
                    "field {path} weighs {weight}: a weight is above 0, at most 1,000"
                )));
            }
            if self.fields[..at].iter().any(|(earlier, _)| earlier == path) {
                return Err(Error::invalid(format!("field {path} is given twice")));
            }
        }
        for (at, path) in self.filters.iter().enumerate() {
            check_path(path)?;
            if self.filters[..at].contains(path) {
                return Err(Error::invalid(format!("filter {path} is given twice")));
            }
        }
        layouts::layouts(&self.layouts)?;
        self.check_table()?;
        self.check_vectors()
    }

    /// The layouts asked for, those of their names a release knows.
    fn layouts(&self) -> Vec<Layout> {
        self.layouts.iter().filter_map(|name| Layout::named(name).ok()).collect()
    }

    /// Refuses a table's index that reads no column as its text, a path
    /// into a column's value, and vectors from no column.
    fn check_table(&self) -> Result<()> {
        let Some(table) = &self.table else { return Ok(()) };
        if self.fields.is_empty() {
            return Err(Error::invalid("a table's index reads at least one column as text: field(column, weight)"));
        }
        let paths = self.fields.iter().map(|(path, _)| path).chain(&self.filters);
        if let Some(path) = paths.into_iter().find(|path| path.contains('.')) {
            return Err(Error::invalid(format!("a table's index reads columns, and {path:?} names none")));
        }
        if self.dimensions.is_some() != table.vector.is_some() {
            return Err(Error::invalid("a table's index reads its vectors from a column: vectors(column, dimensions)"));
        }
        Ok(())
    }

    fn check_vectors(&self) -> Result<()> {
        match self.dimensions {
            Some(0) => return Err(Error::invalid("vectors of 0 dimensions")),
            Some(dimensions) if dimensions > MOST_DIMENSIONS => {
                let refused = format!("vectors of {dimensions} dimensions, past {MOST_DIMENSIONS}");
                return Err(VECTOR_DIMENSIONS.refuse(dimensions as u64, refused));
            }
            None if self.vector_weight.is_some() || self.rerank || self.recall.is_some() => {
                return Err(Error::invalid("a vector weight, a rerank or a recall for an index without vectors"));
            }
            _ => {}
        }
        if let Some(recall) = self.recall.filter(|recall| !(recall > &0.0 && recall < &1.0)) {
            return Err(Error::invalid(format!(
                "a recall of {recall}: it is above 0 and below 1, the exact search being the default"
            )));
        }
        check_hybrid(self.vector_weight, self.rerank)
    }
}

/// Refuses a path that names no field, or fields joined by dots.
pub(crate) fn check_path(path: &str) -> Result<()> {
    if path.is_empty() || path.split('.').any(str::is_empty) {
        return Err(Error::invalid(format!("a path names a field, or fields joined by dots: {path:?}")));
    }
    Ok(())
}

/// Refuses a vector weight outside 0–1, and one beside a rerank, which
/// orders by the vector alone.
pub(crate) fn check_hybrid(vector_weight: Option<f64>, rerank: bool) -> Result<()> {
    match vector_weight {
        Some(weight) if !(0.0..=1.0).contains(&weight) => {
            Err(Error::invalid(format!("a vector weight of {weight}: it is from 0, the words alone, to 1")))
        }
        Some(_) if rerank => Err(Error::invalid("a vector weight beside a rerank, which orders by the vector alone")),
        _ => Ok(()),
    }
}

impl Settings {
    pub(crate) fn new(wanted: &Wanted) -> Settings {
        let fields: Vec<Field> =
            (0..).zip(wanted.fields()).map(|(number, (path, weight))| Field { path, weight, number }).collect();
        let next_field = fields.len() as u32;
        let filters: Vec<Filter> =
            (0..).zip(&wanted.filters).map(|(number, path)| Filter { path: path.clone(), number }).collect();
        Settings {
            fields,
            next_field,
            next_filter: filters.len() as u32,
            filters,
            typo_tolerance: wanted.typo_tolerance,
            layouts: wanted.layouts(),
            analysis: analyzer::VERSION,
            vectors: wanted.dimensions.map(|dimensions| Vectors { dimensions, recall: wanted.recall }),
            hybrid: Hybrid::of(wanted),
            table: wanted.table.clone(),
        }
    }

    /// What `wanted` changes of these: the settings that change at once, each
    /// change as the log says it, and what needs the index read again beside
    /// it. A weight, a field or a filter removed, typo tolerance, layouts,
    /// vectors added, recall and how a search by both combines them change
    /// at once,
    /// a field or a filter removed leaving its keys for a rebuild to drop; a
    /// field or a filter added to an index that holds documents needs them
    /// read again, as do words read by another release. Other vectors than
    /// those kept are refused: the store has nothing to make them from, but
    /// a table's index reads them again from their column. A table's index
    /// follows the table and key it was made for.
    pub(crate) fn change(&self, wanted: &Wanted, documents: u64) -> Result<Changed> {
        self.check_same_table(wanted)?;
        let mut changed = Settings { fields: Vec::new(), ..self.clone() };
        let mut said = Vec::new();
        let mut rebuild = Rebuild::default();
        changed.change_filters(wanted, documents, (&mut said, &mut rebuild));
        changed.change_vectors(wanted, documents, (&mut said, &mut rebuild))?;
        for (path, weight) in wanted.fields() {
            let Some(field) = self.fields.iter().find(|field| field.path == path) else {
                if documents > 0 {
                    rebuild.needs(format!("{} added", shown(&path)));
                    continue;
                }
                said.push(format!("{} added", shown(&path)));
                changed.fields.push(Field { path, weight, number: changed.next_field });
                changed.next_field += 1;
                continue;
            };
            if field.weight != weight {
                said.push(format!("{} weighs {weight}, was {}; at once", shown(&path), field.weight));
            }
            changed.fields.push(Field { weight, ..field.clone() });
        }
        for field in &self.fields {
            if !changed.fields.iter().any(|kept| kept.number == field.number) {
                said.push(format!("{} removed; at once from searches", shown(&field.path)));
                rebuild.cleans(format!("{} removed", shown(&field.path)), documents);
            }
        }
        if wanted.typo_tolerance != self.typo_tolerance {
            let now = if wanted.typo_tolerance { "on" } else { "off" };
            said.push(format!("typo tolerance {now}; at once"));
            changed.typo_tolerance = wanted.typo_tolerance;
        }
        let layouts = wanted.layouts();
        if layouts != self.layouts {
            said.push(match names(&layouts) {
                names if names.is_empty() => "layouts off; at once".to_owned(),
                names => format!("layouts {}; at once", names.join(", ")),
            });
            changed.layouts = layouts;
        }
        if self.analysis != analyzer::VERSION {
            rebuild.needs("words read by another release".to_owned());
        }
        Ok(Changed { settings: changed, said, rebuild: rebuild.asked() })
    }

    /// Whether these settings read documents as `other` does: the same fields
    /// and filters by their paths, vectors of the same dimensions, the same
    /// analysis and table, whatever their numbers, weights and the rest that
    /// changes at once.
    pub(crate) fn reads_as(&self, other: &Settings) -> bool {
        let fields = |settings: &Settings| settings.fields.iter().map(|field| field.path.clone()).collect::<Vec<_>>();
        let filters = |settings: &Settings| settings.filters.iter().map(|f| f.path.clone()).collect::<Vec<_>>();
        let dimensions = |settings: &Settings| settings.vectors.map(|vectors| vectors.dimensions);
        fields(self) == fields(other)
            && filters(self) == filters(other)
            && dimensions(self) == dimensions(other)
            && self.analysis == other.analysis
            && self.table == other.table
    }

    /// Refuses another table or key than the index follows: its documents
    /// would be another's.
    fn check_same_table(&self, wanted: &Wanted) -> Result<()> {
        let followed = |table: &Option<Table>| table.as_ref().map(|table| (table.name.clone(), table.key.clone()));
        let (kept, asked) = (followed(&self.table), followed(&wanted.table));
        if kept == asked {
            return Ok(());
        }
        let shown = |table: Option<(String, String)>| {
            table.map_or_else(|| "documents of its own".to_owned(), |(name, key)| format!("table {name} by {key}"))
        };
        Err(Error::invalid(format!(
            "the index follows {}, not {}: drop it to make one that does",
            shown(kept),
            shown(asked)
        )))
    }

    /// Keeps the filters `wanted` names, each with its number; one removed
    /// leaves searches at once, and one added to an index that holds
    /// documents needs them read again, as a field does.
    fn change_filters(&mut self, wanted: &Wanted, documents: u64, (said, rebuild): (&mut Vec<String>, &mut Rebuild)) {
        let mut filters = Vec::with_capacity(wanted.filters.len());
        for path in &wanted.filters {
            if let Some(kept) = self.filters.iter().find(|filter| &filter.path == path) {
                filters.push(kept.clone());
                continue;
            }
            if documents > 0 {
                rebuild.needs(format!("filter {path} added"));
                continue;
            }
            said.push(format!("filter {path} added"));
            filters.push(Filter { path: path.clone(), number: self.next_filter });
            self.next_filter += 1;
        }
        for filter in &self.filters {
            if !filters.iter().any(|kept| kept.number == filter.number) {
                said.push(format!("filter {} removed; at once from searches", filter.path));
                rebuild.cleans(format!("filter {} removed", filter.path), documents);
            }
        }
        self.filters = filters;
    }

    /// The filter of `path`, while the index has it.
    pub(crate) fn filter(&self, path: &str) -> Option<&Filter> {
        self.filters.iter().find(|filter| filter.path == path)
    }

    fn change_vectors(
        &mut self,
        wanted: &Wanted,
        documents: u64,
        (said, rebuild): (&mut Vec<String>, &mut Rebuild),
    ) -> Result<()> {
        match (self.vectors, wanted.dimensions) {
            _ if self.table.is_some() => self.change_column(wanted, documents, (said, rebuild))?,
            (Some(kept), Some(dimensions)) if kept.dimensions != dimensions => {
                return Err(Error::invalid(format!(
                    "vectors of {dimensions} dimensions where the index keeps {}: they are the program's model's, \
                     and the store has nothing to make new ones from",
                    kept.dimensions
                )));
            }
            (Some(kept), None) => {
                return Err(Error::invalid(format!(
                    "opened without vectors, an index that keeps vectors of {} dimensions would lose them",
                    kept.dimensions
                )));
            }
            (None, Some(dimensions)) => {
                let held =
                    if documents > 0 { format!("; the {documents} documents held have none") } else { "".into() };
                said.push(format!("vectors of {dimensions} dimensions added{held}"));
                self.vectors = Some(Vectors { dimensions, recall: None });
            }
            _ => {}
        }
        self.change_recall(wanted, said);
        self.change_hybrid(wanted, said);
        Ok(())
    }

    fn change_recall(&mut self, wanted: &Wanted, said: &mut Vec<String>) {
        if let Some(vectors) = self.vectors.as_mut().filter(|vectors| vectors.recall != wanted.recall) {
            said.push(match wanted.recall {
                Some(recall) => format!("recall {recall}: clusters built in the background, searches exact until then"),
                None => "recall removed: searches exact at once, clusters dropped in the background".to_owned(),
            });
            vectors.recall = wanted.recall;
        }
    }

    fn change_hybrid(&mut self, wanted: &Wanted, said: &mut Vec<String>) {
        let hybrid = Hybrid::of(wanted);
        if hybrid.vector_weight != self.hybrid.vector_weight {
            said.push(format!("a vector weighs {}, was {}; at once", hybrid.vector_weight, self.hybrid.vector_weight));
        }
        if hybrid.rerank != self.hybrid.rerank {
            said.push(format!("rerank {}; at once", if hybrid.rerank { "on" } else { "off" }));
        }
        self.hybrid = hybrid;
    }

    /// A table's index's vectors as `wanted` reads them: at once while it
    /// holds no document, and by reading the column again once it does.
    fn change_column(
        &mut self,
        wanted: &Wanted,
        documents: u64,
        (said, rebuild): (&mut Vec<String>, &mut Rebuild),
    ) -> Result<()> {
        let column = |table: &Option<Table>| table.as_ref().and_then(|table| table.vector.clone());
        let (kept, asked) = (column(&self.table), column(&wanted.table));
        let kept_dimensions = self.vectors.map(|vectors| vectors.dimensions);
        if (kept.as_ref(), kept_dimensions) == (asked.as_ref(), wanted.dimensions) {
            return Ok(());
        }
        let Some(column) = asked else {
            return Err(Error::invalid(format!(
                "opened without vectors, an index that reads them from column {} would lose them",
                kept.unwrap_or_default()
            )));
        };
        let dimensions = wanted.dimensions.unwrap_or_default();
        let change = format!("vectors of {dimensions} dimensions from column {column}");
        if documents > 0 {
            rebuild.needs(change);
            return Ok(());
        }
        said.push(change);
        self.vectors = Some(Vectors { dimensions, recall: wanted.recall });
        if let Some(table) = &mut self.table {
            table.vector = Some(column);
        }
        Ok(())
    }

    /// The weight of the field numbered `number`, while the index has it.
    pub(crate) fn weight(&self, number: u32) -> Option<f64> {
        self.fields.iter().find(|field| field.number == number).map(|field| field.weight)
    }

    pub(crate) fn to_json(&self) -> String {
        let fields: Vec<Value> = self
            .fields
            .iter()
            .map(|field| json!({ "path": field.path, "weight": field.weight, "number": field.number }))
            .collect();
        let vectors = self.vectors.map(|vectors| json!({ "dimensions": vectors.dimensions, "recall": vectors.recall }));
        let filters: Vec<Value> =
            self.filters.iter().map(|filter| json!({ "path": filter.path, "number": filter.number })).collect();
        let mut settings = json!({
            "fields": fields,
            "next_field": self.next_field,
            "filters": filters,
            "next_filter": self.next_filter,
            "typo_tolerance": self.typo_tolerance,
            "analysis": self.analysis,
            "vectors": vectors,
            "hybrid": { "vector_weight": self.hybrid.vector_weight, "rerank": self.hybrid.rerank },
            "table": self.table.as_ref().map(Table::to_json),
        });
        if !self.layouts.is_empty() {
            settings["layouts"] = json!(names(&self.layouts));
        }
        settings.to_string()
    }

    pub(crate) fn from_json(text: &str) -> Result<Settings> {
        let broken = || Error::corrupt("an index's settings that do not read");
        let settings: Value = serde_json::from_str(text).map_err(|error| broken().with_source(error))?;
        let number = |value: &Value| value.as_u64().and_then(|number| u32::try_from(number).ok()).ok_or_else(broken);
        let mut fields = Vec::new();
        for field in settings["fields"].as_array().ok_or_else(broken)? {
            let path = field["path"].as_str().ok_or_else(broken)?.to_owned();
            let weight = field["weight"].as_f64().ok_or_else(broken)?;
            fields.push(Field { path, weight, number: number(&field["number"])? });
        }
        let mut filters = Vec::new();
        for filter in settings["filters"].as_array().ok_or_else(broken)? {
            let path = filter["path"].as_str().ok_or_else(broken)?.to_owned();
            filters.push(Filter { path, number: number(&filter["number"])? });
        }
        let vectors = match &settings["vectors"] {
            Value::Null => None,
            vectors => Some(Vectors {
                dimensions: number(&vectors["dimensions"])? as usize,
                recall: vectors["recall"].as_f64(),
            }),
        };
        let hybrid = &settings["hybrid"];
        let table = Table::from_json(&settings["table"]).ok_or_else(broken)?;
        // a layout a later release added is one this one does not read
        let layouts = settings["layouts"].as_array().map(Vec::as_slice).unwrap_or_default();
        let layouts = layouts.iter().filter_map(|name| Layout::named(name.as_str()?).ok()).collect();
        Ok(Settings {
            fields,
            next_field: number(&settings["next_field"])?,
            filters,
            next_filter: number(&settings["next_filter"])?,
            typo_tolerance: settings["typo_tolerance"].as_bool().ok_or_else(broken)?,
            layouts,
            analysis: number(&settings["analysis"])?,
            vectors,
            hybrid: Hybrid {
                vector_weight: hybrid["vector_weight"].as_f64().ok_or_else(broken)?,
                rerank: hybrid["rerank"].as_bool().ok_or_else(broken)?,
            },
            table,
        })
    }
}

impl Hybrid {
    fn of(wanted: &Wanted) -> Hybrid {
        Hybrid { vector_weight: wanted.vector_weight.unwrap_or(VECTOR_WEIGHT), rerank: wanted.rerank }
    }
}

fn names(layouts: &[Layout]) -> Vec<&'static str> {
    layouts.iter().map(|layout| layout.name()).collect()
}

/// A field's path as an error or the log says it; the document itself when
/// it is its own text.
pub(crate) fn shown(path: &str) -> String {
    if path.is_empty() { "the document's text".to_owned() } else { path.to_owned() }
}

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

    fn wanted(fields: &[(&str, f64)]) -> Wanted {
        Wanted {
            fields: fields.iter().map(|(path, weight)| ((*path).to_owned(), *weight)).collect(),
            filters: Vec::new(),
            typo_tolerance: true,
            layouts: Vec::new(),
            dimensions: None,
            recall: None,
            vector_weight: None,
            rerank: false,
            manual: false,
            table: None,
        }
    }

    fn with_vectors(dimensions: usize, vector_weight: Option<f64>, rerank: bool) -> Wanted {
        Wanted { dimensions: Some(dimensions), vector_weight, rerank, ..wanted(&[("title", 1.0)]) }
    }

    #[test]
    fn settings_read_back_as_written() {
        let settings = Settings::new(&wanted(&[("title", 2.0), ("body", 1.0)]));
        assert_eq!(Settings::from_json(&settings.to_json()).unwrap(), settings);
        let settings = Settings::new(&with_vectors(1024, Some(0.7), false));
        assert_eq!(Settings::from_json(&settings.to_json()).unwrap(), settings);
    }

    #[test]
    fn a_filter_removed_leaves_at_once_and_one_added_waits_for_an_empty_index_as_a_field_does() {
        let with =
            |filters: &[&str]| Wanted { filters: filters.iter().map(|f| (*f).to_owned()).collect(), ..wanted(&[]) };
        let settings = Settings::new(&with(&["category", "owner"]));
        assert_eq!(Settings::from_json(&settings.to_json()).unwrap(), settings);
        let removed = settings.change(&with(&["owner"]), 5).unwrap();
        assert_eq!(removed.said, ["filter category removed; at once from searches"]);
        assert_eq!(removed.settings.filters, [Filter { path: "owner".to_owned(), number: 1 }]);
        let cleaning = Asked { why: "filter category removed".to_owned(), needed: false };
        assert_eq!(removed.rebuild, Some(cleaning), "its keys left for a rebuild to drop");
        let changed = removed.settings;
        let added = changed.change(&with(&["owner", "category"]), 5).unwrap();
        assert_eq!(added.rebuild, Some(Asked { why: "filter category added".to_owned(), needed: true }));
        assert_eq!(added.settings, changed, "nothing changes at once");
        let added = changed.change(&with(&["owner", "category"]), 0).unwrap().settings;
        assert_eq!(added.filter("category").unwrap().number, 2, "a filter added again takes a new number");
        assert!(with(&["a", "a"]).check().is_err());
        assert!(with(&["a..b"]).check().is_err());
    }

    #[test]
    fn vectors_added_change_at_once_and_other_dimensions_or_none_are_refused() {
        let settings = Settings::new(&wanted(&[("title", 1.0)]));
        let Changed { settings: changed, said, .. } = settings.change(&with_vectors(3, None, false), 2).unwrap();
        assert_eq!(said, ["vectors of 3 dimensions added; the 2 documents held have none"]);
        assert_eq!(changed.vectors, Some(Vectors { dimensions: 3, recall: None }));
        assert_eq!(changed.change(&with_vectors(4, None, false), 2).unwrap_err().kind(), crate::ErrorKind::Invalid);
        assert_eq!(changed.change(&wanted(&[("title", 1.0)]), 2).unwrap_err().kind(), crate::ErrorKind::Invalid);
        let said = changed.change(&with_vectors(3, Some(0.7), false), 2).unwrap().said;
        assert_eq!(said, ["a vector weighs 0.7, was 0.5; at once"]);
        let said = changed.change(&with_vectors(3, None, true), 2).unwrap().said;
        assert_eq!(said, ["rerank on; at once"]);
    }

    #[test]
    fn a_vector_weight_is_from_zero_to_one_and_never_beside_a_rerank() {
        assert!(with_vectors(8, Some(1.0), false).check().is_ok());
        assert!(with_vectors(8, Some(1.5), false).check().is_err());
        assert!(with_vectors(8, Some(f64::NAN), false).check().is_err());
        assert!(with_vectors(8, Some(0.5), true).check().is_err());
        assert!(with_vectors(0, None, false).check().is_err());
        assert_eq!(with_vectors(4097, None, false).check().unwrap_err().kind(), crate::ErrorKind::Limit);
        assert!(Wanted { rerank: true, ..wanted(&[("title", 1.0)]) }.check().is_err());
    }

    #[test]
    fn a_weight_and_a_field_removed_change_at_once_and_say_so() {
        let settings = Settings::new(&wanted(&[("title", 2.0), ("body", 1.0)]));
        let changed = settings.change(&wanted(&[("title", 5.0)]), 10).unwrap();
        assert_eq!(changed.said, ["title weighs 5, was 2; at once", "body removed; at once from searches"]);
        assert_eq!(changed.settings.fields, [Field { path: "title".to_owned(), weight: 5.0, number: 0 }]);
    }

    #[test]
    fn a_field_added_takes_a_new_number_at_once_to_an_empty_index_and_asks_a_rebuild_of_one_that_holds_documents() {
        let settings = Settings::new(&wanted(&[("title", 1.0)]));
        let changed = settings.change(&wanted(&[("body", 1.0)]), 0).unwrap().settings;
        assert_eq!(changed.fields, [Field { path: "body".to_owned(), weight: 1.0, number: 1 }]);
        let rebuilt = settings.change(&wanted(&[("title", 2.0), ("body", 1.0)]), 3).unwrap();
        assert_eq!(rebuilt.rebuild, Some(Asked { why: "body added".to_owned(), needed: true }));
        assert_eq!(rebuilt.said, ["title weighs 2, was 1; at once"], "what needs no rebuild changes at once");
        assert!(
            Settings::new(&wanted(&[("title", 9.0), ("body", 1.0)]))
                .reads_as(&Settings::new(&wanted(&[("title", 1.0), ("body", 3.0)])))
        );
    }

    #[test]
    fn layouts_are_kept_by_their_names_change_at_once_and_an_index_without_keeps_none() {
        let with =
            |names: &[&str]| Wanted { layouts: names.iter().map(|name| (*name).to_owned()).collect(), ..wanted(&[]) };
        let settings = Settings::new(&with(&["ru", "en"]));
        assert_eq!(settings.layouts, [Layout::Ru, Layout::En]);
        assert!(settings.to_json().contains(r#""layouts":["ru","en"]"#));
        assert_eq!(Settings::from_json(&settings.to_json()).unwrap(), settings);
        let without = Settings::new(&with(&[]));
        assert!(!without.to_json().contains("layouts"), "{}", without.to_json());
        assert_eq!(Settings::from_json(&without.to_json()).unwrap(), without);
        let changed = settings.change(&with(&[]), 3).unwrap();
        assert_eq!((changed.said, changed.rebuild), (vec!["layouts off; at once".to_owned()], None));
        let changed = without.change(&with(&["en", "ru"]), 3).unwrap();
        assert_eq!(changed.said, ["layouts en, ru; at once"]);
        assert_eq!(changed.settings.layouts, [Layout::En, Layout::Ru]);
        assert!(settings.reads_as(&without), "layouts read no document otherwise");
        let later = settings.to_json().replace(r#"["ru","en"]"#, r#"["uk","ru","en"]"#);
        assert_eq!(Settings::from_json(&later).unwrap(), settings, "a layout a later release added is passed");
        assert_eq!(with(&["ru", "de"]).check().unwrap_err().kind(), crate::ErrorKind::Invalid);
    }

    #[test]
    fn a_weight_is_above_zero_and_a_path_names_a_field() {
        assert!(wanted(&[("title", 0.0)]).check().is_err());
        assert!(wanted(&[("title", f64::NAN)]).check().is_err());
        assert!(wanted(&[("author..name", 1.0)]).check().is_err());
        assert!(wanted(&[("title", 1.0), ("title", 2.0)]).check().is_err());
        assert!(wanted(&[("author.name", 1.0)]).check().is_ok());
    }
}