disk index: refactor OccupiedEnumTag (#31028)
This commit is contained in:
parent
ab8d3066a5
commit
05f2a01a80
|
@ -5038,6 +5038,7 @@ dependencies = [
|
||||||
"log",
|
"log",
|
||||||
"memmap2",
|
"memmap2",
|
||||||
"modular-bitfield",
|
"modular-bitfield",
|
||||||
|
"num_enum",
|
||||||
"rand 0.7.3",
|
"rand 0.7.3",
|
||||||
"rayon",
|
"rayon",
|
||||||
"solana-logger 1.16.0",
|
"solana-logger 1.16.0",
|
||||||
|
|
|
@ -15,6 +15,7 @@ bv = { workspace = true, features = ["serde"] }
|
||||||
log = { workspace = true }
|
log = { workspace = true }
|
||||||
memmap2 = { workspace = true }
|
memmap2 = { workspace = true }
|
||||||
modular-bitfield = { workspace = true }
|
modular-bitfield = { workspace = true }
|
||||||
|
num_enum = "0.5.7"
|
||||||
rand = { workspace = true }
|
rand = { workspace = true }
|
||||||
solana-measure = { workspace = true }
|
solana-measure = { workspace = true }
|
||||||
solana-sdk = { workspace = true }
|
solana-sdk = { workspace = true }
|
||||||
|
|
|
@ -7,6 +7,7 @@ use {
|
||||||
},
|
},
|
||||||
bv::BitVec,
|
bv::BitVec,
|
||||||
modular_bitfield::prelude::*,
|
modular_bitfield::prelude::*,
|
||||||
|
num_enum::FromPrimitive,
|
||||||
solana_sdk::{clock::Slot, pubkey::Pubkey},
|
solana_sdk::{clock::Slot, pubkey::Pubkey},
|
||||||
std::{fmt::Debug, marker::PhantomData},
|
std::{fmt::Debug, marker::PhantomData},
|
||||||
};
|
};
|
||||||
|
@ -183,32 +184,45 @@ pub(crate) union SingleElementOrMultipleSlots<T: Clone + Copy> {
|
||||||
pub(crate) multiple_slots: MultipleSlots,
|
pub(crate) multiple_slots: MultipleSlots,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// just the values for `OccupiedEnum`
|
||||||
|
/// This excludes the contents of any enum value.
|
||||||
|
#[derive(PartialEq, FromPrimitive)]
|
||||||
|
#[repr(u8)]
|
||||||
|
enum OccupiedEnumTag {
|
||||||
|
#[default]
|
||||||
|
Free = 0,
|
||||||
|
ZeroSlots = 1,
|
||||||
|
OneSlotInIndex = 2,
|
||||||
|
MultipleSlots = 3,
|
||||||
|
}
|
||||||
|
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub(crate) enum OccupiedEnum<'a, T> {
|
pub(crate) enum OccupiedEnum<'a, T> {
|
||||||
/// this spot is not occupied.
|
/// this spot is not occupied.
|
||||||
/// ALL other enum values ARE occupied.
|
/// ALL other enum values ARE occupied.
|
||||||
Free = 0,
|
Free = OccupiedEnumTag::Free as u8,
|
||||||
/// zero slots in the slot list
|
/// zero slots in the slot list
|
||||||
ZeroSlots = 1,
|
ZeroSlots = OccupiedEnumTag::ZeroSlots as u8,
|
||||||
/// one slot in the slot list, it is stored in the index
|
/// one slot in the slot list, it is stored in the index
|
||||||
OneSlotInIndex(&'a T) = 2,
|
OneSlotInIndex(&'a T) = OccupiedEnumTag::OneSlotInIndex as u8,
|
||||||
/// data is stored in data file
|
/// data is stored in data file
|
||||||
MultipleSlots(&'a MultipleSlots) = 3,
|
MultipleSlots(&'a MultipleSlots) = OccupiedEnumTag::MultipleSlots as u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Copy> IndexEntry<T> {
|
impl<T: Copy> IndexEntry<T> {
|
||||||
pub(crate) fn get_slot_count_enum(&self) -> OccupiedEnum<'_, T> {
|
pub(crate) fn get_slot_count_enum(&self) -> OccupiedEnum<'_, T> {
|
||||||
unsafe {
|
let enum_tag =
|
||||||
match self.packed_ref_count.slot_count_enum() {
|
num_enum::FromPrimitive::from_primitive(self.packed_ref_count.slot_count_enum());
|
||||||
0 => OccupiedEnum::Free,
|
match enum_tag {
|
||||||
1 => OccupiedEnum::ZeroSlots,
|
OccupiedEnumTag::Free => OccupiedEnum::Free,
|
||||||
2 => OccupiedEnum::OneSlotInIndex(&self.contents.single_element),
|
OccupiedEnumTag::ZeroSlots => OccupiedEnum::ZeroSlots,
|
||||||
3 => OccupiedEnum::MultipleSlots(&self.contents.multiple_slots),
|
OccupiedEnumTag::OneSlotInIndex => unsafe {
|
||||||
_ => {
|
OccupiedEnum::OneSlotInIndex(&self.contents.single_element)
|
||||||
panic!("unexpected value");
|
},
|
||||||
}
|
OccupiedEnumTag::MultipleSlots => unsafe {
|
||||||
}
|
OccupiedEnum::MultipleSlots(&self.contents.multiple_slots)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -222,18 +236,19 @@ impl<T: Copy> IndexEntry<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn set_slot_count_enum_value<'a>(&'a mut self, value: OccupiedEnum<'a, T>) {
|
pub(crate) fn set_slot_count_enum_value<'a>(&'a mut self, value: OccupiedEnum<'a, T>) {
|
||||||
self.packed_ref_count.set_slot_count_enum(match value {
|
let enum_tag = match value {
|
||||||
OccupiedEnum::Free => 0,
|
OccupiedEnum::Free => OccupiedEnumTag::Free,
|
||||||
OccupiedEnum::ZeroSlots => 1,
|
OccupiedEnum::ZeroSlots => OccupiedEnumTag::ZeroSlots,
|
||||||
OccupiedEnum::OneSlotInIndex(single_element) => {
|
OccupiedEnum::OneSlotInIndex(single_element) => {
|
||||||
self.contents.single_element = *single_element;
|
self.contents.single_element = *single_element;
|
||||||
2
|
OccupiedEnumTag::OneSlotInIndex
|
||||||
}
|
}
|
||||||
OccupiedEnum::MultipleSlots(multiple_slots) => {
|
OccupiedEnum::MultipleSlots(multiple_slots) => {
|
||||||
self.contents.multiple_slots = *multiple_slots;
|
self.contents.multiple_slots = *multiple_slots;
|
||||||
3
|
OccupiedEnumTag::MultipleSlots
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
self.packed_ref_count.set_slot_count_enum(enum_tag as u8);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4470,6 +4470,7 @@ dependencies = [
|
||||||
"log",
|
"log",
|
||||||
"memmap2",
|
"memmap2",
|
||||||
"modular-bitfield",
|
"modular-bitfield",
|
||||||
|
"num_enum",
|
||||||
"rand 0.7.3",
|
"rand 0.7.3",
|
||||||
"solana-measure",
|
"solana-measure",
|
||||||
"solana-sdk 1.16.0",
|
"solana-sdk 1.16.0",
|
||||||
|
|
Loading…
Reference in New Issue