Overpass API regression suite coverage (Nova Scotia) / 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:
307962 q Query_Filter_Strategy delivers_data(Resource_Manager& rman) { return ids_required; }
3221666 q Filter_Constraint(Filter_Statement& stmt_) : stmt(&stmt_) {}
333124 q bool get_ranges(Resource_Manager& rman, Ranges< Uint32_Index >& ranges) { return false; }
345835 q bool get_ranges(Resource_Manager& rman, Ranges< Uint31_Index >& ranges) { return false; }
357962 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)
4763262 q{
4863262 q for (typename std::map< Index, std::vector< Maybe_Attic > >::iterator it_idx = items.begin();
4945940962 q it_idx != items.end(); ++it_idx)
5045877757 q {
5145877757 q std::vector< Maybe_Attic > local_into;
5245877757 q local_into.reserve(it_idx->second.size());
5445877757 q for (typename std::vector< Maybe_Attic >::const_iterator it_elem = it_idx->second.begin();
55199576157 q it_elem != it_idx->second.end(); ++it_elem)
56153698456 q {
57153698456 q if (string_represents_boolean_true(
58153698456 q task.eval(into_context.get_context(it_idx->first, *it_elem), 0)))
5932866933 q local_into.push_back(*it_elem);
60153698456 q }
6245877757 q local_into.swap(it_idx->second);
6345877757 q }
6463262 q}
67void Filter_Constraint::filter(const Statement& query, Resource_Manager& rman, Set& into)
697962 q if (!stmt || !stmt->get_criterion())
700 return;
727962 q Requested_Context requested_context = stmt->get_criterion()->request_context();
737962 q Prepare_Task_Context context(requested_context, query, rman);
757962 q Owner< Eval_Task > task(stmt->get_criterion()->get_string_task(context, 0));
777962 q Set_With_Context into_context;
787962 q into_context.name = "";
797962 q into_context.parent = &context;
807962 q into_context.prefetch(requested_context.object_usage, into, query, rman);
827962 q if (task)
837962 q {
847962 q eval_elems(into.nodes, into_context, *task);
857962 q eval_elems(into.attic_nodes, into_context, *task);
867962 q eval_elems(into.ways, into_context, *task);
877962 q eval_elems(into.attic_ways, into_context, *task);
887962 q eval_elems(into.relations, into_context, *task);
897962 q eval_elems(into.attic_relations, into_context, *task);
907962 q eval_elems(into.areas, into_context, *task);
917962 q eval_elems(into.deriveds, into_context, *task);
927962 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)
10730069 q Statement* filter = 0;
10830069 q uint line_nr = tree_it->line_col.first;
11030069 q Statement* criterion = 0;
11130069 q if (tree_it->token == ":" && tree_it->rhs && tree_it->lhs && tree_it.lhs()->token == "if")
11229268 q criterion = stmt_factory.create_evaluator(
11329268 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));
12130069 q if (criterion)
12228866 q {
12328866 q std::map< std::string, std::string > attributes;
12428866 q filter = new Filter_Statement(line_nr, attributes, global_settings);
12528866 q if (filter)
12628866 q filter->add_statement(criterion, "");
12728866 q }
12930069 q return filter;
133Filter_Statement::Filter_Statement
134 (int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings)
13528866 q : Output_Statement(line_number_), criterion(0)
13728866 q std::map< std::string, std::string > attributes;
13928866 q eval_attributes_array(get_name(), attributes, input_attributes);
143Filter_Statement::~Filter_Statement()
14528866 q for (std::vector< Query_Constraint* >::const_iterator it = constraints.begin();
14650466 q it != constraints.end(); ++it)
14721666 q delete *it;
151void Filter_Statement::add_statement(Statement* statement, std::string text)
15328866 q Evaluator* tag_value = dynamic_cast< Evaluator* >(statement);
15428866 q if (tag_value)
15528866 q {
15628866 q if (!criterion)
15728866 q criterion = tag_value;
1580 else
1590 add_static_error("A filter statement can have at most one evaluator statement.");
16028866 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()
18021666 q constraints.push_back(new Filter_Constraint(*this));
18121666 q return constraints.back();