Skip to content

Commit

Permalink
Manually mplement Hash/Eq in config::Address ignoring stats
Browse files Browse the repository at this point in the history
  • Loading branch information
magec committed Mar 21, 2023
1 parent 7dbbc97 commit 9ab522f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
37 changes: 36 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl PartialEq<Role> for Option<Role> {
}

/// Address identifying a PostgreSQL server uniquely.
#[derive(Clone, PartialEq, Hash, std::cmp::Eq, Debug)]
#[derive(Clone, Debug)]
pub struct Address {
/// Unique ID per addressable Postgres server.
pub id: usize,
Expand Down Expand Up @@ -121,6 +121,41 @@ impl Default for Address {
}
}

// We need to implement PartialEq by ourselves so we skip stats in the comparison
impl PartialEq for Address {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
&& self.host == other.host
&& self.port == other.port
&& self.shard == other.shard
&& self.address_index == other.address_index
&& self.replica_number == other.replica_number
&& self.database == other.database
&& self.role == other.role
&& self.username == other.username
&& self.pool_name == other.pool_name
&& self.mirrors == other.mirrors
}
}
impl Eq for Address {}

// We need to implement Hash by ourselves so we skip stats in the comparison
impl Hash for Address {
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state);
self.host.hash(state);
self.port.hash(state);
self.shard.hash(state);
self.address_index.hash(state);
self.replica_number.hash(state);
self.database.hash(state);
self.role.hash(state);
self.username.hash(state);
self.pool_name.hash(state);
self.mirrors.hash(state);
}
}

impl Address {
/// Address name (aka database) used in `SHOW STATS`, `SHOW DATABASES`, and `SHOW POOLS`.
pub fn name(&self) -> String {
Expand Down
15 changes: 0 additions & 15 deletions src/stats/address.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use log::warn;
use std::hash::Hash;
use std::hash::Hasher;

use std::sync::atomic::*;
use std::sync::Arc;

Expand Down Expand Up @@ -150,15 +147,3 @@ impl AddressStats {
(totals, averages)
}
}

impl PartialEq for AddressStats {
fn eq(&self, _other: &Self) -> bool {
true
}
}

impl Eq for AddressStats {}

impl Hash for AddressStats {
fn hash<H: Hasher>(&self, _state: &mut H) {}
}

0 comments on commit 9ab522f

Please sign in to comment.