kevy-cli 7.0.0

kevy operations CLI: redis-cli-style REPL plus the migration toolchain (import/export/verify/diff/inspect/digest).
Documentation
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//! Migration toolchain: `export` / `import`.
//!
//! Wire format = a RESP command stream of rebuild frames (SET / HSET /
//! RPUSH / SADD / ZADD / PEXPIREAT) — bidirectionally compatible with
//! `redis-cli --pipe`. Export walks SCAN cursors (per-key
//! point-in-time; SCAN-class consistency). Import pipelines 512
//! commands per batch with a fsynced progress file for `--resume`.
//! Every key's frames start with DEL, so replay REBUILDS the key from
//! scratch — genuinely idempotent for every type (RPUSH would
//! otherwise append on re-import; the round-trip test caught exactly
//! that). No cross-batch atomicity to lose.
//!
//! ```
//! use kevy_cli::migrate::{ImportStart, OnErrorReply, run_export, run_import};
//! use kevy_resp_client::Reply;
//! # let port = include!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/doc_server/serve.rs"));
//! let mut client = kevy_resp_client::RespClient::connect("127.0.0.1", port)?;
//! # let port_b = include!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/doc_server/serve.rs"));
//! let mut target = kevy_resp_client::RespClient::connect("127.0.0.1", port_b)?;
//! client.request_borrowed(&[b"RPUSH", b"user:1:tags", b"a", b"b"])?;
//! client.request_borrowed(&[b"SET", b"user:1:name", b"ada"])?;
//!
//! let dir = kevy_tmpdir::TmpDir::new("migrate-roundtrip");
//! let file = dir.path().join("users.resp");
//! assert_eq!(run_export(&mut client, Some(&b"user:"[..]), &file)?.keys, 2);
//! // replay is idempotent: every key is rebuilt from scratch, so twice is once
//! for _ in 0..2 {
//!     run_import(&mut target, &file, ImportStart::Fresh, OnErrorReply::Abort)?;
//! }
//! let tags = target.request_borrowed(&[b"LLEN", b"user:1:tags"])?;
//! assert_eq!(tags, Reply::Int(2));
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```

use std::fs::{File, OpenOptions};
use std::io::{self, BufWriter, Read, Seek, SeekFrom, Write};
use std::path::Path;

use crate::link::Link;
use kevy_resp::Reply;

mod rebuild;
use rebuild::export_key;
pub(crate) use rebuild::{Rebuilt, rebuild_frames};

const PIPELINE: usize = 512;

/// What an export did — including what it did NOT do. The skipped map
/// is the half that used to be invisible: a type with no rebuild verb
/// produced no frames, no error and no mention, so a migration could
/// report success while leaving a whole type behind.
///
/// ```
/// use kevy_cli::migrate::run_export;
/// # let port = include!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/doc_server/serve.rs"));
/// let mut client = kevy_resp_client::RespClient::connect("127.0.0.1", port)?;
/// client.request_borrowed(&[b"SET", b"k", b"v"])?;
/// client.request_borrowed(&[b"XADD", b"events", b"*", b"f", b"v"])?;
/// # let dir = kevy_tmpdir::TmpDir::new("migrate");
/// let e = run_export(&mut client, None, &dir.path().join("all.resp"))?;
/// if !e.skipped.is_empty() {
///     eprintln!("left behind: {:?}", e.skipped); // say so rather than report success
/// }
/// assert_eq!(e.keys, 1);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub struct Export {
    /// Keys whose frames are in the file.
    ///
    /// ```
    /// use kevy_cli::migrate::run_export;
    /// # let port = include!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/doc_server/serve.rs"));
    /// let mut client = kevy_resp_client::RespClient::connect("127.0.0.1", port)?;
    /// client.request_borrowed(&[b"SET", b"a", b"1"])?;
    /// client.request_borrowed(&[b"HSET", b"b", b"f", b"v"])?;
    /// # let dir = kevy_tmpdir::TmpDir::new("migrate");
    /// assert_eq!(run_export(&mut client, None, &dir.path().join("x.resp"))?.keys, 2);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub keys: u64,
    /// Type name -> keys left out because nothing here rebuilds them.
    ///
    /// ```
    /// use kevy_cli::migrate::run_export;
    /// # let port = include!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/doc_server/serve.rs"));
    /// let mut client = kevy_resp_client::RespClient::connect("127.0.0.1", port)?;
    /// client.request_borrowed(&[b"XADD", b"events", b"*", b"f", b"v"])?;
    /// # let dir = kevy_tmpdir::TmpDir::new("migrate");
    /// let e = run_export(&mut client, None, &dir.path().join("x.resp"))?;
    /// assert_eq!(e.skipped.get(&b"stream".to_vec()), Some(&1));
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub skipped: std::collections::BTreeMap<Vec<u8>, u64>,
}

/// Walk the keyspace (optionally under `prefix`) and write rebuild
/// frames to `out_path`, reporting both what went in and what could
/// not.
///
/// ```
/// use kevy_cli::migrate::run_export;
/// # let port = include!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/doc_server/serve.rs"));
/// let mut client = kevy_resp_client::RespClient::connect("127.0.0.1", port)?;
/// client.request_borrowed(&[b"SET", b"user:1", b"ada"])?;
/// client.request_borrowed(&[b"SET", b"order:1", b"open"])?;
/// # let dir = kevy_tmpdir::TmpDir::new("migrate");
/// let file = dir.path().join("users.resp");
/// assert_eq!(run_export(&mut client, Some(&b"user:"[..]), &file)?.keys, 1);
/// // the file is plain RESP, `redis-cli --pipe` compatible: DEL, then the rebuild
/// let text = std::fs::read_to_string(&file)?;
/// assert!(text.starts_with("*2\r\n$3\r\nDEL\r\n$6\r\nuser:1\r\n*3\r\n$3\r\nSET\r\n"));
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn run_export(
    client: &mut dyn Link,
    prefix: Option<&[u8]>,
    out_path: &Path,
) -> io::Result<Export> {
    let mut out = BufWriter::new(File::create(out_path)?);
    let mut cursor: Vec<u8> = b"0".to_vec();
    let mut pattern = prefix.unwrap_or_default().to_vec();
    pattern.push(b'*');
    let mut n = 0u64;
    // Types with no rebuild verb, counted by name. Silence here is how
    // a migration loses a whole type and reports success.
    let mut skipped: std::collections::BTreeMap<Vec<u8>, u64> = Default::default();
    loop {
        let reply =
            client.request_borrowed(&[b"SCAN", &cursor, b"MATCH", &pattern, b"COUNT", b"512"])?;
        let Reply::Array(items) = reply else {
            return Err(io::Error::new(io::ErrorKind::InvalidData, "SCAN reply shape"));
        };
        let (Some(Reply::Bulk(next)), Some(Reply::Array(keys))) = (items.first(), items.get(1))
        else {
            return Err(io::Error::new(io::ErrorKind::InvalidData, "SCAN reply shape"));
        };
        let next = next.clone();
        for k in keys {
            let Reply::Bulk(key) = k else { continue };
            let key = key.clone();
            match export_key(client, &key, &mut out)? {
                Some(None) => n += 1,
                Some(Some(ty)) => *skipped.entry(ty).or_insert(0u64) += 1,
                None => {}
            }
        }
        cursor = next;
        if cursor == b"0" {
            break;
        }
    }
    out.flush()?;
    Ok(Export { keys: n, skipped })
}

/// Import stats.
///
/// ```
/// use kevy_cli::migrate::{ImportStart, OnErrorReply, run_import};
/// # let port = include!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/doc_server/serve.rs"));
/// let mut client = kevy_resp_client::RespClient::connect("127.0.0.1", port)?;
/// # let dir = kevy_tmpdir::TmpDir::new("migrate");
/// # let src = dir.path().join("cmds.resp");
/// # let mut raw = Vec::new();
/// # for argv in [&[&b"SET"[..], b"a", b"1"][..], &[b"SET", b"b", b"x"], &[b"INCR", b"b"]] {
/// #     kevy_resp::encode_command_borrowed(&mut raw, argv);
/// # }
/// # std::fs::write(&src, raw)?;
/// let r = run_import(&mut client, &src, ImportStart::Fresh, OnErrorReply::Count)?;
/// assert_eq!((r.sent, r.errors), (2, 1));
/// assert_eq!(r.offset, std::fs::metadata(&src)?.len());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub struct ImportReport {
    /// Commands sent successfully.
    ///
    /// ```
    /// use kevy_cli::migrate::{ImportStart, OnErrorReply, run_import};
    /// # let port = include!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/doc_server/serve.rs"));
    /// let mut client = kevy_resp_client::RespClient::connect("127.0.0.1", port)?;
    /// # let dir = kevy_tmpdir::TmpDir::new("migrate");
    /// # let src = dir.path().join("cmds.resp");
    /// # let mut raw = Vec::new();
    /// # for argv in [&[&b"SET"[..], b"a", b"1"][..], &[b"SET", b"b", b"x"], &[b"INCR", b"b"]] {
    /// #     kevy_resp::encode_command_borrowed(&mut raw, argv);
    /// # }
    /// # std::fs::write(&src, raw)?;
    /// assert_eq!(run_import(&mut client, &src, ImportStart::Fresh, OnErrorReply::Count)?.sent, 2);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub sent: u64,
    /// -ERR replies (counted; fatal under [`OnErrorReply::Abort`]).
    ///
    /// ```
    /// use kevy_cli::migrate::{ImportStart, OnErrorReply, run_import};
    /// # let port = include!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/doc_server/serve.rs"));
    /// let mut client = kevy_resp_client::RespClient::connect("127.0.0.1", port)?;
    /// # let dir = kevy_tmpdir::TmpDir::new("migrate");
    /// # let src = dir.path().join("cmds.resp");
    /// # let mut raw = Vec::new();
    /// # for argv in [&[&b"SET"[..], b"a", b"1"][..], &[b"SET", b"b", b"x"], &[b"INCR", b"b"]] {
    /// #     kevy_resp::encode_command_borrowed(&mut raw, argv);
    /// # }
    /// # std::fs::write(&src, raw)?;
    /// let r = run_import(&mut client, &src, ImportStart::Fresh, OnErrorReply::Count)?;
    /// assert_eq!(r.errors, 1, "INCR on a non-number");
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub errors: u64,
    /// Byte offset reached in the source file.
    ///
    /// ```
    /// use kevy_cli::migrate::{ImportStart, OnErrorReply, run_import};
    /// # let port = include!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/doc_server/serve.rs"));
    /// let mut client = kevy_resp_client::RespClient::connect("127.0.0.1", port)?;
    /// # let dir = kevy_tmpdir::TmpDir::new("migrate");
    /// # let src = dir.path().join("cmds.resp");
    /// # let mut raw = Vec::new();
    /// # for argv in [&[&b"SET"[..], b"a", b"1"][..], &[b"SET", b"b", b"x"], &[b"INCR", b"b"]] {
    /// #     kevy_resp::encode_command_borrowed(&mut raw, argv);
    /// # }
    /// # std::fs::write(&src, raw)?;
    /// let r = run_import(&mut client, &src, ImportStart::Fresh, OnErrorReply::Count)?;
    /// // the whole file was applied, and `<src>.progress` says so for a later --resume
    /// assert_eq!(r.offset, std::fs::metadata(&src)?.len());
    /// let progress = std::fs::read_to_string(dir.path().join("cmds.resp.progress"))?;
    /// assert_eq!(progress, r.offset.to_string());
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub offset: u64,
}

/// Where an import starts reading its source file.
///
/// ```
/// use kevy_cli::migrate::ImportStart;
/// // `import --resume`
/// assert_ne!(ImportStart::Resume, ImportStart::default());
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum ImportStart {
    /// From the first byte, resetting `<src>.progress` before any batch.
    ///
    /// ```
    /// use kevy_cli::migrate::{ImportStart, OnErrorReply, run_import};
    /// # let port = include!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/doc_server/serve.rs"));
    /// let mut client = kevy_resp_client::RespClient::connect("127.0.0.1", port)?;
    /// # let dir = kevy_tmpdir::TmpDir::new("migrate");
    /// # let src = dir.path().join("cmds.resp");
    /// # let mut raw = Vec::new();
    /// # for argv in [&[&b"SET"[..], b"a", b"1"][..], &[b"SET", b"b", b"x"], &[b"INCR", b"b"]] {
    /// #     kevy_resp::encode_command_borrowed(&mut raw, argv);
    /// # }
    /// # std::fs::write(&src, raw)?;
    /// run_import(&mut client, &src, ImportStart::Fresh, OnErrorReply::Count)?;
    /// // fresh ignores any earlier progress and replays the whole file
    /// let again = run_import(&mut client, &src, ImportStart::Fresh, OnErrorReply::Count)?;
    /// assert_eq!(again.sent + again.errors, 3);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    #[default]
    Fresh,
    /// From the offset `<src>.progress` recorded (`--resume`).
    ///
    /// ```
    /// use kevy_cli::migrate::{ImportStart, OnErrorReply, run_import};
    /// # let port = include!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/doc_server/serve.rs"));
    /// let mut client = kevy_resp_client::RespClient::connect("127.0.0.1", port)?;
    /// # let dir = kevy_tmpdir::TmpDir::new("migrate");
    /// # let src = dir.path().join("cmds.resp");
    /// # let mut raw = Vec::new();
    /// # for argv in [&[&b"SET"[..], b"a", b"1"][..], &[b"SET", b"b", b"x"], &[b"INCR", b"b"]] {
    /// #     kevy_resp::encode_command_borrowed(&mut raw, argv);
    /// # }
    /// # std::fs::write(&src, raw)?;
    /// run_import(&mut client, &src, ImportStart::Fresh, OnErrorReply::Count)?;
    /// // resuming a finished import starts at its end and sends nothing
    /// let resumed = run_import(&mut client, &src, ImportStart::Resume, OnErrorReply::Count)?;
    /// assert_eq!(resumed.sent + resumed.errors, 0);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    Resume,
}

/// What an import does when the server answers a command with an error.
///
/// ```
/// use kevy_cli::migrate::OnErrorReply;
/// // `import --strict`
/// assert_ne!(OnErrorReply::Abort, OnErrorReply::default());
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum OnErrorReply {
    /// Count it in [`ImportReport::errors`] and carry on.
    ///
    /// ```
    /// use kevy_cli::migrate::{ImportStart, OnErrorReply, run_import};
    /// # let port = include!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/doc_server/serve.rs"));
    /// let mut client = kevy_resp_client::RespClient::connect("127.0.0.1", port)?;
    /// # let dir = kevy_tmpdir::TmpDir::new("migrate");
    /// # let src = dir.path().join("cmds.resp");
    /// # let mut raw = Vec::new();
    /// # for argv in [&[&b"SET"[..], b"a", b"1"][..], &[b"SET", b"b", b"x"], &[b"INCR", b"b"]] {
    /// #     kevy_resp::encode_command_borrowed(&mut raw, argv);
    /// # }
    /// # std::fs::write(&src, raw)?;
    /// let r = run_import(&mut client, &src, ImportStart::Fresh, OnErrorReply::Count)?;
    /// assert_eq!((r.sent, r.errors), (2, 1), "counted, and the import went on");
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    #[default]
    Count,
    /// Stop with an `InvalidData` error naming the reply (`--strict`).
    ///
    /// ```
    /// use kevy_cli::migrate::{ImportStart, OnErrorReply, run_import};
    /// # let port = include!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/doc_server/serve.rs"));
    /// let mut client = kevy_resp_client::RespClient::connect("127.0.0.1", port)?;
    /// # let dir = kevy_tmpdir::TmpDir::new("migrate");
    /// # let src = dir.path().join("cmds.resp");
    /// # let mut raw = Vec::new();
    /// # for argv in [&[&b"SET"[..], b"a", b"1"][..], &[b"SET", b"b", b"x"], &[b"INCR", b"b"]] {
    /// #     kevy_resp::encode_command_borrowed(&mut raw, argv);
    /// # }
    /// # std::fs::write(&src, raw)?;
    /// let err = run_import(&mut client, &src, ImportStart::Fresh, OnErrorReply::Abort).unwrap_err();
    /// assert_eq!(err.kind(), std::io::ErrorKind::InvalidData);
    /// assert!(err.to_string().starts_with("server error (strict)"));
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    Abort,
}

/// Run `import` — stream `src` (a RESP command file) into the server,
/// `PIPELINE` commands per batch. The progress file `<src>.progress`
/// records the safely-applied byte offset after every batch (fsynced);
/// [`ImportStart::Resume`] starts there. Idempotent replay.
///
/// ```
/// use kevy_cli::migrate::{ImportStart, OnErrorReply, run_import};
/// use kevy_resp_client::Reply;
/// # let port = include!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/doc_server/serve.rs"));
/// let mut client = kevy_resp_client::RespClient::connect("127.0.0.1", port)?;
/// let dir = kevy_tmpdir::TmpDir::new("import");
/// let path = dir.path().join("dump.resp");
/// let mut raw = Vec::new();
/// kevy_resp::encode_command_borrowed(&mut raw, &[&b"SET"[..], b"greet", b"hello"]);
/// std::fs::write(&path, raw)?;
///
/// let r = run_import(&mut client, &path, ImportStart::Resume, OnErrorReply::Abort)?;
/// println!("{} sent, offset {}", r.sent, r.offset);
/// assert_eq!(client.request_borrowed(&[b"GET", b"greet"])?, Reply::Bulk(b"hello".to_vec()));
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn run_import(
    client: &mut dyn Link,
    src: &Path,
    start: ImportStart,
    on_error: OnErrorReply,
) -> io::Result<ImportReport> {
    // the source first: a missing one must not leave a progress file behind
    let mut f = File::open(src)?;
    let (mut progress, start) = open_progress(src, start)?;
    f.seek(SeekFrom::Start(start))?;
    let mut pending: Vec<u8> = Vec::with_capacity(1 << 20);
    let mut report = ImportReport { sent: 0, errors: 0, offset: start };
    let mut chunk = vec![0u8; 1 << 20];
    let mut batch_bytes = 0usize;
    let mut batch_cmds = 0usize;
    loop {
        let n = f.read(&mut chunk)?;
        if n == 0 {
            break;
        }
        pending.extend_from_slice(&chunk[..n]);
        // carve complete commands off `pending`
        while let Some(used) = command_len(&pending[batch_bytes..]) {
            batch_bytes += used;
            batch_cmds += 1;
            if batch_cmds == PIPELINE {
                flush_batch(client, &pending[..batch_bytes], batch_cmds, on_error, &mut report)?;
                pending.drain(..batch_bytes);
                write_progress(&mut progress, report.offset)?;
                batch_bytes = 0;
                batch_cmds = 0;
            }
        }
    }
    if batch_cmds > 0 {
        flush_batch(client, &pending[..batch_bytes], batch_cmds, on_error, &mut report)?;
        write_progress(&mut progress, report.offset)?;
    }
    Ok(report)
}

fn flush_batch(
    client: &mut dyn Link,
    raw: &[u8],
    n: usize,
    on_error: OnErrorReply,
    report: &mut ImportReport,
) -> io::Result<()> {
    let replies = client.pipeline_raw(raw, n)?;
    for r in replies {
        if let Reply::Error(e) = r {
            report.errors += 1;
            if on_error == OnErrorReply::Abort {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!("server error (strict): {}", String::from_utf8_lossy(&e)),
                ));
            }
        } else {
            report.sent += 1;
        }
    }
    report.offset += raw.len() as u64;
    Ok(())
}

/// The progress file, opened, and the offset to start from.
///
/// The name is `<src>.progress`, APPENDED — the doc always said so, but
/// `with_extension` turned dump.kevy into dump.progress, and everything
/// written against the documented name (the migration drill's cleanup
/// included) touched a file this code never read.
///
/// A fresh (non-resume) import resets the file immediately, before any
/// batch. A completed earlier import leaves offset = EOF on disk; kill a
/// fresh import before its first batch lands and that stale EOF
/// survives — a later `--resume` then seeks to the end, imports nothing,
/// and reports success. The migration drill hit exactly that window.
pub(crate) fn open_progress(src: &Path, start: ImportStart) -> io::Result<(File, u64)> {
    let resume = start == ImportStart::Resume;
    let path = {
        let mut os = src.as_os_str().to_owned();
        os.push(".progress");
        std::path::PathBuf::from(os)
    };
    let mut start = 0u64;
    if resume && let Ok(text) = std::fs::read_to_string(&path) {
        start = text.trim().parse().unwrap_or(0);
    }
    let mut f = OpenOptions::new().create(true).truncate(false).write(true).open(&path)?;
    if !resume {
        write_progress(&mut f, 0)?;
    }
    Ok((f, start))
}

pub(crate) fn write_progress(f: &mut File, offset: u64) -> io::Result<()> {
    f.set_len(0)?;
    f.seek(SeekFrom::Start(0))?;
    f.write_all(offset.to_string().as_bytes())?;
    f.sync_data()
}

/// Crate-visible alias for [`command_len`] (bulk copy counts frames).
pub(crate) fn command_len_pub(b: &[u8]) -> Option<usize> {
    command_len(b)
}

/// Length of one complete RESP command at the head of `b`, or `None`.
fn command_len(b: &[u8]) -> Option<usize> {
    let mut pos = 0usize;
    let line = take_line(b, &mut pos)?;
    if line.first() != Some(&b'*') {
        return None;
    }
    let n: usize = std::str::from_utf8(&line[1..]).ok()?.trim().parse().ok()?;
    for _ in 0..n {
        let hdr = take_line(b, &mut pos)?;
        if hdr.first() != Some(&b'$') {
            return None;
        }
        let len: usize = std::str::from_utf8(&hdr[1..]).ok()?.trim().parse().ok()?;
        if b.len() < pos + len + 2 {
            return None;
        }
        pos += len + 2;
    }
    Some(pos)
}

fn take_line<'b>(b: &'b [u8], pos: &mut usize) -> Option<&'b [u8]> {
    let rest = &b[*pos..];
    let idx = rest.windows(2).position(|w| w == b"\r\n")?;
    let line = &rest[..idx];
    *pos += idx + 2;
    Some(line)
}