Skip to content

Commit cfdba60

Browse files
committed
refactor: replace remove unneeded index rebuild logic
my bad - as we do not overwrite the data - DUH!
1 parent 36d3837 commit cfdba60

File tree

2 files changed

+10
-35
lines changed

2 files changed

+10
-35
lines changed

src/cmd/replace.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,9 @@ Common options:
8888
8989
"#;
9090

91-
use std::{borrow::Cow, collections::HashSet, fs, io::BufWriter, path::Path, sync::Arc};
91+
use std::{borrow::Cow, collections::HashSet, fs, sync::Arc};
9292

9393
use crossbeam_channel;
94-
use csv_index::RandomAccessSimple;
9594
#[cfg(any(feature = "feature_capable", feature = "lite"))]
9695
use indicatif::{HumanCount, ProgressBar, ProgressDrawTarget};
9796
use regex::bytes::RegexBuilder;
@@ -100,7 +99,7 @@ use threadpool::ThreadPool;
10099

101100
use crate::{
102101
CliError, CliResult,
103-
config::{Config, DEFAULT_WTR_BUFFER_CAPACITY, Delimiter},
102+
config::{Config, Delimiter},
104103
index::Indexed,
105104
select::SelectColumns,
106105
util,
@@ -178,14 +177,14 @@ fn handle_replace_results(
178177
total_match_ctr: u64,
179178
flag_quiet: bool,
180179
flag_not_one: bool,
181-
) -> CliResult<u64> {
180+
) -> CliResult<()> {
182181
if !flag_quiet {
183182
eprintln!("{total_match_ctr}");
184183
}
185184
if total_match_ctr == 0 && !flag_not_one {
186185
return Err(CliError::NoMatch());
187186
}
188-
Ok(total_match_ctr)
187+
Ok(())
189188
}
190189

191190
pub fn run(argv: &[&str]) -> CliResult<()> {
@@ -226,21 +225,10 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
226225
if let Some(idx) = rconfig.indexed()?
227226
&& util::njobs(args.flag_jobs) > 1
228227
{
229-
let total_match_ctr = args.parallel_replace(&idx, pattern, &rconfig, replacement)?;
230-
// refresh the index if there were any replacements
231-
if total_match_ctr > 0 {
232-
let mut rdr = rconfig.reader_file()?;
233-
let mut wtr = BufWriter::with_capacity(
234-
DEFAULT_WTR_BUFFER_CAPACITY,
235-
fs::File::create(util::idx_path(Path::new(&args.arg_input.unwrap())))?,
236-
);
237-
RandomAccessSimple::create(&mut rdr, &mut wtr)?;
238-
std::io::Write::flush(&mut wtr)?;
239-
}
228+
args.parallel_replace(&idx, pattern, &rconfig, replacement)
240229
} else {
241-
let _ = args.sequential_replace(&pattern, &rconfig, replacement)?;
230+
args.sequential_replace(&pattern, &rconfig, replacement)
242231
}
243-
Ok(())
244232
}
245233

246234
impl Args {
@@ -249,7 +237,7 @@ impl Args {
249237
pattern: &regex::bytes::Regex,
250238
rconfig: &Config,
251239
replacement: &[u8],
252-
) -> CliResult<u64> {
240+
) -> CliResult<()> {
253241
let mut rdr = rconfig.reader()?;
254242
let mut wtr = Config::new(self.flag_output.as_ref()).writer()?;
255243

@@ -324,7 +312,7 @@ impl Args {
324312
pattern: regex::bytes::Regex,
325313
rconfig: &Config,
326314
replacement: &[u8],
327-
) -> CliResult<u64> {
315+
) -> CliResult<()> {
328316
use rayon::slice::ParallelSliceMut;
329317

330318
let mut rdr = rconfig.reader()?;
@@ -333,7 +321,7 @@ impl Args {
333321

334322
let idx_count = idx.count() as usize;
335323
if idx_count == 0 {
336-
return Ok(0);
324+
return Ok(());
337325
}
338326

339327
let njobs = util::njobs(self.flag_jobs);

tests/test_replace.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -816,12 +816,7 @@ fn replace_indexed_parallel() {
816816
cmd.arg("data.csv");
817817
wrk.assert_success(&mut cmd);
818818

819-
// get the timestamp of the index file
820-
let index_file = wrk.path("data.csv.idx");
821-
let index_file_metadata = std::fs::metadata(index_file).unwrap();
822-
let index_file_timestamp = index_file_metadata.modified().unwrap();
823-
824-
std::thread::sleep(std::time::Duration::from_secs(2));
819+
std::thread::sleep(std::time::Duration::from_secs(1));
825820

826821
// should still have the same output
827822
let mut cmd = wrk.command("replace");
@@ -835,12 +830,4 @@ fn replace_indexed_parallel() {
835830
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
836831
assert_eq!(got, expected);
837832
wrk.assert_success(&mut cmd);
838-
839-
// get the timestamp of the index file
840-
// it should be higher than the original timestamp
841-
// as we auto-reindex the file after a replace operation
842-
let index_file = wrk.path("data.csv.idx");
843-
let index_file_metadata = std::fs::metadata(index_file).unwrap();
844-
let index_file_timestamp_new = index_file_metadata.modified().unwrap();
845-
assert!(index_file_timestamp_new > index_file_timestamp);
846833
}

0 commit comments

Comments
 (0)