From 7fcceaeb00eac9435dc8e9b3343d0d4ba9495771 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kecsk=C3=A9s=20Kriszti=C3=A1n?= <kk1205@sch.bme.hu> Date: Thu, 26 Nov 2015 20:35:52 +0100 Subject: [PATCH] Nocache fix, FIFO fix, LRU impl, LFU impl --- program/Source/app/Application.cpp | 1 - program/Source/cache/BasicCache.cpp | 64 ----------------- program/Source/cache/BasicCache.h | 30 -------- program/Source/cache/Cache.h | 8 +++ program/Source/cache/FIFOCache.cpp | 34 +++++---- program/Source/cache/FIFOCache.h | 3 +- program/Source/cache/LFUCache.cpp | 105 +++++++++++++++++++++------ program/Source/cache/LFUCache.h | 6 +- program/Source/cache/LRUCache.cpp | 108 ++++++++++++++++++++++------ program/Source/cache/LRUCache.h | 6 +- program/Source/cache/NoCache.cpp | 6 +- program/Source/cache/NoCache.h | 1 + 12 files changed, 206 insertions(+), 166 deletions(-) delete mode 100644 program/Source/cache/BasicCache.cpp delete mode 100644 program/Source/cache/BasicCache.h diff --git a/program/Source/app/Application.cpp b/program/Source/app/Application.cpp index db0d9a9d..1e93aae5 100644 --- a/program/Source/app/Application.cpp +++ b/program/Source/app/Application.cpp @@ -5,7 +5,6 @@ #include "../cloud/DropboxAdapter.h" #include "../cloud/FTPAdapter.h" #include "../cache/NoCache.h" -#include "../cache/BasicCache.h" #include "../cache/FIFOCache.h" #include "../cache/LRUCache.h" #include "../cache/LFUCache.h" diff --git a/program/Source/cache/BasicCache.cpp b/program/Source/cache/BasicCache.cpp deleted file mode 100644 index c8a5a222..00000000 --- a/program/Source/cache/BasicCache.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include "BasicCache.h" -#include "../logger/Logger.h" -#include <fstream> - -#include <boost/filesystem.hpp> - -using namespace std; -using namespace boost::filesystem; - -/** - * BasicCache implementation - */ - -ReturnableFile BasicCache::getFile(std::string localFileID) { - LOG_ENTER_EXIT; - ReturnableFile fileFromCache(localFileID); - - //copy file from cache to source - ifstream src(cacheFolder + localFileID, ios::binary); - if (src) { - ofstream dst(sourceFolder + localFileID, ios::binary); - if (dst) { - dst << src.rdbuf(); - dst.close(); - fileFromCache.setValid(true); - } - else { - fileFromCache.setErrorMessage("Cache output could not be initialized."); - } - src.close(); - } - else { - fileFromCache.setErrorMessage("Cache source could not be initialized."); - } - - return fileFromCache; -} - -bool BasicCache::addFile(std::string localFileID) { - LOG_ENTER_EXIT; - - path cacheLocation = cacheFolder + localFileID; - path cacheParentDir = cacheLocation.parent_path(); - - if (!exists(cacheParentDir)) - { - create_directories(cacheParentDir); - } - - bool successful = false; - - //copy file to cache - ifstream src(sourceFolder + localFileID, ios::binary); - if (src) { - ofstream dst(cacheFolder + localFileID, ios::binary); - if (dst) { - dst << src.rdbuf(); - dst.close(); - successful = true; - } - src.close(); - } - return successful; -} \ No newline at end of file diff --git a/program/Source/cache/BasicCache.h b/program/Source/cache/BasicCache.h deleted file mode 100644 index d7a9ea3f..00000000 --- a/program/Source/cache/BasicCache.h +++ /dev/null @@ -1,30 +0,0 @@ -/** - * Project Untitled - */ - - -#ifndef _BASICCACHE_H -#define _BASICCACHE_H - -#include "Cache.h" - -/** - * BasicCache class - * implenents Cache interface - */ -class BasicCache : public Cache { -public: - BasicCache(int size) { maxSize = size; } - - /** - * Tries to get a file from the cache - */ - ReturnableFile getFile(std::string localFileID); - - /** - * Adds a file to the cache - */ - bool addFile(std::string localFileID); -}; - -#endif //_BASICCACHE_H \ No newline at end of file diff --git a/program/Source/cache/Cache.h b/program/Source/cache/Cache.h index 71b4b269..a02c9c9a 100644 --- a/program/Source/cache/Cache.h +++ b/program/Source/cache/Cache.h @@ -1,8 +1,14 @@ #ifndef _CACHE_H #define _CACHE_H +#include <boost/filesystem.hpp> #include "CacheFile.h" #include "../fileModel/ReturnableFile.h" +#include <fstream> +#include <sstream> + + +using namespace boost::filesystem; /** * Cache class @@ -12,6 +18,8 @@ class Cache { protected: int maxSize; int realSize; + int baseValue; + bool readAble; CacheFile* files; std::string sourceFolder; std::string cacheFolder; diff --git a/program/Source/cache/FIFOCache.cpp b/program/Source/cache/FIFOCache.cpp index 18654d11..9fed0106 100644 --- a/program/Source/cache/FIFOCache.cpp +++ b/program/Source/cache/FIFOCache.cpp @@ -1,9 +1,5 @@ #include "FIFOCache.h" #include "../logger/Logger.h" -#include <fstream> -#include <sstream> - -#include <boost/filesystem.hpp> using namespace std; using namespace boost::filesystem; @@ -14,8 +10,13 @@ using namespace boost::filesystem; FIFOCache::FIFOCache(int _size) { maxSize = _size; - realSize = 0; + realSize = 0; + baseValue = 0; + readAble = true; files = new CacheFile[maxSize]; + for (int i = 0; i < maxSize; i++){ + files[i] = CacheFile("", baseValue); + } } FIFOCache::~FIFOCache() @@ -122,20 +123,23 @@ string FIFOCache::toString() { for (int i = 0; i < realSize; i++) { ss << files[i].getFileId() << " "; } - + ss << endl; return ss.str(); } void FIFOCache::readCache() { - path p = cacheFolder; - if (exists(p) && is_directory(p)) - { - vector<path> v; - copy(directory_iterator(p), directory_iterator(), back_inserter(v)); - for (int i = 0; i < v.size(); i++) + if (readAble) { + path p = cacheFolder; + if (exists(p) && is_directory(p)) { - path p = v.at(i).filename(); - files[i] = CacheFile(p.string(), 0); - } + vector<path> v; + copy(directory_iterator(p), directory_iterator(), back_inserter(v)); + for (unsigned int i = 0; i < v.size(); i++) + { + path p = v.at(i).filename(); + files[i] = CacheFile(p.string(), baseValue); + realSize++; + } + } } } \ No newline at end of file diff --git a/program/Source/cache/FIFOCache.h b/program/Source/cache/FIFOCache.h index 4174db89..f92f4a46 100644 --- a/program/Source/cache/FIFOCache.h +++ b/program/Source/cache/FIFOCache.h @@ -26,10 +26,9 @@ public: * Adds a file to the cache */ bool addFile(std::string localFileID); - - void readCache(); bool checkFile(std::string id); string toString(); + void readCache(); }; #endif //_FIFOCACHE_H \ No newline at end of file diff --git a/program/Source/cache/LFUCache.cpp b/program/Source/cache/LFUCache.cpp index 2b94cf24..ef223028 100644 --- a/program/Source/cache/LFUCache.cpp +++ b/program/Source/cache/LFUCache.cpp @@ -1,8 +1,5 @@ #include "LFUCache.h" #include "../logger/Logger.h" -#include <fstream> - -#include <boost/filesystem.hpp> using namespace std; using namespace boost::filesystem; @@ -14,11 +11,13 @@ using namespace boost::filesystem; LFUCache::LFUCache(int _size) { maxSize = _size; - realSize = 0; + realSize = 0; + baseValue = -1; + readAble = true; files = new CacheFile[maxSize]; for (int i = 0; i < maxSize; i++){ - files[i] = CacheFile("", -1); + files[i] = CacheFile("", baseValue); } } @@ -39,6 +38,13 @@ ReturnableFile LFUCache::getFile(std::string localFileID) { dst << src.rdbuf(); dst.close(); fileFromCache.setValid(true); + + for (int idx = 0; idx < maxSize; idx++) { + if (files[idx].getFileId() == localFileID) { + files[idx].used++; + break; + } + } } else { fileFromCache.setErrorMessage("Cache output could not be initialized."); @@ -54,28 +60,51 @@ ReturnableFile LFUCache::getFile(std::string localFileID) { bool LFUCache::addFile(std::string localFileID) { LOG_ENTER_EXIT; + bool successful = true; + if (!checkFile(localFileID)){ + successful = false; + path cacheLocation = cacheFolder + localFileID; + path cacheParentDir = cacheLocation.parent_path(); - path cacheLocation = cacheFolder + localFileID; - path cacheParentDir = cacheLocation.parent_path(); + if (!exists(cacheParentDir)) + { + create_directories(cacheParentDir); + } - if (!exists(cacheParentDir)) - { - create_directories(cacheParentDir); - } + //copy file to cache + ifstream src(sourceFolder + localFileID, ios::binary); + if (src) { + ofstream dst(cacheFolder + localFileID, ios::binary); + if (dst) { + int minAge = 2147483647; + for (int i = 0; i < minAge; i++) + { + if (files[i].used < minAge) { + minAge = files[i].used; + } + } - bool successful = false; + for (int i = 0; i < maxSize; i++) { + if (files[i].used == minAge) { + // tele van, ez�rt az utols�t kit�r�lj�k + string spath = cacheFolder + files[i].getFileId(); + const char* path = spath.c_str(); + int res = remove(path); - //copy file to cache - ifstream src(sourceFolder + localFileID, ios::binary); - if (src) { - ofstream dst(cacheFolder + localFileID, ios::binary); - if (dst) { - dst << src.rdbuf(); - dst.close(); - successful = true; + files[i] = CacheFile(localFileID, 0); + break; + } + } + + dst << src.rdbuf(); + dst.close(); + successful = true; + + cout << toString(); + } + src.close(); } - src.close(); - } + } return successful; } @@ -86,4 +115,36 @@ bool LFUCache::checkFile(string id){ } } return false; +} + +string LFUCache::toString() { + ostringstream ss; + ss << "Files: "; + for (int i = 0; i < maxSize; i++) { + ss << files[i].getFileId() << " "; + } + ss << " Used: "; + for (int i = 0; i < maxSize; i++) { + ss << files[i].used << " "; + } + ss << endl; + return ss.str(); +} + +void LFUCache::readCache() { + if (readAble) { + path p = cacheFolder; + if (exists(p) && is_directory(p)) + { + vector<path> v; + copy(directory_iterator(p), directory_iterator(), back_inserter(v)); + for (unsigned int i = 0; i < v.size(); i++) + { + path p = v.at(i).filename(); + files[i] = CacheFile(p.string(), 0); + realSize++; + } + } + } + cout << toString(); } \ No newline at end of file diff --git a/program/Source/cache/LFUCache.h b/program/Source/cache/LFUCache.h index 592479d0..ae325609 100644 --- a/program/Source/cache/LFUCache.h +++ b/program/Source/cache/LFUCache.h @@ -26,11 +26,9 @@ public: * Adds a file to the cache */ bool addFile(std::string localFileID); - - + string toString(); bool checkFile(std::string id); - - void readCache() {} + void readCache(); }; #endif //_LFUCACHE_H \ No newline at end of file diff --git a/program/Source/cache/LRUCache.cpp b/program/Source/cache/LRUCache.cpp index bf933e87..c1ba7a9a 100644 --- a/program/Source/cache/LRUCache.cpp +++ b/program/Source/cache/LRUCache.cpp @@ -1,8 +1,5 @@ #include "LRUCache.h" #include "../logger/Logger.h" -#include <fstream> - -#include <boost/filesystem.hpp> using namespace std; using namespace boost::filesystem; @@ -15,10 +12,12 @@ LRUCache::LRUCache(int _size) { maxSize = _size; realSize = 0; + baseValue = 1; + readAble = true; files = new CacheFile[maxSize]; for (int i = 0; i < maxSize; i++){ - files[i] = CacheFile("", 1); + files[i] = CacheFile("", baseValue); } } @@ -39,43 +38,75 @@ ReturnableFile LRUCache::getFile(std::string localFileID) { dst << src.rdbuf(); dst.close(); fileFromCache.setValid(true); + + for (int idx = 0; idx < maxSize; idx++) { + if (files[idx].getFileId() == localFileID) { + for (int i = 0; i < maxSize; i++){ + files[i].used++; + } + files[idx].used = 0; + } + } } else { fileFromCache.setErrorMessage("Cache output could not be initialized."); } - src.close(); + src.close(); } else { fileFromCache.setErrorMessage("Cache source could not be initialized."); } - + return fileFromCache; } bool LRUCache::addFile(std::string localFileID) { LOG_ENTER_EXIT; + bool successful = true; + if (!checkFile(localFileID)){ + successful = false; + path cacheLocation = cacheFolder + localFileID; + path cacheParentDir = cacheLocation.parent_path(); - path cacheLocation = cacheFolder + localFileID; - path cacheParentDir = cacheLocation.parent_path(); + if (!exists(cacheParentDir)) + { + create_directories(cacheParentDir); + } - if (!exists(cacheParentDir)) - { - create_directories(cacheParentDir); - } + //copy file to cache + ifstream src(sourceFolder + localFileID, ios::binary); + if (src) { + ofstream dst(cacheFolder + localFileID, ios::binary); + if (dst) { + int maxAge = 0; + for (int i = 0; i < maxSize; i++) + { + if (files[i].used > maxAge) { + maxAge = files[i].used; + } + } - bool successful = false; + for (int i = 0; i < maxSize; i++) { + if (files[i].used == maxAge) { + // tele van, ez�rt az utols�t kit�r�lj�k + string spath = cacheFolder + files[i].getFileId(); + const char* path = spath.c_str(); + int res = remove(path); - //copy file to cache - ifstream src(sourceFolder + localFileID, ios::binary); - if (src) { - ofstream dst(cacheFolder + localFileID, ios::binary); - if (dst) { - dst << src.rdbuf(); - dst.close(); - successful = true; + files[i] = CacheFile(localFileID, 0); + break; + } + } + + dst << src.rdbuf(); + dst.close(); + successful = true; + + cout << toString(); + } + src.close(); } - src.close(); - } + } return successful; } @@ -86,4 +117,35 @@ bool LRUCache::checkFile(string id){ } } return false; +} + +string LRUCache::toString() { + ostringstream ss; + ss << "Files: "; + for (int i = 0; i < maxSize; i++) { + ss << files[i].getFileId() << " "; + } + ss << " Used: "; + for (int i = 0; i < maxSize; i++) { + ss << files[i].used << " "; + } + ss << endl; + return ss.str(); +} + +void LRUCache::readCache() { + if (readAble) { + path p = cacheFolder; + if (exists(p) && is_directory(p)) + { + vector<path> v; + copy(directory_iterator(p), directory_iterator(), back_inserter(v)); + for (unsigned int i = 0; i < v.size(); i++) + { + path p = v.at(i).filename(); + files[i] = CacheFile(p.string(), 0); + realSize++; + } + } + } } \ No newline at end of file diff --git a/program/Source/cache/LRUCache.h b/program/Source/cache/LRUCache.h index 0853e242..62525aac 100644 --- a/program/Source/cache/LRUCache.h +++ b/program/Source/cache/LRUCache.h @@ -26,11 +26,9 @@ public: * Adds a file to the cache */ bool addFile(std::string localFileID); - - + string toString(); bool checkFile(std::string id); - - void readCache() {} + void readCache(); }; #endif //_LRUCACHE_H \ No newline at end of file diff --git a/program/Source/cache/NoCache.cpp b/program/Source/cache/NoCache.cpp index ba51cd9e..38c31a07 100644 --- a/program/Source/cache/NoCache.cpp +++ b/program/Source/cache/NoCache.cpp @@ -5,6 +5,10 @@ * NoCache implementation */ +NoCache::NoCache() { + readAble = false; +} + ReturnableFile NoCache::getFile(std::string localFileID) { LOG_ENTER_EXIT; ReturnableFile fileFromCache(localFileID); @@ -13,5 +17,5 @@ ReturnableFile NoCache::getFile(std::string localFileID) { bool NoCache::addFile(std::string localFileID) { LOG_ENTER_EXIT; - return false; + return true; } \ No newline at end of file diff --git a/program/Source/cache/NoCache.h b/program/Source/cache/NoCache.h index b413a4c4..a1de2ee5 100644 --- a/program/Source/cache/NoCache.h +++ b/program/Source/cache/NoCache.h @@ -14,6 +14,7 @@ */ class NoCache : public Cache { public: + NoCache(); /** * Tries to get a file from the cache */ -- GitLab