Skip to content

Commit 2b92d27

Browse files
wprzytulamuzarski
authored andcommitted
sharding: refactor ShardInfo try_from options
This makes the code more rusty and leverages the type system. Also, it extracts the constants for shared use in the next commit.
1 parent bb824b6 commit 2b92d27

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

scylla/src/routing/sharding.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,25 +107,37 @@ pub enum ShardingError {
107107
ParseIntError(#[from] std::num::ParseIntError),
108108
}
109109

110+
const SHARD_ENTRY: &str = "SCYLLA_SHARD";
111+
const NR_SHARDS_ENTRY: &str = "SCYLLA_NR_SHARDS";
112+
const MSB_IGNORE_ENTRY: &str = "SCYLLA_SHARDING_IGNORE_MSB";
113+
110114
impl<'a> TryFrom<&'a HashMap<String, Vec<String>>> for ShardInfo {
111115
type Error = ShardingError;
112116
fn try_from(options: &'a HashMap<String, Vec<String>>) -> Result<Self, Self::Error> {
113-
let shard_entry = options.get("SCYLLA_SHARD");
114-
let nr_shards_entry = options.get("SCYLLA_NR_SHARDS");
115-
let msb_ignore_entry = options.get("SCYLLA_SHARDING_IGNORE_MSB");
116-
if shard_entry.is_none() || nr_shards_entry.is_none() || msb_ignore_entry.is_none() {
117+
let shard_entry = options.get(SHARD_ENTRY);
118+
let nr_shards_entry = options.get(NR_SHARDS_ENTRY);
119+
let msb_ignore_entry = options.get(MSB_IGNORE_ENTRY);
120+
121+
// Unwrap entries.
122+
let (Some(shard_entry), Some(nr_shards_entry), Some(msb_ignore_entry)) =
123+
(shard_entry, nr_shards_entry, msb_ignore_entry)
124+
else {
117125
return Err(ShardingError::MissingShardInfoParameter);
118-
}
119-
if shard_entry.unwrap().is_empty()
120-
|| nr_shards_entry.unwrap().is_empty()
121-
|| msb_ignore_entry.unwrap().is_empty()
122-
{
126+
};
127+
128+
// Further unwrap entries (they should be the first entries of their corresponding Vecs).
129+
let (Some(shard_entry), Some(nr_shards_entry), Some(msb_ignore_entry)) = (
130+
shard_entry.first(),
131+
nr_shards_entry.first(),
132+
msb_ignore_entry.first(),
133+
) else {
123134
return Err(ShardingError::MissingUnwrapedShardInfoParameter);
124-
}
125-
let shard = shard_entry.unwrap().first().unwrap().parse::<u16>()?;
126-
let nr_shards = nr_shards_entry.unwrap().first().unwrap().parse::<u16>()?;
135+
};
136+
137+
let shard = shard_entry.parse::<u16>()?;
138+
let nr_shards = nr_shards_entry.parse::<u16>()?;
127139
let nr_shards = ShardCount::new(nr_shards).ok_or(ShardingError::ZeroShards)?;
128-
let msb_ignore = msb_ignore_entry.unwrap().first().unwrap().parse::<u8>()?;
140+
let msb_ignore = msb_ignore_entry.parse::<u8>()?;
129141
Ok(ShardInfo::new(shard, nr_shards, msb_ignore))
130142
}
131143
}

0 commit comments

Comments
 (0)