InvoDB/invodb/file/page_manager.cpp

129 lines
3.1 KiB
C++
Raw Normal View History

2021-10-11 22:05:45 +08:00
//
// Created by YuhangQ on 2021/9/24.
//
#include "page_manager.h"
2021-11-03 11:14:34 +08:00
#include "btree/list.h"
2021-11-05 23:15:09 +08:00
2021-10-11 22:05:45 +08:00
int PageManager::loadDatabase(const char *filename) {
Instance().stream.open(filename);
2021-11-03 11:14:34 +08:00
Instance().stream.seekp(0, std::ios::end);
int index = Instance().stream.tellp() / 1024;
if(index == 0) {
2021-11-07 12:57:26 +08:00
StoragePage page(0);
page.setIntStartFrom(4, 2);
page.save();
2021-11-03 11:14:34 +08:00
StoragePage(1).save();
}
2021-10-11 22:05:45 +08:00
return 0;
}
2021-11-07 12:57:26 +08:00
StoragePage PageManager::getPage(const int &index) {
2021-11-03 09:39:13 +08:00
2021-11-07 12:57:26 +08:00
// if(cache.exist(index)) {
// return *cache.get(index);
// }
2021-11-03 09:39:13 +08:00
2021-11-07 12:57:26 +08:00
StoragePage page(index);
2021-10-11 22:05:45 +08:00
// 调整指针位置
stream.clear();
2021-11-05 23:15:09 +08:00
stream.seekg((long long)index * 1024);
2021-11-07 12:57:26 +08:00
stream.read(page, 1024);
2021-10-11 22:05:45 +08:00
return page;
}
void PageManager::setPage(const int &index, const StoragePage &page) {
2021-11-07 12:57:26 +08:00
// auto p = cache.put(index, std::make_shared<StoragePage>(page));
// if(p == nullptr) return;
2021-11-05 23:15:09 +08:00
2021-10-11 22:05:45 +08:00
stream.clear();
2021-11-05 23:15:09 +08:00
stream.seekg((long long)index * 1024);
2021-10-11 22:05:45 +08:00
stream.write(page, 1024);
}
2021-10-23 16:28:57 +08:00
int PageManager::allocate() {
2021-11-05 23:15:09 +08:00
// try to allocate from free block list
auto page = getPage(0);
2021-11-07 12:57:26 +08:00
int index = page.getIntStartFrom(0);
2021-11-05 23:15:09 +08:00
if(index != 0) {
auto allocatePage = getPage(index);
2021-11-07 12:57:26 +08:00
page.setIntStartFrom(0, allocatePage.next());
2021-11-05 23:15:09 +08:00
// reset
2021-11-07 12:57:26 +08:00
allocatePage.clear();
allocatePage.save();
page.save();
//printf("1allocate: %d\n", index);
2021-11-05 23:15:09 +08:00
return index;
}
2021-11-07 12:57:26 +08:00
index = page.getIntStartFrom(4);
//printf("2allocate: %d\n", index);
2021-11-05 23:15:09 +08:00
2021-11-07 12:57:26 +08:00
page.setIntStartFrom(4, index + 1);
page.save();
// allocate block at the end
// stream.seekp(0, std::ios::end);
// index = stream.tellp() / 1024;
2021-10-23 16:28:57 +08:00
setPage(index, StoragePage(index));
return index;
2021-10-11 22:05:45 +08:00
}
2021-10-31 14:51:09 +08:00
2021-11-03 11:14:34 +08:00
void PageManager::release(const int &index, const bool &next) {
2021-11-05 23:15:09 +08:00
auto page = getPage(0);
2021-11-07 12:57:26 +08:00
int head = page.getIntStartFrom(0);
2021-11-05 23:15:09 +08:00
auto releasePage = getPage(index);
2021-11-07 12:57:26 +08:00
releasePage.setNext(head);
page.setIntStartFrom(0, releasePage.getAddress());
page.save();
releasePage.save();
2021-11-05 23:15:09 +08:00
//printf("release: %d\n", index);
2021-11-03 22:05:13 +08:00
2021-10-31 14:51:09 +08:00
}
nlohmann::json PageManager::readJSONFromFile(const int &index) {
std::string content;
2021-11-05 23:15:09 +08:00
auto page = getPage(index);
2021-10-31 14:51:09 +08:00
while(true) {
for(int i=0; i<1016; i++) {
2021-11-07 12:57:26 +08:00
if(page[i] == '\0') break;
content.push_back(page[i]);
2021-10-31 14:51:09 +08:00
}
2021-11-07 12:57:26 +08:00
if(page.next() == 0) break;
page = getPage(page.next());
2021-10-31 14:51:09 +08:00
}
return nlohmann::json::parse(content);
}
int PageManager::saveJSONToFile(const nlohmann::json& json) {
std::string content = json.dump();
int size = content.size();
2021-11-05 23:15:09 +08:00
auto page = getPage(allocate());
2021-11-07 12:57:26 +08:00
int res = page.getAddress();
2021-10-31 14:51:09 +08:00
int p = 0;
while(p < size) {
int len = std::min(size - p, 1016);
2021-11-07 12:57:26 +08:00
page.setStartFrom(0, &content.c_str()[p], len);
page.save();
2021-10-31 14:51:09 +08:00
p += len;
if(p < size) {
int newPage = allocate();
2021-11-07 12:57:26 +08:00
int lastPage = page.getAddress();
page.setNext(newPage);
page.save();
2021-10-31 14:51:09 +08:00
page = getPage(newPage);
2021-11-07 12:57:26 +08:00
page.setLast(lastPage);
page.save();
2021-10-31 14:51:09 +08:00
}
}
return res;
}