batch_mode_tts/
high_water.rs

1crate::ix!();
2
3impl BatchModeTtsJob {
4    /// Find the largest contiguous existing non‑empty part index in `work_dir`
5    /// matching the current response format extension. Returns:
6    /// - `Ok(Some(idx))` for the last existing contiguous index (0‑based),
7    /// - `Ok(None)` if no parts exist,
8    /// - `Err(io::Error)` on unexpected I/O failure.
9    pub fn find_high_water_mark(work_dir: &Path, ext: &str) -> io::Result<Option<usize>> {
10        let mut i: usize = 0;
11        loop {
12            let p = work_dir.join(format!("part_{i:04}.{ext}"));
13            match std::fs::metadata(&p) {
14                Ok(meta) => {
15                    if meta.len() == 0 {
16                        warn!(
17                            "Encountered zero‑length file at contiguous index #{i} ({:?}); \
18                             treating as boundary for high‑water mark",
19                            p
20                        );
21                        break;
22                    }
23                    trace!("Detected existing part #{i} ({:?})", p);
24                    i += 1;
25                    continue;
26                }
27                Err(e) if e.kind() == io::ErrorKind::NotFound => {
28                    // First gap — stop contiguous scan
29                    break;
30                }
31                Err(e) => {
32                    error!("metadata() failed while scanning for high‑water mark: {e}");
33                    return Err(e);
34                }
35            }
36        }
37
38        if i == 0 {
39            Ok(None)
40        } else {
41            Ok(Some(i - 1))
42        }
43    }
44}