Skip to content

Commit 0a1dd0e

Browse files
yancoutofacebook-github-bot
authored andcommitted
Use Vec1 for list of sharded sql connections
Summary: We always have at least one connection, so let's guarantee it on compile time. Some places did manual checks on runtime, now that's not necessary anymore! Reviewed By: liubov-dmitrieva Differential Revision: D40637483 fbshipit-source-id: 057f6464a016b6c2fa196e6b83db6e7cbc05756d
1 parent 5e12d8a commit 0a1dd0e

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

shed/sql/common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ stats = { version = "0.1.0", path = "../../stats" }
2626
thiserror = "1.0.36"
2727
time_ext = { version = "0.1.0", path = "../../time_ext" }
2828
tokio_shim = { version = "0.1.0", path = "../../tokio_shim" }
29+
vec1 = { version = "1", features = ["serde"] }
2930

3031
[dev-dependencies]
3132
sql = { version = "0.1.0", path = ".." }

shed/sql/common/lib.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use anyhow::bail;
2626
use anyhow::format_err;
2727
use anyhow::Context;
2828
use anyhow::Error;
29+
use vec1::Vec1;
2930

3031
// Used in docs
3132
#[cfg(test)]
@@ -114,35 +115,32 @@ impl From<SqlConnectionsWithSchema> for SqlConnections {
114115
#[derive(Clone)]
115116
pub struct SqlShardedConnections {
116117
/// Write connections to the master for each shard
117-
pub write_connections: Vec<Connection>,
118+
pub write_connections: Vec1<Connection>,
118119
/// Read connections for each shard
119-
pub read_connections: Vec<Connection>,
120+
pub read_connections: Vec1<Connection>,
120121
/// Read master connections for each shard
121-
pub read_master_connections: Vec<Connection>,
122+
pub read_master_connections: Vec1<Connection>,
122123
}
123124

124-
impl SqlShardedConnections {
125-
/// Check if the struct is empty.
126-
pub fn is_empty(&self) -> bool {
127-
self.write_connections.is_empty()
128-
}
129-
}
130-
131-
impl From<Vec<SqlConnections>> for SqlShardedConnections {
132-
fn from(shard_connections: Vec<SqlConnections>) -> Self {
133-
let mut write_connections = Vec::with_capacity(shard_connections.len());
134-
let mut read_connections = Vec::with_capacity(shard_connections.len());
135-
let mut read_master_connections = Vec::with_capacity(shard_connections.len());
136-
for connections in shard_connections.into_iter() {
125+
impl From<Vec1<SqlConnections>> for SqlShardedConnections {
126+
fn from(shard_connections: Vec1<SqlConnections>) -> Self {
127+
let (head, last) = shard_connections.split_off_last();
128+
let mut write_connections = Vec::with_capacity(head.len());
129+
let mut read_connections = Vec::with_capacity(head.len());
130+
let mut read_master_connections = Vec::with_capacity(head.len());
131+
for connections in head {
137132
write_connections.push(connections.write_connection);
138133
read_connections.push(connections.read_connection);
139134
read_master_connections.push(connections.read_master_connection);
140135
}
141136

142137
Self {
143-
read_connections,
144-
read_master_connections,
145-
write_connections,
138+
read_connections: Vec1::from_vec_push(read_connections, last.read_connection),
139+
read_master_connections: Vec1::from_vec_push(
140+
read_master_connections,
141+
last.read_master_connection,
142+
),
143+
write_connections: Vec1::from_vec_push(write_connections, last.write_connection),
146144
}
147145
}
148146
}

0 commit comments

Comments
 (0)