Skip to content

Commit

Permalink
[TieredStorage] Add AccountOffset type (solana-labs#33927)
Browse files Browse the repository at this point in the history
#### Problem
TieredStorage conceptually has different offsets.  However, the current code directly
uses the same primitive type for accessing offsets, which is error-prone as one could
easily use one offset to access data that should be accessed with a different offset
type.

#### Summary of Changes
This PR introduces the AccountOffset type, which allows static-check to on different
type of TieredStorage offsets.
  • Loading branch information
yhchiang-sol authored Nov 6, 2023
1 parent 70d97d3 commit da130b8
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions accounts-db/src/tiered_storage/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ pub struct AccountIndexWriterEntry<'a> {
pub intra_block_offset: u64,
}

/// The offset to an account stored inside its accounts block.
/// This struct is used to access the meta and data of an account by looking through
/// its accounts block.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct AccountOffset {
/// The offset to the accounts block that contains the account meta/data.
pub block: usize,
}

/// The index format of a tiered accounts file.
#[repr(u16)]
#[derive(
Expand Down Expand Up @@ -76,21 +85,22 @@ impl IndexBlockFormat {
Ok(address)
}

/// Returns the offset to the account block that contains the account
/// associated with the specified index to the index block.
pub fn get_account_block_offset(
/// Returns the offset to the account given the specified index.
pub fn get_account_offset(
&self,
map: &Mmap,
footer: &TieredStorageFooter,
index: usize,
) -> TieredStorageResult<u64> {
) -> TieredStorageResult<AccountOffset> {
match self {
Self::AddressAndOffset => {
let offset = footer.index_block_offset as usize
+ std::mem::size_of::<Pubkey>() * footer.account_entry_count as usize
+ index * std::mem::size_of::<u64>();
let (account_block_offset, _) = get_type(map, offset)?;
Ok(*account_block_offset)
Ok(AccountOffset {
block: *account_block_offset,
})
}
}
}
Expand Down Expand Up @@ -146,10 +156,8 @@ mod tests {
.unwrap();
let map = unsafe { MmapOptions::new().map(&file).unwrap() };
for (i, index_entry) in index_entries.iter().enumerate() {
assert_eq!(
index_entry.block_offset,
indexer.get_account_block_offset(&map, &footer, i).unwrap()
);
let account_offset = indexer.get_account_offset(&map, &footer, i).unwrap();
assert_eq!(index_entry.block_offset, account_offset.block as u64);
let address = indexer.get_account_address(&map, &footer, i).unwrap();
assert_eq!(index_entry.address, address);
}
Expand Down

0 comments on commit da130b8

Please sign in to comment.