Perform consistency check when reexporting

This commit is contained in:
Joscha 2022-10-03 18:11:37 +02:00
parent e74eee89e6
commit d910047b48
3 changed files with 13 additions and 6 deletions

View file

@ -206,12 +206,7 @@ pub fn ingest(datafile: &Path) -> io::Result<()> {
let data = second_stage.into_adjacency_list();
eprintln!(">> Consistency check");
let range = 0..data.pages.len() as u32;
for link in &data.links {
if !range.contains(&link.to) {
eprintln!("Invalid link detected!");
}
}
data.check_consistency();
eprintln!(">> Export");
let mut datafile = BufWriter::new(File::create(datafile)?);

View file

@ -9,6 +9,9 @@ pub fn reexport(from: &Path, to: &Path) -> io::Result<()> {
let from = BufReader::new(File::open(from)?);
let data = AdjacencyList::read(from)?;
eprintln!(">> Consistency check");
data.check_consistency();
eprintln!(">> Export");
let to = BufWriter::new(File::create(to)?);
data.write(to)?;

View file

@ -24,6 +24,15 @@ pub struct AdjacencyList {
}
impl AdjacencyList {
pub fn check_consistency(&self) {
let range = 0..self.pages.len() as u32;
for link in &self.links {
if !range.contains(&link.to) {
panic!("Invalid link detected!");
}
}
}
pub fn write<W: Write>(&self, mut to: W) -> io::Result<()> {
let n_pages: u32 = self.pages.len() as u32;
to.write_all(&n_pages.to_le_bytes())?;