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
//! Seq Data is a simple file format that contains multiple chunks of data prefixed by a length
use std::fs::{File, OpenOptions};
use std::io::{BufReader, Read, Seek, Write};
use std::marker::PhantomData;
use std::path::Path;

/// Format configuration for SeqData
pub trait SeqDataFormat {
    /// Magic bytes. can be empty
    const MAGIC: &'static [u8];
    /// The size of the header in bytes
    const HEADER_SIZE: usize;
}

/// Writer for a new SeqData
pub struct SeqDataWriter<Format: SeqDataFormat> {
    file: File,
    phantom: PhantomData<Format>,
}

impl<Format: SeqDataFormat> SeqDataWriter<Format> {
    /// Create a new SeqData File at the location specified
    ///
    /// If the file already exists, this call will fail
    ///
    /// The header need to fits the size of Format::HEADER_SIZE
    pub fn create<P: AsRef<Path>>(path: P, header: &[u8]) -> std::io::Result<Self> {
        if Format::HEADER_SIZE != header.len() {
            return Err(std::io::Error::new(
                std::io::ErrorKind::Other,
                format!(
                    "header has invalid size, expecting {} but got {}",
                    Format::HEADER_SIZE,
                    header.len()
                ),
            ));
        }

        let mut file = OpenOptions::new()
            .read(false)
            .write(true)
            .create_new(true)
            .append(true)
            .open(path)?;
        file.write_all(&Format::MAGIC)?;
        file.write_all(header)?;
        Ok(SeqDataWriter {
            file,
            phantom: PhantomData,
        })
    }

    /// Append a new data chunk to this file
    pub fn append(&mut self, data: &[u8]) -> std::io::Result<()> {
        assert!(data.len() <= 0xffff_ffff);
        let len: u32 = data.len() as u32;
        let header = len.to_le_bytes();
        self.file.write_all(&header)?;
        self.file.write_all(data)?;
        Ok(())
    }
}

/// Reader for SeqData
pub struct SeqDataReader<Format: SeqDataFormat> {
    buf_reader: BufReader<File>,
    pos: u64,
    len: u64,
    phantom: PhantomData<Format>,
}

/// this is a version of read_exact that returns a None if the stream is empty
fn optional_read_exact<R: Read + ?Sized>(
    this: &mut R,
    mut buf: &mut [u8],
) -> Option<std::io::Result<()>> {
    let mut read_bytes = 0;
    while !buf.is_empty() {
        match this.read(buf) {
            Ok(0) => break,
            Ok(n) => {
                let tmp = buf;
                buf = &mut tmp[n..];
                read_bytes += n;
            }
            Err(ref e) if e.kind() == std::io::ErrorKind::Interrupted => {}
            Err(e) => return Some(Err(e)),
        }
    }
    if read_bytes == 0 {
        None
    } else if !buf.is_empty() {
        Some(Err(std::io::Error::new(
            std::io::ErrorKind::UnexpectedEof,
            "buffer partially filled",
        )))
    } else {
        Some(Ok(()))
    }
}

fn read_magic_and_header<Format: SeqDataFormat>(
    _format: PhantomData<Format>,
    file: &mut File,
) -> std::io::Result<Vec<u8>> {
    // try to read the magic
    const MAGIC_READ_BUF_SIZE: usize = 16;
    let mut magic_read_buf = [0u8; MAGIC_READ_BUF_SIZE];
    let mut magic_slice = Format::MAGIC;
    while !magic_slice.is_empty() {
        let sz = Format::MAGIC.len().min(MAGIC_READ_BUF_SIZE);
        let rd = file.read(&mut magic_read_buf[0..sz])?;
        if rd == 0 {
            return Err(std::io::Error::new(
                std::io::ErrorKind::UnexpectedEof,
                "unexpected EOF in magic reading",
            ));
        }
        if magic_slice[0..rd] != magic_read_buf[0..rd] {
            return Err(std::io::Error::new(
                std::io::ErrorKind::Other,
                "magic do not match expected value",
            ));
        }
        magic_slice = &magic_slice[rd..];
    }

    let mut header = vec![0u8; Format::HEADER_SIZE];
    file.read_exact(&mut header)?;
    Ok(header)
}

fn get_file_length<Format: SeqDataFormat>(
    _phantom: PhantomData<Format>,
    file: &mut File,
) -> std::io::Result<u64> {
    let meta = file.metadata()?;
    let total_len = meta.len();

    let minimum_size = Format::MAGIC.len() as u64 + Format::HEADER_SIZE as u64;
    if total_len < minimum_size {
        return Err(std::io::Error::new(
            std::io::ErrorKind::Other,
            "file not contains enough bytes for magic and header",
        ));
    }
    Ok(total_len - minimum_size)
}

impl<Format: SeqDataFormat> SeqDataReader<Format> {
    /// Open a SeqData for reading
    pub fn open<P: AsRef<Path>>(path: P) -> std::io::Result<(Self, Vec<u8>)> {
        let mut file = File::open(path)?;

        let phantom = PhantomData;
        let len = get_file_length(phantom, &mut file)?;
        let header = read_magic_and_header(phantom, &mut file)?;

        let buf_reader = BufReader::with_capacity(1024 * 1024, file);
        Ok((
            SeqDataReader {
                buf_reader,
                pos: 0,
                len,
                phantom,
            },
            header,
        ))
    }

    pub fn len(&self) -> u64 {
        self.len
    }

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

    /// Return the next block along with the current offset if it exists, or None if
    /// reached the end of file.
    pub fn next(&mut self) -> Option<std::io::Result<(u64, Vec<u8>)>> {
        let mut lenbuf = [0; 4];
        // try to read the length, if the length return a none, we just expect
        // having reached the end of the stream then
        match optional_read_exact(&mut self.buf_reader, &mut lenbuf) {
            None => None,
            Some(Err(e)) => Some(Err(e)),
            Some(Ok(())) => {
                let len = u32::from_le_bytes(lenbuf);
                let mut out = vec![0; len as usize];
                match self.buf_reader.read_exact(&mut out) {
                    Err(e) => Some(Err(e)),
                    Ok(()) => {
                        let old_pos = self.pos;
                        self.pos += 4 + len as u64;
                        Some(Ok((old_pos, out)))
                    }
                }
            }
        }
    }
}

pub fn truncate_at(path: &Path, len: u64) -> std::io::Result<()> {
    let file = OpenOptions::new()
        .read(false)
        .write(true)
        .create(false)
        .append(false)
        .open(path)?;
    file.set_len(len)?;
    Ok(())
}

/// Seq Data Reader with seek
pub struct SeqDataReaderSeek<Format: SeqDataFormat> {
    handle: File,
    phantom: PhantomData<Format>,
    start: u64,
    len: u64,
}

impl<Format: SeqDataFormat> SeqDataReaderSeek<Format> {
    /// Open a new Seq Data seeker
    pub fn open<P: AsRef<Path>>(path: P) -> std::io::Result<(Self, Vec<u8>)> {
        let mut handle = File::open(path)?;

        let phantom = PhantomData;
        let len = get_file_length(phantom, &mut handle)?;
        let header = read_magic_and_header(phantom, &mut handle)?;

        let start = handle.seek(std::io::SeekFrom::Current(0))?;

        Ok((
            Self {
                handle,
                phantom,
                len,
                start,
            },
            header,
        ))
    }

    /// Return the next block along with the current offset if it exists, or None if
    /// reached the end of file.
    pub fn next(&mut self) -> std::io::Result<Vec<u8>> {
        let mut lenbuf = [0; 4];
        // try to read the length, if the length return a none, we just expect
        // having reached the end of the stream then
        match self.handle.read_exact(&mut lenbuf) {
            Err(e) => Err(e),
            Ok(()) => {
                let len = u32::from_le_bytes(lenbuf);
                let mut out = vec![0; len as usize];
                match self.handle.read_exact(&mut out) {
                    Err(e) => Err(e),
                    Ok(()) => Ok(out),
                }
            }
        }
    }

    /// Return the next block at the offset specified
    ///
    /// Note that if the position specified is not a valid boundary,
    /// then arbitrary invalid stuff might be returns, or some Err
    /// related to reading data
    pub fn next_at(&mut self, pos: u64) -> std::io::Result<Vec<u8>> {
        if pos >= self.len {
            return Err(std::io::Error::new(
                std::io::ErrorKind::Other,
                format!(
                    "trying to access data at {} but data length {}",
                    pos, self.len
                ),
            ));
        }

        let seek = self.start + pos;
        self.handle.seek(std::io::SeekFrom::Start(seek))?;
        self.next()
    }
}