1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al. 3 * This file is part of Overpass_API. 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. 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. 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/>. 22#include "../../template_db/file_blocks.h" 23#include "../../template_db/file_blocks_index.h" 24#include "../../template_db/random_file_index.h" 36template < typename TVal > 37struct OSM_File_Properties : public File_Properties 40 const std::string& file_base_name_, uint32 block_size_, uint32 map_block_size_, int32 min_version_ = 0) 4128066595 q : file_base_name(file_base_name_), block_size(block_size_), map_block_size(map_block_size_), 4228066595 q min_version(min_version_) {} 4416196590 q const std::string& get_file_name_trunk() const { return file_base_name; } 468098590 q const std::string& get_index_suffix() const { return basic_settings().INDEX_SUFFIX; } 4715356590 q const std::string& get_data_suffix() const { return basic_settings().DATA_SUFFIX; } 48840168 q const std::string& get_id_suffix() const { return basic_settings().ID_SUFFIX; } 490 const std::string& get_shadow_suffix() const { return basic_settings().SHADOW_SUFFIX; } 517678590 q uint32 get_block_size() const { return block_size/8; } 527678590 q uint32 get_compression_factor() const { return 8; } 537678590 q uint32 get_compression_method() const { return basic_settings().compression_method; } 54420168 q uint32 get_map_block_size() const { return map_block_size/8; } 55420168 q uint32 get_map_compression_factor() const { return 8; } 56420168 q uint32 get_map_compression_method() const { return basic_settings().map_compression_method; } 58 std::vector< bool > get_data_footprint(const std::string& db_dir) const 600 std::vector< bool > temp = get_data_index_footprint< TVal >(*this, db_dir, min_version); 64 std::vector< bool > get_map_footprint(const std::string& db_dir) const 660 return get_map_index_footprint(*this, db_dir); 69 uint32 id_max_size_of() const 710 return TVal::max_size_of(); 74 File_Blocks_Index_Base* new_data_index( 75 Access_Mode access_mode, bool use_shadow, const std::string& db_dir, const std::string& file_name_extension) 787678590 q if (access_mode == Access_Mode::writeable || access_mode == Access_Mode::truncate) 790 return new Writeable_File_Blocks_Index< TVal > 800 (*this, access_mode, use_shadow, db_dir, file_name_extension); 817678590 q return new Readonly_File_Blocks_Index< TVal > 827678590 q (*this, use_shadow, db_dir, file_name_extension); 85 std::string file_base_name; 87 uint32 map_block_size; 92//----------------------------------------------------------------------------- 94Basic_Settings::Basic_Settings() 99595595 q SHADOW_SUFFIX(".shadow"), 102595595 q db_logfile_name("database.log"), 103595595 q client_logfile_name("transactions.log"), 106595595 q source_hash("87bfad187673d891327f8bb68de7002a9e0e401d"), 108595595 q compression_method(File_Blocks_Index_Base::LZ4_COMPRESSION), 110 compression_method(File_Blocks_Index_Base::ZLIB_COMPRESSION), 112595595 q map_compression_method(File_Blocks_Index_Base::NO_COMPRESSION) 115Basic_Settings& basic_settings() 11734187595 q static Basic_Settings obj; 121//----------------------------------------------------------------------------- 123Osm_Base_Settings::Osm_Base_Settings() 125595595 q NODES(new OSM_File_Properties< Uint32_Index >("nodes", 128*1024, 256*1024)), 126595595 q NODE_TAGS_LOCAL(new OSM_File_Properties< Tag_Index_Local > 127595595 q ("node_tags_local", 128*1024, 0)), 128595595 q NODE_TAGS_GLOBAL(new OSM_File_Properties< Tag_Index_Global > 129595595 q ("node_tags_global", 128*1024, 0, 7561)), 130595595 q NODE_TAGS_GLOBAL_756(new OSM_File_Properties< Tag_Index_Global_Until756 > 131595595 q ("node_tags_global", 128*1024, 0)), 132595595 q NODE_KEYS(new OSM_File_Properties< Uint32_Index > 133595595 q ("node_keys", 512*1024, 0)), 134595595 q NODE_FREQUENT_TAGS(new OSM_File_Properties< String_Index > 135595595 q ("node_frequent_tags", 512*1024, 0)), 137595595 q WAYS(new OSM_File_Properties< Uint31_Index >("ways", 128*1024, 256*1024)), 138595595 q WAY_TAGS_LOCAL(new OSM_File_Properties< Tag_Index_Local > 139595595 q ("way_tags_local", 128*1024, 0)), 140595595 q WAY_TAGS_GLOBAL(new OSM_File_Properties< Tag_Index_Global > 141595595 q ("way_tags_global", 128*1024, 0, 7561)), 142595595 q WAY_TAGS_GLOBAL_756(new OSM_File_Properties< Tag_Index_Global_Until756 > 143595595 q ("way_tags_global", 128*1024, 0)), 144595595 q WAY_KEYS(new OSM_File_Properties< Uint32_Index > 146595595 q WAY_FREQUENT_TAGS(new OSM_File_Properties< String_Index > 147595595 q ("way_frequent_tags", 512*1024, 0)), 149595595 q RELATIONS(new OSM_File_Properties< Uint31_Index >("relations", 512*1024, 256*1024)), 150595595 q RELATION_ROLES(new OSM_File_Properties< Uint32_Index > 151595595 q ("relation_roles", 512*1024, 0)), 152595595 q RELATION_TAGS_LOCAL(new OSM_File_Properties< Tag_Index_Local > 153595595 q ("relation_tags_local", 128*1024, 0)), 154595595 q RELATION_TAGS_GLOBAL(new OSM_File_Properties< Tag_Index_Global > 155595595 q ("relation_tags_global", 128*1024, 0, 7561)), 156595595 q RELATION_TAGS_GLOBAL_756(new OSM_File_Properties< Tag_Index_Global_Until756 > 157595595 q ("relation_tags_global", 128*1024, 0)), 158595595 q RELATION_KEYS(new OSM_File_Properties< Uint32_Index > 159595595 q ("relation_keys", 512*1024, 0)), 160595595 q RELATION_FREQUENT_TAGS(new OSM_File_Properties< String_Index > 161595595 q ("relation_frequent_tags", 512*1024, 0)), 163595595 q shared_name(basic_settings().shared_name_base + "_osm_base"), 166595595 q total_available_space(12ll*1024*1024*1024), 167595595 q total_available_time_units(256*1024) 170595595 q NODES, NODE_TAGS_LOCAL, NODE_TAGS_GLOBAL, NODE_KEYS, NODE_FREQUENT_TAGS, 171595595 q WAYS, WAY_TAGS_LOCAL, WAY_TAGS_GLOBAL, WAY_KEYS, WAY_FREQUENT_TAGS, 172595595 q RELATIONS, RELATION_ROLES, RELATION_TAGS_LOCAL, RELATION_TAGS_GLOBAL, RELATION_KEYS, RELATION_FREQUENT_TAGS }; 173595595 q map_idxs_ = { NODES, WAYS, RELATIONS }; 177const std::vector< File_Properties* >& Osm_Base_Settings::bin_idxs() const 183const std::vector< File_Properties* >& Osm_Base_Settings::map_idxs() const 189const Osm_Base_Settings& osm_base_settings() 19120031595 q static Osm_Base_Settings obj; 195//----------------------------------------------------------------------------- 197Area_Settings::Area_Settings() 199100100 q AREA_BLOCKS(new OSM_File_Properties< Uint31_Index > 200100100 q ("area_blocks", 512*1024, 64*1024)), 201100100 q AREAS(new OSM_File_Properties< Uint31_Index >("areas", 2*1024*1024, 256*1024)), 202100100 q AREA_TAGS_LOCAL(new OSM_File_Properties< Tag_Index_Local > 203100100 q ("area_tags_local", 256*1024, 0)), 204100100 q AREA_TAGS_GLOBAL(new OSM_File_Properties< Tag_Index_Global > 205100100 q ("area_tags_global", 512*1024, 0, 7561)), 207100100 q shared_name(basic_settings().shared_name_base + "_areas"), 210100100 q total_available_space(4ll*1024*1024*1024), 211100100 q total_available_time_units(256*1024) 215const Area_Settings& area_settings() 221//----------------------------------------------------------------------------- 223Meta_Settings::Meta_Settings() 225340340 q USER_DATA(new OSM_File_Properties< Uint32_Index > 226340340 q ("user_data", 512*1024, 0)), 227340340 q USER_INDICES(new OSM_File_Properties< Uint32_Index > 228340340 q ("user_indices", 128*1024, 0)), 229340340 q NODES_META(new OSM_File_Properties< Node::Index > 230340340 q ("nodes_meta", 128*1024, 0)), 231340340 q WAYS_META(new OSM_File_Properties< Way::Index > 232340340 q ("ways_meta", 128*1024, 0)), 233340340 q RELATIONS_META(new OSM_File_Properties< Relation::Index > 234340340 q ("relations_meta", 128*1024, 0)) 236340340 q bin_idxs_ = { USER_DATA, USER_INDICES, NODES_META, WAYS_META, RELATIONS_META }; 240const std::vector< File_Properties* >& Meta_Settings::bin_idxs() const 246const Meta_Settings& meta_settings() 252//----------------------------------------------------------------------------- 254Attic_Settings::Attic_Settings() 256543543 q NODES(new OSM_File_Properties< Node::Index >("nodes_attic", 128*1024, 256*1024)), 257543543 q NODES_UNDELETED(new OSM_File_Properties< Node::Index >("nodes_attic_undeleted", 128*1024, 64*1024)), 258543543 q NODE_IDX_LIST(new OSM_File_Properties< Node::Id_Type > 259543543 q ("node_attic_indexes", 128*1024, 0)), 260543543 q NODE_TAGS_LOCAL(new OSM_File_Properties< Tag_Index_Local > 261543543 q ("node_tags_local_attic", 128*1024, 0)), 262543543 q NODE_TAGS_GLOBAL(new OSM_File_Properties< Tag_Index_Global > 263543543 q ("node_tags_global_attic", 128*1024, 0, 7561)), 264543543 q NODE_TAGS_GLOBAL_756(new OSM_File_Properties< Tag_Index_Global_Until756 > 265543543 q ("node_tags_global_attic", 128*1024, 0)), 266543543 q NODE_FREQUENT_TAGS(new OSM_File_Properties< String_Index > 267543543 q ("node_frequent_tags_attic", 512*1024, 0)), 268543543 q NODES_META(new OSM_File_Properties< Node::Index > 269543543 q ("nodes_meta_attic", 128*1024, 0)), 270543543 q NODE_CHANGELOG(new OSM_File_Properties< Timestamp > 271543543 q ("node_changelog", 128*1024, 0)), 273543543 q WAYS(new OSM_File_Properties< Way::Index >("ways_attic", 128*1024, 256*1024)), 274543543 q WAYS_UNDELETED(new OSM_File_Properties< Way::Index >("ways_attic_undeleted", 128*1024, 64*1024)), 275543543 q WAY_IDX_LIST(new OSM_File_Properties< Way::Id_Type > 276543543 q ("way_attic_indexes", 128*1024, 0)), 277543543 q WAY_TAGS_LOCAL(new OSM_File_Properties< Tag_Index_Local > 278543543 q ("way_tags_local_attic", 128*1024, 0)), 279543543 q WAY_TAGS_GLOBAL(new OSM_File_Properties< Tag_Index_Global > 280543543 q ("way_tags_global_attic", 128*1024, 0, 7561)), 281543543 q WAY_TAGS_GLOBAL_756(new OSM_File_Properties< Tag_Index_Global_Until756 > 282543543 q ("way_tags_global_attic", 128*1024, 0)), 283543543 q WAY_FREQUENT_TAGS(new OSM_File_Properties< String_Index > 284543543 q ("way_frequent_tags_attic", 512*1024, 0)), 285543543 q WAYS_META(new OSM_File_Properties< Uint31_Index > 286543543 q ("ways_meta_attic", 128*1024, 0)), 287543543 q WAY_CHANGELOG(new OSM_File_Properties< Timestamp > 288543543 q ("way_changelog", 128*1024, 0)), 290543543 q RELATIONS(new OSM_File_Properties< Relation::Index >("relations_attic", 512*1024, 256*1024)), 291543543 q RELATIONS_UNDELETED(new OSM_File_Properties< Relation::Index >("relations_attic_undeleted", 128*1024, 64*1024)), 292543543 q RELATION_IDX_LIST(new OSM_File_Properties< Relation::Id_Type > 293543543 q ("relation_attic_indexes", 128*1024, 0)), 294543543 q RELATION_TAGS_LOCAL(new OSM_File_Properties< Tag_Index_Local > 295543543 q ("relation_tags_local_attic", 128*1024, 0)), 296543543 q RELATION_TAGS_GLOBAL(new OSM_File_Properties< Tag_Index_Global > 297543543 q ("relation_tags_global_attic", 128*1024, 0, 7561)), 298543543 q RELATION_TAGS_GLOBAL_756(new OSM_File_Properties< Tag_Index_Global_Until756 > 299543543 q ("relation_tags_global_attic", 128*1024, 0)), 300543543 q RELATION_FREQUENT_TAGS(new OSM_File_Properties< String_Index > 301543543 q ("relation_frequent_tags_attic", 512*1024, 0)), 302543543 q RELATIONS_META(new OSM_File_Properties< Uint31_Index > 303543543 q ("relations_meta_attic", 128*1024, 0)), 304543543 q RELATION_CHANGELOG(new OSM_File_Properties< Timestamp > 305543543 q ("relation_changelog", 128*1024, 0)) 308543543 q NODES, NODES_UNDELETED, NODE_IDX_LIST, NODE_TAGS_LOCAL, NODE_TAGS_GLOBAL, NODE_FREQUENT_TAGS, 310543543 q WAYS, WAYS_UNDELETED, WAY_IDX_LIST, WAY_TAGS_LOCAL, WAY_TAGS_GLOBAL, WAY_FREQUENT_TAGS, 312543543 q RELATIONS, RELATIONS_UNDELETED, RELATION_IDX_LIST, 313543543 q RELATION_TAGS_LOCAL, RELATION_TAGS_GLOBAL, RELATION_FREQUENT_TAGS, 314543543 q RELATIONS_META, RELATION_CHANGELOG }; 318543543 q RELATIONS, RELATIONS_UNDELETED }; 322const std::vector< File_Properties* >& Attic_Settings::bin_idxs() const 328const std::vector< File_Properties* >& Attic_Settings::map_idxs() const 334const Attic_Settings& attic_settings() 33627203543 q static Attic_Settings obj; 340//----------------------------------------------------------------------------- 342void show_mem_status() 3440 std::ostringstream proc_file_name_(""); 3450 proc_file_name_<<"/proc/"<<getpid()<<"/stat"; 3460 std::ifstream stat(proc_file_name_.str().c_str()); 3500 getline(stat, line); 356//----------------------------------------------------------------------------- 358Logger::Logger(const std::string& db_dir, const std::string& filename) 3590 : logfile_full_name(db_dir + (filename.empty() ? basic_settings().db_logfile_name : filename)) {} 361void Logger::annotated_log(const std::string& message) 363 // Collect current time in a user-readable form. 3640 time_t time_t_ = time(0); 3650 struct tm* tm_ = gmtime(&time_t_); 3660 char strftime_buf[21]; 3670 strftime_buf[0] = 0; 3690 strftime(strftime_buf, 21, "%F %H:%M:%S ", tm_); 3710 std::ofstream out(logfile_full_name.c_str(), std::ios_base::app); 3720 out<<strftime_buf<<'['<<getpid()<<"] "<<message<<'\n'; 375void Logger::raw_log(const std::string& message) 3770 std::ofstream out(logfile_full_name.c_str(), std::ios_base::app); 381const std::string& get_logfile_name() 3830 return basic_settings().db_logfile_name; 387const uint64 NOW = std::numeric_limits< unsigned long long >::max(); 389//----------------------------------------------------------------------------- 391Database_Meta_State::Mode Database_Meta_State::from_db_files(const std::string& db_dir) 3930 for (auto i : attic_settings().bin_idxs()) 3950 if (file_exists(db_dir + i->get_file_name_trunk() + i->get_data_suffix())) 3960 return Database_Meta_State::keep_attic; 3990 for (auto i : meta_settings().bin_idxs()) 4010 if (file_exists(db_dir + i->get_file_name_trunk() + i->get_data_suffix())) 4020 return Database_Meta_State::keep_meta; 4050 return Database_Meta_State::only_data; 409std::string get_server_name(const std::string& db_dir) 4110 std::string server_name("/api"); 4150 std::ifstream server_name_f((db_dir + "server_name").c_str()); 4160 getline(server_name_f, server_name); 4200 if (!server_name.empty() && server_name.back() != '/') 4210 return server_name + '/'; 426void set_server_name(const std::string& db_dir, const std::string& server_name) 4280 std::ofstream out((db_dir + "/server_name").c_str()); 4290 out<<server_name<<'\n';