Overpass API regression suite coverage (Nova Scotia) / overpass_api/statements/pivot.cc

overpass_api/statements/pivot.cc

144 of 204 instrumented lines run by the suite (70.6%) 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#include <sstream>
21#include "../../template_db/block_backend.h"
22#include "../../template_db/random_file.h"
23#include "../core/settings.h"
24#include "../data/abstract_processing.h"
25#include "../data/collect_members.h"
26#include "pivot.h"
27#include "query.h"
30Pivot_Statement::Statement_Maker Pivot_Statement::statement_maker;
31Pivot_Statement::Criterion_Maker Pivot_Statement::criterion_maker;
34Statement* Pivot_Statement::Criterion_Maker::create_criterion(const Token_Node_Ptr& tree_it,
35 const std::string& type, const std::string& into,
36 Statement::Factory& stmt_factory, Parsed_Query& global_settings, Error_Output* error_output)
38164 q uint line_nr = tree_it->line_col.first;
39164 q std::string from = "_";
40164 q if (tree_it->rhs && tree_it->token == ".")
41164 q from = tree_it.rhs()->token;
43164 q std::map< std::string, std::string > attributes;
44164 q attributes["from"] = from;
45164 q attributes["into"] = into;
46164 q return new Pivot_Statement(line_nr, attributes, global_settings);
50template< class TIndex, class TObject >
51void collect_elems(Resource_Manager& rman, const File_Properties& prop,
52 const std::vector< typename TObject::Id_Type >& ids,
53 std::map< TIndex, std::vector< TObject > >& elems)
5584 q std::vector< TIndex > req;
5784 q Random_File< typename TObject::Id_Type, TIndex > random(rman.get_transaction()->random_index(&prop));
5884 q for (typename std::vector< typename TObject::Id_Type >::const_iterator
59164 q it = ids.begin(); it != ids.end(); ++it)
6082 q req.push_back(random.get(it->val()));
6284 q std::sort(req.begin(), req.end());
6384 q req.erase(std::unique(req.begin(), req.end()), req.end());
6484 q Block_Backend< TIndex, TObject > elems_db(rman.get_transaction()->data_index(&prop));
651194 q for (auto it = elems_db.discrete_begin(req.begin(), req.end()); !(it == elems_db.discrete_end()); ++it)
661112 q {
671112 q if (binary_search(ids.begin(), ids.end(), it.object().id))
6882 q elems[it.index()].push_back(it.object());
691112 q }
73template< class TIndex, class TObject >
74void filter_elems(const std::vector< typename TObject::Id_Type >& ids,
75 std::map< TIndex, std::vector< TObject > >& elems)
77 for (typename std::map< TIndex, std::vector< TObject > >::iterator it = elems.begin();
78 it != elems.end(); ++it)
79 {
80 std::vector< TObject > local_into;
81 for (typename std::vector< TObject >::const_iterator iit = it->second.begin();
82 iit != it->second.end(); ++iit)
83 {
84 if (binary_search(ids.begin(), ids.end(), iit->id))
85 local_into.push_back(*iit);
86 }
87 it->second.swap(local_into);
88 }
92std::vector< Node::Id_Type > get_node_pivot_ids(const std::map< Uint31_Index, std::vector< Area_Skeleton > >& areas)
9464 q std::vector< Node::Id_Type > pivot_ids;
9564 q for (std::map< Uint31_Index, std::vector< Area_Skeleton > >::const_iterator it = areas.begin();
96144 q it != areas.end(); ++it)
98192 q for (std::vector< Area_Skeleton >::const_iterator sit = it->second.begin(); sit != it->second.end(); ++sit)
99112 q {
100112 q if (sit->id.val() < 2400000000u)
1010 pivot_ids.push_back(sit->id.val());
102112 q }
10464 q sort(pivot_ids.begin(), pivot_ids.end());
10564 q return pivot_ids;
109std::vector< Way::Id_Type > get_way_pivot_ids(const std::map< Uint31_Index, std::vector< Area_Skeleton > >& areas)
11174 q std::vector< Way::Id_Type > pivot_ids;
11274 q for (std::map< Uint31_Index, std::vector< Area_Skeleton > >::const_iterator it = areas.begin();
113154 q it != areas.end(); ++it)
115192 q for (std::vector< Area_Skeleton >::const_iterator sit = it->second.begin(); sit != it->second.end(); ++sit)
116112 q {
117112 q if (sit->id.val() > 2400000000u && sit->id.val() < 3600000000u)
1180 pivot_ids.push_back(sit->id.val() - 2400000000u);
119112 q }
12174 q sort(pivot_ids.begin(), pivot_ids.end());
12274 q return pivot_ids;
126std::vector< Relation::Id_Type > get_relation_pivot_ids(const std::map< Uint31_Index, std::vector< Area_Skeleton > >& areas)
12874 q std::vector< Relation::Id_Type > pivot_ids;
12974 q for (std::map< Uint31_Index, std::vector< Area_Skeleton > >::const_iterator it = areas.begin();
130194 q it != areas.end(); ++it)
131122 q {
132282 q for (std::vector< Area_Skeleton >::const_iterator sit = it->second.begin(); sit != it->second.end(); ++sit)
133162 q {
134162 q if (sit->id.val() > 3600000000u)
135162 q pivot_ids.push_back(sit->id.val() - 3600000000u);
136162 q }
137122 q }
13874 q sort(pivot_ids.begin(), pivot_ids.end());
13974 q return pivot_ids;
143//-----------------------------------------------------------------------------
146class Pivot_Constraint : public Query_Constraint
148 public:
149124 q Pivot_Constraint(Pivot_Statement& stmt_) : stmt(&stmt_) {}
15144 q Query_Filter_Strategy delivers_data(Resource_Manager& rman) { return prefer_ranges; }
153 virtual bool get_data(const Statement& query, Resource_Manager& rman, Set& into,
154 const Ranges< Uint32_Index >& ranges,
155 const std::vector< Node::Id_Type >& ids,
156 bool invert_ids);
157 virtual bool get_data(const Statement& query, Resource_Manager& rman, Set& into,
158 const Ranges< Uint31_Index >& ranges,
159 int type,
160 const std::vector< Uint32_Index >& ids,
161 bool invert_ids);
162 void filter(Resource_Manager& rman, Set& into);
1630 virtual ~Pivot_Constraint() {}
165 private:
166 Pivot_Statement* stmt;
167};
170bool Pivot_Constraint::get_data
171 (const Statement& query, Resource_Manager& rman, Set& into,
172 const Ranges< Uint32_Index >& ranges,
173 const std::vector< Node_Skeleton::Id_Type >& ids,
174 bool invert_ids)
17622 q const Set* input_set = rman.get_set(stmt->get_input());
17722 q std::vector< Node::Id_Type > pivot_ids;
17822 q if (input_set)
17922 q pivot_ids = get_node_pivot_ids(input_set->areas);
18022 q std::vector< Node::Id_Type > intersect_ids(pivot_ids.size());
18122 q if (invert_ids && ids.empty())
18222 q pivot_ids.swap(intersect_ids);
1830 else if (!invert_ids)
1840 intersect_ids.erase(std::set_intersection
1850 (pivot_ids.begin(), pivot_ids.end(),
1860 ids.begin(), ids.end(),
1870 intersect_ids.begin()), intersect_ids.end());
1880 else
1890 intersect_ids.erase(std::set_difference
1900 (pivot_ids.begin(), pivot_ids.end(),
1910 ids.begin(), ids.end(),
1920 intersect_ids.begin()), intersect_ids.end());
19322 q collect_elems(rman, *osm_base_settings().NODES, intersect_ids, into.nodes);
19522 q return true;
199bool Pivot_Constraint::get_data
200 (const Statement& query, Resource_Manager& rman, Set& into,
201 const Ranges< Uint31_Index >& ranges,
202 int type,
203 const std::vector< Uint32_Index >& ids,
204 bool invert_ids)
20664 q const Set* input_set = rman.get_set(stmt->get_input());
20864 q if (type & QUERY_WAY)
21033 q std::vector< Way::Id_Type > pivot_ids;
21133 q if (input_set)
21233 q pivot_ids = get_way_pivot_ids(input_set->areas);
21333 q std::vector< Way::Id_Type > intersect_ids(pivot_ids.size());
21433 q if (invert_ids && ids.empty())
21533 q pivot_ids.swap(intersect_ids);
2160 else if (!invert_ids)
2170 intersect_ids.erase(std::set_intersection
2180 (pivot_ids.begin(), pivot_ids.end(),
2190 ids.begin(), ids.end(),
2200 intersect_ids.begin()), intersect_ids.end());
2210 else
2220 intersect_ids.erase(std::set_difference
2230 (pivot_ids.begin(), pivot_ids.end(),
2240 ids.begin(), ids.end(),
2250 intersect_ids.begin()), intersect_ids.end());
22633 q collect_elems(rman, *osm_base_settings().WAYS, intersect_ids, into.ways);
22733 q if (input_set)
22933 q indexed_set_union(into.ways, input_set->ways);
23033 q into.attic_ways = input_set->attic_ways;
23364 q if (type & QUERY_RELATION)
23533 q std::vector< Relation::Id_Type > pivot_ids;
23633 q if (input_set)
23733 q pivot_ids = get_relation_pivot_ids(input_set->areas);
23833 q std::vector< Relation::Id_Type > intersect_ids(pivot_ids.size());
23933 q if (invert_ids && ids.empty())
24033 q pivot_ids.swap(intersect_ids);
2410 else if (!invert_ids)
2420 intersect_ids.erase(std::set_intersection
2430 (pivot_ids.begin(), pivot_ids.end(),
2440 ids.begin(), ids.end(),
2450 intersect_ids.begin()), intersect_ids.end());
2460 else
2470 intersect_ids.erase(std::set_difference
2480 (pivot_ids.begin(), pivot_ids.end(),
2490 ids.begin(), ids.end(),
2500 intersect_ids.begin()), intersect_ids.end());
25133 q collect_elems(rman, *osm_base_settings().RELATIONS, intersect_ids, into.relations);
25464 q return true;
257void Pivot_Constraint::filter(Resource_Manager& rman, Set& into)
25944 q const Set* input_set = rman.get_set(stmt->get_input());
26144 q if (input_set)
26344 q filter_elems(get_node_pivot_ids(input_set->areas), into.nodes);
26444 q std::vector< Way::Id_Type > pivot_ways = get_way_pivot_ids(input_set->areas);
26544 q if (pivot_ways.empty())
26644 q item_filter_map(into.ways, input_set->ways);
2670 else
2680 {
2690 std::map< Uint31_Index, std::vector< Way_Skeleton > > ways_from_areas = into.ways;
2700 filter_elems(get_way_pivot_ids(input_set->areas), ways_from_areas);
2710 item_filter_map(into.ways, input_set->ways);
2720 indexed_set_union(into.ways, ways_from_areas);
2730 }
27444 q item_filter_map(into.attic_ways, input_set->attic_ways);
27544 q filter_elems(get_relation_pivot_ids(input_set->areas), into.relations);
2770 else
2780 {
2790 into.nodes.clear();
2800 into.ways.clear();
2810 into.relations.clear();
2820 }
28344 q into.attic_nodes.clear();
28444 q into.attic_ways.clear();
28544 q into.attic_relations.clear();
28644 q into.areas.clear();
28744 q into.deriveds.clear();
290//-----------------------------------------------------------------------------
292Pivot_Statement::Pivot_Statement
293 (int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings)
294164 q : Output_Statement(line_number_)
296164 q std::map< std::string, std::string > attributes;
298164 q attributes["from"] = "_";
299164 q attributes["into"] = "_";
301164 q Statement::eval_attributes_array(get_name(), attributes, input_attributes);
303164 q input = attributes["from"];
304164 q set_output(attributes["into"]);
308void Pivot_Statement::execute(Resource_Manager& rman)
3090{
3100 Set into;
3120 const Set* input_set = rman.get_set(input);
3130 if (input_set)
3140 {
3150 collect_elems(rman, *osm_base_settings().NODES, get_node_pivot_ids(input_set->areas), into.nodes);
3160 collect_elems(rman, *osm_base_settings().WAYS, get_way_pivot_ids(input_set->areas), into.ways);
3170 indexed_set_union(into.ways, input_set->ways);
3180 into.attic_ways = input_set->attic_ways;
3190 collect_elems(rman, *osm_base_settings().RELATIONS, get_relation_pivot_ids(input_set->areas), into.relations);
3200 }
3220 transfer_output(rman, into);
3230 rman.health_check(*this);
3240}
327Pivot_Statement::~Pivot_Statement()
329164 q for (std::vector< Query_Constraint* >::const_iterator it = constraints.begin();
330284 q it != constraints.end(); ++it)
331124 q delete *it;
335Query_Constraint* Pivot_Statement::get_query_constraint()
337124 q constraints.push_back(new Pivot_Constraint(*this));
338124 q return constraints.back();