Skip to content

Commit

Permalink
Change the start of the window to the first added item in last iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
Addimator committed Apr 25, 2024
1 parent 01c8849 commit e4c381e
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/bam/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use std::str;
use crate::bam;
use crate::bam::Read;
use crate::errors::{Error, Result};

/// A buffer for BAM records. This allows access regions in a sorted BAM file while iterating
/// over it in a single pass.
/// The buffer is implemented as a ringbuffer, such that extension or movement to the right has
Expand All @@ -25,6 +24,7 @@ pub struct RecordBuffer {
cache_cigar: bool,
min_refetch_distance: u64,
buffer_record: Rc<bam::Record>,
start_pos: Option<u64>,
}

unsafe impl Sync for RecordBuffer {}
Expand All @@ -45,6 +45,7 @@ impl RecordBuffer {
cache_cigar,
min_refetch_distance: 1,
buffer_record: Rc::new(bam::Record::new()),
start_pos: None
}
}

Expand Down Expand Up @@ -89,8 +90,8 @@ impl RecordBuffer {
if self.inner.is_empty()
|| window_start.saturating_sub(self.end().unwrap()) >= self.min_refetch_distance
|| self.tid().unwrap() != tid as i32
|| self.start().unwrap() > window_start
{
|| self.start().unwrap() > self.start_pos.unwrap()
{
let end = self.reader.header.target_len(tid).unwrap();
self.reader.fetch((tid, window_start, end))?;
deleted = self.inner.len();
Expand All @@ -106,7 +107,7 @@ impl RecordBuffer {
self.inner.pop_front();
}
deleted = to_remove;
}
}

// extend to the right
loop {
Expand Down Expand Up @@ -139,6 +140,7 @@ impl RecordBuffer {

if pos >= end as i64 {
self.overflow = Some(record);
self.start_pos = self.start();
break;
} else {
self.inner.push_back(record);
Expand Down

0 comments on commit e4c381e

Please sign in to comment.