in src/storage/datapool/src/lib.rs [697:743]
fn mmapfile_datapool() {
let tempdir = TempDir::new().expect("failed to generate tempdir");
let mut path = tempdir.into_path();
path.push("mmap_test.data");
let magic_a = [0xDE, 0xCA, 0xFB, 0xAD];
let magic_b = [0xBA, 0xDC, 0x0F, 0xFE, 0xEB, 0xAD, 0xCA, 0xFE];
// create a datapool, write some content to it, and close it
{
let mut datapool =
MmapFile::create(&path, 2 * PAGE_SIZE, 0).expect("failed to create pool");
assert_eq!(datapool.len(), 2 * PAGE_SIZE);
datapool.flush().expect("failed to flush");
for (i, byte) in magic_a.iter().enumerate() {
datapool.as_mut_slice()[i] = *byte;
}
datapool.flush().expect("failed to flush");
}
// open the datapool and check the content, then update it
{
let mut datapool =
MmapFile::open(&path, 2 * PAGE_SIZE, 0).expect("failed to create pool");
assert_eq!(datapool.len(), 2 * PAGE_SIZE);
assert_eq!(datapool.as_slice()[0..4], magic_a[0..4]);
assert_eq!(datapool.as_slice()[4..8], [0; 4]);
for (i, byte) in magic_b.iter().enumerate() {
datapool.as_mut_slice()[i] = *byte;
}
datapool.flush().expect("failed to flush");
}
// open the datapool again, and check that it has the new data
{
let datapool = MmapFile::open(&path, 2 * PAGE_SIZE, 0).expect("failed to create pool");
assert_eq!(datapool.len(), 2 * PAGE_SIZE);
assert_eq!(datapool.as_slice()[0..8], magic_b[0..8]);
}
// check that the datapool does not open if the user version is incorrect
{
assert!(MmapFile::open(&path, 2 * PAGE_SIZE, 1).is_err());
}
}