@@ -13,11 +13,15 @@ use std::sync::atomic::{AtomicU64, AtomicU8, Ordering};
1313use std:: sync:: Arc ;
1414use std:: time:: Duration ;
1515
16+ /// Represents the state of a transaction.
1617#[ derive( Debug , Clone , Copy , PartialEq ) ]
1718#[ repr( u8 ) ]
1819pub enum TransactionState {
20+ /// Transaction is active and can perform operations.
1921 Active = 0 ,
22+ /// Transaction has been committed successfully.
2023 Committed = 1 ,
24+ /// Transaction has been aborted/rolled back.
2125 Aborted = 2 ,
2226}
2327
@@ -32,6 +36,10 @@ impl From<u8> for TransactionState {
3236 }
3337}
3438
39+ /// A database transaction.
40+ ///
41+ /// Transactions provide ACID guarantees for database operations.
42+ /// They are created by the `TransactionManager`.
3543pub struct Transaction {
3644 id : TransactionId ,
3745 // Use atomic instead of RwLock to eliminate lock contention
@@ -46,14 +54,19 @@ impl Transaction {
4654 }
4755 }
4856
57+ /// Returns the unique identifier of the transaction.
4958 pub fn id ( & self ) -> TransactionId {
5059 self . id
5160 }
5261
62+ /// Returns the current state of the transaction.
5363 pub fn state ( & self ) -> TransactionState {
5464 self . state . load ( Ordering :: Acquire ) . into ( )
5565 }
5666
67+ /// Commits the transaction.
68+ ///
69+ /// This makes all changes made by the transaction permanent.
5770 pub fn commit ( & self ) -> Result < ( ) > {
5871 // Use compare-exchange to ensure atomic state transition
5972 match self . state . compare_exchange (
@@ -72,6 +85,9 @@ impl Transaction {
7285 }
7386 }
7487
88+ /// Aborts the transaction.
89+ ///
90+ /// This rolls back all changes made by the transaction.
7591 pub fn abort ( & self ) -> Result < ( ) > {
7692 // Use compare-exchange to ensure atomic state transition
7793 match self . state . compare_exchange (
@@ -91,6 +107,7 @@ impl Transaction {
91107 }
92108}
93109
110+ /// Manages the lifecycle of transactions.
94111pub struct TransactionManager {
95112 next_txn_id : AtomicU64 ,
96113 active_transactions : RwLock < HashMap < TransactionId , Arc < Transaction > > > ,
@@ -104,6 +121,7 @@ impl TransactionManager {
104121 }
105122 }
106123
124+ /// Begins a new transaction.
107125 pub fn begin ( & self ) -> Arc < Transaction > {
108126 let txn_id = self . next_txn_id . fetch_add ( 1 , Ordering :: SeqCst ) ;
109127 let txn = Arc :: new ( Transaction :: new ( txn_id) ) ;
@@ -117,6 +135,7 @@ impl TransactionManager {
117135 txn
118136 }
119137
138+ /// Commits a transaction.
120139 pub fn commit ( & self , txn : & Transaction ) -> Result < ( ) > {
121140 // LOCK ORDERING:
122141 // 1. Commit transaction state (atomic operation - no lock)
@@ -129,6 +148,7 @@ impl TransactionManager {
129148 Ok ( ( ) )
130149 }
131150
151+ /// Aborts a transaction.
132152 pub fn abort ( & self , txn : & Transaction ) -> Result < ( ) > {
133153 // LOCK ORDERING: Same as commit - state first (atomic), then active_transactions
134154 txn. abort ( ) ?;
@@ -138,10 +158,12 @@ impl TransactionManager {
138158 Ok ( ( ) )
139159 }
140160
161+ /// Retrieves an active transaction by ID.
141162 pub fn get_transaction ( & self , txn_id : TransactionId ) -> Option < Arc < Transaction > > {
142163 self . active_transactions . read ( ) . get ( & txn_id) . cloned ( )
143164 }
144165
166+ /// Returns the number of currently active transactions.
145167 pub fn active_count ( & self ) -> usize {
146168 self . active_transactions . read ( ) . len ( )
147169 }
@@ -166,6 +188,7 @@ impl TransactionManager {
166188
167189use dashmap:: DashMap ;
168190
191+ /// Manages locks on database resources (e.g., tables).
169192pub struct LockManager {
170193 // Per-table lock entries using lock-free DashMap
171194 // This eliminates the single global lock bottleneck
@@ -178,9 +201,12 @@ struct LockEntry {
178201 lock_type : LockType ,
179202}
180203
204+ /// Types of locks available.
181205#[ derive( Debug , Clone , Copy , PartialEq ) ]
182206pub enum LockType {
207+ /// Shared lock (allows concurrent readers).
183208 Shared ,
209+ /// Exclusive lock (single writer, no readers).
184210 Exclusive ,
185211}
186212
0 commit comments