Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_o3db_sync/tests/cas_o3db.rs

3.6 KiB, 1 run

created by r1870400018:67482, 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//! [`O3dbCas`] keeps a chunk of any length whole.
2//!
3//! A chunk was written as `Dat::BU8`, whose length field is one byte, so a chunk of more than
4//! 255 bytes was stored with its length cut modulo 256 and came back as a shorter chunk. The
5//! address check on `put` passed, since it runs on the bytes before they are stored, so nothing
6//! noticed.
7//! These tests put chunks either side of each width's boundary and read them back, in the session
8//! that wrote them and after a close and a reopen of the same root.
9//!
10//! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\
11//! Anthropic Claude
12
13use oxedyne_fe2o3_core::prelude::*;
14use oxedyne_fe2o3_hash::{
15 csum::ChecksumScheme,
16 hash::HashScheme,
17};
18use oxedyne_fe2o3_o3db_sync::{
19 O3db,
20 cas::{
21 Cas,
22 Chunk,
23 ContentId,
24 },
25 cas_o3db::O3dbCas,
26 data::core::RestSchemesInput,
27 test::setup::{
28 self,
29 Uid,
30 UID_LEN,
31 },
32};
33
34use std::{
35 path::PathBuf,
36 sync::Arc,
37};
38
39
40type Db = O3db<{ UID_LEN }, Uid, (), HashScheme, HashScheme, ChecksumScheme>;
41type TestCas = O3dbCas<{ UID_LEN }, Uid, (), HashScheme, HashScheme, ChecksumScheme>;
42
43// Lengths either side of the one-byte and two-byte length fields, and one that needs four bytes.
44const LENS: [usize; 11] = [1, 100, 254, 255, 256, 300, 1000, 65_534, 65_535, 65_536, 70_000];
45
46fn open(db_root: &PathBuf, wipe: bool) -> Outcome<Db> {
47 let schms_input = RestSchemesInput::new(
48 None::<()>,
49 None::<HashScheme>,
50 None::<HashScheme>,
51 Some(ChecksumScheme::new_crc32()),
52 );
53 let mut cfg = res!(setup::default_cfg());
54 // Every zone in this test's own directory, not the shared container the default names.
55 cfg.zone_overrides = Default::default();
56 setup::start_db(db_root.clone(), Some(cfg), schms_input, None, false, wipe)
57}
58
59fn bytes(n: usize, seed: u8) -> Vec<u8> {
60 (0..n).map(|i| ((i * 31 + seed as usize) % 251) as u8).collect()
61}
62
63fn check_all(cas: &TestCas, when: &str) -> Outcome<()> {
64 for (k, n) in LENS.iter().enumerate() {
65 let want = bytes(*n, k as u8);
66 let id = ContentId::of(&want);
67 let got = match res!(cas.get(&id)) {
68 Some(b) => b,
69 None => return Err(err!(
70 "The chunk of {} bytes put earlier is absent {}.", n, when;
71 Test, Missing)),
72 };
73 if got.len() != want.len() {
74 return Err(err!(
75 "A chunk of {} bytes came back as {} bytes {}.", want.len(), got.len(), when;
76 Test, Mismatch, Size));
77 }
78 if got != want {
79 return Err(err!(
80 "A chunk of {} bytes came back with other bytes {}.", n, when;
81 Test, Mismatch, Data));
82 }
83 if !res!(cas.has(&id)) {
84 return Err(err!("has() denies the chunk of {} bytes {}.", n, when; Test, Mismatch));
85 }
86 }
87 Ok(())
88}
89
90#[test]
91fn chunks_of_every_width_round_trip() -> Outcome<()> {
92 log_set_level!("warn");
93 let outcome = run();
94 log_finish_wait!();
95 outcome
96}
97
98fn run() -> Outcome<()> {
99 let db_dir = PathBuf::from("./test_db_cas_o3db");
100 let _ = std::fs::remove_dir_all(&db_dir);
101 res!(std::fs::create_dir_all(&db_dir));
102 let db_root = res!(db_dir.canonicalize());
103
104 {
105 let db = Arc::new(res!(open(&db_root, true)));
106 let cas = TestCas::new(db.clone(), Uid::default());
107 for (k, n) in LENS.iter().enumerate() {
108 res!(cas.put(&Chunk::new(bytes(*n, k as u8))));
109 }
110 res!(check_all(&cas, "in the session that wrote it"));
111 let ids = res!(cas.ids());
112 if ids.len() != LENS.len() {
113 return Err(err!(
114 "ids() lists {} chunks after {} were put.", ids.len(), LENS.len();
115 Test, Mismatch, Size));
116 }
117 res!(db.close());
118 }
119
120 {
121 let db = Arc::new(res!(open(&db_root, false)));
122 let cas = TestCas::new(db.clone(), Uid::default());
123 res!(check_all(&cas, "after a close and a reopen"));
124 res!(db.close());
125 }
126 Ok(())
127}