-
Notifications
You must be signed in to change notification settings - Fork 10
Description
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 thequeries: Arc<PreparedQueries>,
andpurge_queries: Arc<purge::PreparedQueries>,
fields. And also get rid ofPreparedQueries
andpurge::PreparedQueries
types and all underlying enums. -
Removed fields need to replace with the following maps
prepared_queries: HashMap<TypeId, SizedBatch>
(IfHashMap
will not work because it needs to beSend
useSkipMap
).
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
andCassandraSession ::execute_iter
could become (same wayexecute_upsert
andexecute_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
Type
Projects
Status