Skip to main content

diskann_disk/utils/aligned_file_reader/
aligned_file_reader_factory.rs

1/*
2 * Copyright (c) Microsoft Corporation.
3 * Licensed under the MIT license.
4 */
5
6use diskann::ANNResult;
7
8#[cfg(all(not(miri), target_os = "linux"))]
9use super::LinuxAlignedFileReader;
10#[cfg(any(miri, target_os = "macos"))]
11use super::StorageProviderAlignedFileReader;
12#[cfg(all(not(miri), target_os = "windows"))]
13use super::WindowsAlignedFileReader;
14use crate::utils::aligned_file_reader::traits::AlignedReaderFactory;
15
16#[cfg(any(miri, target_os = "macos"))]
17use diskann_providers::storage::FileStorageProvider;
18
19pub struct AlignedFileReaderFactory {
20    pub file_path: String,
21}
22
23impl AlignedReaderFactory for AlignedFileReaderFactory {
24    /*
25        Fall back to the StorageProviderAlignedFileReader when running in miri or on macOS.
26
27        For miri: Otherwise, miri fails with this error:
28       --> C:\Users\<user>\.cargo\registry\src\msdata.pkgs.visualstudio.com-32ec7033fece98f6\io-uring-0.6.3\src\sys\mod.rs:97:15
29        |
30    97  |     to_result(syscall(SYSCALL_SETUP, entries as c_long, p as c_long) as _)
31        |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't execute syscall with ID 425
32        |
33        = help: this is likely not a bug in the program; it indicates that the program performed an operation that the interpreter does not support
34        = note: BACKTRACE on thread `algorithm::sear`:
35        = note: inside `io_uring::sys::io_uring_setup` at C:\Users\<user>\.cargo\registry\src\msdata.pkgs.visualstudio.com-32ec7033fece98f6\io-uring-0.6.3\src\sys\mod.rs:97:15: 97:69
36        = note: inside `io_uring::IoUring::with_params` at C:\Users\<user>\.cargo\registry\src\msdata.pkgs.visualstudio.com-32ec7033fece98f6\io-uring-0.6.3\src\lib.rs:152:57: 152:93
37        = note: inside `io_uring::Builder::build` at C:\Users\<user>\.cargo\registry\src\msdata.pkgs.visualstudio.com-32ec7033fece98f6\io-uring-0.6.3\src\lib.rs:412:20: 412:62
38        = note: inside `io_uring::IoUring::new` at C:\Users\<user>\.cargo\registry\src\msdata.pkgs.visualstudio.com-32ec7033fece98f6\io-uring-0.6.3\src\lib.rs:82:9: 82:39
39    note: inside `<model::aligned_file_reader::linux_aligned_file_reader::LinuxAlignedFileReader as model::aligned_file_reader::aligned_file_reader::AlignedFileReader>::read`
40       --> diskann\src\model\aligned_file_reader\linux_aligned_file_reader.rs:221:24
41        |
42    221 |         let mut ring = IoUring::new(MAX_IO_CONCURRENCY as u32)?;
43
44        For macOS: macOS does not support io_uring (Linux-only) or IOCompletionPort (Windows-only).
45        StorageProviderAlignedFileReader provides a cross-platform fallback implementation.
46         */
47    #[cfg(any(miri, target_os = "macos"))]
48    type AlignedReaderType = StorageProviderAlignedFileReader;
49
50    #[cfg(all(not(miri), target_os = "linux"))]
51    type AlignedReaderType = LinuxAlignedFileReader;
52
53    #[cfg(all(not(miri), target_os = "windows"))]
54    type AlignedReaderType = WindowsAlignedFileReader;
55
56    fn build(&self) -> ANNResult<Self::AlignedReaderType> {
57        #[cfg(any(miri, target_os = "macos"))]
58        return StorageProviderAlignedFileReader::new(
59            &FileStorageProvider,
60            self.file_path.as_str(),
61        );
62
63        #[cfg(all(not(miri), target_os = "windows"))]
64        return WindowsAlignedFileReader::new(self.file_path.as_str());
65
66        #[cfg(all(not(miri), target_os = "linux"))]
67        return LinuxAlignedFileReader::new(self.file_path.as_str());
68    }
69}
70
71impl AlignedFileReaderFactory {
72    pub fn new(file_path: String) -> Self {
73        Self { file_path }
74    }
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    #[test]
82    fn test_aligned_file_reader_factory_new() {
83        let path = "/tmp/test.bin".to_string();
84        let factory = AlignedFileReaderFactory::new(path.clone());
85        assert_eq!(factory.file_path, path);
86    }
87}