Skip to content

Commit 2a02d04

Browse files
authored
chore: add Hash, PartialEq to Schema (#767)
1 parent e183b34 commit 2a02d04

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

pgdog-stats/src/schema.rs

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use indexmap::IndexMap;
22
use serde::{Deserialize, Serialize};
3-
use std::{collections::HashMap, ops::Deref, sync::Arc};
3+
use std::{collections::HashMap, hash::Hash, ops::Deref, sync::Arc};
44

55
/// Schema name -> Table name -> Relation
66
pub type Relations = HashMap<String, HashMap<String, Relation>>;
77

8-
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
8+
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Hash)]
99
pub struct Column {
1010
pub table_catalog: String,
1111
pub table_schema: String,
@@ -18,7 +18,7 @@ pub struct Column {
1818
pub is_primary_key: bool,
1919
}
2020

21-
#[derive(Debug, Clone, Serialize, Deserialize)]
21+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
2222
pub struct Relation {
2323
pub schema: String,
2424
pub name: String,
@@ -32,6 +32,23 @@ pub struct Relation {
3232
pub columns: IndexMap<String, Column>,
3333
}
3434

35+
impl Hash for Relation {
36+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
37+
self.schema.hash(state);
38+
self.name.hash(state);
39+
self.type_.hash(state);
40+
self.owner.hash(state);
41+
self.persistence.hash(state);
42+
self.access_method.hash(state);
43+
self.description.hash(state);
44+
self.oid.hash(state);
45+
for (key, value) in &self.columns {
46+
key.hash(state);
47+
value.hash(state);
48+
}
49+
}
50+
}
51+
3552
impl Relation {
3653
/// Get schema where the table is located.
3754
pub fn schema(&self) -> &str {
@@ -72,18 +89,41 @@ impl Relation {
7289
}
7390
}
7491

75-
#[derive(Debug, Default, Serialize, Deserialize)]
92+
#[derive(Debug, Default, Serialize, Deserialize, PartialEq)]
7693
pub struct SchemaInner {
7794
pub search_path: Vec<String>,
7895
pub relations: Relations,
7996
}
8097

98+
impl Hash for SchemaInner {
99+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
100+
self.search_path.hash(state);
101+
let mut entries: Vec<_> = self.relations.iter().collect();
102+
entries.sort_by_key(|(k, _)| *k);
103+
for (schema, tables) in entries {
104+
schema.hash(state);
105+
let mut table_entries: Vec<_> = tables.iter().collect();
106+
table_entries.sort_by_key(|(k, _)| *k);
107+
for (name, relation) in table_entries {
108+
name.hash(state);
109+
relation.hash(state);
110+
}
111+
}
112+
}
113+
}
114+
81115
/// Load schema from database.
82-
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
116+
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
83117
pub struct Schema {
84118
inner: Arc<SchemaInner>,
85119
}
86120

121+
impl Hash for Schema {
122+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
123+
self.inner.hash(state);
124+
}
125+
}
126+
87127
impl Schema {
88128
pub fn tables(&self) -> &Relations {
89129
&self.relations

pgdog/src/backend/schema/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::net::parameter::ParameterValue;
2020
static SETUP: &str = include_str!("setup.sql");
2121

2222
/// Load schema from database.
23-
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
23+
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Hash)]
2424
pub struct Schema {
2525
inner: StatsSchema,
2626
}

0 commit comments

Comments
 (0)