Overpass API regression suite coverage (Saxony) / 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:
68328194 q Tag_Store(Transaction& transaction) {}
693531585 q Tag_Store() {}
71328194 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,
733063561 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(
762424809562 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)
882701160 q{
892701160 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
925301955160 q while ((!(current_tag_it == current_items_db.range_end())) &&
935301955160 q (((current_tag_it.index().index) & 0x7fffff00) < coarse_index))
94529925426 q ++current_tag_it;
9568023038160 q while ((!(current_tag_it == current_items_db.range_end())) &&
9668023038160 q (((current_tag_it.index().index) & 0x7fffff00) == coarse_index))
9768020337157 q {
9868020337157 q typename std::vector< Attic< Id_Type > >::const_iterator it_id
9968020337157 q = std::lower_bound(id_vec.begin(), id_vec.end(), Attic< Id_Type >(current_tag_it.object(), 0ull));
10068020337157 q typename std::vector< Attic< Id_Type > >::const_iterator it_id_end
10168020337157 q = std::upper_bound(id_vec.begin(), id_vec.end(), Attic< Id_Type >
10268020337157 q (current_tag_it.object(), 0xffffffffffffffffull));
10368020337157 q if (it_id != it_id_end)
104610135129 q found_tags[Attic< Id_Type >(current_tag_it.object(), 0xffffffffffffffffull)].push_back
105610135129 q (std::make_pair(current_tag_it.index().key, current_tag_it.index().value));
10668020337157 q ++current_tag_it;
10768020337157 q }
109 // Collect all id-matched tag information that is younger than the respective timestamp from the attic tags
1105412536160 q while ((!(attic_tag_it == attic_items_db.range_end())) &&
1115412536160 q (((attic_tag_it.index().index) & 0x7fffff00) < coarse_index))
112540983526 q ++attic_tag_it;
113126805160160 q while ((!(attic_tag_it == attic_items_db.range_end())) &&
114126805160160 q (((attic_tag_it.index().index) & 0x7fffff00) == coarse_index))
115126802459160 q {
116126802459160 q typename std::vector< Attic< Id_Type > >::const_iterator it_id
117126802459160 q = std::lower_bound(id_vec.begin(), id_vec.end(), Attic< Id_Type >(attic_tag_it.object(), 0ull));
118126802459160 q typename std::vector< Attic< Id_Type > >::const_iterator it_id_end
119126802459160 q = std::upper_bound(id_vec.begin(), id_vec.end(), attic_tag_it.object());
120126802459160 q if (it_id != it_id_end)
121461107118 q found_tags[attic_tag_it.object()].push_back
122461107118 q (std::make_pair(attic_tag_it.index().key, attic_tag_it.index().value));
123126802459160 q ++attic_tag_it;
124126802459160 q }
126 // Actually take for each object and key of the multiple versions only the oldest valid version
1272701160 q for (typename std::map< Attic< Id_Type >, std::vector< std::pair< std::string, std::string > > >
1282701160 q ::const_iterator
129334421160 q it = found_tags.begin(); it != found_tags.end(); ++it)
130331720130 q {
131331720130 q typename std::vector< Attic< Id_Type > >::const_iterator it_id
132331720130 q = std::lower_bound(id_vec.begin(), id_vec.end(), Attic< Id_Type >(it->first, 0ull));
133331720130 q typename std::vector< Attic< Id_Type > >::const_iterator it_id_end
134331720130 q = std::upper_bound(id_vec.begin(), id_vec.end(), it->first);
135663440130 q while (it_id != it_id_end)
136331720130 q {
137331720130 q std::vector< std::pair< std::string, std::string > >& obj_vec = tags_by_id[*it_id];
138331720130 q std::vector< std::pair< std::string, std::string > >::const_iterator last_added_it = it->second.end();
139331720130 q for (std::vector< std::pair< std::string, std::string > >::const_iterator
1401402962130 q it_source = it->second.begin(); it_source != it->second.end(); ++it_source)
1411071242130 q {
1421071242130 q if (last_added_it != it->second.end())
143454832126 q {
144454832126 q if (last_added_it->first == it_source->first)
1450 {
1460 obj_vec.push_back(*it_source);
1470 continue;
1480 }
149454832126 q else
150454832126 q last_added_it = it->second.end();
151454832126 q }
1531071242130 q std::vector< std::pair< std::string, std::string > >::const_iterator it_obj = obj_vec.begin();
1545628319130 q for (; it_obj != obj_vec.end(); ++it_obj)
1554914296127 q {
1564914296127 q if (it_obj->first == it_source->first)
157357219117 q break;
1584914296127 q }
1591071242130 q if (it_obj == obj_vec.end())
160714023130 q {
161714023130 q obj_vec.push_back(*it_source);
162714023130 q last_added_it = it_source;
163714023130 q }
1641071242130 q }
165331720130 q ++it_id;
166331720130 q }
167331720130 q }
169 // Remove empty tags. They are placeholders for tags added later than each timestamp in question.
1702701160 q for (typename std::map< Id_Type, std::vector< std::pair< std::string, std::string > > >
1712701160 q ::iterator
172231354160 q it_obj = tags_by_id.begin(); it_obj != tags_by_id.end(); ++it_obj)
173228653130 q {
174228653130 q for (typename std::vector< std::pair< std::string, std::string > >::size_type
1751374611130 q i = 0; i < it_obj->second.size(); )
1761145958130 q {
1771145958130 q if (it_obj->second[i].second == void_tag_value())
178265462112 q {
179265462112 q it_obj->second[i] = it_obj->second.back();
180265462112 q it_obj->second.pop_back();
181265462112 q }
182880496130 q else
183880496130 q ++i;
1841145958130 q }
185228653130 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)
199115466 q{
200115466 q std::vector< Attic< Id_Type > > id_vec = ids_by_coarse[coarse_index];
202115466 q if (!(upper_id_bound == Id_Type()))
203115466 q {
204115466 q typename std::vector< Attic< Id_Type > >::iterator it_id
205115466 q = std::lower_bound(id_vec.begin(), id_vec.end(),
206115466 q Attic< Id_Type >(upper_id_bound, 0ull));
207115466 q id_vec.erase(it_id, id_vec.end());
208115466 q }
209115466 q if (!(lower_id_bound == Id_Type()))
210115466 q {
211115466 q typename std::vector< Attic< Id_Type > >::iterator it_id
212115466 q = std::lower_bound(id_vec.begin(), id_vec.end(),
213115466 q Attic< Id_Type >(lower_id_bound, 0ull));
214115466 q id_vec.erase(id_vec.begin(), it_id);
215115466 q }
217115466 q collect_attic_tags< Id_Type >(tags_by_id, current_items_db, current_tag_it, attic_items_db, attic_tag_it,
218115466 q id_vec, coarse_index);
219115466 q}
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)
2293377890204 q while ((!(tag_it == items_db.range_end())) &&
2303377890204 q (((tag_it.index().index) & 0x7fffff00) < coarse_index))
231336940516 q ++tag_it;
232278484849204 q while ((!(tag_it == items_db.range_end())) &&
233278484849204 q (((tag_it.index().index) & 0x7fffff00) == coarse_index))
234278476364200 q {
235278476364200 q if ((binary_search(ids.begin(), ids.end(), tag_it.object())))
2366271679182 q tags_by_id[tag_it.object()].push_back
2376271679182 q (std::make_pair(tag_it.index().key, tag_it.index().value));
238278476364200 q ++tag_it;
239278476364200 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)
25110307282 q{
25210307282 q while ((!(tag_it == items_db.range_end())) &&
25310307282 q (((tag_it.index().index) & 0x7fffff00) < coarse_index))
2540 ++tag_it;
255314256700282 q while ((!(tag_it == items_db.range_end())) &&
256314256700282 q (((tag_it.index().index) & 0x7fffff00) == coarse_index))
257314246393280 q {
258314246393280 q if (!(tag_it.object() < lower_id_bound) &&
259314246393280 q (tag_it.object() < upper_id_bound) &&
260314246393280 q (binary_search(ids_by_coarse[coarse_index].begin(),
261298793086273 q ids_by_coarse[coarse_index].end(), tag_it.object())))
2626590055267 q tags_by_id[tag_it.object()].push_back
2636590055267 q (std::make_pair(tag_it.index().key, tag_it.index().value));
264314246393280 q ++tag_it;
265314246393280 q }
26610307282 q}
269template< typename Index, typename Object >
270Tag_Store< Index, Object >::Tag_Store(Transaction& transaction_)
27122084662 q : transaction(&transaction_), use_index(false), items_db(0), tag_it(0),
27222084662 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)
2783743249 q use_index = true;
2793743249 q generate_ids_by_coarse(ids_by_coarse, elems);
2803743249 q ranges = formulate_range_query(ids_by_coarse);
2823743249 q delete items_db;
2833743249 q items_db = new Block_Backend< Tag_Index_Local, typename Object::Id_Type >(
2843743249 q transaction->data_index(current_local_tags_file_properties< Object >()));
2863743249 q delete tag_it;
2873743249 q tag_it = new typename Block_Backend< Tag_Index_Local, typename Object::Id_Type >::Range_Iterator(
2883743249 q items_db->range_begin(ranges));
2903743249 q if (!ids_by_coarse.empty())
292865204 q tags_by_id.clear();
293865204 q stored_index = ids_by_coarse.begin()->first;
294865204 q collect_tags< typename Object::Id_Type >(tags_by_id, *items_db, *tag_it,
295865204 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)
304494298 q tags_by_id.clear();
306 //generate std::set of relevant coarse indices
307494298 q generate_ids_by_coarse(ids_by_coarse, elems);
309494298 q Block_Backend< Tag_Index_Local, typename Object::Id_Type > items_db
310494298 q (transaction->data_index(current_local_tags_file_properties< Object >()));
312494298 q Ranges< Tag_Index_Local > ranges = formulate_range_query(ids_by_coarse);
313494298 q auto tag_it = items_db.range_begin(ranges);
314494298 q for (typename std::map< uint32, std::vector< typename Object::Id_Type > >::const_iterator
31510801298 q it = ids_by_coarse.begin(); it != ids_by_coarse.end(); ++it)
31610307282 q collect_tags_framed< typename Object::Id_Type >(tags_by_id, items_db, tag_it, ids_by_coarse, it->first,
31710307282 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)
3241943123 q use_index = true;
3251943123 q generate_ids_by_coarse(attic_ids_by_coarse, attic_items);
3261943123 q ranges = formulate_range_query(attic_ids_by_coarse);
3281943123 q delete items_db;
3291943123 q items_db = new Block_Backend< Tag_Index_Local, typename Object::Id_Type >(
3301943123 q transaction->data_index(current_local_tags_file_properties< Object >()));
3311943123 q delete attic_items_db;
3321943123 q attic_items_db = new Block_Backend< Tag_Index_Local, Attic< typename Object::Id_Type > >(
3331943123 q transaction->data_index(attic_local_tags_file_properties< Object >()));
3351943123 q delete tag_it;
3361943123 q tag_it = new typename Block_Backend< Tag_Index_Local, typename Object::Id_Type >::Range_Iterator(
3371943123 q items_db->range_begin(ranges));
3381943123 q delete attic_tag_it;
3391943123 q attic_tag_it = new typename Block_Backend< Tag_Index_Local, Attic< typename Object::Id_Type > >::Range_Iterator(
3401943123 q attic_items_db->range_begin(ranges));
3421943123 q if (!attic_ids_by_coarse.empty())
344521106 q tags_by_id.clear();
345521106 q stored_index = attic_ids_by_coarse.begin()->first;
346521106 q collect_attic_tags< typename Object::Id_Type >(tags_by_id, *items_db, *tag_it, *attic_items_db, *attic_tag_it,
347521106 q attic_ids_by_coarse[stored_index.val()], stored_index.val());
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
35714270 q generate_ids_by_coarse(attic_ids_by_coarse, attic_items);
35914270 q Block_Backend< Tag_Index_Local, typename Object::Id_Type > current_tags_db
36014270 q (transaction->data_index(current_local_tags_file_properties< Object >()));
36114270 q Block_Backend< Tag_Index_Local, Attic< typename Object::Id_Type > > attic_tags_db
36214270 q (transaction->data_index(attic_local_tags_file_properties< Object >()));
36414270 q Ranges< Tag_Index_Local > ranges = formulate_range_query(attic_ids_by_coarse);
36514270 q auto current_tag_it = current_tags_db.range_begin(ranges);
36614270 q auto attic_tag_it = attic_tags_db.range_begin(ranges);
36714270 q for (typename std::map< uint32, std::vector< Attic< typename Object::Id_Type > > >::const_iterator
368129670 q it = attic_ids_by_coarse.begin(); it != attic_ids_by_coarse.end(); ++it)
369115466 q collect_attic_tags(tags_by_id, current_tags_db, current_tag_it, attic_tags_db, attic_tag_it,
370115466 q attic_ids_by_coarse, it->first, lower_id_bound, upper_id_bound);
374template< typename Index, typename Object >
375Tag_Store< Index, Object >::~Tag_Store()
37622084662 q{
37722084662 q delete items_db;
37822084662 q delete tag_it;
37922084662 q delete attic_items_db;
38022084662 q delete attic_tag_it;
38122084662 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)
38712838114437 q{
38812838114437 q if (use_index && !(stored_index == Index(index.val() & 0x7fffff00)))
3898646157 q {
3908646157 q if (Index(index.val() & 0x7fffff00) < stored_index)
3925819 q delete tag_it;
3935819 q tag_it = new typename Block_Backend< Tag_Index_Local, typename Object::Id_Type >::Range_Iterator(
3945819 q items_db->range_begin(ranges));
3955819 q if (attic_items_db)
39765 q delete attic_tag_it;
39865 q attic_tag_it = new typename Block_Backend< Tag_Index_Local, Attic< typename Object::Id_Type > >::Range_Iterator(
39965 q attic_items_db->range_begin(ranges));
4038646157 q tags_by_id.clear();
4048646157 q stored_index = Index(index.val() & 0x7fffff00);
4058646157 q if (attic_items_db)
406102669 q collect_attic_tags< typename Object::Id_Type >(tags_by_id, *items_db, *tag_it, *attic_items_db, *attic_tag_it,
407102669 q attic_ids_by_coarse[stored_index.val()], stored_index.val());
4087620142 q else
4097620142 q collect_tags< typename Object::Id_Type >(tags_by_id, *items_db, *tag_it,
4107620142 q ids_by_coarse[stored_index.val()], stored_index.val());
4118646157 q }
41312838114437 q auto it = tags_by_id.find(elem.id);
41412838114437 q if (it != tags_by_id.end())
4154765548415 q return &it->second;
4168072566203 q else
4178072566203 q return 0;
41812838114437 q}
421#endif