Skip to main content

quill_sql/transaction/
lock_guard.rs

1use std::sync::Arc;
2
3use crate::storage::record::RecordId;
4use crate::transaction::{LockManager, TransactionId};
5use crate::utils::table_ref::TableReference;
6
7pub struct RowLockGuard {
8    manager: Arc<LockManager>,
9    txn_id: TransactionId,
10    table: TableReference,
11    rid: RecordId,
12    released: bool,
13}
14
15impl RowLockGuard {
16    pub fn new(
17        manager: Arc<LockManager>,
18        txn_id: TransactionId,
19        table: TableReference,
20        rid: RecordId,
21    ) -> Self {
22        Self {
23            manager,
24            txn_id,
25            table,
26            rid,
27            released: false,
28        }
29    }
30
31    pub fn release(mut self) {
32        self.do_release();
33    }
34
35    fn do_release(&mut self) {
36        if !self.released {
37            let _ = self
38                .manager
39                .unlock_row_raw(self.txn_id, self.table.clone(), self.rid);
40            self.released = true;
41        }
42    }
43}
44
45impl Drop for RowLockGuard {
46    fn drop(&mut self) {
47        self.do_release();
48    }
49}
50
51pub enum TxnReadGuard {
52    Temporary(RowLockGuard),
53}
54
55impl TxnReadGuard {
56    pub fn release(self) {
57        match self {
58            TxnReadGuard::Temporary(guard) => guard.release(),
59        }
60    }
61}