feltdb 0.6.1

Embedded durable state store: an append-only log with atomic multi-record transactions, version-conditional writes, and idempotent retries
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/// Transaction API Layer
///
/// Public-facing transaction management API that integrates:
/// - TransactionExecutor for execution logic
/// - OperationLog for operation persistence
/// - StateTransitionStore for transition durability
/// - Precondition validation
/// - Read/write dependency tracking
use crate::convergence::VectorClock;
use crate::operation_log::{MemoryOperationLog, OperationLogEntry};
use crate::state_transition_store::{MemoryStateTransitionStore, StateTransitionRecord};
use crate::transactions::*;
use std::sync::{Arc, Mutex};

/// Result type for transaction API operations
pub type TxResult<T> = Result<T, TxError>;

/// Transaction API errors
#[derive(Clone, Debug)]
pub enum TxError {
    PreconditionFailed(String),
    ConflictDetected(String),
    OperationDeduplication(String),
    InvalidState(String),
    PersistenceFailure(String),
}

impl std::fmt::Display for TxError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TxError::PreconditionFailed(msg) => write!(f, "Precondition failed: {}", msg),
            TxError::ConflictDetected(msg) => write!(f, "Conflict detected: {}", msg),
            TxError::OperationDeduplication(msg) => {
                write!(f, "Operation already executed: {}", msg)
            }
            TxError::InvalidState(msg) => write!(f, "Invalid state: {}", msg),
            TxError::PersistenceFailure(msg) => write!(f, "Persistence failure: {}", msg),
        }
    }
}

impl std::error::Error for TxError {}

/// Transaction manager combining execution, persistence, and validation
pub struct TransactionManager {
    node_id: String,
    executor: TransactionExecutor,
    operation_log: MemoryOperationLog,
    transition_store: MemoryStateTransitionStore,
    current_version: Arc<Mutex<StateVersion>>,
}

impl TransactionManager {
    /// Create a new transaction manager
    pub fn new(node_id: String, initial_version: StateVersion) -> Self {
        Self {
            node_id: node_id.clone(),
            executor: TransactionExecutor::new(node_id),
            operation_log: MemoryOperationLog::new(),
            transition_store: MemoryStateTransitionStore::new(),
            current_version: Arc::new(Mutex::new(initial_version)),
        }
    }

    /// Get the current state version
    pub fn current_state_version(&self) -> StateVersion {
        self.current_version.lock().unwrap().clone()
    }

    /// Execute a transaction with full persistence integration
    pub fn execute_and_commit(
        &mut self,
        transaction_id: String,
        mut operations: Vec<Operation>,
        consistency_contract: ConsistencyContract,
    ) -> TxResult<StateTransitionRecord> {
        // 1. Validate all operations are new (not duplicated in operation log)
        for op in &operations {
            if self.operation_log.contains_operation_id(&op.operation_id) {
                return Err(TxError::OperationDeduplication(format!(
                    "Operation {} already executed",
                    format!("{}:{}", op.operation_id.node_id, op.operation_id.sequence)
                )));
            }
        }

        // 2. Set transaction_id on all operations
        for op in &mut operations {
            op.transaction_id = Some(transaction_id.clone());
        }

        // 3. Execute transaction
        let current_version = self.current_version.lock().unwrap().clone();
        let transition = self.executor.execute_transaction(
            transaction_id.clone(),
            current_version,
            operations.clone(),
            consistency_contract,
        );

        // 4. Validate transition succeeded
        if !transition.is_successful() {
            return Err(TxError::InvalidState(
                "Transaction execution failed".to_string(),
            ));
        }

        // 5. Persist operations to operation log
        for op in &transition.operations {
            self.operation_log
                .append(op.clone())
                .map_err(|e| TxError::PersistenceFailure(e))?;
        }

        // 6. Persist transition to state transition store
        let store_index = self
            .transition_store
            .record_transition(transition.clone())
            .map_err(|e| TxError::PersistenceFailure(e))?;

        // 7. Update current version
        {
            let mut current = self.current_version.lock().unwrap();
            *current = transition.to_version.clone();
        }

        // 8. Retrieve the recorded transition
        let record = self
            .transition_store
            .get_by_index(store_index)
            .ok_or_else(|| {
                TxError::PersistenceFailure("Failed to retrieve recorded transition".to_string())
            })?;

        Ok(record)
    }

    /// Get a recorded operation by ID
    pub fn get_operation(&self, operation_id: &OperationId) -> Option<OperationLogEntry> {
        self.operation_log.get_by_id(operation_id)
    }

    /// Get a recorded transition by transaction ID
    pub fn get_transition(&self, transaction_id: &str) -> Option<StateTransitionRecord> {
        self.transition_store.get_by_transaction_id(transaction_id)
    }

    /// Get the operation log size
    pub fn operation_log_size(&self) -> u64 {
        self.operation_log.size()
    }

    /// Get the transition store size
    pub fn transition_store_size(&self) -> u64 {
        self.transition_store.size()
    }

    /// Rebuild state from operation log (for recovery)
    /// Executes all operations in order to verify state integrity
    pub fn rebuild_from_operation_log(&mut self) -> TxResult<StateVersion> {
        let all_ops = self.operation_log.iter();

        if all_ops.is_empty() {
            return Ok(self.current_state_version());
        }

        let mut current_version = StateVersion::new(VectorClock::new(), "genesis".to_string());

        for entry in all_ops {
            let transition = self.executor.execute_transaction(
                entry.operation.transaction_id.clone().unwrap_or_default(),
                current_version.clone(),
                vec![entry.operation.clone()],
                ConsistencyContract::local(),
            );

            if !transition.is_successful() {
                return Err(TxError::InvalidState(
                    "Failed to rebuild state from operation log".to_string(),
                ));
            }

            current_version = transition.to_version;
        }

        {
            let mut current = self.current_version.lock().unwrap();
            *current = current_version.clone();
        }

        Ok(current_version)
    }

    /// Get the chain of state transitions from a starting version
    pub fn get_transition_chain_from(&self, from_state_hash: &str) -> Vec<StateTransitionRecord> {
        self.transition_store.get_transition_chain(from_state_hash)
    }

    /// Validate preconditions for an operation
    /// (Simple string matching - production would have actual condition evaluation)
    pub fn validate_preconditions(&self, operation: &Operation) -> TxResult<()> {
        if operation.preconditions.is_empty() {
            return Ok(());
        }

        // Placeholder: In production, would evaluate actual state conditions
        // For now, just verify preconditions are syntactically valid
        for (condition_name, condition) in &operation.preconditions {
            if condition.is_empty() {
                return Err(TxError::PreconditionFailed(format!(
                    "Empty condition: {}",
                    condition_name
                )));
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::transactions::OperationCommand;
    use std::collections::HashMap;

    #[test]
    fn test_create_transaction_manager() {
        let mut vc = VectorClock::new();
        vc.increment("node1");
        let version = StateVersion::new(vc, "hash0".to_string());

        let manager = TransactionManager::new("node1".to_string(), version);

        assert_eq!(manager.operation_log_size(), 0);
        assert_eq!(manager.transition_store_size(), 0);
    }

    #[test]
    fn test_execute_and_commit_single_operation() {
        let mut vc = VectorClock::new();
        vc.increment("node1");
        let version = StateVersion::new(vc, "hash0".to_string());

        let mut manager = TransactionManager::new("node1".to_string(), version);

        let op = Operation::new(
            OperationId::new("node1".to_string(), 1),
            manager.current_state_version(),
            "Update".to_string(),
            OperationCommand {
                op_type: "UpdateField".to_string(),
                collection: "test".to_string(),
                record_id: "rec1".to_string(),
                fields: {
                    let mut m = HashMap::new();
                    m.insert("field".to_string(), serde_json::json!("value"));
                    m
                },
            },
            "user".to_string(),
        );

        let result =
            manager.execute_and_commit("txn1".to_string(), vec![op], ConsistencyContract::local());

        assert!(result.is_ok());
        assert_eq!(manager.operation_log_size(), 1);
        assert_eq!(manager.transition_store_size(), 1);
    }

    #[test]
    fn test_duplicate_operation_rejection() {
        let mut vc = VectorClock::new();
        vc.increment("node1");
        let version = StateVersion::new(vc, "hash0".to_string());

        let mut manager = TransactionManager::new("node1".to_string(), version);

        let op = Operation::new(
            OperationId::new("node1".to_string(), 1),
            manager.current_state_version(),
            "Update".to_string(),
            OperationCommand {
                op_type: "UpdateField".to_string(),
                collection: "test".to_string(),
                record_id: "rec1".to_string(),
                fields: {
                    let mut m = HashMap::new();
                    m.insert("field".to_string(), serde_json::json!("value"));
                    m
                },
            },
            "user".to_string(),
        );

        let _ = manager.execute_and_commit(
            "txn1".to_string(),
            vec![op.clone()],
            ConsistencyContract::local(),
        );

        let result =
            manager.execute_and_commit("txn2".to_string(), vec![op], ConsistencyContract::local());

        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("already executed"));
    }

    #[test]
    fn test_get_operation_after_commit() {
        let mut vc = VectorClock::new();
        vc.increment("node1");
        let version = StateVersion::new(vc, "hash0".to_string());

        let mut manager = TransactionManager::new("node1".to_string(), version);

        let op_id = OperationId::new("node1".to_string(), 1);
        let op = Operation::new(
            op_id.clone(),
            manager.current_state_version(),
            "Update".to_string(),
            OperationCommand {
                op_type: "UpdateField".to_string(),
                collection: "test".to_string(),
                record_id: "rec1".to_string(),
                fields: {
                    let mut m = HashMap::new();
                    m.insert("field".to_string(), serde_json::json!("value"));
                    m
                },
            },
            "user".to_string(),
        );

        let _ =
            manager.execute_and_commit("txn1".to_string(), vec![op], ConsistencyContract::local());

        let retrieved = manager.get_operation(&op_id);
        assert!(retrieved.is_some());
    }

    #[test]
    fn test_get_transition_after_commit() {
        let mut vc = VectorClock::new();
        vc.increment("node1");
        let version = StateVersion::new(vc, "hash0".to_string());

        let mut manager = TransactionManager::new("node1".to_string(), version);

        let op = Operation::new(
            OperationId::new("node1".to_string(), 1),
            manager.current_state_version(),
            "Update".to_string(),
            OperationCommand {
                op_type: "UpdateField".to_string(),
                collection: "test".to_string(),
                record_id: "rec1".to_string(),
                fields: {
                    let mut m = HashMap::new();
                    m.insert("field".to_string(), serde_json::json!("value"));
                    m
                },
            },
            "user".to_string(),
        );

        let _ =
            manager.execute_and_commit("txn1".to_string(), vec![op], ConsistencyContract::local());

        let transition = manager.get_transition("txn1");
        assert!(transition.is_some());
    }

    #[test]
    fn test_precondition_validation() {
        let mut vc = VectorClock::new();
        vc.increment("node1");
        let version = StateVersion::new(vc, "hash0".to_string());

        let manager = TransactionManager::new("node1".to_string(), version);

        let mut op = Operation::new(
            OperationId::new("node1".to_string(), 1),
            manager.current_state_version(),
            "Update".to_string(),
            OperationCommand {
                op_type: "UpdateField".to_string(),
                collection: "test".to_string(),
                record_id: "rec1".to_string(),
                fields: {
                    let mut m = HashMap::new();
                    m.insert("field".to_string(), serde_json::json!("value"));
                    m
                },
            },
            "user".to_string(),
        );

        op = op.with_preconditions(vec![("count".to_string(), "count >= 2".to_string())]);

        let result = manager.validate_preconditions(&op);
        assert!(result.is_ok());

        let mut bad_op = Operation::new(
            OperationId::new("node1".to_string(), 2),
            manager.current_state_version(),
            "Update".to_string(),
            OperationCommand {
                op_type: "UpdateField".to_string(),
                collection: "test".to_string(),
                record_id: "rec2".to_string(),
                fields: {
                    let mut m = HashMap::new();
                    m.insert("field".to_string(), serde_json::json!("value"));
                    m
                },
            },
            "user".to_string(),
        );

        bad_op = bad_op.with_preconditions(vec![("invalid".to_string(), "".to_string())]);

        let bad_result = manager.validate_preconditions(&bad_op);
        assert!(bad_result.is_err());
    }

    #[test]
    fn test_current_state_version_tracking() {
        let mut vc = VectorClock::new();
        vc.increment("node1");
        let initial_version = StateVersion::new(vc, "hash0".to_string());
        let initial_hash = initial_version.state_hash.clone();

        let mut manager = TransactionManager::new("node1".to_string(), initial_version);

        assert_eq!(manager.current_state_version().state_hash, initial_hash);

        let op = Operation::new(
            OperationId::new("node1".to_string(), 1),
            manager.current_state_version(),
            "Update".to_string(),
            OperationCommand {
                op_type: "UpdateField".to_string(),
                collection: "test".to_string(),
                record_id: "rec1".to_string(),
                fields: {
                    let mut m = HashMap::new();
                    m.insert("field".to_string(), serde_json::json!("value"));
                    m
                },
            },
            "user".to_string(),
        );

        let _ =
            manager.execute_and_commit("txn1".to_string(), vec![op], ConsistencyContract::local());

        let new_version = manager.current_state_version();
        assert_ne!(new_version.state_hash, initial_hash);
    }
}