writer.hpp
Go to the documentation of this file.
#ifndef OSMIUM_IO_WRITER_HPP
#define OSMIUM_IO_WRITER_HPP
/*
This file is part of Osmium (http://osmcode.org/libosmium).
Copyright 2013-2016 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 <cassert>
#include <future>
#include <memory>
#include <stdexcept>
#include <string>
#include <thread>
#include <utility>
#include <osmium/io/compression.hpp>
#include <osmium/io/detail/output_format.hpp>
#include <osmium/io/detail/queue_util.hpp>
#include <osmium/io/detail/read_write.hpp>
#include <osmium/io/detail/write_thread.hpp>
#include <osmium/io/error.hpp>
#include <osmium/io/file.hpp>
#include <osmium/io/header.hpp>
#include <osmium/io/writer_options.hpp>
#include <osmium/memory/buffer.hpp>
#include <osmium/thread/util.hpp>
namespace osmium {
namespace io {
class Writer {
static constexpr size_t default_buffer_size = 10 * 1024 * 1024;
osmium::io::File m_file;
detail::future_string_queue_type m_output_queue;
std::unique_ptr<osmium::io::detail::OutputFormat> m_output;
osmium::memory::Buffer m_buffer;
size_t m_buffer_size;
std::future<bool> m_write_future;
osmium::thread::thread_handler m_thread;
enum class status {
okay = 0, // normal writing
error = 1, // some error occurred while writing
closed = 2 // close() called successfully
} m_status;
// This function will run in a separate thread.
static void write_thread(detail::future_string_queue_type& output_queue,
std::unique_ptr<osmium::io::Compressor>&& compressor,
std::promise<bool>&& write_promise) {
detail::WriteThread write_thread{output_queue,
std::move(compressor),
std::move(write_promise)};
write_thread();
}
void do_write(osmium::memory::Buffer&& buffer) {
if (buffer && buffer.committed() > 0) {
m_output->write_buffer(std::move(buffer));
}
}
void do_flush() {
osmium::thread::check_for_exception(m_write_future);
if (m_buffer && m_buffer.committed() > 0) {
osmium::memory::Buffer buffer{m_buffer_size,
osmium::memory::Buffer::auto_grow::no};
using std::swap;
swap(m_buffer, buffer);
m_output->write_buffer(std::move(buffer));
}
}
template <typename TFunction, typename... TArgs>
void ensure_cleanup(TFunction func, TArgs&&... args) {
if (m_status != status::okay) {
throw io_error("Can not write to writer when in status 'closed' or 'error'");
}
try {
func(std::forward<TArgs>(args)...);
} catch (...) {
m_status = status::error;
detail::add_to_queue(m_output_queue, std::current_exception());
detail::add_end_of_data_to_queue(m_output_queue);
throw;
}
}
struct options_type {
osmium::io::Header header;
overwrite allow_overwrite = overwrite::no;
fsync sync = fsync::no;
};
static void set_option(options_type& options, const osmium::io::Header& header) {
options.header = header;
}
static void set_option(options_type& options, overwrite value) {
options.allow_overwrite = value;
}
static void set_option(options_type& options, fsync value) {
options.sync = value;
}
public:
template <typename... TArgs>
explicit Writer(const osmium::io::File& file, TArgs&&... args) :
m_file(file.check()),
m_output_queue(20, "raw_output"), // XXX
m_output(osmium::io::detail::OutputFormatFactory::instance().create_output(m_file, m_output_queue)),
m_buffer(),
m_buffer_size(default_buffer_size),
m_write_future(),
m_thread(),
m_status(status::okay) {
assert(!m_file.buffer()); // XXX can't handle pseudo-files
options_type options;
(void)std::initializer_list<int>{
(set_option(options, args), 0)...
};
std::unique_ptr<osmium::io::Compressor> compressor =
CompressionFactory::instance().create_compressor(file.compression(),
osmium::io::detail::open_for_writing(m_file.filename(), options.allow_overwrite),
options.sync);
std::promise<bool> write_promise;
m_write_future = write_promise.get_future();
m_thread = osmium::thread::thread_handler{write_thread, std::ref(m_output_queue), std::move(compressor), std::move(write_promise)};
ensure_cleanup([&](){
m_output->write_header(options.header);
});
}
template <typename... TArgs>
explicit Writer(const std::string& filename, TArgs&&... args) :
Writer(osmium::io::File(filename), std::forward<TArgs>(args)...) {
}
template <typename... TArgs>
explicit Writer(const char* filename, TArgs&&... args) :
Writer(osmium::io::File(filename), std::forward<TArgs>(args)...) {
}
Writer(const Writer&) = delete;
Writer& operator=(const Writer&) = delete;
Writer(Writer&&) = default;
Writer& operator=(Writer&&) = default;
~Writer() noexcept {
try {
close();
} catch (...) {
// Ignore any exceptions because destructor must not throw.
}
}
size_t buffer_size() const noexcept {
return m_buffer_size;
}
void set_buffer_size(size_t size) noexcept {
m_buffer_size = size;
}
void flush() {
ensure_cleanup([&](){
do_flush();
});
}
void operator()(osmium::memory::Buffer&& buffer) {
ensure_cleanup([&](){
do_flush();
do_write(std::move(buffer));
});
}
void operator()(const osmium::memory::Item& item) {
ensure_cleanup([&](){
if (!m_buffer) {
m_buffer = osmium::memory::Buffer{m_buffer_size,
osmium::memory::Buffer::auto_grow::no};
}
try {
m_buffer.push_back(item);
} catch (osmium::buffer_is_full&) {
do_flush();
m_buffer.push_back(item);
}
});
}
void close() {
if (m_status == status::okay) {
ensure_cleanup([&](){
do_write(std::move(m_buffer));
m_output->write_end();
m_status = status::closed;
detail::add_end_of_data_to_queue(m_output_queue);
});
}
if (m_write_future.valid()) {
m_write_future.get();
}
}
}; // class Writer
} // namespace io
} // namespace osmium
#endif // OSMIUM_IO_WRITER_HPP
osmium::io::Writer::options_type::sync
fsync sync
Definition: writer.hpp:154
osmium::io::Writer::~Writer
~Writer() noexcept
Definition: writer.hpp:241
util.hpp
osmium::io::Writer::ensure_cleanup
void ensure_cleanup(TFunction func, TArgs &&...args)
Definition: writer.hpp:136
osmium::io::Writer::options_type
Definition: writer.hpp:151
osmium::io::CompressionFactory::instance
static CompressionFactory & instance()
Definition: compression.hpp:151
osmium::io::Writer::do_write
void do_write(osmium::memory::Buffer &&buffer)
Definition: writer.hpp:117
osmium::io::Writer::do_flush
void do_flush()
Definition: writer.hpp:123
std
Definition: reader_iterator.hpp:39
osmium::memory::swap
void swap(Buffer &lhs, Buffer &rhs)
Definition: buffer.hpp:721
osmium::io::Writer::m_thread
osmium::thread::thread_handler m_thread
Definition: writer.hpp:99
osmium::io::Writer::options_type::header
osmium::io::Header header
Definition: writer.hpp:152
osmium::io::Writer::m_buffer
osmium::memory::Buffer m_buffer
Definition: writer.hpp:93
osmium::io::Writer::m_output
std::unique_ptr< osmium::io::detail::OutputFormat > m_output
Definition: writer.hpp:91
osmium::io::Writer::m_status
enum osmium::io::Writer::status m_status
osmium::io::fsync::no
file.hpp
osmium::io::Writer::set_buffer_size
void set_buffer_size(size_t size) noexcept
Definition: writer.hpp:260
osmium::io::File
Definition: file.hpp:74
osmium::memory::Item
Definition: item.hpp:97
osmium::io::Writer::write_thread
static void write_thread(detail::future_string_queue_type &output_queue, std::unique_ptr< osmium::io::Compressor > &&compressor, std::promise< bool > &&write_promise)
Definition: writer.hpp:108
osmium::io::Writer::operator()
void operator()(osmium::memory::Buffer &&buffer)
Definition: writer.hpp:285
osmium
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
osmium::io::Writer::status
status
Definition: writer.hpp:101
detail
Definition: attr.hpp:298
compression.hpp
osmium::io::Writer::status::error
header.hpp
osmium::io::Writer::buffer_size
size_t buffer_size() const noexcept
Definition: writer.hpp:252
osmium::io::Writer::Writer
Writer(const std::string &filename, TArgs &&...args)
Definition: writer.hpp:226
osmium::io::fsync
fsync
Definition: writer_options.hpp:51
osmium::io::Writer::default_buffer_size
static constexpr size_t default_buffer_size
Definition: writer.hpp:85
osmium::io::Writer::set_option
static void set_option(options_type &options, fsync value)
Definition: writer.hpp:165
osmium::io::overwrite::no
osmium::io::Writer::status::closed
osmium::memory::Buffer::push_back
void push_back(const osmium::memory::Item &item)
Definition: buffer.hpp:488
osmium::io_error
Definition: error.hpp:44
osmium::io::Writer::m_buffer_size
size_t m_buffer_size
Definition: writer.hpp:95
osmium::io::Writer::close
void close()
Definition: writer.hpp:323
osmium::io::Writer::flush
void flush()
Definition: writer.hpp:271
osmium::io::Writer::m_file
osmium::io::File m_file
Definition: writer.hpp:87
osmium::io::CompressionFactory::create_compressor
std::unique_ptr< osmium::io::Compressor > create_compressor(osmium::io::file_compression compression, TArgs &&...args)
Definition: compression.hpp:171
osmium::memory::Buffer::committed
size_t committed() const noexcept
Definition: buffer.hpp:241
error.hpp
osmium::memory::Buffer
Definition: buffer.hpp:97
osmium::buffer_is_full
Definition: buffer.hpp:58
osmium::io::File::buffer
const char * buffer() const noexcept
Definition: file.hpp:158
osmium::io::Writer::set_option
static void set_option(options_type &options, overwrite value)
Definition: writer.hpp:161
buffer.hpp
osmium::io::Writer
Definition: writer.hpp:83
osmium::io::Writer::status::okay
osmium::io::Writer::m_write_future
std::future< bool > m_write_future
Definition: writer.hpp:97
osmium::thread::check_for_exception
void check_for_exception(std::future< T > &future)
Definition: util.hpp:53
osmium::io::Writer::Writer
Writer(const osmium::io::File &file, TArgs &&...args)
Definition: writer.hpp:195
osmium::io::Header
Definition: header.hpp:49
osmium::io::Writer::options_type::allow_overwrite
overwrite allow_overwrite
Definition: writer.hpp:153
osmium::io::Writer::set_option
static void set_option(options_type &options, const osmium::io::Header &header)
Definition: writer.hpp:157
osmium::memory::Buffer::auto_grow::no
osmium::io::Writer::m_output_queue
detail::future_string_queue_type m_output_queue
Definition: writer.hpp:89
writer_options.hpp
osmium::io::File::compression
file_compression compression() const noexcept
Definition: file.hpp:291
osmium::io::File::filename
File & filename(const std::string &filename)
Definition: file.hpp:309
osmium::io::Writer::Writer
Writer(const char *filename, TArgs &&...args)
Definition: writer.hpp:231
osmium::io::Writer::operator()
void operator()(const osmium::memory::Item &item)
Definition: writer.hpp:299
osmium::thread::thread_handler
Definition: util.hpp:83
osmium::io::overwrite
overwrite
Definition: writer_options.hpp:43
osmium::io::Writer::operator=
Writer & operator=(const Writer &)=delete