use std::path::{Path, PathBuf};
use std::sync::Arc;
use super::*;
use crate::cache::BlockCache;
use crate::ids::{Digest128, FamilyId, SegmentId, WriterIncarnation};
use crate::test_rng::TestRng;
const KIND_A: SectionKind = SectionKind::new(1);
const KIND_B: SectionKind = SectionKind::new(2);
fn family(name: &str) -> FamilyId {
FamilyId::from_digest(Digest128::of(name.as_bytes()))
}
fn meta() -> SegmentMeta {
SegmentMeta {
family: family("repo"),
segment: SegmentId::new(9),
incarnation: WriterIncarnation::new(3),
created_unix_ms: 1_793_700_000_000,
}
}
fn spec(kind: SectionKind, key_space: u16, alignment: Alignment) -> SectionSpec {
SectionSpec {
id: SectionId::new(kind, KeySpace::new(key_space)),
version: 1,
alignment,
requirement: Requirement::Required,
}
}
fn known() -> Vec<KnownSection> {
vec![
KnownSection {
kind: KIND_A,
version: 1,
},
KnownSection {
kind: KIND_B,
version: 1,
},
]
}
fn cache() -> Arc<BlockCache> {
Arc::new(BlockCache::new(8 * BLOCK_LEN))
}
fn sample_sections() -> Vec<(SectionSpec, Vec<u8>)> {
let mut rng = TestRng::new(1);
[
0,
1,
BLOCK_LEN - 1,
BLOCK_LEN,
BLOCK_LEN + 1,
3 * BLOCK_LEN + 17,
]
.into_iter()
.enumerate()
.map(|(index, len)| {
let alignment = if index % 2 == 0 {
Alignment::Word
} else {
Alignment::Vector
};
let kind = if index % 3 == 0 { KIND_B } else { KIND_A };
let key_space = u16::try_from(index).unwrap();
(spec(kind, key_space, alignment), rng.bytes(len))
})
.collect()
}
fn write_segment(dir: &Path, sections: &[(SectionSpec, Vec<u8>)]) -> PathBuf {
let path = dir.join("1.seg");
let mut writer = SegmentWriter::create(&path, meta()).unwrap();
for (spec, bytes) in sections {
writer.begin_section(*spec).unwrap();
for piece in bytes.chunks(10_007) {
writer.write(piece).unwrap();
}
writer.end_section(bytes.len() as u64 / 3).unwrap();
}
let summary = writer.finish().unwrap();
assert_eq!(summary.sections, sections.len());
assert_eq!(summary.len, std::fs::metadata(&path).unwrap().len());
path
}
#[test]
fn every_byte_reads_back_through_the_cache() {
let dir = tempfile::tempdir().unwrap();
let sections = sample_sections();
let path = write_segment(dir.path(), §ions);
let segment = Segment::open(&path, family("repo"), &known(), cache()).unwrap();
assert_eq!(segment.id(), SegmentId::new(9));
assert_eq!(segment.incarnation(), WriterIncarnation::new(3));
segment.verify().unwrap();
let mut rng = TestRng::new(2);
for (spec, bytes) in §ions {
let handle = segment.section(spec.id).unwrap();
let info = segment.info(handle);
assert_eq!(info.len, bytes.len() as u64);
assert_eq!(info.item_count, bytes.len() as u64 / 3);
assert_eq!(segment.read_range(handle, 0..info.len).unwrap(), *bytes);
for _ in 0..50 {
if bytes.is_empty() {
break;
}
let start = rng.below_usize(bytes.len());
let end = start + rng.below_usize(bytes.len() - start + 1);
let read = segment
.read_range(handle, start as u64..end as u64)
.unwrap();
assert_eq!(
read,
bytes[start..end],
"range {start}..{end} of {}",
spec.id
);
}
}
}
#[test]
fn sections_start_on_their_alignment() {
let dir = tempfile::tempdir().unwrap();
let sections = sample_sections();
let path = write_segment(dir.path(), §ions);
let file = std::fs::read(&path).unwrap();
let segment = Segment::open(&path, family("repo"), &known(), cache()).unwrap();
for (spec, bytes) in sections.iter().filter(|(_, bytes)| bytes.len() > 64) {
let handle = segment.section(spec.id).unwrap();
let first = segment.read_range(handle, 0..64).unwrap();
let offset = file
.windows(64)
.position(|window| window == first.as_slice())
.unwrap();
assert_eq!(offset as u64 % spec.alignment.bytes(), 0, "{}", spec.id);
assert_eq!(&file[offset..offset + bytes.len()], bytes.as_slice());
}
}
#[test]
fn reads_past_a_section_end_are_refused() {
let dir = tempfile::tempdir().unwrap();
let sections = sample_sections();
let path = write_segment(dir.path(), §ions);
let segment = Segment::open(&path, family("repo"), &known(), cache()).unwrap();
let handle = segment.section(sections[1].0.id).unwrap();
let error = segment.read_range(handle, 0..2).unwrap_err();
assert!(
matches!(error.kind(), ErrorKind::OutOfRange { len: 1, .. }),
"{error}"
);
}
#[test]
fn a_changed_block_fails_only_reads_that_touch_it() {
let dir = tempfile::tempdir().unwrap();
let sections = sample_sections();
let path = write_segment(dir.path(), §ions);
let (spec, bytes) = §ions[5];
let target = {
let segment = Segment::open(&path, family("repo"), &known(), cache()).unwrap();
let handle = segment.section(spec.id).unwrap();
let block_one = segment
.read_range(handle, BLOCK_LEN as u64..BLOCK_LEN as u64 + 32)
.unwrap();
let file = std::fs::read(&path).unwrap();
file.windows(32)
.position(|window| window == block_one.as_slice())
.unwrap()
};
let mut file = std::fs::read(&path).unwrap();
file[target + 5] ^= 0x40;
std::fs::write(&path, &file).unwrap();
let segment = Segment::open(&path, family("repo"), &known(), cache()).unwrap();
let handle = segment.section(spec.id).unwrap();
assert_eq!(
segment.read_range(handle, 0..100).unwrap(),
bytes[..100],
"block 0 is intact"
);
let error = segment
.read_range(handle, BLOCK_LEN as u64..BLOCK_LEN as u64 + 10)
.unwrap_err();
assert!(error.is_corruption());
assert!(
matches!(
error.kind(),
ErrorKind::Corrupt(Region::Block { block: 1, .. })
),
"{error}"
);
assert!(segment.verify().unwrap_err().is_corruption());
}
#[test]
fn a_segment_of_another_family_is_refused() {
let dir = tempfile::tempdir().unwrap();
let path = write_segment(dir.path(), &sample_sections());
let error = Segment::open(&path, family("other"), &known(), cache()).unwrap_err();
assert!(
matches!(error.kind(), ErrorKind::FamilyMismatch { .. }),
"{error}"
);
}
#[test]
fn unknown_required_sections_refuse_and_unknown_optional_ones_are_skipped() {
let dir = tempfile::tempdir().unwrap();
let mut sections = sample_sections();
sections.push((
SectionSpec {
id: SectionId::new(SectionKind::new(77), KeySpace::NONE),
version: 1,
alignment: Alignment::Word,
requirement: Requirement::Optional,
},
b"later plane".to_vec(),
));
let path = write_segment(dir.path(), §ions);
let segment = Segment::open(&path, family("repo"), &known(), cache()).unwrap();
assert_eq!(segment.sections().count(), sections.len() - 1);
assert!(
segment
.section(SectionId::new(SectionKind::new(77), KeySpace::NONE))
.is_none()
);
let only_newer = [KnownSection {
kind: KIND_A,
version: 2,
}];
let error = Segment::open(&path, family("repo"), &only_newer, cache()).unwrap_err();
assert!(
matches!(
error.kind(),
ErrorKind::UnknownRequiredSection { version: 1, .. }
),
"{error}"
);
}
#[test]
fn an_unfinished_segment_is_not_a_segment() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("torn.seg");
let mut writer = SegmentWriter::create(&path, meta()).unwrap();
writer
.write_section(spec(KIND_A, 0, Alignment::Word), &[7; 5000], 1)
.unwrap();
drop(writer);
let error = Segment::open(&path, family("repo"), &known(), cache()).unwrap_err();
assert!(matches!(error.kind(), ErrorKind::NotASegment), "{error}");
}
#[test]
fn writers_refuse_misuse() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("misuse.seg");
std::fs::write(&path, b"existing").unwrap();
assert!(matches!(
SegmentWriter::create(&path, meta()).unwrap_err().kind(),
ErrorKind::Io(error) if error.kind() == std::io::ErrorKind::AlreadyExists
));
let path = dir.path().join("fresh.seg");
let mut writer = SegmentWriter::create(&path, meta()).unwrap();
assert!(writer.write(b"loose").is_err(), "bytes outside a section");
let a = spec(KIND_A, 0, Alignment::Word);
writer.write_section(a, b"one", 1).unwrap();
assert!(matches!(
writer.write_section(a, b"two", 1).unwrap_err().kind(),
ErrorKind::DuplicateSection(_)
));
writer
.begin_section(spec(KIND_B, 0, Alignment::Word))
.unwrap();
assert!(matches!(
writer.finish().unwrap_err().kind(),
ErrorKind::UnfinishedSection(_)
));
}
#[test]
fn closing_a_segment_releases_its_cached_blocks() {
let dir = tempfile::tempdir().unwrap();
let sections = sample_sections();
let path = write_segment(dir.path(), §ions);
let shared = cache();
let segment = Segment::open(&path, family("repo"), &known(), Arc::clone(&shared)).unwrap();
let handle = segment.section(sections[5].0.id).unwrap();
segment
.read_range(handle, 0..segment.info(handle).len)
.unwrap();
assert_eq!(shared.stats().blocks, 4);
drop(segment);
assert_eq!(shared.stats().blocks, 0);
assert_eq!(shared.stats().resident_bytes, 0);
}
fn write_numbered(dir: &Path, n: u64, bytes: &[u8]) -> (PathBuf, SectionId) {
let path = dir.join(format!("{n}.seg"));
let section = spec(KIND_A, 0, Alignment::Word);
let mut writer = SegmentWriter::create(
&path,
SegmentMeta {
segment: SegmentId::new(n),
..meta()
},
)
.unwrap();
writer.begin_section(section).unwrap();
writer.write(bytes).unwrap();
writer.end_section(1).unwrap();
writer.finish().unwrap();
(path, section.id)
}
#[test]
fn past_the_cap_on_open_files_segments_reopen_their_own_file() {
let dir = tempfile::tempdir().unwrap();
let files = Arc::new(open_files::OpenFiles::new(3));
let mut rng = TestRng::new(7);
let written = (1..=6)
.map(|n| {
let bytes = rng.bytes(2 * BLOCK_LEN + 5);
let (path, id) = write_numbered(dir.path(), n, &bytes);
(path, id, bytes)
})
.collect::<Vec<_>>();
let cache = Arc::new(BlockCache::new(BLOCK_LEN));
let segments = written
.iter()
.map(|(path, _, _)| {
Segment::open_in(path, family("repo"), &known(), Arc::clone(&cache), &files).unwrap()
})
.collect::<Vec<_>>();
assert_eq!(files.open_count(), 3);
for round in 0..3 {
for (segment, (_, id, bytes)) in segments.iter().zip(&written) {
let handle = segment.section(*id).unwrap();
assert_eq!(
segment.read_range(handle, 0..bytes.len() as u64).unwrap(),
*bytes,
"round {round}, segment {}",
segment.id().get()
);
assert!(files.open_count() <= 3, "{}", files.open_count());
}
}
segments[5].verify().unwrap();
assert_eq!(files.open_count(), 3);
std::fs::copy(&written[3].0, &written[0].0).unwrap();
std::fs::remove_file(&written[1].0).unwrap();
let first = segments[0].section(written[0].1).unwrap();
let error = segments[0].read_range(first, 0..10).unwrap_err();
assert!(error.is_corruption(), "{error}");
let second = segments[1].section(written[1].1).unwrap();
let error = segments[1].read_range(second, 0..10).unwrap_err();
assert!(
matches!(error.kind(), ErrorKind::Io(io) if io.kind() == std::io::ErrorKind::NotFound),
"{error}"
);
std::fs::write(&written[2].0, b"short").unwrap();
let third = segments[2].section(written[2].1).unwrap();
assert!(
segments[2]
.read_range(third, 0..10)
.unwrap_err()
.is_corruption()
);
assert_eq!(files.open_count(), 3);
let (_, id, bytes) = &written[4];
let handle = segments[4].section(*id).unwrap();
assert_eq!(
segments[4]
.read_range(handle, 0..bytes.len() as u64)
.unwrap(),
*bytes
);
drop(segments);
assert_eq!(files.open_count(), 0);
}
#[test]
fn damaged_files_fail_cleanly_or_read_the_written_bytes() {
let dir = tempfile::tempdir().unwrap();
let sections = sample_sections();
let path = write_segment(dir.path(), §ions);
let original = std::fs::read(&path).unwrap();
let mut rng = TestRng::new(3);
let (mut opened, mut refused_reads) = (0, 0);
for round in 0..300 {
let mut damaged = original.clone();
match round % 3 {
0 => {
let at = rng.below_usize(damaged.len());
damaged[at] ^= 1 << rng.below(8);
}
1 => damaged.truncate(rng.below_usize(damaged.len())),
_ => {
let at = rng.below_usize(damaged.len());
let len = rng.below_usize(64).min(damaged.len() - at);
damaged[at..at + len].fill(0);
}
}
let damaged_path = dir.path().join(format!("damaged-{round}.seg"));
std::fs::write(&damaged_path, &damaged).unwrap();
let Ok(segment) = Segment::open(&damaged_path, family("repo"), &known(), cache()) else {
continue;
};
opened += 1;
for (spec, bytes) in §ions {
let Some(handle) = segment.section(spec.id) else {
continue;
};
match segment.read_range(handle, 0..bytes.len() as u64) {
Ok(read) => {
assert_eq!(read, *bytes, "round {round}: a read returned changed bytes");
}
Err(error) => {
assert!(error.is_corruption(), "round {round}: {error}");
refused_reads += 1;
}
}
}
}
assert!(
opened > 0 && refused_reads > 0,
"opened {opened}, refused reads {refused_reads}"
);
}
const PINNED_FORMAT_VERSION: u16 = 1;
const PINNED_REQUIRED_SECTIONS: [(u16, u16); 22] = [
(0x0100, 1),
(0x0101, 1),
(0x0102, 1),
(0x0103, 1),
(0x0200, 1),
(0x0201, 1),
(0x0202, 1),
(0x0203, 1),
(0x0300, 4),
(0x0301, 4),
(0x0302, 4),
(0x0303, 4),
(0x0304, 4),
(0x0400, 1),
(0x0401, 1),
(0x0402, 1),
(0x0403, 1),
(0x0404, 1),
(0x0405, 1),
(0x0406, 1),
(0x0407, 1),
(0x0408, 1),
];
fn required_sections() -> std::collections::BTreeSet<(u16, u16)> {
crate::text::KNOWN
.iter()
.chain(&crate::content::KNOWN)
.chain(&crate::graph::KNOWN)
.chain(&crate::vector::KNOWN)
.map(|known| (known.kind.get(), known.version))
.collect()
}
#[test]
fn the_segment_format_version_moves_with_the_required_sections() {
let sections = required_sections();
let pinned = PINNED_REQUIRED_SECTIONS
.into_iter()
.collect::<std::collections::BTreeSet<_>>();
assert!(
FORMAT_VERSION != PINNED_FORMAT_VERSION || sections == pinned,
"the required sections changed (added {:?}, removed {:?}) while the segment format \
version stayed {FORMAT_VERSION}: bump FORMAT_VERSION in segment/format.rs, so an older \
build refuses these segments by their version instead of meeting a required section \
it does not know and rebuilding over them. Then pin the new version and sections \
here. Only an optional section, which an older build skips, needs no bump.",
sections.difference(&pinned).collect::<Vec<_>>(),
pinned.difference(§ions).collect::<Vec<_>>()
);
assert!(
FORMAT_VERSION == PINNED_FORMAT_VERSION && sections == pinned,
"the segment format version is {FORMAT_VERSION} but this guard pins version \
{PINNED_FORMAT_VERSION}: pin the new version here together with the required sections \
it holds, {sections:?}"
);
}