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
use super::message::{MessageBuf, MessageError};
use std::fs::File;

/// Trait that allows reading from a slice of the log.
pub trait LogSliceReader {
    /// Result type of this reader.
    type Result: 'static;

    /// Reads the slice of the file containing the message set.
    ///
    /// * `file` - The segment file that contains the slice of the log.
    /// * `file_position` - The offset within the file that starts the slice.
    /// * `bytes` - Total number of bytes, from the offset, that contains the message set slice.
    fn read_from(
        &mut self,
        file: &File,
        file_position: u32,
        bytes: usize,
    ) -> Result<Self::Result, MessageError>;

    /// Called when there are no commits in the log in the desired range.
    fn empty() -> Self::Result;
}

#[cfg(unix)]
#[derive(Default)]
/// Reader of the file segment into memory.
pub struct MessageBufReader;

impl LogSliceReader for MessageBufReader {
    type Result = MessageBuf;

    fn read_from(
        &mut self,
        file: &File,
        file_position: u32,
        bytes: usize,
    ) -> Result<Self::Result, MessageError> {
        use std::os::unix::fs::FileExt;

        let mut vec = vec![0; bytes];
        try!(file.read_at(&mut vec, u64::from(file_position)));
        MessageBuf::from_bytes(vec)
    }

    fn empty() -> Self::Result {
        MessageBuf::default()
    }
}