Skip to content

Commit 61eaf64

Browse files
committed
Refactoring
1 parent df82553 commit 61eaf64

File tree

7 files changed

+33
-47
lines changed

7 files changed

+33
-47
lines changed

neo4j/src/driver/home_db_cache.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -226,19 +226,21 @@ impl SessionAuthKey {
226226

227227
impl HomeDbCacheKey {
228228
pub(super) fn new(user: Option<&Arc<String>>, session_auth: Option<&Arc<AuthToken>>) -> Self {
229-
if let Some(user) = user {
230-
HomeDbCacheKey::FixedUser(Arc::clone(user))
231-
} else if let Some(auth) = session_auth {
232-
if let Some(ValueSend::String(scheme)) = auth.data.get("scheme") {
233-
if scheme == "basic" {
234-
if let Some(ValueSend::String(user)) = auth.data.get("principal") {
235-
return HomeDbCacheKey::FixedUser(Arc::new(user.clone()));
236-
}
237-
}
229+
fn get_basic_auth_principal(auth: &AuthToken) -> Option<&str> {
230+
let scheme = auth.data.get("scheme")?.as_string()?.as_str();
231+
if scheme != "basic" {
232+
return None;
238233
}
239-
HomeDbCacheKey::SessionAuth(SessionAuthKey(Arc::clone(auth)))
240-
} else {
241-
HomeDbCacheKey::DriverUser
234+
Some(auth.data.get("principal")?.as_string()?.as_str())
235+
}
236+
237+
match (user, session_auth) {
238+
(Some(user), _) => HomeDbCacheKey::FixedUser(Arc::clone(user)),
239+
(None, Some(auth)) => match get_basic_auth_principal(auth) {
240+
Some(user) => HomeDbCacheKey::FixedUser(Arc::new(user.to_string())),
241+
None => HomeDbCacheKey::SessionAuth(SessionAuthKey(Arc::clone(auth))),
242+
},
243+
(None, None) => HomeDbCacheKey::DriverUser,
242244
}
243245
}
244246
}

neo4j/src/driver/io/bolt.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -523,15 +523,15 @@ trait BoltProtocol: Debug {
523523
#[enum_dispatch(BoltProtocol)]
524524
#[derive(Debug)]
525525
enum BoltProtocolVersion {
526-
V5x8(Bolt5x8<Bolt5x8StructTranslator>),
527-
V5x7(Bolt5x7<Bolt5x7StructTranslator>),
528-
V5x6(Bolt5x6<Bolt5x6StructTranslator>),
529-
V5x4(Bolt5x4<Bolt5x4StructTranslator>),
530-
V5x3(Bolt5x3<Bolt5x3StructTranslator>),
531-
V5x2(Bolt5x2<Bolt5x2StructTranslator>),
532-
V5x1(Bolt5x1<Bolt5x1StructTranslator>),
533-
V5x0(Bolt5x0<Bolt5x0StructTranslator>),
534526
V4x4(Bolt4x4<Bolt4x4StructTranslator>),
527+
V5x0(Bolt5x0<Bolt5x0StructTranslator>),
528+
V5x1(Bolt5x1<Bolt5x1StructTranslator>),
529+
V5x2(Bolt5x2<Bolt5x2StructTranslator>),
530+
V5x3(Bolt5x3<Bolt5x3StructTranslator>),
531+
V5x4(Bolt5x4<Bolt5x4StructTranslator>),
532+
V5x6(Bolt5x6<Bolt5x6StructTranslator>),
533+
V5x7(Bolt5x7<Bolt5x7StructTranslator>),
534+
V5x8(Bolt5x8<Bolt5x8StructTranslator>),
535535
}
536536

537537
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]

neo4j/src/driver/io/bolt/bolt5x0/protocol.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -709,12 +709,7 @@ impl<T: BoltStructTranslator> BoltProtocol for Bolt5x0<T> {
709709

710710
Self::write_mode_entry(log_buf.as_mut(), &mut serializer, &mut dbg_serializer, mode)?;
711711

712-
Self::write_db_entry(
713-
log_buf.as_mut(),
714-
&mut serializer,
715-
&mut dbg_serializer,
716-
db.as_deref().map(String::as_str),
717-
)?;
712+
Self::write_db_entry(log_buf.as_mut(), &mut serializer, &mut dbg_serializer, db)?;
718713

719714
Self::write_imp_user_entry(
720715
log_buf.as_mut(),

neo4j/src/driver/io/bolt/bolt5x2/protocol.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,7 @@ impl<T: BoltStructTranslator> BoltProtocol for Bolt5x2<T> {
300300
mode,
301301
)?;
302302

303-
Bolt5x0::<T>::write_db_entry(
304-
log_buf.as_mut(),
305-
&mut serializer,
306-
&mut dbg_serializer,
307-
db.as_deref().map(String::as_str),
308-
)?;
303+
Bolt5x0::<T>::write_db_entry(log_buf.as_mut(), &mut serializer, &mut dbg_serializer, db)?;
309304

310305
Bolt5x0::<T>::write_imp_user_entry(
311306
log_buf.as_mut(),

neo4j/src/driver/io/bolt/message_parameters.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub(crate) struct RunParameters<'a, KP: Borrow<str> + Debug, KM: Borrow<str> + D
8484
pub(super) tx_timeout: Option<i64>,
8585
pub(super) tx_metadata: Option<&'a HashMap<KM, ValueSend>>,
8686
pub(super) mode: Option<&'a str>,
87-
pub(super) db: Option<Arc<String>>,
87+
pub(super) db: Option<&'a str>,
8888
pub(super) imp_user: Option<&'a str>,
8989
pub(super) notification_filter: Option<&'a NotificationFilter>,
9090
}
@@ -98,7 +98,7 @@ impl<'a, KP: Borrow<str> + Debug, KM: Borrow<str> + Debug> RunParameters<'a, KP,
9898
tx_timeout: Option<i64>,
9999
tx_metadata: Option<&'a HashMap<KM, ValueSend>>,
100100
mode: Option<&'a str>,
101-
db: Option<Arc<String>>,
101+
db: Option<&'a str>,
102102
imp_user: Option<&'a str>,
103103
notification_filter: &'a NotificationFilter,
104104
) -> Self {

neo4j/src/driver/io/pool.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ impl RoutingPool {
503503
) -> Result<(RwLockReadGuard<RoutingTables>, Option<Arc<String>>)> {
504504
let rt_args = args.update_rt_args;
505505
let db_key = rt_args.rt_key();
506-
let db_name = RefCell::new(rt_args.db_name());
506+
let db_name = RefCell::new(rt_args.db_request());
507507
let db_name_ref = &db_name;
508508
let lock = self.routing_tables.maybe_write(
509509
|rts| {
@@ -589,19 +589,13 @@ impl RoutingPool {
589589
Some(args_db) if !args_db.guess => {
590590
let db = Some(Arc::clone(&args_db.db));
591591
new_rt.database.clone_from(&db);
592-
debug!("Storing new routing table for {:?}: {:?}", db, new_rt);
593-
rts.insert(db.as_ref().map(Arc::clone), new_rt);
594-
self.clean_up_pools(rts);
595-
db
596-
}
597-
_ => {
598-
let db = new_rt.database.clone();
599-
debug!("Storing new routing table for {:?}: {:?}", db, new_rt);
600-
rts.insert(db.clone(), new_rt);
601-
self.clean_up_pools(rts);
602592
db
603593
}
594+
_ => new_rt.database.clone(),
604595
};
596+
debug!("Storing new routing table for {:?}: {:?}", db, new_rt);
597+
rts.insert(db.as_ref().map(Arc::clone), new_rt);
598+
self.clean_up_pools(rts);
605599
if let Some(cb) = args.db_resolution_cb {
606600
cb(db.as_ref().map(Arc::clone));
607601
}
@@ -916,7 +910,7 @@ impl UpdateRtArgs<'_> {
916910
})
917911
}
918912

919-
fn db_name(&self) -> Option<Arc<String>> {
913+
fn db_request(&self) -> Option<Arc<String>> {
920914
self.db.as_ref().and_then(|db| match db.guess {
921915
true => None,
922916
false => Some(Arc::clone(&db.db)),

neo4j/src/driver/session.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ impl<'driver> Session<'driver> {
169169
builder.timeout.raw(),
170170
Some(builder.meta.borrow()),
171171
builder.mode.as_protocol_str(),
172-
target_db,
172+
target_db.as_deref().map(String::as_str),
173173
self.config
174174
.config
175175
.as_ref()

0 commit comments

Comments
 (0)