bearing 0.1.0-alpha.5

A Rust port of Apache Lucene
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
// SPDX-License-Identifier: Apache-2.0

//! Filesystem-backed [`Directory`] implementations.
//!
//! [`FSDirectory`] uses file handles for reads (like Java's `NIOFSDirectory`).
//! [`MmapDirectory`] uses memory-mapped I/O
//! (like Java's `MMapDirectory`) and is preferred for read-heavy workloads.
//! `FSDirectory::open()` returns an `MmapDirectory`.

use std::fs;
use std::fs::File;
use std::io::{self, BufWriter, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;

use super::mmap::MmapDirectory;
use crate::store::checksum::CRC32;
use crate::store::{DataOutput, Directory, IndexOutput};

// ============================================================
// Shared filesystem helpers used by both FSDirectory and MmapDirectory
// ============================================================

pub(crate) fn fs_create_output(dir_path: &Path, name: &str) -> io::Result<Box<dyn IndexOutput>> {
    let file_path = dir_path.join(name);
    let file = File::create(&file_path)?;
    let writer = BufWriter::new(file);
    Ok(Box::new(FSIndexOutput::new(name.to_string(), writer)))
}

pub(crate) fn fs_list_all(dir_path: &Path) -> io::Result<Vec<String>> {
    let mut names: Vec<String> = fs::read_dir(dir_path)?
        .filter_map(|entry| {
            entry.ok().and_then(|e| {
                let ft = e.file_type().ok()?;
                if ft.is_file() {
                    e.file_name().into_string().ok()
                } else {
                    None
                }
            })
        })
        .collect();
    names.sort();
    Ok(names)
}

pub(crate) fn fs_file_length(dir_path: &Path, name: &str) -> io::Result<u64> {
    let file_path = dir_path.join(name);
    let meta = fs::metadata(&file_path)?;
    Ok(meta.len())
}

pub(crate) fn fs_delete_file(dir_path: &Path, name: &str) -> io::Result<()> {
    fs::remove_file(dir_path.join(name))
}

pub(crate) fn fs_rename(dir_path: &Path, source: &str, dest: &str) -> io::Result<()> {
    fs::rename(dir_path.join(source), dir_path.join(dest))
}

pub(crate) fn fs_read_file(dir_path: &Path, name: &str) -> io::Result<Vec<u8>> {
    fs::read(dir_path.join(name))
}

pub(crate) fn fs_write_file(dir_path: &Path, name: &str, data: &[u8]) -> io::Result<()> {
    fs::write(dir_path.join(name), data)
}

pub(crate) fn fs_sync(dir_path: &Path, names: &[&str]) -> io::Result<()> {
    for name in names {
        let file = File::open(dir_path.join(name))?;
        file.sync_all()?;
    }
    Ok(())
}

pub(crate) fn fs_sync_meta_data(dir_path: &Path) -> io::Result<()> {
    let dir = File::open(dir_path)?;
    dir.sync_all()
}

// ============================================================
// FSDirectory — file-handle-based reads
// ============================================================

/// Filesystem directory using file handles for reads.
///
/// For read-heavy workloads (queries), prefer
/// [`MmapDirectory`] which avoids syscalls.
/// `FSDirectory::open()` returns an `MmapDirectory` by default, matching
/// Java's `FSDirectory.open()` behavior.
pub struct FSDirectory {
    path: PathBuf,
}

impl FSDirectory {
    /// Opens (or creates) a filesystem directory at the given path.
    ///
    /// Returns an [`MmapDirectory`], matching
    /// Java where `FSDirectory.open()` returns `MMapDirectory` on 64-bit.
    pub fn open(path: &Path) -> io::Result<super::SharedDirectory> {
        MmapDirectory::create(path)
    }

    /// Opens a filesystem directory with file-handle-based reads.
    ///
    /// Use this only when mmap is not desired. For most read workloads,
    /// prefer `FSDirectory::open()` which returns an `MmapDirectory`.
    pub fn open_with_file_handles(path: &Path) -> io::Result<super::SharedDirectory> {
        fs::create_dir_all(path)?;
        Ok(Arc::new(Self {
            path: path.to_path_buf(),
        }))
    }

    /// Returns the filesystem path of this directory.
    pub fn path(&self) -> &Path {
        &self.path
    }
}

impl Directory for FSDirectory {
    fn create_output(&self, name: &str) -> io::Result<Box<dyn IndexOutput>> {
        fs_create_output(&self.path, name)
    }

    fn list_all(&self) -> io::Result<Vec<String>> {
        fs_list_all(&self.path)
    }

    fn file_length(&self, name: &str) -> io::Result<u64> {
        fs_file_length(&self.path, name)
    }

    fn delete_file(&self, name: &str) -> io::Result<()> {
        fs_delete_file(&self.path, name)
    }

    fn rename(&self, source: &str, dest: &str) -> io::Result<()> {
        fs_rename(&self.path, source, dest)
    }

    fn read_file(&self, name: &str) -> io::Result<Vec<u8>> {
        fs_read_file(&self.path, name)
    }

    fn write_file(&self, name: &str, data: &[u8]) -> io::Result<()> {
        fs_write_file(&self.path, name, data)
    }

    fn sync(&self, names: &[&str]) -> io::Result<()> {
        fs_sync(&self.path, names)
    }

    fn sync_meta_data(&self) -> io::Result<()> {
        fs_sync_meta_data(&self.path)
    }
}

/// Filesystem-backed IndexOutput wrapping a `BufWriter<File>` with CRC32 tracking.
pub struct FSIndexOutput {
    name: String,
    writer: Option<BufWriter<File>>,
    crc: CRC32,
    position: u64,
}

impl FSIndexOutput {
    fn new(name: String, writer: BufWriter<File>) -> Self {
        Self {
            name,
            writer: Some(writer),
            crc: CRC32::new(),
            position: 0,
        }
    }

    fn writer(&mut self) -> &mut BufWriter<File> {
        self.writer
            .as_mut()
            .expect("writer already consumed by into_inner")
    }

    /// Flushes the BufWriter and returns the underlying File.
    pub fn into_inner(mut self) -> io::Result<File> {
        let mut writer = self.writer.take().expect("writer already consumed");
        writer.flush()?;
        writer.into_inner().map_err(io::Error::other)
    }
}

impl Drop for FSIndexOutput {
    fn drop(&mut self) {
        if let Some(ref mut writer) = self.writer {
            let _ = writer.flush();
        }
    }
}

impl io::Write for FSIndexOutput {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.writer().write_all(buf)?;
        self.crc.update(buf);
        self.position += buf.len() as u64;
        Ok(buf.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        if let Some(ref mut writer) = self.writer {
            writer.flush()
        } else {
            Ok(())
        }
    }
}

impl DataOutput for FSIndexOutput {
    fn write_byte(&mut self, b: u8) -> io::Result<()> {
        self.writer().write_all(&[b])?;
        self.crc.update_byte(b);
        self.position += 1;
        Ok(())
    }
}

impl IndexOutput for FSIndexOutput {
    fn name(&self) -> &str {
        &self.name
    }

    fn file_pointer(&self) -> u64 {
        self.position
    }

    fn checksum(&self) -> u64 {
        self.crc.value()
    }
}

#[cfg(test)]
mod tests {
    use std::env;
    use std::process;

    use std::io::Write;

    use super::*;

    fn temp_dir(name: &str) -> PathBuf {
        let dir = env::temp_dir().join(format!("rustlucene_test_fs_{name}_{}", process::id()));
        // Clean up any leftover from a previous run
        let _ = fs::remove_dir_all(&dir);
        dir
    }

    #[test]
    fn test_fs_directory_write_and_list() {
        let dir_path = temp_dir("write_and_list");
        let dir = FSDirectory::open(&dir_path).unwrap();
        let _cleanup = DirCleanup(&dir_path);

        dir.write_file("beta.bin", b"hello").unwrap();
        dir.write_file("alpha.bin", b"world").unwrap();

        let files = dir.list_all().unwrap();
        assert_eq!(files, vec!["alpha.bin", "beta.bin"]);
    }

    #[test]
    fn test_fs_directory_file_length() {
        let dir_path = temp_dir("file_length");
        let dir = FSDirectory::open(&dir_path).unwrap();
        let _cleanup = DirCleanup(&dir_path);

        dir.write_file("test.bin", b"hello world").unwrap();
        assert_eq!(dir.file_length("test.bin").unwrap(), 11);
    }

    #[test]
    fn test_fs_directory_delete_file() {
        let dir_path = temp_dir("delete_file");
        let dir = FSDirectory::open(&dir_path).unwrap();
        let _cleanup = DirCleanup(&dir_path);

        dir.write_file("test.bin", b"data").unwrap();
        assert_ok!(dir.delete_file("test.bin"));
        assert_err!(dir.delete_file("test.bin"));
    }

    #[test]
    fn test_fs_directory_rename() {
        let dir_path = temp_dir("rename");
        let dir = FSDirectory::open(&dir_path).unwrap();
        let _cleanup = DirCleanup(&dir_path);

        dir.write_file("old.bin", b"data").unwrap();
        dir.rename("old.bin", "new.bin").unwrap();

        assert_err!(dir.file_length("old.bin"));
        assert_eq!(dir.file_length("new.bin").unwrap(), 4);
    }

    #[test]
    fn test_fs_directory_create_output() {
        let dir_path = temp_dir("create_output");
        let dir = FSDirectory::open(&dir_path).unwrap();
        let _cleanup = DirCleanup(&dir_path);

        {
            let mut out = dir.create_output("test.bin").unwrap();
            out.write_all(b"hello").unwrap();
            assert_eq!(out.file_pointer(), 5);
            assert_eq!(out.name(), "test.bin");
            // CRC32 of "hello" = 0x3610A686
            assert_eq!(out.checksum(), 0x3610A686);
        }

        // Verify file contents on disk
        let contents = fs::read(dir_path.join("test.bin")).unwrap();
        assert_eq!(contents, b"hello");
    }

    #[test]
    fn test_fs_output_into_inner() {
        let dir_path = temp_dir("into_inner");
        fs::create_dir_all(&dir_path).unwrap();
        let _cleanup = DirCleanup(&dir_path);

        let file = File::create(dir_path.join("into_inner.bin")).unwrap();
        let writer = BufWriter::new(file);
        let mut fs_out = FSIndexOutput::new("into_inner.bin".to_string(), writer);
        fs_out.write_all(b"test data").unwrap();
        let file = fs_out.into_inner().unwrap();
        drop(file);

        // Verify file contents on disk
        let contents = fs::read(dir_path.join("into_inner.bin")).unwrap();
        assert_eq!(contents, b"test data");
    }

    #[test]
    fn test_fs_directory_read_file() {
        let dir_path = temp_dir("read_file");
        let dir = FSDirectory::open(&dir_path).unwrap();
        let _cleanup = DirCleanup(&dir_path);

        dir.write_file("test.bin", b"hello world").unwrap();
        let data = dir.read_file("test.bin").unwrap();
        assert_eq!(data, b"hello world");

        assert_err!(dir.read_file("nonexistent.bin"));
    }

    #[test]
    fn test_fs_directory_sync() {
        let dir_path = temp_dir("sync");
        let dir = FSDirectory::open(&dir_path).unwrap();
        let _cleanup = DirCleanup(&dir_path);

        dir.write_file("sync_test.bin", b"data").unwrap();
        dir.sync(&["sync_test.bin"]).unwrap();
    }

    #[test]
    fn test_fs_directory_sync_meta_data() {
        let dir_path = temp_dir("sync_meta");
        let dir = FSDirectory::open(&dir_path).unwrap();
        let _cleanup = DirCleanup(&dir_path);

        dir.sync_meta_data().unwrap();
    }

    #[test]
    fn test_fs_output_write_byte() {
        let dir_path = temp_dir("write_byte");
        let dir = FSDirectory::open(&dir_path).unwrap();
        let _cleanup = DirCleanup(&dir_path);

        {
            let mut out = dir.create_output("byte_test.bin").unwrap();
            out.write_byte(0x42).unwrap();
            out.write_byte(0x43).unwrap();
            assert_eq!(out.file_pointer(), 2);
        }

        let contents = fs::read(dir_path.join("byte_test.bin")).unwrap();
        assert_eq!(contents, &[0x42, 0x43]);
    }

    #[test]
    fn test_fs_output_checksum() {
        let dir_path = temp_dir("checksum");
        let dir = FSDirectory::open(&dir_path).unwrap();
        let _cleanup = DirCleanup(&dir_path);

        let mut out = dir.create_output("checksum.bin").unwrap();
        out.write_all(b"test").unwrap();

        // Verify checksum matches manually computed CRC32
        let mut crc = CRC32::new();
        crc.update(b"test");
        assert_eq!(out.checksum(), crc.value());
    }

    #[test]
    fn test_fs_directory_open_file_returns_owned_with_correct_bytes() {
        use crate::store::FileBacking;

        let dir_path = temp_dir("open_file");
        let dir = FSDirectory::open_with_file_handles(&dir_path).unwrap();
        let _cleanup = DirCleanup(&dir_path);

        dir.write_file("backing.bin", b"hello fs backing").unwrap();
        let backing = dir.open_file("backing.bin").unwrap();

        assert_matches!(backing, FileBacking::Owned(_));
        assert_eq!(backing.as_bytes(), b"hello fs backing");
        assert_len_eq_x!(backing, 16);
    }

    #[test]
    fn test_fs_directory_open_file_missing() {
        let dir_path = temp_dir("open_file_missing");
        let dir = FSDirectory::open_with_file_handles(&dir_path).unwrap();
        let _cleanup = DirCleanup(&dir_path);

        assert_err!(dir.open_file("nonexistent.bin"));
    }

    /// RAII helper to clean up temp directories after tests.
    struct DirCleanup<'a>(&'a Path);

    impl<'a> Drop for DirCleanup<'a> {
        fn drop(&mut self) {
            let _ = fs::remove_dir_all(self.0);
        }
    }
}