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

overpass_api/statements/user.cc

0 of 355 instrumented lines run by the suite (0.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 "../../template_db/block_backend.h"
20#include "../../template_db/random_file.h"
21#include "../core/settings.h"
22#include "../data/bbox_filter.h"
23#include "../data/collect_members.h"
24#include "../data/meta_collector.h"
25#include "user.h"
27#include <algorithm>
28#include <cmath>
29#include <sstream>
30#include <vector>
33//-----------------------------------------------------------------------------
35class User_Constraint : public Query_Constraint
37 public:
380 User_Constraint(User_Statement& user_) : user(&user_) {}
40 Query_Filter_Strategy delivers_data(Resource_Manager& rman) override
410 { return user->get_criterion() == User_Statement::last ? ids_required : prefer_ranges; }
43 bool get_ranges(Resource_Manager& rman, Ranges< Uint31_Index >& ranges) override;
44 bool get_ranges(Resource_Manager& rman, Ranges< Uint32_Index >& ranges) override;
46 bool get_node_ids(Resource_Manager& rman, std::vector< Node_Skeleton::Id_Type >& ids) override;
47 bool get_way_ids(Resource_Manager& rman, std::vector< Way_Skeleton::Id_Type >& ids) override;
48 bool get_relation_ids(Resource_Manager& rman, std::vector< Relation_Skeleton::Id_Type >& ids) override;
50 void filter(const Statement& query, Resource_Manager& rman, Set& into) override;
510 virtual ~User_Constraint() {}
53 private:
54 User_Statement* user;
55};
58template< typename TIndex, typename TObject >
59void user_filter_map
60 (std::map< TIndex, std::vector< TObject > >& modify,
61 Resource_Manager& rman, const std::set< Uint32_Index >& user_ids, File_Properties* file_properties)
620{
630 if (modify.empty())
640 return;
660 Meta_Collector< TIndex, typename TObject::Id_Type > meta_collector
670 (modify, *rman.get_transaction(), file_properties);
690 for (auto it = modify.begin(); it != modify.end(); ++it)
700 {
710 std::vector< TObject > local_into;
720 for (auto iit = it->second.begin();
730 iit != it->second.end(); ++iit)
740 {
750 const OSM_Element_Metadata_Skeleton< typename TObject::Id_Type >* meta_skel
760 = meta_collector.get(it->first, iit->id);
770 if ((meta_skel) && (user_ids.find(meta_skel->user_id) != user_ids.end()))
780 local_into.push_back(*iit);
790 }
800 it->second.swap(local_into);
810 }
820}
85template< typename TIndex, typename TObject >
86void user_filter_map_attic
87 (std::map< TIndex, std::vector< TObject > >& modify,
88 Resource_Manager& rman, const std::set< Uint32_Index >& user_ids,
89 File_Properties* current_file_properties, File_Properties* attic_file_properties)
900{
910 if (modify.empty())
920 return;
940 Meta_Collector< TIndex, typename TObject::Id_Type > current_meta_collector
950 (modify, *rman.get_transaction(), current_file_properties);
960 Meta_Collector< TIndex, typename TObject::Id_Type > attic_meta_collector
970 (modify, *rman.get_transaction(), attic_file_properties);
990 for (auto it = modify.begin(); it != modify.end(); ++it)
1000 {
1010 std::vector< TObject > local_into;
1020 for (auto iit = it->second.begin();
1030 iit != it->second.end(); ++iit)
1040 {
1050 const OSM_Element_Metadata_Skeleton< typename TObject::Id_Type >* meta_skel
1060 = current_meta_collector.get(it->first, iit->id);
1070 if (!meta_skel || !(meta_skel->timestamp < iit->timestamp))
1080 meta_skel = attic_meta_collector.get(it->first, iit->id, iit->timestamp);
1090 if ((meta_skel) && (user_ids.find(meta_skel->user_id) != user_ids.end()))
1100 local_into.push_back(*iit);
1110 }
1120 it->second.swap(local_into);
1130 }
1140}
117template< typename Index, typename Object >
118std::vector< typename Object::Id_Type > touched_ids_by_users(
119 Resource_Manager& rman, const Ranges< Index >& ranges, const std::set< Uint32_Index >& user_ids)
1200{
1210 std::vector< typename Object::Id_Type > result;
1230 {
1240 Block_Backend< Index, OSM_Element_Metadata_Skeleton< typename Object::Id_Type > > cur_meta_db(
1250 rman.get_transaction()->data_index(current_meta_file_properties< Object >()));
1260 for (auto it = cur_meta_db.range_begin(ranges); !(it == cur_meta_db.range_end()); ++it)
1270 {
1280 if (user_ids.find(it.object().user_id) != user_ids.end()
1290 && (result.empty() || !(result.back() == it.object().ref)))
1300 result.push_back(it.object().ref);
1310 }
1320 }
1330 {
1340 Block_Backend< Index, OSM_Element_Metadata_Skeleton< typename Object::Id_Type > > cur_meta_db(
1350 rman.get_transaction()->data_index(attic_meta_file_properties< Object >()));
1360 for (auto it = cur_meta_db.range_begin(ranges); !(it == cur_meta_db.range_end()); ++it)
1370 {
1380 if (user_ids.find(it.object().user_id) != user_ids.end()
1390 && (result.empty() || !(result.back() == it.object().ref)))
1400 result.push_back(it.object().ref);
1410 }
1420 }
1440 std::sort(result.begin(), result.end());
1450 result.erase(std::unique(result.begin(), result.end()), result.end());
1470 return result;
1480}
151void calc_ranges_32
152 (Ranges< Uint32_Index >& node_req,
153 const std::set< Uint32_Index >& user_ids, Transaction& transaction)
1540{
1560 Block_Backend< Uint32_Index, Uint31_Index, std::set< Uint32_Index >::const_iterator > user_db
1570 (transaction.data_index(meta_settings().USER_INDICES));
1580 for (auto user_it = user_db.discrete_begin(user_ids.begin(), user_ids.end());
1590 !(user_it == user_db.discrete_end()); ++user_it)
1600 {
1610 if ((user_it.object().val() & 0x80000000) == 0)
1620 node_req.push_back(Uint32_Index(user_it.object().val()), Uint32_Index(user_it.object().val() + 0x100));
1630 }
1640 node_req.sort();
1650}
168void calc_ranges_31
169 (Ranges< Uint31_Index >& other_req,
170 const std::set< Uint32_Index >& user_ids, Transaction& transaction)
1710{
1730 Block_Backend< Uint32_Index, Uint31_Index, std::set< Uint32_Index >::const_iterator > user_db
1740 (transaction.data_index(meta_settings().USER_INDICES));
1750 for (auto user_it = user_db.discrete_begin(user_ids.begin(), user_ids.end());
1760 !(user_it == user_db.discrete_end()); ++user_it)
1770 {
1780 if ((user_it.object().val() & 0x80000000) == 0)
1790 other_req.push_back(Uint31_Index(user_it.object().val()), Uint31_Index(user_it.object().val() + 0x100));
1800 else if ((user_it.object().val() & 0xff) == 0)
1810 other_req.push_back(Uint31_Index(user_it.object().val()), Uint31_Index(user_it.object().val() + 0x100));
1820 else
1830 other_req.push_back(Uint31_Index(user_it.object().val()), Uint31_Index(user_it.object().val() + 1));
1840 }
1850 other_req.sort();
1860}
189void calc_ranges_both
190 (Ranges< Uint32_Index >& node_req, Ranges< Uint31_Index >& other_req,
191 const std::set< Uint32_Index >& user_ids, Transaction& transaction)
1920{
1940 Block_Backend< Uint32_Index, Uint31_Index, std::set< Uint32_Index >::const_iterator > user_db
1950 (transaction.data_index(meta_settings().USER_INDICES));
1960 for (auto user_it = user_db.discrete_begin(user_ids.begin(), user_ids.end());
1970 !(user_it == user_db.discrete_end()); ++user_it)
1980 {
1990 if ((user_it.object().val() & 0x80000000) == 0)
2000 {
2010 node_req.push_back(Uint32_Index(user_it.object().val()), Uint32_Index(user_it.object().val() + 0x100));
2020 other_req.push_back(Uint31_Index(user_it.object().val()), Uint31_Index(user_it.object().val() + 0x100));
2030 }
2040 else if ((user_it.object().val() & 0xff) == 0)
2050 other_req.push_back(Uint31_Index(user_it.object().val()), Uint31_Index(user_it.object().val() + 0x100));
2060 else
2070 other_req.push_back(Uint31_Index(user_it.object().val()), Uint31_Index(user_it.object().val() + 1));
2080 }
2090 node_req.sort();
2100 other_req.sort();
2110}
214void User_Constraint::filter(const Statement& query, Resource_Manager& rman, Set& into)
2150{
2160 std::set< Uint32_Index > user_ids = user->get_ids(*rman.get_transaction());
2180 if (user->get_criterion() == User_Statement::last)
2190 {
2200 user_filter_map(into.nodes, rman, user_ids, meta_settings().NODES_META);
2210 user_filter_map(into.ways, rman, user_ids, meta_settings().WAYS_META);
2220 user_filter_map(into.relations, rman, user_ids, meta_settings().RELATIONS_META);
2240 if (!into.attic_nodes.empty())
2250 user_filter_map_attic(
2260 into.attic_nodes, rman, user_ids, meta_settings().NODES_META, attic_settings().NODES_META);
2280 if (!into.attic_ways.empty())
2290 user_filter_map_attic(
2300 into.attic_ways, rman, user_ids, meta_settings().WAYS_META, attic_settings().WAYS_META);
2320 if (!into.attic_relations.empty())
2330 user_filter_map_attic(
2340 into.attic_relations, rman, user_ids, meta_settings().RELATIONS_META, attic_settings().RELATIONS_META);
2350 }
2360 else
2370 {
2380 Ranges< Uint32_Index > node_ranges;
2390 Ranges< Uint31_Index > other_ranges;
2400 calc_ranges_both(node_ranges, other_ranges, user->get_ids(*rman.get_transaction()), *rman.get_transaction());
2420 if (!into.nodes.empty() || !into.attic_nodes.empty())
2430 Timeless< Uint32_Index, Node_Skeleton >{ into.nodes, into.attic_nodes }.filter_by_id(
2440 touched_ids_by_users< Uint32_Index, Node_Skeleton >(
2450 rman, node_ranges, user->get_ids(*rman.get_transaction())))
2460 .swap(into.nodes, into.attic_nodes);
2470 if (!into.ways.empty() || !into.attic_ways.empty())
2480 Timeless< Uint31_Index, Way_Skeleton >{ into.ways, into.attic_ways }.filter_by_id(
2490 touched_ids_by_users< Uint31_Index, Way_Skeleton >(
2500 rman, other_ranges, user->get_ids(*rman.get_transaction())))
2510 .swap(into.ways, into.attic_ways);
2520 if (!into.nodes.empty() || !into.attic_nodes.empty())
2530 Timeless< Uint31_Index, Relation_Skeleton >{ into.relations, into.attic_relations }.filter_by_id(
2540 touched_ids_by_users< Uint31_Index, Relation_Skeleton >(
2550 rman, other_ranges, user->get_ids(*rman.get_transaction())))
2560 .swap(into.relations, into.attic_relations);
2570 }
2590 into.areas.clear();
2600}
262//-----------------------------------------------------------------------------
265User_Statement::Statement_Maker User_Statement::statement_maker;
266User_Statement::Criterion_Maker User_Statement::criterion_maker;
269Statement* User_Statement::Criterion_Maker::create_criterion(const Token_Node_Ptr& input_tree,
270 const std::string& result_type, const std::string& into,
271 Statement::Factory& stmt_factory, Parsed_Query& global_settings, Error_Output* error_output)
2720{
2730 Token_Node_Ptr tree_it = input_tree;
2740 uint line_nr = tree_it->line_col.first;
2750 std::vector< std::string > users;
2770 while (tree_it->token == "," && tree_it->rhs && tree_it->lhs)
2780 {
2790 users.push_back(tree_it.rhs()->token);
2800 tree_it = tree_it.lhs();
2810 }
2830 if (tree_it->token == ":" && tree_it->rhs)
2840 users.push_back(tree_it.rhs()->token);
2860 std::reverse(users.begin(), users.end());
2880 std::map< std::string, std::string > attributes;
2890 attributes["into"] = into;
2900 attributes["type"] = result_type;
2920 std::string prefix;
2930 if (tree_it->lhs)
2940 {
2950 if (tree_it.lhs()->token == "uid_touched" || tree_it.lhs()->token == "user_touched")
2960 attributes["criterion"] = "touched";
2980 if (tree_it.lhs()->token == "user" || tree_it.lhs()->token == "user_touched")
2990 {
3000 for (std::vector< std::string >::iterator it = users.begin(); it != users.end(); ++it)
3010 *it = decode_json(*it, error_output);
3020 prefix = "name";
3030 }
3040 else
3050 prefix = "uid";
3060 }
3080 std::vector< std::string >::const_iterator it = users.begin();
3090 if (it != users.end())
3100 attributes[prefix] = *it;
3110 for (uint i = 0; it != users.end(); ++it)
3120 {
3130 std::ostringstream id;
3140 id<<prefix<<"_"<<++i;
3150 attributes[id.str()] = *it;
3160 }
3180 return new User_Statement(line_nr, attributes, global_settings);
3190}
322User_Statement::User_Statement
323 (int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings)
3240 : Output_Statement(line_number_), bbox_limitation(&global_settings.get_global_bbox_limitation()), criterion(last)
3250{
3260 std::map< std::string, std::string > attributes;
3280 attributes["into"] = "_";
3290 attributes["uid"] = "";
3300 attributes["name"] = "";
3310 attributes["type"] = "";
3320 attributes["criterion"] = "last";
3340 for (std::map<std::string, std::string>::const_iterator it = input_attributes.begin();
3350 it != input_attributes.end(); ++it)
3360 {
3370 if (it->first.find("name_") == 0 || it->first.find("uid_") == 0)
3380 attributes[it->first] = "";
3390 }
3410 eval_attributes_array(get_name(), attributes, input_attributes);
3430 set_output(attributes["into"]);
3450 std::string user_name = attributes["name"];
3460 uint32 user_id = atoll(attributes["uid"].c_str());
3480 if (user_name != "")
3490 user_names.insert(user_name);
3510 if (user_id != 0)
3520 user_ids.insert(user_id);
3540 for (std::map<std::string, std::string>::iterator it = attributes.begin();
3550 it != attributes.end(); ++it)
3560 {
3570 if (it->first.find("name_") == 0)
3580 {
3590 if (it->second != "")
3600 user_names.insert(it->second);
3610 }
3620 if (it->first.find("uid_") == 0)
3630 {
3640 user_id = atoll(it->second.c_str());
3650 if (user_id != 0)
3660 user_ids.insert(user_id);
3670 }
3680 }
3700 if (!(user_ids.empty() ^ user_names.empty()))
3710 add_static_error("Exactly one of the two attributes \"name\" and \"uid\" must be set.");
3730 if (attributes["criterion"] == "touched")
3740 criterion = touched;
3750 else if (attributes["criterion"] != "last")
3760 add_static_error("Atttribute \"criterion\" must have the value \"last\" or \"touched\".");
3780 result_type = attributes["type"];
3790}
382User_Statement::~User_Statement()
3830{
3840 for (std::vector< Query_Constraint* >::const_iterator it = constraints.begin();
3850 it != constraints.end(); ++it)
3860 delete *it;
3870}
390std::set< Uint32_Index > get_user_ids(const std::set< std::string >& user_names, Transaction& transaction)
3910{
3920 std::set< Uint32_Index > ids;
3940 Block_Backend< Uint32_Index, User_Data > user_db
3950 (transaction.data_index(meta_settings().USER_DATA));
3960 for (Block_Backend< Uint32_Index, User_Data >::Flat_Iterator
3970 user_it = user_db.flat_begin(); !(user_it == user_db.flat_end()); ++user_it)
3980 {
3990 if (user_names.find(user_it.object().name) != user_names.end())
4000 ids.insert(user_it.object().id);
4010 }
4020 return ids;
4030}
406std::set< Uint32_Index > User_Statement::get_ids(Transaction& transaction)
4070{
4080 if (!user_names.empty())
4090 user_ids = get_user_ids(user_names, transaction);
4110 return user_ids;
4120}
415bool User_Constraint::get_ranges(Resource_Manager& rman, Ranges< Uint32_Index >& ranges)
4160{
4170 if (user->get_criterion() == User_Statement::last)
4180 {
4190 calc_ranges_32(ranges, user->get_ids(*rman.get_transaction()), *rman.get_transaction());
4200 return true;
4210 }
4220 return false;
4230}
426bool User_Constraint::get_ranges(Resource_Manager& rman, Ranges< Uint31_Index >& ranges)
4270{
4280 if (user->get_criterion() == User_Statement::last)
4290 {
4300 calc_ranges_31(ranges, user->get_ids(*rman.get_transaction()), *rman.get_transaction());
4310 return true;
4320 }
4330 return false;
4340}
437bool User_Constraint::get_node_ids(Resource_Manager& rman, std::vector< Node_Skeleton::Id_Type >& ids)
4380{
4390 if (user->get_criterion() == User_Statement::touched)
4400 {
4410 Ranges< Uint32_Index > node_ranges;
4420 calc_ranges_32(node_ranges, user->get_ids(*rman.get_transaction()), *rman.get_transaction());
4440 touched_ids_by_users< Uint32_Index, Node_Skeleton >(
4450 rman, node_ranges, user->get_ids(*rman.get_transaction())).swap(ids);
4460 return true;
4470 }
4480 return false;
4490}
452bool User_Constraint::get_way_ids(Resource_Manager& rman, std::vector< Way_Skeleton::Id_Type >& ids)
4530{
4540 if (user->get_criterion() == User_Statement::touched)
4550 {
4560 Ranges< Uint31_Index > other_ranges;
4570 calc_ranges_31(other_ranges, user->get_ids(*rman.get_transaction()), *rman.get_transaction());
4590 touched_ids_by_users< Uint31_Index, Way_Skeleton >(
4600 rman, other_ranges, user->get_ids(*rman.get_transaction())).swap(ids);
4610 return true;
4620 }
4630 return false;
4640}
467bool User_Constraint::get_relation_ids(Resource_Manager& rman, std::vector< Relation_Skeleton::Id_Type >& ids)
4680{
4690 if (user->get_criterion() == User_Statement::touched)
4700 {
4710 Ranges< Uint31_Index > other_ranges;
4720 calc_ranges_31(other_ranges, user->get_ids(*rman.get_transaction()), *rman.get_transaction());
4740 touched_ids_by_users< Uint31_Index, Relation_Skeleton >(
4750 rman, other_ranges, user->get_ids(*rman.get_transaction())).swap(ids);
4760 return true;
4770 }
4780 return false;
4790}
482void User_Statement::execute(Resource_Manager& rman)
4830{
4840 Set into;
4850 User_Constraint constraint(*this);
4870 if ((result_type == "") || (result_type == "node"))
4880 {
4890 Ranges< Uint32_Index > ranges;
4900 constraint.get_ranges(rman, ranges);
4910 get_elements_from_db< Uint32_Index, Node_Skeleton >(ranges, *this, rman)
4920 .swap(into.nodes, into.attic_nodes);
4930 filter_attic_elements(rman, rman.get_desired_timestamp(), into.nodes, into.attic_nodes);
4940 }
4960 if ((result_type == "") || (result_type == "way"))
4970 {
4980 Ranges< Uint31_Index > ranges;
4990 constraint.get_ranges(rman, ranges);
5000 get_elements_from_db< Uint31_Index, Way_Skeleton >(ranges, *this, rman)
5010 .swap(into.ways, into.attic_ways);
5020 filter_attic_elements(rman, rman.get_desired_timestamp(), into.ways, into.attic_ways);
5030 }
5050 if ((result_type == "") || (result_type == "relation"))
5060 {
5070 Ranges< Uint31_Index > ranges;
5080 constraint.get_ranges(rman, ranges);
5090 get_elements_from_db< Uint31_Index, Relation_Skeleton >(ranges, *this, rman)
5100 .swap(into.relations, into.attic_relations);
5110 filter_attic_elements(rman, rman.get_desired_timestamp(), into.relations, into.attic_relations);
5120 }
5140 if (bbox_limitation)
5150 {
5160 Bbox_Filter filter(*bbox_limitation);
5170 filter.filter(into);
5180 constraint.filter(*this, rman, into);
5190 filter.filter(*this, rman, into);
5200 }
5210 else
5220 constraint.filter(*this, rman, into);
5240 transfer_output(rman, into);
5250 rman.health_check(*this);
5260}
529Query_Constraint* User_Statement::get_query_constraint()
5300{
5310 constraints.push_back(new User_Constraint(*this));
5320 return constraints.back();
5330}