-
Notifications
You must be signed in to change notification settings - Fork 71
/
split.rs
214 lines (181 loc) · 7.87 KB
/
split.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
static USAGE: &str = r#"
Splits the given CSV data into chunks.
Uses multithreading to go faster if the CSV has an index.
The files are written to the directory given with the name '{start}.csv',
where {start} is the index of the first record of the chunk (starting at 0).
Examples:
qsv split outdir --size 100 --filename chunk_{}.csv input.csv
qsv split outputdir -s 100 --filename chunk_{}.csv --pad 5 input.csv
qsv split . -s 100 input.csv
cat in.csv | qsv split mysplitoutput -s 1000
qsv split outdir --chunks 10 input.csv
qsv split splitoutdir -c 10 -j 4 input.csv
For more examples, see https://github.com/jqnatividad/qsv/blob/master/tests/test_split.rs.
Usage:
qsv split [options] (--size <arg> | --chunks <arg>) <outdir> [<input>]
qsv split --help
split arguments:
<outdir> The directory where the output files will be written.
If it does not exist, it will be created.
<input> The CSV file to read. If not given, input is read from
STDIN.
split options:
-s, --size <arg> The number of records to write into each chunk.
[default: 500]
-c, --chunks <arg> The number of chunks to split the data into.
This option is mutually exclusive with --size.
The number of rows in each chunk is determined by
the number of records in the CSV data and the number
of desired chunks. If the number of records is not evenly
divisible by the number of chunks, the last chunk will
have fewer records.
-j, --jobs <arg> The number of splitting jobs to run in parallel.
This only works when the given CSV data has
an index already created. Note that a file handle
is opened for each job.
When not set, the number of jobs is set to the
number of CPUs detected.
--filename <filename> A filename template to use when constructing
the names of the output files. The string '{}'
will be replaced by a value based on the value
of the field, but sanitized for shell safety.
[default: {}.csv]
--pad <arg> The zero padding width that is used in the
generated filename.
[default: 0]
Common options:
-h, --help Display this message
-n, --no-headers When set, the first row will NOT be interpreted
as column names. Otherwise, the first row will
appear in all chunks as the header row.
-d, --delimiter <arg> The field delimiter for reading CSV data.
Must be a single character. (default: ,)
"#;
use std::{fs, io, path::Path};
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use serde::Deserialize;
use crate::{
config::{Config, Delimiter},
index::Indexed,
util::{self, FilenameTemplate},
CliResult,
};
#[derive(Clone, Deserialize)]
struct Args {
arg_input: Option<String>,
arg_outdir: String,
flag_size: usize,
flag_chunks: Option<usize>,
flag_jobs: Option<usize>,
flag_filename: FilenameTemplate,
flag_pad: usize,
flag_no_headers: bool,
flag_delimiter: Option<Delimiter>,
}
pub fn run(argv: &[&str]) -> CliResult<()> {
let args: Args = util::get_args(USAGE, argv)?;
if args.flag_size == 0 {
return fail_incorrectusage_clierror!("--size must be greater than 0.");
}
// check if outdir is set correctly
if Path::new(&args.arg_outdir).is_file() && args.arg_input.is_none() {
return fail_incorrectusage_clierror!("<outdir> is not specified or is a file.");
}
fs::create_dir_all(&args.arg_outdir)?;
match args.rconfig().indexed()? {
Some(idx) => args.parallel_split(&idx),
None => args.sequential_split(),
}
}
impl Args {
fn sequential_split(&self) -> CliResult<()> {
let rconfig = self.rconfig();
let mut rdr = rconfig.reader()?;
let headers = rdr.byte_headers()?.clone();
let chunk_size = if let Some(flag_chunks) = self.flag_chunks {
let count = util::count_rows(&rconfig)?;
let chunk = flag_chunks;
if chunk == 0 {
return fail_incorrectusage_clierror!("--chunk must be greater than 0.");
}
(count as f64 / chunk as f64).ceil() as usize
} else {
self.flag_size
};
let mut wtr = self.new_writer(&headers, 0, self.flag_pad)?;
let mut i = 0;
let mut row = csv::ByteRecord::new();
while rdr.read_byte_record(&mut row)? {
if i > 0 && i % chunk_size == 0 {
wtr.flush()?;
wtr = self.new_writer(&headers, i, self.flag_pad)?;
}
wtr.write_byte_record(&row)?;
i += 1;
}
wtr.flush()?;
Ok(())
}
fn parallel_split(&self, idx: &Indexed<fs::File, fs::File>) -> CliResult<()> {
let args = self.clone();
let chunk_size;
let nchunks = if let Some(flag_chunks) = args.flag_chunks {
chunk_size = (idx.count() as f64 / flag_chunks as f64).ceil() as usize;
flag_chunks
} else {
chunk_size = args.flag_size;
util::num_of_chunks(idx.count() as usize, self.flag_size)
};
if nchunks == 1 {
// there's only one chunk, we can just do a sequential split
// which has less overhead and better error handling
return self.sequential_split();
}
util::njobs(args.flag_jobs);
// safety: we cannot use ? here because we're in a closure
(0..nchunks).into_par_iter().for_each(|i| {
let conf = args.rconfig();
// safety: safe to unwrap because we know the file is indexed
let mut idx = conf.indexed().unwrap().unwrap();
// safety: the only way this can fail is if the file first row of the chunk
// is not a valid CSV record, which is impossible because we're reading
// from a file with a valid index
let headers = idx.byte_headers().unwrap();
let mut wtr = args
// safety: the only way this can fail is if we cannot create a file
.new_writer(headers, i * chunk_size, args.flag_pad)
.unwrap();
// safety: we know that there is more than one chunk, so we can safely
// seek to the start of the chunk
idx.seek((i * chunk_size) as u64).unwrap();
for row in idx.byte_records().take(chunk_size) {
let row = row.unwrap();
wtr.write_byte_record(&row).unwrap();
}
// safety: safe to unwrap because we know the writer is a file
// the only way this can fail is if we cannot write to the file
wtr.flush().unwrap();
});
Ok(())
}
fn new_writer(
&self,
headers: &csv::ByteRecord,
start: usize,
width: usize,
) -> CliResult<csv::Writer<Box<dyn io::Write + 'static>>> {
let dir = Path::new(&self.arg_outdir);
let path = dir.join(self.flag_filename.filename(&format!("{start:0>width$}")));
let spath = Some(path.display().to_string());
let mut wtr = Config::new(&spath).writer()?;
if !self.rconfig().no_headers {
wtr.write_record(headers)?;
}
Ok(wtr)
}
fn rconfig(&self) -> Config {
Config::new(&self.arg_input)
.delimiter(self.flag_delimiter)
.no_headers(self.flag_no_headers)
}
}