pigeonhole-db 1.2.2 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Cargo.lock +16 -16
- data/Cargo.toml +5 -5
- data/lib/pigeonhole/version.rb +1 -1
- data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/io-event-1.22.3/IO_Event.so +0 -0
- data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/io-event-1.22.3/gem_make.out +6 -6
- data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/io-event-1.22.3/mkmf.log +26 -26
- data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/json-3.1.0/gem_make.out +6 -6
- data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/json-3.1.0/json/ext/generator.so +0 -0
- data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/json-3.1.0/json/ext/parser.so +0 -0
- data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/json-3.1.0/mkmf.log +19 -19
- data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/prism-1.9.0/gem_make.out +9 -9
- data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/prism-1.9.0/mkmf.log +6 -6
- data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/prism-1.9.0/prism/prism.so +0 -0
- data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/rbs-4.2.0/gem_make.out +6 -6
- data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/rbs-4.2.0/mkmf.log +7 -7
- data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/rbs-4.2.0/rbs_extension.so +0 -0
- data/vendor/bundle/ruby/3.4.0/gems/io-event-1.22.3/ext/Makefile +3 -3
- data/vendor/bundle/ruby/3.4.0/gems/io-event-1.22.3/lib/IO_Event.so +0 -0
- data/vendor/bundle/ruby/3.4.0/gems/json-3.1.0/ext/json/ext/generator/Makefile +3 -3
- data/vendor/bundle/ruby/3.4.0/gems/json-3.1.0/ext/json/ext/parser/Makefile +3 -3
- data/vendor/bundle/ruby/3.4.0/gems/json-3.1.0/lib/json/ext/generator.so +0 -0
- data/vendor/bundle/ruby/3.4.0/gems/json-3.1.0/lib/json/ext/parser.so +0 -0
- data/vendor/bundle/ruby/3.4.0/gems/prism-1.9.0/ext/prism/Makefile +3 -3
- data/vendor/bundle/ruby/3.4.0/gems/prism-1.9.0/lib/prism/prism.so +0 -0
- data/vendor/bundle/ruby/3.4.0/gems/rbs-4.2.0/ext/rbs_extension/Makefile +3 -3
- data/vendor/bundle/ruby/3.4.0/gems/rbs-4.2.0/lib/rbs_extension.so +0 -0
- data/vendor/cache/Cargo.toml +2 -2
- data/vendor/cache/src/lib.rs +37 -0
- data/vendor/cache/src/loom_tests.rs +32 -0
- data/vendor/cache/src/s3fifo.rs +239 -11
- data/vendor/cache/src/sync.rs +4 -4
- data/vendor/compaction/Cargo.toml +6 -6
- data/vendor/compaction/src/cursor.rs +86 -4
- data/vendor/compaction/src/job.rs +24 -2
- data/vendor/engine/Cargo.toml +11 -11
- data/vendor/engine/src/admission.rs +3 -1
- data/vendor/engine/src/catalog.rs +39 -4
- data/vendor/engine/src/check.rs +59 -20
- data/vendor/engine/src/compact.rs +2 -2
- data/vendor/engine/src/engine/hooks.rs +144 -2
- data/vendor/engine/src/engine/info.rs +6 -0
- data/vendor/engine/src/engine.rs +1492 -50
- data/vendor/engine/src/external_registry.rs +447 -0
- data/vendor/engine/src/feed.rs +313 -84
- data/vendor/engine/src/feed_reader/catalog.rs +490 -0
- data/vendor/engine/src/feed_reader/history.rs +236 -0
- data/vendor/engine/src/feed_reader/read_ahead.rs +241 -0
- data/vendor/engine/src/feed_reader/token.rs +232 -0
- data/vendor/engine/src/feed_reader.rs +970 -852
- data/vendor/engine/src/feed_stop_loom.rs +73 -0
- data/vendor/engine/src/flush.rs +29 -4
- data/vendor/engine/src/io_count.rs +190 -0
- data/vendor/engine/src/large.rs +4 -1
- data/vendor/engine/src/lib.rs +16 -2
- data/vendor/engine/src/maintenance.rs +2 -2
- data/vendor/engine/src/manifest.rs +38 -6
- data/vendor/engine/src/nonblocking.rs +3 -3
- data/vendor/engine/src/options.rs +26 -0
- data/vendor/engine/src/punch.rs +282 -0
- data/vendor/engine/src/range_copy.rs +365 -0
- data/vendor/engine/src/read/keys.rs +37 -5
- data/vendor/engine/src/read/reduce.rs +1 -0
- data/vendor/engine/src/read.rs +7 -0
- data/vendor/engine/src/row_cache.rs +24 -0
- data/vendor/engine/src/shard/tablets.rs +127 -31
- data/vendor/engine/src/shard.rs +1006 -33
- data/vendor/engine/src/source.rs +105 -12
- data/vendor/engine/src/write.rs +310 -4
- data/vendor/format/Cargo.toml +1 -1
- data/vendor/format/src/shm.rs +10 -0
- data/vendor/format/src/superblock.rs +9 -2
- data/vendor/format/src/wal.rs +818 -16
- data/vendor/io/Cargo.toml +3 -3
- data/vendor/io/src/count.rs +76 -0
- data/vendor/io/src/file.rs +70 -0
- data/vendor/io/src/lib.rs +2 -0
- data/vendor/io/src/os/unix.rs +124 -0
- data/vendor/io/src/os/windows.rs +51 -0
- data/vendor/io/src/pread.rs +26 -0
- data/vendor/io/src/sim.rs +81 -9
- data/vendor/io/src/sys.rs +53 -0
- data/vendor/io/src/uring.rs +28 -0
- data/vendor/io/src/vfs.rs +4 -0
- data/vendor/memory/Cargo.toml +4 -4
- data/vendor/memory/src/hooks.rs +6 -0
- data/vendor/memory/src/ivf.rs +67 -1
- data/vendor/memory/src/lib.rs +4 -1
- data/vendor/memory/src/recall/pool.rs +872 -0
- data/vendor/memory/src/recall.rs +351 -273
- data/vendor/memory/src/store.rs +15 -0
- data/vendor/memtable/Cargo.toml +3 -3
- data/vendor/pager/Cargo.toml +3 -3
- data/vendor/pager/src/alloc.rs +430 -0
- data/vendor/pager/src/lib.rs +114 -0
- data/vendor/pigeonhole/Cargo.toml +6 -5
- data/vendor/pigeonhole/src/db.rs +91 -2
- data/vendor/pigeonhole/src/error.rs +2 -1
- data/vendor/pigeonhole/src/feed.rs +210 -6
- data/vendor/pigeonhole/src/lib.rs +3 -0
- data/vendor/pigeonhole/src/metrics.rs +37 -0
- data/vendor/pigeonhole/src/nonblocking.rs +94 -111
- data/vendor/pigeonhole/src/options.rs +69 -0
- data/vendor/pigeonhole/src/replication.rs +601 -11
- data/vendor/pigeonhole/src/write.rs +81 -0
- data/vendor/runtime/Cargo.toml +2 -2
- data/vendor/shm/Cargo.toml +3 -3
- data/vendor/shm/src/region.rs +20 -2
- data/vendor/sst/Cargo.toml +4 -4
- data/vendor/sst/src/blob.rs +85 -7
- data/vendor/sst/src/iter.rs +98 -2
- data/vendor/sst/src/lib.rs +11 -2
- data/vendor/sst/src/writer.rs +12 -2
- data/vendor/text/Cargo.toml +2 -2
- data/vendor/text/src/block.rs +638 -0
- data/vendor/text/src/error.rs +8 -3
- data/vendor/text/src/fuzzy.rs +1 -1
- data/vendor/text/src/index.rs +1055 -80
- data/vendor/text/src/layout.rs +63 -2
- data/vendor/text/src/lib.rs +5 -1
- data/vendor/text/src/maxscore.rs +481 -94
- data/vendor/text/src/merged.rs +470 -0
- data/vendor/text/src/search.rs +1278 -341
- data/vendor/text/src/stats.rs +431 -0
- data/vendor/vector/Cargo.toml +2 -2
- data/vendor/vector/src/hooks.rs +33 -4
- data/vendor/vector/src/index.rs +498 -69
- data/vendor/wal/Cargo.toml +3 -3
- data/vendor/wal/src/feed.rs +313 -19
- data/vendor/wal/src/recovery.rs +31 -1
- data/vendor/wal/src/stream.rs +28 -0
- metadata +15 -1
data/vendor/text/src/index.rs
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
|
|
4
4
|
use std::borrow::Cow;
|
|
5
5
|
use std::collections::{BTreeMap, BTreeSet};
|
|
6
|
-
use std::ops::Bound;
|
|
7
|
-
use std::sync::Arc;
|
|
6
|
+
use std::ops::{Bound, ControlFlow};
|
|
7
|
+
use std::sync::{Arc, OnceLock};
|
|
8
8
|
|
|
9
9
|
use pigeonhole::{
|
|
10
10
|
CellRef, Family, Pigeonhole, PigeonholeReader, ReadTable, RetryPolicy, RowRead, Scan, Snapshot,
|
|
@@ -12,8 +12,10 @@ use pigeonhole::{
|
|
|
12
12
|
};
|
|
13
13
|
use unicode_normalization::{IsNormalized, UnicodeNormalization, is_nfc_quick, is_nfkc_quick};
|
|
14
14
|
|
|
15
|
+
use crate::block::{Block, Builder, MAX_POSTINGS, Posting};
|
|
15
16
|
use crate::error::{Error, Result, corrupt};
|
|
16
17
|
use crate::layout::*;
|
|
18
|
+
use crate::merged::find_block;
|
|
17
19
|
use crate::query::Query;
|
|
18
20
|
use crate::search::Search;
|
|
19
21
|
use crate::tokenizer::{Standard, Tokenizer};
|
|
@@ -22,6 +24,13 @@ use crate::tokenizer::{Standard, Tokenizer};
|
|
|
22
24
|
/// (#1514): its view lives for the page's scan only.
|
|
23
25
|
const REBUILD_PAGE: u64 = 256;
|
|
24
26
|
|
|
27
|
+
/// The most posting cells one chunk of a pack takes: one transaction (#1585).
|
|
28
|
+
const PACK_CHUNK: usize = 512;
|
|
29
|
+
/// The fewest postings a pack makes a block of where the term has no block near them: fewer
|
|
30
|
+
/// stay cells until more have gathered, so a term that grows a few postings between packs
|
|
31
|
+
/// isn't cut into blocks of a few.
|
|
32
|
+
const PACK_MIN_RUN: usize = 32;
|
|
33
|
+
|
|
25
34
|
/// A full-text index over one source column (D309).
|
|
26
35
|
///
|
|
27
36
|
/// Clones share one handle. Every write to the source column must go through the index
|
|
@@ -56,6 +65,9 @@ pub(crate) struct Inner {
|
|
|
56
65
|
pub(crate) tok: Box<dyn Tokenizer>,
|
|
57
66
|
/// The built-in tokeniser lowercases again after NFKC (D309 rev 6 §A.2).
|
|
58
67
|
builtin: bool,
|
|
68
|
+
/// `table` opened again, for a handle older than the index's first pack: a table handle
|
|
69
|
+
/// knows the families of when it was opened, and the first pack adds [`POSTB`].
|
|
70
|
+
fresh: OnceLock<Rows>,
|
|
59
71
|
}
|
|
60
72
|
|
|
61
73
|
/// An [`Index`]'s write handles.
|
|
@@ -63,6 +75,8 @@ struct Writer {
|
|
|
63
75
|
db: Pigeonhole,
|
|
64
76
|
table: Table,
|
|
65
77
|
src: Table,
|
|
78
|
+
/// `table` with the block family ([`Inner::fresh`]'s write side).
|
|
79
|
+
fresh: OnceLock<Table>,
|
|
66
80
|
}
|
|
67
81
|
|
|
68
82
|
/// A database handle for reads: a writer's, or a reader process's.
|
|
@@ -200,6 +214,12 @@ pub struct IndexStats {
|
|
|
200
214
|
pub source_mismatch: u64,
|
|
201
215
|
/// Source cells that aren't valid UTF-8 (skipped).
|
|
202
216
|
pub invalid_utf8: u64,
|
|
217
|
+
/// Postings held in packed blocks (a version 2 index, [`Index::pack`]); the rest of
|
|
218
|
+
/// `postings` are cells.
|
|
219
|
+
pub packed: u64,
|
|
220
|
+
/// Deleted postings not yet removed by a pack (a version 2 index). Information, not a
|
|
221
|
+
/// mismatch: they cost a search a step each until the next pack.
|
|
222
|
+
pub tombstones: u64,
|
|
203
223
|
}
|
|
204
224
|
|
|
205
225
|
impl IndexStats {
|
|
@@ -219,6 +239,96 @@ pub struct RebuildStats {
|
|
|
219
239
|
pub changed: u64,
|
|
220
240
|
/// Source cells that aren't valid UTF-8 (skipped).
|
|
221
241
|
pub invalid_utf8: u64,
|
|
242
|
+
/// Whether the step of [`Index::rebuild_with`] or [`Index::resume_rebuild_with`] stopped
|
|
243
|
+
/// the walk before its end (#1756). The index is consistent, and a later
|
|
244
|
+
/// [`Index::resume_rebuild_with`] carries on.
|
|
245
|
+
pub stopped: bool,
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/// How [`Index::pack_with`] runs.
|
|
249
|
+
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
|
|
250
|
+
#[non_exhaustive]
|
|
251
|
+
pub struct PackOptions {
|
|
252
|
+
with_readers: bool,
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
impl PackOptions {
|
|
256
|
+
/// The defaults: an index's first pack is refused while reader processes are attached.
|
|
257
|
+
pub fn new() -> Self {
|
|
258
|
+
Self::default()
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/// Lets an index's first pack run while reader processes are attached to the database
|
|
262
|
+
/// file. Default off.
|
|
263
|
+
///
|
|
264
|
+
/// The first pack moves the index to layout version 2, which a 1.2 build can't read: one
|
|
265
|
+
/// that has the index open goes on running and fails its searches with `Busy`. The
|
|
266
|
+
/// database records that a reader is attached, not which build it is, so the pack
|
|
267
|
+
/// refuses any ([`Error::PackRefused`]). Set this when every attached reader is 1.3 or
|
|
268
|
+
/// later.
|
|
269
|
+
pub fn with_readers(mut self, allow: bool) -> Self {
|
|
270
|
+
self.with_readers = allow;
|
|
271
|
+
self
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/// What [`Index::pack`] did.
|
|
276
|
+
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
|
|
277
|
+
#[non_exhaustive]
|
|
278
|
+
pub struct PackStats {
|
|
279
|
+
/// Terms it took posting cells of.
|
|
280
|
+
pub terms: u64,
|
|
281
|
+
/// Postings it moved from cells into blocks.
|
|
282
|
+
pub postings: u64,
|
|
283
|
+
/// Deleted postings it removed (tombstones, with the packed postings they hid).
|
|
284
|
+
pub tombstones: u64,
|
|
285
|
+
/// Whether the step of [`Index::pack_with`] stopped it before the end. The index is
|
|
286
|
+
/// consistent, and the next pack carries on from where it stopped.
|
|
287
|
+
pub stopped: bool,
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/// What a pack's chunk found to do.
|
|
291
|
+
struct PackWork {
|
|
292
|
+
term: Vec<u8>,
|
|
293
|
+
/// The term's posting cells past the cursor: id, timestamp, value.
|
|
294
|
+
cells: Vec<(Vec<u8>, u64, u64)>,
|
|
295
|
+
/// Their position cells: id to timestamp and value.
|
|
296
|
+
positions: BTreeMap<Vec<u8>, (u64, Vec<u8>)>,
|
|
297
|
+
/// The blocks their ids fall among, as read: qualifier and value.
|
|
298
|
+
blocks: Vec<(Vec<u8>, Vec<u8>)>,
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/// A posting a pack holds between reading and writing.
|
|
302
|
+
struct Packed {
|
|
303
|
+
tf: u32,
|
|
304
|
+
len: u32,
|
|
305
|
+
positions: Vec<u8>,
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/// Whether the index is version 2, from its state cell's value ([`REPAIR`]): `Busy` while a
|
|
309
|
+
/// repair runs.
|
|
310
|
+
pub(crate) fn packed_state(value: &[u8]) -> Result<bool> {
|
|
311
|
+
if value == PACKED {
|
|
312
|
+
Ok(true)
|
|
313
|
+
} else {
|
|
314
|
+
Err(Error::Busy)
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/// Which walk [`Index::rebuild_inner`] makes.
|
|
319
|
+
enum Walk {
|
|
320
|
+
/// A rebuild, which keeps a cursor; from the row after `from`, if it resumes (#1756).
|
|
321
|
+
Rebuild { from: Option<Vec<u8>> },
|
|
322
|
+
/// A repair's, from an emptied table: no cursor, read or written.
|
|
323
|
+
Repair,
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/// How a page of a rebuild ended.
|
|
327
|
+
enum Paged {
|
|
328
|
+
/// Its step stopped the walk.
|
|
329
|
+
Stopped,
|
|
330
|
+
/// Every document done; whether the last one's transaction wrote the cursor.
|
|
331
|
+
Done { cursor_at_last: bool },
|
|
222
332
|
}
|
|
223
333
|
|
|
224
334
|
/// A transaction body's failure: the database's error (retried as `transact` retries), or
|
|
@@ -278,7 +388,7 @@ impl Index {
|
|
|
278
388
|
let tok = Box::new(Standard {
|
|
279
389
|
stop_words: opts.get_stop_words(),
|
|
280
390
|
});
|
|
281
|
-
Self::create_inner(db, name, source, opts, tok, true)
|
|
391
|
+
Self::create_inner(db, name, source, opts, tok, true, true)
|
|
282
392
|
}
|
|
283
393
|
|
|
284
394
|
/// [`create`](Self::create) with a custom tokeniser. Its name and fingerprint are stored,
|
|
@@ -297,7 +407,7 @@ impl Index {
|
|
|
297
407
|
Standard::NAME
|
|
298
408
|
)));
|
|
299
409
|
}
|
|
300
|
-
Self::create_inner(db, name, source, options.checked()?, tokenizer, false)
|
|
410
|
+
Self::create_inner(db, name, source, options.checked()?, tokenizer, false, true)
|
|
301
411
|
}
|
|
302
412
|
|
|
303
413
|
fn create_inner(
|
|
@@ -307,6 +417,7 @@ impl Index {
|
|
|
307
417
|
opts: IndexOptions,
|
|
308
418
|
tok: Box<dyn Tokenizer>,
|
|
309
419
|
builtin: bool,
|
|
420
|
+
fill: bool,
|
|
310
421
|
) -> Result<Index> {
|
|
311
422
|
if source.table == name {
|
|
312
423
|
return Err(Error::InvalidArgument(
|
|
@@ -335,7 +446,7 @@ impl Index {
|
|
|
335
446
|
}
|
|
336
447
|
let table = builder.create()?;
|
|
337
448
|
let (major, minor, update) = unicode_normalization::UNICODE_VERSION;
|
|
338
|
-
table
|
|
449
|
+
let mut config = table
|
|
339
450
|
.mutate(GLOBAL)
|
|
340
451
|
.put_u64(META, LAYOUT, LAYOUT_VERSION)
|
|
341
452
|
.put(META, OPTS, &opts.encode())
|
|
@@ -347,8 +458,15 @@ impl Index {
|
|
|
347
458
|
META,
|
|
348
459
|
NORMALIZER_UNICODE,
|
|
349
460
|
format!("{major}.{minor}.{update}").as_bytes(),
|
|
350
|
-
)
|
|
351
|
-
|
|
461
|
+
);
|
|
462
|
+
// Unfilled: a rebuild from the top is pending (the empty cursor), with the config,
|
|
463
|
+
// and marked as the fill (#1722 PR 2c).
|
|
464
|
+
if !fill {
|
|
465
|
+
config = config
|
|
466
|
+
.put(META, REBUILD_AFTER, b"")
|
|
467
|
+
.put(META, UNFILLED, b"");
|
|
468
|
+
}
|
|
469
|
+
config.commit()?;
|
|
352
470
|
let index = Index {
|
|
353
471
|
inner: Arc::new(Inner {
|
|
354
472
|
db: Handle::W(db.clone()),
|
|
@@ -358,17 +476,74 @@ impl Index {
|
|
|
358
476
|
db: db.clone(),
|
|
359
477
|
table,
|
|
360
478
|
src,
|
|
479
|
+
fresh: OnceLock::new(),
|
|
361
480
|
}),
|
|
362
481
|
source,
|
|
363
482
|
opts,
|
|
364
483
|
tok,
|
|
365
484
|
builtin,
|
|
485
|
+
fresh: OnceLock::new(),
|
|
366
486
|
}),
|
|
367
487
|
};
|
|
368
|
-
|
|
488
|
+
if fill {
|
|
489
|
+
index.rebuild_inner(Walk::Rebuild { from: None }, |_| ControlFlow::Continue(()))?;
|
|
490
|
+
}
|
|
369
491
|
Ok(index)
|
|
370
492
|
}
|
|
371
493
|
|
|
494
|
+
/// The text indexes in `db` whose source column is in the table `table` (#1916): the
|
|
495
|
+
/// indexes dropping that table would leave behind, unable to write, rebuild or find
|
|
496
|
+
/// their documents. Sorted. A table without the index's families is skipped without a
|
|
497
|
+
/// read; one with them is read for its source, whatever its tokeniser.
|
|
498
|
+
pub fn over_table(db: &Pigeonhole, table: &str) -> Result<Vec<String>> {
|
|
499
|
+
let mut out = Vec::new();
|
|
500
|
+
for name in db.tables() {
|
|
501
|
+
if name == table {
|
|
502
|
+
continue;
|
|
503
|
+
}
|
|
504
|
+
// A table dropped since the listing has nothing to leave behind.
|
|
505
|
+
let rows = match db.table(&name).and_then(|t| t.open()) {
|
|
506
|
+
Ok(t) => Rows::W(t),
|
|
507
|
+
Err(e) if e.code() == pigeonhole::ErrorCode::TableNotFound => continue,
|
|
508
|
+
Err(e) => return Err(e.into()),
|
|
509
|
+
};
|
|
510
|
+
if [POST, DF, TERMS, META, STATS]
|
|
511
|
+
.iter()
|
|
512
|
+
.any(|f| rows.family_info(f).is_none())
|
|
513
|
+
{
|
|
514
|
+
continue;
|
|
515
|
+
}
|
|
516
|
+
let found = rows
|
|
517
|
+
.get(GLOBAL, META, SOURCE)?
|
|
518
|
+
.and_then(|c| Source::decode(c.value()));
|
|
519
|
+
if found.is_some_and(|s| s.table == table) {
|
|
520
|
+
out.push(name);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
out.sort();
|
|
524
|
+
Ok(out)
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/// [`create`](Self::create) without the backfill (#1722): the index starts empty, with a
|
|
528
|
+
/// rebuild from the top pending ([`rebuild_pending`](Self::rebuild_pending) is true), which
|
|
529
|
+
/// [`resume_rebuild_with`](Self::resume_rebuild_with) then runs in steps. Writes through
|
|
530
|
+
/// the index meanwhile are indexed at once. For a server that fills an index in the
|
|
531
|
+
/// background, a document at a time, rather than inside the create (a cluster's
|
|
532
|
+
/// partitions create their indexes when they take the map). Until the rebuild finishes,
|
|
533
|
+
/// the index lacks the source rows written before the create.
|
|
534
|
+
pub fn create_unfilled(
|
|
535
|
+
db: &Pigeonhole,
|
|
536
|
+
name: &str,
|
|
537
|
+
source: Source,
|
|
538
|
+
options: IndexOptions,
|
|
539
|
+
) -> Result<Index> {
|
|
540
|
+
let opts = options.checked()?;
|
|
541
|
+
let tok = Box::new(Standard {
|
|
542
|
+
stop_words: opts.get_stop_words(),
|
|
543
|
+
});
|
|
544
|
+
Self::create_inner(db, name, source, opts, tok, true, false)
|
|
545
|
+
}
|
|
546
|
+
|
|
372
547
|
/// Opens the index `name`, with the built-in tokeniser. Everything else is read back
|
|
373
548
|
/// from the index. `Mismatch` if it was created with a custom tokeniser, or with the
|
|
374
549
|
/// built-in one at another version (a dependency bump that changed its word boundaries:
|
|
@@ -405,11 +580,13 @@ impl Index {
|
|
|
405
580
|
db: db.clone(),
|
|
406
581
|
table,
|
|
407
582
|
src,
|
|
583
|
+
fresh: OnceLock::new(),
|
|
408
584
|
}),
|
|
409
585
|
source,
|
|
410
586
|
opts,
|
|
411
587
|
tok,
|
|
412
588
|
builtin,
|
|
589
|
+
fresh: OnceLock::new(),
|
|
413
590
|
}),
|
|
414
591
|
})
|
|
415
592
|
}
|
|
@@ -439,6 +616,53 @@ impl Index {
|
|
|
439
616
|
.expect("an Index is built with its write handles")
|
|
440
617
|
}
|
|
441
618
|
|
|
619
|
+
/// The index's table for reads: with the block family in a version 2 index, which a
|
|
620
|
+
/// handle opened before the first pack gets by opening the table again, once.
|
|
621
|
+
#[inline]
|
|
622
|
+
pub(crate) fn rows(&self, packed: bool) -> Result<&Rows> {
|
|
623
|
+
if !packed {
|
|
624
|
+
return Ok(&self.inner.table);
|
|
625
|
+
}
|
|
626
|
+
self.rows_packed()
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
/// [`rows`](Self::rows) for a version 2 index, out of line.
|
|
630
|
+
#[inline(never)]
|
|
631
|
+
fn rows_packed(&self) -> Result<&Rows> {
|
|
632
|
+
let inn = &*self.inner;
|
|
633
|
+
if inn.table.family_info(POSTB).is_some() {
|
|
634
|
+
return Ok(&inn.table);
|
|
635
|
+
}
|
|
636
|
+
if let Some(rows) = inn.fresh.get() {
|
|
637
|
+
return Ok(rows);
|
|
638
|
+
}
|
|
639
|
+
let rows = match &inn.db {
|
|
640
|
+
Handle::W(db) => Rows::W(db.table(inn.table.name())?.open()?),
|
|
641
|
+
Handle::R(db) => Rows::R(db.table(inn.table.name())?),
|
|
642
|
+
};
|
|
643
|
+
if rows.family_info(POSTB).is_none() {
|
|
644
|
+
return Err(corrupt("a version 2 index has no block family"));
|
|
645
|
+
}
|
|
646
|
+
Ok(inn.fresh.get_or_init(|| rows))
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
/// The index's table for writes of blocks, and reads of them in a transaction: the block
|
|
650
|
+
/// family is added if the table lacks it (a pack's first step).
|
|
651
|
+
fn block_table(&self) -> Result<&Table> {
|
|
652
|
+
let w = self.w();
|
|
653
|
+
if w.table.family_info(POSTB).is_some() {
|
|
654
|
+
return Ok(&w.table);
|
|
655
|
+
}
|
|
656
|
+
if let Some(t) = w.fresh.get() {
|
|
657
|
+
return Ok(t);
|
|
658
|
+
}
|
|
659
|
+
let t =
|
|
660
|
+
w.db.table(w.table.name())?
|
|
661
|
+
.family(POSTB, Family::default().max_versions(1))
|
|
662
|
+
.open()?;
|
|
663
|
+
Ok(w.fresh.get_or_init(|| t))
|
|
664
|
+
}
|
|
665
|
+
|
|
442
666
|
/// The index's options.
|
|
443
667
|
pub fn options(&self) -> IndexOptions {
|
|
444
668
|
self.inner.opts
|
|
@@ -467,7 +691,7 @@ impl Index {
|
|
|
467
691
|
/// written. It fails with `Busy` while a [`repair`](Self::repair) runs: retry after it
|
|
468
692
|
/// finishes.
|
|
469
693
|
pub fn put_in(&self, txn: &mut Transaction, doc: &[u8], text: &str) -> Result<()> {
|
|
470
|
-
self.
|
|
694
|
+
let packed = self.state_in(txn)?;
|
|
471
695
|
let inn = &*self.inner;
|
|
472
696
|
let w = self.w();
|
|
473
697
|
let stored = txn
|
|
@@ -485,7 +709,7 @@ impl Index {
|
|
|
485
709
|
&inn.source.qualifier,
|
|
486
710
|
text.as_bytes(),
|
|
487
711
|
);
|
|
488
|
-
self.apply(txn, doc, &old, &new, Mode::Put)?;
|
|
712
|
+
self.apply(txn, doc, &old, &new, Mode::Put, packed)?;
|
|
489
713
|
Ok(())
|
|
490
714
|
}
|
|
491
715
|
|
|
@@ -494,7 +718,7 @@ impl Index {
|
|
|
494
718
|
/// cell is deleted either way. It fails with `Busy` while a [`repair`](Self::repair) runs:
|
|
495
719
|
/// retry after it finishes.
|
|
496
720
|
pub fn delete_in(&self, txn: &mut Transaction, doc: &[u8]) -> Result<bool> {
|
|
497
|
-
self.
|
|
721
|
+
let packed = self.state_in(txn)?;
|
|
498
722
|
let inn = &*self.inner;
|
|
499
723
|
let w = self.w();
|
|
500
724
|
let old = self.read_forward(txn, doc)?;
|
|
@@ -507,10 +731,7 @@ impl Index {
|
|
|
507
731
|
let t = &w.table;
|
|
508
732
|
for term in old.terms.keys() {
|
|
509
733
|
let row = [POST_PREFIX, term].concat();
|
|
510
|
-
txn
|
|
511
|
-
if inn.opts.get_positions() {
|
|
512
|
-
txn.delete_column(t, &row, POS, doc);
|
|
513
|
-
}
|
|
734
|
+
Self::unpost(txn, t, &row, doc, inn.opts.get_positions(), packed);
|
|
514
735
|
txn.incr(t, &row, DF, N, -1);
|
|
515
736
|
}
|
|
516
737
|
txn.delete_row(t, &fwd_row(doc))
|
|
@@ -561,8 +782,466 @@ impl Index {
|
|
|
561
782
|
/// length) trust `F{doc}` as the record of what was indexed, so after a write around the
|
|
562
783
|
/// index's own rows (not just the source column), use `repair`, which starts from an
|
|
563
784
|
/// emptied table. It fails with `Busy` while a repair runs.
|
|
785
|
+
///
|
|
786
|
+
/// It starts from the first source row, and drops the cursor a stopped
|
|
787
|
+
/// [`rebuild_with`](Self::rebuild_with) left: it covers every source row as of the call.
|
|
564
788
|
pub fn rebuild(&self) -> Result<RebuildStats> {
|
|
565
|
-
self.
|
|
789
|
+
self.rebuild_with(|_| ControlFlow::Continue(()))
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
/// [`rebuild`](Self::rebuild), calling `step` after each document's transaction with the
|
|
793
|
+
/// counts so far (#1756). A `Break` stops the walk there: the index is consistent (each
|
|
794
|
+
/// document is its own transaction), the result has `stopped` set, and
|
|
795
|
+
/// [`resume_rebuild_with`](Self::resume_rebuild_with) carries on from the cursor the walk
|
|
796
|
+
/// keeps in the index. The cursor is written once a page, and in the transaction of any
|
|
797
|
+
/// document that writes anyway, so a document already right still writes nothing, and a
|
|
798
|
+
/// resume redoes at most one page of documents. A walk costs two commits more: the empty
|
|
799
|
+
/// cursor at its start, and the cursor's removal at its end.
|
|
800
|
+
///
|
|
801
|
+
/// Like `rebuild`, it starts from the first source row, replacing any earlier cursor with
|
|
802
|
+
/// an empty one ("from the top") in its first commit: a stop or a crash before its first
|
|
803
|
+
/// page's cursor still leaves it pending.
|
|
804
|
+
pub fn rebuild_with(
|
|
805
|
+
&self,
|
|
806
|
+
step: impl FnMut(&RebuildStats) -> ControlFlow<()>,
|
|
807
|
+
) -> Result<RebuildStats> {
|
|
808
|
+
// A stopped pack's cursor goes too: the rebuild writes cells behind it, and the next
|
|
809
|
+
// pack starts over (#1585).
|
|
810
|
+
self.w()
|
|
811
|
+
.table
|
|
812
|
+
.mutate(GLOBAL)
|
|
813
|
+
.put(META, REBUILD_AFTER, b"")
|
|
814
|
+
.delete_column(META, PACK_AFTER)
|
|
815
|
+
.commit()?;
|
|
816
|
+
self.rebuild_inner(Walk::Rebuild { from: None }, step)
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
/// Carries on a rebuild that [`rebuild_with`](Self::rebuild_with)'s step stopped, after
|
|
820
|
+
/// the last row its cursor names (#1756); nothing if none is pending. It ends as that
|
|
821
|
+
/// rebuild would have: documents before the cursor were reconciled when it visited them,
|
|
822
|
+
/// and writes through the index have kept them so since. A write around the index to a
|
|
823
|
+
/// row before the cursor is missed, as it would be by one rebuild that had passed that
|
|
824
|
+
/// row: call [`rebuild`](Self::rebuild) (which starts over) or [`repair`](Self::repair)
|
|
825
|
+
/// after such writes, never this.
|
|
826
|
+
pub fn resume_rebuild_with(
|
|
827
|
+
&self,
|
|
828
|
+
step: impl FnMut(&RebuildStats) -> ControlFlow<()>,
|
|
829
|
+
) -> Result<RebuildStats> {
|
|
830
|
+
let Some(from) = self.rebuild_cursor()? else {
|
|
831
|
+
return Ok(RebuildStats::default());
|
|
832
|
+
};
|
|
833
|
+
// An empty cursor: stopped before its first page's, so from the top.
|
|
834
|
+
let from = (!from.is_empty()).then_some(from);
|
|
835
|
+
self.rebuild_inner(Walk::Rebuild { from }, step)
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
/// The text indexes in `db`, by name (#1585): for a server that packs them in the
|
|
839
|
+
/// background. A catalog listing: each table with the index's families, none of them
|
|
840
|
+
/// opened as an index. A table dropped since the catalog was listed is passed over.
|
|
841
|
+
pub fn list(db: &Pigeonhole) -> Result<Vec<String>> {
|
|
842
|
+
Ok(Self::index_tables(db)?
|
|
843
|
+
.into_iter()
|
|
844
|
+
.map(|(n, _)| n)
|
|
845
|
+
.collect())
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
/// The tables of `db` that have a text index's families, with their names.
|
|
849
|
+
fn index_tables(db: &Pigeonhole) -> Result<Vec<(String, Table)>> {
|
|
850
|
+
let mut out = Vec::new();
|
|
851
|
+
for name in db.tables() {
|
|
852
|
+
let t = match db.table(&name).and_then(|b| b.open()) {
|
|
853
|
+
Ok(t) => t,
|
|
854
|
+
Err(e) if e.code() == pigeonhole::ErrorCode::TableNotFound => continue,
|
|
855
|
+
Err(e) => return Err(e.into()),
|
|
856
|
+
};
|
|
857
|
+
let families = t.families();
|
|
858
|
+
if [POST, DF, TERMS, META, STATS]
|
|
859
|
+
.iter()
|
|
860
|
+
.all(|f| families.contains(f))
|
|
861
|
+
{
|
|
862
|
+
out.push((name, t));
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
Ok(out)
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
/// The text indexes in `db` with a stopped rebuild pending (#1756): for a server to
|
|
869
|
+
/// resume them. A point read per table with the index's families; a table dropped since
|
|
870
|
+
/// the catalog was listed is passed over.
|
|
871
|
+
pub fn pending_rebuilds(db: &Pigeonhole) -> Result<Vec<String>> {
|
|
872
|
+
let mut out = Vec::new();
|
|
873
|
+
for (name, t) in Self::index_tables(db)? {
|
|
874
|
+
if t.get(GLOBAL, META, REBUILD_AFTER)?.is_some() {
|
|
875
|
+
out.push(name);
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
Ok(out)
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
/// Whether a stopped rebuild is pending (a cursor is kept): one point read.
|
|
882
|
+
pub fn rebuild_pending(&self) -> Result<bool> {
|
|
883
|
+
Ok(self.rebuild_cursor()?.is_some())
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
/// Whether the index was created unfilled ([`create_unfilled`](Self::create_unfilled))
|
|
887
|
+
/// and no walk of its whole source has finished since (#1722 PR 2c): one point read.
|
|
888
|
+
/// Until then it lacks source rows written before its create, so a search's answer is
|
|
889
|
+
/// short. [`rebuild_pending`](Self::rebuild_pending) is true then too, but also while
|
|
890
|
+
/// a client's [`rebuild`](Self::rebuild) runs or is stopped part way, when the index
|
|
891
|
+
/// is as it was before the rebuild except for the documents it has reconciled: whole,
|
|
892
|
+
/// unless rows were written around it.
|
|
893
|
+
pub fn fill_pending(&self) -> Result<bool> {
|
|
894
|
+
Ok(self.w().table.get(GLOBAL, META, UNFILLED)?.is_some())
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
fn rebuild_cursor(&self) -> Result<Option<Vec<u8>>> {
|
|
898
|
+
Ok(self
|
|
899
|
+
.w()
|
|
900
|
+
.table
|
|
901
|
+
.get(GLOBAL, META, REBUILD_AFTER)?
|
|
902
|
+
.map(|c| c.value().to_vec()))
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
/// Packs the index's postings into blocks (#1585, ICR 0143): each term's posting cells,
|
|
906
|
+
/// one a document, are moved into blocks of up to 128 postings under them, which a
|
|
907
|
+
/// search of a common word reads several times faster. Searches and writes go on while
|
|
908
|
+
/// it runs, and see the same postings throughout.
|
|
909
|
+
///
|
|
910
|
+
/// **Nothing else packs an index.** The first pack moves the index to layout version 2,
|
|
911
|
+
/// which a 1.2 build can't open: pack only once you won't roll back to 1.2. A 1.2
|
|
912
|
+
/// build that already has the index open fails its searches and writes with `Busy`
|
|
913
|
+
/// from then on. So the first pack is refused ([`Error::PackRefused`]) while reader
|
|
914
|
+
/// processes are attached to the file, unless [`PackOptions::with_readers`] allows it.
|
|
915
|
+
///
|
|
916
|
+
/// **What it does.** Chunk by chunk (a transaction each), it takes a term's posting
|
|
917
|
+
/// cells, merges them with the blocks their ids fall among, writes the blocks again, and
|
|
918
|
+
/// removes the cells it read (each only if it is still as read, so a write that lands
|
|
919
|
+
/// meanwhile stays, and wins). Deleted postings ([`IndexStats::tombstones`]) are removed
|
|
920
|
+
/// with what they hid. A few postings with no block near them stay cells until more have
|
|
921
|
+
/// gathered. Writes after a pack are cells again, until the next one.
|
|
922
|
+
///
|
|
923
|
+
/// It carries on from where a stopped pack left off ([`pack_with`](Self::pack_with)),
|
|
924
|
+
/// and otherwise starts at the first term. `PackRefused` while a rebuild is pending
|
|
925
|
+
/// ([`rebuild_pending`](Self::rebuild_pending)): finish the rebuild first. `Busy` while
|
|
926
|
+
/// a repair runs.
|
|
927
|
+
pub fn pack(&self) -> Result<PackStats> {
|
|
928
|
+
self.pack_with(PackOptions::new(), |_| ControlFlow::Continue(()))
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
/// [`pack`](Self::pack) with `options`, calling `step` after each chunk with the counts
|
|
932
|
+
/// so far. A `Break` stops it there: the index is consistent, the result has `stopped`
|
|
933
|
+
/// set, [`pack_pending`](Self::pack_pending) is true, and the next pack carries on from
|
|
934
|
+
/// the cursor it keeps in the index.
|
|
935
|
+
pub fn pack_with(
|
|
936
|
+
&self,
|
|
937
|
+
options: PackOptions,
|
|
938
|
+
mut step: impl FnMut(&PackStats) -> ControlFlow<()>,
|
|
939
|
+
) -> Result<PackStats> {
|
|
940
|
+
self.pack_begin(options)?;
|
|
941
|
+
let mut stats = PackStats::default();
|
|
942
|
+
let (mut term, mut at) = (None, None);
|
|
943
|
+
while self.pack_chunk(&mut stats, &mut term, &mut at)? {
|
|
944
|
+
if step(&stats).is_break() {
|
|
945
|
+
stats.stopped = true;
|
|
946
|
+
break;
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
Ok(stats)
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
/// Whether a stopped pack is pending (a cursor is kept): one point read.
|
|
953
|
+
pub fn pack_pending(&self) -> Result<bool> {
|
|
954
|
+
Ok(self.w().table.get(GLOBAL, META, PACK_AFTER)?.is_some())
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
/// A pack's first commit: the index becomes version 2 if it wasn't, and a pass's cursor
|
|
958
|
+
/// is put (empty: from the top) unless a stopped pack left one.
|
|
959
|
+
fn pack_begin(&self, options: PackOptions) -> Result<()> {
|
|
960
|
+
let w = self.w();
|
|
961
|
+
if w.table.get(GLOBAL, META, REPAIR)?.is_none() && !options.with_readers {
|
|
962
|
+
// Version 1 still. The check and the commit below aren't one step: a reader that
|
|
963
|
+
// attaches between them isn't seen (the guide's rule covers that, not this).
|
|
964
|
+
let readers = w.db.engine_metrics().reader_slots_active;
|
|
965
|
+
if readers > 0 {
|
|
966
|
+
return Err(Error::PackRefused(format!(
|
|
967
|
+
"{readers} reader process(es) are attached to the database file, and the \
|
|
968
|
+
first pack moves the index to a layout a 1.2 build can't read. Close \
|
|
969
|
+
them (a reader that exited without closing holds its slot until the \
|
|
970
|
+
writer reclaims it), or allow it with PackOptions::with_readers once \
|
|
971
|
+
every reader is 1.3 or later"
|
|
972
|
+
)));
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
// The block family, before anything can name it.
|
|
976
|
+
self.block_table()?;
|
|
977
|
+
let t = &w.table;
|
|
978
|
+
w.db.transact(&RetryPolicy::default(), |tx| {
|
|
979
|
+
if tx.get(t, GLOBAL, META, REBUILD_AFTER)?.is_some() {
|
|
980
|
+
return Err(Tx::Fail(Error::PackRefused(
|
|
981
|
+
"a rebuild is pending: finish it first".into(),
|
|
982
|
+
)));
|
|
983
|
+
}
|
|
984
|
+
if !self.state_in(tx)? {
|
|
985
|
+
tx.put_u64(t, GLOBAL, META, LAYOUT, LAYOUT_PACKED)
|
|
986
|
+
.put(t, GLOBAL, META, REPAIR, PACKED);
|
|
987
|
+
}
|
|
988
|
+
if tx.get(t, GLOBAL, META, PACK_AFTER)?.is_none() {
|
|
989
|
+
tx.put(t, GLOBAL, META, PACK_AFTER, b"");
|
|
990
|
+
}
|
|
991
|
+
Ok(())
|
|
992
|
+
})?;
|
|
993
|
+
Ok(())
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
/// One chunk of a pack: `false` once the pass is done (or another pack finished it).
|
|
997
|
+
/// `term` is the term the last chunk was in, for the count of terms.
|
|
998
|
+
///
|
|
999
|
+
/// `at` is how far the pass has read past the stored cursor (with that cursor): chunks
|
|
1000
|
+
/// with nothing to write, which are most terms of a real vocabulary (a few postings
|
|
1001
|
+
/// each, left as cells), commit nothing, the cursor included. A pass that stops there
|
|
1002
|
+
/// reads them again. After 64 such chunks it returns, so the caller's step still runs.
|
|
1003
|
+
fn pack_chunk(
|
|
1004
|
+
&self,
|
|
1005
|
+
stats: &mut PackStats,
|
|
1006
|
+
term: &mut Option<Vec<u8>>,
|
|
1007
|
+
at: &mut Option<(Vec<u8>, Vec<u8>)>,
|
|
1008
|
+
) -> Result<bool> {
|
|
1009
|
+
let w = self.w();
|
|
1010
|
+
let t = self.block_table()?;
|
|
1011
|
+
let rows = self.rows(true)?;
|
|
1012
|
+
let positions = self.inner.opts.get_positions();
|
|
1013
|
+
// The most cells the chunk takes: cut when its transaction runs out of retries, so
|
|
1014
|
+
// a pack beside steady writers of the same documents still gets through.
|
|
1015
|
+
let mut limit = PACK_CHUNK;
|
|
1016
|
+
let mut idle = 0;
|
|
1017
|
+
loop {
|
|
1018
|
+
let Some(cursor) = t.get(GLOBAL, META, PACK_AFTER)?.map(|c| c.value().to_vec()) else {
|
|
1019
|
+
return Ok(false);
|
|
1020
|
+
};
|
|
1021
|
+
let from = match at {
|
|
1022
|
+
Some((stored, read)) if *stored == cursor => read.clone(),
|
|
1023
|
+
_ => cursor.clone(),
|
|
1024
|
+
};
|
|
1025
|
+
// The chunk's cells and blocks at one snapshot, dropped before the transaction
|
|
1026
|
+
// (#1514).
|
|
1027
|
+
let work = {
|
|
1028
|
+
let s = w.db.snapshot()?;
|
|
1029
|
+
self.pack_read(rows, &s, &from, limit)?
|
|
1030
|
+
};
|
|
1031
|
+
let Some(work) = work else {
|
|
1032
|
+
// The pass is done: the cursor goes, unless it moved (another pack's).
|
|
1033
|
+
w.db.transact(&RetryPolicy::default(), |tx| {
|
|
1034
|
+
let same = tx
|
|
1035
|
+
.get(t, GLOBAL, META, PACK_AFTER)?
|
|
1036
|
+
.is_some_and(|c| c.value() == cursor.as_slice());
|
|
1037
|
+
if same {
|
|
1038
|
+
tx.delete_column(t, GLOBAL, META, PACK_AFTER);
|
|
1039
|
+
}
|
|
1040
|
+
Ok::<_, Tx>(())
|
|
1041
|
+
})?;
|
|
1042
|
+
return Ok(false);
|
|
1043
|
+
};
|
|
1044
|
+
let plan = pack_plan(&work, positions, limit)?;
|
|
1045
|
+
let row = [POST_PREFIX, &work.term[..]].concat();
|
|
1046
|
+
let last = &work.cells.last().expect("a chunk has cells").0;
|
|
1047
|
+
let next = pack_cursor(&work.term, last);
|
|
1048
|
+
if plan.posts.is_empty() && plan.positions.is_empty() && plan.blocks == work.blocks {
|
|
1049
|
+
*at = Some((cursor, next));
|
|
1050
|
+
idle += 1;
|
|
1051
|
+
if idle == 64 {
|
|
1052
|
+
return Ok(true);
|
|
1053
|
+
}
|
|
1054
|
+
continue;
|
|
1055
|
+
}
|
|
1056
|
+
let done = w.db.transact(&RetryPolicy::default(), |tx| {
|
|
1057
|
+
if !self.state_in(tx)? {
|
|
1058
|
+
return Err(Tx::Fail(corrupt("a pack met a version 1 index")));
|
|
1059
|
+
}
|
|
1060
|
+
if tx.get(t, GLOBAL, META, REBUILD_AFTER)?.is_some() {
|
|
1061
|
+
return Err(Tx::Fail(Error::PackRefused(
|
|
1062
|
+
"a rebuild is pending: finish it first".into(),
|
|
1063
|
+
)));
|
|
1064
|
+
}
|
|
1065
|
+
// The cursor and the blocks read are read again here (ai1249's C1): only a
|
|
1066
|
+
// pack and a rebuild's reset write them, so two packs, or a pack and a
|
|
1067
|
+
// reset, can't leave overlapping blocks. An ordinary write never conflicts
|
|
1068
|
+
// on them.
|
|
1069
|
+
let same = tx
|
|
1070
|
+
.get(t, GLOBAL, META, PACK_AFTER)?
|
|
1071
|
+
.is_some_and(|c| c.value() == cursor.as_slice());
|
|
1072
|
+
if !same {
|
|
1073
|
+
return Ok(false);
|
|
1074
|
+
}
|
|
1075
|
+
for (q, v) in &work.blocks {
|
|
1076
|
+
let same = tx
|
|
1077
|
+
.get(t, &row, POSTB, q)?
|
|
1078
|
+
.is_some_and(|c| c.value() == v.as_slice());
|
|
1079
|
+
if !same {
|
|
1080
|
+
return Ok(false);
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
for (q, _) in &work.blocks {
|
|
1084
|
+
if !plan.blocks.iter().any(|(nq, _)| nq == q) {
|
|
1085
|
+
tx.delete_column(t, &row, POSTB, q);
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
for (q, v) in &plan.blocks {
|
|
1089
|
+
if !work.blocks.iter().any(|(oq, ov)| oq == q && ov == v) {
|
|
1090
|
+
tx.put(t, &row, POSTB, q, v);
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
// Each cell is read again here and deleted only if it is still the version
|
|
1094
|
+
// the chunk read. One written since stays, and hides the block's posting,
|
|
1095
|
+
// which is then the older one; one written before this commits makes it
|
|
1096
|
+
// conflict and run again. (Not a delete of the version read, by timestamp:
|
|
1097
|
+
// that uncovers the column's older versions, a posting the tombstone or the
|
|
1098
|
+
// overwrite had replaced.)
|
|
1099
|
+
for (family, cells) in [(POST, &plan.posts), (POS, &plan.positions)] {
|
|
1100
|
+
for (id, ts) in cells {
|
|
1101
|
+
let same = tx
|
|
1102
|
+
.get(t, &row, family, id)?
|
|
1103
|
+
.is_some_and(|c| c.timestamp() == *ts);
|
|
1104
|
+
if same {
|
|
1105
|
+
tx.delete_column(t, &row, family, id);
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
tx.put(t, GLOBAL, META, PACK_AFTER, &next);
|
|
1110
|
+
Ok::<_, Tx>(true)
|
|
1111
|
+
});
|
|
1112
|
+
let done = match done.map_err(Error::from) {
|
|
1113
|
+
Ok((done, _)) => done,
|
|
1114
|
+
Err(e) if e.code() == pigeonhole::ErrorCode::Conflict && limit > 8 => {
|
|
1115
|
+
limit = (limit / 8).max(8);
|
|
1116
|
+
continue;
|
|
1117
|
+
}
|
|
1118
|
+
Err(e) => return Err(e),
|
|
1119
|
+
};
|
|
1120
|
+
*at = None;
|
|
1121
|
+
if done {
|
|
1122
|
+
if term.as_deref() != Some(&work.term[..]) {
|
|
1123
|
+
stats.terms += 1;
|
|
1124
|
+
*term = Some(work.term);
|
|
1125
|
+
}
|
|
1126
|
+
stats.postings += plan.packed;
|
|
1127
|
+
stats.tombstones += plan.tombstones;
|
|
1128
|
+
return Ok(true);
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
/// The next chunk after `cursor`, at `s`: the next term with posting cells, up to
|
|
1134
|
+
/// `limit` of them, their position cells, and the blocks their ids fall among.
|
|
1135
|
+
/// `None` when no posting cell is left.
|
|
1136
|
+
fn pack_read(
|
|
1137
|
+
&self,
|
|
1138
|
+
rows: &Rows,
|
|
1139
|
+
s: &Snapshot,
|
|
1140
|
+
cursor: &[u8],
|
|
1141
|
+
limit: usize,
|
|
1142
|
+
) -> Result<Option<PackWork>> {
|
|
1143
|
+
let cells_of = |term: &[u8], after: Option<&[u8]>| -> Result<Vec<(Vec<u8>, u64, u64)>> {
|
|
1144
|
+
let row = [POST_PREFIX, term].concat();
|
|
1145
|
+
let start = after.map_or(Bound::Unbounded, Bound::Excluded);
|
|
1146
|
+
let mut cells = rows
|
|
1147
|
+
.row(&row)
|
|
1148
|
+
.family(POST)
|
|
1149
|
+
.qualifier_bounds(start, Bound::Unbounded)
|
|
1150
|
+
.snapshot(s)
|
|
1151
|
+
.cells()?;
|
|
1152
|
+
let mut out = Vec::new();
|
|
1153
|
+
while out.len() < limit {
|
|
1154
|
+
let Some(e) = cells.next()? else {
|
|
1155
|
+
break;
|
|
1156
|
+
};
|
|
1157
|
+
let v = e
|
|
1158
|
+
.cell
|
|
1159
|
+
.as_u64()
|
|
1160
|
+
.ok_or_else(|| corrupt("a posting isn't a u64"))?;
|
|
1161
|
+
out.push((e.qualifier.to_vec(), e.cell.timestamp(), v));
|
|
1162
|
+
}
|
|
1163
|
+
Ok(out)
|
|
1164
|
+
};
|
|
1165
|
+
let at = unpack_cursor(cursor)?;
|
|
1166
|
+
let mut found = None;
|
|
1167
|
+
if let Some((term, doc)) = at {
|
|
1168
|
+
let cells = cells_of(term, Some(doc))?;
|
|
1169
|
+
if !cells.is_empty() {
|
|
1170
|
+
found = Some((term.to_vec(), cells));
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
if found.is_none() {
|
|
1174
|
+
// The next term's row with a posting cell.
|
|
1175
|
+
let after = at.map(|(term, _)| [POST_PREFIX, term].concat());
|
|
1176
|
+
let start = match &after {
|
|
1177
|
+
Some(row) => Bound::Excluded(row.as_slice()),
|
|
1178
|
+
None => Bound::Included(POST_PREFIX),
|
|
1179
|
+
};
|
|
1180
|
+
let end = [POST_PREFIX[0] + 1];
|
|
1181
|
+
let next = {
|
|
1182
|
+
let mut scan = rows
|
|
1183
|
+
.scan_bounds(start, Bound::Excluded(&end))
|
|
1184
|
+
.family(POST)
|
|
1185
|
+
.snapshot(s)
|
|
1186
|
+
.limit(1)
|
|
1187
|
+
.iter()?;
|
|
1188
|
+
match scan.next() {
|
|
1189
|
+
Some(row) => Some(row?.key()[POST_PREFIX.len()..].to_vec()),
|
|
1190
|
+
None => None,
|
|
1191
|
+
}
|
|
1192
|
+
};
|
|
1193
|
+
let Some(term) = next else {
|
|
1194
|
+
return Ok(None);
|
|
1195
|
+
};
|
|
1196
|
+
let cells = cells_of(&term, None)?;
|
|
1197
|
+
if cells.is_empty() {
|
|
1198
|
+
return Err(corrupt(
|
|
1199
|
+
"a term's row lost its posting cells within a snapshot",
|
|
1200
|
+
));
|
|
1201
|
+
}
|
|
1202
|
+
found = Some((term, cells));
|
|
1203
|
+
}
|
|
1204
|
+
let (term, cells) = found.expect("set above");
|
|
1205
|
+
let row = [POST_PREFIX, &term[..]].concat();
|
|
1206
|
+
let (first, last) = (&cells[0].0, &cells[cells.len() - 1].0);
|
|
1207
|
+
let mut positions = BTreeMap::new();
|
|
1208
|
+
if self.inner.opts.get_positions() {
|
|
1209
|
+
let mut read = rows
|
|
1210
|
+
.row(&row)
|
|
1211
|
+
.family(POS)
|
|
1212
|
+
.qualifier_bounds(Bound::Included(first), Bound::Included(last))
|
|
1213
|
+
.snapshot(s)
|
|
1214
|
+
.cells()?;
|
|
1215
|
+
while let Some(e) = read.next()? {
|
|
1216
|
+
positions.insert(
|
|
1217
|
+
e.qualifier.to_vec(),
|
|
1218
|
+
(e.cell.timestamp(), e.cell.value().to_vec()),
|
|
1219
|
+
);
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
// A block is named by its last id: from the first one that can hold `first`, while
|
|
1223
|
+
// a block starts at or before `last`.
|
|
1224
|
+
let mut blocks = Vec::new();
|
|
1225
|
+
let mut block = Block::default();
|
|
1226
|
+
let mut read = rows
|
|
1227
|
+
.row(&row)
|
|
1228
|
+
.family(POSTB)
|
|
1229
|
+
.qualifier_bounds(Bound::Included(first), Bound::Unbounded)
|
|
1230
|
+
.snapshot(s)
|
|
1231
|
+
.cells()?;
|
|
1232
|
+
while let Some(e) = read.next()? {
|
|
1233
|
+
block.decode(e.cell.value())?;
|
|
1234
|
+
if block.id(0) > last.as_slice() {
|
|
1235
|
+
break;
|
|
1236
|
+
}
|
|
1237
|
+
blocks.push((e.qualifier.to_vec(), e.cell.value().to_vec()));
|
|
1238
|
+
}
|
|
1239
|
+
Ok(Some(PackWork {
|
|
1240
|
+
term,
|
|
1241
|
+
cells,
|
|
1242
|
+
positions,
|
|
1243
|
+
blocks,
|
|
1244
|
+
}))
|
|
566
1245
|
}
|
|
567
1246
|
|
|
568
1247
|
/// Rebuilds the index from an emptied table (D309 §3, M2): drops every index row, then
|
|
@@ -573,7 +1252,16 @@ impl Index {
|
|
|
573
1252
|
let w = self.w();
|
|
574
1253
|
let t = &w.table;
|
|
575
1254
|
// The flag first: every write in flight read it, so they conflict and retry into Busy.
|
|
576
|
-
|
|
1255
|
+
// A stopped rebuild's cursor goes with it: the repair walks the whole source, and a
|
|
1256
|
+
// resume after it would skip rows of the emptied index (ai1249 on #1756).
|
|
1257
|
+
// A version 2 index stays one: its emptied rows have no blocks, and its state cell
|
|
1258
|
+
// says so again at the end. A pack's cursor goes with the rebuild's.
|
|
1259
|
+
let packed = t.get(GLOBAL, META, LAYOUT)?.and_then(|c| c.as_u64()) == Some(LAYOUT_PACKED);
|
|
1260
|
+
t.mutate(GLOBAL)
|
|
1261
|
+
.put(META, REPAIR, b"")
|
|
1262
|
+
.delete_column(META, REBUILD_AFTER)
|
|
1263
|
+
.delete_column(META, PACK_AFTER)
|
|
1264
|
+
.commit()?;
|
|
577
1265
|
for prefix in [POST_PREFIX, FWD_PREFIX, STATS_ROW] {
|
|
578
1266
|
// The keys first, under a snapshot and an iterator dropped before anything below
|
|
579
1267
|
// commits (#1514): held across this thread's own commits, they would pin the
|
|
@@ -595,18 +1283,30 @@ impl Index {
|
|
|
595
1283
|
batch.commit()?;
|
|
596
1284
|
}
|
|
597
1285
|
}
|
|
598
|
-
let stats = self.rebuild_inner(
|
|
599
|
-
|
|
1286
|
+
let stats = self.rebuild_inner(Walk::Repair, |_| ControlFlow::Continue(()))?;
|
|
1287
|
+
if packed {
|
|
1288
|
+
t.mutate(GLOBAL).put(META, REPAIR, PACKED).commit()?;
|
|
1289
|
+
} else {
|
|
1290
|
+
t.mutate(GLOBAL).delete_column(META, REPAIR).commit()?;
|
|
1291
|
+
}
|
|
600
1292
|
Ok(stats)
|
|
601
1293
|
}
|
|
602
1294
|
|
|
603
|
-
|
|
604
|
-
|
|
1295
|
+
/// The walk over the source: a rebuild's, which keeps a cursor (from `from`, if it
|
|
1296
|
+
/// resumes), or a repair's, which keeps none (`rebuild_inner`).
|
|
1297
|
+
fn rebuild_inner(
|
|
1298
|
+
&self,
|
|
1299
|
+
walk: Walk,
|
|
1300
|
+
mut step: impl FnMut(&RebuildStats) -> ControlFlow<()>,
|
|
1301
|
+
) -> Result<RebuildStats> {
|
|
605
1302
|
let inn = &*self.inner;
|
|
606
1303
|
let w = self.w();
|
|
607
1304
|
let mut stats = RebuildStats::default();
|
|
608
1305
|
let q = inn.source.qualifier.as_slice();
|
|
609
|
-
let mut after
|
|
1306
|
+
let (repairing, mut after) = match walk {
|
|
1307
|
+
Walk::Repair => (true, None),
|
|
1308
|
+
Walk::Rebuild { from } => (false, from),
|
|
1309
|
+
};
|
|
610
1310
|
loop {
|
|
611
1311
|
// A page of documents, under a snapshot and an iterator dropped before the page's
|
|
612
1312
|
// transactions commit (#1514): held across them, they would pin the memtables a
|
|
@@ -630,34 +1330,69 @@ impl Index {
|
|
|
630
1330
|
}
|
|
631
1331
|
page
|
|
632
1332
|
};
|
|
633
|
-
let Some(last) = page.last() else {
|
|
634
|
-
return
|
|
1333
|
+
let Some(last) = page.last().cloned() else {
|
|
1334
|
+
return self.rebuilt(repairing, stats);
|
|
635
1335
|
};
|
|
636
|
-
after = Some(last.clone());
|
|
637
1336
|
let full = page.len() as u64 == REBUILD_PAGE;
|
|
638
|
-
self.rebuild_page(&page, repairing, &mut stats)
|
|
1337
|
+
let cursor_at_last = match self.rebuild_page(&page, repairing, &mut stats, &mut step)? {
|
|
1338
|
+
Paged::Stopped => {
|
|
1339
|
+
stats.stopped = true;
|
|
1340
|
+
return Ok(stats);
|
|
1341
|
+
}
|
|
1342
|
+
Paged::Done { cursor_at_last } => cursor_at_last,
|
|
1343
|
+
};
|
|
639
1344
|
if !full {
|
|
640
|
-
return
|
|
1345
|
+
return self.rebuilt(repairing, stats);
|
|
1346
|
+
}
|
|
1347
|
+
// The page's cursor, unless its last document's transaction wrote it: a resume
|
|
1348
|
+
// redoes at most the next page (ai1249's Q2 on #1756).
|
|
1349
|
+
if !repairing && !cursor_at_last {
|
|
1350
|
+
w.table
|
|
1351
|
+
.mutate(GLOBAL)
|
|
1352
|
+
.put(META, REBUILD_AFTER, &last)
|
|
1353
|
+
.commit()?;
|
|
641
1354
|
}
|
|
1355
|
+
after = Some(last);
|
|
642
1356
|
}
|
|
643
1357
|
}
|
|
644
1358
|
|
|
645
|
-
///
|
|
1359
|
+
/// A walk that reached the source's end: a rebuild's cursor goes, and so does an
|
|
1360
|
+
/// unfilled index's mark (#1722 PR 2c), a repair's too: every source row has been
|
|
1361
|
+
/// visited.
|
|
1362
|
+
fn rebuilt(&self, repairing: bool, stats: RebuildStats) -> Result<RebuildStats> {
|
|
1363
|
+
let cursor = !repairing && self.rebuild_pending()?;
|
|
1364
|
+
let unfilled = self.fill_pending()?;
|
|
1365
|
+
if cursor || unfilled {
|
|
1366
|
+
let mut m = self.w().table.mutate(GLOBAL);
|
|
1367
|
+
if cursor {
|
|
1368
|
+
m = m.delete_column(META, REBUILD_AFTER);
|
|
1369
|
+
}
|
|
1370
|
+
if unfilled {
|
|
1371
|
+
m = m.delete_column(META, UNFILLED);
|
|
1372
|
+
}
|
|
1373
|
+
m.commit()?;
|
|
1374
|
+
}
|
|
1375
|
+
Ok(stats)
|
|
1376
|
+
}
|
|
1377
|
+
|
|
1378
|
+
/// Re-indexes each of `docs`, one transaction each (`rebuild_inner`'s page), calling
|
|
1379
|
+
/// `step` after each. A rebuild's document that writes anyway also writes the cursor.
|
|
646
1380
|
fn rebuild_page(
|
|
647
1381
|
&self,
|
|
648
1382
|
docs: &[Vec<u8>],
|
|
649
1383
|
repairing: bool,
|
|
650
1384
|
stats: &mut RebuildStats,
|
|
651
|
-
|
|
1385
|
+
step: &mut impl FnMut(&RebuildStats) -> ControlFlow<()>,
|
|
1386
|
+
) -> Result<Paged> {
|
|
652
1387
|
let inn = &*self.inner;
|
|
653
1388
|
let w = self.w();
|
|
654
1389
|
let q = inn.source.qualifier.as_slice();
|
|
1390
|
+
let mut cursor_at_last = false;
|
|
655
1391
|
for doc in docs {
|
|
656
1392
|
stats.documents += 1;
|
|
657
1393
|
let ((changed, bad), _) = w.db.transact(&RetryPolicy::default(), |tx| {
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
}
|
|
1394
|
+
// A repair's rows were emptied, blocks included: it writes as version 1 does.
|
|
1395
|
+
let packed = !repairing && self.state_in(tx)?;
|
|
661
1396
|
// The current text, not the snapshot's: a put since the scan wins.
|
|
662
1397
|
let text = match tx.get(&w.src, doc, &inn.source.family, q)? {
|
|
663
1398
|
None => return Ok((false, false)),
|
|
@@ -668,20 +1403,84 @@ impl Index {
|
|
|
668
1403
|
};
|
|
669
1404
|
let old = self.read_forward(tx, doc)?;
|
|
670
1405
|
let new = self.analyse(&text)?;
|
|
671
|
-
let changed = self.apply(tx, doc, &old, &new, Mode::Rebuild)?;
|
|
1406
|
+
let changed = self.apply(tx, doc, &old, &new, Mode::Rebuild, packed)?;
|
|
1407
|
+
if changed && !repairing {
|
|
1408
|
+
tx.put(&w.table, GLOBAL, META, REBUILD_AFTER, doc);
|
|
1409
|
+
}
|
|
672
1410
|
Ok::<_, Tx>((changed, false))
|
|
673
1411
|
})?;
|
|
674
1412
|
stats.changed += u64::from(changed);
|
|
675
1413
|
stats.invalid_utf8 += u64::from(bad);
|
|
1414
|
+
cursor_at_last = changed && !repairing;
|
|
1415
|
+
if step(stats).is_break() {
|
|
1416
|
+
return Ok(Paged::Stopped);
|
|
1417
|
+
}
|
|
676
1418
|
}
|
|
677
|
-
Ok(
|
|
1419
|
+
Ok(Paged::Done { cursor_at_last })
|
|
678
1420
|
}
|
|
679
1421
|
|
|
680
|
-
|
|
1422
|
+
/// The index's state, read in the transaction ([`REPAIR`]): whether it is version 2, or
|
|
1423
|
+
/// `Busy` while a repair runs. Every write through the index reads it, so a repair's
|
|
1424
|
+
/// start and an index's first pack both make the writes in flight conflict and run
|
|
1425
|
+
/// again under the new state.
|
|
1426
|
+
fn state_in(&self, txn: &mut Transaction) -> Result<bool> {
|
|
681
1427
|
match txn.get(&self.w().table, GLOBAL, META, REPAIR)? {
|
|
682
|
-
Some(
|
|
683
|
-
None => Ok(
|
|
1428
|
+
Some(c) => packed_state(c.value()),
|
|
1429
|
+
None => Ok(false),
|
|
1430
|
+
}
|
|
1431
|
+
}
|
|
1432
|
+
|
|
1433
|
+
/// Removes `doc`'s posting from `row`. In a version 1 index its cells are deleted. In a
|
|
1434
|
+
/// version 2 index they are overwritten with tombstones, since a packed block may hold
|
|
1435
|
+
/// the posting too: a blind write, as the delete is (the writer doesn't look).
|
|
1436
|
+
fn unpost(
|
|
1437
|
+
txn: &mut Transaction,
|
|
1438
|
+
t: &Table,
|
|
1439
|
+
row: &[u8],
|
|
1440
|
+
doc: &[u8],
|
|
1441
|
+
positions: bool,
|
|
1442
|
+
packed: bool,
|
|
1443
|
+
) {
|
|
1444
|
+
if packed {
|
|
1445
|
+
txn.put_u64(t, row, POST, doc, TOMBSTONE);
|
|
1446
|
+
if positions {
|
|
1447
|
+
txn.put(t, row, POS, doc, b"");
|
|
1448
|
+
}
|
|
1449
|
+
} else {
|
|
1450
|
+
txn.delete_column(t, row, POST, doc);
|
|
1451
|
+
if positions {
|
|
1452
|
+
txn.delete_column(t, row, POS, doc);
|
|
1453
|
+
}
|
|
1454
|
+
}
|
|
1455
|
+
}
|
|
1456
|
+
|
|
1457
|
+
/// `doc`'s posting in `row`'s packed blocks, for a rebuild's compare: its value and its
|
|
1458
|
+
/// encoded positions. The block is found by a read outside the transaction (a
|
|
1459
|
+
/// transaction reads single cells) and then read inside it: `None` unless the two agree,
|
|
1460
|
+
/// so a pack that rewrites the block before this transaction commits makes it conflict,
|
|
1461
|
+
/// and one that already has makes the rebuild write the posting as a cell, which is
|
|
1462
|
+
/// right either way.
|
|
1463
|
+
fn stored_in_block(
|
|
1464
|
+
&self,
|
|
1465
|
+
tx: &mut Transaction,
|
|
1466
|
+
row: &[u8],
|
|
1467
|
+
doc: &[u8],
|
|
1468
|
+
block: &mut Block,
|
|
1469
|
+
) -> Result<Option<(u64, Vec<u8>)>> {
|
|
1470
|
+
let Some(found) = find_block(self.rows(true)?, None, row, doc, block)? else {
|
|
1471
|
+
return Ok(None);
|
|
1472
|
+
};
|
|
1473
|
+
let Some(at) = found.at else {
|
|
1474
|
+
return Ok(None);
|
|
1475
|
+
};
|
|
1476
|
+
let same = tx
|
|
1477
|
+
.get(self.block_table()?, row, POSTB, &found.qualifier)?
|
|
1478
|
+
.is_some_and(|c| c.value() == found.value.as_slice());
|
|
1479
|
+
if !same {
|
|
1480
|
+
return Ok(None);
|
|
684
1481
|
}
|
|
1482
|
+
let (tf, len) = block.posting(at);
|
|
1483
|
+
Ok(Some((posting(tf, len), block.positions(at).to_vec())))
|
|
685
1484
|
}
|
|
686
1485
|
|
|
687
1486
|
fn read_forward(&self, txn: &mut Transaction, doc: &[u8]) -> Result<Forward> {
|
|
@@ -712,6 +1511,7 @@ impl Index {
|
|
|
712
1511
|
old: &Forward,
|
|
713
1512
|
new: &Analysed,
|
|
714
1513
|
mode: Mode,
|
|
1514
|
+
packed: bool,
|
|
715
1515
|
) -> Result<bool> {
|
|
716
1516
|
let inn = &*self.inner;
|
|
717
1517
|
let w = self.w();
|
|
@@ -720,37 +1520,58 @@ impl Index {
|
|
|
720
1520
|
let fwd = fwd_row(doc);
|
|
721
1521
|
let mut changed = false;
|
|
722
1522
|
let mut buf = Vec::new();
|
|
1523
|
+
let mut block = Block::default();
|
|
723
1524
|
let len_changed = !old.present || old.len != new.len;
|
|
724
1525
|
for (term, ps) in &new.terms {
|
|
725
1526
|
let row = post_row(term);
|
|
726
1527
|
let tf = u32::try_from(ps.len()).unwrap_or(u32::MAX);
|
|
727
1528
|
let old_tf = old.terms.get(term.as_bytes()).copied();
|
|
728
1529
|
let value = posting(tf, new.len);
|
|
729
|
-
let
|
|
730
|
-
|
|
1530
|
+
let enc = positions.then(|| encode_positions(ps));
|
|
1531
|
+
let (mut write_post, write_pos) = match mode {
|
|
1532
|
+
Mode::Put => (old_tf != Some(tf) || len_changed, positions),
|
|
731
1533
|
Mode::Rebuild => {
|
|
732
|
-
tx.get(t, &row, POST, doc)?.
|
|
1534
|
+
let post = tx.get(t, &row, POST, doc)?.map(|c| c.as_u64());
|
|
1535
|
+
let pos = match positions {
|
|
1536
|
+
true => tx.get(t, &row, POS, doc)?.map(|c| c.value().to_vec()),
|
|
1537
|
+
false => None,
|
|
1538
|
+
};
|
|
1539
|
+
// What the cells don't say, the blocks may (a version 2 index): a
|
|
1540
|
+
// posting that is right there is left there.
|
|
1541
|
+
let below = match packed && (post.is_none() || (positions && pos.is_none())) {
|
|
1542
|
+
true => self.stored_in_block(tx, &row, doc, &mut block)?,
|
|
1543
|
+
false => None,
|
|
1544
|
+
};
|
|
1545
|
+
let post = match post {
|
|
1546
|
+
Some(v) => v,
|
|
1547
|
+
None => below.as_ref().map(|(v, _)| *v),
|
|
1548
|
+
};
|
|
1549
|
+
let pos = pos.or(below.map(|(_, ps)| ps));
|
|
1550
|
+
let pos_wrong = positions
|
|
1551
|
+
&& match pos {
|
|
1552
|
+
// An empty list is a tombstone.
|
|
1553
|
+
Some(b) if !b.is_empty() => {
|
|
1554
|
+
decode_positions(&b, &mut buf).is_err() || buf != *ps
|
|
1555
|
+
}
|
|
1556
|
+
_ => true,
|
|
1557
|
+
};
|
|
1558
|
+
(post != Some(value), pos_wrong)
|
|
733
1559
|
}
|
|
734
1560
|
};
|
|
1561
|
+
// In a version 2 index a position cell never stands without its posting cell,
|
|
1562
|
+
// so a pack, which walks the posting cells, meets every cell there is.
|
|
1563
|
+
if packed && write_pos {
|
|
1564
|
+
write_post = true;
|
|
1565
|
+
}
|
|
735
1566
|
if write_post {
|
|
736
1567
|
tx.put_u64(t, &row, POST, doc, value)
|
|
737
1568
|
.aggregate(t, &row, MAXTF, BOUND, i64::from(tf))
|
|
738
1569
|
.aggregate(t, &row, MINLEN, BOUND, i64::from(new.len));
|
|
739
1570
|
changed = true;
|
|
740
1571
|
}
|
|
741
|
-
if
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
Mode::Put => true,
|
|
745
|
-
Mode::Rebuild => match tx.get(t, &row, POS, doc)? {
|
|
746
|
-
Some(c) => decode_positions(c.value(), &mut buf).is_err() || buf != *ps,
|
|
747
|
-
None => true,
|
|
748
|
-
},
|
|
749
|
-
};
|
|
750
|
-
if write_pos {
|
|
751
|
-
tx.put(t, &row, POS, doc, &enc);
|
|
752
|
-
changed = true;
|
|
753
|
-
}
|
|
1572
|
+
if write_pos {
|
|
1573
|
+
tx.put(t, &row, POS, doc, enc.as_deref().expect("positions are on"));
|
|
1574
|
+
changed = true;
|
|
754
1575
|
}
|
|
755
1576
|
if old_tf.is_none() {
|
|
756
1577
|
tx.incr(t, &row, DF, N, 1);
|
|
@@ -768,10 +1589,7 @@ impl Index {
|
|
|
768
1589
|
continue;
|
|
769
1590
|
}
|
|
770
1591
|
let row = post_row(term);
|
|
771
|
-
tx
|
|
772
|
-
if positions {
|
|
773
|
-
tx.delete_column(t, &row, POS, doc);
|
|
774
|
-
}
|
|
1592
|
+
Self::unpost(tx, t, &row, doc, positions, packed);
|
|
775
1593
|
tx.incr(t, &row, DF, N, -1)
|
|
776
1594
|
.delete_column(t, &fwd, TERMS, term.as_bytes());
|
|
777
1595
|
changed = true;
|
|
@@ -873,8 +1691,13 @@ impl Index {
|
|
|
873
1691
|
/// `Corruption` only if a row doesn't parse.
|
|
874
1692
|
pub fn verify(&self) -> Result<IndexStats> {
|
|
875
1693
|
let inn = &*self.inner;
|
|
876
|
-
let t = &inn.table;
|
|
877
1694
|
let s = inn.db.snapshot()?;
|
|
1695
|
+
// A repair running isn't an error here: its rows are checked as they stand.
|
|
1696
|
+
let packed = inn
|
|
1697
|
+
.table
|
|
1698
|
+
.get_at(&s, GLOBAL, META, REPAIR)?
|
|
1699
|
+
.is_some_and(|c| c.value() == PACKED);
|
|
1700
|
+
let t = self.rows(packed)?;
|
|
878
1701
|
let mut st = IndexStats::default();
|
|
879
1702
|
// Forward rows.
|
|
880
1703
|
let mut fwd: BTreeMap<Vec<u8>, Forward> = BTreeMap::new();
|
|
@@ -909,7 +1732,11 @@ impl Index {
|
|
|
909
1732
|
if inn.opts.get_positions() {
|
|
910
1733
|
fams.push(POS);
|
|
911
1734
|
}
|
|
1735
|
+
if packed {
|
|
1736
|
+
fams.push(POSTB);
|
|
1737
|
+
}
|
|
912
1738
|
let mut buf = Vec::new();
|
|
1739
|
+
let mut block = Block::default();
|
|
913
1740
|
for row in t
|
|
914
1741
|
.scan_prefix(POST_PREFIX)
|
|
915
1742
|
.families(fams)
|
|
@@ -919,30 +1746,50 @@ impl Index {
|
|
|
919
1746
|
let row = row?;
|
|
920
1747
|
let term = row.key()[POST_PREFIX.len()..].to_vec();
|
|
921
1748
|
let (mut df, mut maxtf, mut minlen) = (0i64, i64::MIN, i64::MAX);
|
|
922
|
-
|
|
923
|
-
|
|
1749
|
+
// The merged view (a version 2 index): the blocks' postings, then the cells
|
|
1750
|
+
// over them, a tombstone removing what it hides.
|
|
1751
|
+
let mut post_docs: BTreeMap<Vec<u8>, (u32, u32)> = BTreeMap::new();
|
|
924
1752
|
let mut pos_docs: BTreeMap<Vec<u8>, u32> = BTreeMap::new();
|
|
1753
|
+
let mut last: Option<Vec<u8>> = None;
|
|
1754
|
+
for e in row.view().iter().filter(|e| e.family == POSTB) {
|
|
1755
|
+
block.decode(e.cell.value())?;
|
|
1756
|
+
if block.id(block.len() - 1) != e.qualifier
|
|
1757
|
+
|| last.as_deref().is_some_and(|l| l >= block.id(0))
|
|
1758
|
+
{
|
|
1759
|
+
return Err(corrupt("a term's posting blocks overlap or are misnamed"));
|
|
1760
|
+
}
|
|
1761
|
+
last = Some(e.qualifier.to_vec());
|
|
1762
|
+
st.packed += block.len() as u64;
|
|
1763
|
+
for i in 0..block.len() {
|
|
1764
|
+
post_docs.insert(block.id(i).to_vec(), block.posting(i));
|
|
1765
|
+
if inn.opts.get_positions() {
|
|
1766
|
+
decode_positions(block.positions(i), &mut buf)?;
|
|
1767
|
+
pos_docs.insert(block.id(i).to_vec(), buf.len() as u32);
|
|
1768
|
+
}
|
|
1769
|
+
}
|
|
1770
|
+
}
|
|
925
1771
|
for e in row.view().iter() {
|
|
926
1772
|
match e.family {
|
|
927
1773
|
POST => {
|
|
928
|
-
let
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
if f.map(|f| (f.terms.get(&term).copied(), f.len)) != Some((Some(tf), len))
|
|
939
|
-
{
|
|
940
|
-
st.forward_mismatch += 1;
|
|
1774
|
+
let v = e
|
|
1775
|
+
.cell
|
|
1776
|
+
.as_u64()
|
|
1777
|
+
.ok_or_else(|| corrupt("a posting isn't a u64"))?;
|
|
1778
|
+
if packed && v == TOMBSTONE {
|
|
1779
|
+
st.tombstones += 1;
|
|
1780
|
+
st.packed -= u64::from(post_docs.remove(e.qualifier).is_some());
|
|
1781
|
+
} else {
|
|
1782
|
+
let under = post_docs.insert(e.qualifier.to_vec(), unposting(v));
|
|
1783
|
+
st.packed -= u64::from(under.is_some());
|
|
941
1784
|
}
|
|
942
1785
|
}
|
|
943
1786
|
POS => {
|
|
944
|
-
|
|
945
|
-
|
|
1787
|
+
if packed && e.cell.value().is_empty() {
|
|
1788
|
+
pos_docs.remove(e.qualifier);
|
|
1789
|
+
} else {
|
|
1790
|
+
decode_positions(e.cell.value(), &mut buf)?;
|
|
1791
|
+
pos_docs.insert(e.qualifier.to_vec(), buf.len() as u32);
|
|
1792
|
+
}
|
|
946
1793
|
}
|
|
947
1794
|
DF => df = e.cell.as_i64().unwrap_or(0),
|
|
948
1795
|
MAXTF => maxtf = e.cell.as_i64().unwrap_or(i64::MIN),
|
|
@@ -950,6 +1797,16 @@ impl Index {
|
|
|
950
1797
|
_ => {}
|
|
951
1798
|
}
|
|
952
1799
|
}
|
|
1800
|
+
let (mut max_seen, mut min_seen) = (0i64, i64::MAX);
|
|
1801
|
+
for (doc, &(tf, len)) in &post_docs {
|
|
1802
|
+
max_seen = max_seen.max(i64::from(tf));
|
|
1803
|
+
min_seen = min_seen.min(i64::from(len));
|
|
1804
|
+
*seen.entry(doc.clone()).or_default() += 1;
|
|
1805
|
+
let f = fwd.get(doc);
|
|
1806
|
+
if f.map(|f| (f.terms.get(&term).copied(), f.len)) != Some((Some(tf), len)) {
|
|
1807
|
+
st.forward_mismatch += 1;
|
|
1808
|
+
}
|
|
1809
|
+
}
|
|
953
1810
|
let n = post_docs.len() as i64;
|
|
954
1811
|
st.postings += n as u64;
|
|
955
1812
|
if n > 0 {
|
|
@@ -962,7 +1819,12 @@ impl Index {
|
|
|
962
1819
|
st.counter_drift += 1;
|
|
963
1820
|
}
|
|
964
1821
|
// Each posting has its positions, as many as its frequency, and no more.
|
|
965
|
-
if inn.opts.get_positions()
|
|
1822
|
+
if inn.opts.get_positions()
|
|
1823
|
+
&& (pos_docs.len() != post_docs.len()
|
|
1824
|
+
|| pos_docs
|
|
1825
|
+
.iter()
|
|
1826
|
+
.any(|(d, n)| post_docs.get(d).map(|p| p.0) != Some(*n)))
|
|
1827
|
+
{
|
|
966
1828
|
st.forward_mismatch += 1;
|
|
967
1829
|
}
|
|
968
1830
|
}
|
|
@@ -991,12 +1853,14 @@ impl Index {
|
|
|
991
1853
|
st.counter_drift += 1;
|
|
992
1854
|
}
|
|
993
1855
|
// The source column against the forward rows (and positions, by re-tokenising).
|
|
994
|
-
self.verify_source(&s, &fwd, &mut st)?;
|
|
1856
|
+
self.verify_source(t, packed, &s, &fwd, &mut st)?;
|
|
995
1857
|
Ok(st)
|
|
996
1858
|
}
|
|
997
1859
|
|
|
998
1860
|
fn verify_source(
|
|
999
1861
|
&self,
|
|
1862
|
+
t: &Rows,
|
|
1863
|
+
packed: bool,
|
|
1000
1864
|
s: &Snapshot,
|
|
1001
1865
|
fwd: &BTreeMap<Vec<u8>, Forward>,
|
|
1002
1866
|
st: &mut IndexStats,
|
|
@@ -1005,6 +1869,7 @@ impl Index {
|
|
|
1005
1869
|
let q = inn.source.qualifier.as_slice();
|
|
1006
1870
|
let mut sourced = BTreeSet::new();
|
|
1007
1871
|
let mut buf = Vec::new();
|
|
1872
|
+
let mut block = Block::default();
|
|
1008
1873
|
for row in inn
|
|
1009
1874
|
.src
|
|
1010
1875
|
.scan_prefix(b"")
|
|
@@ -1043,9 +1908,16 @@ impl Index {
|
|
|
1043
1908
|
}
|
|
1044
1909
|
if inn.opts.get_positions() {
|
|
1045
1910
|
for (term, ps) in &a.terms {
|
|
1046
|
-
let
|
|
1047
|
-
let ok = match
|
|
1911
|
+
let row = post_row(term);
|
|
1912
|
+
let ok = match t.get_at(s, &row, POS, &doc)? {
|
|
1048
1913
|
Some(c) => decode_positions(c.value(), &mut buf).is_ok() && buf == *ps,
|
|
1914
|
+
// Without a cell, the block's (a version 2 index).
|
|
1915
|
+
None if packed => find_block(t, Some(s), &row, &doc, &mut block)?
|
|
1916
|
+
.and_then(|f| f.at)
|
|
1917
|
+
.is_some_and(|at| {
|
|
1918
|
+
decode_positions(block.positions(at), &mut buf).is_ok()
|
|
1919
|
+
&& buf == *ps
|
|
1920
|
+
}),
|
|
1049
1921
|
None => false,
|
|
1050
1922
|
};
|
|
1051
1923
|
if !ok {
|
|
@@ -1063,6 +1935,108 @@ impl Index {
|
|
|
1063
1935
|
}
|
|
1064
1936
|
}
|
|
1065
1937
|
|
|
1938
|
+
/// What a pack's chunk writes: the blocks that replace the ones it read, and the cells it
|
|
1939
|
+
/// consumed, each with the timestamp it was read at.
|
|
1940
|
+
struct PackPlan {
|
|
1941
|
+
blocks: Vec<(Vec<u8>, Vec<u8>)>,
|
|
1942
|
+
posts: Vec<(Vec<u8>, u64)>,
|
|
1943
|
+
positions: Vec<(Vec<u8>, u64)>,
|
|
1944
|
+
/// Postings moved from cells into blocks, and tombstones removed.
|
|
1945
|
+
packed: u64,
|
|
1946
|
+
tombstones: u64,
|
|
1947
|
+
}
|
|
1948
|
+
|
|
1949
|
+
/// Merges a chunk's cells into the blocks they fall among, and cuts the result into blocks
|
|
1950
|
+
/// again. A cell replaces the block's posting for its document; a tombstone removes it.
|
|
1951
|
+
fn pack_plan(work: &PackWork, with_positions: bool, limit: usize) -> Result<PackPlan> {
|
|
1952
|
+
let mut run: BTreeMap<Vec<u8>, Packed> = BTreeMap::new();
|
|
1953
|
+
let mut block = Block::default();
|
|
1954
|
+
let mut last: Option<Vec<u8>> = None;
|
|
1955
|
+
for (_, value) in &work.blocks {
|
|
1956
|
+
block.decode(value)?;
|
|
1957
|
+
if last.as_deref().is_some_and(|l| l >= block.id(0)) {
|
|
1958
|
+
return Err(corrupt("a term's posting blocks overlap"));
|
|
1959
|
+
}
|
|
1960
|
+
last = Some(block.id(block.len() - 1).to_vec());
|
|
1961
|
+
for i in 0..block.len() {
|
|
1962
|
+
let (tf, len) = block.posting(i);
|
|
1963
|
+
run.insert(
|
|
1964
|
+
block.id(i).to_vec(),
|
|
1965
|
+
Packed {
|
|
1966
|
+
tf,
|
|
1967
|
+
len,
|
|
1968
|
+
positions: block.positions(i).to_vec(),
|
|
1969
|
+
},
|
|
1970
|
+
);
|
|
1971
|
+
}
|
|
1972
|
+
}
|
|
1973
|
+
let mut plan = PackPlan {
|
|
1974
|
+
blocks: Vec::new(),
|
|
1975
|
+
posts: Vec::new(),
|
|
1976
|
+
positions: Vec::new(),
|
|
1977
|
+
packed: 0,
|
|
1978
|
+
tombstones: 0,
|
|
1979
|
+
};
|
|
1980
|
+
// A few postings with no block among them wait as cells for more. A full chunk never
|
|
1981
|
+
// waits: its term has more than a block's worth.
|
|
1982
|
+
let live = work
|
|
1983
|
+
.cells
|
|
1984
|
+
.iter()
|
|
1985
|
+
.filter(|(_, _, v)| *v != TOMBSTONE)
|
|
1986
|
+
.count();
|
|
1987
|
+
let wait = work.blocks.is_empty() && live < PACK_MIN_RUN && work.cells.len() < limit;
|
|
1988
|
+
for (id, ts, value) in &work.cells {
|
|
1989
|
+
let pos = work.positions.get(id);
|
|
1990
|
+
if *value == TOMBSTONE {
|
|
1991
|
+
run.remove(id);
|
|
1992
|
+
plan.posts.push((id.clone(), *ts));
|
|
1993
|
+
if let Some((pts, _)) = pos {
|
|
1994
|
+
plan.positions.push((id.clone(), *pts));
|
|
1995
|
+
}
|
|
1996
|
+
plan.tombstones += 1;
|
|
1997
|
+
continue;
|
|
1998
|
+
}
|
|
1999
|
+
if wait {
|
|
2000
|
+
continue;
|
|
2001
|
+
}
|
|
2002
|
+
let (tf, len) = unposting(*value);
|
|
2003
|
+
// Its positions: its cell's, else what the block held for it. A posting with
|
|
2004
|
+
// neither (drift, which `verify` reports) stays a cell.
|
|
2005
|
+
let positions = match pos {
|
|
2006
|
+
Some((_, b)) if !b.is_empty() => b.clone(),
|
|
2007
|
+
_ if !with_positions => Vec::new(),
|
|
2008
|
+
_ => match run.get(id) {
|
|
2009
|
+
Some(p) if pos.is_none() => p.positions.clone(),
|
|
2010
|
+
_ => continue,
|
|
2011
|
+
},
|
|
2012
|
+
};
|
|
2013
|
+
run.insert(id.clone(), Packed { tf, len, positions });
|
|
2014
|
+
plan.posts.push((id.clone(), *ts));
|
|
2015
|
+
if let Some((pts, _)) = pos {
|
|
2016
|
+
plan.positions.push((id.clone(), *pts));
|
|
2017
|
+
}
|
|
2018
|
+
plan.packed += 1;
|
|
2019
|
+
}
|
|
2020
|
+
// Even blocks: as many as the postings need, each with its share, so none is left
|
|
2021
|
+
// under half full beside a full one. A block still closes early at its byte cap.
|
|
2022
|
+
let n = run.len();
|
|
2023
|
+
let share = n.div_ceil(n.div_ceil(MAX_POSTINGS).max(1)).max(1);
|
|
2024
|
+
let mut builder = Builder::new(with_positions);
|
|
2025
|
+
for (id, p) in &run {
|
|
2026
|
+
builder.push(Posting {
|
|
2027
|
+
id,
|
|
2028
|
+
tf: p.tf,
|
|
2029
|
+
len: p.len,
|
|
2030
|
+
positions: &p.positions,
|
|
2031
|
+
});
|
|
2032
|
+
if builder.len() >= share || builder.full() {
|
|
2033
|
+
plan.blocks.extend(builder.finish());
|
|
2034
|
+
}
|
|
2035
|
+
}
|
|
2036
|
+
plan.blocks.extend(builder.finish());
|
|
2037
|
+
Ok(plan)
|
|
2038
|
+
}
|
|
2039
|
+
|
|
1066
2040
|
/// Reads an index's `G` row and checks it: its source, options, and the tokeniser to use
|
|
1067
2041
|
/// (the built-in one, or `custom`), which must match the stored name and fingerprint.
|
|
1068
2042
|
fn load(
|
|
@@ -1085,7 +2059,7 @@ fn load(
|
|
|
1085
2059
|
.map(|c| c.map(|c| c.value().to_vec()))
|
|
1086
2060
|
};
|
|
1087
2061
|
match rows.get(GLOBAL, META, LAYOUT)?.and_then(|c| c.as_u64()) {
|
|
1088
|
-
Some(LAYOUT_VERSION) => {}
|
|
2062
|
+
Some(LAYOUT_VERSION | LAYOUT_PACKED) => {}
|
|
1089
2063
|
Some(v) => return Err(not(&format!("layout version {v} is unknown"))),
|
|
1090
2064
|
None => return Err(not("it has no layout")),
|
|
1091
2065
|
}
|
|
@@ -1165,6 +2139,7 @@ impl ReadIndex {
|
|
|
1165
2139
|
opts,
|
|
1166
2140
|
tok,
|
|
1167
2141
|
builtin,
|
|
2142
|
+
fresh: OnceLock::new(),
|
|
1168
2143
|
}),
|
|
1169
2144
|
},
|
|
1170
2145
|
})
|