quill_sql/storage/
engine.rs

1use std::sync::Arc;
2
3use crate::{
4    catalog::Catalog,
5    error::QuillSQLResult,
6    storage::{index::btree_index::BPlusTreeIndex, table_heap::TableHeap, tuple::Tuple},
7    transaction::{CommandId, TransactionId},
8    utils::table_ref::TableReference,
9};
10
11pub trait StorageEngine: Send + Sync {
12    fn table_heap(
13        &self,
14        catalog: &Catalog,
15        table: &TableReference,
16    ) -> QuillSQLResult<Arc<TableHeap>>;
17
18    fn table_indexes(
19        &self,
20        catalog: &Catalog,
21        table: &TableReference,
22    ) -> QuillSQLResult<Vec<Arc<BPlusTreeIndex>>>;
23
24    fn mvcc_insert(
25        &self,
26        catalog: &Catalog,
27        table: &TableReference,
28        tuple: &Tuple,
29        txn_id: TransactionId,
30        cid: CommandId,
31    ) -> QuillSQLResult<(Arc<TableHeap>, crate::storage::page::RecordId)>;
32
33    fn mvcc_update(
34        &self,
35        heap: &Arc<TableHeap>,
36        rid: crate::storage::page::RecordId,
37        new_tuple: Tuple,
38        txn_id: TransactionId,
39        cid: CommandId,
40    ) -> QuillSQLResult<(
41        crate::storage::page::RecordId,
42        crate::storage::page::TupleMeta,
43    )>;
44
45    fn mvcc_delete(
46        &self,
47        heap: &Arc<TableHeap>,
48        rid: crate::storage::page::RecordId,
49        txn_id: TransactionId,
50        cid: CommandId,
51    ) -> QuillSQLResult<crate::storage::page::TupleMeta>;
52}
53
54#[derive(Default)]
55pub struct DefaultStorageEngine;
56
57impl StorageEngine for DefaultStorageEngine {
58    fn table_heap(
59        &self,
60        catalog: &Catalog,
61        table: &TableReference,
62    ) -> QuillSQLResult<Arc<TableHeap>> {
63        catalog.table_heap(table)
64    }
65
66    fn table_indexes(
67        &self,
68        catalog: &Catalog,
69        table: &TableReference,
70    ) -> QuillSQLResult<Vec<Arc<BPlusTreeIndex>>> {
71        catalog.table_indexes(table)
72    }
73
74    fn mvcc_insert(
75        &self,
76        catalog: &Catalog,
77        table: &TableReference,
78        tuple: &Tuple,
79        txn_id: crate::transaction::TransactionId,
80        cid: CommandId,
81    ) -> QuillSQLResult<(Arc<TableHeap>, crate::storage::page::RecordId)> {
82        let heap = catalog.table_heap(table)?;
83        let (rid, _) = heap.mvcc_insert_version(tuple, txn_id, cid, None)?;
84        Ok((heap, rid))
85    }
86
87    fn mvcc_update(
88        &self,
89        heap: &Arc<TableHeap>,
90        rid: crate::storage::page::RecordId,
91        new_tuple: Tuple,
92        txn_id: crate::transaction::TransactionId,
93        cid: CommandId,
94    ) -> QuillSQLResult<(
95        crate::storage::page::RecordId,
96        crate::storage::page::TupleMeta,
97    )> {
98        heap.mvcc_update(rid, new_tuple, txn_id, cid)
99    }
100
101    fn mvcc_delete(
102        &self,
103        heap: &Arc<TableHeap>,
104        rid: crate::storage::page::RecordId,
105        txn_id: crate::transaction::TransactionId,
106        cid: CommandId,
107    ) -> QuillSQLResult<crate::storage::page::TupleMeta> {
108        heap.mvcc_mark_deleted(rid, txn_id, cid)
109    }
110}