keep track of each pending query for the transaction and then apply the queries on commit

This commit is contained in:
pedrocarlo
2025-09-25 18:47:17 -03:00
parent 399f35f73c
commit d3c2198a75
3 changed files with 37 additions and 35 deletions

View File

@@ -394,30 +394,6 @@ pub enum InteractionsType {
Fault(Fault),
}
impl Shadow for Interactions {
type Result = ();
fn shadow(&self, tables: &mut ShadowTablesMut) {
match &self.interactions {
InteractionsType::Property(property) => {
let initial_tables = tables.clone();
for interaction in property.interactions(self.connection_index) {
let res = interaction.shadow(tables);
if res.is_err() {
// If any interaction fails, we reset the tables to the initial state
**tables = initial_tables.clone();
break;
}
}
}
InteractionsType::Query(query) => {
let _ = query.shadow(tables);
}
InteractionsType::Fault(_) => {}
}
}
}
impl Interactions {
pub(crate) fn name(&self) -> Option<&str> {
match &self.interactions {
@@ -674,15 +650,17 @@ impl Shadow for InteractionType {
type Result = anyhow::Result<Vec<Vec<SimValue>>>;
fn shadow(&self, env: &mut ShadowTablesMut) -> Self::Result {
match self {
Self::Query(query) => query.shadow(env),
Self::FsyncQuery(query) => {
let mut first = query.shadow(env)?;
first.extend(query.shadow(env)?);
Ok(first)
}
Self::Assumption(_) | Self::Assertion(_) | Self::Fault(_) | Self::FaultyQuery(_) => {
Ok(vec![])
Self::Query(query) => {
if !query.is_transaction() {
env.add_query(query);
}
query.shadow(env)
}
Self::Assumption(_)
| Self::Assertion(_)
| Self::Fault(_)
| Self::FaultyQuery(_)
| Self::FsyncQuery(_) => Ok(vec![]),
}
}
}

View File

@@ -61,6 +61,14 @@ impl Query {
Query::Begin(..) | Query::Commit(..) | Query::Rollback(..) => vec![],
}
}
#[inline]
pub fn is_transaction(&self) -> bool {
matches!(
self,
Self::Begin(..) | Self::Commit(..) | Self::Rollback(..)
)
}
}
impl Display for Query {

View File

@@ -38,15 +38,15 @@ pub(crate) enum SimulationPhase {
#[derive(Debug, Clone)]
pub struct TransactionTables {
snapshot_tables: Vec<Table>,
current_tables: Vec<Table>,
pending_changes: Vec<Query>,
}
impl TransactionTables {
pub fn new(tables: Vec<Table>) -> Self {
Self {
snapshot_tables: tables.clone(),
current_tables: tables,
pending_changes: Vec::new(),
}
}
}
@@ -104,13 +104,29 @@ where
// TODO: as we do not have concurrent tranasactions yet in the simulator
// there is no conflict we are ignoring conflict problems right now
if let Some(transation_tables) = self.transaction_tables.take() {
*self.commited_tables = transation_tables.current_tables
let mut shadow_table = ShadowTablesMut {
commited_tables: self.commited_tables,
transaction_tables: &mut None,
};
for query in transation_tables.pending_changes {
// TODO: maybe panic on shadow error here
let _ = query.shadow(&mut shadow_table);
}
}
}
pub fn delete_snapshot(&mut self) {
*self.transaction_tables = None;
}
/// Append non transaction queries to the shadow tables
pub fn add_query(&mut self, query: &Query) {
assert!(!query.is_transaction());
if let Some(transaction_tables) = self.transaction_tables {
transaction_tables.pending_changes.push(query.clone());
}
}
}
impl<'a> Deref for ShadowTablesMut<'a> {