Overpass API regression suite coverage (Nova Scotia) / overpass_api/statements/changed.cc

overpass_api/statements/changed.cc

183 of 256 instrumented lines run by the suite (71.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 <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 "../data/filenames.h"
27#include "changed.h"
30bool Changed_Statement::area_query_exists_ = false;
33Changed_Statement::Statement_Maker Changed_Statement::statement_maker;
34Changed_Statement::Criterion_Maker Changed_Statement::criterion_maker;
37Statement* Changed_Statement::Criterion_Maker::create_criterion(const Token_Node_Ptr& tree_it,
38 const std::string& type, const std::string& into,
39 Statement::Factory& stmt_factory, Parsed_Query& global_settings, Error_Output* error_output)
415213 q std::string since;
425213 q std::string until;
435213 q uint line_nr = tree_it->line_col.first;
455213 q if (tree_it->token == ":" && tree_it->rhs)
464411 q {
474411 q since = decode_json(tree_it.rhs()->token, error_output);
484411 q until = since;
494411 q }
5082 q else if (tree_it->token == "," && tree_it->lhs && tree_it.lhs()->token == ":" && tree_it.lhs()->rhs
5182 q && tree_it->rhs)
5382 q since = decode_json(tree_it.lhs().rhs()->token, error_output);
5482 q until = decode_json(tree_it.rhs()->token, error_output);
560 else if (tree_it->token == "changed")
570 {
580 since = "auto";
590 until = "auto";
600 }
610 else if (tree_it->token == ":")
620 {
630 if (error_output)
640 error_output->add_parse_error("Date required after \"changed\" with colon",
650 tree_it->line_col.first);
660 return 0;
670 }
680 else
690 {
700 if (error_output)
710 error_output->add_parse_error("Unexpected token \"" + tree_it->token + "\" after \"changed\"",
720 tree_it->line_col.first);
730 return 0;
740 }
765213 q std::map< std::string, std::string > attributes;
775213 q attributes["since"] = since;
785213 q attributes["until"] = until;
795213 q return new Changed_Statement(line_nr, attributes, global_settings);
83template< class TIndex, class TObject >
84void filter_elems(const std::vector< typename TObject::Id_Type >& ids, std::map< TIndex, std::vector< TObject > >& elems)
8510417 q{
8610417 q for (typename std::map< TIndex, std::vector< TObject > >::iterator it = elems.begin();
873232817 q it != elems.end(); ++it)
883222412 q {
893222412 q std::vector< TObject > local_into;
903222412 q for (typename std::vector< TObject >::const_iterator iit = it->second.begin();
919693812 q iit != it->second.end(); ++iit)
926471412 q {
936471412 q if (std::binary_search(ids.begin(), ids.end(), iit->id))
946471412 q local_into.push_back(*iit);
956471412 q }
963222412 q it->second.swap(local_into);
973222412 q }
9810417 q}
101template< typename Index, typename Skeleton, typename Id_Predicate >
102std::vector< typename Skeleton::Id_Type > collect_changed_elements
103 (uint64 since, uint64 until,
104 const Id_Predicate& relevant, Resource_Manager& rman)
1067013 q std::vector< typename Skeleton::Id_Type > ids;
1087013 q Ranges< Timestamp > ranges{ Timestamp(since), Timestamp(until) };
1097013 q auto changelog_db_idx = rman.get_transaction()->data_index(changelog_file_properties< Skeleton >());
1107013 q if (changelog_db_idx->get_file_format_version() <= 7561)
1110 {
1120 Block_Backend< Timestamp, Change_Entry< typename Skeleton::Id_Type > > changelog_db(changelog_db_idx);
1130 for (auto it = changelog_db.range_begin(ranges); !it.is_end(); ++it)
1140 {
1150 if (relevant(it.object().elem_id))
1160 ids.push_back(it.object().elem_id);
1170 }
1180 }
1197013 q else
1217013 q Block_Backend< Timestamp, typename Skeleton::Id_Type > changelog_db(changelog_db_idx);
1227010135913 q for (auto it = changelog_db.range_begin(ranges); !it.is_end(); ++it)
1237010128910 q {
1247010128910 q if (relevant(it.object()))
1251303537610 q ids.push_back(it.object());
1267010128910 q }
1297013 q std::sort(ids.begin(), ids.end());
1307013 q ids.erase(std::unique(ids.begin(), ids.end()), ids.end());
1317013 q return ids;
135//-----------------------------------------------------------------------------
137template< typename Id_Type >
138struct Trivial_Id_Predicate
1401273349210 q bool operator()(Id_Type id) const { return true; }
141};
144template< typename Index, typename Skeleton >
145struct Ids_In_Set_Predicate
147 Ids_In_Set_Predicate(
148 const std::map< Index, std::vector< Skeleton > >& current,
149 const std::map< Index, std::vector< Attic< Skeleton > > >& attic);
151 bool operator()(typename Skeleton::Id_Type id) const
1525736779710 q { return std::binary_search(set_ids.begin(), set_ids.end(), id); }
154private:
155 std::vector< typename Skeleton::Id_Type > set_ids;
156};
159template< typename Index, typename Skeleton >
160Ids_In_Set_Predicate< Index, Skeleton >::Ids_In_Set_Predicate(
161 const std::map< Index, std::vector< Skeleton > >& current,
162 const std::map< Index, std::vector< Attic< Skeleton > > >& attic)
1644813 q for (typename std::map< Index, std::vector< Skeleton > >::const_iterator it_idx = current.begin();
1651994713 q it_idx != current.end(); ++it_idx)
1661989910 q {
1671989910 q for (typename std::vector< Skeleton >::const_iterator it_elem = it_idx->second.begin();
1686432510 q it_elem != it_idx->second.end(); ++it_elem)
1694442610 q set_ids.push_back(it_elem->id);
1701989910 q }
1724813 q for (typename std::map< Index, std::vector< Attic< Skeleton > > >::const_iterator it_idx = attic.begin();
1731236713 q it_idx != attic.end(); ++it_idx)
174123195 q {
175123195 q for (typename std::vector< Attic< Skeleton > >::const_iterator it_elem = it_idx->second.begin();
176326005 q it_elem != it_idx->second.end(); ++it_elem)
177202815 q set_ids.push_back(it_elem->id);
178123195 q }
1804813 q std::sort(set_ids.begin(), set_ids.end());
1814813 q set_ids.erase(std::unique(set_ids.begin(), set_ids.end()), set_ids.end());
185class Changed_Constraint : public Query_Constraint
187 public:
1883913 q Changed_Constraint(Changed_Statement& stmt_) : stmt(&stmt_) {}
1901613 q Query_Filter_Strategy delivers_data(Resource_Manager& rman) { return prefer_ranges; }
192 bool get_node_ids
193 (Resource_Manager& rman, std::vector< Node_Skeleton::Id_Type >& ids);
194 bool get_way_ids
195 (Resource_Manager& rman, std::vector< Way_Skeleton::Id_Type >& ids);
196 bool get_relation_ids
197 (Resource_Manager& rman, std::vector< Relation_Skeleton::Id_Type >& ids);
199 void filter(Resource_Manager& rman, Set& into);
2000 virtual ~Changed_Constraint() {}
202 private:
203 Changed_Statement* stmt;
204};
207bool Changed_Constraint::get_node_ids(Resource_Manager& rman, std::vector< Node_Skeleton::Id_Type >& ids)
20954 q ids = collect_changed_elements< Uint32_Index, Node_Skeleton >(
21054 q stmt->get_since(rman), stmt->get_until(rman), Trivial_Id_Predicate< Node_Skeleton::Id_Type >(), rman);
21154 q return true;
215bool Changed_Constraint::get_way_ids(Resource_Manager& rman, std::vector< Way_Skeleton::Id_Type >& ids)
21797 q ids = collect_changed_elements< Uint31_Index, Way_Skeleton >(
21897 q stmt->get_since(rman), stmt->get_until(rman), Trivial_Id_Predicate< Way_Skeleton::Id_Type >(), rman);
21997 q return true;
223bool Changed_Constraint::get_relation_ids(Resource_Manager& rman, std::vector< Relation_Skeleton::Id_Type >& ids)
22587 q ids = collect_changed_elements< Uint31_Index, Relation_Skeleton >(
22687 q stmt->get_since(rman), stmt->get_until(rman), Trivial_Id_Predicate< Relation_Skeleton::Id_Type >(), rman);
22787 q return true;
231void Changed_Constraint::filter(Resource_Manager& rman, Set& into)
2331613 q if (!stmt->trivial())
2351613 q std::vector< Node_Skeleton::Id_Type > ids =
2361613 q collect_changed_elements< Uint32_Index, Node_Skeleton >
2371613 q (stmt->get_since(rman), stmt->get_until(rman),
2381613 q Ids_In_Set_Predicate< Uint32_Index, Node_Skeleton >(into.nodes, into.attic_nodes), rman);
2401613 q filter_elems(ids, into.nodes);
2411613 q filter_elems(ids, into.attic_nodes);
2441613 q if (!stmt->trivial())
2461613 q std::vector< Way_Skeleton::Id_Type > ids =
2471613 q collect_changed_elements< Uint31_Index, Way_Skeleton >
2481613 q (stmt->get_since(rman), stmt->get_until(rman),
2491613 q Ids_In_Set_Predicate< Uint31_Index, Way_Skeleton >(into.ways, into.attic_ways), rman);
2511613 q filter_elems(ids, into.ways);
2521613 q filter_elems(ids, into.attic_ways);
2551613 q if (!stmt->trivial())
2571613 q std::vector< Relation_Skeleton::Id_Type > ids =
2581613 q collect_changed_elements< Uint31_Index, Relation_Skeleton >
2591613 q (stmt->get_since(rman), stmt->get_until(rman),
2601613 q Ids_In_Set_Predicate< Uint31_Index, Relation_Skeleton >(into.relations, into.attic_relations), rman);
2621613 q filter_elems(ids, into.relations);
2631613 q filter_elems(ids, into.attic_relations);
2661613 q into.areas.clear();
269//-----------------------------------------------------------------------------
271Changed_Statement::Changed_Statement
272 (int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings)
2735213 q : Output_Statement(line_number_), since(NOW), until(NOW), behave_trivial(false)
2755213 q std::map< std::string, std::string > attributes;
2775213 q attributes["into"] = "_";
2785213 q attributes["since"] = "auto";
2795213 q attributes["until"] = "auto";
2815213 q Statement::eval_attributes_array(get_name(), attributes, input_attributes);
2835213 q set_output(attributes["into"]);
2855213 q if (attributes["since"] == "init")
2860 behave_trivial = true;
2885213 q if (!behave_trivial && ((attributes["since"] == "auto") ^ (attributes["until"] == "auto")))
2890 {
2900 std::ostringstream temp;
2910 temp<<"The attributes \"since\" and \"until\" must be set either both or none.";
2920 add_static_error(temp.str());
2930 }
2955213 q std::string timestamp = attributes["since"];
2965213 q if (timestamp.size() >= 19)
2975213 q since = Timestamp(timestamp).timestamp;
2995213 q if (!behave_trivial && attributes["since"] != "auto" && (since == 0 || since == NOW))
3000 add_static_error("The attribute \"since\" must contain a timestamp exactly in the form \"yyyy-mm-ddThh:mm:ssZ\".");
3025213 q timestamp = attributes["until"];
3035213 q if (timestamp.size() >= 19)
3045213 q until = Timestamp(timestamp).timestamp;
3065213 q if (!behave_trivial && attributes["until"] != "auto" && (until == 0 || until == NOW))
3070 add_static_error("The attribute \"until\" must contain a timestamp exactly in the form \"yyyy-mm-ddThh:mm:ssZ\".");
311uint64 Changed_Statement::get_since(Resource_Manager& rman) const
3137013 q if (since == NOW && until == NOW)
314 // We have zero arguments on changed.
3150 {
3160 if (rman.get_diff_from_timestamp() != rman.get_diff_to_timestamp())
317 // We are on diff mode
3180 return rman.get_diff_from_timestamp() + (rman.get_diff_from_timestamp() == NOW ? 0 : 1);
3190 else if (rman.get_desired_timestamp() != NOW)
320 // We are in attic mode
3210 return rman.get_desired_timestamp() + 1;
3220 else
323 // We are in present mode.
324 // Trigger an empty result on purpose
3250 return NOW;
3260 }
3277013 q else if (since == until)
328 // We have one argument on changed only.
3306111 q if (rman.get_diff_from_timestamp() != rman.get_diff_to_timestamp())
331 // We are on diff mode
332263 q {
333263 q if (since == rman.get_diff_from_timestamp() || since == rman.get_diff_to_timestamp())
334 // If the only parameter is equal to the time range then silently ignore it
3350 return rman.get_diff_from_timestamp() + 1;
336263 q else
337 // Trigger an empty result on purpose
338263 q return NOW;
339263 q }
340358 q else if (rman.get_desired_timestamp() != NOW)
341 // We are in attic mode
342164 q return std::min(since, rman.get_desired_timestamp()) + 1;
343194 q else
344 // We are in present mode.
345194 q return since + 1;
34792 q else
348 // We have two arguments on changed.
34992 q return since + 1;
353uint64 Changed_Statement::get_until(Resource_Manager& rman) const
3557013 q if (since == NOW && until == NOW)
356 // We have zero arguments on changed.
3570 {
3580 if (rman.get_diff_from_timestamp() != rman.get_diff_to_timestamp())
359 // We are on diff mode
3600 return rman.get_diff_to_timestamp() + (rman.get_diff_to_timestamp() == NOW ? 0 : 1);
3610 else if (rman.get_desired_timestamp() != NOW)
362 // We are in attic mode
3630 return NOW;
3640 else
365 // We are in present mode.
366 // Trigger an empty result on purpose
3670 return NOW;
3680 }
3697013 q else if (since == until)
370 // We have one argument on changed only.
3726111 q if (rman.get_diff_from_timestamp() != rman.get_diff_to_timestamp())
373 // We are on diff mode
374263 q {
375263 q if (since == rman.get_diff_from_timestamp() || since == rman.get_diff_to_timestamp())
376 // If the only parameter is equal to the time range then silently ignore it
3770 return rman.get_diff_to_timestamp() + (rman.get_diff_to_timestamp() == NOW ? 0 : 1);
378263 q else
379 // Trigger an empty result on purpose
380263 q return NOW;
381263 q }
382358 q else if (rman.get_desired_timestamp() != NOW)
383 // We are in attic mode
384164 q return std::max(until, rman.get_desired_timestamp()) + 1;
385194 q else
386 // We are in present mode.
387194 q return NOW;
38992 q else
390 // We have two arguments on changed.
39192 q return until + 1;
395template< typename Index, typename Skeleton >
396void get_elements(Changed_Statement& stmt, Resource_Manager& rman,
397 std::map< Index, std::vector< Skeleton > >& current_result,
398 std::map< Index, std::vector< Attic< Skeleton > > >& attic_result)
3990{
4000 std::vector< typename Skeleton::Id_Type > ids =
4010 collect_changed_elements< Index, Skeleton >(stmt.get_since(rman), stmt.get_until(rman),
4020 Trivial_Id_Predicate< typename Skeleton::Id_Type >(), rman);
4030 std::vector< Index > req = get_indexes_< Index, Skeleton >(ids, rman);
4050 Request_Context context(&stmt, rman);
4060 collect_items_discrete(context, req, Id_Predicate< Skeleton >(ids), current_result, attic_result);
4080 if (rman.get_desired_timestamp() != NOW)
4090 filter_attic_elements(rman, rman.get_desired_timestamp(), current_result, attic_result);
4100}
413void Changed_Statement::execute(Resource_Manager& rman)
4140{
4150 Set into;
4170 get_elements(*this, rman, into.nodes, into.attic_nodes);
4180 get_elements(*this, rman, into.ways, into.attic_ways);
4190 get_elements(*this, rman, into.relations, into.attic_relations);
4200 into.areas.clear();
4220 transfer_output(rman, into);
4230 rman.health_check(*this);
4240}
427Changed_Statement::~Changed_Statement()
4295213 q for (std::vector< Query_Constraint* >::const_iterator it = constraints.begin();
4309113 q it != constraints.end(); ++it)
4313913 q delete *it;
435Query_Constraint* Changed_Statement::get_query_constraint()
4373913 q constraints.push_back(new Changed_Constraint(*this));
4383913 q return constraints.back();