data: refreshing with DB
* database stores modification time of files. * FileManager updates internal state based on database info. * reimplemented FileManager's check if a file is in project. * implemented clearing data of updated or removed files before reparsing the updated and added files.
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
#include "FileInfo.h"
|
||||
|
||||
FileInfo::FileInfo()
|
||||
: path(FilePath(""))
|
||||
, lastWriteTime(boost::posix_time::not_a_date_time)
|
||||
{
|
||||
}
|
||||
|
||||
FileInfo::FileInfo(const FilePath& path, boost::posix_time::ptime lastWriteTime)
|
||||
: path(path)
|
||||
, lastWriteTime(lastWriteTime)
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
struct FileInfo
|
||||
{
|
||||
FileInfo();
|
||||
FileInfo(const FilePath& path, boost::posix_time::ptime lastWriteTime);
|
||||
|
||||
FilePath path;
|
||||
|
||||
@@ -4,8 +4,10 @@
|
||||
#include <set>
|
||||
|
||||
#include "utility/file/FileSystem.h"
|
||||
#include "utility/utility.h"
|
||||
|
||||
FileManager::FileManager()
|
||||
FileManager::FileManager(StorageAccessProxy* storageAccessProxy)
|
||||
: m_storageAccessProxy(storageAccessProxy)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -35,58 +37,42 @@ void FileManager::setPaths(
|
||||
m_includeExtensions = includeExtensions;
|
||||
}
|
||||
|
||||
void FileManager::reset()
|
||||
{
|
||||
m_files.clear();
|
||||
m_addedFiles.clear();
|
||||
m_updatedFiles.clear();
|
||||
m_removedFiles.clear();
|
||||
}
|
||||
|
||||
void FileManager::fetchFilePaths()
|
||||
{
|
||||
std::map<FilePath, FileInfo> files;
|
||||
for (FileInfo oldFileInfo: m_storageAccessProxy->getInfoOnAllFiles())
|
||||
{
|
||||
files[oldFileInfo.path] = oldFileInfo;
|
||||
}
|
||||
|
||||
m_addedFiles.clear();
|
||||
m_updatedFiles.clear();
|
||||
m_removedFiles.clear();
|
||||
|
||||
for (std::map<FilePath, FileInfo>::iterator it = m_files.begin(); it != m_files.end(); it++)
|
||||
for (std::map<FilePath, FileInfo>::iterator it = files.begin(); it != files.end(); it++)
|
||||
{
|
||||
m_removedFiles.insert(it->first);
|
||||
}
|
||||
|
||||
std::vector<std::pair<std::vector<FilePath>, std::vector<std::string>>> pathsExtensionsPairs;
|
||||
pathsExtensionsPairs.push_back(std::make_pair(m_includePaths, m_includeExtensions));
|
||||
pathsExtensionsPairs.push_back(std::make_pair(m_sourcePaths, m_sourceExtensions));
|
||||
|
||||
for (size_t i = 0; i < pathsExtensionsPairs.size(); i++)
|
||||
std::vector<FileInfo> fileInfos = getFileInfosInProject();
|
||||
for (FileInfo fileInfo: fileInfos)
|
||||
{
|
||||
std::vector<FileInfo> fileInfos =
|
||||
FileSystem::getFileInfosFromPaths(pathsExtensionsPairs[i].first, pathsExtensionsPairs[i].second);
|
||||
|
||||
for (FileInfo fileInfo: fileInfos)
|
||||
const FilePath& filePath = fileInfo.path;
|
||||
std::map<FilePath, FileInfo>::iterator it = files.find(filePath);
|
||||
if (it != files.end())
|
||||
{
|
||||
const FilePath& filePath = fileInfo.path;
|
||||
std::map<FilePath, FileInfo>::iterator it = m_files.find(filePath);
|
||||
if (it != m_files.end())
|
||||
m_removedFiles.erase(filePath);
|
||||
if (fileInfo.lastWriteTime > it->second.lastWriteTime)
|
||||
{
|
||||
m_removedFiles.erase(filePath);
|
||||
if (fileInfo.lastWriteTime > it->second.lastWriteTime)
|
||||
{
|
||||
it->second.lastWriteTime = fileInfo.lastWriteTime;
|
||||
m_updatedFiles.insert(fileInfo.path);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
m_files.insert(std::pair<FilePath, FileInfo>(filePath, fileInfo));
|
||||
m_addedFiles.insert(filePath);
|
||||
it->second.lastWriteTime = fileInfo.lastWriteTime;
|
||||
m_updatedFiles.insert(fileInfo.path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const FilePath& filePath : m_removedFiles)
|
||||
{
|
||||
m_files.erase(filePath);
|
||||
else
|
||||
{
|
||||
files.insert(std::pair<FilePath, FileInfo>(filePath, fileInfo));
|
||||
m_addedFiles.insert(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,7 +93,16 @@ std::set<FilePath> FileManager::getRemovedFilePaths() const
|
||||
|
||||
bool FileManager::hasFilePath(const FilePath& filePath) const
|
||||
{
|
||||
return (m_files.find(filePath) != m_files.end());
|
||||
std::vector<FileInfo> fileInfos = getFileInfosInProject();
|
||||
for (size_t i = 0; i < fileInfos.size(); i++)
|
||||
{
|
||||
if (fileInfos[i].path == filePath)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool FileManager::hasSourceExtension(const FilePath& filePath) const
|
||||
@@ -119,3 +114,22 @@ bool FileManager::hasIncludeExtension(const FilePath& filePath) const
|
||||
{
|
||||
return filePath.hasExtension(m_includeExtensions);
|
||||
}
|
||||
|
||||
std::vector<FileInfo> FileManager::getFileInfosInProject() const
|
||||
{
|
||||
std::vector<FileInfo> fileInfos;
|
||||
|
||||
std::vector<std::pair<std::vector<FilePath>, std::vector<std::string>>> pathsExtensionsPairs;
|
||||
pathsExtensionsPairs.push_back(std::make_pair(m_includePaths, m_includeExtensions));
|
||||
pathsExtensionsPairs.push_back(std::make_pair(m_sourcePaths, m_sourceExtensions));
|
||||
|
||||
for (size_t i = 0; i < pathsExtensionsPairs.size(); i++)
|
||||
{
|
||||
utility::append(
|
||||
fileInfos,
|
||||
FileSystem::getFileInfosFromPaths(pathsExtensionsPairs[i].first, pathsExtensionsPairs[i].second)
|
||||
);
|
||||
}
|
||||
|
||||
return fileInfos;
|
||||
}
|
||||
|
||||
@@ -5,12 +5,13 @@
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
#include "FileInfo.h"
|
||||
#include "data/access/StorageAccessProxy.h"
|
||||
#include "utility/file/FileInfo.h"
|
||||
|
||||
class FileManager
|
||||
{
|
||||
public:
|
||||
FileManager();
|
||||
FileManager(StorageAccessProxy* storageAccessProxy);
|
||||
virtual ~FileManager();
|
||||
|
||||
const std::vector<FilePath>& getSourcePaths() const;
|
||||
@@ -23,7 +24,6 @@ public:
|
||||
std::vector<std::string> includeExtensions
|
||||
);
|
||||
|
||||
void reset();
|
||||
void fetchFilePaths();
|
||||
|
||||
std::set<FilePath> getAddedFilePaths() const;
|
||||
@@ -35,15 +35,18 @@ public:
|
||||
virtual bool hasIncludeExtension(const FilePath& filePath) const;
|
||||
|
||||
private:
|
||||
std::vector<FileInfo> getFileInfosInProject() const;
|
||||
|
||||
std::vector<FilePath> m_sourcePaths;
|
||||
std::vector<FilePath> m_includePaths;
|
||||
std::vector<std::string> m_sourceExtensions;
|
||||
std::vector<std::string> m_includeExtensions;
|
||||
|
||||
std::map<FilePath, FileInfo> m_files;
|
||||
std::set<FilePath> m_addedFiles;
|
||||
std::set<FilePath> m_updatedFiles;
|
||||
std::set<FilePath> m_removedFiles;
|
||||
|
||||
StorageAccessProxy* m_storageAccessProxy;
|
||||
};
|
||||
|
||||
#endif // FILE_MANAGER_H
|
||||
|
||||
@@ -52,7 +52,16 @@ std::vector<std::string> FileSystem::getFileNamesFromDirectoryUpdatedAfter(
|
||||
return files;
|
||||
}
|
||||
|
||||
|
||||
FileInfo FileSystem::getFileInfoForPath(FilePath filePath)
|
||||
{
|
||||
if (filePath.exists())
|
||||
{
|
||||
std::time_t t = boost::filesystem::last_write_time(filePath.path());
|
||||
boost::posix_time::ptime lastWriteTime = boost::posix_time::from_time_t(t);
|
||||
return FileInfo(filePath, lastWriteTime);
|
||||
}
|
||||
return FileInfo();
|
||||
}
|
||||
|
||||
std::vector<FileInfo> FileSystem::getFileInfosFromPaths(
|
||||
const std::vector<FilePath>& paths, const std::vector<std::string>& fileExtensions
|
||||
@@ -85,7 +94,7 @@ std::vector<FileInfo> FileSystem::getFileInfosFromPaths(
|
||||
return files;
|
||||
}
|
||||
|
||||
std::string FileSystem::getTimeStringNow()
|
||||
std::string FileSystem::getTimeStringNow() // TODO: move to utility
|
||||
{
|
||||
return boost::posix_time::to_iso_string(boost::posix_time::second_clock::universal_time());
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@ public:
|
||||
static std::vector<std::string> getFileNamesFromDirectoryUpdatedAfter(
|
||||
const std::string& path, const std::vector<std::string>& extensions, const std::string& timeString);
|
||||
|
||||
static FileInfo getFileInfoForPath(FilePath filePath);
|
||||
|
||||
static std::vector<FileInfo> getFileInfosFromPaths(
|
||||
const std::vector<FilePath>& paths, const std::vector<std::string>& fileExtensions);
|
||||
|
||||
|
||||
@@ -21,6 +21,23 @@ float utility::duration(std::function<void()> func)
|
||||
return duration(start);
|
||||
}
|
||||
|
||||
std::string utility::timeToString(const time_t time)
|
||||
{
|
||||
char buff[20];
|
||||
strftime(buff, 20, "%Y-%m-%d %H:%M:%S", localtime(&time));
|
||||
return std::string(buff);
|
||||
}
|
||||
|
||||
std::string utility::timeToString(const boost::posix_time::ptime time)
|
||||
{
|
||||
std::stringstream stream;
|
||||
boost::posix_time::time_facet* facet = new boost::posix_time::time_facet();
|
||||
facet->format("%Y-%m-%d %H:%M:%S");
|
||||
stream.imbue(std::locale(std::locale::classic(), facet));
|
||||
stream << time;
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
bool utility::intersectionPoint(Vec2f a1, Vec2f b1, Vec2f a2, Vec2f b2, Vec2f* i)
|
||||
{
|
||||
Vec2f p = a1;
|
||||
|
||||
@@ -4,6 +4,9 @@
|
||||
#include <deque>
|
||||
#include <chrono>
|
||||
#include <set>
|
||||
#include <time.h>
|
||||
|
||||
#include "boost/date_time/posix_time/posix_time.hpp"
|
||||
|
||||
#include "utility/math/Vector2.h"
|
||||
|
||||
@@ -15,6 +18,9 @@ namespace utility
|
||||
float duration(const TimePoint& start);
|
||||
float duration(std::function<void()> func);
|
||||
|
||||
std::string timeToString(const time_t time);
|
||||
std::string timeToString(const boost::posix_time::ptime time);
|
||||
|
||||
template<typename T>
|
||||
std::vector<T> concat(const std::vector<T>& a, const std::vector<T>& b);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user