Overpass API regression suite coverage (Nova Scotia) / 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)
41408100 q Token_Node_Ptr tree_it = input_tree;
42408100 q uint line_nr = tree_it->line_col.first;
43408100 q std::vector< std::string > ref;
45736100 q while (tree_it->token == "," && tree_it->rhs && tree_it->lhs)
4632843 q {
4732843 q ref.push_back(tree_it.rhs()->token);
4832843 q tree_it = tree_it.lhs();
4932843 q }
51408100 q if (tree_it->token == ":" && tree_it->rhs)
5220852 q ref.push_back(tree_it.rhs()->token);
5320049 q else
5420049 q ref.push_back(tree_it->token);
56408100 q std::reverse(ref.begin(), ref.end());
58408100 q std::map< std::string, std::string > attributes;
59408100 q if (result_type != "nwr" && result_type != "nw" && result_type != "wr" && result_type != "nr")
60408100 q attributes["type"] = result_type;
61408100 q attributes["into"] = into;
631144100 q for (uint i = 0; i < ref.size(); ++i)
64736100 q {
65736100 q std::ostringstream id;
66736100 q if (i == 0)
67408100 q id << "ref";
6832843 q else
6932843 q id << "ref_" << i;
70736100 q attributes[id.str()] = ref[i];
71736100 q }
73408100 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)
9521728 q{
9621728 q for (typename std::map< TIndex, std::vector< TObject > >::iterator it = elems.begin();
9745028 q it != elems.end(); ++it)
9823327 q {
9923327 q std::vector< TObject > local_into;
10023327 q for (typename std::vector< TObject >::const_iterator iit = it->second.begin();
101889227 q iit != it->second.end(); ++iit)
102865927 q {
103865927 q if (std::binary_search(ids.begin(), ids.end(), iit->id.val()))
1043619 q local_into.push_back(*iit);
105865927 q }
10623327 q it->second.swap(local_into);
10723327 q }
111template< typename Id_Type >
112std::vector< Id_Type > filtered_assign(const std::vector< uint64 >& arg)
11414792 q std::vector< Id_Type > result;
11514792 q result.reserve(arg.size());
11614792 q for (auto i : arg)
11726892 q {
11826892 q if (i <= (uint64)Id_Type::max().val())
11926892 q result.push_back(Id_Type(static_cast< decltype(result.front().val()) >(i)));
12026892 q }
12114792 q return result;
125//-----------------------------------------------------------------------------
128class Id_Query_Constraint : public Query_Constraint
130 public:
1318428 q Id_Query_Constraint(Id_Query_Statement& stmt_) : stmt(&stmt_) {}
1333128 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)
157109 q ids = filtered_assign< Node_Skeleton::Id_Type >(stmt->get_refs());
158109 q return true;
162bool Id_Query_Constraint::get_way_ids(Resource_Manager& rman, std::vector< Way_Skeleton::Id_Type >& ids)
16465 q ids = filtered_assign< Way_Skeleton::Id_Type >(stmt->get_refs());
16565 q return true;
169bool Id_Query_Constraint::get_relation_ids(Resource_Manager& rman, std::vector< Relation_Skeleton::Id_Type >& ids)
17176 q ids = filtered_assign< Relation_Skeleton::Id_Type >(stmt->get_refs());
17276 q return true;
176bool Id_Query_Constraint::get_area_ids(Resource_Manager& rman, std::vector< Area_Skeleton::Id_Type >& ids)
17811 q ids = filtered_assign< Area_Skeleton::Id_Type >(stmt->get_refs());
17911 q return true;
183bool Id_Query_Constraint::get_ranges(Resource_Manager& rman, Ranges< Uint32_Index >& ranges)
18588 q std::vector< Node_Skeleton::Id_Type > ids = filtered_assign< Node_Skeleton::Id_Type >(stmt->get_refs());
18688 q std::vector< Uint32_Index > req = get_indexes_< Uint32_Index, Node_Skeleton >(ids, rman);
18888 q ranges = Ranges< Uint32_Index >();
189238 q for (std::vector< Uint32_Index >::const_iterator it = req.begin(); it != req.end(); ++it)
190158 q ranges.push_back(*it, ++Uint32_Index(*it));
19188 q ranges.sort();
19388 q return true;
197bool Id_Query_Constraint::get_ranges(Resource_Manager& rman, Ranges< Uint31_Index >& ranges)
1991311 q std::vector< Uint31_Index > req;
2001311 q ranges = Ranges< Uint31_Index >();
2031311 q std::vector< Way_Skeleton::Id_Type > ids = filtered_assign< Way_Skeleton::Id_Type >(stmt->get_refs());
2041311 q get_indexes_< Uint31_Index, Way_Skeleton >(ids, rman).swap(req);
2053011 q for (std::vector< Uint31_Index >::const_iterator it = req.begin(); it != req.end(); ++it)
2061711 q ranges.push_back(*it, inc(*it));
2101311 q std::vector< Relation_Skeleton::Id_Type > ids = filtered_assign< Relation_Skeleton::Id_Type >(stmt->get_refs());
2111311 q get_indexes_< Uint31_Index, Relation_Skeleton >(ids, rman).swap(req);
2123011 q for (std::vector< Uint31_Index >::const_iterator it = req.begin(); it != req.end(); ++it)
2131711 q ranges.push_back(*it, inc(*it));
2161311 q ranges.sort();
2181311 q return true;
222void Id_Query_Constraint::filter(Resource_Manager& rman, Set& into)
2243128 q filter_elems(stmt->get_refs(), into.nodes);
2253128 q filter_elems(stmt->get_refs(), into.attic_nodes);
2273128 q filter_elems(stmt->get_refs(), into.ways);
2283128 q filter_elems(stmt->get_refs(), into.attic_ways);
2303128 q filter_elems(stmt->get_refs(), into.relations);
2313128 q filter_elems(stmt->get_refs(), into.attic_relations);
2333128 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)
240441103 q : Output_Statement(line_number_)
242441103 q std::map< std::string, std::string > attributes;
244441103 q attributes["into"] = "_";
245441103 q attributes["type"] = "";
246441103 q attributes["ref"] = "";
247441103 q attributes["lower"] = "";
248441103 q attributes["upper"] = "";
250441103 q for (std::map<std::string, std::string>::const_iterator it = input_attributes.begin();
2512056103 q it != input_attributes.end(); ++it)
2521615103 q {
2531615103 q if (it->first.find("ref_") == 0)
25432843 q attributes[it->first] = "";
2551615103 q }
257441103 q Statement::eval_attributes_array(get_name(), attributes, input_attributes);
259441103 q set_output(attributes["into"]);
261441103 q if (attributes["type"] == "node")
26216241 q type = Statement::NODE;
26327962 q else if (attributes["type"] == "way")
26413633 q type = Statement::WAY;
26514329 q else if (attributes["type"] == "relation")
26613928 q type = Statement::RELATION;
26741 q else if (attributes["type"] == "area")
26941 q type = Statement::AREA;
27041 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 }
283441103 q uint64 ref = atoll(attributes["ref"].c_str());
285441103 q if (ref > 0)
286438102 q refs.push_back(ref);
288441103 q for (std::map< std::string, std::string >::iterator it = attributes.begin();
2892974103 q it != attributes.end(); ++it)
2902533103 q {
2912533103 q if (it->first.find("ref_") == 0)
29232843 q {
29332843 q ref = atoll(it->second.c_str());
29432843 q if (ref > 0)
29532843 q refs.push_back(ref);
29632843 q }
2972533103 q }
299441103 q uint64 lower = atoll(attributes["lower"].c_str());
300441103 q uint64 upper = atoll(attributes["upper"].c_str());
302441103 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;
313438102 q else
315438102 q lower = ref;
316438102 q upper = ++ref;
319441103 q if (lower > 0 && upper > 0)
321876102 q for (uint64 i = lower; i < upper; ++i)
322438102 q refs.push_back(i);
325441103 q std::sort(refs.begin(), refs.end());
326441103 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)
3888972 q std::vector< typename Skeleton::Id_Type > ids = filtered_assign< typename Skeleton::Id_Type >(refs);
3898972 q std::vector< Index > req = get_indexes_< Index, Skeleton >(ids, rman);
3918972 q Request_Context context(stmt, rman);
3928972 q collect_items_discrete(context, req, Id_Predicate< Skeleton >(ids), current_result, attic_result);
3948972 q if (rman.get_desired_timestamp() != NOW)
3953735 q filter_attic_elements(rman, rman.get_desired_timestamp(), current_result, attic_result);
399void Id_Query_Statement::execute(Resource_Manager& rman)
4018972 q Set into;
4038972 q if (type == NODE)
4042928 q get_elements(refs, this, rman, into.nodes, into.attic_nodes);
4056044 q else if (type == WAY)
4063024 q get_elements(refs, this, rman, into.ways, into.attic_ways);
4073020 q else if (type == RELATION)
4083020 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 }
4158972 q transfer_output(rman, into);
4168972 q rman.health_check(*this);
420Id_Query_Statement::~Id_Query_Statement()
422441103 q for (std::vector< Query_Constraint* >::const_iterator it = constraints.begin();
423525103 q it != constraints.end(); ++it)
4248428 q delete *it;
428Query_Constraint* Id_Query_Statement::get_query_constraint()
4308428 q constraints.push_back(new Id_Query_Constraint(*this));
4318428 q return constraints.back();