Skip to content

Commit

Permalink
fix offset calculation
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Sidorov <[email protected]>
  • Loading branch information
Mikhail Sidorov authored and mikelsid committed Aug 15, 2024
1 parent ac4e194 commit 8724a17
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/file-source/src/file_watcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct FileWatcher {
findable: bool,
reader: Box<dyn BufRead>,
file_position: FilePosition,
last_line_offset: u64,
devno: u64,
inode: u64,
is_dead: bool,
Expand Down Expand Up @@ -142,6 +143,7 @@ impl FileWatcher {
findable: true,
reader,
file_position,
last_line_offset: file_position,
devno,
inode: ino,
is_dead: false,
Expand Down Expand Up @@ -208,10 +210,12 @@ impl FileWatcher {
/// a new file handler as needed, transparently to the caller.
pub(super) fn read_line(&mut self) -> io::Result<Option<RawLine>> {
self.track_read_attempt();
self.track_last_line_offset();

let line_offset = self.last_line_offset;
let reader = &mut self.reader;
let file_position = &mut self.file_position;
let initial_position = *file_position;

match read_until_with_max_size(
reader,
file_position,
Expand All @@ -222,7 +226,7 @@ impl FileWatcher {
Ok(Some(_)) => {
self.track_read_success();
Ok(Some(RawLine {
offset: initial_position,
offset: line_offset,
bytes: self.buf.split().freeze(),
}))
}
Expand All @@ -239,7 +243,7 @@ impl FileWatcher {
Ok(None)
} else {
Ok(Some(RawLine {
offset: initial_position,
offset: line_offset,
bytes: buf,
}))
}
Expand Down Expand Up @@ -267,6 +271,13 @@ impl FileWatcher {
self.last_read_success = Instant::now();
}

#[inline]
fn track_last_line_offset(&mut self) {
if self.buf.is_empty() {
self.last_line_offset = self.file_position
}
}

#[inline]
pub fn last_read_success(&self) -> Instant {
self.last_read_success
Expand Down

0 comments on commit 8724a17

Please sign in to comment.