Skip to content

Commit

Permalink
[TieredStorage] Make IndexOffset use u32 (#34152)
Browse files Browse the repository at this point in the history
#### Problem
IndexOffset currently uses `usize`, which size is platform dependent.
We want a fixed size type that is consist to what we persist in the tiered-storage file.

#### Summary of Changes
This PR makes IndexOffset use u32.
  • Loading branch information
yhchiang-sol committed Nov 20, 2023
1 parent 8a298f1 commit c73f226
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
8 changes: 6 additions & 2 deletions accounts-db/src/tiered_storage/hot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,10 +531,14 @@ pub mod tests {

let hot_storage = HotStorageReader::new_from_path(&path).unwrap();
for (i, index_writer_entry) in index_writer_entries.iter().enumerate() {
let account_offset = hot_storage.get_account_offset(IndexOffset(i)).unwrap();
let account_offset = hot_storage
.get_account_offset(IndexOffset(i as u32))
.unwrap();
assert_eq!(account_offset.block as u32, index_writer_entry.block_offset);

let account_address = hot_storage.get_account_address(IndexOffset(i)).unwrap();
let account_address = hot_storage
.get_account_address(IndexOffset(i as u32))
.unwrap();
assert_eq!(account_address, index_writer_entry.address);
}
}
Expand Down
11 changes: 6 additions & 5 deletions accounts-db/src/tiered_storage/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct AccountOffset {
/// This can be used to obtain the AccountOffset and address by looking through
/// the accounts index block.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct IndexOffset(pub usize);
pub struct IndexOffset(pub u32);

/// The index format of a tiered accounts file.
#[repr(u16)]
Expand Down Expand Up @@ -84,7 +84,8 @@ impl IndexBlockFormat {
) -> TieredStorageResult<&'a Pubkey> {
let account_offset = match self {
Self::AddressAndBlockOffsetOnly => {
footer.index_block_offset as usize + std::mem::size_of::<Pubkey>() * index_offset.0
footer.index_block_offset as usize
+ std::mem::size_of::<Pubkey>() * (index_offset.0 as usize)
}
};
let (address, _) = get_type::<Pubkey>(mmap, account_offset)?;
Expand All @@ -102,7 +103,7 @@ impl IndexBlockFormat {
Self::AddressAndBlockOffsetOnly => {
let account_offset = footer.index_block_offset as usize
+ std::mem::size_of::<Pubkey>() * footer.account_entry_count as usize
+ index_offset.0 * std::mem::size_of::<u32>();
+ std::mem::size_of::<u32>() * index_offset.0 as usize;
let (block_offset, _) = get_type::<u32>(mmap, account_offset)?;

Ok(AccountOffset {
Expand Down Expand Up @@ -166,11 +167,11 @@ mod tests {
let mmap = unsafe { MmapOptions::new().map(&file).unwrap() };
for (i, index_entry) in index_entries.iter().enumerate() {
let account_offset = indexer
.get_account_offset(&mmap, &footer, IndexOffset(i))
.get_account_offset(&mmap, &footer, IndexOffset(i as u32))
.unwrap();
assert_eq!(index_entry.block_offset, account_offset.block as u32);
let address = indexer
.get_account_address(&mmap, &footer, IndexOffset(i))
.get_account_address(&mmap, &footer, IndexOffset(i as u32))
.unwrap();
assert_eq!(index_entry.address, address);
}
Expand Down

0 comments on commit c73f226

Please sign in to comment.