Skip to content

Commit 5d7a9ac

Browse files
authored
feat(p4): refactor table heap + tuple for project 4 (#542)
* feat(p4): refactor table heap + tuple for project 4 Signed-off-by: Alex Chi <[email protected]> * verify p3 Signed-off-by: Alex Chi <[email protected]> * fix Signed-off-by: Alex Chi <[email protected]> * fmt Signed-off-by: Alex Chi <[email protected]> * impl hack Signed-off-by: Alex Chi <[email protected]> * fix Signed-off-by: Alex Chi <[email protected]> * fix Signed-off-by: Alex Chi <[email protected]> * fix race Signed-off-by: Alex Chi <[email protected]> * fix race Signed-off-by: Alex Chi <[email protected]> * fix race Signed-off-by: Alex Chi <[email protected]> --------- Signed-off-by: Alex Chi <[email protected]>
1 parent fc3bbf3 commit 5d7a9ac

File tree

15 files changed

+98
-34
lines changed

15 files changed

+98
-34
lines changed

src/catalog/table_generator.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <algorithm>
44
#include <random>
55
#include <vector>
6+
#include "common/config.h"
67

78
namespace bustub {
89

@@ -76,7 +77,8 @@ void TableGenerator::FillTable(TableInfo *info, TableInsertMeta *table_meta) {
7677
for (const auto &col : values) {
7778
entry.emplace_back(col[i]);
7879
}
79-
auto rid = info->table_->InsertTuple(TupleMeta{0, 0, false}, Tuple(entry, &info->schema_));
80+
auto rid =
81+
info->table_->InsertTuple(TupleMeta{INVALID_TXN_ID, INVALID_TXN_ID, false}, Tuple(entry, &info->schema_));
8082
BUSTUB_ENSURE(rid != std::nullopt, "Sequential insertion cannot fail");
8183
num_inserted++;
8284
}

src/concurrency/lock_manager.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ auto LockManager::LockRow(Transaction *txn, LockMode lock_mode, const table_oid_
2626
return true;
2727
}
2828

29-
auto LockManager::UnlockRow(Transaction *txn, const table_oid_t &oid, const RID &rid) -> bool { return true; }
29+
auto LockManager::UnlockRow(Transaction *txn, const table_oid_t &oid, const RID &rid, bool force) -> bool {
30+
return true;
31+
}
3032

3133
void LockManager::AddEdge(txn_id_t t1, txn_id_t t2) {}
3234

src/include/concurrency/lock_manager.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,10 @@ class LockManager {
260260
* @param rid the RID that is locked by the transaction
261261
* @param oid the table_oid_t of the table the row belongs to
262262
* @param rid the RID of the row to be unlocked
263+
* @param force unlock the tuple regardless of isolation level, not changing the transaction state
263264
* @return true if the unlock is successful, false otherwise
264265
*/
265-
auto UnlockRow(Transaction *txn, const table_oid_t &oid, const RID &rid) -> bool;
266+
auto UnlockRow(Transaction *txn, const table_oid_t &oid, const RID &rid, bool force = false) -> bool;
266267

267268
/*** Graph API ***/
268269

src/include/concurrency/transaction.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ using index_oid_t = uint32_t;
6565
class TableWriteRecord {
6666
public:
6767
// NOLINTNEXTLINE
68-
TableWriteRecord(RID rid, WType wtype, const Tuple &tuple, TableHeap *table)
69-
: rid_(rid), wtype_(wtype), tuple_(tuple), table_(table) {}
68+
TableWriteRecord(table_oid_t tid, RID rid, TableHeap *table_heap) : tid_(tid), rid_(rid), table_heap_(table_heap) {}
7069

70+
table_oid_t tid_;
7171
RID rid_;
72+
TableHeap *table_heap_;
73+
74+
// Recording write type might be useful if you want to implement in-place update for leaderboard
75+
// optimization. You don't need it for the basic implementation.
7276
WType wtype_;
73-
/** The tuple is only used for the update operation. */
74-
Tuple tuple_;
75-
/** The table heap specifies which table this write record is for. */
76-
TableHeap *table_;
7777
};
7878

7979
/**
@@ -86,6 +86,11 @@ class IndexWriteRecord {
8686
Catalog *catalog)
8787
: rid_(rid), table_oid_(table_oid), wtype_(wtype), tuple_(tuple), index_oid_(index_oid), catalog_(catalog) {}
8888

89+
/**
90+
* Note(spring2023): I don't know what are these for. If you are implementing leaderboard optimizations, you can
91+
* figure out how to use this structure to store what you need.
92+
*/
93+
8994
/** The rid is the value stored in the index. */
9095
RID rid_;
9196
/** Table oid. */

src/include/optimizer/optimizer.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
#include "execution/expressions/abstract_expression.h"
1313
#include "execution/plans/abstract_plan.h"
1414

15-
#define BUSTUB_OPTIMIZER_HACK_REMOVE_AFTER_2022_FALL
16-
1715
namespace bustub {
1816

1917
/**

src/include/storage/page/table_page.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ class TablePage {
8585
*/
8686
auto GetTuple(const RID &rid) const -> std::pair<TupleMeta, Tuple>;
8787

88+
/**
89+
* Read a tuple meta from a table.
90+
*/
91+
auto GetTupleMeta(const RID &rid) const -> TupleMeta;
92+
8893
/**
8994
* Update a tuple in place.
9095
*/

src/include/storage/table/table_heap.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,26 @@ class TableHeap {
6060
/**
6161
* Read a tuple from the table.
6262
* @param rid rid of the tuple to read
63-
* @param tuple output variable for the tuple
64-
* @return the tuple
63+
* @return the meta and tuple
6564
*/
6665
auto GetTuple(RID rid) -> std::pair<TupleMeta, Tuple>;
6766

67+
/**
68+
* Read a tuple meta from the table. Note: if you want to get tuple and meta together, use `GetTuple` insead
69+
* to ensure atomicity.
70+
* @param rid rid of the tuple to read
71+
* @return the meta
72+
*/
73+
auto GetTupleMeta(RID rid) -> TupleMeta;
74+
6875
/** @return the iterator of this table */
6976
auto MakeIterator() -> TableIterator;
7077

7178
/** @return the id of the first page of this table */
7279
inline auto GetFirstPageId() const -> page_id_t { return first_page_id_; }
7380

7481
/**
75-
* Update a tuple in place. SHOULD BE ONLY USED IN PROJECT 3. Only tuple of the same
76-
* size is allowed.
82+
* Update a tuple in place. SHOULD NOT BE USED UNLESS YOU WANT TO OPTIMIZE FOR PROJECT 4.
7783
* @param meta new tuple meta
7884
* @param tuple new tuple
7985
* @param[out] rid the rid of the tuple to be updated

src/include/storage/table/table_iterator.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class TableIterator {
3434
public:
3535
DISALLOW_COPY(TableIterator);
3636

37-
TableIterator(TableHeap *table_heap, RID rid);
37+
TableIterator(TableHeap *table_heap, RID rid, RID stop_at_rid);
3838
TableIterator(TableIterator &&) = default;
3939

4040
~TableIterator() = default;
@@ -50,6 +50,11 @@ class TableIterator {
5050
private:
5151
TableHeap *table_heap_;
5252
RID rid_;
53+
54+
// When creating table iterator, we will record the maximum RID that we should scan.
55+
// Otherwise we will have dead loops when updating while scanning. (In project 4, update should be implemented as
56+
// deletion + insertion.)
57+
RID stop_at_rid_;
5358
};
5459

5560
} // namespace bustub

src/optimizer/eliminate_true_filter.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
namespace bustub {
1010

11-
#ifdef BUSTUB_OPTIMIZER_HACK_REMOVE_AFTER_2022_FALL
12-
1311
auto Optimizer::OptimizeEliminateTrueFilter(const AbstractPlanNodeRef &plan) -> AbstractPlanNodeRef {
1412
std::vector<AbstractPlanNodeRef> children;
1513
for (const auto &child : plan->GetChildren()) {
@@ -29,6 +27,4 @@ auto Optimizer::OptimizeEliminateTrueFilter(const AbstractPlanNodeRef &plan) ->
2927
return optimized_plan;
3028
}
3129

32-
#endif
33-
3430
} // namespace bustub

src/optimizer/merge_filter_scan.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
namespace bustub {
1212

13-
#ifdef BUSTUB_OPTIMIZER_HACK_REMOVE_AFTER_2022_FALL
14-
1513
auto Optimizer::OptimizeMergeFilterScan(const AbstractPlanNodeRef &plan) -> AbstractPlanNodeRef {
1614
std::vector<AbstractPlanNodeRef> children;
1715
for (const auto &child : plan->GetChildren()) {
@@ -36,6 +34,4 @@ auto Optimizer::OptimizeMergeFilterScan(const AbstractPlanNodeRef &plan) -> Abst
3634
return optimized_plan;
3735
}
3836

39-
#endif
40-
4137
} // namespace bustub

0 commit comments

Comments
 (0)