Skip to content

Commit

Permalink
Fix hash collision bug
Browse files Browse the repository at this point in the history
  • Loading branch information
bglw committed Jun 8, 2022
1 parent 32c1bce commit 1beff0c
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions pagefind/src/index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,12 @@ where
let mut short_hash = &page.fragment.hash[0..=6];
// If we hit a collision, extend both hashes until we stop colliding
while let Some(collision) = fragments.remove(short_hash) {
let new_length = short_hash.len() + 1;

fragments.insert(collision.hash[0..=new_length].to_string(), collision);
short_hash = &page.fragment.hash[0..=new_length];

if short_hash.len() == page.fragment.hash.len() {
break;
if short_hash.len() >= page.fragment.hash.len() {
fragments.insert(format!("{}0", collision.hash), collision);
} else {
let new_length = short_hash.len() + 1;
fragments.insert(collision.hash[0..=new_length].to_string(), collision);
short_hash = &page.fragment.hash[0..=new_length];
}
}
fragments.insert(short_hash.to_string(), page.fragment);
Expand Down

0 comments on commit 1beff0c

Please sign in to comment.