hawdb-storage 0.37.0

An embedded Rust graph database for local data-plane workloads
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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
// Copyright 2026 Nowledge
// SPDX-License-Identifier: Apache-2.0

//! Engine-owned BM25 postings over one TEXT column of a RowPage table.
//!
//! A search index is a synchronously maintained derived relation: its
//! compact posting blocks, per-document lengths and corpus statistics are
//! ordinary rows of a hidden RowPage table that the base transaction writes
//! before it commits (layout in `layout`).
//! WAL replay re-derives them from the same deterministic analyzer, so there is
//! no separately acknowledged projection, watermark or catch-up job.

use super::*;

mod layout;
use layout::{
    bounds_key, decode_document_ordinal, document_key, put_varint, sealed_block_key,
    sealed_block_range, MIN_BLOCK_POSTINGS,
};
pub use layout::{
    decode_search_sealed_block, decode_search_sealed_block_header, search_index_block_bounds_range,
    search_index_block_fence, search_index_dictionary_key, search_index_length_block_key,
    search_index_length_block_range, search_index_length_block_start,
    search_index_sealed_block_after_range, search_index_sealed_block_range,
    search_index_sealed_block_window, search_index_statistics_key, SearchBlockBounds,
    SearchDocumentEntry, SearchIndexStatistics, SearchLengthBlock, SearchPosting,
    SearchPostingBlockHeader, SearchTermDictionary, SEARCH_INDEX_BLOCK_POSTINGS,
    SEARCH_INDEX_LENGTH_BLOCK_DOCUMENTS, SEARCH_INDEX_MAX_BLOCK_POSTINGS,
    SEARCH_INDEX_TAIL_POSTINGS,
};

/// Every row of a search index relation is `(kind, name, slot) -> data`; see
/// `layout` for the row kinds and their encodings.
pub const SEARCH_INDEX_KIND_COLUMN: &str = "__hawdb_search_kind";
pub const SEARCH_INDEX_NAME_COLUMN: &str = "__hawdb_search_name";
pub const SEARCH_INDEX_SLOT_COLUMN: &str = "__hawdb_search_slot";
pub const SEARCH_INDEX_DATA_COLUMN: &str = "__hawdb_search_data";
/// Number of physical columns in a search index relation.
pub const SEARCH_INDEX_COLUMN_COUNT: usize = 4;
/// The physical layout recorded with every search index definition. Readers
/// refuse any other layout as an unsupported format.
pub(crate) const SEARCH_INDEX_LAYOUT_TAG: u8 = 3;
/// Tokens longer than this many bytes are not indexed (encoded blobs, hashes).
pub const SEARCH_INDEX_MAX_TOKEN_BYTES: usize = 128;

/// The deterministic text analysis bound into a persistent definition. A new
/// analysis is a new variant; an existing one never changes its output.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SearchIndexAnalyzer {
    /// Lowercased Unicode letter/digit words, with full-width ASCII folded to
    /// ASCII; Han, Kana and Hangul runs indexed as character unigrams and
    /// overlapping bigrams.
    CjkNgram,
}

impl SearchIndexAnalyzer {
    pub const DEFAULT: Self = Self::CjkNgram;

    pub fn name(self) -> &'static str {
        match self {
            Self::CjkNgram => "cjk_ngram",
        }
    }

    pub fn parse(name: &str) -> Option<Self> {
        name.eq_ignore_ascii_case("cjk_ngram")
            .then_some(Self::CjkNgram)
    }

    pub(crate) fn tag(self) -> u8 {
        match self {
            Self::CjkNgram => 1,
        }
    }

    pub(crate) fn from_tag(tag: u8) -> Option<Self> {
        (tag == 1).then_some(Self::CjkNgram)
    }

    /// Index-side tokens in document order, with repetition. The count is
    /// the BM25 document length. Allocates every token; maintenance uses
    /// [`Self::document_terms`] instead.
    pub fn index_tokens(self, text: &str) -> Vec<String> {
        let mut tokens = Vec::new();
        let Ok(()) = self.visit_index_tokens::<std::convert::Infallible>(text, |token| {
            tokens.push(token.to_owned());
            Ok(())
        });
        tokens
    }

    /// Visits index-side tokens in document order, with repetition. Tokens
    /// borrow bounded scratch buffers, so analysis itself allocates nothing
    /// proportional to the text, and `visit` can stop it at any token.
    pub fn visit_index_tokens<E>(
        self,
        text: &str,
        mut visit: impl FnMut(&str) -> Result<(), E>,
    ) -> Result<(), E> {
        let mut buffer = [0u8; 8];
        analyze(text, |piece| match piece {
            Piece::Word(word) => visit(word),
            Piece::Ideograph { previous, ch } => {
                if let Some(previous) = previous {
                    visit(ideographic_token(&mut buffer, Some(previous), ch))?;
                }
                visit(ideographic_token(&mut buffer, None, ch))
            }
            Piece::LoneIdeograph(_) => Ok(()),
        })
    }

    /// One document's distinct index terms with their frequencies, and its
    /// length. `admit` is called with an estimate of the bytes each new
    /// distinct term will occupy before that term is allocated, and with 0
    /// every [`SEARCH_INDEX_ANALYSIS_CHECKPOINT_TOKENS`] tokens as a
    /// cancellation checkpoint; its first error stops the analysis. Repeated
    /// tokens allocate nothing.
    pub fn document_terms<E>(
        self,
        text: &str,
        mut admit: impl FnMut(usize) -> Result<(), E>,
    ) -> Result<(BTreeMap<String, u64>, u64), E> {
        let mut terms = BTreeMap::<String, u64>::new();
        let mut length = 0u64;
        self.visit_index_tokens(text, |token| {
            length += 1;
            if length.is_multiple_of(SEARCH_INDEX_ANALYSIS_CHECKPOINT_TOKENS) {
                admit(0)?;
            }
            match terms.get_mut(token) {
                Some(frequency) => *frequency += 1,
                None => {
                    admit(search_index_term_entry_bytes(token))?;
                    terms.insert(token.to_owned(), 1);
                }
            }
            Ok(())
        })?;
        Ok((terms, length))
    }

    /// Distinct query-side terms in ascending order. Multi-character
    /// ideographic runs use their bigrams, which are more selective than the
    /// unigrams also present in the index; a lone character uses its unigram.
    pub fn query_terms(self, text: &str) -> Vec<String> {
        let Ok(terms) = self.admitted_query_terms::<std::convert::Infallible>(text, |_| Ok(()));
        terms
    }

    /// [`Self::query_terms`], calling `admit` with each new distinct term's
    /// estimated bytes before allocating it.
    pub fn admitted_query_terms<E>(
        self,
        text: &str,
        mut admit: impl FnMut(usize) -> Result<(), E>,
    ) -> Result<Vec<String>, E> {
        let mut terms = BTreeSet::<String>::new();
        let mut buffer = [0u8; 8];
        let mut insert = |term: &str| {
            if !terms.contains(term) {
                admit(search_index_term_entry_bytes(term))?;
                terms.insert(term.to_owned());
            }
            Ok(())
        };
        analyze(text, |piece| match piece {
            Piece::Word(word) => insert(word),
            Piece::Ideograph {
                previous: Some(previous),
                ch,
            } => insert(ideographic_token(&mut buffer, Some(previous), ch)),
            Piece::Ideograph { previous: None, .. } => Ok(()),
            Piece::LoneIdeograph(ch) => insert(ideographic_token(&mut buffer, None, ch)),
        })?;
        Ok(terms.into_iter().collect())
    }
}

/// The search-specific part of a derived relation definition.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SearchIndexSpec {
    pub column: String,
    pub analyzer: SearchIndexAnalyzer,
}

fn fold(ch: char) -> char {
    // Full-width ASCII forms (U+FF01..=U+FF5E) and the ideographic space.
    match ch as u32 {
        0xFF01..=0xFF5E => char::from_u32(ch as u32 - 0xFEE0).unwrap_or(ch),
        0x3000 => ' ',
        _ => ch,
    }
}

pub(crate) fn is_ideographic(ch: char) -> bool {
    matches!(
        ch as u32,
        0x1100..=0x11FF // Hangul Jamo
            | 0x2E80..=0x2FDF // CJK radicals, Kangxi radicals
            | 0x3040..=0x309F // Hiragana
            | 0x30A0..=0x30FF // Katakana
            | 0x3130..=0x318F // Hangul compatibility Jamo
            | 0x31F0..=0x31FF // Katakana phonetic extensions
            | 0x3400..=0x4DBF // CJK extension A
            | 0x4E00..=0x9FFF // CJK unified ideographs
            | 0xAC00..=0xD7AF // Hangul syllables
            | 0xF900..=0xFAFF // CJK compatibility ideographs
            | 0x20000..=0x323AF // CJK extensions B..H
    )
}

/// One analysis event. Words borrow a scratch buffer bounded by
/// [`SEARCH_INDEX_MAX_TOKEN_BYTES`]; ideographic runs are streamed one
/// character at a time, so nothing proportional to the text is buffered.
enum Piece<'a> {
    Word(&'a str),
    /// An ideographic character and its predecessor in the same run.
    Ideograph {
        previous: Option<char>,
        ch: char,
    },
    /// The end of an ideographic run of exactly one character.
    LoneIdeograph(char),
}

/// A unigram (`previous == None`) or bigram encoded into `buffer`.
fn ideographic_token(buffer: &mut [u8; 8], previous: Option<char>, ch: char) -> &str {
    let mut length = 0;
    if let Some(previous) = previous {
        length = previous.encode_utf8(&mut buffer[..]).len();
    }
    length += ch.encode_utf8(&mut buffer[length..]).len();
    std::str::from_utf8(&buffer[..length]).expect("encoded characters are UTF-8")
}

/// Estimated resident bytes of one distinct analysis term in a B-tree map
/// or set: the term's bytes plus twice the entry (B-tree nodes other than
/// the root are at least half full) plus node headers.
pub fn search_index_term_entry_bytes(term: &str) -> usize {
    term.len()
        .saturating_add(2 * (std::mem::size_of::<String>() + std::mem::size_of::<u64>()))
        .saturating_add(16)
}

/// Tokens between analysis cancellation checkpoints.
pub const SEARCH_INDEX_ANALYSIS_CHECKPOINT_TOKENS: u64 = 4096;

struct Scanner {
    word: String,
    /// The current word already exceeds the token bound; the rest of it is
    /// skipped instead of buffered.
    oversized: bool,
    previous: Option<char>,
    run: usize,
}

impl Scanner {
    fn flush_word<E>(
        &mut self,
        emit: &mut impl FnMut(Piece<'_>) -> Result<(), E>,
    ) -> Result<(), E> {
        let oversized = std::mem::take(&mut self.oversized);
        if !oversized && !self.word.is_empty() {
            emit(Piece::Word(&self.word))?;
        }
        self.word.clear();
        Ok(())
    }

    fn flush_run<E>(&mut self, emit: &mut impl FnMut(Piece<'_>) -> Result<(), E>) -> Result<(), E> {
        let previous = self.previous.take();
        if let (1, Some(ch)) = (std::mem::take(&mut self.run), previous) {
            emit(Piece::LoneIdeograph(ch))?;
        }
        Ok(())
    }
}

fn analyze<E>(text: &str, mut emit: impl FnMut(Piece<'_>) -> Result<(), E>) -> Result<(), E> {
    let mut scanner = Scanner {
        word: String::new(),
        oversized: false,
        previous: None,
        run: 0,
    };
    for ch in text.chars().map(fold) {
        if is_ideographic(ch) {
            scanner.flush_word(&mut emit)?;
            emit(Piece::Ideograph {
                previous: scanner.previous,
                ch,
            })?;
            scanner.previous = Some(ch);
            scanner.run = scanner.run.saturating_add(1);
        } else if ch.is_alphanumeric() {
            scanner.flush_run(&mut emit)?;
            if !scanner.oversized {
                scanner.word.extend(ch.to_lowercase());
                if scanner.word.len() > SEARCH_INDEX_MAX_TOKEN_BYTES {
                    scanner.oversized = true;
                    scanner.word.clear();
                }
            }
        } else {
            scanner.flush_word(&mut emit)?;
            scanner.flush_run(&mut emit)?;
        }
    }
    scanner.flush_word(&mut emit)?;
    scanner.flush_run(&mut emit)
}

pub(super) fn table_schema(
    name: String,
    definition: &MaterializedViewDefinition,
    spec: &SearchIndexSpec,
    source: &RelationalTableSchema,
) -> Result<RelationalTableSchema, RelationalError> {
    if !definition.columns.is_empty()
        || !definition.group_by.is_empty()
        || definition.filter.is_some()
    {
        return Err(RelationalError::Schema(
            "search index definitions have no projection, grouping or filter".into(),
        ));
    }
    let column = source_column(source, &spec.column)?;
    if column.scalar_type != RelationalScalarType::Text {
        return Err(RelationalError::Schema(format!(
            "search index column {} must be TEXT",
            spec.column
        )));
    }
    let internal = |name: &str, scalar_type| RelationalColumnSchema {
        name: name.into(),
        scalar_type,
        nullable: false,
        default: None,
    };
    let schema = RelationalTableSchema {
        name,
        columns: vec![
            internal(SEARCH_INDEX_KIND_COLUMN, RelationalScalarType::BigInt),
            internal(SEARCH_INDEX_NAME_COLUMN, RelationalScalarType::Bytea),
            internal(SEARCH_INDEX_SLOT_COLUMN, RelationalScalarType::BigInt),
            internal(SEARCH_INDEX_DATA_COLUMN, RelationalScalarType::Bytea),
        ],
        primary_key: vec![
            SEARCH_INDEX_KIND_COLUMN.into(),
            SEARCH_INDEX_NAME_COLUMN.into(),
            SEARCH_INDEX_SLOT_COLUMN.into(),
        ],
        unique_constraints: Vec::new(),
        foreign_keys: Vec::new(),
        indexes: Vec::new(),
        materialized_view: Some(Box::new(definition.clone())),
    };
    validate_table_schema(&schema)?;
    Ok(schema)
}

fn analyzed_document(
    spec: &SearchIndexSpec,
    source: &RelationalTableSchema,
    row: Option<&RelationalRow>,
    budget: &mut MaintenanceBudget<'_>,
) -> Result<(BTreeMap<String, u64>, u64), RelationalError> {
    let Some(row) = row else {
        return Ok((BTreeMap::new(), 0));
    };
    let text = match value(source, row, &spec.column) {
        RelationalValue::Null => return Ok((BTreeMap::new(), 0)),
        RelationalValue::Text(text) => text.as_str(),
        _ => {
            return Err(RelationalError::Corruption(
                "search index source value is not resident TEXT".into(),
            ))
        }
    };
    // Each distinct term is admitted against the transaction payload budget
    // before it is allocated, which bounds one document's analysis state;
    // a large ideographic value is refused before it expands into terms.
    spec.analyzer
        .document_terms(text, |bytes| budget.charge(0, bytes))
}

fn corruption(message: &str) -> RelationalError {
    RelationalError::Corruption(format!("search index {message}"))
}

pub(super) struct SearchMaintenance<'a, 'b> {
    pub previous: &'a RelationalState,
    pub next: &'a mut RelationalState,
    pub schema: &'a RelationalTableSchema,
    pub source: &'a RelationalTableSchema,
    pub spec: &'a SearchIndexSpec,
    pub rebuild: bool,
    pub view_keys: &'a mut BTreeSet<RelationalKey>,
    pub overflow_config: RelationalOverflowConfig,
    pub budget: &'a mut MaintenanceBudget<'b>,
}

/// Reads and writes rows of one search index relation inside the commit.
struct IndexRows<'a, 'b> {
    next: &'a mut RelationalState,
    schema: &'a RelationalTableSchema,
    view_keys: &'a mut BTreeSet<RelationalKey>,
    overflow_config: RelationalOverflowConfig,
    budget: &'a mut MaintenanceBudget<'b>,
}

impl IndexRows<'_, '_> {
    fn read(&mut self, key: &RelationalKey) -> Result<Option<Vec<u8>>, RelationalError> {
        let Some(row) = self.budget.read(self.next, &self.schema.name, key)? else {
            return Ok(None);
        };
        match row.values() {
            [_, _, _, RelationalValue::Bytea(data)] => Ok(Some(data.clone())),
            _ => Err(corruption("row has an invalid payload")),
        }
    }

    fn write(&mut self, key: RelationalKey, data: Option<Vec<u8>>) -> Result<(), RelationalError> {
        let row = data.map(|data| {
            let mut values = key.0.clone();
            values.push(RelationalValue::Bytea(data));
            RelationalRow::new(values)
        });
        write_output(
            self.next,
            self.schema,
            key,
            row,
            self.view_keys,
            self.overflow_config,
            self.budget,
        )
    }

    /// The fence of the first (`last == false`) or last sealed block key in
    /// `range`, located without hydrating any block.
    fn fence(
        &mut self,
        range: RelationalPrimaryKeyRange,
        last: bool,
    ) -> Result<Option<u64>, RelationalError> {
        self.budget.charge(1, 0)?;
        let rows = &self
            .next
            .segments
            .get(&self.schema.name)
            .ok_or_else(|| corruption("relation is absent"))?
            .rows;
        let mut keys = rows.range(Some(range)).map(|(key, _)| key);
        let key = if last { keys.next_back() } else { keys.next() };
        key.map(|key| search_index_block_fence(&key.0[2]))
            .transpose()
    }
}

/// One commit's change to one term's posting for one document.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum PostingChange {
    /// The document now contains the term `frequency` times and is `length`
    /// tokens long.
    Set { frequency: u64, length: u64 },
    /// The document no longer contains the term (or left the corpus).
    Remove,
    /// The posting is unchanged, but its document became shorter (`length`
    /// tokens), so the owning block's minimum length must not exceed it.
    Shorter { length: u64 },
}

/// Lowers a conservative minimum length, treating 0 on either side as absent.
fn lower(min_length: &mut u64, length: u64) {
    if length == 0 {
        return;
    }
    *min_length = if *min_length == 0 {
        length
    } else {
        (*min_length).min(length)
    };
}

/// Applies ordinal-sorted changes to an ordinal-sorted posting list, keeping
/// `frequency` (the term's document frequency) exact and `min_length` at most
/// the length of every document the list holds.
fn apply_changes(
    postings: &mut Vec<SearchPosting>,
    changes: &[(u64, PostingChange)],
    frequency: &mut u64,
    min_length: &mut u64,
) -> Result<(), RelationalError> {
    let mut merged = Vec::with_capacity(postings.len() + changes.len());
    let mut existing = std::mem::take(postings).into_iter().peekable();
    for &(ordinal, change) in changes {
        while let Some(posting) = existing.next_if(|posting| posting.ordinal < ordinal) {
            merged.push(posting);
        }
        let present = existing.next_if(|posting| posting.ordinal == ordinal);
        match change {
            PostingChange::Set {
                frequency: term_frequency,
                length,
            } => {
                if present.is_none() {
                    *frequency = frequency
                        .checked_add(1)
                        .ok_or_else(|| corruption("document frequency overflows"))?;
                }
                merged.push(SearchPosting {
                    ordinal,
                    frequency: term_frequency,
                });
                lower(min_length, length);
            }
            PostingChange::Remove if present.is_some() => {
                *frequency = frequency
                    .checked_sub(1)
                    .ok_or_else(|| corruption("document frequency is negative"))?;
            }
            PostingChange::Remove => return Err(corruption("posting to remove is missing")),
            PostingChange::Shorter { length } => {
                merged.push(present.ok_or_else(|| corruption("posting to bound is missing"))?);
                lower(min_length, length);
            }
        }
    }
    merged.extend(existing);
    *postings = merged;
    Ok(())
}

/// A sealed block's postings with its bounds row, checked against each other.
fn read_block(
    index: &mut IndexRows<'_, '_>,
    term: &str,
    fence: u64,
) -> Result<(Vec<SearchPosting>, SearchBlockBounds), RelationalError> {
    let data = index
        .read(&sealed_block_key(term, fence))?
        .ok_or_else(|| corruption("sealed block is missing"))?;
    let mut postings = Vec::new();
    decode_search_sealed_block(fence, &data, &mut postings)?;
    let bounds = read_bounds(index, term, fence)?;
    if !bounds.describes(fence, &postings) {
        return Err(corruption("block bounds disagree with their block"));
    }
    Ok((postings, bounds))
}

fn read_bounds(
    index: &mut IndexRows<'_, '_>,
    term: &str,
    fence: u64,
) -> Result<SearchBlockBounds, RelationalError> {
    let data = index
        .read(&bounds_key(term, fence))?
        .ok_or_else(|| corruption("block bounds are missing"))?;
    SearchBlockBounds::decode(fence, &data)
}

/// Writes a sealed block and its bounds row: the exact count, last ordinal
/// and maximum frequency of `postings`, and `min_length` as the length bound.
fn write_block(
    index: &mut IndexRows<'_, '_>,
    term: &str,
    fence: u64,
    postings: &[SearchPosting],
    min_length: u64,
) -> Result<(), RelationalError> {
    if !postings.is_empty() && min_length == 0 {
        return Err(corruption("sealed block has no length bound"));
    }
    let bounds = SearchBlockBounds::of(fence, postings, min_length);
    index.write(
        sealed_block_key(term, fence),
        Some(layout::encode_sealed_block(fence, postings)),
    )?;
    index.write(bounds_key(term, fence), Some(bounds.encode(fence)))
}

fn delete_block(
    index: &mut IndexRows<'_, '_>,
    term: &str,
    fence: u64,
) -> Result<(), RelationalError> {
    index.write(sealed_block_key(term, fence), None)?;
    index.write(bounds_key(term, fence), None)
}

fn encode_ordinal(ordinal: u64) -> Vec<u8> {
    let mut data = Vec::with_capacity(4);
    put_varint(&mut data, ordinal);
    data
}

pub(super) fn maintain(
    maintenance: SearchMaintenance<'_, '_>,
    keys: &BTreeSet<RelationalKey>,
) -> Result<(), RelationalError> {
    let SearchMaintenance {
        previous,
        next,
        schema,
        source,
        spec,
        rebuild,
        view_keys,
        overflow_config,
        budget,
    } = maintenance;
    let mut index = IndexRows {
        next,
        schema,
        view_keys,
        overflow_config,
        budget,
    };
    let statistics_key = search_index_statistics_key();
    let stored = index
        .read(&statistics_key)?
        .map(|data| SearchIndexStatistics::decode(&data))
        .transpose()?;
    let mut statistics = stored.unwrap_or_default();
    // Changes are gathered for the whole commit first so that each term's
    // dictionary row and each touched block is rewritten once per commit.
    let mut terms = BTreeMap::<String, BTreeMap<u64, PostingChange>>::new();
    let mut lengths = BTreeMap::<u64, Option<SearchDocumentEntry>>::new();
    for key in keys {
        let old = if rebuild {
            None
        } else {
            index.budget.read(previous, &source.name, key)?
        };
        // Overflowed TEXT is hydrated by the maintenance read before analysis.
        let new = index.budget.read(index.next, &source.name, key)?;
        let (old_terms, old_length) = analyzed_document(spec, source, old.as_ref(), index.budget)?;
        let (new_terms, new_length) = analyzed_document(spec, source, new.as_ref(), index.budget)?;
        if old_terms == new_terms {
            continue;
        }
        let document = encode_relational_primary_key(key)?;
        index.budget.charge(0, document.len())?;
        let document_row = document_key(&document);
        let ordinal = if old_terms.is_empty() {
            // A document entering the corpus gets the next ordinal; WAL replay
            // assigns the same one because it replays the same key order.
            let ordinal = statistics.allocate_ordinal()?;
            index.write(document_row, Some(encode_ordinal(ordinal)))?;
            statistics.documents += 1;
            ordinal
        } else {
            let data = index
                .read(&document_row)?
                .ok_or_else(|| corruption("document row is missing"))?;
            let ordinal = decode_document_ordinal(&data)?;
            if new_terms.is_empty() {
                index.write(document_row, None)?;
                statistics.documents = statistics
                    .documents
                    .checked_sub(1)
                    .ok_or_else(|| corruption("document count is negative"))?;
            }
            ordinal
        };
        statistics.tokens = statistics
            .tokens
            .checked_add(new_length)
            .and_then(|tokens| tokens.checked_sub(old_length))
            .ok_or_else(|| corruption("token count is out of range"))?;
        if new_terms.is_empty() {
            lengths.insert(ordinal, None);
        } else if old_terms.is_empty() || old_length != new_length {
            lengths.insert(
                ordinal,
                Some(SearchDocumentEntry {
                    length: new_length,
                    key: document,
                }),
            );
        }
        for term in old_terms.keys() {
            if !new_terms.contains_key(term) {
                index.budget.charge(0, term.len().saturating_add(16))?;
                terms
                    .entry(term.clone())
                    .or_default()
                    .insert(ordinal, PostingChange::Remove);
            }
        }
        // A shorter document can score higher for a term whose posting did
        // not change, so its owning block's length bound is lowered too.
        let shorter = !old_terms.is_empty() && new_length < old_length;
        for (term, frequency) in new_terms {
            let change = if old_terms.get(&term) != Some(&frequency) {
                PostingChange::Set {
                    frequency,
                    length: new_length,
                }
            } else if shorter {
                PostingChange::Shorter { length: new_length }
            } else {
                continue;
            };
            index.budget.charge(0, term.len().saturating_add(16))?;
            terms.entry(term).or_default().insert(ordinal, change);
        }
    }
    let mut block_changes = BTreeMap::<u64, Vec<(u64, Option<SearchDocumentEntry>)>>::new();
    for (ordinal, entry) in lengths {
        block_changes
            .entry(ordinal / SEARCH_INDEX_LENGTH_BLOCK_DOCUMENTS)
            .or_default()
            .push((ordinal, entry));
    }
    for (block, changes) in block_changes {
        let key = search_index_length_block_key(block * SEARCH_INDEX_LENGTH_BLOCK_DOCUMENTS);
        let mut lengths = index
            .read(&key)?
            .map(|data| SearchLengthBlock::decode(&data))
            .transpose()?
            .unwrap_or_default();
        for (ordinal, entry) in changes {
            lengths.set(ordinal, entry);
        }
        index.write(key, lengths.encode())?;
    }
    for (term, changes) in terms {
        let changes = changes.into_iter().collect::<Vec<_>>();
        maintain_term(&mut index, &term, &changes)?;
    }
    if stored != Some(statistics) {
        // An empty corpus leaves an empty relation: every document, lengths
        // and term row has been removed with its last document.
        let data = (statistics.documents > 0).then(|| statistics.encode());
        if data.is_some() || stored.is_some() {
            index.write(statistics_key, data)?;
        }
    }
    Ok(())
}

/// Applies one commit's ordinal-sorted changes to one term. Appends touch
/// only the dictionary row (flushing a full tail into sealed blocks); changes to
/// older documents rewrite the sealed blocks that own them, and a document
/// that only became shorter rewrites just its blocks' bounds rows. Every
/// rewritten block holds at most `SEARCH_INDEX_MAX_BLOCK_POSTINGS` postings, so the work is
/// bounded by the number of changes rather than by the term's document
/// frequency.
fn maintain_term(
    index: &mut IndexRows<'_, '_>,
    term: &str,
    changes: &[(u64, PostingChange)],
) -> Result<(), RelationalError> {
    let dictionary_key = search_index_dictionary_key(term);
    let stored = index.read(&dictionary_key)?;
    let mut dictionary = stored
        .as_deref()
        .map(SearchTermDictionary::decode)
        .transpose()?
        .unwrap_or_default();
    let split = changes.partition_point(|(ordinal, _)| *ordinal < dictionary.tail_start);
    let (sealed_changes, tail_changes) = changes.split_at(split);
    // Route each older change to the sealed block that owns its ordinal:
    // the block with the greatest fence at or below it.
    let mut owned = BTreeMap::<u64, Vec<(u64, PostingChange)>>::new();
    let mut owner: Option<(u64, Option<u64>)> = None;
    for &(ordinal, change) in sealed_changes {
        let fence = match owner {
            Some((fence, upper)) if upper.is_none_or(|upper| ordinal < upper) => fence,
            _ => {
                let fence = index
                    .fence(
                        sealed_block_range(term, (0, true), Some((ordinal, true)))?,
                        true,
                    )?
                    .ok_or_else(|| corruption("posting has no owning block"))?;
                let upper = index.fence(sealed_block_range(term, (fence, false), None)?, false)?;
                owner = Some((fence, upper));
                fence
            }
        };
        owned.entry(fence).or_default().push((ordinal, change));
    }
    let mut touched = Vec::with_capacity(owned.len());
    for (fence, changes) in owned {
        if changes
            .iter()
            .all(|(_, change)| matches!(change, PostingChange::Shorter { .. }))
        {
            // Postings are unchanged: only the length bound can tighten.
            let mut bounds = read_bounds(index, term, fence)?;
            let mut min_length = bounds.min_length;
            for (_, change) in &changes {
                if let PostingChange::Shorter { length } = change {
                    lower(&mut min_length, *length);
                }
            }
            if bounds.count == 0 {
                return Err(corruption("posting to bound is missing"));
            }
            if min_length != bounds.min_length {
                bounds.min_length = min_length;
                index.write(bounds_key(term, fence), Some(bounds.encode(fence)))?;
            }
            continue;
        }
        let (mut postings, bounds) = read_block(index, term, fence)?;
        let mut min_length = bounds.min_length;
        apply_changes(
            &mut postings,
            &changes,
            &mut dictionary.document_frequency,
            &mut min_length,
        )?;
        if postings.len() > SEARCH_INDEX_MAX_BLOCK_POSTINGS {
            // Many older documents gained this term in one commit: split into
            // full blocks keyed by their first ordinals. Each part keeps the
            // whole block's (conservative) length bound.
            let rest = postings.split_off(SEARCH_INDEX_BLOCK_POSTINGS);
            for chunk in rest.chunks(SEARCH_INDEX_BLOCK_POSTINGS) {
                write_block(index, term, chunk[0].ordinal, chunk, min_length)?;
                dictionary.sealed_blocks += 1;
            }
        }
        if postings.is_empty() && fence != 0 {
            // The predecessor's range absorbs this one. The first block keeps
            // fence 0 even when empty so that every older ordinal has an owner.
            delete_block(index, term, fence)?;
            dictionary.sealed_blocks = dictionary
                .sealed_blocks
                .checked_sub(1)
                .ok_or_else(|| corruption("sealed block count is negative"))?;
        } else {
            write_block(index, term, fence, &postings, min_length)?;
            touched.push(fence);
        }
    }
    apply_changes(
        &mut dictionary.tail,
        tail_changes,
        &mut dictionary.document_frequency,
        &mut dictionary.tail_min_length,
    )?;
    if dictionary.tail.len() >= SEARCH_INDEX_TAIL_POSTINGS {
        // Flush the tail: first top up the last sealed block to a full block,
        // then seal what remains in blocks of up to a full block each. The
        // dictionary row rewritten by every append stays small, and a sealed
        // block is rewritten once per flush, not once per append. Flushed
        // postings carry the tail's length bound into their blocks.
        let mut tail = std::mem::take(&mut dictionary.tail);
        let tail_min_length = dictionary.tail_min_length;
        if dictionary.sealed_blocks > 0 {
            let fence = index
                .fence(sealed_block_range(term, (0, true), None)?, true)?
                .ok_or_else(|| corruption("sealed block is missing"))?;
            let (mut postings, bounds) = read_block(index, term, fence)?;
            let room = SEARCH_INDEX_BLOCK_POSTINGS.saturating_sub(postings.len());
            if room > 0 {
                postings.extend(tail.drain(..room.min(tail.len())));
                dictionary.tail_start = postings[postings.len() - 1].ordinal + 1;
                let mut min_length = bounds.min_length;
                lower(&mut min_length, tail_min_length);
                write_block(index, term, fence, &postings, min_length)?;
            }
        }
        while tail.len() >= SEARCH_INDEX_TAIL_POSTINGS {
            let chunk = tail
                .drain(..SEARCH_INDEX_BLOCK_POSTINGS.min(tail.len()))
                .collect::<Vec<_>>();
            let fence = dictionary.tail_start;
            write_block(index, term, fence, &chunk, tail_min_length)?;
            dictionary.sealed_blocks += 1;
            dictionary.tail_start = chunk[chunk.len() - 1].ordinal + 1;
        }
        dictionary.tail = tail;
    }
    if dictionary.tail.is_empty() {
        dictionary.tail_min_length = 0;
    }
    // Compaction: a changed block that shrank below a quarter merges into its
    // predecessor, or absorbs its successor, when the two fit in one block.
    // Delete-heavy churn therefore cannot leave a run of nearly empty blocks,
    // and each changed block costs at most one extra block rewrite.
    for fence in touched {
        if index.read(&sealed_block_key(term, fence))?.is_none() {
            continue;
        }
        let (mut postings, bounds) = read_block(index, term, fence)?;
        if postings.len() >= MIN_BLOCK_POSTINGS {
            continue;
        }
        let predecessor = if fence == 0 {
            None
        } else {
            index.fence(
                sealed_block_range(term, (0, true), Some((fence, false)))?,
                true,
            )?
        };
        let successor = index.fence(sealed_block_range(term, (fence, false), None)?, false)?;
        for (neighbor, absorb) in [(predecessor, false), (successor, true)] {
            let Some(neighbor) = neighbor else {
                continue;
            };
            let (mut neighbor_postings, neighbor_bounds) = read_block(index, term, neighbor)?;
            if postings.len() + neighbor_postings.len() > SEARCH_INDEX_BLOCK_POSTINGS {
                continue;
            }
            let mut min_length = bounds.min_length;
            lower(&mut min_length, neighbor_bounds.min_length);
            let (kept, kept_fence, removed) = if absorb {
                postings.extend(neighbor_postings);
                (postings, fence, neighbor)
            } else {
                neighbor_postings.extend(postings);
                (neighbor_postings, neighbor, fence)
            };
            delete_block(index, term, removed)?;
            write_block(index, term, kept_fence, &kept, min_length)?;
            dictionary.sealed_blocks = dictionary
                .sealed_blocks
                .checked_sub(1)
                .ok_or_else(|| corruption("sealed block count is negative"))?;
            break;
        }
    }
    if dictionary.document_frequency == 0 {
        // Only the retained empty first block can remain.
        match dictionary.sealed_blocks {
            0 => {}
            1 => delete_block(index, term, 0)?,
            _ => return Err(corruption("empty term retains sealed blocks")),
        }
        if stored.is_some() {
            index.write(dictionary_key, None)?;
        }
        return Ok(());
    }
    let encoded = dictionary.encode();
    if stored.as_deref() != Some(encoded.as_slice()) {
        index.write(dictionary_key, Some(encoded))?;
    }
    Ok(())
}

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

    #[test]
    fn analyzer_lowercases_words_and_folds_full_width_ascii() {
        let analyzer = SearchIndexAnalyzer::CjkNgram;
        assert_eq!(
            analyzer.index_tokens("Fix the HawDB-bm25 index, ABC v2!"),
            ["fix", "the", "hawdb", "bm25", "index", "abc", "v2"]
        );
        assert_eq!(analyzer.query_terms("the THE index"), ["index", "the"]);
        assert!(analyzer.index_tokens(" ,.;  ").is_empty());
    }

    #[test]
    fn analyzer_indexes_ideographic_unigrams_and_bigrams() {
        let analyzer = SearchIndexAnalyzer::CjkNgram;
        assert_eq!(
            analyzer.index_tokens("数据库用HawDB"),
            ["数", "数据", "据", "据库", "库", "库用", "用", "hawdb"]
        );
        assert_eq!(analyzer.query_terms("数据库"), ["据库", "数据"]);
        assert_eq!(analyzer.query_terms("库"), ["库"]);
        assert_eq!(
            analyzer.index_tokens("東京タワー 서울"),
            [
                "東", "東京", "京", "京タ", "タ", "タワ", "ワ", "ワー", "ー", "서", "서울", "울"
            ]
        );
    }

    /// A long run of distinct ideographs: about two distinct terms per
    /// character (its unigram and the bigram ending at it).
    fn distinct_cjk(characters: usize) -> String {
        let mut state = 0x2545_f491_u32;
        (0..characters)
            .map(|_| {
                state ^= state << 13;
                state ^= state >> 17;
                state ^= state << 5;
                char::from_u32(0x4E00 + state % 0x5200).expect("CJK unified ideograph")
            })
            .collect()
    }

    #[test]
    fn analyzer_streams_document_terms_and_matches_materialized_tokens() {
        let analyzer = SearchIndexAnalyzer::CjkNgram;
        let long = "y".repeat(SEARCH_INDEX_MAX_TOKEN_BYTES + 1);
        for text in [
            "数据库用HawDB 数据库 the THE 東京タワー 서울 库".to_owned(),
            format!("{long} 数 a{long}b ABC v2 数据"),
            distinct_cjk(5_000),
        ] {
            let tokens = analyzer.index_tokens(&text);
            let mut expected = BTreeMap::<String, u64>::new();
            for token in &tokens {
                *expected.entry(token.clone()).or_default() += 1;
            }
            let mut admitted = 0usize;
            let (terms, length) = analyzer
                .document_terms::<()>(&text, |bytes| {
                    admitted += bytes;
                    Ok(())
                })
                .unwrap();
            assert_eq!((terms, length), (expected.clone(), tokens.len() as u64));
            assert_eq!(
                admitted,
                expected
                    .keys()
                    .map(|term| search_index_term_entry_bytes(term))
                    .sum::<usize>()
            );
        }
    }

    #[cfg(not(target_arch = "wasm32"))]
    #[test]
    fn analyzer_refuses_large_cjk_document_before_expanding_it() {
        // 1,000,000 distinct-ish ideographs (3 MB of TEXT) materialize about
        // two million tokens, tens of megabytes of transient strings. The
        // admission callback refuses at 64 KiB of distinct terms, and the
        // analysis must stop there without having expanded the rest.
        let analyzer = SearchIndexAnalyzer::CjkNgram;
        let text = distinct_cjk(1_000_000);
        let limit = 64 * 1024;
        let mut admitted = 0usize;
        let mut checks = 0usize;
        let observation = crate::test_allocator::AllocationObservation::start_all();
        let result = analyzer.document_terms(&text, |bytes| {
            checks += 1;
            if admitted + bytes > limit {
                return Err(admitted);
            }
            admitted += bytes;
            Ok(())
        });
        let allocated = observation.finish_bytes();
        assert!(result.is_err(), "the oversized document must be refused");
        assert!(admitted <= limit);
        // Refused within the first few thousand terms, not after two million.
        assert!(
            checks < 2 * limit / search_index_term_entry_bytes("数据"),
            "{checks}"
        );
        assert!(
            allocated < 4 * limit,
            "analysis allocated {allocated} bytes before refusing"
        );
    }

    #[test]
    fn analyzer_drops_oversized_tokens_and_keeps_names_stable() {
        let analyzer = SearchIndexAnalyzer::CjkNgram;
        let long = "x".repeat(SEARCH_INDEX_MAX_TOKEN_BYTES + 1);
        assert_eq!(analyzer.index_tokens(&format!("a {long} b")), ["a", "b"]);
        assert_eq!(SearchIndexAnalyzer::parse("CJK_NGRAM"), Some(analyzer));
        assert_eq!(SearchIndexAnalyzer::parse("jieba"), None);
        assert_eq!(
            SearchIndexAnalyzer::from_tag(analyzer.tag()),
            Some(analyzer)
        );
    }
}