summa-core 2.0.0

Core async search engine library with WASM support
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
//! Byte-preserving range copies shared by encoded payloads and sparse scratch.

use std::io::{Read, Seek, SeekFrom, Write};
use std::sync::atomic::{AtomicBool, Ordering};

use super::{OffsetWriter, block_in_place_if_multithread};
use crate::Result;
use crate::directories::DirectoryWriter;

const READ_CHUNK: usize = 4 * 1024 * 1024;

fn check_cancellation(cancellation: Option<&AtomicBool>) -> Result<()> {
    if cancellation.is_some_and(|flag| flag.load(Ordering::Acquire)) {
        return Err(crate::Error::IndexClosed);
    }
    Ok(())
}

/// Attempt a byte-identical local range copy through the streaming writer's
/// kernel-assisted path. Returns `Ok(false)` without emitting bytes when the
/// backend/filesystem does not support it.
fn try_copy_local_file_range(
    writer: &mut OffsetWriter,
    source: &std::fs::File,
    source_path: &std::path::Path,
    source_range: std::ops::Range<u64>,
    cancellation: Option<&AtomicBool>,
    context: &str,
) -> Result<bool> {
    const COPY_CHUNK: usize = 16 * 1024 * 1024;

    let expected = source_range
        .end
        .checked_sub(source_range.start)
        .ok_or_else(|| crate::Error::Corruption(format!("{context} source range is inverted")))?;
    let mut source_offset = source_range.start;
    let mut copied = 0u64;
    while copied < expected {
        check_cancellation(cancellation)?;
        let requested = usize::try_from((expected - copied).min(COPY_CHUNK as u64))
            .expect("bounded copy chunk fits usize");
        match writer.copy_from_file_range(source, &mut source_offset, requested) {
            Ok(0) => {
                return Err(crate::Error::Io(std::io::Error::new(
                    std::io::ErrorKind::UnexpectedEof,
                    format!(
                        "kernel {context} copy stopped after {copied} of {expected} bytes from \
                         {source_path:?}",
                    ),
                )));
            }
            Ok(count) => {
                copied = copied.checked_add(count as u64).ok_or_else(|| {
                    crate::Error::Internal(format!("{context} copied-byte count exceeds u64"))
                })?;
            }
            Err(error) if error.kind() == std::io::ErrorKind::Interrupted => continue,
            Err(error) if error.kind() == std::io::ErrorKind::Unsupported && copied == 0 => {
                return Ok(false);
            }
            Err(error) => return Err(crate::Error::Io(error)),
        }
    }

    static LOGGED: AtomicBool = AtomicBool::new(false);
    if copied != 0 && !LOGGED.swap(true, Ordering::Relaxed) {
        log::info!("[merge] byte-identical local ranges use kernel-assisted file copies");
    }
    Ok(true)
}

/// Keep one source descriptor through kernel admission and local fallback.
fn copy_local_file_range(
    writer: &mut OffsetWriter,
    source_path: &std::path::Path,
    source_range: std::ops::Range<u64>,
    cancellation: Option<&AtomicBool>,
    context: &str,
) -> Result<()> {
    let mut source = std::fs::File::open(source_path)?;
    if try_copy_local_file_range(
        writer,
        &source,
        source_path,
        source_range.clone(),
        cancellation,
        context,
    )? {
        return Ok(());
    }
    static LOGGED: AtomicBool = AtomicBool::new(false);
    if !LOGGED.swap(true, Ordering::Relaxed) {
        log::info!("[merge] unsupported local range copies use bounded file reads");
    }
    // Unsupported is accepted only before any kernel copy succeeded. Seek
    // the original range explicitly rather than relying on the fd cursor.
    copy_reader_range(writer, &mut source, source_range, cancellation)
}

fn copy_reader_range(
    writer: &mut OffsetWriter,
    source: &mut (impl Read + Seek),
    source_range: std::ops::Range<u64>,
    cancellation: Option<&AtomicBool>,
) -> Result<()> {
    let mut remaining = source_range
        .end
        .checked_sub(source_range.start)
        .ok_or_else(|| crate::Error::Corruption("local copy source range is inverted".into()))?;
    if remaining == 0 {
        return Ok(());
    }
    check_cancellation(cancellation)?;
    source.seek(SeekFrom::Start(source_range.start))?;
    let mut buffer = vec![0; remaining.min(READ_CHUNK as u64) as usize];
    while remaining > 0 {
        check_cancellation(cancellation)?;
        let count = remaining.min(buffer.len() as u64) as usize;
        // read_exact retries Interrupted and refuses a truncated source. A
        // failed chunk is never partly appended to the destination.
        source.read_exact(&mut buffer[..count])?;
        check_cancellation(cancellation)?;
        writer.write_all(&buffer[..count])?;
        remaining -= count as u64;
    }
    Ok(())
}

/// Copy an admitted byte-identical range. Local sources use the kernel or
/// bounded reads; abstract directories retain their mapped-byte fallback.
pub(in crate::segment) fn copy_local_range_or_bytes(
    writer: &mut OffsetWriter,
    source_path: Option<&std::path::Path>,
    source_range: std::ops::Range<u64>,
    bytes: &[u8],
    cancellation: Option<&AtomicBool>,
    context: &str,
) -> Result<()> {
    let range_len = source_range
        .end
        .checked_sub(source_range.start)
        .ok_or_else(|| crate::Error::Corruption(format!("{context} source range is inverted")))?;
    if range_len != bytes.len() as u64 {
        return Err(crate::Error::Corruption(format!(
            "{context} source range is {range_len} bytes but mapped section is {} bytes",
            bytes.len(),
        )));
    }
    if bytes.is_empty() {
        return Ok(());
    }

    if let Some(path) = source_path {
        return copy_local_file_range(writer, path, source_range, cancellation, context);
    }

    for chunk in bytes.chunks(READ_CHUNK) {
        check_cancellation(cancellation)?;
        writer.write_all(chunk).map_err(crate::Error::Io)?;
    }
    Ok(())
}

/// Append an exact-length temporary directory file to a segment output and
/// remove it. Used by sparse skip tables so neither merge nor BP rewrite
/// buffers a corpus-sized metadata section on heap.
pub(crate) async fn append_and_delete_temp<D: DirectoryWriter>(
    directory: &D,
    path: &std::path::Path,
    expected_bytes: u64,
    writer: &mut OffsetWriter,
    index_label: &str,
) -> Result<()> {
    let actual_bytes = directory.file_size(path).await?;
    if actual_bytes != expected_bytes {
        return Err(crate::Error::Corruption(format!(
            "temporary sparse section {:?} has {} bytes, expected {}",
            path, actual_bytes, expected_bytes,
        )));
    }
    if let Some(local_path) = directory.local_path(path) {
        block_in_place_if_multithread(|| {
            copy_local_file_range(
                writer,
                &local_path,
                0..expected_bytes,
                None,
                "sparse scratch",
            )
        })?;
    } else {
        let mut offset = 0u64;
        while offset < expected_bytes {
            let end = (offset + READ_CHUNK as u64).min(expected_bytes);
            let chunk = directory.read_range(path, offset..end).await?;
            writer
                .write_all(chunk.as_slice())
                .map_err(crate::Error::Io)?;
            offset = end;
        }
    }
    if let Err(error) = directory.delete(path).await {
        // The section is already complete in the output. This output-scoped
        // scratch file is safe for the startup orphan sweep and must not
        // invalidate an otherwise successful multi-hour merge.
        log::warn!(
            "[merge] index={} failed to remove temporary sparse section {:?}: {}",
            index_label,
            path,
            error,
        );
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::directories::StreamingWriter;
    use std::collections::VecDeque;
    use std::io::{self, Cursor};
    use std::sync::{Arc, Mutex};

    enum KernelStep {
        Copy(usize),
        Error(io::ErrorKind),
    }

    #[derive(Default)]
    struct Recorded {
        bytes: Vec<u8>,
        kernel_requests: Vec<(u64, usize)>,
        write_sizes: Vec<usize>,
    }

    struct TestWriter {
        recorded: Arc<Mutex<Recorded>>,
        steps: VecDeque<KernelStep>,
        cancel_after_write: Option<Arc<AtomicBool>>,
        write_limit: usize,
        interrupt_write: bool,
    }

    impl Write for TestWriter {
        fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
            if std::mem::take(&mut self.interrupt_write) {
                return Err(io::Error::from(io::ErrorKind::Interrupted));
            }
            let bytes = &bytes[..bytes.len().min(self.write_limit)];
            let mut recorded = self.recorded.lock().unwrap();
            recorded.bytes.extend_from_slice(bytes);
            recorded.write_sizes.push(bytes.len());
            if let Some(cancelled) = &self.cancel_after_write {
                cancelled.store(true, Ordering::Release);
            }
            Ok(bytes.len())
        }
        fn flush(&mut self) -> io::Result<()> {
            Ok(())
        }
    }

    impl StreamingWriter for TestWriter {
        fn finish(self: Box<Self>) -> io::Result<()> {
            Ok(())
        }
        fn bytes_written(&self) -> u64 {
            self.recorded.lock().unwrap().bytes.len() as u64
        }
        fn copy_from_file_range(
            &mut self,
            source: &std::fs::File,
            offset: &mut u64,
            len: usize,
        ) -> io::Result<usize> {
            self.recorded
                .lock()
                .unwrap()
                .kernel_requests
                .push((*offset, len));
            match self
                .steps
                .pop_front()
                .unwrap_or(KernelStep::Error(io::ErrorKind::Unsupported))
            {
                KernelStep::Copy(count) => {
                    let count = count.min(len);
                    let mut source = source.try_clone()?;
                    source.seek(SeekFrom::Start(*offset))?;
                    let mut bytes = vec![0; count];
                    source.read_exact(&mut bytes)?;
                    self.recorded
                        .lock()
                        .unwrap()
                        .bytes
                        .extend_from_slice(&bytes);
                    *offset += count as u64;
                    Ok(count)
                }
                KernelStep::Error(kind) => Err(io::Error::from(kind)),
            }
        }
    }

    fn writer(steps: impl IntoIterator<Item = KernelStep>) -> (OffsetWriter, Arc<Mutex<Recorded>>) {
        let recorded = Arc::new(Mutex::new(Recorded::default()));
        (
            OffsetWriter::new(Box::new(TestWriter {
                recorded: Arc::clone(&recorded),
                steps: steps.into_iter().collect(),
                cancel_after_write: None,
                write_limit: usize::MAX,
                interrupt_write: false,
            })),
            recorded,
        )
    }

    #[test]
    fn truncated_local_range_fails_instead_of_copying_a_stale_mapped_snapshot() {
        let source = tempfile::NamedTempFile::new().unwrap();
        let admitted = (0..100u8).collect::<Vec<_>>();
        std::fs::write(source.path(), &admitted).unwrap();
        // An externally truncated immutable source must fail copying; the
        // previously admitted view does not justify publishing missing bytes.
        source.as_file().set_len(20).unwrap();
        let (mut output, recorded) = writer([]);
        let error = copy_local_range_or_bytes(
            &mut output,
            Some(source.path()),
            10..30,
            &admitted[10..30],
            None,
            "test",
        )
        .unwrap_err();
        assert!(
            matches!(error, crate::Error::Io(error) if error.kind() == io::ErrorKind::UnexpectedEof)
        );
        assert!(recorded.lock().unwrap().bytes.is_empty());
    }
    #[test]
    fn local_fallback_copies_an_exact_subrange_in_bounded_chunks() {
        let source = tempfile::NamedTempFile::new().unwrap();
        let bytes: Vec<_> = (0..READ_CHUNK * 2 + 83).map(|i| (i % 251) as u8).collect();
        std::fs::write(source.path(), &bytes).unwrap();
        let (mut output, recorded) = writer([]);
        output.write_all(b"prefix").unwrap();
        let range = 17..(bytes.len() - 11);
        copy_local_range_or_bytes(
            &mut output,
            Some(source.path()),
            range.start as u64..range.end as u64,
            &bytes[range.clone()],
            None,
            "test",
        )
        .unwrap();
        let recorded = recorded.lock().unwrap();
        assert_eq!(&recorded.bytes[..6], b"prefix");
        assert_eq!(&recorded.bytes[6..], &bytes[range.clone()]);
        assert_eq!(output.offset(), (range.len() + 6) as u64);
        assert_eq!(&recorded.write_sizes[1..], &[READ_CHUNK, READ_CHUNK, 55]);
        assert_eq!(recorded.kernel_requests, [(17, range.len())]);
    }

    #[test]
    fn kernel_short_copies_and_interruptions_preserve_source_offsets() {
        let source = tempfile::NamedTempFile::new().unwrap();
        let bytes = (0..40u8).collect::<Vec<_>>();
        std::fs::write(source.path(), &bytes).unwrap();
        let (mut output, recorded) = writer([
            KernelStep::Error(io::ErrorKind::Interrupted),
            KernelStep::Copy(3),
            KernelStep::Error(io::ErrorKind::Interrupted),
            KernelStep::Copy(7),
        ]);
        copy_local_range_or_bytes(
            &mut output,
            Some(source.path()),
            9..19,
            &bytes[9..19],
            None,
            "test",
        )
        .unwrap();
        let recorded = recorded.lock().unwrap();
        assert_eq!(recorded.bytes, bytes[9..19]);
        assert_eq!(
            recorded.kernel_requests,
            [(9, 10), (9, 10), (12, 7), (12, 7)]
        );
        assert!(
            recorded.write_sizes.is_empty(),
            "successful kernel copies must not stage or duplicate bytes"
        );
        assert_eq!(output.offset(), 10);
    }

    #[test]
    fn kernel_copy_failure_after_partial_output_never_restarts_fallback() {
        let source = tempfile::NamedTempFile::new().unwrap();
        let bytes = (0..40u8).collect::<Vec<_>>();
        std::fs::write(source.path(), &bytes).unwrap();
        for kind in [io::ErrorKind::Unsupported, io::ErrorKind::Other] {
            let (mut output, recorded) = writer([KernelStep::Copy(3), KernelStep::Error(kind)]);
            let error = copy_local_range_or_bytes(
                &mut output,
                Some(source.path()),
                9..19,
                &bytes[9..19],
                None,
                "test",
            )
            .unwrap_err();
            assert!(matches!(error, crate::Error::Io(error) if error.kind() == kind));
            let recorded = recorded.lock().unwrap();
            assert_eq!(recorded.bytes, bytes[9..12]);
            assert_eq!(recorded.kernel_requests, [(9, 10), (12, 7)]);
            assert!(recorded.write_sizes.is_empty());
            assert_eq!(output.offset(), 3);
        }
        let (mut output, recorded) = writer([KernelStep::Copy(0)]);
        let error = copy_local_range_or_bytes(
            &mut output,
            Some(source.path()),
            9..19,
            &bytes[9..19],
            None,
            "test",
        )
        .unwrap_err();
        assert!(
            matches!(error, crate::Error::Io(error) if error.kind() == io::ErrorKind::UnexpectedEof)
        );
        assert!(recorded.lock().unwrap().bytes.is_empty());
    }

    struct InterruptedReader {
        cursor: Cursor<Vec<u8>>,
        interrupted: bool,
        largest_request: usize,
    }
    impl Read for InterruptedReader {
        fn read(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
            self.largest_request = self.largest_request.max(buffer.len());
            if !self.interrupted {
                self.interrupted = true;
                return Err(io::Error::from(io::ErrorKind::Interrupted));
            }
            let len = buffer.len().min(1024);
            self.cursor.read(&mut buffer[..len])
        }
    }
    impl Seek for InterruptedReader {
        fn seek(&mut self, from: SeekFrom) -> io::Result<u64> {
            self.cursor.seek(from)
        }
    }

    #[test]
    fn staged_reads_retry_interruptions_and_short_reads_without_exceeding_scratch_bound() {
        let bytes: Vec<_> = (0..READ_CHUNK + 83).map(|i| (i % 251) as u8).collect();
        let mut source = InterruptedReader {
            cursor: Cursor::new(bytes.clone()),
            interrupted: false,
            largest_request: 0,
        };
        let (mut output, recorded) = writer([]);
        copy_reader_range(
            &mut output,
            &mut source,
            17..(bytes.len() - 11) as u64,
            None,
        )
        .unwrap();
        assert!(source.interrupted);
        assert_eq!(source.largest_request, READ_CHUNK);
        assert_eq!(source.cursor.position(), (bytes.len() - 11) as u64);
        let recorded = recorded.lock().unwrap();
        assert_eq!(recorded.bytes, bytes[17..bytes.len() - 11]);
        assert_eq!(recorded.write_sizes, [READ_CHUNK, 55]);
    }

    #[test]
    fn staged_copy_retries_interrupted_and_short_destination_writes() {
        let recorded = Arc::new(Mutex::new(Recorded::default()));
        let mut output = OffsetWriter::new(Box::new(TestWriter {
            recorded: Arc::clone(&recorded),
            steps: VecDeque::new(),
            cancel_after_write: None,
            write_limit: 3,
            interrupt_write: true,
        }));
        let mut source = Cursor::new(b"0123456789abc".to_vec());
        copy_reader_range(&mut output, &mut source, 1..12, None).unwrap();
        let recorded = recorded.lock().unwrap();
        assert_eq!(recorded.bytes, b"123456789ab");
        assert_eq!(recorded.write_sizes, [3, 3, 3, 2]);
        assert_eq!(output.offset(), 11);
    }

    #[test]
    fn cancellation_stops_local_staging_before_the_next_chunk() {
        let source = tempfile::NamedTempFile::new().unwrap();
        let bytes = vec![7; READ_CHUNK + 1];
        std::fs::write(source.path(), &bytes).unwrap();
        let cancelled = Arc::new(AtomicBool::new(false));
        let recorded = Arc::new(Mutex::new(Recorded::default()));
        let mut output = OffsetWriter::new(Box::new(TestWriter {
            recorded: Arc::clone(&recorded),
            steps: VecDeque::new(),
            cancel_after_write: Some(Arc::clone(&cancelled)),
            write_limit: usize::MAX,
            interrupt_write: false,
        }));
        let error = copy_local_range_or_bytes(
            &mut output,
            Some(source.path()),
            0..bytes.len() as u64,
            &bytes,
            Some(&cancelled),
            "test",
        )
        .unwrap_err();
        assert!(matches!(error, crate::Error::IndexClosed));
        assert_eq!(recorded.lock().unwrap().write_sizes, [READ_CHUNK]);
        assert_eq!(output.offset(), READ_CHUNK as u64);
        let (mut output, recorded) = writer([]);
        let error = copy_local_range_or_bytes(
            &mut output,
            Some(source.path()),
            0..1,
            &bytes[..1],
            Some(&cancelled),
            "test",
        )
        .unwrap_err();
        assert!(matches!(error, crate::Error::IndexClosed));
        assert!(recorded.lock().unwrap().kernel_requests.is_empty());
    }

    #[test]
    fn abstract_and_empty_ranges_preserve_the_existing_mapped_fallback() {
        let (mut output, recorded) = writer([]);
        copy_local_range_or_bytes(&mut output, None, 27..30, b"abc", None, "test").unwrap();
        let directory = tempfile::tempdir().unwrap();
        copy_local_range_or_bytes(
            &mut output,
            Some(&directory.path().join("missing")),
            7..7,
            b"",
            None,
            "test",
        )
        .unwrap();
        assert_eq!(recorded.lock().unwrap().bytes, b"abc");
        assert!(recorded.lock().unwrap().kernel_requests.is_empty());
        assert!(matches!(
            copy_local_range_or_bytes(&mut output, None, 0..4, b"abc", None, "test"),
            Err(crate::Error::Corruption(_))
        ));
    }

    #[tokio::test]
    async fn temporary_sections_share_the_local_fallback_and_delete_only_after_copy() {
        use crate::directories::{MmapDirectory, RamDirectory};
        async fn check<D: DirectoryWriter>(dir: D) {
            let path = std::path::Path::new("section.tmp");
            dir.write(path, b"temporary section").await.unwrap();
            let (mut output, recorded) = writer([]);
            assert!(
                append_and_delete_temp(&dir, path, 1, &mut output, "test")
                    .await
                    .is_err()
            );
            assert!(dir.exists(path).await.unwrap());
            assert!(recorded.lock().unwrap().bytes.is_empty());
            append_and_delete_temp(&dir, path, 17, &mut output, "test")
                .await
                .unwrap();
            assert_eq!(recorded.lock().unwrap().bytes, b"temporary section");
            assert!(!dir.exists(path).await.unwrap());
        }
        check(RamDirectory::new()).await;
        let temp = tempfile::tempdir().unwrap();
        check(MmapDirectory::new(temp.path())).await;
    }
}