diff --git a/program/Source/app/Application.cpp b/program/Source/app/Application.cpp index db0d9a9d076f957bd75f6c1ea96e0b3feee8e9b3..1e93aae58cad4606ef8a941a0af2476a0dcb44dc 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 c8a5a22237781cf8963038ebd6a412ed25356fe6..0000000000000000000000000000000000000000 --- 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 d7a9ea3fa728dc0885afe38cd3fbb12245debbd1..0000000000000000000000000000000000000000 --- 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 71b4b2699da919e649963d6b2c76a87426a9327e..a02c9c9a4d27e4653978ebf24ec3b6cdfd0a7983 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 18654d1170f3213d0111ecb05826686b7807b6a4..9fed0106e17c93f4df19dd6c4a01aab77aa88faf 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 4174db890b1c77f2d51984a95b734a81d4b5dba8..f92f4a46693df9ef3307a91bca14799312ed714d 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 2b94cf24529a05f54815286243cddbd409b5d2d6..ef223028adc6fb266269614165cbdac5b5c79b80 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 592479d0d32103f39f5625c5ad85041924f2d3b8..ae3256090bf57154bf1f0c590088c6c0fc56df5c 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 bf933e8787853127d261113cd0681152b62545cd..c1ba7a9a58125b59d8e84c9ac58e6589ff4132cb 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 0853e242696f540bb6ad084263d02286a6c5b736..62525aac02a03c2230356df72ae6b744ef789289 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 ba51cd9e8958954c19373324014a7b28daab74a1..38c31a07b7c6f281ea8c01505d21f008bd8ef41d 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 b413a4c4ebe4a19bdc87ecfdfd98013f0db6debb..a1de2ee5a4838c7674c239c25c9ee2c8838dae0c 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 */