2017-12-29 17:27:56 +00:00
|
|
|
#include "cache_manager.h"
|
2018-01-20 19:34:37 +00:00
|
|
|
#include "match.h"
|
2017-12-06 03:32:33 +00:00
|
|
|
#include "message_handler.h"
|
|
|
|
#include "platform.h"
|
2017-12-29 16:29:47 +00:00
|
|
|
#include "project.h"
|
|
|
|
#include "queue_manager.h"
|
|
|
|
#include "timestamp_manager.h"
|
|
|
|
#include "working_files.h"
|
2017-12-06 03:32:33 +00:00
|
|
|
|
|
|
|
#include <loguru.hpp>
|
2018-01-27 17:29:28 +00:00
|
|
|
#include <queue>
|
|
|
|
#include <unordered_set>
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2017-12-06 05:03:38 +00:00
|
|
|
namespace {
|
2018-01-30 00:27:43 +00:00
|
|
|
struct Ipc_CqueryFreshenIndex
|
|
|
|
: public NotificationMessage<Ipc_CqueryFreshenIndex> {
|
2017-12-06 04:39:44 +00:00
|
|
|
const static IpcId kIpcId = IpcId::CqueryFreshenIndex;
|
2018-01-27 17:29:28 +00:00
|
|
|
struct Params {
|
|
|
|
bool dependencies = true;
|
|
|
|
std::vector<std::string> whitelist;
|
|
|
|
std::vector<std::string> blacklist;
|
|
|
|
};
|
|
|
|
Params params;
|
2017-12-06 04:39:44 +00:00
|
|
|
};
|
2018-01-30 00:27:43 +00:00
|
|
|
MAKE_REFLECT_STRUCT(Ipc_CqueryFreshenIndex::Params,
|
|
|
|
dependencies,
|
|
|
|
whitelist,
|
|
|
|
blacklist);
|
2018-01-27 17:29:28 +00:00
|
|
|
MAKE_REFLECT_STRUCT(Ipc_CqueryFreshenIndex, params);
|
2017-12-06 04:39:44 +00:00
|
|
|
REGISTER_IPC_MESSAGE(Ipc_CqueryFreshenIndex);
|
|
|
|
|
2018-01-20 19:34:37 +00:00
|
|
|
struct CqueryFreshenIndexHandler : BaseMessageHandler<Ipc_CqueryFreshenIndex> {
|
|
|
|
void Run(Ipc_CqueryFreshenIndex* request) override {
|
2017-12-06 03:32:33 +00:00
|
|
|
LOG_S(INFO) << "Freshening " << project->entries.size() << " files";
|
|
|
|
|
|
|
|
// TODO: think about this flow and test it more.
|
2018-01-27 17:29:28 +00:00
|
|
|
GroupMatch matcher(request->params.whitelist, request->params.blacklist);
|
2017-12-06 03:32:33 +00:00
|
|
|
|
|
|
|
// Unmark all files whose timestamp has changed.
|
2018-01-30 05:34:28 +00:00
|
|
|
std::shared_ptr<ICacheManager> cache_manager = ICacheManager::Make(config);
|
2018-01-27 17:29:28 +00:00
|
|
|
|
|
|
|
std::queue<const QueryFile*> q;
|
|
|
|
// |need_index| stores every filename ever enqueued.
|
|
|
|
std::unordered_set<std::string> need_index;
|
|
|
|
// Reverse dependency graph.
|
|
|
|
std::unordered_map<std::string, std::vector<std::string>> graph;
|
|
|
|
// filename -> QueryFile mapping for files haven't enqueued.
|
|
|
|
std::unordered_map<std::string, const QueryFile*> path_to_file;
|
|
|
|
|
|
|
|
for (const auto& file : db->files)
|
|
|
|
if (file.def) {
|
|
|
|
if (matcher.IsMatch(file.def->path))
|
|
|
|
q.push(&file);
|
|
|
|
else
|
|
|
|
path_to_file[file.def->path] = &file;
|
|
|
|
for (const std::string& dependency : file.def->dependencies)
|
|
|
|
graph[dependency].push_back(file.def->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!q.empty()) {
|
|
|
|
const QueryFile* file = q.front();
|
|
|
|
q.pop();
|
|
|
|
need_index.insert(file->def->path);
|
2017-12-06 03:32:33 +00:00
|
|
|
|
|
|
|
optional<int64_t> modification_timestamp =
|
2018-01-27 17:29:28 +00:00
|
|
|
GetLastModificationTime(file->def->path);
|
2017-12-06 03:32:33 +00:00
|
|
|
if (!modification_timestamp)
|
|
|
|
continue;
|
|
|
|
optional<int64_t> cached_modification =
|
2017-12-29 17:27:56 +00:00
|
|
|
timestamp_manager->GetLastCachedModificationTime(cache_manager.get(),
|
2018-01-27 17:29:28 +00:00
|
|
|
file->def->path);
|
2017-12-06 03:32:33 +00:00
|
|
|
if (modification_timestamp != cached_modification)
|
2018-01-27 17:29:28 +00:00
|
|
|
file_consumer_shared->Reset(file->def->path);
|
|
|
|
|
|
|
|
if (request->params.dependencies)
|
|
|
|
for (const std::string& path : graph[file->def->path]) {
|
|
|
|
auto it = path_to_file.find(path);
|
|
|
|
if (it != path_to_file.end()) {
|
|
|
|
q.push(it->second);
|
|
|
|
path_to_file.erase(it);
|
|
|
|
}
|
|
|
|
}
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
|
2017-12-24 00:25:18 +00:00
|
|
|
auto* queue = QueueManager::instance();
|
|
|
|
|
2017-12-06 03:32:33 +00:00
|
|
|
// Send index requests for every file.
|
2017-12-29 18:00:44 +00:00
|
|
|
project->ForAllFilteredFiles(
|
|
|
|
config, [&](int i, const Project::Entry& entry) {
|
2018-01-27 17:29:28 +00:00
|
|
|
if (!need_index.count(entry.filename))
|
2018-01-20 19:34:37 +00:00
|
|
|
return;
|
2017-12-29 18:00:44 +00:00
|
|
|
optional<std::string> content = ReadContent(entry.filename);
|
|
|
|
if (!content) {
|
|
|
|
LOG_S(ERROR) << "When freshening index, cannot read file "
|
|
|
|
<< entry.filename;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bool is_interactive =
|
|
|
|
working_files->GetFileByFilename(entry.filename) != nullptr;
|
2018-02-05 06:03:22 +00:00
|
|
|
queue->index_request.PushBack(
|
|
|
|
Index_Request(entry.filename, entry.args, is_interactive,
|
|
|
|
*content, ICacheManager::Make(config)));
|
2017-12-29 18:00:44 +00:00
|
|
|
});
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
REGISTER_MESSAGE_HANDLER(CqueryFreshenIndexHandler);
|
2017-12-31 03:18:33 +00:00
|
|
|
} // namespace
|