|
3 | 3 |
|
4 | 4 | #include "util/exception.hpp" |
5 | 5 |
|
| 6 | +#include <boost/assert.hpp> |
| 7 | +#include <boost/flyweight.hpp> |
| 8 | +#include <boost/flyweight/no_tracking.hpp> |
| 9 | + |
| 10 | +#include <osmium/osm/area.hpp> |
| 11 | +#include <osmium/osm/item_type.hpp> |
| 12 | +#include <osmium/osm/node.hpp> |
| 13 | +#include <osmium/osm/object.hpp> |
6 | 14 | #include <osmium/osm/relation.hpp> |
| 15 | +#include <osmium/osm/types.hpp> |
| 16 | +#include <osmium/osm/way.hpp> |
7 | 17 |
|
8 | | -#include <boost/assert.hpp> |
| 18 | +#include <oneapi/tbb/concurrent_map.h> |
| 19 | +#include <oneapi/tbb/mutex.h> |
9 | 20 |
|
| 21 | +#include <osmium/storage/item_stash.hpp> |
10 | 22 | #include <string> |
11 | | -#include <unordered_map> |
12 | 23 | #include <vector> |
13 | 24 |
|
14 | 25 | namespace osrm::extractor |
15 | 26 | { |
16 | 27 |
|
17 | | -struct ExtractionRelation |
| 28 | +// It contains data of all parsed relations for each node/way element |
| 29 | +class ExtractionRelationContainer |
18 | 30 | { |
19 | | - class OsmIDTyped |
20 | | - { |
21 | | - public: |
22 | | - OsmIDTyped(osmium::object_id_type _id, osmium::item_type _type) : id(_id), type(_type) {} |
23 | | - |
24 | | - std::uint64_t GetID() const { return std::uint64_t(id); } |
25 | | - osmium::item_type GetType() const { return type; } |
26 | | - |
27 | | - std::uint64_t Hash() const { return id ^ (static_cast<std::uint64_t>(type) << 56); } |
28 | | - |
29 | | - private: |
30 | | - osmium::object_id_type id; |
31 | | - osmium::item_type type; |
32 | | - }; |
33 | | - |
34 | | - using AttributesList = std::vector<std::pair<std::string, std::string>>; |
35 | | - using MembersRolesList = std::vector<std::pair<std::uint64_t, std::string>>; |
36 | | - |
37 | | - explicit ExtractionRelation(const OsmIDTyped &_id) : id(_id) {} |
38 | | - ExtractionRelation() : id{-1, osmium::item_type::relation} {}; |
39 | | - |
40 | | - void Clear() |
41 | | - { |
42 | | - attributes.clear(); |
43 | | - members_role.clear(); |
44 | | - } |
| 31 | + public: |
| 32 | + using rel_id_type = osmium::object_id_type; |
| 33 | + using handle_type = osmium::ItemStash::handle_type; |
| 34 | + using mutex_type = tbb::mutex; |
45 | 35 |
|
46 | | - const char *GetAttr(const std::string &attr) const |
47 | | - { |
48 | | - auto it = std::lower_bound( |
49 | | - attributes.begin(), attributes.end(), std::make_pair(attr, std::string())); |
| 36 | + using RelationIDList = std::vector<rel_id_type>; |
| 37 | + using id_and_type = std::pair<osmium::object_id_type, osmium::item_type>; |
50 | 38 |
|
51 | | - if (it != attributes.end() && (*it).first == attr) |
52 | | - return (*it).second.c_str(); |
| 39 | + using RelationHandleMap = tbb::concurrent_map<rel_id_type, handle_type>; |
| 40 | + using MemberRelationMap = tbb::concurrent_map<id_and_type, RelationIDList>; |
53 | 41 |
|
54 | | - return nullptr; |
55 | | - } |
| 42 | + ExtractionRelationContainer() = default; |
| 43 | + ExtractionRelationContainer(ExtractionRelationContainer &&) = delete; |
| 44 | + ExtractionRelationContainer(const ExtractionRelationContainer &) = delete; |
56 | 45 |
|
57 | | - void Prepare() |
| 46 | + void AddRelation(const osmium::Relation &rel) |
58 | 47 | { |
59 | | - std::sort(attributes.begin(), attributes.end()); |
60 | | - std::sort(members_role.begin(), members_role.end()); |
61 | | - } |
| 48 | + BOOST_ASSERT(handles.find(rel.id()) == handles.end()); |
| 49 | + for (auto const &m : rel.members()) |
| 50 | + { |
| 51 | + AddRelationMember(rel.id(), m.ref(), m.type()); |
| 52 | + } |
62 | 53 |
|
63 | | - void AddMember(const OsmIDTyped &member_id, const char *role) |
64 | | - { |
65 | | - members_role.emplace_back(std::make_pair(member_id.Hash(), std::string(role))); |
| 54 | + handle_type handle; |
| 55 | + { |
| 56 | + mutex_type::scoped_lock lock(mutex); |
| 57 | + handle = stash.add_item(rel); |
| 58 | + } |
| 59 | + handles.emplace(rel.id(), handle); |
66 | 60 | } |
67 | 61 |
|
68 | | - const char *GetRole(const OsmIDTyped &member_id) const |
| 62 | + void |
| 63 | + AddRelationMember(rel_id_type relation_id, osmium::object_id_type id, osmium::item_type type) |
69 | 64 | { |
70 | | - const auto hash = member_id.Hash(); |
71 | | - auto it = std::lower_bound( |
72 | | - members_role.begin(), members_role.end(), std::make_pair(hash, std::string())); |
73 | | - |
74 | | - if (it != members_role.end() && (*it).first == hash) |
75 | | - return (*it).second.c_str(); |
76 | | - |
77 | | - return nullptr; |
| 65 | + parents[id_and_type{id, type}].push_back(relation_id); |
78 | 66 | } |
79 | 67 |
|
80 | | - OsmIDTyped id; |
81 | | - AttributesList attributes; |
82 | | - MembersRolesList members_role; |
83 | | -}; |
84 | | - |
85 | | -// It contains data of all parsed relations for each node/way element |
86 | | -class ExtractionRelationContainer |
87 | | -{ |
88 | | - public: |
89 | | - using AttributesMap = ExtractionRelation::AttributesList; |
90 | | - using OsmIDTyped = ExtractionRelation::OsmIDTyped; |
91 | | - using RelationList = std::vector<AttributesMap>; |
92 | | - using RelationIDList = std::vector<ExtractionRelation::OsmIDTyped>; |
93 | | - using RelationRefMap = std::unordered_map<std::uint64_t, RelationIDList>; |
94 | | - |
95 | | - ExtractionRelationContainer() = default; |
96 | | - ExtractionRelationContainer(ExtractionRelationContainer &&) = default; |
97 | | - ExtractionRelationContainer(const ExtractionRelationContainer &) = delete; |
| 68 | + std::size_t GetRelationsNum() const { return handles.size(); } |
98 | 69 |
|
99 | | - void AddRelation(ExtractionRelation &&rel) |
| 70 | + const RelationIDList &_GetRelationsFor(osmium::object_id_type id, osmium::item_type type) const |
100 | 71 | { |
101 | | - rel.Prepare(); |
| 72 | + if (type == osmium::item_type::area) |
| 73 | + { |
| 74 | + type = (id & 1) ? osmium::item_type::relation : osmium::item_type::way; |
| 75 | + id /= 2; |
| 76 | + } |
| 77 | + auto it = parents.find(id_and_type{id, type}); |
| 78 | + if (it != parents.end()) |
| 79 | + return it->second; |
102 | 80 |
|
103 | | - BOOST_ASSERT(relations_data.find(rel.id.GetID()) == relations_data.end()); |
104 | | - relations_data.insert(std::make_pair(rel.id.GetID(), std::move(rel))); |
| 81 | + return empty_rel_list; |
105 | 82 | } |
106 | 83 |
|
107 | | - void AddRelationMember(const OsmIDTyped &relation_id, const OsmIDTyped &member_id) |
| 84 | + const RelationIDList &GetRelationsFor(const osmium::OSMObject &o) const |
108 | 85 | { |
109 | | - switch (member_id.GetType()) |
110 | | - { |
111 | | - case osmium::item_type::node: |
112 | | - node_refs[member_id.GetID()].push_back(relation_id); |
113 | | - break; |
114 | | - |
115 | | - case osmium::item_type::way: |
116 | | - way_refs[member_id.GetID()].push_back(relation_id); |
117 | | - break; |
118 | | - |
119 | | - case osmium::item_type::relation: |
120 | | - rel_refs[member_id.GetID()].push_back(relation_id); |
121 | | - break; |
122 | | - |
123 | | - default: |
124 | | - break; |
125 | | - }; |
| 86 | + return _GetRelationsFor(o.id(), o.type()); |
126 | 87 | } |
127 | 88 |
|
128 | | - void Merge(ExtractionRelationContainer &&other) |
| 89 | + // Note: non-const because SOL somehow chokes on const. |
| 90 | + osmium::Relation &GetRelation(rel_id_type rel_id) const |
129 | 91 | { |
130 | | - for (auto it : other.relations_data) |
131 | | - { |
132 | | - const auto res = relations_data.insert(std::make_pair(it.first, std::move(it.second))); |
133 | | - BOOST_ASSERT(res.second); |
134 | | - (void)res; // prevent unused warning in release |
135 | | - } |
| 92 | + auto it = handles.find(rel_id); |
| 93 | + if (it == handles.end()) |
| 94 | + throw osrm::util::exception("Can't find relation data for " + std::to_string(rel_id)); |
136 | 95 |
|
137 | | - auto MergeRefMap = [&](RelationRefMap &source, RelationRefMap &target) |
138 | | - { |
139 | | - for (auto it : source) |
140 | | - { |
141 | | - auto &v = target[it.first]; |
142 | | - v.insert(v.end(), it.second.begin(), it.second.end()); |
143 | | - } |
144 | | - }; |
145 | | - |
146 | | - MergeRefMap(other.way_refs, way_refs); |
147 | | - MergeRefMap(other.node_refs, node_refs); |
148 | | - MergeRefMap(other.rel_refs, rel_refs); |
| 96 | + return stash.get<osmium::Relation>(it->second); |
149 | 97 | } |
150 | 98 |
|
151 | | - std::size_t GetRelationsNum() const { return relations_data.size(); } |
| 99 | + void reset() { iter = handles.begin(); } |
152 | 100 |
|
153 | | - const RelationIDList &GetRelations(const OsmIDTyped &member_id) const |
| 101 | + osmium::memory::Buffer read() |
154 | 102 | { |
155 | | - auto getFromMap = [this](std::uint64_t id, |
156 | | - const RelationRefMap &map) -> const RelationIDList & |
157 | | - { |
158 | | - auto it = map.find(id); |
159 | | - if (it != map.end()) |
160 | | - return it->second; |
161 | | - |
162 | | - return empty_rel_list; |
163 | | - }; |
164 | | - |
165 | | - switch (member_id.GetType()) |
| 103 | + osmium::memory::Buffer buffer(16 * 1024, osmium::memory::Buffer::auto_grow::yes); |
| 104 | + auto end = handles.end(); |
| 105 | + if (iter == end) |
| 106 | + return osmium::memory::Buffer(); |
| 107 | + while (iter != end && buffer.written() < 12 * 1024) |
166 | 108 | { |
167 | | - case osmium::item_type::node: |
168 | | - return getFromMap(member_id.GetID(), node_refs); |
169 | | - |
170 | | - case osmium::item_type::way: |
171 | | - return getFromMap(member_id.GetID(), way_refs); |
172 | | - |
173 | | - case osmium::item_type::relation: |
174 | | - return getFromMap(member_id.GetID(), rel_refs); |
175 | | - |
176 | | - default: |
177 | | - break; |
| 109 | + buffer.add_item(stash.get_item(iter->second)); |
| 110 | + buffer.commit(); |
| 111 | + ++iter; |
178 | 112 | } |
179 | | - |
180 | | - return empty_rel_list; |
181 | | - } |
182 | | - |
183 | | - const ExtractionRelation &GetRelationData(const ExtractionRelation::OsmIDTyped &rel_id) const |
184 | | - { |
185 | | - auto it = relations_data.find(rel_id.GetID()); |
186 | | - if (it == relations_data.end()) |
187 | | - throw osrm::util::exception("Can't find relation data for " + |
188 | | - std::to_string(rel_id.GetID())); |
189 | | - |
190 | | - return it->second; |
191 | | - } |
| 113 | + return buffer; |
| 114 | + }; |
192 | 115 |
|
193 | 116 | private: |
| 117 | + osmium::ItemStash stash; |
194 | 118 | RelationIDList empty_rel_list; |
195 | | - std::unordered_map<std::uint64_t, ExtractionRelation> relations_data; |
| 119 | + RelationHandleMap handles; |
| 120 | + RelationHandleMap::const_iterator iter; |
| 121 | + MemberRelationMap parents; |
| 122 | + |
| 123 | + mutex_type mutex; |
| 124 | +}; |
| 125 | + |
| 126 | +/** |
| 127 | + * @brief A copiable RelationMember |
| 128 | + */ |
| 129 | +class RelationMember |
| 130 | +{ |
| 131 | + osmium::object_id_type m_ref; |
| 132 | + osmium::item_type m_type; |
| 133 | + std::string m_role; |
196 | 134 |
|
197 | | - // each map contains list of relation id's, that has keyed id as a member |
198 | | - RelationRefMap way_refs; |
199 | | - RelationRefMap node_refs; |
200 | | - RelationRefMap rel_refs; |
| 135 | + public: |
| 136 | + RelationMember(const osmium::RelationMember &o) |
| 137 | + : m_ref{o.ref()}, m_type{o.type()}, m_role(o.role()){}; |
| 138 | + osmium::object_id_type ref() const noexcept { return m_ref; } |
| 139 | + osmium::item_type type() const noexcept { return m_type; } |
| 140 | + const std::string &role() const noexcept { return m_role; } |
201 | 141 | }; |
202 | 142 |
|
203 | 143 | } // namespace osrm::extractor |
|
0 commit comments