Overpass API regression suite coverage (Saxony) / 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)
38246 q uint line_nr = tree_it->line_col.first;
39246 q std::string from = "_";
40246 q if (tree_it->rhs && tree_it->token == ".")
41246 q from = tree_it.rhs()->token;
43246 q std::map< std::string, std::string > attributes;
44246 q attributes["from"] = from;
45246 q attributes["into"] = into;
46246 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)
55126 q std::vector< TIndex > req;
56126 q {
57126 q Random_File< typename TObject::Id_Type, TIndex > random(rman.get_transaction()->random_index(&prop));
58126 q for (typename std::vector< typename TObject::Id_Type >::const_iterator
5928656 q it = ids.begin(); it != ids.end(); ++it)
6028535 q req.push_back(random.get(it->val()));
61126 q }
62126 q std::sort(req.begin(), req.end());
63126 q req.erase(std::unique(req.begin(), req.end()), req.end());
64126 q Block_Backend< TIndex, TObject > elems_db(rman.get_transaction()->data_index(&prop));
65133406 q for (auto it = elems_db.discrete_begin(req.begin(), req.end()); !(it == elems_db.discrete_end()); ++it)
66133285 q {
67133285 q if (binary_search(ids.begin(), ids.end(), it.object().id))
6828535 q elems[it.index()].push_back(it.object());
69133285 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)
9496 q std::vector< Node::Id_Type > pivot_ids;
9596 q for (std::map< Uint31_Index, std::vector< Area_Skeleton > >::const_iterator it = areas.begin();
965666 q it != areas.end(); ++it)
975575 q {
9862575 q for (std::vector< Area_Skeleton >::const_iterator sit = it->second.begin(); sit != it->second.end(); ++sit)
9957005 q {
10057005 q if (sit->id.val() < 2400000000u)
1010 pivot_ids.push_back(sit->id.val());
10257005 q }
1035575 q }
10496 q sort(pivot_ids.begin(), pivot_ids.end());
10596 q return pivot_ids;
109std::vector< Way::Id_Type > get_way_pivot_ids(const std::map< Uint31_Index, std::vector< Area_Skeleton > >& areas)
11196 q std::vector< Way::Id_Type > pivot_ids;
11296 q for (std::map< Uint31_Index, std::vector< Area_Skeleton > >::const_iterator it = areas.begin();
1135666 q it != areas.end(); ++it)
1145575 q {
11562575 q for (std::vector< Area_Skeleton >::const_iterator sit = it->second.begin(); sit != it->second.end(); ++sit)
11657005 q {
11757005 q if (sit->id.val() > 2400000000u && sit->id.val() < 3600000000u)
1180 pivot_ids.push_back(sit->id.val() - 2400000000u);
11957005 q }
1205575 q }
12196 q sort(pivot_ids.begin(), pivot_ids.end());
12296 q return pivot_ids;
126std::vector< Relation::Id_Type > get_relation_pivot_ids(const std::map< Uint31_Index, std::vector< Area_Skeleton > >& areas)
128126 q std::vector< Relation::Id_Type > pivot_ids;
129126 q for (std::map< Uint31_Index, std::vector< Area_Skeleton > >::const_iterator it = areas.begin();
1305726 q it != areas.end(); ++it)
1315605 q {
13262665 q for (std::vector< Area_Skeleton >::const_iterator sit = it->second.begin(); sit != it->second.end(); ++sit)
13357065 q {
13457065 q if (sit->id.val() > 3600000000u)
13557065 q pivot_ids.push_back(sit->id.val() - 3600000000u);
13657065 q }
1375605 q }
138126 q sort(pivot_ids.begin(), pivot_ids.end());
139126 q return pivot_ids;
143//-----------------------------------------------------------------------------
146class Pivot_Constraint : public Query_Constraint
148 public:
149186 q Pivot_Constraint(Pivot_Statement& stmt_) : stmt(&stmt_) {}
15166 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)
17633 q const Set* input_set = rman.get_set(stmt->get_input());
17733 q std::vector< Node::Id_Type > pivot_ids;
17833 q if (input_set)
17933 q pivot_ids = get_node_pivot_ids(input_set->areas);
18033 q std::vector< Node::Id_Type > intersect_ids(pivot_ids.size());
18133 q if (invert_ids && ids.empty())
18233 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());
19333 q collect_elems(rman, *osm_base_settings().NODES, intersect_ids, into.nodes);
19533 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)
20696 q const Set* input_set = rman.get_set(stmt->get_input());
20896 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;
23396 q if (type & QUERY_RELATION)
23566 q std::vector< Relation::Id_Type > pivot_ids;
23666 q if (input_set)
23766 q pivot_ids = get_relation_pivot_ids(input_set->areas);
23866 q std::vector< Relation::Id_Type > intersect_ids(pivot_ids.size());
23966 q if (invert_ids && ids.empty())
24066 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());
25166 q collect_elems(rman, *osm_base_settings().RELATIONS, intersect_ids, into.relations);
25496 q return true;
257void Pivot_Constraint::filter(Resource_Manager& rman, Set& into)
25966 q const Set* input_set = rman.get_set(stmt->get_input());
26166 q if (input_set)
26366 q filter_elems(get_node_pivot_ids(input_set->areas), into.nodes);
26466 q std::vector< Way::Id_Type > pivot_ways = get_way_pivot_ids(input_set->areas);
26566 q if (pivot_ways.empty())
26666 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 }
27466 q item_filter_map(into.attic_ways, input_set->attic_ways);
27566 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 }
28366 q into.attic_nodes.clear();
28466 q into.attic_ways.clear();
28566 q into.attic_relations.clear();
28666 q into.areas.clear();
28766 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)
294246 q : Output_Statement(line_number_)
296246 q std::map< std::string, std::string > attributes;
298246 q attributes["from"] = "_";
299246 q attributes["into"] = "_";
301246 q Statement::eval_attributes_array(get_name(), attributes, input_attributes);
303246 q input = attributes["from"];
304246 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()
329246 q for (std::vector< Query_Constraint* >::const_iterator it = constraints.begin();
330426 q it != constraints.end(); ++it)
331186 q delete *it;
335Query_Constraint* Pivot_Statement::get_query_constraint()
337186 q constraints.push_back(new Pivot_Constraint(*this));
338186 q return constraints.back();