GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — osm2pgsql/src/middle-pgsql.cpp at master · osm2pgsql-dev/osm2pgsql Expand file tree/ Copy pathmiddle-pgsql.cppMore file actions 1352 lines (1133 loc) · 41.1 KB / Copy pathmiddle-pgsql.cppFile metadata and controls1352 lines (1133 loc) · 41.1 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 /** * SPDX-License-Identifier: GPL-2.0-or-later * * This file is part of osm2pgsql (https://osm2pgsql.org/). * * Copyright (C) 2006-2026 by the osm2pgsql developer community. * For a full list of authors see the git log. */ /* Implements the mid-layer processing for osm2pgsql * using several PostgreSQL tables * * This layer stores data read in from the planet.osm file * and is then read by the backend processing code to * emit the final geometry-enabled output formats */ #include <cassert> #include <cstdint> #include <cstdlib> #include <initializer_list> #include <iterator> #include <memory> #include <stdexcept> #include <string> #include <unordered_map> #include <utility> #include <vector> #include <osmium/builder/osm_object_builder.hpp> #include <osmium/memory/buffer.hpp> #include <osmium/osm/types_from_string.hpp> #include <nlohmann/json.hpp> #include "format.hpp" #include "idlist.hpp" #include "json-writer.hpp" #include "logging.hpp" #include "middle-pgsql.hpp" #include "node-locations.hpp" #include "node-persistent-cache.hpp" #include "options.hpp" #include "osmtypes.hpp" #include "pgsql-helper.hpp" #include "template.hpp" #include "util.hpp" namespace { void send_id_list(pg_conn_t const &db_connection, std::string const &table, idlist_t const &ids) { std::string data; for (auto const id : ids) { fmt::format_to(std::back_inserter(data), FMT_STRING("{}\n"), id); } auto const sql = fmt::format("COPY {} FROM STDIN", table); db_connection.copy_start(sql); db_connection.copy_send(data, table); db_connection.copy_end(table); } void load_id_list(pg_conn_t const &db_connection, std::string const &table, idlist_t *ids) { auto const res = db_connection.exec( fmt::format("SELECT DISTINCT id FROM {} ORDER BY id", table)); for (int n = 0; n < res.num_tuples(); ++n) { ids->push_back(osmium::string_to_object_id(res.get_value(n, 0))); } } } // anonymous namespace middle_pgsql_t::table_desc_t::table_desc_t(options_t const &options, std::string_view name) : m_copy_target(std::make_shared<db_target_descr_t>( options.middle_dbschema, fmt::format("{}_{}", options.prefix, name), "id")) { } std::string middle_pgsql_t::render_template(std::string_view templ) const { template_t sql_template{templ}; sql_template.set_params(m_params); return sql_template.render(); } void middle_pgsql_t::dbexec(std::string_view templ) const { m_db_connection.exec(render_template(templ)); } void middle_query_pgsql_t::prepare(std::string const &stmt, std::string const &sql_cmd) const { m_db_connection.prepare(stmt, fmt::runtime(sql_cmd)); } void middle_pgsql_t::table_desc_t::drop_table( pg_conn_t const &db_connection) const { util::timer_t timer; log_info("Dropping table '{}'", name()); drop_table_if_exists(db_connection, schema(), name()); log_info("Table '{}' dropped in {}", name(), util::human_readable_duration(timer.stop())); } void middle_pgsql_t::table_desc_t::init_max_id(pg_conn_t const &db_connection) { auto const qual_name = qualified_name(schema(), name()); auto const res = db_connection.exec("SELECT max(id) FROM {}", qual_name); if (res.is_null(0, 0)) { return; } m_max_id = osmium::string_to_object_id(res.get_value(0, 0)); } namespace { /** * Parse JSON-encoded tags from a version 2 middle table and add them to the * builder. */ template <typename T> void pgsql_parse_json_tags(char const *string, osmium::memory::Buffer *buffer, T *obuilder) { if (*string == '\0') { // NULL return; } auto const tags = nlohmann::json::parse(string); if (!tags.is_object()) { throw std::runtime_error{"Database format for tags invalid."}; } // These will come out sorted, because internally "tags" uses a std::map. osmium::builder::TagListBuilder builder{*buffer, obuilder}; for (auto const &tag : tags.items()) { builder.add_tag(tag.key(), tag.value()); } } /** * Helper class for parsing relation members encoded in JSON. */ class member_list_json_builder_t { public: explicit member_list_json_builder_t( osmium::builder::RelationMemberListBuilder *builder) : m_builder(builder) {} static bool number_integer(nlohmann::json::number_integer_t /*val*/) { return true; } bool number_unsigned(nlohmann::json::number_unsigned_t val) { m_ref = static_cast<osmium::object_id_type>(val); return true; } static bool number_float(nlohmann::json::number_float_t /*val*/, nlohmann::json::string_t const & /*s*/) { return true; } bool key(nlohmann::json::string_t &val) { if (val == "type") { m_next_val = next_val::type; } else if (val == "ref") { m_next_val = next_val::ref; } else if (val == "role") { m_next_val = next_val::role; } else { m_next_val = next_val::none; } return true; } bool string(nlohmann::json::string_t &val) { if (m_next_val == next_val::type && val.size() == 1) { switch (val[0]) { case 'N': m_type = osmium::item_type::node; break; case 'W': m_type = osmium::item_type::way; break; default: m_type = osmium::item_type::relation; break; } } else if (m_next_val == next_val::role) { m_role = val; } return true; } bool end_object() { m_builder->add_member(m_type, m_ref, m_role); m_next_val = next_val::none; m_type = osmium::item_type::undefined; m_ref = 0; m_role.clear(); return true; } static bool null() { return true; } static bool boolean(bool /*val*/) { return true; } static bool binary(std::vector<std::uint8_t> & /*val*/) { return true; } static bool start_object(std::size_t /*elements*/) { return true; } static bool start_array(std::size_t /*elements*/) { return true; } static bool end_array() { return true; } static bool parse_error(std::size_t /*position*/, std::string const & /*last_token*/, nlohmann::json::exception const &ex) { throw ex; } private: std::string m_role; osmium::builder::RelationMemberListBuilder *m_builder; osmium::object_id_type m_ref = 0; osmium::item_type m_type = osmium::item_type::undefined; enum class next_val : std::uint8_t { none, type, ref, role } m_next_val = next_val::none; }; // class member_list_json_builder_t template <typename T> void pgsql_parse_json_members(char const *string, osmium::memory::Buffer *buffer, T *obuilder) { if (*string == '\0') { // NULL return; } osmium::builder::RelationMemberListBuilder builder{*buffer, obuilder}; member_list_json_builder_t parser{&builder}; nlohmann::json::sax_parse(string, &parser); } void pgsql_parse_nodes(char const *string, osmium::memory::Buffer *buffer, osmium::builder::WayBuilder *obuilder) { if (*string++ == '{') { osmium::builder::WayNodeListBuilder wnl_builder{*buffer, obuilder}; while (*string != '}') { char *ptr = nullptr; wnl_builder.add_node_ref(std::strtoll(string, &ptr, 10)); string = ptr; if (*string == ',') { ++string; } } } } /** * Set the attributes of an object from the result of a query with the * columns from {attribute_columns_use} starting at the specified column. */ template <typename T> void set_attributes_on_builder(T *builder, pg_result_t const &result, int num, int column) { if (!result.is_null(num, column)) { builder->set_timestamp( std::strtoul(result.get_value(num, column), nullptr, 10)); } if (!result.is_null(num, column + 1)) { builder->set_version(result.get_value(num, column + 1)); } if (!result.is_null(num, column + 2)) { builder->set_changeset(result.get_value(num, column + 2)); } if (!result.is_null(num, column + 3)) { builder->set_uid(result.get_value(num, column + 3)); } if (!result.is_null(num, column + 4)) { builder->set_user(result.get_value(num, column + 4)); } } void tags_to_json(osmium::TagList const &tags, json_writer_t *writer) { writer->start_object(); for (auto const &tag : tags) { writer->key(tag.key()); writer->string(tag.value()); writer->next(); } writer->end_object(); } void members_to_json(osmium::RelationMemberList const &members, json_writer_t *writer) { writer->start_array(); for (auto const &member : members) { writer->start_object(); writer->key("type"); switch (member.type()) { case osmium::item_type::node: writer->string("N"); break; case osmium::item_type::way: writer->string("W"); break; default: // osmium::item_type::relation writer->string("R"); break; } writer->next(); writer->key("ref"); writer->number(member.ref()); writer->next(); writer->key("role"); writer->string(member.role()); writer->end_object(); writer->next(); } writer->end_array(); } } // anonymous namespace void middle_pgsql_t::copy_attributes(osmium::OSMObject const &obj) { if (obj.timestamp()) { m_db_copy.add_column(obj.timestamp()); } else { m_db_copy.add_null_column(); } if (obj.version()) { m_db_copy.add_column(obj.version()); } else { m_db_copy.add_null_column(); } if (obj.changeset()) { m_db_copy.add_columns(obj.changeset()); } else { m_db_copy.add_null_column(); } if (obj.uid()) { m_db_copy.add_columns(obj.uid()); m_users.try_emplace(obj.uid(), obj.user()); } else { m_db_copy.add_null_column(); } } void middle_pgsql_t::copy_tags(osmium::OSMObject const &obj) { if (obj.tags().empty()) { m_db_copy.add_null_column(); return; } json_writer_t writer; tags_to_json(obj.tags(), &writer); m_db_copy.add_column(writer.json()); } std::size_t middle_query_pgsql_t::get_way_node_locations_db( osmium::WayNodeList *nodes) const { size_t count = 0; util::string_joiner_t id_list{',', '\0', '{', '}'}; // get nodes where possible from cache, // at the same time build a list for querying missing nodes from DB for (auto &n : *nodes) { auto const loc = m_cache->get(n.ref()); if (loc.valid()) { n.set_location(loc); ++count; } else { id_list.add(fmt::to_string(n.ref())); } } if (id_list.empty()) { return count; } // get any remaining nodes from the DB // Nodes must have been written back at this point. auto const res = m_db_connection.exec_prepared("get_node_list", id_list()); std::unordered_map<osmid_t, osmium::Location> locs; for (int i = 0; i < res.num_tuples(); ++i) { locs.emplace(osmium::string_to_object_id(res.get_value(i, 0)), osmium::Location{ (int)std::strtol(res.get_value(i, 1), nullptr, 10), (int)std::strtol(res.get_value(i, 2), nullptr, 10)}); } for (auto &n : *nodes) { auto const el = locs.find(n.ref()); if (el != locs.end()) { n.set_location(el->second); ++count; } } return count; } void middle_pgsql_t::node(osmium::Node const &node) { assert(m_middle_state == middle_state::node); if (node.deleted()) { node_delete(node.id()); } else { if (m_options->append) { node_delete(node.id()); } node_set(node); } } void middle_pgsql_t::way(osmium::Way const &way) { assert(m_middle_state == middle_state::way); if (way.deleted()) { way_delete(way.id()); } else { if (m_options->append) { way_delete(way.id()); } way_set(way); } } void middle_pgsql_t::relation(osmium::Relation const &relation) { assert(m_middle_state == middle_state::relation); if (relation.deleted()) { relation_delete(relation.id()); } else { if (m_options->append) { relation_delete(relation.id()); } relation_set(relation); } } void middle_pgsql_t::node_set(osmium::Node const &node) { m_cache->set(node.id(), node.location()); if (m_persistent_cache) { m_persistent_cache->set(node.id(), node.location()); } if (!m_store_options.nodes) { return; } if (!m_store_options.untagged_nodes && node.tags().empty()) { return; } m_db_copy.new_line(m_tables.nodes().copy_target()); m_db_copy.add_columns(node.id(), node.location().y(), node.location().x()); if (m_store_options.with_attributes) { copy_attributes(node); } copy_tags(node); m_db_copy.finish_line(); } std::size_t middle_query_pgsql_t::get_way_node_locations_flatnodes( osmium::WayNodeList *nodes) const { std::size_t count = 0; for (auto &n : *nodes) { auto loc = m_cache->get(n.ref()); if (!loc.valid() && n.ref() >= 0) { loc = m_persistent_cache->get(n.ref()); } n.set_location(loc); if (loc.valid()) { ++count; } } return count; } osmium::Location middle_query_pgsql_t::get_node_location_db(osmid_t id) const { auto const res = m_db_connection.exec_prepared("get_node_location", id); if (res.num_tuples() == 0) { return osmium::Location{}; } return osmium::Location{(int)std::strtol(res.get_value(0, 1), nullptr, 10), (int)std::strtol(res.get_value(0, 2), nullptr, 10)}; } osmium::Location middle_query_pgsql_t::get_node_location_flatnodes(osmid_t id) const { if (id >= 0) { return m_persistent_cache->get(id); } return osmium::Location{}; } osmium::Location middle_query_pgsql_t::get_node_location(osmid_t id) const { auto const loc = m_cache->get(id); if (loc.valid()) { return loc; } return m_persistent_cache ? get_node_location_flatnodes(id) : get_node_location_db(id); } size_t middle_query_pgsql_t::nodes_get_list(osmium::WayNodeList *nodes) const { return m_persistent_cache ? get_way_node_locations_flatnodes(nodes) : get_way_node_locations_db(nodes); } void middle_pgsql_t::node_delete(osmid_t osm_id) { assert(m_options->append); if (m_persistent_cache) { m_persistent_cache->set(osm_id, osmium::Location{}); } if (m_store_options.nodes && osm_id <= m_tables.nodes().max_id()) { m_db_copy.new_line(m_tables.nodes().copy_target()); m_db_copy.delete_object(osm_id); } } void middle_pgsql_t::get_node_parents(idlist_t const &changed_nodes, idlist_t *parent_ways, idlist_t *parent_relations) const { util::timer_t timer; m_db_connection.exec("BEGIN"); m_db_connection.exec("CREATE TEMP TABLE osm2pgsql_changed_nodes" " (id int8 NOT NULL) ON COMMIT DROP"); m_db_connection.exec("CREATE TEMP TABLE osm2pgsql_changed_ways" " (id int8 NOT NULL) ON COMMIT DROP"); m_db_connection.exec("CREATE TEMP TABLE osm2pgsql_changed_relations" " (id int8 NOT NULL) ON COMMIT DROP"); send_id_list(m_db_connection, "osm2pgsql_changed_nodes", changed_nodes); std::vector<std::string> queries; queries.emplace_back("ANALYZE osm2pgsql_changed_nodes"); // The query to get the parent ways of changed nodes is "hidden" // inside a PL/pgSQL function so that the query planner only sees // a single node id that is being queried for. If we ask for all // nodes at the same time the query planner sometimes thinks it is // better to do a full table scan which totally destroys performance. // This is due to the PostgreSQL statistics on ARRAYs being way off. queries.emplace_back(R"( CREATE OR REPLACE FUNCTION {schema}osm2pgsql_find_changed_ways() RETURNS void AS $$ DECLARE changed_buckets RECORD; BEGIN FOR changed_buckets IN SELECT array_agg(id) AS node_ids, id >> {way_node_index_id_shift} AS bucket FROM osm2pgsql_changed_nodes GROUP BY id >> {way_node_index_id_shift} LOOP INSERT INTO osm2pgsql_changed_ways SELECT DISTINCT w.id FROM {schema}"{prefix}_ways" w WHERE w.nodes && changed_buckets.node_ids AND {schema}"{prefix}_index_bucket"(w.nodes) && ARRAY[changed_buckets.bucket]; END LOOP; END; $$ LANGUAGE plpgsql )"); queries.emplace_back("SELECT {schema}osm2pgsql_find_changed_ways()"); queries.emplace_back("DROP FUNCTION {schema}osm2pgsql_find_changed_ways()"); queries.emplace_back(R"( INSERT INTO osm2pgsql_changed_relations SELECT r.id FROM {schema}"{prefix}_rels" r, osm2pgsql_changed_nodes c WHERE {schema}"{prefix}_member_ids"(r.members, 'N'::char) && ARRAY[c.id]; )"); for (auto const &query : queries) { dbexec(query); } if (parent_ways) { load_id_list(m_db_connection, "osm2pgsql_changed_ways", parent_ways); } load_id_list(m_db_connection, "osm2pgsql_changed_relations", parent_relations); m_db_connection.exec("COMMIT"); timer.stop(); log_debug("Found {} new/changed nodes in input.", changed_nodes.size()); auto const elapsed_sec = std::chrono::duration_cast<std::chrono::seconds>(timer.elapsed()); if (parent_ways) { log_debug(" Found in {} their {} parent ways and {} parent relations.", elapsed_sec, parent_ways->size(), parent_relations->size()); } else { log_debug(" Found in {} their {} parent relations.", elapsed_sec, parent_relations->size()); } } void middle_pgsql_t::get_way_parents(idlist_t const &changed_ways, idlist_t *parent_relations) const { util::timer_t timer; auto const num_relations_referenced_by_nodes = parent_relations->size(); m_db_connection.exec("BEGIN"); m_db_connection.exec("CREATE TEMP TABLE osm2pgsql_changed_ways" " (id int8 NOT NULL) ON COMMIT DROP"); m_db_connection.exec("CREATE TEMP TABLE osm2pgsql_changed_relations" " (id int8 NOT NULL) ON COMMIT DROP"); send_id_list(m_db_connection, "osm2pgsql_changed_ways", changed_ways); m_db_connection.exec("ANALYZE osm2pgsql_changed_ways"); dbexec(R"( INSERT INTO osm2pgsql_changed_relations SELECT DISTINCT r.id FROM {schema}"{prefix}_rels" r, osm2pgsql_changed_ways c WHERE {schema}"{prefix}_member_ids"(r.members, 'W'::char) && ARRAY[c.id]; )"); load_id_list(m_db_connection, "osm2pgsql_changed_relations", parent_relations); m_db_connection.exec("COMMIT"); timer.stop(); log_debug("Found {} ways that are new/changed in input or parent of" " changed node.", changed_ways.size()); log_debug(" Found in {} their {} parent relations.", std::chrono::duration_cast<std::chrono::seconds>(timer.elapsed()), parent_relations->size() - num_relations_referenced_by_nodes); // (Potentially) contains parent relations from nodes and from ways. Make // sure they are merged. parent_relations->sort_unique(); } void middle_pgsql_t::way_set(osmium::Way const &way) { m_db_copy.new_line(m_tables.ways().copy_target()); m_db_copy.add_column(way.id()); if (m_store_options.with_attributes) { copy_attributes(way); } // nodes m_db_copy.new_array(); for (auto const &n : way.nodes()) { m_db_copy.add_array_elem(n.ref()); } m_db_copy.finish_array(); copy_tags(way); m_db_copy.finish_line(); } namespace { /** * Build node in buffer from database results. */ void build_node(osmid_t id, pg_result_t const &res, int res_num, int offset, osmium::memory::Buffer *buffer, bool with_attributes) { osmium::builder::NodeBuilder builder{*buffer}; builder.set_id(id); builder.set_location(osmium::Location{ (int)std::strtol(res.get_value(res_num, offset + 0), nullptr, 10), (int)std::strtol(res.get_value(res_num, offset + 1), nullptr, 10)}); if (with_attributes) { // lon, lat, tags, then the attributes set_attributes_on_builder(&builder, res, res_num, offset + 3); } pgsql_parse_json_tags(res.get_value(res_num, offset + 2), buffer, &builder); } /** * Build way in buffer from database results. */ void build_way(osmid_t id, pg_result_t const &res, int res_num, int offset, osmium::memory::Buffer *buffer, bool with_attributes) { osmium::builder::WayBuilder builder{*buffer}; builder.set_id(id); if (with_attributes) { // nodes, tags, then the attributes set_attributes_on_builder(&builder, res, res_num, offset + 2); } pgsql_parse_nodes(res.get_value(res_num, offset + 0), buffer, &builder); pgsql_parse_json_tags(res.get_value(res_num, offset + 1), buffer, &builder); } } // anonymous namespace bool middle_query_pgsql_t::node_get(osmid_t id, osmium::memory::Buffer *buffer) const { assert(buffer); if (m_store_options.nodes) { auto const res = m_db_connection.exec_prepared("get_node", id); if (res.num_tuples() == 1) { build_node(id, res, 0, 0, buffer, m_store_options.with_attributes); buffer->commit(); return true; } } if (m_store_options.use_flat_node_file) { auto const location = get_node_location_flatnodes(id); if (!location.valid()) { return false; } { osmium::builder::NodeBuilder builder{*buffer}; builder.set_id(id); builder.set_location(location); } buffer->commit(); return true; } return false; } bool middle_query_pgsql_t::way_get(osmid_t id, osmium::memory::Buffer *buffer) const { assert(buffer); auto const res = m_db_connection.exec_prepared("get_way", id); if (res.num_tuples() != 1) { return false; } build_way(id, res, 0, 0, buffer, m_store_options.with_attributes); buffer->commit(); return true; } std::size_t middle_query_pgsql_t::rel_members_get(osmium::Relation const &rel, osmium::memory::Buffer *buffer, osmium::osm_entity_bits::type types) const { assert(buffer); assert((types & osmium::osm_entity_bits::relation) == 0); pg_result_t res; idlist_t wayidspg; if (types & osmium::osm_entity_bits::way) { // collect ids from all way members into a list.. util::string_joiner_t way_ids{',', '\0', '{', '}'}; for (auto const &member : rel.members()) { if (member.type() == osmium::item_type::way) { way_ids.add(fmt::to_string(member.ref())); } } // ...and get those ways from database if (!way_ids.empty()) { res = m_db_connection.exec_prepared("get_way_list", way_ids()); wayidspg = get_ids_from_result(res); } } std::size_t members_found = 0; for (auto const &member : rel.members()) { if (member.type() == osmium::item_type::node && (types & osmium::osm_entity_bits::node)) { osmium::builder::NodeBuilder builder{*buffer}; builder.set_id(member.ref()); ++members_found; } else if (member.type() == osmium::item_type::way && (types & osmium::osm_entity_bits::way) && res) { // Match the list of ways coming from postgres in a different order // back to the list of ways given by the caller for (int j = 0; j < res.num_tuples(); ++j) { if (member.ref() == wayidspg[static_cast<std::size_t>(j)]) { build_way(member.ref(), res, j, 1, buffer, m_store_options.with_attributes); ++members_found; break; } } } } buffer->commit(); return members_found; } void middle_pgsql_t::way_delete(osmid_t osm_id) { assert(m_options->append); if (osm_id <= m_tables.ways().max_id()) { m_db_copy.new_line(m_tables.ways().copy_target()); m_db_copy.delete_object(osm_id); } } void middle_pgsql_t::relation_set(osmium::Relation const &rel) { m_db_copy.new_line(m_tables.relations().copy_target()); m_db_copy.add_column(rel.id()); if (m_store_options.with_attributes) { copy_attributes(rel); } json_writer_t writer; members_to_json(rel.members(), &writer); m_db_copy.add_column(writer.json()); copy_tags(rel); m_db_copy.finish_line(); } bool middle_query_pgsql_t::relation_get(osmid_t id, osmium::memory::Buffer *buffer) const { assert(buffer); auto const res = m_db_connection.exec_prepared("get_rel", id); if (res.num_tuples() == 0) { return false; } { osmium::builder::RelationBuilder builder{*buffer}; builder.set_id(id); if (m_store_options.with_attributes) { // members, tags, then the attributes set_attributes_on_builder(&builder, res, 0, 2); } pgsql_parse_json_members(res.get_value(0, 0), buffer, &builder); pgsql_parse_json_tags(res.get_value(0, 1), buffer, &builder); } buffer->commit(); return true; } void middle_pgsql_t::relation_delete(osmid_t osm_id) { assert(m_options->append); if (osm_id <= m_tables.relations().max_id()) { m_db_copy.new_line(m_tables.relations().copy_target()); m_db_copy.delete_object(osm_id); } } void middle_pgsql_t::after_nodes() { assert(m_middle_state == middle_state::node); #ifndef NDEBUG m_middle_state = middle_state::way; #endif m_db_copy.sync(); if (!m_options->append && m_store_options.nodes) { auto const &table = m_tables.nodes(); analyze_table(m_db_connection, table.schema(), table.name()); } m_cache->log_stats(); } void middle_pgsql_t::after_ways() { assert(m_middle_state == middle_state::way); #ifndef NDEBUG m_middle_state = middle_state::relation; #endif m_db_copy.sync(); if (!m_options->append) { auto const &table = m_tables.ways(); analyze_table(m_db_connection, table.schema(), table.name()); } } void middle_pgsql_t::after_relations() { assert(m_middle_state == middle_state::relation); #ifndef NDEBUG m_middle_state = middle_state::done; #endif m_db_copy.sync(); if (!m_options->append) { auto const &table = m_tables.relations(); analyze_table(m_db_connection, table.schema(), table.name()); } if (m_store_options.with_attributes && !m_options->droptemp) { if (m_append) { update_users_table(); } else { write_users_table(); } } // release the copy thread and its database connection m_copy_thread->finish(); } middle_query_pgsql_t::middle_query_pgsql_t( connection_params_t const &connection_params, std::shared_ptr<node_locations_t> cache, std::shared_ptr<node_persistent_cache_t> persistent_cache, middle_pgsql_options const &options) : m_db_connection(connection_params, "middle.query"), m_cache(std::move(cache)), |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |