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) 4131780673 q : file_base_name(file_base_name_), block_size(block_size_), map_block_size(map_block_size_), 4231780673 q min_version(min_version_) {} 4418310668 q const std::string& get_file_name_trunk() const { return file_base_name; } 469155668 q const std::string& get_index_suffix() const { return basic_settings().INDEX_SUFFIX; } 4717184668 q const std::string& get_data_suffix() const { return basic_settings().DATA_SUFFIX; } 481126228 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; } 518592668 q uint32 get_block_size() const { return block_size/8; } 528592668 q uint32 get_compression_factor() const { return 8; } 538592668 q uint32 get_compression_method() const { return basic_settings().compression_method; } 54563228 q uint32 get_map_block_size() const { return map_block_size/8; } 55563228 q uint32 get_map_compression_factor() const { return 8; } 56563228 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) 788592668 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); 818592668 q return new Readonly_File_Blocks_Index< TVal > 828592668 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() 99673673 q SHADOW_SUFFIX(".shadow"), 102673673 q db_logfile_name("database.log"), 103673673 q client_logfile_name("transactions.log"), 106673673 q source_hash("87bfad187673d891327f8bb68de7002a9e0e401d"), 108673673 q compression_method(File_Blocks_Index_Base::LZ4_COMPRESSION), 110 compression_method(File_Blocks_Index_Base::ZLIB_COMPRESSION), 112673673 q map_compression_method(File_Blocks_Index_Base::NO_COMPRESSION) 115Basic_Settings& basic_settings() 11738657673 q static Basic_Settings obj; 121//----------------------------------------------------------------------------- 123Osm_Base_Settings::Osm_Base_Settings() 125673673 q NODES(new OSM_File_Properties< Uint32_Index >("nodes", 128*1024, 256*1024)), 126673673 q NODE_TAGS_LOCAL(new OSM_File_Properties< Tag_Index_Local > 127673673 q ("node_tags_local", 128*1024, 0)), 128673673 q NODE_TAGS_GLOBAL(new OSM_File_Properties< Tag_Index_Global > 129673673 q ("node_tags_global", 128*1024, 0, 7561)), 130673673 q NODE_TAGS_GLOBAL_756(new OSM_File_Properties< Tag_Index_Global_Until756 > 131673673 q ("node_tags_global", 128*1024, 0)), 132673673 q NODE_KEYS(new OSM_File_Properties< Uint32_Index > 133673673 q ("node_keys", 512*1024, 0)), 134673673 q NODE_FREQUENT_TAGS(new OSM_File_Properties< String_Index > 135673673 q ("node_frequent_tags", 512*1024, 0)), 137673673 q WAYS(new OSM_File_Properties< Uint31_Index >("ways", 128*1024, 256*1024)), 138673673 q WAY_TAGS_LOCAL(new OSM_File_Properties< Tag_Index_Local > 139673673 q ("way_tags_local", 128*1024, 0)), 140673673 q WAY_TAGS_GLOBAL(new OSM_File_Properties< Tag_Index_Global > 141673673 q ("way_tags_global", 128*1024, 0, 7561)), 142673673 q WAY_TAGS_GLOBAL_756(new OSM_File_Properties< Tag_Index_Global_Until756 > 143673673 q ("way_tags_global", 128*1024, 0)), 144673673 q WAY_KEYS(new OSM_File_Properties< Uint32_Index > 146673673 q WAY_FREQUENT_TAGS(new OSM_File_Properties< String_Index > 147673673 q ("way_frequent_tags", 512*1024, 0)), 149673673 q RELATIONS(new OSM_File_Properties< Uint31_Index >("relations", 512*1024, 256*1024)), 150673673 q RELATION_ROLES(new OSM_File_Properties< Uint32_Index > 151673673 q ("relation_roles", 512*1024, 0)), 152673673 q RELATION_TAGS_LOCAL(new OSM_File_Properties< Tag_Index_Local > 153673673 q ("relation_tags_local", 128*1024, 0)), 154673673 q RELATION_TAGS_GLOBAL(new OSM_File_Properties< Tag_Index_Global > 155673673 q ("relation_tags_global", 128*1024, 0, 7561)), 156673673 q RELATION_TAGS_GLOBAL_756(new OSM_File_Properties< Tag_Index_Global_Until756 > 157673673 q ("relation_tags_global", 128*1024, 0)), 158673673 q RELATION_KEYS(new OSM_File_Properties< Uint32_Index > 159673673 q ("relation_keys", 512*1024, 0)), 160673673 q RELATION_FREQUENT_TAGS(new OSM_File_Properties< String_Index > 161673673 q ("relation_frequent_tags", 512*1024, 0)), 163673673 q shared_name(basic_settings().shared_name_base + "_osm_base"), 166673673 q total_available_space(12ll*1024*1024*1024), 167673673 q total_available_time_units(256*1024) 170673673 q NODES, NODE_TAGS_LOCAL, NODE_TAGS_GLOBAL, NODE_KEYS, NODE_FREQUENT_TAGS, 171673673 q WAYS, WAY_TAGS_LOCAL, WAY_TAGS_GLOBAL, WAY_KEYS, WAY_FREQUENT_TAGS, 172673673 q RELATIONS, RELATION_ROLES, RELATION_TAGS_LOCAL, RELATION_TAGS_GLOBAL, RELATION_KEYS, RELATION_FREQUENT_TAGS }; 173673673 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() 19121447673 q static Osm_Base_Settings obj; 195//----------------------------------------------------------------------------- 197Area_Settings::Area_Settings() 199120120 q AREA_BLOCKS(new OSM_File_Properties< Uint31_Index > 200120120 q ("area_blocks", 512*1024, 64*1024)), 201120120 q AREAS(new OSM_File_Properties< Uint31_Index >("areas", 2*1024*1024, 256*1024)), 202120120 q AREA_TAGS_LOCAL(new OSM_File_Properties< Tag_Index_Local > 203120120 q ("area_tags_local", 256*1024, 0)), 204120120 q AREA_TAGS_GLOBAL(new OSM_File_Properties< Tag_Index_Global > 205120120 q ("area_tags_global", 512*1024, 0, 7561)), 207120120 q shared_name(basic_settings().shared_name_base + "_areas"), 210120120 q total_available_space(4ll*1024*1024*1024), 211120120 q total_available_time_units(256*1024) 215const Area_Settings& area_settings() 221//----------------------------------------------------------------------------- 223Meta_Settings::Meta_Settings() 225387387 q USER_DATA(new OSM_File_Properties< Uint32_Index > 226387387 q ("user_data", 512*1024, 0)), 227387387 q USER_INDICES(new OSM_File_Properties< Uint32_Index > 228387387 q ("user_indices", 128*1024, 0)), 229387387 q NODES_META(new OSM_File_Properties< Node::Index > 230387387 q ("nodes_meta", 128*1024, 0)), 231387387 q WAYS_META(new OSM_File_Properties< Way::Index > 232387387 q ("ways_meta", 128*1024, 0)), 233387387 q RELATIONS_META(new OSM_File_Properties< Relation::Index > 234387387 q ("relations_meta", 128*1024, 0)) 236387387 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() 256614614 q NODES(new OSM_File_Properties< Node::Index >("nodes_attic", 128*1024, 256*1024)), 257614614 q NODES_UNDELETED(new OSM_File_Properties< Node::Index >("nodes_attic_undeleted", 128*1024, 64*1024)), 258614614 q NODE_IDX_LIST(new OSM_File_Properties< Node::Id_Type > 259614614 q ("node_attic_indexes", 128*1024, 0)), 260614614 q NODE_TAGS_LOCAL(new OSM_File_Properties< Tag_Index_Local > 261614614 q ("node_tags_local_attic", 128*1024, 0)), 262614614 q NODE_TAGS_GLOBAL(new OSM_File_Properties< Tag_Index_Global > 263614614 q ("node_tags_global_attic", 128*1024, 0, 7561)), 264614614 q NODE_TAGS_GLOBAL_756(new OSM_File_Properties< Tag_Index_Global_Until756 > 265614614 q ("node_tags_global_attic", 128*1024, 0)), 266614614 q NODE_FREQUENT_TAGS(new OSM_File_Properties< String_Index > 267614614 q ("node_frequent_tags_attic", 512*1024, 0)), 268614614 q NODES_META(new OSM_File_Properties< Node::Index > 269614614 q ("nodes_meta_attic", 128*1024, 0)), 270614614 q NODE_CHANGELOG(new OSM_File_Properties< Timestamp > 271614614 q ("node_changelog", 128*1024, 0)), 273614614 q WAYS(new OSM_File_Properties< Way::Index >("ways_attic", 128*1024, 256*1024)), 274614614 q WAYS_UNDELETED(new OSM_File_Properties< Way::Index >("ways_attic_undeleted", 128*1024, 64*1024)), 275614614 q WAY_IDX_LIST(new OSM_File_Properties< Way::Id_Type > 276614614 q ("way_attic_indexes", 128*1024, 0)), 277614614 q WAY_TAGS_LOCAL(new OSM_File_Properties< Tag_Index_Local > 278614614 q ("way_tags_local_attic", 128*1024, 0)), 279614614 q WAY_TAGS_GLOBAL(new OSM_File_Properties< Tag_Index_Global > 280614614 q ("way_tags_global_attic", 128*1024, 0, 7561)), 281614614 q WAY_TAGS_GLOBAL_756(new OSM_File_Properties< Tag_Index_Global_Until756 > 282614614 q ("way_tags_global_attic", 128*1024, 0)), 283614614 q WAY_FREQUENT_TAGS(new OSM_File_Properties< String_Index > 284614614 q ("way_frequent_tags_attic", 512*1024, 0)), 285614614 q WAYS_META(new OSM_File_Properties< Uint31_Index > 286614614 q ("ways_meta_attic", 128*1024, 0)), 287614614 q WAY_CHANGELOG(new OSM_File_Properties< Timestamp > 288614614 q ("way_changelog", 128*1024, 0)), 290614614 q RELATIONS(new OSM_File_Properties< Relation::Index >("relations_attic", 512*1024, 256*1024)), 291614614 q RELATIONS_UNDELETED(new OSM_File_Properties< Relation::Index >("relations_attic_undeleted", 128*1024, 64*1024)), 292614614 q RELATION_IDX_LIST(new OSM_File_Properties< Relation::Id_Type > 293614614 q ("relation_attic_indexes", 128*1024, 0)), 294614614 q RELATION_TAGS_LOCAL(new OSM_File_Properties< Tag_Index_Local > 295614614 q ("relation_tags_local_attic", 128*1024, 0)), 296614614 q RELATION_TAGS_GLOBAL(new OSM_File_Properties< Tag_Index_Global > 297614614 q ("relation_tags_global_attic", 128*1024, 0, 7561)), 298614614 q RELATION_TAGS_GLOBAL_756(new OSM_File_Properties< Tag_Index_Global_Until756 > 299614614 q ("relation_tags_global_attic", 128*1024, 0)), 300614614 q RELATION_FREQUENT_TAGS(new OSM_File_Properties< String_Index > 301614614 q ("relation_frequent_tags_attic", 512*1024, 0)), 302614614 q RELATIONS_META(new OSM_File_Properties< Uint31_Index > 303614614 q ("relations_meta_attic", 128*1024, 0)), 304614614 q RELATION_CHANGELOG(new OSM_File_Properties< Timestamp > 305614614 q ("relation_changelog", 128*1024, 0)) 308614614 q NODES, NODES_UNDELETED, NODE_IDX_LIST, NODE_TAGS_LOCAL, NODE_TAGS_GLOBAL, NODE_FREQUENT_TAGS, 310614614 q WAYS, WAYS_UNDELETED, WAY_IDX_LIST, WAY_TAGS_LOCAL, WAY_TAGS_GLOBAL, WAY_FREQUENT_TAGS, 312614614 q RELATIONS, RELATIONS_UNDELETED, RELATION_IDX_LIST, 313614614 q RELATION_TAGS_LOCAL, RELATION_TAGS_GLOBAL, RELATION_FREQUENT_TAGS, 314614614 q RELATIONS_META, RELATION_CHANGELOG }; 318614614 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() 336175037614 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';