Skip to content

Commit f501334

Browse files
committed
store: Use dynamic schema in clear_stale_call_cache
1 parent a2fe887 commit f501334

1 file changed

Lines changed: 16 additions & 24 deletions

File tree

store/postgres/src/chain_store.rs

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,11 @@ mod data {
305305
fn contract_address(&self) -> DynColumn<Bytea> {
306306
self.table.column::<Bytea, _>("contract_address")
307307
}
308+
309+
fn accessed_at(&self) -> DynColumn<diesel::sql_types::Date> {
310+
self.table
311+
.column::<diesel::sql_types::Date, _>(Self::ACCESSED_AT)
312+
}
308313
}
309314

310315
#[derive(Clone, Debug)]
@@ -1799,17 +1804,6 @@ mod data {
17991804
call_meta,
18001805
..
18011806
}) => {
1802-
let select_query = format!(
1803-
"WITH stale_contracts AS (
1804-
SELECT contract_address
1805-
FROM {}
1806-
WHERE accessed_at < current_date - interval '{} days'
1807-
LIMIT $1
1808-
)
1809-
SELECT contract_address FROM stale_contracts",
1810-
call_meta.qname, ttl_days
1811-
);
1812-
18131807
let delete_cache_query = format!(
18141808
"WITH targets AS (
18151809
SELECT id
@@ -1827,12 +1821,6 @@ mod data {
18271821
call_meta.qname
18281822
);
18291823

1830-
#[derive(QueryableByName)]
1831-
struct ContractAddress {
1832-
#[diesel(sql_type = Bytea)]
1833-
contract_address: Vec<u8>,
1834-
}
1835-
18361824
loop {
18371825
if let Some(0) = remaining_contracts(total_contracts) {
18381826
info!(
@@ -1848,13 +1836,17 @@ mod data {
18481836
.map(|left| left.min(contracts_batch_size))
18491837
.unwrap_or(contracts_batch_size);
18501838

1851-
let stale_contracts: Vec<Vec<u8>> = sql_query(&select_query)
1852-
.bind::<BigInt, _>(batch_limit)
1853-
.load::<ContractAddress>(conn)
1854-
.await?
1855-
.into_iter()
1856-
.map(|r| r.contract_address)
1857-
.collect();
1839+
let stale_contracts = call_meta
1840+
.table()
1841+
.select(call_meta.contract_address())
1842+
.filter(
1843+
call_meta
1844+
.accessed_at()
1845+
.lt(diesel::dsl::date(diesel::dsl::now - ttl_days.days())),
1846+
)
1847+
.limit(batch_limit)
1848+
.get_results::<Vec<u8>>(conn)
1849+
.await?;
18581850

18591851
if stale_contracts.is_empty() {
18601852
info!(

0 commit comments

Comments
 (0)