Skip to content

Commit

Permalink
add UpdateTupleMetaTs() and UpdateTupleMetaIsDelete() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Wangch29 committed Dec 18, 2024
1 parent 1964231 commit 7bdee1f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/include/storage/page/table_page.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ class TablePage {
*/
void UpdateTupleMeta(const TupleMeta &meta, const RID &rid);

/**
* Update timestamp of the meta of a tuple.
*/
void UpdateTupleMetaTs(timestamp_t ts, const RID &rid);

/**
* Update is_deleted of the meta of a tuple.
*/
void UpdateTupleMetaIsDeleted(bool is_deleted, const RID &rid);

/**
* Read a tuple from a table.
*/
Expand Down
14 changes: 14 additions & 0 deletions src/include/storage/table/table_heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ class TableHeap {
*/
void UpdateTupleMeta(const TupleMeta &meta, RID rid);

/**
* Update timestamp of the meta of a tuple.
* @param ts new timestamp of meta
* @param rid the rid of the inserted tuple
*/
void UpdateTupleMetaTs(timestamp_t ts, RID rid);

/**
* Update is_deleted of the meta of a tuple.
* @param is_deleted new is_deleted
* @param rid the rid of the inserted tuple
*/
void UpdateTupleMetaIsDelete(bool is_deleted, RID rid);

/**
* Read a tuple from the table.
* @param rid rid of the tuple to read
Expand Down
23 changes: 23 additions & 0 deletions src/storage/page/table_page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,29 @@ void TablePage::UpdateTupleMeta(const TupleMeta &meta, const RID &rid) {
tuple_info_[tuple_id] = std::make_tuple(offset, size, meta);
}

void TablePage::UpdateTupleMetaTs(timestamp_t ts, const RID &rid) {
auto tuple_id = rid.GetSlotNum();
if (tuple_id >= num_tuples_) {
throw bustub::Exception("Tuple ID out of range");
}
auto &[offset, size, old_meta] = tuple_info_[tuple_id];
TupleMeta new_meta = {ts, old_meta.is_deleted_};
tuple_info_[tuple_id] = std::make_tuple(offset, size, new_meta);
}

void TablePage::UpdateTupleMetaIsDeleted(bool is_deleted, const RID &rid) {
auto tuple_id = rid.GetSlotNum();
if (tuple_id >= num_tuples_) {
throw bustub::Exception("Tuple ID out of range");
}
auto &[offset, size, old_meta] = tuple_info_[tuple_id];
if (!old_meta.is_deleted_ && is_deleted) {
num_deleted_tuples_++;
}
TupleMeta new_meta = {old_meta.ts_, is_deleted};
tuple_info_[tuple_id] = std::make_tuple(offset, size, new_meta);
}

auto TablePage::GetTuple(const RID &rid) const -> std::pair<TupleMeta, Tuple> {
auto tuple_id = rid.GetSlotNum();
if (tuple_id >= num_tuples_) {
Expand Down
12 changes: 12 additions & 0 deletions src/storage/table/table_heap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ void TableHeap::UpdateTupleMeta(const TupleMeta &meta, RID rid) {
page->UpdateTupleMeta(meta, rid);
}

void TableHeap::UpdateTupleMetaTs(timestamp_t ts, RID rid) {
auto page_guard = bpm_->WritePage(rid.GetPageId());
auto page = page_guard.AsMut<TablePage>();
page->UpdateTupleMetaTs(ts, rid);
}

void TableHeap::UpdateTupleMetaIsDelete(bool is_deleted, RID rid) {
auto page_guard = bpm_->WritePage(rid.GetPageId());
auto page = page_guard.AsMut<TablePage>();
page->UpdateTupleMetaIsDeleted(is_deleted, rid);
}

auto TableHeap::GetTuple(RID rid) -> std::pair<TupleMeta, Tuple> {
auto page_guard = bpm_->ReadPage(rid.GetPageId());
auto page = page_guard.As<TablePage>();
Expand Down

0 comments on commit 7bdee1f

Please sign in to comment.