|
| 1 | +//! Chain Root For Role0 Key (RBAC 509 registrations) Queries used in purging data. |
| 2 | +use std::{fmt::Debug, sync::Arc}; |
| 3 | + |
| 4 | +use scylla::{ |
| 5 | + prepared_statement::PreparedStatement, transport::iterator::TypedRowIterator, SerializeRow, |
| 6 | + Session, |
| 7 | +}; |
| 8 | +use tracing::error; |
| 9 | + |
| 10 | +use crate::{ |
| 11 | + db::index::{ |
| 12 | + queries::{ |
| 13 | + purge::{PreparedDeleteQuery, PreparedQueries, PreparedSelectQuery}, |
| 14 | + FallibleQueryResults, SizedBatch, |
| 15 | + }, |
| 16 | + session::CassandraSession, |
| 17 | + }, |
| 18 | + settings::cassandra_db, |
| 19 | +}; |
| 20 | + |
| 21 | +pub(crate) mod result { |
| 22 | + //! Return values for Chain Root For Role0 Key registration purge queries. |
| 23 | +
|
| 24 | + /// Primary Key Row |
| 25 | + pub(crate) type PrimaryKey = (Vec<u8>, num_bigint::BigInt, i16); |
| 26 | +} |
| 27 | + |
| 28 | +/// Select primary keys for Chain Root For Role0 Key registration. |
| 29 | +const SELECT_QUERY: &str = include_str!("./cql/get_chain_root_for_role0_key.cql"); |
| 30 | + |
| 31 | +/// Primary Key Value. |
| 32 | +#[derive(SerializeRow)] |
| 33 | +pub(crate) struct Params { |
| 34 | + /// Role0 Key - Binary 16 bytes. |
| 35 | + pub(crate) role0_key: Vec<u8>, |
| 36 | + /// Block Slot Number |
| 37 | + pub(crate) slot_no: num_bigint::BigInt, |
| 38 | + /// Transaction Offset inside the block. |
| 39 | + pub(crate) txn: i16, |
| 40 | +} |
| 41 | + |
| 42 | +impl Debug for Params { |
| 43 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 44 | + f.debug_struct("Params") |
| 45 | + .field("role0_key", &self.role0_key) |
| 46 | + .field("slot_no", &self.slot_no) |
| 47 | + .field("txn", &self.txn) |
| 48 | + .finish() |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl From<result::PrimaryKey> for Params { |
| 53 | + fn from(value: result::PrimaryKey) -> Self { |
| 54 | + Self { |
| 55 | + role0_key: value.0, |
| 56 | + slot_no: value.1, |
| 57 | + txn: value.2, |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | +/// Get primary key for Chain Root For Role0 Key registration query. |
| 62 | +pub(crate) struct PrimaryKeyQuery; |
| 63 | + |
| 64 | +impl PrimaryKeyQuery { |
| 65 | + /// Prepares a query to get all Chain Root For Role0 Key registration primary keys. |
| 66 | + pub(crate) async fn prepare(session: &Arc<Session>) -> anyhow::Result<PreparedStatement> { |
| 67 | + let select_primary_key = PreparedQueries::prepare( |
| 68 | + session.clone(), |
| 69 | + SELECT_QUERY, |
| 70 | + scylla::statement::Consistency::All, |
| 71 | + true, |
| 72 | + ) |
| 73 | + .await; |
| 74 | + |
| 75 | + if let Err(ref error) = select_primary_key { |
| 76 | + error!(error=%error, "Failed to prepare get Chain Root For Role0 Key registration primary key query"); |
| 77 | + }; |
| 78 | + |
| 79 | + select_primary_key |
| 80 | + } |
| 81 | + |
| 82 | + /// Executes a query to get all Chain Root For Role0 Key registration primary keys. |
| 83 | + pub(crate) async fn execute( |
| 84 | + session: &CassandraSession, |
| 85 | + ) -> anyhow::Result<TypedRowIterator<result::PrimaryKey>> { |
| 86 | + let iter = session |
| 87 | + .purge_execute_iter(PreparedSelectQuery::ChainRootForRole0Key) |
| 88 | + .await? |
| 89 | + .into_typed::<result::PrimaryKey>(); |
| 90 | + |
| 91 | + Ok(iter) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/// Delete Chain Root For Role0 Key registration |
| 96 | +const DELETE_QUERY: &str = include_str!("./cql/delete_chain_root_for_role0_key.cql"); |
| 97 | + |
| 98 | +/// Delete Chain Root For Role0 Key registration Query |
| 99 | +pub(crate) struct DeleteQuery; |
| 100 | + |
| 101 | +impl DeleteQuery { |
| 102 | + /// Prepare Batch of Delete Queries |
| 103 | + pub(crate) async fn prepare_batch( |
| 104 | + session: &Arc<Session>, cfg: &cassandra_db::EnvVars, |
| 105 | + ) -> anyhow::Result<SizedBatch> { |
| 106 | + let delete_queries = PreparedQueries::prepare_batch( |
| 107 | + session.clone(), |
| 108 | + DELETE_QUERY, |
| 109 | + cfg, |
| 110 | + scylla::statement::Consistency::Any, |
| 111 | + true, |
| 112 | + false, |
| 113 | + ) |
| 114 | + .await?; |
| 115 | + Ok(delete_queries) |
| 116 | + } |
| 117 | + |
| 118 | + /// Executes a DELETE Query |
| 119 | + pub(crate) async fn execute( |
| 120 | + session: &CassandraSession, params: Vec<Params>, |
| 121 | + ) -> FallibleQueryResults { |
| 122 | + let results = session |
| 123 | + .purge_execute_batch(PreparedDeleteQuery::ChainRootForRole0Key, params) |
| 124 | + .await?; |
| 125 | + |
| 126 | + Ok(results) |
| 127 | + } |
| 128 | +} |
0 commit comments