Skip to content

Commit

Permalink
add prepare_sql_params_list fn
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Leshiy committed May 7, 2024
1 parent 48f6c65 commit ef6dd8e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 69 deletions.
18 changes: 2 additions & 16 deletions catalyst-gateway/bin/src/event_db/cardano/chain_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use handlebars::Handlebars;
use pallas::ledger::traverse::{wellknown::GenesisValues, MultiEraBlock};
use tracing::error;

use crate::event_db::{error::NotFoundError, EventDB};
use crate::event_db::{error::NotFoundError, utils::prepare_sql_params_list, EventDB};

/// Block time
pub type DateTime = chrono::DateTime<chrono::offset::Utc>;
Expand Down Expand Up @@ -152,21 +152,7 @@ impl EventDB {
let chunk_size = (i16::MAX / 5) as usize;
for chunk in values.chunks(chunk_size) {
// Build query VALUES statements
let mut values_strings = Vec::with_capacity(chunk.len());
let mut i = 1;

for _ in chunk {
values_strings.push(format!(
"(${},${},${},${},${})",
i,
i + 1,
i + 2,
i + 3,
i + 4,
));

i += 5;
}
let values_strings = prepare_sql_params_list(5, chunk.len());

let query = format!("INSERT INTO cardano_slot_index (slot_no, network, epoch_no, block_time, block_hash) VALUES {} ON CONFLICT DO NOTHING", values_strings.join(","));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use crate::{
cip36_registration::{Cip36Metadata, VotingInfo},
util::valid_era,
},
event_db::{cardano::chain_state::SlotNumber, error::NotFoundError, EventDB},
event_db::{
cardano::chain_state::SlotNumber, error::NotFoundError, utils::prepare_sql_params_list,
EventDB,
},
};

/// Transaction id
Expand Down Expand Up @@ -150,24 +153,7 @@ impl EventDB {
let chunk_size = (i16::MAX / 8) as usize;
for chunk in values.chunks(chunk_size) {
// Build query VALUES statements
let mut values_strings = Vec::with_capacity(chunk.len());
let mut i = 1;

for _ in chunk {
values_strings.push(format!(
"(${},${},${},${},${},${},${},${})",
i,
i + 1,
i + 2,
i + 3,
i + 4,
i + 5,
i + 6,
i + 7,
));

i += 8;
}
let values_strings = prepare_sql_params_list(8, chunk.len());

let query = format!(
r#"INSERT INTO cardano_voter_registration (tx_id, stake_credential, public_voting_key, payment_address, nonce, metadata_cip36, stats, valid) VALUES {}
Expand Down
38 changes: 4 additions & 34 deletions catalyst-gateway/bin/src/event_db/cardano/utxo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tracing::error;
use super::{chain_state::SlotNumber, cip36_registration::StakeCredential};
use crate::{
cardano::util::parse_policy_assets,
event_db::{error::NotFoundError, EventDB},
event_db::{error::NotFoundError, utils::prepare_sql_params_list, EventDB},
};

/// Stake amount.
Expand Down Expand Up @@ -136,20 +136,7 @@ impl EventDB {
// Postgres has a limit of i16::MAX parameters a query can have.
let chunk_size = (i16::MAX / 5) as usize;
for chunk in values.chunks(chunk_size) {
let mut values_strings = Vec::with_capacity(chunk.len());
let mut i = 1;

for _ in chunk {
values_strings.push(format!(
"(${},${},${},${},${})",
i,
i + 1,
i + 2,
i + 3,
i + 4
));
i += 5;
}
let values_strings = prepare_sql_params_list(5, chunk.len());

let query = format!(
"INSERT INTO cardano_utxo (tx_id, index, asset, stake_credential, value) VALUES {} ON CONFLICT (index, tx_id) DO NOTHING",
Expand Down Expand Up @@ -190,18 +177,7 @@ impl EventDB {
// Postgres has a limit of i16::MAX parameters a query can have.
let chunk_size = (i16::MAX / 3) as usize;
for chunk in values.chunks(chunk_size) {
let mut values_strings = Vec::with_capacity(chunk.len());
let mut i = 1;

for _ in chunk {
values_strings.push(format!(
"(${}::bytea,${}::bytea,${}::integer)",
i,
i + 1,
i + 2
));
i += 3;
}
let values_strings = prepare_sql_params_list(3, chunk.len());

let query = format!(
"UPDATE cardano_utxo AS c SET spent_tx_id = v.tx_id FROM (VALUES {}) AS v(tx_id, output_hash, index) WHERE v.index = c.index AND v.output_hash = c.tx_id",
Expand Down Expand Up @@ -241,13 +217,7 @@ impl EventDB {
let chunk_size = (i16::MAX / 3) as usize;
for chunk in values.chunks(chunk_size) {
// Build query VALUES statements
let mut values_strings = Vec::with_capacity(chunk.len());
let mut i = 1;

for _ in chunk {
values_strings.push(format!("(${},${},${})", i, i + 1, i + 2));
i += 3;
}
let values_strings = prepare_sql_params_list(3, chunk.len());

let query = format!(
"INSERT INTO cardano_txn_index (id, slot_no, network) VALUES {} ON CONFLICT (id) DO NOTHING",
Expand Down
1 change: 1 addition & 0 deletions catalyst-gateway/bin/src/event_db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub(crate) mod cardano;
pub(crate) mod error;
pub(crate) mod legacy;
pub(crate) mod schema_check;
pub(crate) mod utils;

/// Database URL Environment Variable name.
/// eg: "`postgres://catalyst-dev:CHANGE_ME@localhost/CatalystDev`"
Expand Down
15 changes: 15 additions & 0 deletions catalyst-gateway/bin/src/event_db/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! Utility functions for the event db operations

/// Prepare SQL parameters list from the provided list size and number of parameters.
/// Output format: `[($1, $2, $3), ($4, $5, $6)]`
pub(crate) fn prepare_sql_params_list(params_num: usize, list_size: usize) -> Vec<String> {
(0..list_size)
.map(|row| {
let placeholders: String = (0..params_num)
.map(|i| format!("${}", row * params_num + i + 1))
.collect::<Vec<_>>()
.join(",");
format!("({placeholders})")
})
.collect()
}

0 comments on commit ef6dd8e

Please sign in to comment.