evolution_slicer/
slicer.rs

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
//
// MIT License
//
// Copyright (c) 2024 Firelink Data
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// File created: 2023-12-11
// Last updated: 2024-05-31
//

use evolution_common::error::{ExecutionError, Result};
use log::warn;

use std::fs::{File, OpenOptions};
use std::io::{BufReader, ErrorKind, Read};
use std::path::PathBuf;
use std::sync::Arc;

///
pub trait Slicer {
    fn is_done(&self) -> bool;
}

///
pub type SlicerRef = Arc<dyn Slicer>;

///
pub struct FileSlicer {
    inner: BufReader<File>,
    bytes_to_read: usize,
    remaining_bytes: usize,
    bytes_processed: usize,
    bytes_overlapped: usize,
}

impl FileSlicer {
    /// Try creating a new [`FileSlicer`] from a relative or absolute path
    /// to the fixed-length file that is to be sliced.
    ///
    /// # Errors
    /// This function can return an error for the following reasons:
    /// * Any I/O error was returned when trying to open the path as a file.
    /// * Could not read the metadata of the file at the path.
    pub fn try_from_path(in_path: PathBuf) -> Result<Self> {
        let file: File = OpenOptions::new().read(true).open(in_path)?;

        let bytes_to_read: usize = file.metadata()?.len() as usize;
        let remaining_bytes: usize = bytes_to_read;
        let bytes_processed: usize = 0;
        let bytes_overlapped: usize = 0;

        let inner: BufReader<File> = BufReader::new(file);

        Ok(FileSlicer {
            inner,
            bytes_to_read,
            remaining_bytes,
            bytes_processed,
            bytes_overlapped,
        })
    }

    /// Create a new [`FixedLengthFileSlicer`] from a relative or absolute path to
    /// the fixed-length file that is to be sliced.
    ///
    /// # Panics
    /// This function can panic for the following reasons:
    /// * Any I/O error was returned when trying to open the path as a file.
    /// * Could not read the metadata of the file at the path.
    pub fn from_path(in_path: PathBuf) -> Self {
        FileSlicer::try_from_path(in_path).unwrap()
    }

    /// Get the total number of bytes to read.
    pub fn bytes_to_read(&self) -> usize {
        self.bytes_to_read
    }

    /// Get the number of remaining bytes to read.
    pub fn remaining_bytes(&self) -> usize {
        self.remaining_bytes
    }

    /// Set the number of remaining bytes to read.
    pub fn set_remaining_bytes(&mut self, remaining_bytes: usize) {
        self.remaining_bytes = remaining_bytes;
    }

    /// Get the total number of processed bytes.
    pub fn bytes_processed(&self) -> usize {
        self.bytes_processed
    }

    /// Set the total number of processed bytes.
    pub fn set_bytes_processed(&mut self, bytes_processed: usize) {
        self.bytes_processed = bytes_processed;
    }

    /// Get the total number of overlapped bytes (due to sliding window).
    pub fn bytes_overlapped(&self) -> usize {
        self.bytes_overlapped
    }

    /// Set the total number of overlapped bytes.
    pub fn set_bytes_overlapped(&mut self, bytes_overlapped: usize) {
        self.bytes_overlapped = bytes_overlapped;
    }

    /// Try and read from the buffered reader into the provided buffer. This function
    /// reads enough bytes to fill the buffer, hence, it is up to the caller to
    /// ensure that the buffer has the correct and/or wanted capacity.
    ///
    /// # Errors
    /// If the buffered reader encounters an EOF before completely filling the buffer.
    pub fn try_read_to_buffer(&mut self, buffer: &mut [u8]) -> Result<()> {
        match self.inner.read_exact(buffer) {
            Ok(()) => Ok(()),
            Err(e) => match e.kind() {
                ErrorKind::UnexpectedEof => {
                    warn!("EOF reached, this should be the last time reading from the file.");
                    Ok(())
                }
                _ => Err(Box::new(e)),
            },
        }
    }

    /// Read from the buffered reader into the provided buffer. This function reads
    /// enough bytes to fill the buffer, hence, it is up to the caller to ensure that
    /// that buffer has the correct and/or wanted capacity.
    ///
    /// # Panics
    /// If the buffered reader encounters an EOF before completely filling the buffer.
    pub fn read_to_buffer(&mut self, buffer: &mut [u8]) {
        self.inner.read_exact(buffer).unwrap();
    }

    /// Try and find the last linebreak character in a byte slice and return the index
    /// of the character. The function looks specifically for two character, the
    /// carriage-return (CR) and line-feed (LF) characters, represented as the character
    /// sequence '\r\n' on Windows systems.
    ///
    /// # Errors
    /// If either the byte slice to search through was empty, or there existed no linebreak
    /// character in the byte slice.
    #[cfg(target_os = "windows")]
    pub fn try_find_last_line_break(&self, bytes: &[u8]) -> Result<usize> {
        if bytes.is_empty() {
            return Err(Box::new(ExecutionError::new(
                "Byte slice to find newlines in was empty, exiting...",
            )));
        };

        let mut idx: usize = bytes.len() - 1;

        while idx > 1 {
            if (bytes[idx - 1] == 0x0d) && (bytes[idx] == 0x0a) {
                return Ok(idx - 1);
            };

            idx -= 1;
        }

        Err(Box::new(ExecutionError::new(
            "Could not find any newlines in byte slice, exiting...",
        )))
    }

    /// Try and find the last linebreak character in a byte slice and return the index
    /// of the character. The function looks specifically for a line-feed (LF) character,
    /// represented as '\n' on Unix systems.
    ///
    /// # Errors
    /// If either the byte slice to search through was empty, or there existed no linebreak
    /// character in the byte slice.
    #[cfg(not(target_os = "windows"))]
    pub fn try_find_last_line_break(&self, bytes: &[u8]) -> Result<usize> {
        if bytes.is_empty() {
            return Err(Box::new(ExecutionError::new(
                "Byte slice to find newlines in was empty, exiting...",
            )));
        };

        let mut idx: usize = bytes.len() - 1;

        while idx > 0 {
            if bytes[idx] == 0x0a {
                return Ok(idx);
            };

            idx -= 1;
        }

        Err(Box::new(ExecutionError::new(
            "Could not find any newlines in byte slice, exiting...",
        )))
    }

    /// Try and find all occurances of linebreak characters in a byte slice and push
    /// the index of the byte to a provided buffer. The function looks specifically
    /// for two characters, the carriage-return (CR) and line-feed (LF) characters,
    /// represented as the character sequence '\r\n' on Windows systems.
    ///
    /// # Errors
    /// If the byte slice to search through was empty.
    #[cfg(target_os = "windows")]
    pub fn try_find_line_breaks(
        &self,
        bytes: &[u8],
        buffer: &mut Vec<usize>,
        add_starting_idx: bool,
    ) -> Result<()> {
        if bytes.is_empty() {
            return Err(Box::new(ExecutionError::new(
                "Byte slice to find newlines in was empty, exiting...",
            )));
        };

        // We need to also set the starting position of the current buffer, which is on index 0.
        // This is needed for multitthreading when threads need to know the byte indices of their slice.
        if add_starting_idx {
            buffer.push(0);
        }

        (1..bytes.len()).for_each(|idx| {
            if (bytes[idx - 1] == 0x0d) && (bytes[idx] == 0x0a) {
                buffer.push(idx - 1);
            };
        });

        Ok(())
    }
    /// Try and find all occurances of linebreak characters in a byte slice and push
    /// the index of the byte to a provided buffer. The function looks specifically
    /// for a line-feed (LF) character, represented as '\n' on Unix systems.
    ///
    /// # Errors
    /// If the byte slice to search through was empty.
    #[cfg(not(target_os = "windows"))]
    pub fn try_find_line_breaks(
        &self,
        bytes: &[u8],
        buffer: &mut Vec<usize>,
        add_starting_idx: bool,
    ) -> Result<()> {
        if bytes.is_empty() {
            return Err(Box::new(ExecutionError::new(
                "Byte slice to find newlines in was empty, exiting...",
            )));
        };

        // We need to also set the starting position of the current buffer, which is on index 0.
        // This is needed for multitthreading when threads need to know the byte indices of their slice.
        if add_starting_idx {
            buffer.push(0);
        }

        (0..bytes.len()).for_each(|idx| {
            if bytes[idx] == 0x0a {
                buffer.push(idx);
            };
        });

        Ok(())
    }

    /// Try and seek relative to the current position in the buffered reader.
    ///
    /// # Errors
    /// Seeking to a negative offset will return an error.
    pub fn try_seek_relative(&mut self, bytes_to_seek: i64) -> Result<()> {
        self.inner.seek_relative(bytes_to_seek)?;
        Ok(())
    }

    /// Seek relative to the current position in the buffered reader.
    ///
    /// # Panics
    /// Seeking to a negative offset will cause the program to panic.
    pub fn seek_relative(&mut self, bytes_to_seek: i64) {
        self.try_seek_relative(bytes_to_seek).unwrap()
    }
}

impl Slicer for FileSlicer {
    /// Get whether or not this [`Slicer`] is done reading the input file.
    fn is_done(&self) -> bool {
        self.bytes_processed >= self.bytes_to_read
    }
}