Skip to content

cat-gateway index-db CassandraSession cleanup #2637

@Mr-Leshiy

Description

@Mr-Leshiy

Summary

Cleanup the code related to the preparing queries batches and statements for the CassandraSession.
The idea is to move our from storing each prepared query statement or prepared query batch under its own field value, and keeping its own enum variant type and starting to rely on std::any::TypeId of each query type.

Acceptance criteria

  • All integration tests pass test_assets.py, test_rbac.py

Description

  • Inside the CassandraSession structure get rid of the queries: Arc<PreparedQueries>, and purge_queries: Arc<purge::PreparedQueries>, fields. And also get rid of PreparedQueries and purge::PreparedQueries types and all underlying enums.

  • Removed fields need to replace with the following maps prepared_queries: HashMap<TypeId, SizedBatch> (If HashMap will not work because it needs to be Send use SkipMap).

enum PreparedQueryKind {
  Statement(PreparedStatement),
  Batch(SizedBatch)
}


pub(crate) struct CassandraSession {
 ...
prepared_queries: HashMap<TypeId, PreparedQueryKind>
}
  • Add few new traits and correspondingly implement them for each query type like TxiInsertQuery etc.
trait Query: std::fmt::Display {
    fn prepare_query(session: &Session, cfg: &cassandra_db::EnvVars,) -> anyhow::Result<PreparedQueryKind >;
}
  • Add new CassandraSession::prepare_batch methods.
pub(crate) async fn prepare_query<Q: Query>(&self, cfg: &cassandra_db::EnvVars) -> anyhow::Result<()> {
   let prepared_query = Q::prepare_query(self, cfg)?;
   self.prepared_queries.insert(TypeId::of::<Q>(), prepared_query);
   Ok(())
}

So the preparation step which right now spreader under the PreparedQueries::new and purge::PreparedQueries::new methods could be done with the following approach.

fn prepare_queries(session: Arc<Session>, cfg: &cassandra_db::EnvVars,) -> anyhow::Result<()> {
 
}
  • Update CassandraSession::execute_batch, CassandraSession::execute_upsert and CassandraSession ::execute_iter could become (same way execute_upsert and execute_iter but relying on different traits);
pub(crate) async fn execute_batch<T: SerializeRow + Debug, Q: Query >(
        &self, values: Vec<T>,
    ) -> anyhow::Result<FallibleQueryResults> {
        let session = self.session.clone();
        let cfg = self.cfg.clone();
        
       let prepared_query = self.prepared_queries.get(TypeId::of::<Q>()).map_err()?;
       let PreparedQueryKind::Batch(prepared_batch) = prepared_query else {
        anyhow::bail!("Not a batch query");
       }; 

       session_execute_batch(session, prepared_batch , cfg, values).await
}
  • Also get rid of the purge_execute_batch, purge_execute_iter as they are the same as common batch and select queries. And make a proper check that purge queries are executed on the volatile session on the caller side.

Metadata

Metadata

Assignees

Labels

F14-RC2Release Candidate 2 for F14 Tech ReadinessrustPull requests that update Rust codesquad: gatekeepersCatalyst App Backend, System Development & Integration Team

Type

Projects

Status

📋 Backlog

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions