oxedyne/fe2o3/fe2o3_o3db_sync/tests/helper_index.rs
4.0 KiB, 1 run
created by r1870400018:63101, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! The files `test::file::save_multiple_files` writes must be a store a start believes (Q4, |
| 2 | //! 2026-10-01). Its index entries named where each record ended, not where it started, so a |
| 3 | //! start that checks an index against its data file refused every one and rebuilt it, which |
| 4 | //! hid that the helper's idea of the format was wrong. The oracle is the store itself: the |
| 5 | //! helper's files are put where a store keeps them and opened, and none of the index files may be |
| 6 | //! touched by the start nor any value read back wrong. |
| 7 | |
| 8 | mod gc_pair; |
| 9 | |
| 10 | use gc_pair::*; |
| 11 | |
| 12 | use oxedyne_fe2o3_core::prelude::*; |
| 13 | use oxedyne_fe2o3_o3db_sync::{ |
| 14 | file::core::FileType, |
| 15 | test::{ |
| 16 | file::save_multiple_files, |
| 17 | setup::{ |
| 18 | self, |
| 19 | Uid, |
| 20 | }, |
| 21 | }, |
| 22 | }; |
| 23 | |
| 24 | use std::{ |
| 25 | fs, |
| 26 | path::Path, |
| 27 | time::SystemTime, |
| 28 | }; |
| 29 | |
| 30 | #[test] |
| 31 | fn main() -> Outcome<()> { |
| 32 | log_set_level!("error"); |
| 33 | let (dir_a, dir_b) = ("./test_db_helper_index_a", "./test_db_helper_index_b"); |
| 34 | for dir in [dir_a, dir_b] { |
| 35 | let _ = fs::remove_dir_all(dir); |
| 36 | res!(fs::create_dir_all(dir)); |
| 37 | } |
| 38 | let (root_a, root_b) = (res!(Path::new(dir_a).canonicalize()), res!(Path::new(dir_b).canonicalize())); |
| 39 | |
| 40 | // What the writer makes of these records: five to a file, so twelve keys fill three files. |
| 41 | let probe = res!(probe("./test_db_helper_index_probe")); |
| 42 | let len = probe.len; |
| 43 | let cfg = res!(config(5 * len + len / 2)); |
| 44 | |
| 45 | // The helper encodes with a database's keys and schemes, then writes the files itself, into |
| 46 | // the zone directory of a store that is not yet there. |
| 47 | let mut db_a: TestDb = res!(setup::start_db(root_a, Some(cfg.clone()), schemes(), None, false, true)); |
| 48 | let zone = zone_dir(&root_b, &cfg); |
| 49 | res!(fs::create_dir_all(&zone)); |
| 50 | let ks: Vec<_> = (0..NKEYS).map(key).collect(); |
| 51 | let vs: Vec<_> = (0..NKEYS).map(|i| value(i, 1)).collect(); |
| 52 | res!(save_multiple_files( |
| 53 | zone.clone(), |
| 54 | res!(cfg.data_file_max_bytes.try_into()), |
| 55 | &mut db_a, |
| 56 | Uid::default(), |
| 57 | None, |
| 58 | ks, |
| 59 | vs, |
| 60 | NKEYS * len as usize, |
| 61 | )); |
| 62 | res!(db_a.close()); |
| 63 | |
| 64 | // Records of the writer's length, five to a file. |
| 65 | let data = |n| file(&root_b, &cfg, FileType::Data, n); |
| 66 | let index = |n| file(&root_b, &cfg, FileType::Index, n); |
| 67 | assert_eq!(size(&data(1)), 5 * len, "the helper's first file does not hold five of the writer's records"); |
| 68 | assert_eq!(size(&data(2)), 5 * len, "the helper's second file does not hold five of the writer's records"); |
| 69 | assert_eq!(size(&data(3)), 2 * len, "the helper's third file does not hold the last two records"); |
| 70 | // And index files as long as the writer's, whose entries are as long as the offsets they name. |
| 71 | assert_eq!(size(&index(1)), probe.ind[5], "the helper's first index is not the writer's length"); |
| 72 | assert_eq!(size(&index(3)), probe.ind[2], "the helper's last index is not the writer's length"); |
| 73 | let before: Vec<(Vec<u8>, SystemTime)> = (1..=3) |
| 74 | .map(|n| { |
| 75 | let p = index(n); |
| 76 | ( |
| 77 | fs::read(&p).unwrap_or_default(), |
| 78 | fs::metadata(&p).and_then(|m| m.modified()).unwrap_or(SystemTime::UNIX_EPOCH), |
| 79 | ) |
| 80 | }) |
| 81 | .collect(); |
| 82 | |
| 83 | // A start over the helper's files. |
| 84 | let db_b: TestDb = res!(setup::start_db(root_b.clone(), Some(cfg.clone()), schemes(), None, false, false)); |
| 85 | let want = vec![1u8; NKEYS]; |
| 86 | assert_eq!(judge(&db_b, &want, "Over the helper's files"), 0); |
| 87 | res!(db_b.close()); |
| 88 | log_finish_wait!(); |
| 89 | |
| 90 | for (i, (bytes, mtime)) in before.iter().enumerate() { |
| 91 | let p = index(i as u32 + 1); |
| 92 | assert!(fs::read(&p).unwrap_or_default() == *bytes, |
| 93 | "the start rewrote index file {}: it refused the helper's entries and rebuilt them", i + 1); |
| 94 | assert!(fs::metadata(&p).and_then(|m| m.modified()).unwrap_or(SystemTime::UNIX_EPOCH) == *mtime, |
| 95 | "the start touched index file {}", i + 1); |
| 96 | } |
| 97 | |
| 98 | for dir in [dir_a, dir_b] { |
| 99 | let _ = fs::remove_dir_all(dir); |
| 100 | } |
| 101 | Ok(()) |
| 102 | } |