数据库文件页管理器完成

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) { int PageManager::loadDatabase(const char *filename) {
stream.open(filename); stream.open(filename);
printf("%d", stream.is_open());
stream << "hello" << std::endl;
stream.flush(); stream.flush();
return 0; return 0;
} }
StoragePage& PageManager::getPage(const int &index) { StoragePage& PageManager::getPage(const int &index) {
StoragePage page; StoragePage page;
// 调整指针位置 // 调整指针位置
stream.seekg((index - 1) << 2); stream.clear();
stream.seekg(index * 4096);
stream.read(page, 4096); stream.read(page, 4096);
return page; 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 <iostream>
#include <fstream> #include <fstream>
#include <cstring>
class StoragePage { class StoragePage {
public: public:
void print();
StoragePage() { memset(page, 0, sizeof(page)); }
char& operator[] (int index) { return this->page[index]; } 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; } operator char *() { return this->page; }
~StoragePage() {
printf("我被回收了");
}
private: private:
char page[4096]; char page[4096];
}; };
@ -29,12 +29,13 @@ public:
int loadDatabase(const char *filename); int loadDatabase(const char *filename);
StoragePage& getPage(const int &index); StoragePage& getPage(const int &index);
void setPage(const int &index, const StoragePage &page); void setPage(const int &index, const StoragePage &page);
private: private:
std::fstream stream; std::fstream stream;
// 私有化实现单例 // 私有化实现单例
PageManager(); PageManager() {}
~PageManager(); ~PageManager() {}
PageManager(const PageManager&); PageManager(const PageManager&);
PageManager& operator=(const PageManager&); PageManager& operator=(const PageManager&);
}; };

View File

@ -9,7 +9,9 @@ int main() {
PageManager& manager = PageManager::Instance(); PageManager& manager = PageManager::Instance();
manager.loadDatabase("test.invodb"); 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; return 0;
} }

View File

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