From 99e16b7cf676d2c7d168792fd794342ae28db3bc Mon Sep 17 00:00:00 2001 From: YuhangQ Date: Wed, 3 Nov 2021 09:39:13 +0800 Subject: [PATCH] add cache --- .idea/.name | 1 + .idea/misc.xml | 4 +++ CMakeLists.txt | 2 +- invodb/file/page_manager.cpp | 11 +++++++- invodb/file/page_manager.h | 4 ++- invodb/main.cpp | 11 +++----- invodb/models/cache.h | 52 ++++++++++++++++++++++++++++++++++++ 7 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 .idea/.name create mode 100644 .idea/misc.xml create mode 100644 invodb/models/cache.h diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..96275ad --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +InvoDB \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..79b3c94 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 778b760..d741eb3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/invodb/file/page_manager.cpp b/invodb/file/page_manager.cpp index 0b79ccb..f4c911e 100644 --- a/invodb/file/page_manager.cpp +++ b/invodb/file/page_manager.cpp @@ -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() { diff --git a/invodb/file/page_manager.h b/invodb/file/page_manager.h index 0b3aa90..0382015 100644 --- a/invodb/file/page_manager.h +++ b/invodb/file/page_manager.h @@ -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 map; std::fstream stream; + LRUCache cache; // 私有化实现单例 - PageManager() {} + PageManager():cache(LRUCache(50000)) {} ~PageManager() {} PageManager(const PageManager&); PageManager& operator=(const PageManager&); diff --git a/invodb/main.cpp b/invodb/main.cpp index 3740796..f523155 100644 --- a/invodb/main.cpp +++ b/invodb/main.cpp @@ -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; } diff --git a/invodb/models/cache.h b/invodb/models/cache.h new file mode 100644 index 0000000..4805a44 --- /dev/null +++ b/invodb/models/cache.h @@ -0,0 +1,52 @@ +// +// Created by i on 2021/11/2. +// + +#ifndef INVODBCACHEH +#define INVODBCACHEH + +#include +#include + +template +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> ls; + std::unordered_map>::iterator> hash; +}; + + + +#endif //INVODBCACHEH