-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #680 from blockscout/lymarenkolev/bens/address-nam…
…es-view Add materialized view for address name resolving
- Loading branch information
Showing
11 changed files
with
273 additions
and
21 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
blockscout-ens/bens-logic/src/subgraphs_reader/sql/address_names.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use crate::subgraphs_reader::SubgraphReadError; | ||
use sqlx::{postgres::PgPool, Executor}; | ||
use tracing::instrument; | ||
|
||
use super::{DOMAIN_DEFAULT_WHERE_CLAUSE, DOMAIN_NOT_EXPIRED_WHERE_CLAUSE}; | ||
|
||
#[instrument( | ||
name = "create_address_names_view", | ||
skip(pool), | ||
err(level = "error"), | ||
level = "info" | ||
)] | ||
pub async fn create_address_names_view( | ||
pool: &PgPool, | ||
schema: &str, | ||
) -> Result<(), SubgraphReadError> { | ||
let mut tx = pool.begin().await?; | ||
|
||
tx.execute(sqlx::query(&format!( | ||
r#" | ||
CREATE MATERIALIZED VIEW IF NOT EXISTS {schema}.address_names AS | ||
SELECT DISTINCT ON (resolved_address) | ||
id, | ||
name AS domain_name, | ||
resolved_address | ||
from {schema}.domain | ||
where | ||
resolved_address IS NOT NULL | ||
AND name NOT LIKE '%[%' | ||
AND {DOMAIN_DEFAULT_WHERE_CLAUSE} | ||
AND {DOMAIN_NOT_EXPIRED_WHERE_CLAUSE} | ||
ORDER BY resolved_address, created_at | ||
"#, | ||
))) | ||
.await?; | ||
|
||
tx.execute(sqlx::query(&format!( | ||
r#" | ||
CREATE UNIQUE INDEX IF NOT EXISTS | ||
address_names_unique_resolved_address | ||
ON {schema}.address_names (resolved_address); | ||
"# | ||
))) | ||
.await?; | ||
|
||
let function_name = refresh_function_name(schema); | ||
tx.execute(sqlx::query(&format!( | ||
r#" | ||
CREATE OR REPLACE FUNCTION {function_name} | ||
RETURNS void AS | ||
$$ | ||
BEGIN | ||
REFRESH MATERIALIZED VIEW CONCURRENTLY {schema}.address_names; | ||
END; | ||
$$ | ||
LANGUAGE plpgsql; | ||
"# | ||
))) | ||
.await?; | ||
|
||
tx.commit().await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[instrument( | ||
name = "refresh_address_names_view", | ||
skip(pool), | ||
err(level = "error"), | ||
level = "info" | ||
)] | ||
pub async fn refresh_address_names_view( | ||
pool: &PgPool, | ||
schema: &str, | ||
) -> Result<(), SubgraphReadError> { | ||
let function_name = refresh_function_name(schema); | ||
sqlx::query(&format!("SELECT {function_name};")) | ||
.execute(pool) | ||
.await?; | ||
Ok(()) | ||
} | ||
|
||
fn refresh_function_name(schema: &str) -> String { | ||
format!("{schema}_refresh_address_names()") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
mod address_names; | ||
mod domain; | ||
mod transaction_history; | ||
|
||
pub use address_names::*; | ||
pub use domain::*; | ||
pub use transaction_history::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.