forked from organicmaps/organicmaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraw_generator_writer.cpp
82 lines (68 loc) · 1.99 KB
/
raw_generator_writer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "generator/raw_generator_writer.hpp"
#include "coding/varint.hpp"
#include "base/file_name_utils.hpp"
#include <iterator>
namespace generator
{
RawGeneratorWriter::RawGeneratorWriter(std::shared_ptr<FeatureProcessorQueue> const & queue,
std::string const & path)
: m_queue(queue), m_path(path)
{
}
RawGeneratorWriter::~RawGeneratorWriter() { ShutdownAndJoin(); }
void RawGeneratorWriter::Run()
{
m_thread = std::thread([&]() {
while (true)
{
FeatureProcessorChunk chunk;
m_queue->WaitAndPop(chunk);
// As a sign of the end of tasks, we use an empty message. We have the right to do that,
// because there is only one reader.
if (!chunk.has_value())
return;
Write(*chunk);
}
});
}
std::vector<std::string> RawGeneratorWriter::GetNames()
{
CHECK(!m_thread.joinable(), ());
std::vector<std::string> names;
names.reserve(m_writers.size());
for (const auto & p : m_writers)
names.emplace_back(p.first);
return names;
}
void RawGeneratorWriter::Write(std::vector<ProcessedData> const & vecChunks)
{
for (auto const & chunk : vecChunks)
{
for (auto const & affiliation : chunk.m_affiliations)
{
if (affiliation.empty())
continue;
auto writerIt = m_writers.find(affiliation);
if (writerIt == std::cend(m_writers))
{
auto path = base::JoinPath(m_path, affiliation + DATA_FILE_EXTENSION_TMP);
LOG(LINFO, ("Start writing to ", path));
auto writer = std::make_unique<FileWriter>(std::move(path));
writerIt = m_writers.emplace(affiliation, std::move(writer)).first;
}
auto & writer = writerIt->second;
auto const & buffer = chunk.m_buffer;
WriteVarUint(*writer, static_cast<uint32_t>(buffer.size()));
writer->Write(buffer.data(), buffer.size());
}
}
}
void RawGeneratorWriter::ShutdownAndJoin()
{
if (m_thread.joinable())
{
m_queue->Push({});
m_thread.join();
}
}
} // namespace generator