export interface Transaction {
id: string;
operations: Operation[];
status: 'pending' | 'committed' | 'rolled_back';
}
export interface Operation {
type: 'write' | 'update' | 'delete' | 'move';
path: string;
data?: any;
backup?: any;
}
export class TransactionManager {
private transactions: Map<string, Transaction> = new Map();
createTransaction(): string {
const id = Date.now().toString();
this.transactions.set(id, {
id,
operations: [],
status: 'pending'
});
return id;
}
addOperation(transactionId: string, operation: Operation): void {
const transaction = this.transactions.get(transactionId);
if (!transaction) {
throw new Error(`Transaction ${transactionId} not found`);
}
if (transaction.status !== 'pending') {
throw new Error(`Transaction ${transactionId} is not pending`);
}
transaction.operations.push(operation);
}
async commit(transactionId: string): Promise<void> {
const transaction = this.transactions.get(transactionId);
if (!transaction) {
throw new Error(`Transaction ${transactionId} not found`);
}
try {
// Execute all operations
for (const op of transaction.operations) {
await this.executeOperation(op);
}
transaction.status = 'committed';
} catch (error) {
// Rollback on error
await this.rollback(transactionId);
throw error;
}
}
async rollback(transactionId: string): Promise<void> {
const transaction = this.transactions.get(transactionId);
if (!transaction) {
throw new Error(`Transaction ${transactionId} not found`);
}
// Rollback operations in reverse order
for (let i = transaction.operations.length - 1; i >= 0; i--) {
await this.rollbackOperation(transaction.operations[i]);
}
transaction.status = 'rolled_back';
}
private async executeOperation(operation: Operation): Promise<void> {
// Implementation would depend on actual file system operations
// This is a placeholder
console.log(`Executing ${operation.type} on ${operation.path}`);
}
private async rollbackOperation(operation: Operation): Promise<void> {
// Implementation would depend on actual file system operations
// This is a placeholder
console.log(`Rolling back ${operation.type} on ${operation.path}`);
}
}