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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
use std::path::PathBuf;
use crate::{error::StorageIOError, store::ChunkingResult};
/// Split data by lines (newline characters)
pub fn split_by_lines_impl(data: &[u8]) -> Result<ChunkingResult, StorageIOError> {
let mut chunks = Vec::new();
let mut start = 0;
let total_size = data.len();
// Iterate through data to find line boundaries
let mut i = 0;
while i < data.len() {
if data[i] == b'\n' {
// Unix line ending
let line_end = i + 1; // Include \n
// Extract line data (include newline character)
let line_data = data[start..line_end].to_vec();
// Create chunk for this line
let chunk = crate::store::create_chunk(line_data);
chunks.push(chunk);
// Move start to next line
start = line_end;
i = line_end;
} else if data[i] == b'\r' && i + 1 < data.len() && data[i + 1] == b'\n' {
// Windows line ending
let line_end = i + 2; // Include both \r and \n
// Extract line data (include newline characters)
let line_data = data[start..line_end].to_vec();
// Create chunk for this line
let chunk = crate::store::create_chunk(line_data);
chunks.push(chunk);
// Move start to next line
start = line_end;
i = line_end;
} else {
i += 1;
}
}
// Handle remaining data (last line without newline)
if start < total_size {
let line_data = data[start..].to_vec();
let chunk = crate::store::create_chunk(line_data);
chunks.push(chunk);
}
// Handle empty file (no lines)
if chunks.is_empty() && total_size == 0 {
let chunk = crate::store::create_chunk(Vec::new());
chunks.push(chunk);
}
Ok(ChunkingResult {
chunks,
total_size: total_size as u64,
})
}
/// Split file by lines
pub async fn write_file_line<I: Into<PathBuf>>(
file_to_write: I,
storage_dir: I,
output_index_file: I,
) -> Result<(), StorageIOError> {
use crate::store::{StorageConfig, write_file};
let config = StorageConfig::line();
write_file(file_to_write, storage_dir, output_index_file, &config).await
}
/// Utility function to split data by lines with custom line ending detection
pub fn split_by_lines_custom<E: LineEnding>(data: &[u8]) -> Result<ChunkingResult, StorageIOError> {
let mut chunks = Vec::new();
let mut start = 0;
let total_size = data.len();
let mut i = 0;
while i < total_size {
if E::is_line_ending(data, i) {
let line_end = i + E::ending_length(data, i);
let line_data = data[start..line_end].to_vec();
let chunk = crate::store::create_chunk(line_data);
chunks.push(chunk);
start = line_end;
i = line_end;
} else {
i += 1;
}
}
// Handle remaining data
if start < total_size {
let line_data = data[start..].to_vec();
let chunk = crate::store::create_chunk(line_data);
chunks.push(chunk);
}
// Handle empty file
if chunks.is_empty() && total_size == 0 {
let chunk = crate::store::create_chunk(Vec::new());
chunks.push(chunk);
}
Ok(ChunkingResult {
chunks,
total_size: total_size as u64,
})
}
/// Trait for different line ending types
pub trait LineEnding {
/// Check if position i is the start of a line ending
fn is_line_ending(data: &[u8], i: usize) -> bool;
/// Get the length of the line ending at position i
fn ending_length(data: &[u8], i: usize) -> usize;
}
/// Unix line endings (\n)
pub struct UnixLineEnding;
impl LineEnding for UnixLineEnding {
fn is_line_ending(data: &[u8], i: usize) -> bool {
i < data.len() && data[i] == b'\n'
}
fn ending_length(_data: &[u8], _i: usize) -> usize {
1
}
}
/// Windows line endings (\r\n)
pub struct WindowsLineEnding;
impl LineEnding for WindowsLineEnding {
fn is_line_ending(data: &[u8], i: usize) -> bool {
i + 1 < data.len() && data[i] == b'\r' && data[i + 1] == b'\n'
}
fn ending_length(_data: &[u8], _i: usize) -> usize {
2
}
}
/// Mixed line endings (detects both Unix and Windows)
pub struct MixedLineEnding;
impl LineEnding for MixedLineEnding {
fn is_line_ending(data: &[u8], i: usize) -> bool {
if i < data.len() && data[i] == b'\n' {
true
} else if i + 1 < data.len() && data[i] == b'\r' && data[i + 1] == b'\n' {
true
} else {
false
}
}
fn ending_length(data: &[u8], i: usize) -> usize {
if i < data.len() && data[i] == b'\n' {
1
} else if i + 1 < data.len() && data[i] == b'\r' && data[i + 1] == b'\n' {
2
} else {
1 // Default to 1 if somehow called incorrectly
}
}
}
/// Detect line ending type from data
pub fn detect_line_ending(data: &[u8]) -> LineEndingType {
let mut unix_count = 0;
let mut windows_count = 0;
let mut i = 0;
while i < data.len() {
if data[i] == b'\n' {
unix_count += 1;
i += 1;
} else if i + 1 < data.len() && data[i] == b'\r' && data[i + 1] == b'\n' {
windows_count += 1;
i += 2;
} else {
i += 1;
}
}
if unix_count > windows_count {
LineEndingType::Unix
} else if windows_count > unix_count {
LineEndingType::Windows
} else {
LineEndingType::Mixed
}
}
/// Line ending type enum
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum LineEndingType {
Unix,
Windows,
Mixed,
}
impl LineEndingType {
/// Split data using the detected line ending type
pub fn split_by_lines(&self, data: &[u8]) -> Result<ChunkingResult, StorageIOError> {
match self {
LineEndingType::Unix => split_by_lines_custom::<UnixLineEnding>(data),
LineEndingType::Windows => split_by_lines_custom::<WindowsLineEnding>(data),
LineEndingType::Mixed => split_by_lines_custom::<MixedLineEnding>(data),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_line_chunking_unix() {
let data = b"Hello\nWorld\nTest\n";
let result = split_by_lines_impl(data).unwrap();
// Should have 3 chunks
assert_eq!(result.chunks.len(), 3);
// Verify chunk contents
assert_eq!(result.chunks[0].data, b"Hello\n");
assert_eq!(result.chunks[1].data, b"World\n");
assert_eq!(result.chunks[2].data, b"Test\n");
// Verify total size
let total_chunk_size: usize = result.chunks.iter().map(|c| c.data.len()).sum();
assert_eq!(total_chunk_size, data.len());
}
#[test]
fn test_line_chunking_windows() {
let data = b"Hello\r\nWorld\r\nTest\r\n";
let result = split_by_lines_impl(data).unwrap();
// Should have 3 chunks
assert_eq!(result.chunks.len(), 3);
// Verify chunk contents (should include \r\n)
assert_eq!(result.chunks[0].data, b"Hello\r\n");
assert_eq!(result.chunks[1].data, b"World\r\n");
assert_eq!(result.chunks[2].data, b"Test\r\n");
}
#[test]
fn test_line_chunking_mixed() {
let data = b"Hello\nWorld\r\nTest\n";
let result = split_by_lines_impl(data).unwrap();
// Should have 3 chunks
assert_eq!(result.chunks.len(), 3);
// Verify chunk contents
assert_eq!(result.chunks[0].data, b"Hello\n");
assert_eq!(result.chunks[1].data, b"World\r\n");
assert_eq!(result.chunks[2].data, b"Test\n");
}
#[test]
fn test_line_chunking_no_trailing_newline() {
let data = b"Hello\nWorld\nTest";
let result = split_by_lines_impl(data).unwrap();
// Should have 3 chunks
assert_eq!(result.chunks.len(), 3);
// Verify chunk contents
assert_eq!(result.chunks[0].data, b"Hello\n");
assert_eq!(result.chunks[1].data, b"World\n");
assert_eq!(result.chunks[2].data, b"Test");
}
#[test]
fn test_line_chunking_empty_lines() {
let data = b"Hello\n\nWorld\n\n\n";
let result = split_by_lines_impl(data).unwrap();
// Should have 5 chunks (including empty lines)
// "Hello\n", "\n", "World\n", "\n", "\n"
assert_eq!(result.chunks.len(), 5);
// Verify chunk contents
assert_eq!(result.chunks[0].data, b"Hello\n");
assert_eq!(result.chunks[1].data, b"\n");
assert_eq!(result.chunks[2].data, b"World\n");
assert_eq!(result.chunks[3].data, b"\n");
assert_eq!(result.chunks[4].data, b"\n");
}
#[test]
fn test_line_chunking_empty_file() {
let data = b"";
let result = split_by_lines_impl(data).unwrap();
// Should have 1 empty chunk
assert_eq!(result.chunks.len(), 1);
assert_eq!(result.chunks[0].data, b"");
}
#[test]
fn test_detect_line_ending() {
// Test Unix detection
let unix_data = b"Hello\nWorld\n";
assert_eq!(detect_line_ending(unix_data), LineEndingType::Unix);
// Test Windows detection
let windows_data = b"Hello\r\nWorld\r\n";
assert_eq!(detect_line_ending(windows_data), LineEndingType::Windows);
// Test mixed detection
let mixed_data = b"Hello\nWorld\r\n";
assert_eq!(detect_line_ending(mixed_data), LineEndingType::Mixed);
// Test no newlines
let no_newlines = b"Hello World";
assert_eq!(detect_line_ending(no_newlines), LineEndingType::Mixed);
}
#[test]
fn test_custom_line_ending_unix() {
let data = b"Hello\nWorld\n";
let result = split_by_lines_custom::<UnixLineEnding>(data).unwrap();
assert_eq!(result.chunks.len(), 2);
assert_eq!(result.chunks[0].data, b"Hello\n");
assert_eq!(result.chunks[1].data, b"World\n");
}
#[test]
fn test_custom_line_ending_windows() {
let data = b"Hello\r\nWorld\r\n";
let result = split_by_lines_custom::<WindowsLineEnding>(data).unwrap();
assert_eq!(result.chunks.len(), 2);
assert_eq!(result.chunks[0].data, b"Hello\r\n");
assert_eq!(result.chunks[1].data, b"World\r\n");
}
#[test]
fn test_line_ending_type_split() {
let unix_data = b"Hello\nWorld\n";
let windows_data = b"Hello\r\nWorld\r\n";
let mixed_data = b"Hello\nWorld\r\n";
// Test Unix
let unix_result = LineEndingType::Unix.split_by_lines(unix_data).unwrap();
assert_eq!(unix_result.chunks.len(), 2);
// Test Windows
let windows_result = LineEndingType::Windows
.split_by_lines(windows_data)
.unwrap();
assert_eq!(windows_result.chunks.len(), 2);
// Test Mixed
let mixed_result = LineEndingType::Mixed.split_by_lines(mixed_data).unwrap();
assert_eq!(mixed_result.chunks.len(), 2);
}
#[test]
fn test_chunk_hash_uniqueness() {
// Test that different lines produce different hashes
let data1 = b"Hello\n";
let data2 = b"World\n";
let result1 = split_by_lines_impl(data1).unwrap();
let result2 = split_by_lines_impl(data2).unwrap();
assert_ne!(result1.chunks[0].hash, result2.chunks[0].hash);
}
}
|