Overpass API regression suite coverage (Nova Scotia) / overpass_api/data/tag_store.h

overpass_api/data/tag_store.h

244 of 249 instrumented lines run by the suite (98.0%) at 8edf11b0419c (this file on GitHub).

runnever runcolumns: line, executions, queries (click for the queries that run the line)

1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al.
2 *
3 * This file is part of Overpass_API.
4 *
5 * Overpass_API is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * Overpass_API is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.
17 */
19#ifndef DE__OSM3S___OVERPASS_API__DATA__TAG_STORE_H
20#define DE__OSM3S___OVERPASS_API__DATA__TAG_STORE_H
23#include "filenames.h"
24#include "../core/datatypes.h"
25#include "../../template_db/block_backend.h"
26#include "../../template_db/transaction.h"
28#include <map>
29#include <string>
30#include <vector>
33template< typename Index, typename Object >
34class Tag_Store
36public:
37 Tag_Store(Transaction& transaction);
38 ~Tag_Store();
40 void prefetch_all(const std::map< Index, std::vector< Object > >& elems);
41 void prefetch_chunk(const std::map< Index, std::vector< Object > >& elems,
42 typename Object::Id_Type lower_id_bound, typename Object::Id_Type upper_id_bound);
43 void prefetch_all(const std::map< Index, std::vector< Attic< Object > > >& elems);
44 void prefetch_chunk(const std::map< Index, std::vector< Attic< Object > > >& elems,
45 typename Object::Id_Type lower_id_bound, typename Object::Id_Type upper_id_bound);
47 const std::vector< std::pair< std::string, std::string > >* get(const Index& index, const Object& elem);
49private:
50 std::map< typename Object::Id_Type, std::vector< std::pair< std::string, std::string > > > tags_by_id;
51 Transaction* transaction;
52 bool use_index;
53 Index stored_index;
54 Ranges< Tag_Index_Local > ranges;
55 std::map< uint32, std::vector< typename Object::Id_Type > > ids_by_coarse;
56 std::map< uint32, std::vector< Attic< typename Object::Id_Type > > > attic_ids_by_coarse;
57 Block_Backend< Tag_Index_Local, typename Object::Id_Type >* items_db;
58 typename Block_Backend< Tag_Index_Local, typename Object::Id_Type >::Range_Iterator* tag_it;
59 Block_Backend< Tag_Index_Local, Attic< typename Object::Id_Type > >* attic_items_db;
60 typename Block_Backend< Tag_Index_Local, Attic< typename Object::Id_Type > >::Range_Iterator* attic_tag_it;
61};
64template< >
65class Tag_Store< Uint31_Index, Derived_Structure >
67public:
68302166 q Tag_Store(Transaction& transaction) {}
695946511 q Tag_Store() {}
71302166 q void prefetch_all(const std::map< Uint31_Index, std::vector< Derived_Structure > >& elems) {}
72 void prefetch_chunk(const std::map< Uint31_Index, std::vector< Derived_Structure > >& elems,
733206494 q Derived_Structure::Id_Type lower_id_bound, Derived_Structure::Id_Type upper_id_bound) {}
75 const std::vector< std::pair< std::string, std::string > >* get(
76142226495 q const Uint31_Index& index, const Derived_Structure& elem) const { return &elem.tags; }
77};
80template< class Id_Type >
81void collect_attic_tags
82 (std::map< Id_Type, std::vector< std::pair< std::string, std::string > > >& tags_by_id,
83 const Block_Backend< Tag_Index_Local, Id_Type >& current_items_db,
84 typename Block_Backend< Tag_Index_Local, Id_Type >::Range_Iterator& current_tag_it,
85 const Block_Backend< Tag_Index_Local, Attic< Id_Type > >& attic_items_db,
86 typename Block_Backend< Tag_Index_Local, Attic< Id_Type > >::Range_Iterator& attic_tag_it,
87 const std::vector< Attic< Id_Type > >& id_vec, uint32 coarse_index)
883086141 q{
893086141 q std::map< Attic< Id_Type >, std::vector< std::pair< std::string, std::string > > > found_tags;
91 // Collect all id-matched tag information from the current tags
922501404141 q while ((!(current_tag_it == current_items_db.range_end())) &&
932501404141 q (((current_tag_it.index().index) & 0x7fffff00) < coarse_index))
94249831827 q ++current_tag_it;
9521568139141 q while ((!(current_tag_it == current_items_db.range_end())) &&
9621568139141 q (((current_tag_it.index().index) & 0x7fffff00) == coarse_index))
9721565053140 q {
9821565053140 q typename std::vector< Attic< Id_Type > >::const_iterator it_id
9921565053140 q = std::lower_bound(id_vec.begin(), id_vec.end(), Attic< Id_Type >(current_tag_it.object(), 0ull));
10021565053140 q typename std::vector< Attic< Id_Type > >::const_iterator it_id_end
10121565053140 q = std::upper_bound(id_vec.begin(), id_vec.end(), Attic< Id_Type >
10221565053140 q (current_tag_it.object(), 0xffffffffffffffffull));
10321565053140 q if (it_id != it_id_end)
104246693109 q found_tags[Attic< Id_Type >(current_tag_it.object(), 0xffffffffffffffffull)].push_back
105246693109 q (std::make_pair(current_tag_it.index().key, current_tag_it.index().value));
10621565053140 q ++current_tag_it;
10721565053140 q }
109 // Collect all id-matched tag information that is younger than the respective timestamp from the attic tags
1102489985141 q while ((!(attic_tag_it == attic_items_db.range_end())) &&
1112489985141 q (((attic_tag_it.index().index) & 0x7fffff00) < coarse_index))
112248689927 q ++attic_tag_it;
11380803277141 q while ((!(attic_tag_it == attic_items_db.range_end())) &&
11480803277141 q (((attic_tag_it.index().index) & 0x7fffff00) == coarse_index))
11580800191141 q {
11680800191141 q typename std::vector< Attic< Id_Type > >::const_iterator it_id
11780800191141 q = std::lower_bound(id_vec.begin(), id_vec.end(), Attic< Id_Type >(attic_tag_it.object(), 0ull));
11880800191141 q typename std::vector< Attic< Id_Type > >::const_iterator it_id_end
11980800191141 q = std::upper_bound(id_vec.begin(), id_vec.end(), attic_tag_it.object());
12080800191141 q if (it_id != it_id_end)
121784143108 q found_tags[attic_tag_it.object()].push_back
122784143108 q (std::make_pair(attic_tag_it.index().key, attic_tag_it.index().value));
12380800191141 q ++attic_tag_it;
12480800191141 q }
126 // Actually take for each object and key of the multiple versions only the oldest valid version
1273086141 q for (typename std::map< Attic< Id_Type >, std::vector< std::pair< std::string, std::string > > >
1283086141 q ::const_iterator
129148230141 q it = found_tags.begin(); it != found_tags.end(); ++it)
130145144116 q {
131145144116 q typename std::vector< Attic< Id_Type > >::const_iterator it_id
132145144116 q = std::lower_bound(id_vec.begin(), id_vec.end(), Attic< Id_Type >(it->first, 0ull));
133145144116 q typename std::vector< Attic< Id_Type > >::const_iterator it_id_end
134145144116 q = std::upper_bound(id_vec.begin(), id_vec.end(), it->first);
135290289116 q while (it_id != it_id_end)
136145145116 q {
137145145116 q std::vector< std::pair< std::string, std::string > >& obj_vec = tags_by_id[*it_id];
138145145116 q std::vector< std::pair< std::string, std::string > >::const_iterator last_added_it = it->second.end();
139145145116 q for (std::vector< std::pair< std::string, std::string > >::const_iterator
1401175983116 q it_source = it->second.begin(); it_source != it->second.end(); ++it_source)
1411030838116 q {
1421030838116 q if (last_added_it != it->second.end())
143197967113 q {
144197967113 q if (last_added_it->first == it_source->first)
1450 {
1460 obj_vec.push_back(*it_source);
1470 continue;
1480 }
149197967113 q else
150197967113 q last_added_it = it->second.end();
151197967113 q }
1531030838116 q std::vector< std::pair< std::string, std::string > >::const_iterator it_obj = obj_vec.begin();
15458073116116 q for (; it_obj != obj_vec.end(); ++it_obj)
15557772791115 q {
15657772791115 q if (it_obj->first == it_source->first)
157730513104 q break;
15857772791115 q }
1591030838116 q if (it_obj == obj_vec.end())
160300325116 q {
161300325116 q obj_vec.push_back(*it_source);
162300325116 q last_added_it = it_source;
163300325116 q }
1641030838116 q }
165145145116 q ++it_id;
166145145116 q }
167145144116 q }
169 // Remove empty tags. They are placeholders for tags added later than each timestamp in question.
1703086141 q for (typename std::map< Id_Type, std::vector< std::pair< std::string, std::string > > >
1713086141 q ::iterator
17289937141 q it_obj = tags_by_id.begin(); it_obj != tags_by_id.end(); ++it_obj)
17386851116 q {
17486851116 q for (typename std::vector< std::pair< std::string, std::string > >::size_type
175524334116 q i = 0; i < it_obj->second.size(); )
176437483116 q {
177437483116 q if (it_obj->second[i].second == void_tag_value())
1788007596 q {
1798007596 q it_obj->second[i] = it_obj->second.back();
1808007596 q it_obj->second.pop_back();
1818007596 q }
182357408114 q else
183357408114 q ++i;
184437483116 q }
18586851116 q }
189template< class Id_Type >
190void collect_attic_tags
191 (std::map< Id_Type, std::vector< std::pair< std::string, std::string > > >& tags_by_id,
192 const Block_Backend< Tag_Index_Local, Id_Type >& current_items_db,
193 typename Block_Backend< Tag_Index_Local, Id_Type >::Range_Iterator& current_tag_it,
194 const Block_Backend< Tag_Index_Local, Attic< Id_Type > >& attic_items_db,
195 typename Block_Backend< Tag_Index_Local, Attic< Id_Type > >::Range_Iterator& attic_tag_it,
196 std::map< uint32, std::vector< Attic< Id_Type > > >& ids_by_coarse,
197 uint32 coarse_index,
198 Id_Type lower_id_bound, Id_Type upper_id_bound)
20063863 q std::vector< Attic< Id_Type > > id_vec = ids_by_coarse[coarse_index];
20263863 q if (!(upper_id_bound == Id_Type()))
20363863 q {
20463863 q typename std::vector< Attic< Id_Type > >::iterator it_id
20563863 q = std::lower_bound(id_vec.begin(), id_vec.end(),
20663863 q Attic< Id_Type >(upper_id_bound, 0ull));
20763863 q id_vec.erase(it_id, id_vec.end());
20863863 q }
20963863 q if (!(lower_id_bound == Id_Type()))
21063863 q {
21163863 q typename std::vector< Attic< Id_Type > >::iterator it_id
21263863 q = std::lower_bound(id_vec.begin(), id_vec.end(),
21363863 q Attic< Id_Type >(lower_id_bound, 0ull));
21463863 q id_vec.erase(id_vec.begin(), it_id);
21563863 q }
21763863 q collect_attic_tags< Id_Type >(tags_by_id, current_items_db, current_tag_it, attic_items_db, attic_tag_it,
21863863 q id_vec, coarse_index);
222template< class Id_Type >
223void collect_tags
224 (std::map< Id_Type, std::vector< std::pair< std::string, std::string > > >& tags_by_id,
225 const Block_Backend< Tag_Index_Local, Id_Type >& items_db,
226 typename Block_Backend< Tag_Index_Local, Id_Type >::Range_Iterator& tag_it,
227 const std::vector< Id_Type >& ids, uint32 coarse_index)
22821437185 q{
2292900779185 q while ((!(tag_it == items_db.range_end())) &&
2302900779185 q (((tag_it.index().index) & 0x7fffff00) < coarse_index))
231287934217 q ++tag_it;
232134445157185 q while ((!(tag_it == items_db.range_end())) &&
233134445157185 q (((tag_it.index().index) & 0x7fffff00) == coarse_index))
234134423720184 q {
235134423720184 q if ((binary_search(ids.begin(), ids.end(), tag_it.object())))
2363611666167 q tags_by_id[tag_it.object()].push_back
2373611666167 q (std::make_pair(tag_it.index().key, tag_it.index().value));
238134423720184 q ++tag_it;
239134423720184 q }
24021437185 q}
243template< class Id_Type >
244void collect_tags_framed
245 (std::map< Id_Type, std::vector< std::pair< std::string, std::string > > >& tags_by_id,
246 const Block_Backend< Tag_Index_Local, Id_Type >& items_db,
247 typename Block_Backend< Tag_Index_Local, Id_Type >::Range_Iterator& tag_it,
248 std::map< uint32, std::vector< Id_Type > >& ids_by_coarse,
249 uint32 coarse_index,
250 Id_Type lower_id_bound, Id_Type upper_id_bound)
25121969263 q{
25221969263 q while ((!(tag_it == items_db.range_end())) &&
25321969263 q (((tag_it.index().index) & 0x7fffff00) < coarse_index))
2540 ++tag_it;
255118621812263 q while ((!(tag_it == items_db.range_end())) &&
256118621812263 q (((tag_it.index().index) & 0x7fffff00) == coarse_index))
257118599843261 q {
258118599843261 q if (!(tag_it.object() < lower_id_bound) &&
259118599843261 q (tag_it.object() < upper_id_bound) &&
260118599843261 q (binary_search(ids_by_coarse[coarse_index].begin(),
26185820926250 q ids_by_coarse[coarse_index].end(), tag_it.object())))
2625384266240 q tags_by_id[tag_it.object()].push_back
2635384266240 q (std::make_pair(tag_it.index().key, tag_it.index().value));
264118599843261 q ++tag_it;
265118599843261 q }
26621969263 q}
269template< typename Index, typename Object >
270Tag_Store< Index, Object >::Tag_Store(Transaction& transaction_)
27130539583 q : transaction(&transaction_), use_index(false), items_db(0), tag_it(0),
27230539583 q attic_items_db(0), attic_tag_it(0) {}
275template< typename Index, typename Object >
276void Tag_Store< Index, Object >::prefetch_all(const std::map< Index, std::vector< Object > >& elems)
2783506217 q use_index = true;
2793506217 q generate_ids_by_coarse(ids_by_coarse, elems);
2803506217 q ranges = formulate_range_query(ids_by_coarse);
2823506217 q delete items_db;
2833506217 q items_db = new Block_Backend< Tag_Index_Local, typename Object::Id_Type >(
2843506217 q transaction->data_index(current_local_tags_file_properties< Object >()));
2863506217 q delete tag_it;
2873506217 q tag_it = new typename Block_Backend< Tag_Index_Local, typename Object::Id_Type >::Range_Iterator(
2883506217 q items_db->range_begin(ranges));
2903506217 q if (!ids_by_coarse.empty())
292887185 q tags_by_id.clear();
293887185 q stored_index = ids_by_coarse.begin()->first;
294887185 q collect_tags< typename Object::Id_Type >(tags_by_id, *items_db, *tag_it,
295887185 q ids_by_coarse[stored_index.val()], stored_index.val());
300template< typename Index, typename Object >
301void Tag_Store< Index, Object >::prefetch_chunk(const std::map< Index, std::vector< Object > >& elems,
302 typename Object::Id_Type lower_id_bound, typename Object::Id_Type upper_id_bound)
3042795275 q tags_by_id.clear();
306 //generate std::set of relevant coarse indices
3072795275 q generate_ids_by_coarse(ids_by_coarse, elems);
3092795275 q Block_Backend< Tag_Index_Local, typename Object::Id_Type > items_db
3102795275 q (transaction->data_index(current_local_tags_file_properties< Object >()));
3122795275 q Ranges< Tag_Index_Local > ranges = formulate_range_query(ids_by_coarse);
3132795275 q auto tag_it = items_db.range_begin(ranges);
3142795275 q for (typename std::map< uint32, std::vector< typename Object::Id_Type > >::const_iterator
31524764275 q it = ids_by_coarse.begin(); it != ids_by_coarse.end(); ++it)
31621969263 q collect_tags_framed< typename Object::Id_Type >(tags_by_id, items_db, tag_it, ids_by_coarse, it->first,
31721969263 q lower_id_bound, upper_id_bound);
321template< typename Index, typename Object >
322void Tag_Store< Index, Object >::prefetch_all(const std::map< Index, std::vector< Attic< Object > > >& attic_items)
3241725104 q use_index = true;
3251725104 q generate_ids_by_coarse(attic_ids_by_coarse, attic_items);
3261725104 q ranges = formulate_range_query(attic_ids_by_coarse);
3281725104 q delete items_db;
3291725104 q items_db = new Block_Backend< Tag_Index_Local, typename Object::Id_Type >(
3301725104 q transaction->data_index(current_local_tags_file_properties< Object >()));
3311725104 q delete attic_items_db;
3321725104 q attic_items_db = new Block_Backend< Tag_Index_Local, Attic< typename Object::Id_Type > >(
3331725104 q transaction->data_index(attic_local_tags_file_properties< Object >()));
3351725104 q delete tag_it;
3361725104 q tag_it = new typename Block_Backend< Tag_Index_Local, typename Object::Id_Type >::Range_Iterator(
3371725104 q items_db->range_begin(ranges));
3381725104 q delete attic_tag_it;
3391725104 q attic_tag_it = new typename Block_Backend< Tag_Index_Local, Attic< typename Object::Id_Type > >::Range_Iterator(
3401725104 q attic_items_db->range_begin(ranges));
3421725104 q if (!attic_ids_by_coarse.empty())
34348785 q {
34448785 q tags_by_id.clear();
34548785 q stored_index = attic_ids_by_coarse.begin()->first;
34648785 q collect_attic_tags< typename Object::Id_Type >(tags_by_id, *items_db, *tag_it, *attic_items_db, *attic_tag_it,
34748785 q attic_ids_by_coarse[stored_index.val()], stored_index.val());
34848785 q }
352template< typename Index, typename Object >
353void Tag_Store< Index, Object >::prefetch_chunk(const std::map< Index, std::vector< Attic< Object > > >& attic_items,
354 typename Object::Id_Type lower_id_bound, typename Object::Id_Type upper_id_bound)
356 //generate std::set of relevant coarse indices
35712469 q generate_ids_by_coarse(attic_ids_by_coarse, attic_items);
35912469 q Block_Backend< Tag_Index_Local, typename Object::Id_Type > current_tags_db
36012469 q (transaction->data_index(current_local_tags_file_properties< Object >()));
36112469 q Block_Backend< Tag_Index_Local, Attic< typename Object::Id_Type > > attic_tags_db
36212469 q (transaction->data_index(attic_local_tags_file_properties< Object >()));
36412469 q Ranges< Tag_Index_Local > ranges = formulate_range_query(attic_ids_by_coarse);
36512469 q auto current_tag_it = current_tags_db.range_begin(ranges);
36612469 q auto attic_tag_it = attic_tags_db.range_begin(ranges);
36712469 q for (typename std::map< uint32, std::vector< Attic< typename Object::Id_Type > > >::const_iterator
36876269 q it = attic_ids_by_coarse.begin(); it != attic_ids_by_coarse.end(); ++it)
36963863 q collect_attic_tags(tags_by_id, current_tags_db, current_tag_it, attic_tags_db, attic_tag_it,
37063863 q attic_ids_by_coarse, it->first, lower_id_bound, upper_id_bound);
374template< typename Index, typename Object >
375Tag_Store< Index, Object >::~Tag_Store()
37630539583 q{
37730539583 q delete items_db;
37830539583 q delete tag_it;
37930539583 q delete attic_items_db;
38030539583 q delete attic_tag_it;
38130539583 q}
384template< typename Index, typename Object >
385const std::vector< std::pair< std::string, std::string > >*
386 Tag_Store< Index, Object >::get(const Index& index, const Object& elem)
3875903718402 q{
3885903718402 q if (use_index && !(stored_index == Index(index.val() & 0x7fffff00)))
38922511148 q {
39022511148 q if (Index(index.val() & 0x7fffff00) < stored_index)
3928225 q delete tag_it;
3938225 q tag_it = new typename Block_Backend< Tag_Index_Local, typename Object::Id_Type >::Range_Iterator(
3948225 q items_db->range_begin(ranges));
3958225 q if (attic_items_db)
39711 q delete attic_tag_it;
39811 q attic_tag_it = new typename Block_Backend< Tag_Index_Local, Attic< typename Object::Id_Type > >::Range_Iterator(
39911 q attic_items_db->range_begin(ranges));
40322511148 q tags_by_id.clear();
40422511148 q stored_index = Index(index.val() & 0x7fffff00);
40522511148 q if (attic_items_db)
406196156 q collect_attic_tags< typename Object::Id_Type >(tags_by_id, *items_db, *tag_it, *attic_items_db, *attic_tag_it,
407196156 q attic_ids_by_coarse[stored_index.val()], stored_index.val());
40820550141 q else
40920550141 q collect_tags< typename Object::Id_Type >(tags_by_id, *items_db, *tag_it,
41020550141 q ids_by_coarse[stored_index.val()], stored_index.val());
41122511148 q }
4135903718402 q auto it = tags_by_id.find(elem.id);
4145903718402 q if (it != tags_by_id.end())
4152115769375 q return &it->second;
4163787949221 q else
4173787949221 q return 0;
4185903718402 q}
421#endif