[generator] Add o5m chunk reading

This commit is contained in:
Anatoly Serdtcev 2019-11-03 10:42:08 +03:00 committed by Sergey Yershov
parent 89194cb45b
commit 7d331ef83e
2 changed files with 35 additions and 6 deletions

View file

@ -188,21 +188,41 @@ void ProcessOsmElementsFromO5M(SourceReader & stream, function<void(OsmElement &
processor(std::move(element));
}
ProcessorOsmElementsFromO5M::ProcessorOsmElementsFromO5M(SourceReader & stream)
ProcessorOsmElementsFromO5M::ProcessorOsmElementsFromO5M(
SourceReader & stream, size_t taskCount, size_t taskId, size_t chunkSize)
: m_stream(stream)
, m_dataset([&](uint8_t * buffer, size_t size) {
return m_stream.Read(reinterpret_cast<char *>(buffer), size);
})
, m_taskCount{taskCount}
, m_taskId{taskId}
, m_chunkSize{chunkSize}
, m_pos(m_dataset.begin())
{
}
bool ProcessorOsmElementsFromO5M::TryRead(OsmElement & element)
{
if (m_pos == m_dataset.end())
return false;
while (m_pos != m_dataset.end())
{
auto const chunkId = m_elementCounter / m_chunkSize;
auto const chunkTaskId = chunkId % m_taskCount;
if (chunkTaskId == m_taskId)
return Read(element);
++m_pos;
++m_elementCounter;
}
return false;
}
bool ProcessorOsmElementsFromO5M::Read(OsmElement & element)
{
using Type = osm::O5MSource::EntityType;
element.Clear();
auto const translate = [](Type t) -> OsmElement::EntityType {
switch (t)
{
@ -213,8 +233,6 @@ bool ProcessorOsmElementsFromO5M::TryRead(OsmElement & element)
}
};
element.Clear();
// Be careful, we could call Nodes(), Members(), Tags() from O5MSource::Entity
// only once (!). Because these functions read data from file simultaneously with
// iterating in loop. Furthermore, into Tags() method calls Nodes.Skip() and Members.Skip(),
@ -252,6 +270,7 @@ bool ProcessorOsmElementsFromO5M::TryRead(OsmElement & element)
element.AddTag(tag.key, tag.value);
++m_pos;
++m_elementCounter;
return true;
}

View file

@ -59,15 +59,25 @@ public:
class ProcessorOsmElementsFromO5M : public ProcessorOsmElementsInterface
{
public:
explicit ProcessorOsmElementsFromO5M(SourceReader & stream);
explicit ProcessorOsmElementsFromO5M(SourceReader & stream,
size_t taskCount = 1, size_t taskId = 0,
size_t chunkSize = 1);
// ProcessorOsmElementsInterface overrides:
bool TryRead(OsmElement & element) override;
size_t ChunkSize() const noexcept { return m_chunkSize; }
private:
SourceReader & m_stream;
osm::O5MSource m_dataset;
size_t const m_taskCount;
size_t const m_taskId;
size_t const m_chunkSize;
uint64_t m_elementCounter{0};
osm::O5MSource::Iterator m_pos;
bool Read(OsmElement & element);
};
class ProcessorOsmElementsFromXml : public ProcessorOsmElementsInterface