cbvault 0.1.2

Read ChessBase databases as moves2 streams: Database, GameIter, PGN writer, and .cbv/.cbz archives that unarchive in full
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
// Copyright (c) 2026 the cbvault contributors (MIT). See LICENSE.
//! Full-database benchmark (local-only): move-decode parity with `cbtool
//! verify` plus a main-line SAN sample for diffing against `cbtool pgn`.
//!
//! The parity deliberately walks what `cbtool verify` walks for moves: every
//! move through the decoder (the `Counter` below mirrors `cbtool`'s
//! `classic::Counter`), counting main/all plies, null moves, en passant and
//! promotion captures. Annotation `check_positions` is upstream's second half
//! of `verify` and belongs to phase 4 (`.cba` framing is not ported yet), so
//! the failure list here can only converge on the move-decode failures. The
//! emitted JSON mirrors the `verify` report: records, games, texts, unknowns,
//! deleted, chess960, setups, main/all plies, null moves, en passant,
//! promotion captures (and the distinct-capture split), annotated games
//! (header offset only, pending phase 4 checks), and the failure list.
use cbvault::pgn::PgnWriter;
use cbvault_chess::decode::{GameRef, MoveSink, start_as_played, walk_from};
use cbvault_chess::start::{Start, start_board};
use cbvault_format::cbh::moves::GameMoves;
use cbvault_format::cbh::{Annotations, Entities, GameHeader, Headers, Wide};
use cbvault_format::file::DbFile;
use cbvault_format::game::RecordKind;
use gigachess::{Board, Move};
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::{Path, PathBuf};
use std::time::Instant;
#[derive(Default)]
struct Counter {
    null_moves: u64,
    en_passant: u64,
    promo_captures: u64,
    promo_captures_distinct: u64,
}
impl MoveSink for Counter {
    fn play(&mut self, before: &Board, mv: u16, _main: bool) {
        let mv = Move::from_word(mv);
        if mv.is_null() {
            self.null_moves += 1;
            return;
        }
        let moving = before.piece_at(mv.from());
        let target = before.piece_at(mv.to());
        let pawn = matches!(moving, Some(p) if p.role == gigachess::Role::Pawn);
        if pawn && mv.from().file() != mv.to().file() && target.is_none() {
            self.en_passant += 1;
        }
        if let (Some(promo), Some(target)) = (mv.promotion(), target)
            && moving.is_some_and(|m| m.color != target.color)
        {
            self.promo_captures += 1;
            if target.role != promo {
                self.promo_captures_distinct += 1;
            }
        }
    }
    fn played(&mut self, _board: &Board) {}
    fn branch(&mut self) {}
    fn resume(&mut self) {}
}
fn peak_rss_bytes() -> u64 {
    // No libc dependency: current RSS via `ps` (macOS reports KiB).
    if let Ok(out) =
        std::process::Command::new("ps").args(["-o", "rss=", "-p", &std::process::id().to_string()]).output()
        && let Ok(text) = String::from_utf8(out.stdout)
        && let Ok(kb) = text.trim().parse::<u64>()
    {
        return kb * 1024;
    }
    0
}
/// The export write buffer: 1 MiB, so the whole-database export issues
/// thousands of writes instead of the ~1,000,000 that an 8 KiB buffer needs
/// for 7.6 GB (`pgn-export-sota-performance` task 4.2).
const PGN_BUFFER: usize = 1 << 20;
/// The default, and the tuned value: the write buffer's cost is 1 MiB of
/// resident memory for the whole export, and its benefit has flattened out by
/// 1 MiB — see `benchmarks/baseline.json` under `buffer_sweep` for the sweep of
/// 64 KiB .. 16 MiB and the chunk-size sweep behind it.
const PGN_BATCH: u32 = 8192;

fn main_line_sans(game: &GameMoves<'_>) -> Result<String, String> {
    let what = GameRef::new(0);
    let start = start_as_played(what, game).map_err(|e| e.to_string())?;
    let board = start_board(&start).map_err(|e| e.to_string())?;
    struct Main {
        board: Board,
        sans: String,
    }
    impl MoveSink for Main {
        fn play(&mut self, _before: &Board, mv: u16, main: bool) {
            if !main {
                return;
            }
            if !self.sans.is_empty() {
                self.sans.push('|');
            }
            // One path for a pass and an ordinary move alike: `move_to_san`
            // renders `Move::NULL` as a bare `--` and `play` applies it, so the
            // special case this example used to carry is gone.
            let mv = Move::from_word(mv);
            let san = gigachess::san::move_to_san(&self.board, mv).expect("replay");
            self.sans.push_str(&san);
            self.board.play(mv).expect("replay");
        }
        fn played(&mut self, _board: &Board) {}
        fn branch(&mut self) {}
        fn resume(&mut self) {}
    }
    let mut sink = Main { board, sans: String::new() };
    walk_from(what, game, &start, &mut sink).map_err(|e| e.to_string())?;
    Ok(sink.sans)
}
fn main() {
    let mut args = std::env::args().skip(1);
    let mut base = std::env::var("CBVAULT_TEST_DB").unwrap_or_else(|_| {
        Path::new(env!("CARGO_MANIFEST_DIR")).join("../../Mega Database 2025/Mega Database 2025").display().to_string()
    });
    let mut pgn_out: Option<PathBuf> = None;
    let mut pgn_last: u32 = 0;
    let mut pgn_buffer: usize = PGN_BUFFER;
    let mut pgn_batch: u32 = PGN_BATCH;
    let mut sample_out: Option<PathBuf> = None;
    let mut decode_only = false;
    let mut threads: Option<usize> = None;
    while let Some(arg) = args.next() {
        match arg.as_str() {
            "--pgn-out" => pgn_out = args.next().map(PathBuf::from),
            "--pgn-last" => pgn_last = args.next().and_then(|v| v.parse().ok()).unwrap_or(0),
            "--pgn-buffer" => pgn_buffer = args.next().and_then(|v| v.parse().ok()).unwrap_or(PGN_BUFFER),
            "--pgn-batch" => pgn_batch = args.next().and_then(|v| v.parse().ok()).unwrap_or(PGN_BATCH),
            "--sample-out" => sample_out = args.next().map(PathBuf::from),
            "--decode-only" => decode_only = true,
            "--threads" => {
                threads = args.next().and_then(|s| s.parse().ok());
            }
            other => base = other.to_string(),
        }
    }
    run(&PathBuf::from(base), pgn_out, sample_out, decode_only, threads, pgn_last, pgn_buffer, pgn_batch);
}

#[allow(clippy::too_many_lines)]
#[allow(clippy::too_many_arguments)]
fn run(
    base: &Path,
    pgn_out: Option<PathBuf>,
    sample_out: Option<PathBuf>,
    decode_only: bool,
    threads: Option<usize>,
    pgn_last: u32,
    pgn_buffer: usize,
    pgn_batch: u32,
) {
    if decode_only {
        let t = threads.unwrap_or(0);
        let t0 = Instant::now();
        let (stats, failures) = cbvault::replay::verify_parallel(base, t, 8192, 50).expect("verify parallel");
        let secs = t0.elapsed().as_secs_f64();
        let records = stats.games + stats.texts + stats.unknowns;
        let items = failures.iter().take(50).map(|s| format!("{s:?}")).collect::<Vec<_>>().join(",");
        println!(
            concat!(
                "{{\"records\":{records},\"seconds\":{secs:.2},\"records_per_second\":{rps:.0},",
                "\"main_line_plies\":{main_plies},\"all_plies\":{total_plies},",
                "\"plies_per_second\":{pps:.0},\"peak_rss_bytes\":{rss},\"pgn_bytes\":0,",
                "\"games\":{games},\"guiding_texts\":{texts},\"unknown_kind\":{unknowns},",
                "\"deleted\":{deleted},\"chess960\":{chess960},\"setup_positions\":{setups},",
                "\"annotated_games\":{annotated},\"null_moves\":{nulls},\"en_passant\":{ep},",
                "\"promotion_captures\":{promo},\"failed\":{failed},\"failure_items\":[{items}]}}"
            ),
            records = records,
            secs = secs,
            rps = records as f64 / secs,
            main_plies = stats.main_plies,
            total_plies = stats.total_plies,
            pps = stats.total_plies as f64 / secs,
            rss = peak_rss_bytes(),
            games = stats.games,
            texts = stats.texts,
            unknowns = stats.unknowns,
            deleted = stats.deleted,
            chess960 = stats.chess960,
            setups = stats.setups,
            annotated = stats.annotated,
            nulls = stats.null_moves,
            ep = stats.en_passant,
            promo = stats.promo_captures,
            failed = failures.len(),
            items = items,
        );
        return;
    }
    let cbg_path = PathBuf::from(format!("{}.cbg", base.display()));
    let headers = Headers::open(base).expect("headers");
    let entities = Entities::open(base).expect("entities");
    let cbg = DbFile::open(cbg_path.clone()).expect("cbg");
    let total = headers.records();
    let mut record_buf = Vec::with_capacity(1 << 20);
    let mut buf = cbvault_chess::tree::MovesBuf::with_capacity(512);
    let mut counting = Counter::default();
    let mut writer = PgnWriter::new();
    if decode_only && (pgn_out.is_some() || sample_out.is_some()) {
        eprintln!("--decode-only ignores outputs");
    }
    let mut pgn_sink: Option<Counted<BufWriter<File>>> = if decode_only {
        None
    } else {
        pgn_out.map(|p| Counted {
            inner: BufWriter::with_capacity(pgn_buffer, File::create(p).expect("pgn-out")),
            bytes: 0,
        })
    };
    // `--threads` above one exports through the Rayon pipeline: the same bytes,
    // written in record order (`pgn-export-sota-performance` task 5.3).
    if !decode_only && threads.unwrap_or(0) > 1 {
        let t0 = Instant::now();
        let mut sink = pgn_sink.take().expect("--pgn-out");
        let stats = cbvault::pgn::export_range(base, &mut sink, threads.unwrap_or(0), pgn_batch, 50, pgn_last)
            .expect("parallel export");
        sink.flush().expect("flush");
        let secs = t0.elapsed().as_secs_f64();
        println!(
            "{{\"records\":{},\"seconds\":{secs:.2},\"records_per_second\":{:.0},\"games\":{},\"pgn_bytes\":{},\"threads\":{},\"parallel\":true}}",
            stats.records,
            stats.records as f64 / secs,
            stats.games,
            stats.bytes,
            threads.unwrap_or(0)
        );
        return;
    }
    let annotations = Annotations::open(base).expect("the .cba file");
    let wide = Wide::open(base).ok();
    let mut ann_scratch: Vec<u8> = Vec::new();
    let mut sample_sink: Option<BufWriter<File>> =
        if decode_only { None } else { sample_out.map(|p| BufWriter::new(File::create(p).expect("sample-out"))) };
    let mut pgn_bytes: u64 = 0;
    let mut games = 0u64;
    let mut texts = 0u64;
    let mut unknowns = 0u64;
    let mut deleted = 0u64;
    let mut chess960 = 0u64;
    let mut setups = 0u64;
    let mut annotated = 0u64;
    let mut main_plies = 0u64;
    let mut total_plies = 0u64;
    let mut failures: Vec<String> = Vec::new();
    // `--pgn-last` bounds the walk, as it does the parallel path: a range
    // export, which the buffer sweeps use.
    let upto = if pgn_last == 0 { total } else { pgn_last.min(total) };
    let is_sample =
        |id: u32| id <= 2 || (2_601_298..=2_601_308).contains(&id) || id == 2_602_603 || id > total.saturating_sub(2);
    let t0 = Instant::now();
    let mut id = 1u32;
    let mut header_buf = vec![0u8; 46 * 160];
    while id <= upto {
        let n = headers.read_records(id, 160, &mut header_buf).expect("batch");
        if n == 0 {
            break;
        }
        // The batch may reach past `--pgn-last`; only the records in range go.
        let last = (id + n - 1).min(upto);
        for i in 0..=(last - id) {
            one(
                id + i,
                &header_buf[i as usize * 46..(i as usize + 1) * 46],
                &cbg,
                &cbg_path,
                &entities,
                &mut buf,
                &mut record_buf,
                &mut counting,
                &mut writer,
                &annotations,
                wide.as_ref(),
                &mut ann_scratch,
                pgn_sink.as_mut(),
                sample_sink.as_mut(),
                &is_sample,
                &mut games,
                &mut texts,
                &mut unknowns,
                &mut deleted,
                &mut chess960,
                &mut setups,
                &mut annotated,
                &mut main_plies,
                &mut total_plies,
                &mut pgn_bytes,
                &mut failures,
            );
        }
        id = last + 1;
    }
    if let Some(mut out) = pgn_sink {
        out.flush().expect("flush");
    }
    if let Some(mut out) = sample_sink {
        out.flush().expect("flush");
    }
    let secs = t0.elapsed().as_secs_f64();
    let records = u64::from(total);
    let items = failures.iter().take(50).map(|s| format!("{s:?}")).collect::<Vec<_>>().join(",");
    println!(
        concat!(
            "{{\"records\":{records},\"seconds\":{secs:.2},\"records_per_second\":{rps:.0},",
            "\"main_line_plies\":{main_plies},\"all_plies\":{total_plies},",
            "\"plies_per_second\":{pps:.0},\"peak_rss_bytes\":{rss},\"pgn_bytes\":{pgn_bytes},",
            "\"games\":{games},\"guiding_texts\":{texts},\"unknown_kind\":{unknowns},",
            "\"deleted\":{deleted},\"chess960\":{chess960},\"setup_positions\":{setups},",
            "\"annotated_games\":{annotated},\"null_moves\":{nulls},\"en_passant\":{ep},",
            "\"promotion_captures\":{promo},\"failed\":{failed},\"failure_items\":[{items}]}}"
        ),
        records = records,
        secs = secs,
        rps = records as f64 / secs,
        main_plies = main_plies,
        total_plies = total_plies,
        pps = total_plies as f64 / secs,
        rss = peak_rss_bytes(),
        pgn_bytes = pgn_bytes,
        games = games,
        texts = texts,
        unknowns = unknowns,
        deleted = deleted,
        chess960 = chess960,
        setups = setups,
        annotated = annotated,
        nulls = counting.null_moves,
        ep = counting.en_passant,
        promo = counting.promo_captures,
        failed = failures.len(),
        items = items,
    );
}

#[allow(clippy::too_many_arguments)]
/// A `Write` that counts the bytes it has taken: `BufWriter`'s buffer length is
/// not a game's size, because the buffer flushes whenever it fills.
struct Counted<W> {
    inner: W,
    bytes: u64,
}

impl<W: Write> Write for Counted<W> {
    #[inline]
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        let n = self.inner.write(buf)?;
        self.bytes += n as u64;
        Ok(n)
    }

    #[inline]
    fn flush(&mut self) -> std::io::Result<()> {
        self.inner.flush()
    }
}

#[allow(clippy::too_many_arguments)]
fn one(
    rec_id: u32,
    bytes: &[u8],
    cbg: &DbFile,
    cbg_path: &Path,
    entities: &Entities,
    buf: &mut cbvault_chess::tree::MovesBuf,
    record_buf: &mut Vec<u8>,
    counting: &mut Counter,
    writer: &mut PgnWriter,
    annotations: &Annotations,
    wide: Option<&Wide>,
    ann_scratch: &mut Vec<u8>,
    pgn_sink: Option<&mut Counted<BufWriter<File>>>,
    sample_sink: Option<&mut BufWriter<File>>,
    is_sample: &dyn Fn(u32) -> bool,
    games: &mut u64,
    texts: &mut u64,
    unknowns: &mut u64,
    deleted: &mut u64,
    chess960: &mut u64,
    setups: &mut u64,
    annotated: &mut u64,
    main_plies: &mut u64,
    total_plies: &mut u64,
    pgn_bytes: &mut u64,
    failures: &mut Vec<String>,
) {
    let bytes: &[u8; 46] = bytes.try_into().expect("record");
    let header = GameHeader::from_bytes(rec_id, bytes);
    if header.is_deleted() {
        *deleted += 1;
    }
    match header.kind() {
        RecordKind::Game => *games += 1,
        RecordKind::Text => {
            *texts += 1;
            return;
        }
        RecordKind::Analysis | RecordKind::Unknown(_) => {
            *unknowns += 1;
            return;
        }
    }
    let at = u64::from(header.moves_offset());
    let mut head = [0u8; 4];
    if cbg.read_into(at, &mut head).is_err() {
        if failures.len() < 50 {
            failures.push(format!("game {rec_id}: unreadable at {at}"));
        }
        return;
    }
    let size = u32::from_be_bytes([0, head[1], head[2], head[3]]) as usize;
    if !(4..=(1 << 28)).contains(&size) {
        if failures.len() < 50 {
            failures.push(format!("game {rec_id}: size {size}"));
        }
        return;
    }
    record_buf.clear();
    record_buf.resize(size, 0);
    if cbg.read_into(at, record_buf).is_err() {
        if failures.len() < 50 {
            failures.push(format!("game {rec_id}: truncated at {at}"));
        }
        return;
    }
    let game = match GameMoves::parse(cbg_path, record_buf) {
        Ok(game) => game,
        Err(e) => {
            if failures.len() < 50 {
                failures.push(format!("game {rec_id}: {e}"));
            }
            return;
        }
    };
    if game.is_chess960() {
        *chess960 += 1;
    }
    let what = GameRef::at(rec_id, at);
    let start = match start_as_played(what, &game) {
        Ok(start) => start,
        Err(e) => {
            if failures.len() < 50 {
                failures.push(format!("game {rec_id}: {e}"));
            }
            return;
        }
    };
    if matches!(start, Start::Setup(_)) {
        *setups += 1;
    }
    struct Both<'a> {
        buf: &'a mut cbvault_chess::tree::MovesBuf,
        counter: &'a mut Counter,
    }
    impl MoveSink for Both<'_> {
        fn play(&mut self, board: &Board, mv: u16, main: bool) {
            self.counter.play(board, mv, main);
            self.buf.play(board, mv, main);
        }
        fn played(&mut self, board: &Board) {
            self.buf.played(board);
        }
        fn branch(&mut self) {
            self.buf.branch();
        }
        fn resume(&mut self) {
            self.buf.resume();
        }
    }
    let stats = {
        let mut both = Both { buf, counter: counting };
        match walk_from(what, &game, &start, &mut both) {
            Ok(stats) => stats,
            Err(e) => {
                if failures.len() < 50 {
                    failures.push(format!("game {rec_id}: {e}"));
                }
                return;
            }
        }
    };
    buf.clear();
    *main_plies += u64::from(stats.main_line_plies);
    *total_plies += u64::from(stats.total_plies);
    if header.annotations_offset() != 0 {
        *annotated += 1;
    }
    if let Some(out) = pgn_sink {
        // The game's annotations, the way `pgn::export_parallel` and the gold
        // harness read them: the export is the whole game, comments included.
        // Passing `None` here silently dropped every one of them, and the byte
        // count below was 128 MB short of the library's own export.
        let anns = match annotations.of(&header, wide, ann_scratch) {
            Ok(a) => Some(a),
            Err(e) => {
                if failures.len() < 50 {
                    failures.push(format!("game {rec_id}: annotations: {e}"));
                }
                None
            }
        };
        let before = out.bytes;
        match writer.write_game(out, &header, entities, &game, anns.as_ref()) {
            Ok(()) => *pgn_bytes += out.bytes - before,
            Err(e) => {
                if failures.len() < 50 {
                    failures.push(format!("game {rec_id}: pgn: {e}"));
                }
            }
        }
    }
    if let Some(out) = sample_sink
        && is_sample(rec_id)
    {
        match main_line_sans(&game) {
            Ok(sans) => writeln!(out, "{rec_id}:{sans}").expect("sample write"),
            Err(e) => writeln!(out, "{rec_id}:ERROR:{e}").expect("sample write"),
        }
    }
}