Skip to main content

raft_log/
dump_writer.rs

1use std::fmt;
2use std::io;
3
4use codeq::OffsetSize;
5
6use crate::ChunkId;
7use crate::Types;
8use crate::WALRecord;
9use crate::num::format_pad9_u64;
10use crate::types::Segment;
11
12pub fn write_record_debug<T: Types, W: io::Write>(
13    w: &mut W,
14    chunk_id: ChunkId,
15    in_chunk_record_index: u64,
16    res: Result<(Segment, WALRecord<T>), io::Error>,
17) -> Result<(), io::Error> {
18    match res {
19        Ok((seg, rec)) => {
20            // Before the first record of a chunk, print the chunk ID
21            if seg.offset().0 == 0 {
22                writeln!(w, "{}", chunk_id)?;
23            }
24            writeln!(
25                w,
26                "  R-{in_chunk_record_index:05}: [{}, {}) {}: {:?}",
27                format_pad9_u64(*seg.offset()),
28                format_pad9_u64(*seg.end()),
29                seg.size(),
30                rec
31            )?;
32        }
33        Err(io_err) => {
34            writeln!(w, "Error: {}", io_err)?;
35        }
36    }
37    Ok(())
38}
39
40/// Write each record using `std::fmt::Display` instead of using
41/// `std::fmt::Debug`
42pub fn write_record_display<T: Types, W: io::Write>(
43    w: &mut W,
44    chunk_id: ChunkId,
45    in_chunk_record_index: u64,
46    res: Result<(Segment, WALRecord<T>), io::Error>,
47) -> Result<(), io::Error>
48where
49    WALRecord<T>: fmt::Display,
50{
51    match res {
52        Ok((seg, rec)) => {
53            // Before the first record of a chunk, print the chunk ID
54            if seg.offset().0 == 0 {
55                writeln!(w, "{}", chunk_id)?;
56            }
57            writeln!(
58                w,
59                "  R-{in_chunk_record_index:05}: [{}, {}) {}: {}",
60                format_pad9_u64(*seg.offset()),
61                format_pad9_u64(*seg.end()),
62                seg.size(),
63                rec
64            )?;
65        }
66        Err(io_err) => {
67            writeln!(w, "Error: {}", io_err)?;
68        }
69    }
70    Ok(())
71}