Skip to main content

diskann_disk/utils/aligned_file_reader/
aligned_read.rs

1/*
2 * Copyright (c) Microsoft Corporation.
3 * Licensed under the MIT license.
4 */
5
6use diskann::{ANNError, ANNResult};
7
8pub const DISK_IO_ALIGNMENT: usize = 512;
9
10/// Aligned read struct for disk IO, it takes the ownership of the AlignedBoxedSlice and returns the AlignedBoxWithSlice data immutably.
11pub struct AlignedRead<'a, T> {
12    /// where to read from
13    /// offset needs to be aligned with DISK_IO_ALIGNMENT
14    offset: u64,
15
16    /// where to read into
17    /// aligned_buf and its len need to be aligned with DISK_IO_ALIGNMENT
18    aligned_buf: &'a mut [T],
19}
20
21impl<'a, T> AlignedRead<'a, T> {
22    pub fn new(offset: u64, aligned_buf: &'a mut [T]) -> ANNResult<Self> {
23        Self::assert_is_aligned(offset as usize)?;
24        Self::assert_is_aligned(std::mem::size_of_val(aligned_buf))?;
25
26        Ok(Self {
27            offset,
28            aligned_buf,
29        })
30    }
31
32    fn assert_is_aligned(val: usize) -> ANNResult<()> {
33        match val % DISK_IO_ALIGNMENT {
34            0 => Ok(()),
35            _ => Err(ANNError::log_disk_io_request_alignment_error(format!(
36                "The offset or length of AlignedRead request is not {} bytes aligned",
37                DISK_IO_ALIGNMENT
38            ))),
39        }
40    }
41
42    /// where to read from
43    /// offset needs to be aligned with DISK_IO_ALIGNMENT
44    pub fn offset(&self) -> u64 {
45        self.offset
46    }
47
48    pub fn aligned_buf(&self) -> &[T] {
49        self.aligned_buf
50    }
51
52    /// where to read into
53    /// aligned_buf and its len need to be aligned with DISK_IO_ALIGNMENT
54    pub fn aligned_buf_mut(&mut self) -> &mut [T] {
55        self.aligned_buf
56    }
57}