Skip to content

Commit 345f05a

Browse files
authored
fix(p3): add in place update to avoid dead loop (#541)
* fix(p3): add in place update to avoid dead loop Signed-off-by: Alex Chi <[email protected]> * add update executor to cmakelist Signed-off-by: Alex Chi <[email protected]> --------- Signed-off-by: Alex Chi <[email protected]>
1 parent 4eb3650 commit 345f05a

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ set(P3_FILES
323323
"src/include/execution/executors/seq_scan_executor.h"
324324
"src/include/execution/executors/sort_executor.h"
325325
"src/include/execution/executors/topn_executor.h"
326+
"src/include/execution/executors/update_executor.h"
326327
"src/execution/aggregation_executor.cpp"
327328
"src/execution/delete_executor.cpp"
328329
"src/execution/filter_executor.cpp"
@@ -335,6 +336,7 @@ set(P3_FILES
335336
"src/execution/seq_scan_executor.cpp"
336337
"src/execution/sort_executor.cpp"
337338
"src/execution/topn_executor.cpp"
339+
"src/execution/update_executor.cpp"
338340
"src/include/optimizer/optimizer.h"
339341
"src/include/optimizer/optimizer_internal.h"
340342
"src/optimizer/nlj_as_hash_join.cpp"
@@ -359,8 +361,6 @@ add_custom_target(submit-p3
359361
set(P4_FILES
360362
"src/include/concurrency/lock_manager.h"
361363
"src/concurrency/lock_manager.cpp"
362-
"src/include/execution/executors/update_executor.h"
363-
"src/execution/update_executor.cpp"
364364
"src/include/execution/plans/index_scan_plan.h"
365365
"tools/terrier_bench/terrier_bench_config.h"
366366
${P3_FILES}

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+
* Update a tuple in place.
90+
*/
91+
void UpdateTupleInPlaceUnsafe(const TupleMeta &meta, const Tuple &tuple, RID rid);
92+
8893
static_assert(sizeof(page_id_t) == 4);
8994

9095
private:

src/include/storage/table/table_heap.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ class TableHeap {
7171
/** @return the id of the first page of this table */
7272
inline auto GetFirstPageId() const -> page_id_t { return first_page_id_; }
7373

74+
/**
75+
* Update a tuple in place. SHOULD BE ONLY USED IN PROJECT 3. Only tuple of the same
76+
* size is allowed.
77+
* @param meta new tuple meta
78+
* @param tuple new tuple
79+
* @param[out] rid the rid of the tuple to be updated
80+
*/
81+
void UpdateTupleInPlaceUnsafe(const TupleMeta &meta, const Tuple &tuple, RID rid);
82+
7483
private:
7584
BufferPoolManager *bpm_;
7685
page_id_t first_page_id_{INVALID_PAGE_ID};

src/storage/page/table_page.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,20 @@ auto TablePage::GetTuple(const RID &rid) const -> std::pair<TupleMeta, Tuple> {
8181
return std::make_pair(meta, std::move(tuple));
8282
}
8383

84+
void TablePage::UpdateTupleInPlaceUnsafe(const TupleMeta &meta, const Tuple &tuple, RID rid) {
85+
auto tuple_id = rid.GetSlotNum();
86+
if (tuple_id >= num_tuples_) {
87+
throw bustub::Exception("Tuple ID out of range");
88+
}
89+
auto &[offset, size, old_meta] = tuple_info_[tuple_id];
90+
if (size != tuple.GetLength()) {
91+
throw bustub::Exception("Tuple size mismatch");
92+
}
93+
if (!old_meta.is_deleted_ && meta.is_deleted_) {
94+
num_deleted_tuples_++;
95+
}
96+
tuple_info_[tuple_id] = std::make_tuple(offset, size, meta);
97+
memcpy(page_start_ + offset, tuple.data_.data(), tuple.GetLength());
98+
}
99+
84100
} // namespace bustub

src/storage/table/table_heap.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,10 @@ auto TableHeap::GetTuple(RID rid) -> std::pair<TupleMeta, Tuple> {
8181

8282
auto TableHeap::MakeIterator() -> TableIterator { return {this, {first_page_id_, 0}}; }
8383

84+
void TableHeap::UpdateTupleInPlaceUnsafe(const TupleMeta &meta, const Tuple &tuple, RID rid) {
85+
auto page_guard = bpm_->FetchPageWrite(rid.GetPageId());
86+
auto page = page_guard.AsMut<TablePage>();
87+
page->UpdateTupleInPlaceUnsafe(meta, tuple, rid);
88+
}
89+
8490
} // namespace bustub

0 commit comments

Comments
 (0)