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"
|
|
|
|
|
|
|
|
List<int, 4>* PageManager::freeList = new List<int, 4>(1);
|
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) {
|
|
|
|
StoragePage(0).save();
|
|
|
|
StoragePage(1).save();
|
|
|
|
StoragePage(2).save();
|
|
|
|
}
|
2021-10-11 22:05:45 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
StoragePage PageManager::getPage(const int &index) {
|
2021-11-03 09:39:13 +08:00
|
|
|
|
2021-11-03 22:05:13 +08:00
|
|
|
/*
|
2021-11-03 09:39:13 +08:00
|
|
|
if(cache.exist(index)) {
|
|
|
|
return cache.get(index);
|
|
|
|
}
|
2021-11-03 22:05:13 +08:00
|
|
|
*/
|
2021-11-03 09:39:13 +08:00
|
|
|
|
2021-10-23 16:28:57 +08:00
|
|
|
StoragePage page(index);
|
2021-10-11 22:05:45 +08:00
|
|
|
// 调整指针位置
|
|
|
|
stream.clear();
|
|
|
|
stream.seekg(index * 1024);
|
|
|
|
stream.read(page, 1024);
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageManager::setPage(const int &index, const StoragePage &page) {
|
2021-11-03 22:05:13 +08:00
|
|
|
//cache.put(index, page);
|
2021-10-11 22:05:45 +08:00
|
|
|
stream.clear();
|
|
|
|
stream.seekg(index * 1024);
|
|
|
|
stream.write(page, 1024);
|
|
|
|
}
|
|
|
|
|
2021-10-23 16:28:57 +08:00
|
|
|
int PageManager::allocate() {
|
|
|
|
stream.seekp(0, std::ios::end);
|
|
|
|
int index = stream.tellp() / 1024;
|
|
|
|
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-03 22:05:13 +08:00
|
|
|
|
|
|
|
return;
|
2021-11-03 11:14:34 +08:00
|
|
|
auto page = getPage(index);
|
|
|
|
freeList->insert(page.getAddress());
|
|
|
|
if(next) {
|
|
|
|
while(page.next()) {
|
|
|
|
freeList->insert(page.next());
|
|
|
|
page = getPage(page.next());
|
|
|
|
}
|
|
|
|
}
|
2021-10-31 14:51:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
nlohmann::json PageManager::readJSONFromFile(const int &index) {
|
|
|
|
std::string content;
|
|
|
|
|
|
|
|
StoragePage page = getPage(index);
|
|
|
|
while(true) {
|
|
|
|
for(int i=0; i<1016; i++) {
|
|
|
|
if(page[i] == '\0') break;
|
|
|
|
content.push_back(page[i]);
|
|
|
|
}
|
|
|
|
if(page.next() == 0) break;
|
|
|
|
page = getPage(page.next());
|
|
|
|
}
|
|
|
|
|
|
|
|
return nlohmann::json::parse(content);
|
|
|
|
}
|
|
|
|
|
|
|
|
int PageManager::saveJSONToFile(const nlohmann::json& json) {
|
|
|
|
std::string content = json.dump();
|
|
|
|
int size = content.size();
|
|
|
|
|
|
|
|
StoragePage page = getPage(allocate());
|
|
|
|
int res = page.getAddress();
|
|
|
|
int p = 0;
|
|
|
|
while(p < size) {
|
|
|
|
int len = std::min(size - p, 1016);
|
|
|
|
page.setStartFrom(0, &content.c_str()[p], len);
|
|
|
|
page.save();
|
|
|
|
p += len;
|
|
|
|
if(p < size) {
|
|
|
|
int newPage = allocate();
|
|
|
|
int lastPage = page.getAddress();
|
|
|
|
page.setNext(newPage);
|
|
|
|
page.save();
|
|
|
|
page = getPage(newPage);
|
|
|
|
page.setLast(lastPage);
|
|
|
|
page.save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|