Loading...
Searching...
No Matches
compression.hpp
Go to the documentation of this file.
#ifndef OSMIUM_IO_COMPRESSION_HPP
#define OSMIUM_IO_COMPRESSION_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2025 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/io/detail/read_write.hpp>
#include <osmium/io/error.hpp>
#include <osmium/io/file_compression.hpp>
#include <osmium/io/writer_options.hpp>
#include <osmium/util/file.hpp>
#include <atomic>
#include <cerrno>
#include <cstddef>
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <system_error>
#include <tuple>
#include <utility>
namespace osmium {
namespace io {
class Compressor {
fsync m_fsync;
protected:
bool do_fsync() const noexcept {
return m_fsync == fsync::yes;
}
public:
explicit Compressor(const fsync sync) noexcept :
m_fsync(sync) {
}
Compressor(const Compressor&) = default;
Compressor& operator=(const Compressor&) = default;
Compressor(Compressor&&) noexcept = default;
Compressor& operator=(Compressor&&) noexcept = default;
virtual ~Compressor() noexcept = default;
virtual void write(const std::string& data) = 0;
virtual void close() = 0;
virtual std::size_t file_size() const {
return 0;
}
}; // class Compressor
class Decompressor {
std::atomic<std::size_t>* m_offset_ptr{nullptr};
std::atomic_bool m_want_buffered_pages_removed{false};
public:
enum {
input_buffer_size = 1024U * 1024U
};
Decompressor() = default;
Decompressor(const Decompressor&) = delete;
Decompressor& operator=(const Decompressor&) = delete;
Decompressor(Decompressor&&) = delete;
Decompressor& operator=(Decompressor&&) = delete;
virtual ~Decompressor() noexcept = default;
virtual std::string read() = 0;
virtual void close() = 0;
virtual bool is_real() const noexcept {
return true;
}
void set_offset_ptr(std::atomic<std::size_t>* offset_ptr) noexcept {
m_offset_ptr = offset_ptr;
}
void set_offset(const std::size_t offset) noexcept {
if (m_offset_ptr) {
*m_offset_ptr = offset;
}
}
bool want_buffered_pages_removed() const noexcept {
return m_want_buffered_pages_removed;
}
void set_want_buffered_pages_removed(bool value) noexcept {
m_want_buffered_pages_removed = value;
}
}; // class Decompressor
class CompressionFactory {
public:
using create_compressor_type = std::function<osmium::io::Compressor*(int, fsync)>;
using create_decompressor_type_fd = std::function<osmium::io::Decompressor*(int)>;
using create_decompressor_type_buffer = std::function<osmium::io::Decompressor*(const char*, std::size_t)>;
private:
using callbacks_type = std::tuple<create_compressor_type,
create_decompressor_type_fd,
create_decompressor_type_buffer>;
using compression_map_type = std::map<const osmium::io::file_compression, callbacks_type>;
compression_map_type m_callbacks;
CompressionFactory() = default;
const callbacks_type& find_callbacks(const osmium::io::file_compression compression) const {
const auto it = m_callbacks.find(compression);
if (it != m_callbacks.end()) {
return it->second;
}
std::string error_message{"Support for compression '"};
error_message += as_string(compression);
error_message += "' not compiled into this binary";
throw unsupported_file_format_error{error_message};
}
public:
CompressionFactory(const CompressionFactory&) = delete;
CompressionFactory& operator=(const CompressionFactory&) = delete;
CompressionFactory(CompressionFactory&&) = delete;
CompressionFactory& operator=(CompressionFactory&&) = delete;
~CompressionFactory() noexcept = default;
static CompressionFactory& instance() {
static CompressionFactory factory;
return factory;
}
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) {
const compression_map_type::value_type cc{compression,
std::make_tuple(create_compressor,
create_decompressor_fd,
create_decompressor_buffer)};
return m_callbacks.insert(cc).second;
}
template <typename... TArgs>
std::unique_ptr<osmium::io::Compressor> create_compressor(const osmium::io::file_compression compression, TArgs&&... args) const {
const auto callbacks = find_callbacks(compression);
return std::unique_ptr<osmium::io::Compressor>(std::get<0>(callbacks)(std::forward<TArgs>(args)...));
}
std::unique_ptr<osmium::io::Decompressor> create_decompressor(const osmium::io::file_compression compression, const int fd) const {
const auto callbacks = find_callbacks(compression);
return std::unique_ptr<osmium::io::Decompressor>(std::get<1>(callbacks)(fd));
}
std::unique_ptr<osmium::io::Decompressor> create_decompressor(const osmium::io::file_compression compression, const char* buffer, const std::size_t size) const {
const auto callbacks = find_callbacks(compression);
return std::unique_ptr<osmium::io::Decompressor>(std::get<2>(callbacks)(buffer, size));
}
}; // class CompressionFactory
class NoCompressor final : public Compressor {
std::size_t m_file_size = 0;
int m_fd;
public:
NoCompressor(const int fd, const fsync sync) :
Compressor(sync),
m_fd(fd) {
}
NoCompressor(const NoCompressor&) = delete;
NoCompressor& operator=(const NoCompressor&) = delete;
NoCompressor(NoCompressor&&) = delete;
NoCompressor& operator=(NoCompressor&&) = delete;
~NoCompressor() noexcept override {
try {
close();
} catch (...) { // NOLINT(bugprone-empty-catch)
// Ignore any exceptions because destructor must not throw.
}
}
void write(const std::string& data) override {
osmium::io::detail::reliable_write(m_fd, data.data(), data.size());
m_file_size += data.size();
}
void close() override {
if (m_fd >= 0) {
const int fd = m_fd;
m_fd = -1;
// Do not sync or close stdout
if (fd == 1) {
return;
}
if (do_fsync()) {
osmium::io::detail::reliable_fsync(fd);
}
osmium::io::detail::reliable_close(fd);
}
}
std::size_t file_size() const override {
return m_file_size;
}
}; // class NoCompressor
class DummyDecompressor final : public Decompressor {
public:
DummyDecompressor() = default;
DummyDecompressor(const DummyDecompressor&) = delete;
DummyDecompressor& operator=(const DummyDecompressor&) = delete;
DummyDecompressor(DummyDecompressor&&) = delete;
DummyDecompressor& operator=(DummyDecompressor&&) = delete;
~DummyDecompressor() noexcept override = default;
std::string read() override {
return {};
}
void close() override {
}
bool is_real() const noexcept override {
return false;
}
}; // class DummyDecompressor
class NoDecompressor final : public Decompressor {
int m_fd = -1;
const char* m_buffer = nullptr;
std::size_t m_buffer_size = 0;
std::size_t m_offset = 0;
public:
explicit NoDecompressor(const int fd) :
m_fd(fd) {
}
NoDecompressor(const char* buffer, const std::size_t size) :
m_buffer(buffer),
m_buffer_size(size) {
}
NoDecompressor(const NoDecompressor&) = delete;
NoDecompressor& operator=(const NoDecompressor&) = delete;
NoDecompressor(NoDecompressor&&) = delete;
NoDecompressor& operator=(NoDecompressor&&) = delete;
~NoDecompressor() noexcept override {
try {
close();
} catch (...) { // NOLINT(bugprone-empty-catch)
// Ignore any exceptions because destructor must not throw.
}
}
std::string read() override {
std::string buffer;
if (m_buffer) {
if (m_buffer_size != 0) {
const std::size_t size = m_buffer_size;
m_buffer_size = 0;
buffer.append(m_buffer, size);
}
} else {
buffer.resize(osmium::io::Decompressor::input_buffer_size);
if (want_buffered_pages_removed()) {
osmium::io::detail::remove_buffered_pages(m_fd, m_offset);
}
const auto nread = detail::reliable_read(m_fd, &*buffer.begin(), osmium::io::Decompressor::input_buffer_size);
buffer.resize(static_cast<std::string::size_type>(nread));
}
m_offset += buffer.size();
set_offset(m_offset);
return buffer;
}
void close() override {
if (m_fd >= 0) {
if (want_buffered_pages_removed()) {
osmium::io::detail::remove_buffered_pages(m_fd);
}
const int fd = m_fd;
m_fd = -1;
osmium::io::detail::reliable_close(fd);
}
}
}; // class NoDecompressor
namespace detail {
// we want the register_compression() function to run, setting
// the variable is only a side-effect, it will never be used
const bool registered_no_compression = osmium::io::CompressionFactory::instance().register_compression(osmium::io::file_compression::none,
[](const int fd, const fsync sync) { return new osmium::io::NoCompressor{fd, sync}; },
[](const int fd) { return new osmium::io::NoDecompressor{fd}; },
[](const char* buffer, std::size_t size) { return new osmium::io::NoDecompressor{buffer, size}; }
);
// dummy function to silence the unused variable warning from above
inline bool get_registered_no_compression() noexcept {
return registered_no_compression;
}
} // namespace detail
} // namespace io
} // namespace osmium
#endif // OSMIUM_IO_COMPRESSION_HPP
osmium::io::CompressionFactory
Definition compression.hpp:148
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:216
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::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:211
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::callbacks_type
std::tuple< create_compressor_type, create_decompressor_type_fd, create_decompressor_type_buffer > callbacks_type
Definition compression.hpp:160
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:221
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::input_buffer_size
@ input_buffer_size
Definition compression.hpp:100
osmium::io::Decompressor::is_real
virtual bool is_real() const noexcept
Definition compression.hpp:117
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
Definition compression.hpp:287
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:304
osmium::io::DummyDecompressor::is_real
bool is_real() const noexcept override
Definition compression.hpp:307
osmium::io::DummyDecompressor::DummyDecompressor
DummyDecompressor()=default
osmium::io::DummyDecompressor::read
std::string read() override
Definition compression.hpp:300
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:228
osmium::io::NoCompressor::close
void close() override
Definition compression.hpp:259
osmium::io::NoCompressor::write
void write(const std::string &data) override
Definition compression.hpp:254
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:246
osmium::io::NoCompressor::m_file_size
std::size_t m_file_size
Definition compression.hpp:230
osmium::io::NoCompressor::operator=
NoCompressor & operator=(const NoCompressor &)=delete
osmium::io::NoCompressor::file_size
std::size_t file_size() const override
Definition compression.hpp:276
osmium::io::NoCompressor::NoCompressor
NoCompressor(const int fd, const fsync sync)
Definition compression.hpp:235
osmium::io::NoCompressor::NoCompressor
NoCompressor(NoCompressor &&)=delete
osmium::io::NoCompressor::m_fd
int m_fd
Definition compression.hpp:231
osmium::io::NoDecompressor
Definition compression.hpp:313
osmium::io::NoDecompressor::operator=
NoDecompressor & operator=(const NoDecompressor &)=delete
osmium::io::NoDecompressor::m_buffer
const char * m_buffer
Definition compression.hpp:316
osmium::io::NoDecompressor::NoDecompressor
NoDecompressor(const NoDecompressor &)=delete
osmium::io::NoDecompressor::m_fd
int m_fd
Definition compression.hpp:315
osmium::io::NoDecompressor::NoDecompressor
NoDecompressor(const char *buffer, const std::size_t size)
Definition compression.hpp:326
osmium::io::NoDecompressor::m_buffer_size
std::size_t m_buffer_size
Definition compression.hpp:317
osmium::io::NoDecompressor::m_offset
std::size_t m_offset
Definition compression.hpp:318
osmium::io::NoDecompressor::operator=
NoDecompressor & operator=(NoDecompressor &&)=delete
osmium::io::NoDecompressor::read
std::string read() override
Definition compression.hpp:345
osmium::io::NoDecompressor::NoDecompressor
NoDecompressor(NoDecompressor &&)=delete
osmium::io::NoDecompressor::~NoDecompressor
~NoDecompressor() noexcept override
Definition compression.hpp:337
osmium::io::NoDecompressor::NoDecompressor
NoDecompressor(const int fd)
Definition compression.hpp:322
osmium::io::NoDecompressor::close
void close() override
Definition compression.hpp:369
error.hpp
file_compression.hpp
detail
Definition attr.hpp:342
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
osmium::io::fsync
fsync
Definition writer_options.hpp:51
osmium::io::fsync::yes
@ yes
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
std
Definition location.hpp:555
osmium::unsupported_file_format_error
Definition error.hpp:58
file.hpp
writer_options.hpp