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

overpass_api/statements/filter.cc

87 of 100 instrumented lines run by the suite (87.0%) 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 "filter.h"
20#include "tag_value.h"
21#include "../data/tag_store.h"
23#include <algorithm>
24#include <sstream>
27class Filter_Constraint : public Query_Constraint
29 public:
308773 q Query_Filter_Strategy delivers_data(Resource_Manager& rman) { return ids_required; }
3223774 q Filter_Constraint(Filter_Statement& stmt_) : stmt(&stmt_) {}
332722 q bool get_ranges(Resource_Manager& rman, Ranges< Uint32_Index >& ranges) { return false; }
344335 q bool get_ranges(Resource_Manager& rman, Ranges< Uint31_Index >& ranges) { return false; }
358773 q void filter(Resource_Manager& rman, Set& into) {}
36 void filter(const Statement& query, Resource_Manager& rman, Set& into);
370 virtual ~Filter_Constraint() {}
39 private:
40 Filter_Statement* stmt;
41};
44template< typename Index, typename Maybe_Attic >
45void eval_elems(std::map< Index, std::vector< Maybe_Attic > >& items,
46 Set_With_Context& into_context, Eval_Task& task)
4769673 q{
4869673 q for (typename std::map< Index, std::vector< Maybe_Attic > >::iterator it_idx = items.begin();
4924377973 q it_idx != items.end(); ++it_idx)
5024308363 q {
5124308363 q std::vector< Maybe_Attic > local_into;
5224308363 q local_into.reserve(it_idx->second.size());
5424308363 q for (typename std::vector< Maybe_Attic >::const_iterator it_elem = it_idx->second.begin();
5593699663 q it_elem != it_idx->second.end(); ++it_elem)
5669391363 q {
5769391363 q if (string_represents_boolean_true(
5869391363 q task.eval(into_context.get_context(it_idx->first, *it_elem), 0)))
5927288834 q local_into.push_back(*it_elem);
6069391363 q }
6224308363 q local_into.swap(it_idx->second);
6324308363 q }
6469673 q}
67void Filter_Constraint::filter(const Statement& query, Resource_Manager& rman, Set& into)
698773 q if (!stmt || !stmt->get_criterion())
700 return;
728773 q Requested_Context requested_context = stmt->get_criterion()->request_context();
738773 q Prepare_Task_Context context(requested_context, query, rman);
758773 q Owner< Eval_Task > task(stmt->get_criterion()->get_string_task(context, 0));
778773 q Set_With_Context into_context;
788773 q into_context.name = "";
798773 q into_context.parent = &context;
808773 q into_context.prefetch(requested_context.object_usage, into, query, rman);
828773 q if (task)
838773 q {
848773 q eval_elems(into.nodes, into_context, *task);
858773 q eval_elems(into.attic_nodes, into_context, *task);
868773 q eval_elems(into.ways, into_context, *task);
878773 q eval_elems(into.attic_ways, into_context, *task);
888773 q eval_elems(into.relations, into_context, *task);
898773 q eval_elems(into.attic_relations, into_context, *task);
908773 q eval_elems(into.areas, into_context, *task);
918773 q eval_elems(into.deriveds, into_context, *task);
928773 q }
96//-----------------------------------------------------------------------------
99Filter_Statement::Statement_Maker Filter_Statement::statement_maker;
100Filter_Statement::Criterion_Maker Filter_Statement::criterion_maker;
103Statement* Filter_Statement::Criterion_Maker::create_criterion(const Token_Node_Ptr& tree_it,
104 const std::string& type, const std::string& into,
105 Statement::Factory& stmt_factory, Parsed_Query& global_settings, Error_Output* error_output)
10732877 q Statement* filter = 0;
10832877 q uint line_nr = tree_it->line_col.first;
11032877 q Statement* criterion = 0;
11132877 q if (tree_it->token == ":" && tree_it->rhs && tree_it->lhs && tree_it.lhs()->token == "if")
11232076 q criterion = stmt_factory.create_evaluator(
11332076 q tree_it.rhs(), Statement::elem_eval_possible, Statement::Single_Return_Type_Checker(Statement::string));
11482 q else
11682 q Token_Tree pruned(tree_it, find_leftmost_but_one_token(tree_it));
11782 q criterion = stmt_factory.create_evaluator(
11882 q Token_Node_Ptr(pruned).rhs(), Statement::elem_eval_possible,
11982 q Statement::Single_Return_Type_Checker(Statement::string));
12132877 q if (criterion)
12231674 q {
12331674 q std::map< std::string, std::string > attributes;
12431674 q filter = new Filter_Statement(line_nr, attributes, global_settings);
12531674 q if (filter)
12631674 q filter->add_statement(criterion, "");
12731674 q }
12932877 q return filter;
133Filter_Statement::Filter_Statement
134 (int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings)
13531674 q : Output_Statement(line_number_), criterion(0)
13731674 q std::map< std::string, std::string > attributes;
13931674 q eval_attributes_array(get_name(), attributes, input_attributes);
143Filter_Statement::~Filter_Statement()
14531674 q for (std::vector< Query_Constraint* >::const_iterator it = constraints.begin();
14655374 q it != constraints.end(); ++it)
14723774 q delete *it;
151void Filter_Statement::add_statement(Statement* statement, std::string text)
15331674 q Evaluator* tag_value = dynamic_cast< Evaluator* >(statement);
15431674 q if (tag_value)
15531674 q {
15631674 q if (!criterion)
15731674 q criterion = tag_value;
1580 else
1590 add_static_error("A filter statement can have at most one evaluator statement.");
16031674 q }
1610 else
1620 substatement_error(get_name(), statement);
166void Filter_Statement::execute(Resource_Manager& rman)
1670{
1680 Set into;
1700 Filter_Constraint constraint(*this);
1710 constraint.filter(rman, into);
1730 transfer_output(rman, into);
1740 rman.health_check(*this);
1750}
178Query_Constraint* Filter_Statement::get_query_constraint()
18023774 q constraints.push_back(new Filter_Constraint(*this));
18123774 q return constraints.back();