Skip to content

Commit

Permalink
Upgrade zerocopy (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaimast authored Oct 18, 2024
1 parent 526df5f commit 9b9e30a
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 67 deletions.
42 changes: 31 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ lru = "0.12"
parking_lot = "0.12"
memmap2 = "0.9"
byte-slice-cast = "1"
zerocopy = { version="0.7", features=["derive"] }
zerocopy = { version="0.8", features=["derive"] }
log = "0.4"
futures = "0.3"
snap = { version="1", optional=true }
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[toolchain]
channel="nightly-2024-09-17"
channel="nightly-2024-10-17"
10 changes: 5 additions & 5 deletions src/data_blocks/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::sorted_table::Key;

use super::{DataEntry, SearchResult};

use zerocopy::{AsBytes, FromBytes, FromZeroes};
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};

#[cfg(feature = "bloom-filters")]
use bloomfilter::Bloom;
Expand All @@ -29,7 +29,7 @@ pub(super) const BLOOM_KEY_NUM: usize = 1024;
* 4. Sequence of variable-length entries
* 5. Variable length restart list (each entry is 4bytes; so we don't need length information)
*/
#[derive(AsBytes, FromBytes, FromZeroes)]
#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
#[repr(C)]
pub(super) struct DataBlockHeader {
pub(super) restart_list_start: u32,
Expand Down Expand Up @@ -66,7 +66,7 @@ pub(super) struct DataBlockHeader {
* - Variable length key suffix
* - Variable length value
*/
#[derive(AsBytes, FromBytes, FromZeroes)]
#[derive(IntoBytes, Immutable, FromBytes, KnownLayout)]
#[repr(packed)]
pub(super) struct EntryHeader {
pub(super) prefix_len: u32,
Expand Down Expand Up @@ -95,7 +95,7 @@ impl DataBlock {
pub fn new_from_data(data: Vec<u8>, restart_interval: u32) -> Self {
assert!(!data.is_empty(), "No data?");

let header = DataBlockHeader::ref_from(&data[..Self::header_length()]).unwrap();
let header = DataBlockHeader::ref_from_bytes(&data[..Self::header_length()]).unwrap();

#[cfg(feature = "bloom-filters")]
let bloom_filter = {
Expand Down Expand Up @@ -146,7 +146,7 @@ impl DataBlock {
panic!("Invalid offset {offset}");
}

let header = EntryHeader::ref_from(&self_ptr.data[offset..offset + header_len])
let header = EntryHeader::ref_from_bytes(&self_ptr.data[offset..offset + header_len])
.expect("Failed to read entry header");
let entry_offset = offset;

Expand Down
2 changes: 1 addition & 1 deletion src/data_blocks/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::Arc;
use crate::manifest::SeqNumber;
use crate::{disk, Error};

use zerocopy::AsBytes;
use zerocopy::IntoBytes;

use super::block::{DataBlockHeader, EntryHeader};
use super::{DataBlock, DataBlockId, DataBlocks, PrefixedKey};
Expand Down
2 changes: 1 addition & 1 deletion src/data_blocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl DataEntry {
fn get_header(&self) -> &EntryHeader {
let header_len = std::mem::size_of::<EntryHeader>();
let header_data = &self.block.data[self.offset..self.offset + header_len];
EntryHeader::ref_from(header_data).expect("Failed to read entry header")
EntryHeader::ref_from_bytes(header_data).expect("Failed to read entry header")
}

pub fn get_sequence_number(&self) -> u64 {
Expand Down
20 changes: 10 additions & 10 deletions src/index_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use crate::sorted_table::TableId;
use crate::{disk, Error};
use crate::{Key, Params};

use zerocopy::{AsBytes, FromBytes, FromZeroes};
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};

#[derive(Debug, AsBytes, FromBytes, FromZeroes)]
#[derive(Debug, IntoBytes, KnownLayout, Immutable, FromBytes)]
#[repr(packed)]
struct IndexBlockHeader {
size: u64,
Expand All @@ -19,7 +19,7 @@ struct IndexBlockHeader {
_padding: u32,
}

#[derive(AsBytes, FromBytes, FromZeroes)]
#[derive(IntoBytes, KnownLayout, Immutable, FromBytes)]
#[repr(packed)]
struct IndexEntryHeader {
block_id: DataBlockId,
Expand Down Expand Up @@ -109,7 +109,7 @@ impl IndexBlock {
}

fn get_header(&self) -> &IndexBlockHeader {
IndexBlockHeader::ref_from_prefix(&self.data[..]).unwrap()
IndexBlockHeader::ref_from_prefix(&self.data[..]).unwrap().0
}

fn get_entry_offset(&self, pos: usize) -> usize {
Expand All @@ -121,17 +121,17 @@ impl IndexBlock {
+ header.max_key_len as usize;

let offset_offset = crate::pad_offset(offset) + pos * size_of::<u32>();

*u32::ref_from_prefix(&self.data[offset_offset..]).unwrap() as usize
*u32::ref_from_prefix(&self.data[offset_offset..]).unwrap().0 as usize
}

/// Get the unique id for the data block at the specified index
pub fn get_block_id(&self, pos: usize) -> DataBlockId {
let offset = self.get_entry_offset(pos);

let entry_header =
IndexEntryHeader::ref_from(&self.data[offset..offset + size_of::<IndexEntryHeader>()])
.unwrap();
let entry_header = IndexEntryHeader::ref_from_bytes(
&self.data[offset..offset + size_of::<IndexEntryHeader>()],
)
.unwrap();

entry_header.block_id
}
Expand All @@ -140,7 +140,7 @@ impl IndexBlock {
pub fn get_block_key(&self, pos: usize) -> &[u8] {
let offset = self.get_entry_offset(pos);

let entry_header = IndexEntryHeader::ref_from_prefix(&self.data[offset..]).unwrap();
let (entry_header, _) = IndexEntryHeader::ref_from_prefix(&self.data[offset..]).unwrap();

let key_start = offset + size_of::<IndexEntryHeader>();
&self.data[key_start..key_start + (entry_header.key_len as usize)]
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![feature(trait_alias)]
#![feature(get_mut_unchecked)]
#![feature(let_chains)]
#![feature(const_option)]
#![feature(trivial_bounds)]
// Temporary workaround for the io_uring code
#![allow(clippy::arc_with_non_send_sync)]
Expand Down
Loading

0 comments on commit 9b9e30a

Please sign in to comment.