Skip to content

Commit

Permalink
fix: block backlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
marvin-j97 committed May 17, 2024
1 parent f2c5837 commit 0cb81fa
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/segment/block/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Serializable for Header {
// Write CRC
writer.write_u32::<BigEndian>(self.crc)?;

// Write CRC
// Write prev offset
writer.write_u64::<BigEndian>(self.previous_block_offset)?;

// Write data length
Expand Down Expand Up @@ -74,7 +74,7 @@ impl Deserializable for Header {
// Read CRC
let crc = reader.read_u32::<BigEndian>()?;

// Read prev
// Read prev offset
let previous_block_offset = reader.read_u64::<BigEndian>()?;

// Read data length
Expand Down
13 changes: 10 additions & 3 deletions src/segment/block_index/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ fn pipe_file_into_writer<P: AsRef<Path>, W: Write>(src_path: P, sink: &mut W) ->
pub struct Writer {
pub index_block_tmp_file_path: PathBuf,
file_pos: u64,

prev_pos: (u64, u64),

block_writer: Option<BufWriter<File>>,
block_size: u32,
block_counter: u32,
Expand All @@ -42,6 +45,7 @@ impl Writer {
Ok(Self {
index_block_tmp_file_path,
file_pos: 0,
prev_pos: (0, 0),
block_writer: Some(block_writer),
block_counter: 0,
block_size,
Expand All @@ -54,12 +58,12 @@ impl Writer {
let mut block_writer = self.block_writer.as_mut().expect("should exist");

// Write to file
let (header, data) = IndexBlock::to_bytes_compressed(&self.block_handles, self.file_pos)?;
let (header, data) = IndexBlock::to_bytes_compressed(&self.block_handles, self.prev_pos.0)?;

header.serialize(&mut block_writer)?;
block_writer.write_all(&data)?;

let bytes_written = BlockHeader::serialized_len() + data.len();
let bytes_written = (BlockHeader::serialized_len() + data.len()) as u64;

// Expect is fine, because the chunk is not empty
let last = self
Expand All @@ -75,7 +79,10 @@ impl Writer {
self.tli_pointers.push(index_block_handle);

self.block_counter = 0;
self.file_pos += bytes_written as u64;
self.file_pos += bytes_written;

self.prev_pos.0 = self.prev_pos.1;
self.prev_pos.1 += bytes_written;

self.block_handles.clear();

Expand Down
13 changes: 10 additions & 3 deletions src/segment/trailer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ pub const TRAILER_SIZE: usize = 256;
#[derive(Debug)]
#[allow(clippy::module_name_repetitions)]
pub struct SegmentFileTrailer {
pub(crate) metadata: Metadata,
pub(crate) offsets: FileOffsets,
#[doc(hidden)]
pub metadata: Metadata,

#[doc(hidden)]
pub offsets: FileOffsets,
}

impl SegmentFileTrailer {
Expand Down Expand Up @@ -79,7 +82,11 @@ impl Serializable for SegmentFileTrailer {

v.write_all(TRAILER_MAGIC)?;

debug_assert_eq!(v.len(), TRAILER_SIZE);
assert_eq!(
v.len(),
TRAILER_SIZE,
"segment file trailer has invalid size"
);

writer.write_all(&v)?;

Expand Down
22 changes: 14 additions & 8 deletions src/segment/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub struct Writer {
pub item_count: usize,
pub file_pos: u64,

prev_pos: (u64, u64),

/// Only takes user data into account
pub uncompressed_size: u64,

Expand Down Expand Up @@ -68,13 +70,11 @@ pub struct Options {

#[derive(Debug)]
pub struct FileOffsets {
pub(crate) index_block_ptr: u64,
pub(crate) tli_ptr: u64,
pub(crate) bloom_ptr: u64,
pub(crate) range_tombstone_ptr: u64,

#[allow(unused)]
pub(crate) metadata_ptr: u64,
pub index_block_ptr: u64,
pub tli_ptr: u64,
pub bloom_ptr: u64,
pub range_tombstone_ptr: u64,
pub metadata_ptr: u64,
}

impl Writer {
Expand All @@ -101,6 +101,9 @@ impl Writer {
block_count: 0,
item_count: 0,
file_pos: 0,

prev_pos: (0, 0),

uncompressed_size: 0,

first_key: None,
Expand Down Expand Up @@ -134,7 +137,7 @@ impl Writer {
self.uncompressed_size += uncompressed_chunk_size;

// Write to file
let (header, data) = ValueBlock::to_bytes_compressed(&self.chunk, self.file_pos)?;
let (header, data) = ValueBlock::to_bytes_compressed(&self.chunk, self.prev_pos.0)?;

header.serialize(&mut self.block_writer)?;
self.block_writer.write_all(&data)?;
Expand All @@ -152,6 +155,9 @@ impl Writer {
self.item_count += self.chunk.len();
self.block_count += 1;

self.prev_pos.0 = self.prev_pos.1;
self.prev_pos.1 += bytes_written;

self.chunk.clear();

Ok(())
Expand Down

0 comments on commit 0cb81fa

Please sign in to comment.