数据库文件页管理器完成

This commit is contained in:
YuhangQ 2021-10-05 16:02:01 +08:00
parent 07218a800a
commit 62c724a132
4 changed files with 29 additions and 14 deletions

View File

@ -6,18 +6,31 @@
int PageManager::loadDatabase(const char *filename) {
stream.open(filename);
printf("%d", stream.is_open());
stream << "hello" << std::endl;
stream.flush();
return 0;
}
StoragePage& PageManager::getPage(const int &index) {
StoragePage page;
// 调整指针位置
stream.seekg((index - 1) << 2);
stream.clear();
stream.seekg(index * 4096);
stream.read(page, 4096);
return page;
}
void PageManager::setPage(const int &index, const StoragePage &page) {
stream.clear();
stream.seekg(index * 4096);
stream.write(page, 4096);
}
void StoragePage::print() {
for(int i=0; i<16; i++) {
for(int j=0; j<256; j++) {
printf("%u ", page[i * 16 + j]);
}
printf("\n");
}
}

View File

@ -7,15 +7,15 @@
#include <iostream>
#include <fstream>
#include <cstring>
class StoragePage {
public:
void print();
StoragePage() { memset(page, 0, sizeof(page)); }
char& operator[] (int index) { return this->page[index]; }
operator const char *() { return this->page; }
operator const char *() const { return this->page; }
operator char *() { return this->page; }
~StoragePage() {
printf("我被回收了");
}
private:
char page[4096];
};
@ -29,12 +29,13 @@ public:
int loadDatabase(const char *filename);
StoragePage& getPage(const int &index);
void setPage(const int &index, const StoragePage &page);
private:
std::fstream stream;
// 私有化实现单例
PageManager();
~PageManager();
PageManager() {}
~PageManager() {}
PageManager(const PageManager&);
PageManager& operator=(const PageManager&);
};

View File

@ -9,7 +9,9 @@ int main() {
PageManager& manager = PageManager::Instance();
manager.loadDatabase("test.invodb");
manager.getPage(0);
StoragePage page = manager.getPage(1);
for(int i=0; i<100; i++) printf("%c", page[i]);
return 0;
}

View File

@ -7,6 +7,5 @@
#include <iostream>
#include "io/page_manager.h"
#include "io/virtual_storage.h"
#endif //INVODB_MAIN_H