Overpass API regression suite coverage (Saxony) / overpass_api/statements/id_query.cc

overpass_api/statements/id_query.cc

201 of 227 instrumented lines run by the suite (88.5%) 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 "../../template_db/block_backend.h"
20#include "../../template_db/random_file.h"
21#include "../core/settings.h"
22#include "../data/abstract_processing.h"
23#include "../data/collect_members.h"
24#include "../data/filenames.h"
25#include "id_query.h"
27#include <sstream>
30bool Id_Query_Statement::area_query_exists_ = false;
33Id_Query_Statement::Statement_Maker Id_Query_Statement::statement_maker;
34Id_Query_Statement::Criterion_Maker Id_Query_Statement::criterion_maker;
37Statement* Id_Query_Statement::Criterion_Maker::create_criterion(const Token_Node_Ptr& input_tree,
38 const std::string& result_type, const std::string& into,
39 Statement::Factory& stmt_factory, Parsed_Query& global_settings, Error_Output* error_output)
41672160 q Token_Node_Ptr tree_it = input_tree;
42672160 q uint line_nr = tree_it->line_col.first;
43672160 q std::vector< std::string > ref;
451256160 q while (tree_it->token == "," && tree_it->rhs && tree_it->lhs)
4658460 q {
4758460 q ref.push_back(tree_it.rhs()->token);
4858460 q tree_it = tree_it.lhs();
4958460 q }
51672160 q if (tree_it->token == ":" && tree_it->rhs)
5236090 q ref.push_back(tree_it.rhs()->token);
5331275 q else
5431275 q ref.push_back(tree_it->token);
56672160 q std::reverse(ref.begin(), ref.end());
58672160 q std::map< std::string, std::string > attributes;
59672160 q if (result_type != "nwr" && result_type != "nw" && result_type != "wr" && result_type != "nr")
60672160 q attributes["type"] = result_type;
61672160 q attributes["into"] = into;
631928160 q for (uint i = 0; i < ref.size(); ++i)
641256160 q {
651256160 q std::ostringstream id;
661256160 q if (i == 0)
67672160 q id << "ref";
6858460 q else
6958460 q id << "ref_" << i;
701256160 q attributes[id.str()] = ref[i];
711256160 q }
73672160 q return new Id_Query_Statement(line_nr, attributes, global_settings);
77void collect_elems_flat(Resource_Manager& rman,
78 const std::vector< uint64 >& ids,
79 std::map< Uint31_Index, std::vector< Area_Skeleton > >& elems)
800{
810 Block_Backend< Uint31_Index, Area_Skeleton > elems_db
820 (rman.get_transaction()->data_index(area_settings().AREAS));
830 for (Block_Backend< Uint31_Index, Area_Skeleton >::Flat_Iterator
840 it = elems_db.flat_begin(); !(it == elems_db.flat_end()); ++it)
850 {
860 if (std::binary_search(ids.begin(), ids.end(), it.object().id.val()))
870 elems[it.index()].push_back(it.object());
880 }
890}
92template< class TIndex, class TObject >
93void filter_elems(const std::vector< uint64 > & ids,
94 std::map< TIndex, std::vector< TObject > >& elems)
9546959 q{
9646959 q for (typename std::map< TIndex, std::vector< TObject > >::iterator it = elems.begin();
97831259 q it != elems.end(); ++it)
98784354 q {
99784354 q std::vector< TObject > local_into;
100784354 q for (typename std::vector< TObject >::const_iterator iit = it->second.begin();
1013846454 q iit != it->second.end(); ++iit)
1023062154 q {
1033062154 q if (std::binary_search(ids.begin(), ids.end(), iit->id.val()))
1049646 q local_into.push_back(*iit);
1053062154 q }
106784354 q it->second.swap(local_into);
107784354 q }
111template< typename Id_Type >
112std::vector< Id_Type > filtered_assign(const std::vector< uint64 >& arg)
114302148 q std::vector< Id_Type > result;
115302148 q result.reserve(arg.size());
116302148 q for (auto i : arg)
118562148 q if (i <= (uint64)Id_Type::max().val())
119562148 q result.push_back(Id_Type(static_cast< decltype(result.front().val()) >(i)));
121302148 q return result;
125//-----------------------------------------------------------------------------
128class Id_Query_Constraint : public Query_Constraint
130 public:
13118359 q Id_Query_Constraint(Id_Query_Statement& stmt_) : stmt(&stmt_) {}
1336759 q Query_Filter_Strategy delivers_data(Resource_Manager& rman) { return prefer_ranges; }
135 bool get_ranges(Resource_Manager& rman, Ranges< Uint32_Index >& ranges);
136 bool get_ranges(Resource_Manager& rman, Ranges< Uint31_Index >& ranges);
138 bool get_node_ids
139 (Resource_Manager& rman, std::vector< Node_Skeleton::Id_Type >& ids);
140 bool get_way_ids
141 (Resource_Manager& rman, std::vector< Way_Skeleton::Id_Type >& ids);
142 bool get_relation_ids
143 (Resource_Manager& rman, std::vector< Relation_Skeleton::Id_Type >& ids);
144 bool get_area_ids
145 (Resource_Manager& rman, std::vector< Area_Skeleton::Id_Type >& ids);
147 void filter(Resource_Manager& rman, Set& into);
1480 virtual ~Id_Query_Constraint() {}
150 private:
151 Id_Query_Statement* stmt;
152};
155bool Id_Query_Constraint::get_node_ids(Resource_Manager& rman, std::vector< Node_Skeleton::Id_Type >& ids)
1571513 q ids = filtered_assign< Node_Skeleton::Id_Type >(stmt->get_refs());
1581513 q return true;
162bool Id_Query_Constraint::get_way_ids(Resource_Manager& rman, std::vector< Way_Skeleton::Id_Type >& ids)
1642926 q ids = filtered_assign< Way_Skeleton::Id_Type >(stmt->get_refs());
1652926 q return true;
169bool Id_Query_Constraint::get_relation_ids(Resource_Manager& rman, std::vector< Relation_Skeleton::Id_Type >& ids)
1711312 q ids = filtered_assign< Relation_Skeleton::Id_Type >(stmt->get_refs());
1721312 q return true;
176bool Id_Query_Constraint::get_area_ids(Resource_Manager& rman, std::vector< Area_Skeleton::Id_Type >& ids)
1782320 q ids = filtered_assign< Area_Skeleton::Id_Type >(stmt->get_refs());
1792320 q return true;
183bool Id_Query_Constraint::get_ranges(Resource_Manager& rman, Ranges< Uint32_Index >& ranges)
1851513 q std::vector< Node_Skeleton::Id_Type > ids = filtered_assign< Node_Skeleton::Id_Type >(stmt->get_refs());
1861513 q std::vector< Uint32_Index > req = get_indexes_< Uint32_Index, Node_Skeleton >(ids, rman);
1881513 q ranges = Ranges< Uint32_Index >();
1897013 q for (std::vector< Uint32_Index >::const_iterator it = req.begin(); it != req.end(); ++it)
1905513 q ranges.push_back(*it, ++Uint32_Index(*it));
1911513 q ranges.sort();
1931513 q return true;
197bool Id_Query_Constraint::get_ranges(Resource_Manager& rman, Ranges< Uint31_Index >& ranges)
1994137 q std::vector< Uint31_Index > req;
2004137 q ranges = Ranges< Uint31_Index >();
2034137 q std::vector< Way_Skeleton::Id_Type > ids = filtered_assign< Way_Skeleton::Id_Type >(stmt->get_refs());
2044137 q get_indexes_< Uint31_Index, Way_Skeleton >(ids, rman).swap(req);
2058437 q for (std::vector< Uint31_Index >::const_iterator it = req.begin(); it != req.end(); ++it)
2064337 q ranges.push_back(*it, inc(*it));
2104137 q std::vector< Relation_Skeleton::Id_Type > ids = filtered_assign< Relation_Skeleton::Id_Type >(stmt->get_refs());
2114137 q get_indexes_< Uint31_Index, Relation_Skeleton >(ids, rman).swap(req);
21211137 q for (std::vector< Uint31_Index >::const_iterator it = req.begin(); it != req.end(); ++it)
2137037 q ranges.push_back(*it, inc(*it));
2164137 q ranges.sort();
2184137 q return true;
222void Id_Query_Constraint::filter(Resource_Manager& rman, Set& into)
2246759 q filter_elems(stmt->get_refs(), into.nodes);
2256759 q filter_elems(stmt->get_refs(), into.attic_nodes);
2276759 q filter_elems(stmt->get_refs(), into.ways);
2286759 q filter_elems(stmt->get_refs(), into.attic_ways);
2306759 q filter_elems(stmt->get_refs(), into.relations);
2316759 q filter_elems(stmt->get_refs(), into.attic_relations);
2336759 q filter_elems(stmt->get_refs(), into.areas);
236//-----------------------------------------------------------------------------
238Id_Query_Statement::Id_Query_Statement
239 (int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings)
240705163 q : Output_Statement(line_number_)
242705163 q std::map< std::string, std::string > attributes;
244705163 q attributes["into"] = "_";
245705163 q attributes["type"] = "";
246705163 q attributes["ref"] = "";
247705163 q attributes["lower"] = "";
248705163 q attributes["upper"] = "";
250705163 q for (std::map<std::string, std::string>::const_iterator it = input_attributes.begin();
2513368163 q it != input_attributes.end(); ++it)
2522663163 q {
2532663163 q if (it->first.find("ref_") == 0)
25458460 q attributes[it->first] = "";
2552663163 q }
257705163 q Statement::eval_attributes_array(get_name(), attributes, input_attributes);
259705163 q set_output(attributes["into"]);
261705163 q if (attributes["type"] == "node")
26218246 q type = Statement::NODE;
263523118 q else if (attributes["type"] == "way")
26419247 q type = Statement::WAY;
26533173 q else if (attributes["type"] == "relation")
26624756 q type = Statement::RELATION;
2678420 q else if (attributes["type"] == "area")
2698420 q type = Statement::AREA;
2708420 q area_query_exists_ = true;
2720 else if (attributes["type"].empty())
2730 type = 0;
2740 else
2750 {
2760 type = 0;
2770 std::ostringstream temp;
2780 temp<<"For the attribute \"type\" of the element \"id-query\""
2790 <<" the only allowed values are \"node\", \"way\", \"relation\", or \"area\".";
2800 add_static_error(temp.str());
2810 }
283705163 q uint64 ref = atoll(attributes["ref"].c_str());
285705163 q if (ref > 0)
286702162 q refs.push_back(ref);
288705163 q for (std::map< std::string, std::string >::iterator it = attributes.begin();
2894814163 q it != attributes.end(); ++it)
2904109163 q {
2914109163 q if (it->first.find("ref_") == 0)
29258460 q {
29358460 q ref = atoll(it->second.c_str());
29458460 q if (ref > 0)
29558460 q refs.push_back(ref);
29658460 q }
2974109163 q }
299705163 q uint64 lower = atoll(attributes["lower"].c_str());
300705163 q uint64 upper = atoll(attributes["upper"].c_str());
302705163 q if (ref <= 0)
30431 q if (lower == 0 || upper == 0)
30631 q std::ostringstream temp;
30731 q temp<<"For the attribute \"ref\" of the element \"id-query\""
30831 q <<" the only allowed values are positive integers.";
30931 q add_static_error(temp.str());
31131 q ++upper;
313702162 q else
315702162 q lower = ref;
316702162 q upper = ++ref;
319705163 q if (lower > 0 && upper > 0)
3211404162 q for (uint64 i = lower; i < upper; ++i)
322702162 q refs.push_back(i);
325705163 q std::sort(refs.begin(), refs.end());
326705163 q refs.erase(std::unique(refs.begin(), refs.end()), refs.end());
330// Nach "data" verschieben ---------------------------
333template< typename Id_Type >
334struct Id_Pair_First_Comparator
336 bool operator()(const std::pair< Id_Type, uint64 >& lhs,
337 const std::pair< Id_Type, uint64 >& rhs) const
338 {
339 return (lhs.first < rhs.first);
340 }
341};
344template< typename Id_Type >
345struct Id_Pair_Full_Comparator
347 bool operator()(const std::pair< Id_Type, uint64 >& lhs,
348 const std::pair< Id_Type, uint64 >& rhs) const
349 {
350 if (lhs.first < rhs.first)
351 return true;
352 if (rhs.first < lhs.first)
353 return false;
354 return (lhs.second < rhs.second);
355 }
356};
359template< typename Index, typename Skeleton >
360struct Attic_Skeleton_By_Id
362 Attic_Skeleton_By_Id(const typename Skeleton::Id_Type& id_, uint64 timestamp_)
363 : id(id_), timestamp(timestamp_), index(0u), meta_confirmed(false),
364 elem(Skeleton(), 0xffffffffffffffffull) {}
366 typename Skeleton::Id_Type id;
367 uint64 timestamp;
368 Index index;
369 bool meta_confirmed;
370 Attic< Skeleton > elem;
372 bool operator<(const Attic_Skeleton_By_Id& rhs) const
373 {
374 if (id < rhs.id)
375 return true;
376 if (rhs.id < id)
377 return false;
378 return (rhs.timestamp < timestamp);
379 }
380};
383template< typename Index, typename Skeleton >
384void get_elements(const std::vector< uint64 >& refs, Statement* stmt, Resource_Manager& rman,
385 std::map< Index, std::vector< Skeleton > >& current_result,
386 std::map< Index, std::vector< Attic< Skeleton > > >& attic_result)
388125103 q std::vector< typename Skeleton::Id_Type > ids = filtered_assign< typename Skeleton::Id_Type >(refs);
389125103 q std::vector< Index > req = get_indexes_< Index, Skeleton >(ids, rman);
391125103 q Request_Context context(stmt, rman);
392125103 q collect_items_discrete(context, req, Id_Predicate< Skeleton >(ids), current_result, attic_result);
394125103 q if (rman.get_desired_timestamp() != NOW)
3955247 q filter_attic_elements(rman, rman.get_desired_timestamp(), current_result, attic_result);
399void Id_Query_Statement::execute(Resource_Manager& rman)
401125103 q Set into;
403125103 q if (type == NODE)
4042827 q get_elements(refs, this, rman, into.nodes, into.attic_nodes);
4059776 q else if (type == WAY)
4064235 q get_elements(refs, this, rman, into.ways, into.attic_ways);
4075541 q else if (type == RELATION)
4085541 q get_elements(refs, this, rman, into.relations, into.attic_relations);
4090 else if (type == AREA)
4100 {
4110 get_elements(refs, this, rman, into.ways, into.attic_ways);
4120 collect_elems_flat(rman, refs, into.areas);
4130 }
415125103 q transfer_output(rman, into);
416125103 q rman.health_check(*this);
420Id_Query_Statement::~Id_Query_Statement()
422705163 q for (std::vector< Query_Constraint* >::const_iterator it = constraints.begin();
423888163 q it != constraints.end(); ++it)
42418359 q delete *it;
428Query_Constraint* Id_Query_Statement::get_query_constraint()
43018359 q constraints.push_back(new Id_Query_Constraint(*this));
43118359 q return constraints.back();