Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Leshiy committed Apr 9, 2024
1 parent e1764d3 commit 2b5b005
Showing 1 changed file with 37 additions and 24 deletions.
61 changes: 37 additions & 24 deletions catalyst-gateway/bin/src/event_db/follower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,39 @@ const BLOCK_TIME_COLUMN: &str = "block_time";
/// `ended` column name
const ENDED_COLUMN: &str = "ended";

/// `insert_slot_index.sql`
const INSERT_SLOT_INDEX_SQL: &str =
include_str!("../../../event-db/queries/follower/insert_slot_index.sql");
/// `previous_slot_info.sql`
const PREVIOUS_SLOT_INFO_SQL: &str = include_str!(
"../../../event-db/queries/follower/select_slot_index_by_datetime/previous_slot_info.sql"
);
/// `current_slot_info.sql`
const CURRENT_SLOT_INFO_SQL: &str = include_str!(
"../../../event-db/queries/follower/select_slot_index_by_datetime/current_slot_info.sql"
);
/// `next_slot_info.sql`
const NEXT_SLOT_INFO_SQL: &str = include_str!(
"../../../event-db/queries/follower/select_slot_index_by_datetime/next_slot_info.sql"
);

/// Query type
pub(crate) enum SlotInfoQueryType {
Previous,
Current,
Next,
}

impl SlotInfoQueryType {
fn get_sql_query(&self) -> &'static str {
match self {
SlotInfoQueryType::Previous => PREVIOUS_SLOT_INFO_SQL,
SlotInfoQueryType::Current => CURRENT_SLOT_INFO_SQL,
SlotInfoQueryType::Next => NEXT_SLOT_INFO_SQL,
}
}
}

impl EventDB {
/// Index follower block stream
pub(crate) async fn index_follower_data(
Expand All @@ -40,16 +66,13 @@ impl EventDB {
let conn = self.pool.get().await?;

let _rows = conn
.query(
include_str!("../../../event-db/queries/follower/insert_slot_index.sql"),
&[
&slot_no,
&network.to_string(),
&epoch_no,
&block_time,
&hex::decode(block_hash).map_err(|e| Error::DecodeHex(e.to_string()))?,
],
)
.query(INSERT_SLOT_INDEX_SQL, &[
&slot_no,
&network.to_string(),
&epoch_no,
&block_time,
&hex::decode(block_hash).map_err(|e| Error::DecodeHex(e.to_string()))?,
])
.await?;

Ok(())
Expand All @@ -62,20 +85,10 @@ impl EventDB {
let conn = self.pool.get().await?;

let rows = conn
.query(
match query_type {
SlotInfoQueryType::Previous => include_str!(
"../../../event-db/queries/follower/select_slot_index_by_datetime/previous_slot_info.sql"
),
SlotInfoQueryType::Current => include_str!(
"../../../event-db/queries/follower/select_slot_index_by_datetime/current_slot_info.sql"
),
SlotInfoQueryType::Next => include_str!(
"../../../event-db/queries/follower/select_slot_index_by_datetime/next_slot_info.sql"
),
},
&[&network.to_string(), &date_time],
)
.query(query_type.get_sql_query(), &[
&network.to_string(),
&date_time,
])
.await?;

let Some(row) = rows.first() else {
Expand Down

0 comments on commit 2b5b005

Please sign in to comment.