add cache

This commit is contained in:
YuhangQ 2021-11-03 09:39:13 +08:00
parent 7e8d6d8e5a
commit 99e16b7cf6
7 changed files with 75 additions and 10 deletions

1
.idea/.name generated Normal file
View File

@ -0,0 +1 @@
InvoDB

4
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>

View File

@ -11,4 +11,4 @@ include_directories(./invodb)
add_executable(InvoDB
invodb/main.cpp
invodb/main.h invodb/file/page_manager.cpp invodb/file/page_manager.h invodb/models/collection.cpp invodb/models/collection.h invodb/file/storage_page.cpp invodb/file/storage_page.h invodb/utils/logger.h invodb/utils/uuid.h invodb/btree/node.h invodb/btree/btree.h invodb/index/index.cpp invodb/index/index.h invodb/btree/list.h invodb/index/query.cpp invodb/index/query.h)
invodb/main.h invodb/file/page_manager.cpp invodb/file/page_manager.h invodb/models/collection.cpp invodb/models/collection.h invodb/file/storage_page.cpp invodb/file/storage_page.h invodb/utils/logger.h invodb/utils/uuid.h invodb/btree/node.h invodb/btree/btree.h invodb/index/index.cpp invodb/index/index.h invodb/btree/list.h invodb/index/query.cpp invodb/index/query.h invodb/models/cache.h)

View File

@ -10,6 +10,13 @@ int PageManager::loadDatabase(const char *filename) {
}
StoragePage PageManager::getPage(const int &index) {
if(cache.exist(index)) {
static int cnt = 0;
printf("%d\n", ++cnt);
return cache.get(index);
}
StoragePage page(index);
// 调整指针位置
stream.clear();
@ -19,10 +26,12 @@ StoragePage PageManager::getPage(const int &index) {
}
void PageManager::setPage(const int &index, const StoragePage &page) {
cache.put(index, page);
stream.clear();
stream.seekg(index * 1024);
stream.write(page, 1024);
stream.flush();
}
int PageManager::allocate() {

View File

@ -11,6 +11,7 @@
#include "storage_page.h"
#include "json/json.hpp"
#include "models/cache.h"
class PageManager {
public:
@ -28,8 +29,9 @@ public:
private:
std::map<int, StoragePage> map;
std::fstream stream;
LRUCache<int, StoragePage> cache;
// 私有化实现单例
PageManager() {}
PageManager():cache(LRUCache<int, StoragePage>(50000)) {}
~PageManager() {}
PageManager(const PageManager&);
PageManager& operator=(const PageManager&);

View File

@ -29,7 +29,7 @@ int main() {
col = &Collection::getCollection("hello");
}
//testAndBenchmark(10000);
nlohmann::json j = nlohmann::json::parse(R"(
@ -42,19 +42,16 @@ int main() {
"id": 3
},
"array": ["1", "2", "3"]
}
)");
try {
col->insert(j);
}catch(const char * s ){
puts(s);
}
printf("?");
col->remove(j);
testAndBenchmark(100000);
return 0;
}

52
invodb/models/cache.h Normal file
View File

@ -0,0 +1,52 @@
//
// Created by i on 2021/11/2.
//
#ifndef INVODBCACHEH
#define INVODBCACHEH
#include <list>
#include <map>
template<typename KT, typename VT>
class LRUCache {
public:
LRUCache(int capacity) : capacity(capacity) {}
bool exist(KT const &key) {
return hash.find(key) != hash.end();
}
VT get(KT const &key) {
if (hash.find(key) == hash.end())
throw "cache error";
else {
VT value = hash[key]->second;
ls.erase(hash[key]);
ls.push_front(std::make_pair(key, value));
hash[key] = ls.begin();
return value;
}
}
void put(KT const &key, VT const &value) {
if (hash.find(key) != hash.end()) {
ls.erase(hash[key]);
}
else if (ls.size() >= capacity) {
hash.erase(ls.back().first);
ls.pop_back();
}
ls.push_front(std::make_pair(key, value));
hash[key] = ls.begin();
}
private:
int capacity;
std::list<std::pair<KT, VT>> ls;
std::unordered_map<KT, typename std::list<std::pair<KT, VT>>::iterator> hash;
};
#endif //INVODBCACHEH