Skip to content

Commit

Permalink
[TieredStorage] Add IndexOffset type (solana-labs#33929)
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 adds IndexOffset type -- a struct for obtaining the ith entry inside the
index-block to obtain account's offset and address.
  • Loading branch information
yhchiang-sol authored Nov 7, 2023
1 parent b8115b4 commit b013c03
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions accounts-db/src/tiered_storage/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ pub struct AccountOffset {
pub block: usize,
}

/// The offset to an account/address entry in the accounts index block.
/// 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(usize);

/// The index format of a tiered accounts file.
#[repr(u16)]
#[derive(
Expand Down Expand Up @@ -74,11 +80,11 @@ impl IndexBlockFormat {
&self,
map: &'a Mmap,
footer: &TieredStorageFooter,
index: usize,
offset: IndexOffset,
) -> TieredStorageResult<&'a Pubkey> {
let offset = match self {
Self::AddressAndOffset => {
footer.index_block_offset as usize + std::mem::size_of::<Pubkey>() * index
footer.index_block_offset as usize + std::mem::size_of::<Pubkey>() * offset.0
}
};
let (address, _) = get_type::<Pubkey>(map, offset)?;
Expand All @@ -90,13 +96,13 @@ impl IndexBlockFormat {
&self,
map: &Mmap,
footer: &TieredStorageFooter,
index: usize,
offset: IndexOffset,
) -> 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>();
+ offset.0 * std::mem::size_of::<u64>();
let (account_block_offset, _) = get_type(map, offset)?;
Ok(AccountOffset {
block: *account_block_offset,
Expand Down Expand Up @@ -156,9 +162,13 @@ mod tests {
.unwrap();
let map = unsafe { MmapOptions::new().map(&file).unwrap() };
for (i, index_entry) in index_entries.iter().enumerate() {
let account_offset = indexer.get_account_offset(&map, &footer, i).unwrap();
let account_offset = indexer
.get_account_offset(&map, &footer, IndexOffset(i))
.unwrap();
assert_eq!(index_entry.block_offset, account_offset.block as u64);
let address = indexer.get_account_address(&map, &footer, i).unwrap();
let address = indexer
.get_account_address(&map, &footer, IndexOffset(i))
.unwrap();
assert_eq!(index_entry.address, address);
}
}
Expand Down

0 comments on commit b013c03

Please sign in to comment.