Skip to content

Commit

Permalink
Various steps to reduce amounts of watched paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Andersbakken committed Jun 11, 2013
1 parent f670437 commit 1917826
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 30 deletions.
38 changes: 15 additions & 23 deletions src/FileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ FileManager::FileManager()
void FileManager::init(const shared_ptr<Project> &proj)
{
mProject = proj;
recurseDirs();
reload();
}

void FileManager::recurseDirs()
void FileManager::reload()
{
shared_ptr<Project> project = mProject.lock();
assert(project);
Expand All @@ -27,7 +27,6 @@ void FileManager::recurseDirs()

void FileManager::onRecurseJobFinished(Set<Path> paths)
{
const bool watch = !(Server::instance()->options().options & Server::NoFileManagerWatch);
bool emitJS = false;
{
MutexLocker lock(&mMutex); // ### is this needed now?
Expand All @@ -37,6 +36,7 @@ void FileManager::onRecurseJobFinished(Set<Path> paths)
shared_ptr<Project> project = mProject.lock();
assert(project);
FilesMap &map = project->files();
map.clear();
mWatcher.clear();
for (Set<Path>::const_iterator it = paths.begin(); it != paths.end(); ++it) {
if (it->endsWith(".js"))
Expand All @@ -48,8 +48,8 @@ void FileManager::onRecurseJobFinished(Set<Path> paths)
}
assert(!parent.isEmpty());
Set<String> &dir = map[parent];
if (dir.isEmpty() && watch)
mWatcher.watch(parent);
if (dir.isEmpty())
watch(parent);
dir.insert(it->fileName());
}
assert(!map.contains(""));
Expand All @@ -71,7 +71,7 @@ void FileManager::onFileAdded(const Path &path)
const Filter::Result res = Filter::filter(path);
switch (res) {
case Filter::Directory:
recurseDirs();
reload();
return;
case Filter::Filtered:
return;
Expand All @@ -85,8 +85,8 @@ void FileManager::onFileAdded(const Path &path)
const Path parent = path.parentDir();
if (!parent.isEmpty()) {
Set<String> &dir = map[parent];
if (dir.isEmpty() && !(Server::instance()->options().options & Server::NoFileManagerWatch))
mWatcher.watch(parent);
if (dir.isEmpty())
watch(parent);
dir.insert(path.fileName());
emitJS = path.endsWith(".js");
} else {
Expand All @@ -104,16 +104,15 @@ void FileManager::onFileRemoved(const Path &path)
shared_ptr<Project> project = mProject.lock();
FilesMap &map = project->files();
if (map.contains(path)) {
recurseDirs();
reload();
return;
}
const Path parent = path.parentDir();
if (map.contains(parent)) {
Set<String> &dir = map[parent];
dir.remove(path.fileName());
if (dir.isEmpty()) {
if (!(Server::instance()->options().options & Server::NoFileManagerWatch))
mWatcher.unwatch(parent);
mWatcher.unwatch(parent);
map.remove(parent);
}
}
Expand All @@ -139,22 +138,15 @@ bool FileManager::contains(const Path &path) const
return false;
}

void FileManager::reload()
{
MutexLocker lock(&mMutex);
shared_ptr<Project> proj = mProject.lock();
FilesMap &map = proj->files();
map.clear();
recurseDirs();
}
Set<Path> FileManager::jsFiles() const
{
MutexLocker lock(&mMutex);
return mJSFiles;
}

void FileManager::clearFileSystemWatcher()
void FileManager::watch(const Path &path)
{
MutexLocker lock(&mMutex);
mWatcher.clear();
if (!(Server::instance()->options().options & Server::NoFileManagerWatch)
&& !path.contains("/.git/") && !path.contains("/.svn/") && !path.contains("/.cvs/")) {
mWatcher.watch(path);
}
}
7 changes: 4 additions & 3 deletions src/FileManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ class FileManager : public EventReceiver
public:
FileManager();
void init(const shared_ptr<Project> &proj);
void recurseDirs();
void reload();
void onFileAdded(const Path &path);
void onFileRemoved(const Path &path);
void onRecurseJobFinished(Set<Path> mPaths);
bool contains(const Path &path) const;
void clearFileSystemWatcher();
void reload();
void clearFileSystemWatcher() { mWatcher.clear(); }
Set<Path> watchedPaths() const { return mWatcher.watchedPaths(); }
Set<Path> jsFiles() const;
signalslot::Signal0 &jsFilesChanged() { return mJSFilesChanged; }
private:
void watch(const Path &path);
FileSystemWatcher mWatcher;
weak_ptr<Project> mProject;
signalslot::Signal0 mJSFilesChanged;
Expand Down
9 changes: 9 additions & 0 deletions src/Project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ Project::Project(const Path &path)
{
mWatcher.modified().connect(this, &Project::onFileModified);
mWatcher.removed().connect(this, &Project::onFileModified);
if (Server::instance()->options().options & Server::NoFileManagerWatch) {
mWatcher.removed().connect(this, &Project::reloadFileManager);
mWatcher.added().connect(this, &Project::reloadFileManager);
}
}

void Project::init()
Expand Down Expand Up @@ -822,3 +826,8 @@ void Project::onJSFilesAdded()
index(*it);
}
}

void Project::reloadFileManager(const Path &)
{
fileManager->reload();
}
1 change: 1 addition & 0 deletions src/Project.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class Project : public EventReceiver
bool isIndexing() const { MutexLocker lock(&mMutex); return !mJobs.isEmpty(); }
void onJSFilesAdded();
private:
void reloadFileManager(const Path &);
bool initJobFromCache(const Path &path, const List<String> &args,
CXIndex &index, CXTranslationUnit &unit, List<String> *argsOut);
void onFileModified(const Path &);
Expand Down
10 changes: 8 additions & 2 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,10 @@ shared_ptr<Project> Server::setCurrentProject(const Path &path) // lock always h

shared_ptr<Project> Server::setCurrentProject(const shared_ptr<Project> &project)
{
if (project && project != mCurrentProject.lock()) {
shared_ptr<Project> old = mCurrentProject.lock();
if (project && project != old) {
if (old)
old->fileManager->clearFileSystemWatcher();
mCurrentProject = project;
FILE *f = fopen((mOptions.dataDir + ".currentProject").constData(), "w");
if (f) {
Expand All @@ -911,8 +914,11 @@ shared_ptr<Project> Server::setCurrentProject(const shared_ptr<Project> &project
error() << "error opening" << (mOptions.dataDir + ".currentProject") << "for write";
}

if (!project->isValid())
if (!project->isValid()) {
loadProject(project);
} else {
project->fileManager->reload();
}
return project;
}
return shared_ptr<Project>();
Expand Down
10 changes: 9 additions & 1 deletion src/StatusJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,18 @@ void StatusJob::execute()
write(delimiter);
write("watchedpaths");
write(delimiter);
const Set<Path> watched = proj->watchedPaths();
Set<Path> watched = proj->watchedPaths();
write("Indexer");
for (Set<Path>::const_iterator it = watched.begin(); it != watched.end(); ++it) {
write<256>(" %s", it->constData());
}
if (proj->fileManager) {
write("FileManager");
watched = proj->fileManager->watchedPaths();
for (Set<Path>::const_iterator it = watched.begin(); it != watched.end(); ++it) {
write<256>(" %s", it->constData());
}
}
if (isAborted())
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/rct

0 comments on commit 1917826

Please sign in to comment.