提交 e0d2919b 编写于 作者: 羽飞's avatar 羽飞

implenment record iterator

上级 8d904d04
...@@ -390,6 +390,13 @@ RC RecordFileScanner::open_scan(DiskBufferPool &buffer_pool, ConditionFilter *co ...@@ -390,6 +390,13 @@ RC RecordFileScanner::open_scan(DiskBufferPool &buffer_pool, ConditionFilter *co
RC RecordFileScanner::fetch_next_record() RC RecordFileScanner::fetch_next_record()
{ {
RC rc = RC::SUCCESS; RC rc = RC::SUCCESS;
if (record_page_iterator_.is_valid()) {
rc = fetch_next_record_in_page();
if (rc == RC::SUCCESS || rc != RC::RECORD_EOF) {
return rc;
}
}
while (bp_iterator_.has_next()) { while (bp_iterator_.has_next()) {
PageNum page_num = bp_iterator_.next(); PageNum page_num = bp_iterator_.next();
record_page_handler_.cleanup(); record_page_handler_.cleanup();
...@@ -400,7 +407,7 @@ RC RecordFileScanner::fetch_next_record() ...@@ -400,7 +407,7 @@ RC RecordFileScanner::fetch_next_record()
} }
record_page_iterator_.init(record_page_handler_); record_page_iterator_.init(record_page_handler_);
rc = fetch_record_in_page(); rc = fetch_next_record_in_page();
if (rc == RC::SUCCESS || rc != RC::RECORD_EOF) { if (rc == RC::SUCCESS || rc != RC::RECORD_EOF) {
return rc; return rc;
} }
...@@ -409,7 +416,7 @@ RC RecordFileScanner::fetch_next_record() ...@@ -409,7 +416,7 @@ RC RecordFileScanner::fetch_next_record()
return RC::RECORD_EOF; return RC::RECORD_EOF;
} }
RC RecordFileScanner::fetch_record_in_page() RC RecordFileScanner::fetch_next_record_in_page()
{ {
RC rc = RC::SUCCESS; RC rc = RC::SUCCESS;
while (record_page_iterator_.has_next()) { while (record_page_iterator_.has_next()) {
......
...@@ -24,11 +24,11 @@ typedef int32_t SlotNum; ...@@ -24,11 +24,11 @@ typedef int32_t SlotNum;
class ConditionFilter; class ConditionFilter;
struct PageHeader { struct PageHeader {
int record_num; // 当前页面记录的个数 int32_t record_num; // 当前页面记录的个数
int record_capacity; // 最大记录个数 int32_t record_capacity; // 最大记录个数
int record_real_size; // 每条记录的实际大小 int32_t record_real_size; // 每条记录的实际大小
int record_size; // 每条记录占用实际空间大小(可能对齐) int32_t record_size; // 每条记录占用实际空间大小(可能对齐)
int first_record_offset; // 第一条记录的偏移量 int32_t first_record_offset; // 第一条记录的偏移量
}; };
struct RID { struct RID {
...@@ -108,6 +108,9 @@ public: ...@@ -108,6 +108,9 @@ public:
bool has_next(); bool has_next();
RC next(Record &record); RC next(Record &record);
bool is_valid() const {
return record_page_handler_ != nullptr;
}
private: private:
RecordPageHandler *record_page_handler_ = nullptr; RecordPageHandler *record_page_handler_ = nullptr;
PageNum page_num_ = BP_INVALID_PAGE_NUM; PageNum page_num_ = BP_INVALID_PAGE_NUM;
...@@ -227,7 +230,7 @@ public: ...@@ -227,7 +230,7 @@ public:
private: private:
RC fetch_next_record(); RC fetch_next_record();
RC fetch_record_in_page(); RC fetch_next_record_in_page();
private: private:
DiskBufferPool *disk_buffer_pool_ = nullptr; DiskBufferPool *disk_buffer_pool_ = nullptr;
......
...@@ -106,12 +106,12 @@ RC BufferPoolIterator::init(DiskBufferPool &bp, PageNum start_page /* = 0 */) ...@@ -106,12 +106,12 @@ RC BufferPoolIterator::init(DiskBufferPool &bp, PageNum start_page /* = 0 */)
bool BufferPoolIterator::has_next() bool BufferPoolIterator::has_next()
{ {
return bitmap_.next_setted_bit(current_page_num_) != -1; return bitmap_.next_setted_bit(current_page_num_ + 1) != -1;
} }
PageNum BufferPoolIterator::next() PageNum BufferPoolIterator::next()
{ {
PageNum next_page = bitmap_.next_setted_bit(current_page_num_); PageNum next_page = bitmap_.next_setted_bit(current_page_num_ + 1);
if (next_page != -1) { if (next_page != -1) {
current_page_num_ = next_page; current_page_num_ = next_page;
} }
......
...@@ -102,6 +102,81 @@ TEST(test_record_page_handler, test_record_page_handler) ...@@ -102,6 +102,81 @@ TEST(test_record_page_handler, test_record_page_handler)
ASSERT_EQ(rc, RC::SUCCESS); ASSERT_EQ(rc, RC::SUCCESS);
} }
ASSERT_EQ(count, 6); ASSERT_EQ(count, 6);
bpm->close_file(record_manager_file);
}
TEST(test_record_page_handler, test_record_file_iterator)
{
const char *record_manager_file = "record_manager.bp";
::remove(record_manager_file);
BufferPoolManager *bpm = new BufferPoolManager();
DiskBufferPool *bp = nullptr;
RC rc = bpm->create_file(record_manager_file);
ASSERT_EQ(rc, RC::SUCCESS);
rc = bpm->open_file(record_manager_file, bp);
ASSERT_EQ(rc, RC::SUCCESS);
RecordFileHandler file_handler;
rc = file_handler.init(bp);
ASSERT_EQ(rc, RC::SUCCESS);
RecordFileScanner file_scanner;
rc = file_scanner.open_scan(*bp, nullptr);
ASSERT_EQ(rc, RC::SUCCESS);
int count = 0;
Record record;
while (file_scanner.has_next()) {
rc = file_scanner.next(record);
ASSERT_EQ(rc, RC::SUCCESS);
count++;
}
file_scanner.close_scan();
ASSERT_EQ(count, 0);
const int record_insert_num = 1000;
char record_data[20];
std::vector<RID> rids;
for (int i = 0; i < record_insert_num; i++) {
RID rid;
rc = file_handler.insert_record(record_data, sizeof(record_data), &rid);
ASSERT_EQ(rc, RC::SUCCESS);
rids.push_back(rid);
}
rc = file_scanner.open_scan(*bp, nullptr);
ASSERT_EQ(rc, RC::SUCCESS);
count = 0;
while (file_scanner.has_next()) {
rc = file_scanner.next(record);
ASSERT_EQ(rc, RC::SUCCESS);
count++;
}
file_scanner.close_scan();
ASSERT_EQ(count, rids.size());
for (int i = 0; i < record_insert_num; i += 2) {
rc = file_handler.delete_record(&rids[i]);
ASSERT_EQ(rc, RC::SUCCESS);
}
rc = file_scanner.open_scan(*bp, nullptr);
ASSERT_EQ(rc, RC::SUCCESS);
count = 0;
while (file_scanner.has_next()) {
rc = file_scanner.next(record);
ASSERT_EQ(rc, RC::SUCCESS);
count++;
}
file_scanner.close_scan();
ASSERT_EQ(count, rids.size() / 2);
bpm->close_file(record_manager_file);
} }
int main(int argc, char **argv) int main(int argc, char **argv)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册