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

overpass_api/data/meta_collector.h

104 of 128 instrumented lines run by the suite (81.2%) 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__STATEMENTS__META_COLLECTOR_H
20#define DE__OSM3S___OVERPASS_API__STATEMENTS__META_COLLECTOR_H
22#include <map>
23#include <set>
24#include <vector>
26#include "../../template_db/block_backend.h"
27#include "../../template_db/random_file.h"
28#include "../../template_db/transaction.h"
29#include "../core/datatypes.h"
30#include "../core/settings.h"
31#include "filenames.h"
34template< typename Index, typename Id_Type >
35struct Meta_Collector
37public:
38 template< typename Object >
39 Meta_Collector(const std::map< Index, std::vector< Object > >& items,
40 Transaction& transaction, const File_Properties* meta_file_prop = 0);
42 Meta_Collector(const Ranges< Index >& used_ranges,
43 Transaction& transaction, const File_Properties* meta_file_prop = 0);
45 void reset();
46 const OSM_Element_Metadata_Skeleton< Id_Type >* get
47 (const Index& index, Id_Type ref);
48 const OSM_Element_Metadata_Skeleton< Id_Type >* get
49 (const Index& index, Id_Type ref, uint64 timestamp);
51 ~Meta_Collector()
5214084335 q {
5314084335 q delete last_index;
5414084335 q delete current_index;
5514084335 q delete range_it;
5614084335 q delete db_it;
5714084335 q delete meta_db;
5814084335 q }
60private:
61 std::set< Index > used_indices;
62 Ranges< Index > used_ranges;
63 Block_Backend< Index, OSM_Element_Metadata_Skeleton< Id_Type >, typename std::set< Index >::const_iterator >* meta_db;
64 typename Block_Backend< Index, OSM_Element_Metadata_Skeleton< Id_Type >, typename std::set< Index >::const_iterator >
65 ::Discrete_Iterator* db_it;
66 typename Block_Backend< Index, OSM_Element_Metadata_Skeleton< Id_Type >, typename std::set< Index >::const_iterator >
67 ::Range_Iterator* range_it;
68 Index* current_index;
69 Index* last_index;
70 std::set< OSM_Element_Metadata_Skeleton< Id_Type > > current_objects;
72 void update_current_objects(const Index&);
73};
76template< typename Index, typename Object >
77struct Attic_Meta_Collector
79public:
80 Attic_Meta_Collector(const std::map< Index, std::vector< Attic< Object > > >& items,
81 Transaction& transaction, bool turn_on);
83 const OSM_Element_Metadata_Skeleton< typename Object::Id_Type >* get
84 (const Index& index, typename Object::Id_Type ref, uint64 timestamp = NOW);
86private:
87 Meta_Collector< Index, typename Object::Id_Type > current;
88 Meta_Collector< Index, typename Object::Id_Type > attic;
89};
92/** Implementation --------------------------------------------------------- */
94template< typename Index, typename Object >
95void generate_index_query
96 (std::set< Index >& indices,
97 const std::map< Index, std::vector< Object > >& items)
986723188 q{
99880783188 q for (auto it = items.begin(); it != items.end(); ++it)
100874060165 q indices.insert(it->first);
104template< typename Index, typename Id_Type >
105template< typename Object >
106Meta_Collector< Index, Id_Type >::Meta_Collector
107 (const std::map< Index, std::vector< Object > >& items,
108 Transaction& transaction, const File_Properties* meta_file_prop)
10914084335 q : meta_db(0), db_it(0), range_it(0), current_index(0), last_index(0)
11014084335 q{
11114084335 q if (!meta_file_prop)
1127361185 q return;
1146723188 q generate_index_query(used_indices, items);
1156723188 q meta_db = new Block_Backend< Index, OSM_Element_Metadata_Skeleton< Id_Type >, typename std::set< Index >::const_iterator >
1166723188 q (transaction.data_index(meta_file_prop));
1186723188 q reset();
122template< typename Index, typename Id_Type >
123Meta_Collector< Index, Id_Type >::Meta_Collector
124 (const Ranges< Index >& used_ranges_,
125 Transaction& transaction, const File_Properties* meta_file_prop)
126 : used_ranges(used_ranges_), meta_db(0), db_it(0), range_it(0), current_index(0), last_index(0)
128 if (!meta_file_prop)
129 return;
131 meta_db = new Block_Backend< Index, OSM_Element_Metadata_Skeleton< Id_Type >, typename std::set< Index >::const_iterator >
132 (transaction.data_index(meta_file_prop));
134 reset();
138template< typename Index, typename Id_Type >
139void Meta_Collector< Index, Id_Type >::reset()
1416936240 q if (!meta_db)
14211262 q return;
1446824188 q delete db_it;
1456824188 q db_it = 0;
1466824188 q delete range_it;
1476824188 q range_it = 0;
1486824188 q delete current_index;
1496824188 q current_index = 0;
1506824188 q delete last_index;
1516824188 q last_index = 0;
1536824188 q if (used_ranges.empty())
1546824188 q {
1556824188 q if (!used_indices.empty())
1561893165 q last_index = new Index(*used_indices.begin());
1586824188 q db_it = new typename Block_Backend< Index, OSM_Element_Metadata_Skeleton< Id_Type >, typename std::set< Index >::const_iterator >
1596824188 q ::Discrete_Iterator(meta_db->discrete_begin(used_indices.begin(), used_indices.end()));
1616824188 q if (!(*db_it == meta_db->discrete_end()))
1621797165 q current_index = new Index(db_it->index());
163475141188 q while (!(*db_it == meta_db->discrete_end()) && (*current_index == db_it->index()))
164468317165 q {
165468317165 q current_objects.insert(db_it->object());
166468317165 q ++(*db_it);
167468317165 q }
1686824188 q }
1690 else
1700 {
1710 last_index = new Index(used_ranges.begin().lower_bound());
1720 range_it = new auto(meta_db->range_begin(used_ranges));
1740 if (!(*range_it == meta_db->range_end()))
1750 current_index = new Index(range_it->index());
1760 while (!(*range_it == meta_db->range_end()) && (*current_index == range_it->index()))
1770 {
1780 current_objects.insert(range_it->object());
1790 ++(*range_it);
1800 }
1810 }
185template< typename Index, typename Id_Type >
186void Meta_Collector< Index, Id_Type >::update_current_objects(const Index& index)
187452123139 q{
188452123139 q current_objects.clear();
189452123139 q if (last_index)
190452123139 q *last_index = index;
192452123139 q if (db_it)
193452123139 q {
1943098611139 q while (!(*db_it == meta_db->discrete_end()) && (db_it->index() < index))
195264648855 q ++(*db_it);
196452123139 q if (!(*db_it == meta_db->discrete_end()))
197452111139 q *current_index = db_it->index();
19820412543139 q while (!(*db_it == meta_db->discrete_end()) && (*current_index == db_it->index()))
19919960420139 q {
20019960420139 q current_objects.insert(db_it->object());
20119960420139 q ++(*db_it);
20219960420139 q }
203452123139 q }
2040 else if (range_it)
2050 {
2060 while (!(*range_it == meta_db->range_end()) && (range_it->index() < index))
2070 ++(*range_it);
2080 if (!(*range_it == meta_db->range_end()))
2090 *current_index = range_it->index();
2100 while (!(*range_it == meta_db->range_end()) && (*current_index == range_it->index()))
2110 {
2120 current_objects.insert(range_it->object());
2130 ++(*range_it);
2140 }
2150 }
216452123139 q}
219template< typename Index, typename Id_Type >
220const OSM_Element_Metadata_Skeleton< Id_Type >* Meta_Collector< Index, Id_Type >::get
221 (const Index& index, Id_Type ref)
2223368732200 q{
2233368732200 q if (!meta_db)
2249373556 q return 0;
2263274997154 q if (current_index && index < *last_index)
2276915 q reset();
2283274997154 q if (current_index && *current_index < index)
229372583136 q update_current_objects(index);
2313274997154 q typename std::set< OSM_Element_Metadata_Skeleton< Id_Type > >::iterator it
2323274997154 q = current_objects.lower_bound(OSM_Element_Metadata_Skeleton< Id_Type >(ref));
2333274997154 q if (it != current_objects.end() && it->ref == ref)
2343254342154 q return &*it;
235206555 q else
236206555 q return 0;
2373274997154 q}
240template< typename Index, typename Id_Type >
241const OSM_Element_Metadata_Skeleton< Id_Type >* Meta_Collector< Index, Id_Type >::get
242 (const Index& index, Id_Type ref, uint64 timestamp)
243585780116 q{
244585780116 q if (!meta_db)
2455405256 q return 0;
24753172870 q if (current_index && index < *last_index)
24821 q reset();
24953172870 q if (current_index && *current_index < index)
2507954063 q update_current_objects(index);
25253172870 q typename std::set< OSM_Element_Metadata_Skeleton< Id_Type > >::iterator it
25353172870 q = current_objects.lower_bound(OSM_Element_Metadata_Skeleton< Id_Type >(ref, timestamp));
25453172870 q if (it == current_objects.begin())
2554331655 q return 0;
25648841270 q --it;
25748841270 q if (it->ref == ref)
25832959270 q return &*it;
25915882058 q else
26015882058 q return 0;
26148841270 q}
264template< typename Index, typename Object >
265Attic_Meta_Collector< Index, Object >::Attic_Meta_Collector(
266 const std::map< Index, std::vector< Attic< Object > > >& items, Transaction& transaction, bool turn_on)
2671824162 q : current(items, transaction, turn_on ? current_meta_file_properties< Object >() : 0),
2681824162 q attic(items, transaction, turn_on ? attic_meta_file_properties< Object >() : 0)
2691824162 q{}
272template< typename Index, typename Object >
273const OSM_Element_Metadata_Skeleton< typename Object::Id_Type >* Attic_Meta_Collector< Index, Object >::get(
274 const Index& index, typename Object::Id_Type ref, uint64 timestamp)
275237686115 q{
276237686115 q const OSM_Element_Metadata_Skeleton< typename Object::Id_Type >* meta
277237686115 q = current.get(index, ref, timestamp);
278237686115 q if (meta)
279852436 q return meta;
280229162112 q return attic.get(index, ref, timestamp);
281237686115 q}
284#endif