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