diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index a37872a..3f8fd5c 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -26,4 +26,4 @@ jobs: - name: Build run: | - clang++ -o cmr_cache main.cpp -I./vendor/yaml -I/usr/local/include/boost -L/usr/local/lib -lboost_system -lpthread -Wmissing-declarations + clang++ -o cmr_cache main.cpp -I./vendor/yaml -I/usr/local/include/boost -L/usr/local/lib -lboost_system -lpthread -std=c++17 diff --git a/core/cache/cache.cpp b/core/cache/cache.cpp index b733781..6157da9 100644 --- a/core/cache/cache.cpp +++ b/core/cache/cache.cpp @@ -22,37 +22,41 @@ #include "cache.h" Cache::Cache(){ - std::ifstream file("data/databases.dat"); - if (file.is_open()){ - size_t cacheSize; - file.read(reinterpret_cast(&cacheSize), sizeof(cacheSize)); - - for (size_t i = 0; i < cacheSize; ++i) { - std::string key; - size_t keySize; - file.read(reinterpret_cast(&keySize), sizeof(keySize)); - key.resize(keySize); - file.read(&key[0], keySize); - - std::unordered_map map; - - size_t mapSize; - file.read(reinterpret_cast(&mapSize), sizeof(mapSize)); - - for (size_t f = 0; f < mapSize; ++f) { - std::string itemKey; - size_t itemKeySize; - file.read(reinterpret_cast(&itemKeySize), sizeof(itemKeySize)); - itemKey.resize(itemKeySize); - file.read(&itemKey[0], itemKeySize); - - CacheStruct cacheStruct; - cacheStruct.deserialize(file); - - map[itemKey] = cacheStruct; - } + for (const auto& row: std::filesystem::directory_iterator("data")){ + if (row.path().extension() != ".dat") continue; + + std::ifstream file(row.path(), std::ios::binary); + if (file.is_open()){ + size_t cacheSize; + file.read(reinterpret_cast(&cacheSize), sizeof(cacheSize)); + + for (size_t i = 0; i < cacheSize; ++i) { + std::string key; + size_t keySize; + file.read(reinterpret_cast(&keySize), sizeof(keySize)); + key.resize(keySize); + file.read(&key[0], keySize); + + std::unordered_map map; + + size_t mapSize; + file.read(reinterpret_cast(&mapSize), sizeof(mapSize)); + + for (size_t f = 0; f < mapSize; ++f) { + std::string itemKey; + size_t itemKeySize; + file.read(reinterpret_cast(&itemKeySize), sizeof(itemKeySize)); + itemKey.resize(itemKeySize); + file.read(&itemKey[0], itemKeySize); + + CacheStruct cacheStruct; + cacheStruct.deserialize(file); - cache_[key] = map; + map[itemKey] = cacheStruct; + } + + cache_[key] = map; + } } } } @@ -101,15 +105,19 @@ std::vector Cache::keys(std::string db){ } void Cache::save(){ - std::ofstream file("data/databases.dat"); + std::filesystem::create_directory("data"); size_t cacheSize = cache_.size(); - file.write(reinterpret_cast(&cacheSize), sizeof(cacheSize)); for (const auto& row: cache_) { const std::string& key = row.first; const std::unordered_map& map = row.second; + std::string filename = "data/" + key + ".dat"; + std::ofstream file(filename, std::ios::binary); + + file.write(reinterpret_cast(&cacheSize), sizeof(cacheSize)); + size_t keySize = key.size(); file.write(reinterpret_cast(&keySize), sizeof(keySize)); file.write(key.data(), keySize); @@ -127,6 +135,8 @@ void Cache::save(){ cachedata.serialize(file); } + + file.close(); } }