@@ -55,32 +55,27 @@ HeapTable::~HeapTable() {
5555
5656/* --- Iterator Implementation --- */
5757
58-
5958common::Value HeapTable::TupleView::get_value (size_t col_index) const {
6059 if (!schema || col_index >= schema->column_count ()) {
6160 return common::Value::make_null ();
6261 }
63-
62+
6463 // We must walk the serialized payload from the beginning to reach col_index
6564 size_t cursor = 0 ;
6665 for (size_t i = 0 ; i <= col_index; ++i) {
6766 if (cursor >= payload_len) return common::Value::make_null ();
68-
67+
6968 auto type = static_cast <common::ValueType>(payload_data[cursor++]);
70-
69+
7170 if (type == common::ValueType::TYPE_NULL) {
7271 if (i == col_index) return common::Value::make_null ();
7372 continue ;
7473 }
7574
76- if (type == common::ValueType::TYPE_BOOL ||
77- type == common::ValueType::TYPE_INT8 ||
78- type == common::ValueType::TYPE_INT16 ||
79- type == common::ValueType::TYPE_INT32 ||
80- type == common::ValueType::TYPE_INT64 ||
81- type == common::ValueType::TYPE_FLOAT32 ||
75+ if (type == common::ValueType::TYPE_BOOL || type == common::ValueType::TYPE_INT8 ||
76+ type == common::ValueType::TYPE_INT16 || type == common::ValueType::TYPE_INT32 ||
77+ type == common::ValueType::TYPE_INT64 || type == common::ValueType::TYPE_FLOAT32 ||
8278 type == common::ValueType::TYPE_FLOAT64) {
83-
8479 if (cursor + 8 > payload_len) return common::Value::make_null ();
8580
8681 if (i == col_index) {
@@ -105,9 +100,9 @@ common::Value HeapTable::TupleView::get_value(size_t col_index) const {
105100 uint32_t len;
106101 std::memcpy (&len, payload_data + cursor, 4 );
107102 cursor += 4 ;
108-
103+
109104 if (cursor + len > payload_len) return common::Value::make_null ();
110-
105+
111106 if (i == col_index) {
112107 std::string s (reinterpret_cast <const char *>(payload_data + cursor), len);
113108 return common::Value::make_text (s);
@@ -201,14 +196,13 @@ bool HeapTable::Iterator::next_meta(TupleMeta& out_meta) {
201196 eof_ = true ;
202197 return false ;
203198 }
204-
199+
205200 // Cache page header and buffer pointer (Phase 2 optimization)
206201 cached_buffer_ = reinterpret_cast <const uint8_t *>(current_page_->get_data ());
207202 std::memcpy (&cached_header_, cached_buffer_, sizeof (PageHeader));
208203 }
209204
210205 if (cached_header_.free_space_offset == 0 ) {
211-
212206 table_.bpm_ .unpin_page_by_id (table_.file_id_ , current_page_num_, false );
213207 current_page_ = nullptr ;
214208 cached_buffer_ = nullptr ;
@@ -219,9 +213,10 @@ bool HeapTable::Iterator::next_meta(TupleMeta& out_meta) {
219213 /* Scan slots in the current page starting from next_id_.slot_num */
220214 while (next_id_.slot_num < cached_header_.num_slots ) {
221215 uint16_t offset = 0 ;
222- std::memcpy (&offset,
223- cached_buffer_ + sizeof (PageHeader) + (next_id_.slot_num * sizeof (uint16_t )),
224- sizeof (uint16_t ));
216+ std::memcpy (
217+ &offset,
218+ cached_buffer_ + sizeof (PageHeader) + (next_id_.slot_num * sizeof (uint16_t )),
219+ sizeof (uint16_t ));
225220
226221 if (offset != 0 ) {
227222 /* Found a record: Deserialize it in-place from the pinned buffer */
@@ -599,7 +594,7 @@ bool HeapTable::get_meta(const TupleId& tuple_id, TupleMeta& out_meta) const {
599594 return false ;
600595 }
601596
602- const uint8_t * const data = reinterpret_cast <const uint8_t *>(buffer + offset);
597+ const uint8_t * const data = reinterpret_cast <const uint8_t *>(buffer + offset);
603598
604599 uint16_t tuple_data_len;
605600 std::memcpy (&tuple_data_len, data, 2 );
@@ -761,7 +756,6 @@ bool HeapTable::write_page(uint32_t page_num, const char* buffer) {
761756}
762757
763758bool HeapTable::Iterator::next_view (TupleView& out_view) {
764-
765759 if (eof_) {
766760 return false ;
767761 }
@@ -775,14 +769,13 @@ bool HeapTable::Iterator::next_view(TupleView& out_view) {
775769 eof_ = true ;
776770 return false ;
777771 }
778-
772+
779773 // Cache page header and buffer pointer (Phase 2 optimization)
780774 cached_buffer_ = reinterpret_cast <const uint8_t *>(current_page_->get_data ());
781775 std::memcpy (&cached_header_, cached_buffer_, sizeof (PageHeader));
782776 }
783777
784778 if (cached_header_.free_space_offset == 0 ) {
785-
786779 table_.bpm_ .unpin_page_by_id (table_.file_id_ , current_page_num_, false );
787780 current_page_ = nullptr ;
788781 cached_buffer_ = nullptr ;
@@ -793,9 +786,10 @@ bool HeapTable::Iterator::next_view(TupleView& out_view) {
793786 /* Scan slots in the current page starting from next_id_.slot_num */
794787 while (next_id_.slot_num < cached_header_.num_slots ) {
795788 uint16_t offset = 0 ;
796- std::memcpy (&offset,
797- cached_buffer_ + sizeof (PageHeader) + (next_id_.slot_num * sizeof (uint16_t )),
798- sizeof (uint16_t ));
789+ std::memcpy (
790+ &offset,
791+ cached_buffer_ + sizeof (PageHeader) + (next_id_.slot_num * sizeof (uint16_t )),
792+ sizeof (uint16_t ));
799793
800794 if (offset != 0 ) {
801795 const uint8_t * const data = cached_buffer_ + offset;
@@ -840,4 +834,3 @@ bool HeapTable::Iterator::next_view(TupleView& out_view) {
840834}
841835
842836} // namespace cloudsql::storage
843-
0 commit comments