Skip to content

Commit

Permalink
[TieredStorage] Add IndexPosition type
Browse files Browse the repository at this point in the history
  • Loading branch information
yhchiang-sol committed Oct 30, 2023
1 parent cdc2841 commit d445622
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions accounts-db/src/tiered_storage/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ pub enum AccountIndexFormat {
AddressAndOffset = 0,
}

/// The position of 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.
pub struct IndexPosition(usize);

impl AccountIndexFormat {
/// Persists the specified index_entries to the specified file and returns
/// the total number of bytes written.
Expand Down Expand Up @@ -65,11 +70,11 @@ impl AccountIndexFormat {
&self,
map: &'a Mmap,
footer: &TieredStorageFooter,
index: usize,
index: IndexPosition,
) -> TieredStorageResult<&'a Pubkey> {
let offset = match self {
Self::AddressAndOffset => {
footer.account_index_offset as usize + std::mem::size_of::<Pubkey>() * index
footer.account_index_offset as usize + std::mem::size_of::<Pubkey>() * index.0
}
};
let (address, _) = get_type::<Pubkey>(map, offset)?;
Expand All @@ -82,13 +87,13 @@ impl AccountIndexFormat {
&self,
map: &Mmap,
footer: &TieredStorageFooter,
index: usize,
index: IndexPosition,
) -> TieredStorageResult<u64> {
match self {
Self::AddressAndOffset => {
let offset = footer.account_index_offset as usize
+ std::mem::size_of::<Pubkey>() * footer.account_entry_count as usize
+ index * std::mem::size_of::<u64>();
+ index.0 * std::mem::size_of::<u64>();
let (account_block_offset, _) = get_type(map, offset)?;
Ok(*account_block_offset)
}
Expand Down Expand Up @@ -148,9 +153,13 @@ mod tests {
for (i, index_entry) in index_entries.iter().enumerate() {
assert_eq!(
index_entry.block_offset,
indexer.get_account_block_offset(&map, &footer, i).unwrap()
indexer
.get_account_block_offset(&map, &footer, IndexPosition(i))
.unwrap()
);
let address = indexer.get_account_address(&map, &footer, i).unwrap();
let address = indexer
.get_account_address(&map, &footer, IndexPosition(i))
.unwrap();
assert_eq!(index_entry.address, address);
}
}
Expand Down

0 comments on commit d445622

Please sign in to comment.