rename num_buckets to max_buckets

This commit is contained in:
Brooks Prumo
2021-09-15 15:41:15 -05:00
committed by Jeff Washington (jwash)
parent 97ba5f2559
commit 9ddfdd3319
2 changed files with 17 additions and 14 deletions

View File

@ -23,15 +23,15 @@ pub struct BucketMapKeyValue<T> {
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct BucketMapConfig { pub struct BucketMapConfig {
pub num_buckets: usize, pub max_buckets: usize,
pub drives: Option<Vec<PathBuf>>, pub drives: Option<Vec<PathBuf>>,
pub max_search: Option<MaxSearch>, pub max_search: Option<MaxSearch>,
} }
impl BucketMapConfig { impl BucketMapConfig {
pub fn new(num_buckets: usize) -> BucketMapConfig { pub fn new(max_buckets: usize) -> BucketMapConfig {
BucketMapConfig { BucketMapConfig {
num_buckets, max_buckets,
..BucketMapConfig::default() ..BucketMapConfig::default()
} }
} }
@ -40,7 +40,7 @@ impl BucketMapConfig {
pub struct BucketMap<T: Clone + Copy + Debug> { pub struct BucketMap<T: Clone + Copy + Debug> {
buckets: Vec<RwLock<Option<Bucket<T>>>>, buckets: Vec<RwLock<Option<Bucket<T>>>>,
drives: Arc<Vec<PathBuf>>, drives: Arc<Vec<PathBuf>>,
num_buckets_pow2: u8, max_buckets_pow2: u8,
max_search: MaxSearch, max_search: MaxSearch,
pub stats: Arc<BucketMapStats>, pub stats: Arc<BucketMapStats>,
pub temp_dir: Option<TempDir>, pub temp_dir: Option<TempDir>,
@ -68,13 +68,16 @@ pub enum BucketMapError {
impl<T: Clone + Copy + Debug> BucketMap<T> { impl<T: Clone + Copy + Debug> BucketMap<T> {
pub fn new(config: BucketMapConfig) -> Self { pub fn new(config: BucketMapConfig) -> Self {
assert_ne!(config.num_buckets, 0, "Number of buckets must be non-zero"); assert_ne!(
assert!( config.max_buckets, 0,
config.num_buckets.is_power_of_two(), "Max number of buckets must be non-zero"
"Number of buckets must be a power of two"
); );
let mut buckets = Vec::with_capacity(config.num_buckets); assert!(
buckets.resize_with(config.num_buckets, || RwLock::new(None)); config.max_buckets.is_power_of_two(),
"Max number of buckets must be a power of two"
);
let mut buckets = Vec::with_capacity(config.max_buckets);
buckets.resize_with(config.max_buckets, || RwLock::new(None));
let stats = Arc::new(BucketMapStats::default()); let stats = Arc::new(BucketMapStats::default());
// this should be <= 1 << DEFAULT_CAPACITY or we end up searching the same items over and over - probably not a big deal since it is so small anyway // this should be <= 1 << DEFAULT_CAPACITY or we end up searching the same items over and over - probably not a big deal since it is so small anyway
const MAX_SEARCH: MaxSearch = 32; const MAX_SEARCH: MaxSearch = 32;
@ -96,7 +99,7 @@ impl<T: Clone + Copy + Debug> BucketMap<T> {
Self { Self {
buckets, buckets,
drives, drives,
num_buckets_pow2: log2(config.num_buckets) as u8, max_buckets_pow2: log2(config.max_buckets) as u8,
stats, stats,
max_search, max_search,
temp_dir, temp_dir,
@ -174,9 +177,9 @@ impl<T: Clone + Copy + Debug> BucketMap<T> {
} }
pub fn bucket_ix(&self, key: &Pubkey) -> usize { pub fn bucket_ix(&self, key: &Pubkey) -> usize {
if self.num_buckets_pow2 > 0 { if self.max_buckets_pow2 > 0 {
let location = read_be_u64(key.as_ref()); let location = read_be_u64(key.as_ref());
(location >> (u64::BITS - self.num_buckets_pow2 as u32)) as usize (location >> (u64::BITS - self.max_buckets_pow2 as u32)) as usize
} else { } else {
0 0
} }

View File

@ -17,7 +17,7 @@ fn bucket_map_test_mt() {
.collect(); .collect();
assert!(!paths.is_empty()); assert!(!paths.is_empty());
let index = BucketMap::new(BucketMapConfig { let index = BucketMap::new(BucketMapConfig {
num_buckets: 1 << 12, max_buckets: 1 << 12,
drives: Some(paths.clone()), drives: Some(paths.clone()),
..BucketMapConfig::default() ..BucketMapConfig::default()
}); });