Overpass API regression suite coverage (Saxony) / template_db/types.h

template_db/types.h

40 of 104 instrumented lines run by the suite (38.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#ifndef DE__OSM3S___TEMPLATE_DB__TYPES_H
20#define DE__OSM3S___TEMPLATE_DB__TYPES_H
22#include <fcntl.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <unistd.h>
27#include <cerrno>
28#include <cstdlib>
29#include <string>
30#include <vector>
32#ifdef NATIVE_LARGE_FILES
33#define ftruncate64 ftruncate
34#define lseek64 lseek
35#define open64 open
36#endif
39typedef unsigned int uint;
41typedef char int8;
42typedef short int int16;
43typedef int int32;
44typedef long long int64;
46typedef unsigned char uint8;
47typedef unsigned short int uint16;
48typedef unsigned int uint32;
49typedef unsigned long long uint64;
52const int S_666 = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
55struct File_Error
57 File_Error(uint32 errno_, const std::string& filename_, const std::string& origin_)
580 : error_number(errno_), filename(filename_), origin(origin_) {}
60 uint32 error_number;
61 std::string filename;
62 std::string origin;
63};
66struct File_Properties_Exception
680 File_Properties_Exception(int32 i) : id(i) {}
70 int32 id;
71};
74struct File_Blocks_Index_Base
76 virtual bool empty() const = 0;
778592668 q virtual ~File_Blocks_Index_Base() {}
79 virtual std::string get_data_file_name() const = 0;
80 virtual uint64 get_block_size() const = 0;
81 virtual uint32 get_compression_factor() const = 0;
82 virtual uint32 get_compression_method() const = 0;
83 virtual uint32 get_block_count() const = 0;
84 virtual int32 get_file_format_version() const = 0;
86 static const int USE_DEFAULT = -1;
87 static const int NO_COMPRESSION = 0;
88 static const int ZLIB_COMPRESSION = 1;
89 static const int LZ4_COMPRESSION = 2;
90 static const unsigned int IDX_HEADER_LENGTH = 8;
91};
94enum class Access_Mode
95{ readonly, writeable, truncate };
98struct File_Properties
100 virtual const std::string& get_file_name_trunk() const = 0;
101 virtual const std::string& get_index_suffix() const = 0;
102 virtual const std::string& get_data_suffix() const = 0;
103 virtual const std::string& get_id_suffix() const = 0;
104 virtual const std::string& get_shadow_suffix() const = 0;
105 virtual uint32 get_block_size() const = 0;
106 virtual uint32 get_compression_factor() const = 0;
107 virtual uint32 get_compression_method() const = 0;
108 virtual uint32 get_map_block_size() const = 0;
109 virtual uint32 get_map_compression_factor() const = 0;
110 virtual uint32 get_map_compression_method() const = 0;
111 virtual std::vector< bool > get_data_footprint(const std::string& db_dir) const = 0;
112 virtual std::vector< bool > get_map_footprint(const std::string& db_dir) const = 0;
113 virtual uint32 id_max_size_of() const = 0;
115 // The returned object is of type File_Blocks_Index< .. >*
116 // and goes into the ownership of the caller.
117 virtual File_Blocks_Index_Base* new_data_index(
118 Access_Mode access_mode, bool use_shadow, const std::string& db_dir, const std::string& file_name_extension)
119 const = 0;
120};
123/** Simple RAII class to keep a file descriptor. */
124class Raw_File
126 Raw_File(const Raw_File&);
127 Raw_File operator=(const Raw_File&);
129 public:
130 Raw_File(const std::string& name, int oflag, mode_t mode, const std::string& caller_id);
131295319668 q ~Raw_File() { close(fd_); }
132788644668 q int fd() const { return fd_; }
133 uint64 size(const std::string& caller_id) const;
134 void resize(uint64 size, const std::string& caller_id) const;
135 void read(void* buf, uint64 size, const std::string& caller_id) const;
136 void write(void* buf, uint64 size, const std::string& caller_id) const;
137 void seek(uint64 pos, const std::string& caller_id) const;
139 private:
140 int fd_;
141 std::string name;
142};
145/** Simple RAII class to keep a pointer to some memory on the heap. */
146template < class T >
147class Void_Pointer
149 Void_Pointer(const Void_Pointer&);
150 Void_Pointer& operator=(const Void_Pointer&);
152public:
1535777228 q explicit Void_Pointer(int block_size) { ptr = block_size > 0 ? (T*)malloc(block_size) : 0; }
1545777228 q ~Void_Pointer() { clear(); }
156 Void_Pointer(Void_Pointer&& rhs) : ptr(rhs.ptr)
157 {
158 rhs.ptr = nullptr;
159 }
160 Void_Pointer& operator=(Void_Pointer&& rhs)
161 {
162 if (ptr != rhs.ptr)
163 {
164 clear();
165 ptr = rhs.ptr;
166 rhs.ptr = nullptr;
167 }
168 return *this;
169 }
171 void clear()
1725777228 q {
1735777228 q if (ptr)
1745777228 q free(ptr);
1755777228 q ptr = 0;
1765777228 q }
177 void resize(int block_size)
1780 {
1790 clear();
1800 ptr = 0;
1810 if (block_size > 0)
1820 ptr = (T*)malloc(block_size);
1830 }
185 T* ptr;
186};
190/** Simple RAII class to keep a pointer to some memory on the heap. Enforces 64 bit alignment. */
191template < class T >
192class Void64_Pointer
194 Void64_Pointer(const Void64_Pointer&);
195 Void64_Pointer& operator=(const Void64_Pointer&);
197public:
198 explicit Void64_Pointer(int block_size)
1992632274668 q {
2002632274668 q ptr = block_size > 0 ? (T*)aligned_alloc(8, block_size) : 0;
2012632274668 q }
2022632274668 q ~Void64_Pointer() { clear(); }
204 Void64_Pointer(Void64_Pointer&& rhs) : ptr(rhs.ptr)
205 {
206 rhs.ptr = nullptr;
207 }
208 Void64_Pointer& operator=(Void64_Pointer&& rhs)
209 {
210 if (ptr != rhs.ptr)
211 {
212 clear();
213 ptr = rhs.ptr;
214 rhs.ptr = nullptr;
215 }
216 return *this;
217 }
219 void clear()
2202632274668 q {
2212632274668 q if (ptr)
2222632274668 q free(ptr);
2232632274668 q ptr = 0;
2242632274668 q }
225 void resize(int block_size)
226 {
227 clear();
228 ptr = 0;
229 if (block_size > 0)
230 ptr = (T*)aligned_alloc(8, block_size);
231 }
232 void swap(Void64_Pointer& rhs)
2330 {
2340 T* temp = ptr;
2350 ptr = rhs.ptr;
2360 rhs.ptr = temp;
2370 }
239 T* ptr;
240};
243inline bool file_exists(const std::string& filename)
2440{
2450 return (access(filename.c_str(), F_OK) == 0);
2460}
249/** Simple RAII class to keep a unix socket. */
250class Unix_Socket
252public:
253 Unix_Socket(const std::string& socket_name = "", uint max_num_reading_processes = 0);
254 void open(const std::string& socket_name);
255 ~Unix_Socket();
2570 int descriptor() const { return socket_descriptor; }
259private:
260 int socket_descriptor;
261 uint max_num_reading_processes;
262};
265//-----------------------------------------------------------------------------
267inline Raw_File::Raw_File(const std::string& name_, int oflag, mode_t mode, const std::string& caller_id)
268295319668 q : fd_(0), name(name_)
269295319668 q{
270295319668 q fd_ = open64(name.c_str(), oflag, mode);
271295319668 q if (fd_ < 0)
2720 throw File_Error(errno, name, caller_id);
273295319668 q if (mode & O_CREAT)
2740 {
2750 int ret = fchmod(fd_, mode);
2760 if (ret < 0)
2770 throw File_Error(errno, name, caller_id + "::fchmod");
2780 }
279295319668 q}
281inline uint64 Raw_File::size(const std::string& caller_id) const
28218310668 q{
28318310668 q off64_t size = lseek64(fd_, 0, SEEK_END);
28418310668 q if (size < 0)
2850 throw File_Error(errno, name, caller_id);
28618310668 q off64_t foo = lseek64(fd_, 0, SEEK_SET);
28718310668 q if (foo != 0)
2880 throw File_Error(errno, name, caller_id);
28918310668 q return size;
29018310668 q}
292inline void Raw_File::resize(uint64 size, const std::string& caller_id) const
2930{
2940 uint64 foo = ftruncate64(fd_, size);
2950 if (foo != 0)
2960 throw File_Error(errno, name, caller_id);
2970}
299inline void Raw_File::read(void* buf, uint64 size, const std::string& caller_id) const
300158478228 q{
301158478228 q uint64 foo = ::read(fd_, buf, size);
302158478228 q if (foo != size)
3030 throw File_Error(errno, name, caller_id);
304158478228 q}
306inline void Raw_File::write(void* buf, uint64 size, const std::string& caller_id) const
3070{
3080 uint64 foo = ::write(fd_, buf, size);
3090 if (foo != size)
3100 throw File_Error(errno, name, caller_id);
3110}
313inline void Raw_File::seek(uint64 pos, const std::string& caller_id) const
314157915195 q{
315157915195 q uint64 foo = lseek64(fd_, pos, SEEK_SET);
316157915195 q if (foo != pos)
3170 throw File_Error(errno, name, caller_id);
318157915195 q}
320//-----------------------------------------------------------------------------
323template< typename Int >
324int shift_log(Int val)
3250{
3260 int count = 0;
3270 while (val > 1)
3280 {
3290 val = val>>1;
3300 ++count;
3310 }
3320 return count;
3330}
336template< typename Iterator, typename Object >
337void rearrange_block(const Iterator& begin, Iterator& it, Object to_move)
3380{
3390 Iterator predecessor = it;
3400 if (it != begin)
3410 --predecessor;
3420 while (to_move < *predecessor)
3430 {
3440 *it = *predecessor;
3450 --it;
3460 if (it == begin)
3470 break;
3480 --predecessor;
3490 }
3500 *it = to_move;
3510}
354inline void zero_padding(uint8* from, uint32 bytes)
3550{
3560 for (uint32 i = 0; i < bytes; ++i)
3570 *(from + i) = 0;
3580}
361//-----------------------------------------------------------------------------
364int& global_read_counter();
366enum Signal_Status { absent = 0, received, processed };
367Signal_Status& sigterm_status();
368void sigterm(int);
371void millisleep(uint32 milliseconds);
374void copy_file(const std::string& source, const std::string& dest);
375void force_link_file(const std::string& source, const std::string& dest);
378#endif