Libosmium: include/osmium/io/compression.hpp Source File

Libosmium Fast and flexible C++ library for working with OpenStreetMap data

Loading...
Searching...
No Matches
compression.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_IO_COMPRESSION_HPP
2#define OSMIUM_IO_COMPRESSION_HPP
3
4/*
5
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2026 Jochen Topf <jochen@topf.org> and others (see README).
9
10Boost Software License - Version 1.0 - August 17th, 2003
11
12Permission is hereby granted, free of charge, to any person or organization
13obtaining a copy of the software and accompanying documentation covered by
14this license (the "Software") to use, reproduce, display, distribute,
15execute, and transmit the Software, and to prepare derivative works of the
16Software, and to permit third-parties to whom the Software is furnished to
17do so, all subject to the following:
18
19The copyright notices in the Software and this entire statement, including
20the above license grant, this restriction and the following disclaimer,
21must be included in all copies of the Software, in whole or in part, and
22all derivative works of the Software, unless such copies or derivative
23works are solely in the form of machine-executable object code generated by
24a source language processor.
25
26THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32DEALINGS IN THE SOFTWARE.
33
34*/
35
36#include <osmium/io/detail/read_write.hpp>
37#include <osmium/io/error.hpp>
38#include <osmium/io/file_compression.hpp>
39#include <osmium/io/writer_options.hpp>
40#include <osmium/util/file.hpp>
41
42#include <atomic>
43#include <cerrno>
44#include <cstddef>
45#include <functional>
46#include <map>
47#include <memory>
48#include <string>
49#include <system_error>
50#include <tuple>
51#include <utility>
52
53namespace osmium {
54
55 namespace io {
56
57 class Compressor {
58
59 fsync m_fsync;
60
61 protected:
62
63 bool do_fsync() const noexcept {
64 return m_fsync == fsync::yes;
65 }
66
67 public:
68
69 explicit Compressor(const fsync sync) noexcept :
70 m_fsync(sync) {
71 }
72
73 Compressor(const Compressor&) = default;
74 Compressor& operator=(const Compressor&) = default;
75
76 Compressor(Compressor&&) noexcept = default;
77 Compressor& operator=(Compressor&&) noexcept = default;
78
79 virtual ~Compressor() noexcept = default;
80
81 virtual void write(const std::string& data) = 0;
82
83 virtual void close() = 0;
84
85 virtual std::size_t file_size() const {
86 return 0;
87 }
88
89 }; // class Compressor
90
91 class Decompressor {
92
93 std::atomic<std::size_t>* m_offset_ptr{nullptr};
94
95 std::atomic_bool m_want_buffered_pages_removed{false};
96
97 public:
98
99 enum {
100 input_buffer_size = 1024U * 1024U
101 };
102
103 Decompressor() = default;
104
105 Decompressor(const Decompressor&) = delete;
106 Decompressor& operator=(const Decompressor&) = delete;
107
108 Decompressor(Decompressor&&) = delete;
109 Decompressor& operator=(Decompressor&&) = delete;
110
111 virtual ~Decompressor() noexcept = default;
112
113 virtual std::string read() = 0;
114
115 virtual void close() = 0;
116
117 virtual bool is_real() const noexcept {
118 return true;
119 }
120
121 void set_offset_ptr(std::atomic<std::size_t>* offset_ptr) noexcept {
122 m_offset_ptr = offset_ptr;
123 }
124
125 void set_offset(const std::size_t offset) noexcept {
126 if (m_offset_ptr) {
127 *m_offset_ptr = offset;
128 }
129 }
130
131 bool want_buffered_pages_removed() const noexcept {
132 return m_want_buffered_pages_removed;
133 }
134
135 void set_want_buffered_pages_removed(bool value) noexcept {
136 m_want_buffered_pages_removed = value;
137 }
138
139 }; // class Decompressor
140
148 class CompressionFactory {
149
150 public:
151
152 using create_compressor_type = std::function<osmium::io::Compressor*(int, fsync)>;
153 using create_decompressor_type_fd = std::function<osmium::io::Decompressor*(int)>;
154 using create_decompressor_type_buffer = std::function<osmium::io::Decompressor*(const char*, std::size_t)>;
155
156 private:
157
158 using callbacks_type = std::tuple<create_compressor_type,
159 create_decompressor_type_fd,
160 create_decompressor_type_buffer>;
161
162 using compression_map_type = std::map<const osmium::io::file_compression, callbacks_type>;
163
164 compression_map_type m_callbacks;
165
166 CompressionFactory() = default;
167
168 const callbacks_type& find_callbacks(const osmium::io::file_compression compression) const {
169 const auto it = m_callbacks.find(compression);
170
171 if (it != m_callbacks.end()) {
172 return it->second;
173 }
174
175 std::string error_message{"Support for compression '"};
176 error_message += as_string(compression);
177 error_message += "' not compiled into this binary";
178 throw unsupported_file_format_error{error_message};
179 }
180
181 public:
182
183 CompressionFactory(const CompressionFactory&) = delete;
184 CompressionFactory& operator=(const CompressionFactory&) = delete;
185
186 CompressionFactory(CompressionFactory&&) = delete;
187 CompressionFactory& operator=(CompressionFactory&&) = delete;
188
189 ~CompressionFactory() noexcept = default;
190
191 static CompressionFactory& instance() {
192 static CompressionFactory factory;
193 return factory;
194 }
195
196 bool register_compression(
197 osmium::io::file_compression compression,
198 const create_compressor_type& create_compressor,
199 const create_decompressor_type_fd& create_decompressor_fd,
200 const create_decompressor_type_buffer& create_decompressor_buffer) {
201
202 const compression_map_type::value_type cc{compression,
203 std::make_tuple(create_compressor,
204 create_decompressor_fd,
205 create_decompressor_buffer)};
206
207 return m_callbacks.insert(cc).second;
208 }
209
210 void clear_register() {
211 m_callbacks.clear();
212 }
213
214 template <typename... TArgs>
215 std::unique_ptr<osmium::io::Compressor> create_compressor(const osmium::io::file_compression compression, TArgs&&... args) const {
216 const auto callbacks = find_callbacks(compression);
217 return std::unique_ptr<osmium::io::Compressor>(std::get<0>(callbacks)(std::forward<TArgs>(args)...));
218 }
219
220 std::unique_ptr<osmium::io::Decompressor> create_decompressor(const osmium::io::file_compression compression, const int fd) const {
221 const auto callbacks = find_callbacks(compression);
222 return std::unique_ptr<osmium::io::Decompressor>(std::get<1>(callbacks)(fd));
223 }
224
225 std::unique_ptr<osmium::io::Decompressor> create_decompressor(const osmium::io::file_compression compression, const char* buffer, const std::size_t size) const {
226 const auto callbacks = find_callbacks(compression);
227 return std::unique_ptr<osmium::io::Decompressor>(std::get<2>(callbacks)(buffer, size));
228 }
229
230 }; // class CompressionFactory
231
232 class NoCompressor final : public Compressor {
233
234 std::size_t m_file_size = 0;
235 int m_fd;
236
237 public:
238
239 NoCompressor(const int fd, const fsync sync) :
240 Compressor(sync),
241 m_fd(fd) {
242 }
243
244 NoCompressor(const NoCompressor&) = delete;
245 NoCompressor& operator=(const NoCompressor&) = delete;
246
247 NoCompressor(NoCompressor&&) = delete;
248 NoCompressor& operator=(NoCompressor&&) = delete;
249
250 ~NoCompressor() noexcept override {
251 try {
252 close();
253 } catch (...) { // NOLINT(bugprone-empty-catch)
254 // Ignore any exceptions because destructor must not throw.
255 }
256 }
257
258 void write(const std::string& data) override {
259 osmium::io::detail::reliable_write(m_fd, data.data(), data.size());
260 m_file_size += data.size();
261 }
262
263 void close() override {
264 if (m_fd >= 0) {
265 const int fd = m_fd;
266 m_fd = -1;
267
268 // Do not sync or close stdout
269 if (fd == 1) {
270 return;
271 }
272
273 if (do_fsync()) {
274 osmium::io::detail::reliable_fsync(fd);
275 }
276 osmium::io::detail::reliable_close(fd);
277 }
278 }
279
280 std::size_t file_size() const override {
281 return m_file_size;
282 }
283
284 }; // class NoCompressor
285
291 class DummyDecompressor final : public Decompressor {
292 public:
293
294 DummyDecompressor() = default;
295
296 DummyDecompressor(const DummyDecompressor&) = delete;
297 DummyDecompressor& operator=(const DummyDecompressor&) = delete;
298
299 DummyDecompressor(DummyDecompressor&&) = delete;
300 DummyDecompressor& operator=(DummyDecompressor&&) = delete;
301
302 ~DummyDecompressor() noexcept override = default;
303
304 std::string read() override {
305 return {};
306 }
307
308 void close() override {
309 }
310
311 bool is_real() const noexcept override {
312 return false;
313 }
314
315 }; // class DummyDecompressor
316
317 class NoDecompressor final : public Decompressor {
318
319 int m_fd = -1;
320 const char* m_buffer = nullptr;
321 std::size_t m_buffer_size = 0;
322 std::size_t m_offset = 0;
323
324 public:
325
326 explicit NoDecompressor(const int fd) :
327 m_fd(fd) {
328 }
329
330 NoDecompressor(const char* buffer, const std::size_t size) :
331 m_buffer(buffer),
332 m_buffer_size(size) {
333 }
334
335 NoDecompressor(const NoDecompressor&) = delete;
336 NoDecompressor& operator=(const NoDecompressor&) = delete;
337
338 NoDecompressor(NoDecompressor&&) = delete;
339 NoDecompressor& operator=(NoDecompressor&&) = delete;
340
341 ~NoDecompressor() noexcept override {
342 try {
343 close();
344 } catch (...) { // NOLINT(bugprone-empty-catch)
345 // Ignore any exceptions because destructor must not throw.
346 }
347 }
348
349 std::string read() override {
350 std::string buffer;
351
352 if (m_buffer) {
353 if (m_buffer_size != 0) {
354 const std::size_t size = m_buffer_size;
355 m_buffer_size = 0;
356 buffer.append(m_buffer, size);
357 }
358 } else {
359 buffer.resize(osmium::io::Decompressor::input_buffer_size);
360 if (want_buffered_pages_removed()) {
361 osmium::io::detail::remove_buffered_pages(m_fd, m_offset);
362 }
363 const auto nread = detail::reliable_read(m_fd, &*buffer.begin(), osmium::io::Decompressor::input_buffer_size);
364 buffer.resize(static_cast<std::string::size_type>(nread));
365 }
366
367 m_offset += buffer.size();
368 set_offset(m_offset);
369
370 return buffer;
371 }
372
373 void close() override {
374 if (m_fd >= 0) {
375 if (want_buffered_pages_removed()) {
376 osmium::io::detail::remove_buffered_pages(m_fd);
377 }
378 const int fd = m_fd;
379 m_fd = -1;
380 osmium::io::detail::reliable_close(fd);
381 }
382 }
383
384 }; // class NoDecompressor
385
386 namespace detail {
387
388 // we want the register_compression() function to run, setting
389 // the variable is only a side-effect, it will never be used
390 const bool registered_no_compression = osmium::io::CompressionFactory::instance().register_compression(osmium::io::file_compression::none,
391 [](const int fd, const fsync sync) { return new osmium::io::NoCompressor{fd, sync}; },
392 [](const int fd) { return new osmium::io::NoDecompressor{fd}; },
393 [](const char* buffer, std::size_t size) { return new osmium::io::NoDecompressor{buffer, size}; }
394 );
395
396 // dummy function to silence the unused variable warning from above
397 inline bool get_registered_no_compression() noexcept {
398 return registered_no_compression;
399 }
400
401 } // namespace detail
402
403 } // namespace io
404
405} // namespace osmium
406
407#endif // OSMIUM_IO_COMPRESSION_HPP
osmium::io::CompressionFactory::~CompressionFactory
~CompressionFactory() noexcept=default
osmium::io::CompressionFactory::create_decompressor
std::unique_ptr< osmium::io::Decompressor > create_decompressor(const osmium::io::file_compression compression, const int fd) const
Definition compression.hpp:220
osmium::io::CompressionFactory::register_compression
bool register_compression(osmium::io::file_compression compression, const create_compressor_type &create_compressor, const create_decompressor_type_fd &create_decompressor_fd, const create_decompressor_type_buffer &create_decompressor_buffer)
Definition compression.hpp:196
osmium::io::CompressionFactory::m_callbacks
compression_map_type m_callbacks
Definition compression.hpp:164
osmium::io::CompressionFactory::operator=
CompressionFactory & operator=(const CompressionFactory &)=delete
osmium::io::CompressionFactory::create_compressor_type
std::function< osmium::io::Compressor *(int, fsync)> create_compressor_type
Definition compression.hpp:152
osmium::io::CompressionFactory::clear_register
void clear_register()
Definition compression.hpp:210
osmium::io::CompressionFactory::callbacks_type
std::tuple< create_compressor_type, create_decompressor_type_fd, create_decompressor_type_buffer > callbacks_type
Definition compression.hpp:158
osmium::io::CompressionFactory::find_callbacks
const callbacks_type & find_callbacks(const osmium::io::file_compression compression) const
Definition compression.hpp:168
osmium::io::CompressionFactory::compression_map_type
std::map< const osmium::io::file_compression, callbacks_type > compression_map_type
Definition compression.hpp:162
osmium::io::CompressionFactory::create_compressor
std::unique_ptr< osmium::io::Compressor > create_compressor(const osmium::io::file_compression compression, TArgs &&... args) const
Definition compression.hpp:215
osmium::io::CompressionFactory::create_decompressor_type_fd
std::function< osmium::io::Decompressor *(int)> create_decompressor_type_fd
Definition compression.hpp:153
osmium::io::CompressionFactory::CompressionFactory
CompressionFactory(const CompressionFactory &)=delete
osmium::io::CompressionFactory::operator=
CompressionFactory & operator=(CompressionFactory &&)=delete
osmium::io::CompressionFactory::create_decompressor_type_buffer
std::function< osmium::io::Decompressor *(const char *, std::size_t)> create_decompressor_type_buffer
Definition compression.hpp:154
osmium::io::CompressionFactory::CompressionFactory
CompressionFactory(CompressionFactory &&)=delete
osmium::io::CompressionFactory::CompressionFactory
CompressionFactory()=default
osmium::io::CompressionFactory::instance
static CompressionFactory & instance()
Definition compression.hpp:191
osmium::io::CompressionFactory::create_decompressor
std::unique_ptr< osmium::io::Decompressor > create_decompressor(const osmium::io::file_compression compression, const char *buffer, const std::size_t size) const
Definition compression.hpp:225
osmium::io::Compressor
Definition compression.hpp:57
osmium::io::Compressor::do_fsync
bool do_fsync() const noexcept
Definition compression.hpp:63
osmium::io::Compressor::Compressor
Compressor(const fsync sync) noexcept
Definition compression.hpp:69
osmium::io::Compressor::Compressor
Compressor(const Compressor &)=default
osmium::io::Compressor::Compressor
Compressor(Compressor &&) noexcept=default
osmium::io::Compressor::m_fsync
fsync m_fsync
Definition compression.hpp:59
osmium::io::Compressor::write
virtual void write(const std::string &data)=0
osmium::io::Compressor::close
virtual void close()=0
osmium::io::Compressor::operator=
Compressor & operator=(const Compressor &)=default
osmium::io::Compressor::file_size
virtual std::size_t file_size() const
Definition compression.hpp:85
osmium::io::Decompressor
Definition compression.hpp:91
osmium::io::Decompressor::m_want_buffered_pages_removed
std::atomic_bool m_want_buffered_pages_removed
Definition compression.hpp:95
osmium::io::Decompressor::set_offset_ptr
void set_offset_ptr(std::atomic< std::size_t > *offset_ptr) noexcept
Definition compression.hpp:121
osmium::io::Decompressor::is_real
virtual bool is_real() const noexcept
Definition compression.hpp:117
osmium::io::Decompressor::input_buffer_size
@ input_buffer_size
Definition compression.hpp:100
osmium::io::Decompressor::read
virtual std::string read()=0
osmium::io::Decompressor::Decompressor
Decompressor()=default
osmium::io::Decompressor::operator=
Decompressor & operator=(const Decompressor &)=delete
osmium::io::Decompressor::set_want_buffered_pages_removed
void set_want_buffered_pages_removed(bool value) noexcept
Definition compression.hpp:135
osmium::io::Decompressor::close
virtual void close()=0
osmium::io::Decompressor::operator=
Decompressor & operator=(Decompressor &&)=delete
osmium::io::Decompressor::~Decompressor
virtual ~Decompressor() noexcept=default
osmium::io::Decompressor::m_offset_ptr
std::atomic< std::size_t > * m_offset_ptr
Definition compression.hpp:93
osmium::io::Decompressor::set_offset
void set_offset(const std::size_t offset) noexcept
Definition compression.hpp:125
osmium::io::Decompressor::want_buffered_pages_removed
bool want_buffered_pages_removed() const noexcept
Definition compression.hpp:131
osmium::io::Decompressor::Decompressor
Decompressor(const Decompressor &)=delete
osmium::io::Decompressor::Decompressor
Decompressor(Decompressor &&)=delete
osmium::io::DummyDecompressor::operator=
DummyDecompressor & operator=(const DummyDecompressor &)=delete
osmium::io::DummyDecompressor::~DummyDecompressor
~DummyDecompressor() noexcept override=default
osmium::io::DummyDecompressor::close
void close() override
Definition compression.hpp:308
osmium::io::DummyDecompressor::is_real
bool is_real() const noexcept override
Definition compression.hpp:311
osmium::io::DummyDecompressor::DummyDecompressor
DummyDecompressor()=default
osmium::io::DummyDecompressor::read
std::string read() override
Definition compression.hpp:304
osmium::io::DummyDecompressor::DummyDecompressor
DummyDecompressor(DummyDecompressor &&)=delete
osmium::io::DummyDecompressor::DummyDecompressor
DummyDecompressor(const DummyDecompressor &)=delete
osmium::io::DummyDecompressor::operator=
DummyDecompressor & operator=(DummyDecompressor &&)=delete
osmium::io::NoCompressor
Definition compression.hpp:232
osmium::io::NoCompressor::close
void close() override
Definition compression.hpp:263
osmium::io::NoCompressor::write
void write(const std::string &data) override
Definition compression.hpp:258
osmium::io::NoCompressor::NoCompressor
NoCompressor(const NoCompressor &)=delete
osmium::io::NoCompressor::operator=
NoCompressor & operator=(NoCompressor &&)=delete
osmium::io::NoCompressor::~NoCompressor
~NoCompressor() noexcept override
Definition compression.hpp:250
osmium::io::NoCompressor::m_file_size
std::size_t m_file_size
Definition compression.hpp:234
osmium::io::NoCompressor::operator=
NoCompressor & operator=(const NoCompressor &)=delete
osmium::io::NoCompressor::file_size
std::size_t file_size() const override
Definition compression.hpp:280
osmium::io::NoCompressor::NoCompressor
NoCompressor(const int fd, const fsync sync)
Definition compression.hpp:239
osmium::io::NoCompressor::NoCompressor
NoCompressor(NoCompressor &&)=delete
osmium::io::NoCompressor::m_fd
int m_fd
Definition compression.hpp:235
osmium::io::NoDecompressor::operator=
NoDecompressor & operator=(const NoDecompressor &)=delete
osmium::io::NoDecompressor::m_buffer
const char * m_buffer
Definition compression.hpp:320
osmium::io::NoDecompressor::NoDecompressor
NoDecompressor(const NoDecompressor &)=delete
osmium::io::NoDecompressor::m_fd
int m_fd
Definition compression.hpp:319
osmium::io::NoDecompressor::NoDecompressor
NoDecompressor(const char *buffer, const std::size_t size)
Definition compression.hpp:330
osmium::io::NoDecompressor::m_buffer_size
std::size_t m_buffer_size
Definition compression.hpp:321
osmium::io::NoDecompressor::m_offset
std::size_t m_offset
Definition compression.hpp:322
osmium::io::NoDecompressor::operator=
NoDecompressor & operator=(NoDecompressor &&)=delete
osmium::io::NoDecompressor::read
std::string read() override
Definition compression.hpp:349
osmium::io::NoDecompressor::NoDecompressor
NoDecompressor(NoDecompressor &&)=delete
osmium::io::NoDecompressor::~NoDecompressor
~NoDecompressor() noexcept override
Definition compression.hpp:341
osmium::io::NoDecompressor::NoDecompressor
NoDecompressor(const int fd)
Definition compression.hpp:326
osmium::io::NoDecompressor::close
void close() override
Definition compression.hpp:373
error.hpp
file_compression.hpp
detail
Definition attr.hpp:342
osmium::io
Everything related to input and output of OSM data.
Definition bzip2_compression.hpp:86
osmium::io::as_string
const char * as_string(file_compression compression)
Definition file_compression.hpp:48
osmium::io::file_compression
file_compression
Definition file_compression.hpp:42
osmium::io::file_compression::none
@ none
Definition file_compression.hpp:43
osmium::io::fsync
fsync
Definition writer_options.hpp:51
osmium::io::fsync::yes
@ yes
Definition writer_options.hpp:53
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
std
Definition location.hpp:654
osmium::unsupported_file_format_error
Definition error.hpp:58
file.hpp
writer_options.hpp