2017-04-08 20:00:08 +00:00
|
|
|
#include "file_consumer.h"
|
|
|
|
|
2017-04-08 22:54:36 +00:00
|
|
|
#include "indexer.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
2017-04-08 20:00:08 +00:00
|
|
|
FileConsumer::FileConsumer(SharedState* shared_state) : shared_(shared_state) {}
|
|
|
|
|
2017-04-08 22:54:36 +00:00
|
|
|
IndexedFile* FileConsumer::TryConsumeFile(const std::string& file) {
|
2017-04-08 20:00:08 +00:00
|
|
|
// Try to find cached local result.
|
|
|
|
auto it = local_.find(file);
|
|
|
|
if (it != local_.end())
|
2017-04-08 22:54:36 +00:00
|
|
|
return it->second.get();
|
2017-04-08 20:00:08 +00:00
|
|
|
|
|
|
|
// No result in local; we need to query global.
|
|
|
|
bool did_insert = false;
|
|
|
|
{
|
2017-04-08 22:54:36 +00:00
|
|
|
std::lock_guard<std::mutex> lock(shared_->mutex);
|
2017-04-08 20:00:08 +00:00
|
|
|
did_insert = shared_->files.insert(file).second;
|
|
|
|
}
|
2017-04-08 22:54:36 +00:00
|
|
|
local_[file] = did_insert ? MakeUnique<IndexedFile>(file) : nullptr;
|
|
|
|
return local_[file].get();
|
2017-04-10 00:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileConsumer::ForceLocal(const std::string& file) {
|
|
|
|
auto it = local_.find(file);
|
|
|
|
if (it == local_.end())
|
|
|
|
local_[file] = MakeUnique<IndexedFile>(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<IndexedFile>> FileConsumer::TakeLocalState() {
|
|
|
|
std::vector<std::unique_ptr<IndexedFile>> result;
|
|
|
|
for (auto& entry : local_) {
|
|
|
|
if (entry.second)
|
|
|
|
result.push_back(std::move(entry.second));
|
|
|
|
}
|
|
|
|
return result;
|
2017-04-08 20:00:08 +00:00
|
|
|
}
|