use crate::exec::write_stmt::{ExecutionContext, StatementExecutor};
use crate::exec::ExecutionError;
use crate::storage::GraphCache;
use crate::txn::UndoOperation;
pub trait DataStatementExecutor: StatementExecutor {
fn execute_modification(
&self,
graph: &mut GraphCache,
context: &mut ExecutionContext,
) -> Result<(UndoOperation, usize), ExecutionError>;
fn execute(
&self,
context: &mut ExecutionContext,
storage: &crate::storage::StorageManager,
) -> Result<(String, usize), ExecutionError> {
self.pre_execute(context)?;
let graph_name = context.get_graph_name()?;
let rows_affected = self.execute_unified_flow(context, &graph_name, storage)?;
self.post_execute(context, rows_affected)?;
let message = self.operation_description(context);
Ok((message, rows_affected))
}
fn execute_unified_flow(
&self,
context: &mut ExecutionContext,
graph_name: &str,
storage: &crate::storage::StorageManager,
) -> Result<usize, ExecutionError> {
use std::sync::{Arc, Mutex};
log::debug!("UNIFIED_FLOW: Starting for graph '{}'", graph_name);
let rows_affected = Arc::new(Mutex::new(0usize));
let rows_affected_clone = rows_affected.clone();
let description = self.operation_description(context);
log::debug!(
"WAL: Logged {} operation for graph '{}'",
description,
graph_name
);
log::debug!("UNIFIED_FLOW: Getting graph '{}' from storage", graph_name);
let mut graph = storage
.get_graph(graph_name)
.map_err(|e| {
log::error!("UNIFIED_FLOW: Failed to get graph: {}", e);
ExecutionError::StorageError(format!("Failed to get graph: {}", e))
})?
.ok_or_else(|| {
log::error!("UNIFIED_FLOW: Graph '{}' not found", graph_name);
ExecutionError::StorageError(format!("Graph not found: {}", graph_name))
})?;
log::debug!(
"UNIFIED_FLOW: Got graph with {} nodes",
graph.node_count().unwrap_or(0)
);
let (undo_op, affected) = self.execute_modification(&mut graph, context)?;
*rows_affected_clone.lock().unwrap() = affected;
log::debug!("Executed modification for graph '{}'", graph_name);
context.log_transaction_operation(undo_op)?;
let normalized_graph_name = graph_name.to_string();
storage
.save_graph(&normalized_graph_name, graph)
.map_err(|e| {
ExecutionError::StorageError(format!("Failed to update in-memory graph: {}", e))
})?;
log::debug!(
"MEMORY: Updated in-memory graph '{}' after persistence",
graph_name
);
let affected = *rows_affected.lock().unwrap();
Ok(affected)
}
}
#[derive(Debug, Clone, Copy)]
#[allow(dead_code)] pub enum DataStatementType {
Insert,
Set,
Delete,
Remove,
MatchInsert,
}