Skip to content

Commit

Permalink
fix: invalid ScoreWindow in VariableBlockPartition
Browse files Browse the repository at this point in the history
Signed-off-by: Mingzhuo Yin <[email protected]>
  • Loading branch information
silver-ymz committed Jan 7, 2025
1 parent 72d663e commit 7e92b62
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/algorithm/block_encode/delta_bitpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ mod tests {
use super::*;

#[test]
fn test_delta_bitpack_next() {
fn test_next() {
let mut encoder = DeltaBitpackEncode::new();
let mut decoder = DeltaBitpackDecode::new();

Expand Down Expand Up @@ -294,7 +294,7 @@ mod tests {
}

#[test]
fn test_delta_bitpack_seek() {
fn test_seek() {
let mut encoder = DeltaBitpackEncode::new();
let mut decoder = DeltaBitpackDecode::new();

Expand Down Expand Up @@ -326,7 +326,7 @@ mod tests {
}

#[test]
fn test_delta_bitpack_seek2() {
fn test_seek_long() {
let mut encoder = DeltaBitpackEncode::new();
let mut decoder = DeltaBitpackDecode::new();

Expand Down Expand Up @@ -354,7 +354,7 @@ mod tests {
}

#[test]
fn test_delta_bitpack_zero_bit_width() {
fn test_zero_bit_width() {
let mut encoder = DeltaBitpackEncode::new();
let mut decoder = DeltaBitpackDecode::new();

Expand Down
6 changes: 3 additions & 3 deletions src/algorithm/block_encode/elias_fano.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ mod tests {
use super::*;

#[test]
fn test_elias_fano_next() {
fn test_next() {
let mut encoder = EliasFanoEncode::new();
let mut decoder = EliasFanoDecode::new();

Expand Down Expand Up @@ -404,7 +404,7 @@ mod tests {
}

#[test]
fn test_elias_fano_seek() {
fn test_seek() {
let mut encoder = EliasFanoEncode::new();
let mut decoder = EliasFanoDecode::new();

Expand Down Expand Up @@ -436,7 +436,7 @@ mod tests {
}

#[test]
fn test_elias_fano_seek2() {
fn test_seek_long() {
let mut encoder = EliasFanoEncode::new();
let mut decoder = EliasFanoDecode::new();

Expand Down
58 changes: 56 additions & 2 deletions src/algorithm/block_partition/variable_block_partition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl BlockPartitionTrait for VariableBlockPartition {

let mut cost_bound = self.lambda;
while self.eps1 == 0. || cost_bound < self.lambda / self.eps1 {
self.score_window.push(Default::default());
self.score_window.push(ScoreWindow::new(cost_bound));
if cost_bound >= max_block_cost {
break;
}
Expand Down Expand Up @@ -129,7 +129,6 @@ impl BlockPartitionTrait for VariableBlockPartition {
}
}

#[derive(Default)]
struct ScoreWindow {
start: u32,
end: u32,
Expand All @@ -139,6 +138,16 @@ struct ScoreWindow {
}

impl ScoreWindow {
fn new(cost_upper_bound: f32) -> Self {
Self {
start: 0,
end: 0,
cost_upper_bound,
sum: 0.,
max_queue: VecDeque::new(),
}
}

fn advance_start(&mut self, scores: &[f32]) {
let score = scores[self.start as usize];
self.sum -= score;
Expand All @@ -162,3 +171,48 @@ impl ScoreWindow {
(self.end - self.start) as f32 * self.max_queue.front().unwrap() - self.sum + fixed_cost
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_single() {
let mut partition = VariableBlockPartition::new(1., 0.01, 0.4);
partition.add_doc(1.);
partition.make_partitions();
assert_eq!(partition.partitions(), &[0]);
assert_eq!(partition.max_doc(), &[0]);
}

#[test]
fn test_split_block() {
let mut partition = VariableBlockPartition::new(0.1, 0.01, 0.4);
partition.add_doc(1.);
partition.add_doc(2.);
partition.make_partitions();
assert_eq!(partition.partitions(), &[0, 1]);
assert_eq!(partition.max_doc(), &[0, 1]);
}

#[test]
fn test_merge_block() {
let mut partition = VariableBlockPartition::new(f32::MAX, 0.01, 0.4);
partition.add_doc(1.);
partition.add_doc(2.);
partition.make_partitions();
assert_eq!(partition.partitions(), &[1]);
assert_eq!(partition.max_doc(), &[1]);
}

#[test]
fn test_optimal_block() {
let mut partition = VariableBlockPartition::new(1., 0.01, 0.4);
partition.add_doc(1.);
partition.add_doc(1.);
partition.add_doc(10.);
partition.make_partitions();
assert_eq!(partition.partitions(), &[1, 2]);
assert_eq!(partition.max_doc(), &[1, 2]);
}
}

0 comments on commit 7e92b62

Please sign in to comment.