Libosmium  2.16.0
Fast and flexible C++ library for working with OpenStreetMap data
writer.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_IO_WRITER_HPP
2 #define OSMIUM_IO_WRITER_HPP
3 
4 /*
5 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2021 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
37 #include <osmium/io/detail/output_format.hpp>
38 #include <osmium/io/detail/queue_util.hpp>
39 #include <osmium/io/detail/read_write.hpp>
40 #include <osmium/io/detail/write_thread.hpp>
41 #include <osmium/io/error.hpp>
42 #include <osmium/io/file.hpp>
43 #include <osmium/io/header.hpp>
45 #include <osmium/memory/buffer.hpp>
46 #include <osmium/thread/pool.hpp>
47 #include <osmium/thread/util.hpp>
48 #include <osmium/util/config.hpp>
49 #include <osmium/version.hpp>
50 
51 #include <cassert>
52 #include <cstddef>
53 #include <exception>
54 #include <functional>
55 #include <future>
56 #include <initializer_list>
57 #include <memory>
58 #include <string>
59 #include <utility>
60 
61 namespace osmium {
62 
63  namespace memory {
64  class Item;
65  } //namespace memory
66 
67  namespace io {
68 
69  namespace detail {
70 
71  inline std::size_t get_output_queue_size() noexcept {
72  return osmium::config::get_max_queue_size("OUTPUT", 20);
73  }
74 
75  } // namespace detail
76 
100  class Writer {
101 
102  enum {
103  default_buffer_size = 10UL * 1024UL * 1024UL
104  };
105 
107 
108  detail::future_string_queue_type m_output_queue{detail::get_output_queue_size(), "raw_output"};
109 
110  std::unique_ptr<osmium::io::detail::OutputFormat> m_output{nullptr};
111 
113 
115 
116  std::future<std::size_t> m_write_future{};
117 
119 
120  enum class status {
121  okay = 0, // normal writing
122  error = 1, // some error occurred while writing
123  closed = 2 // close() called successfully
125 
126  // This function will run in a separate thread.
127  static void write_thread(detail::future_string_queue_type& output_queue,
128  std::unique_ptr<osmium::io::Compressor>&& compressor,
129  std::promise<std::size_t>&& write_promise) {
130  detail::WriteThread write_thread{output_queue,
131  std::move(compressor),
132  std::move(write_promise)};
133  write_thread();
134  }
135 
137  if (buffer && buffer.committed() > 0) {
138  m_output->write_buffer(std::move(buffer));
139  }
140  }
141 
142  void do_flush() {
144  if (m_buffer && m_buffer.committed() > 0) {
147  using std::swap;
148  swap(m_buffer, buffer);
149 
150  m_output->write_buffer(std::move(buffer));
151  }
152  }
153 
154  template <typename TFunction, typename... TArgs>
155  void ensure_cleanup(TFunction func, TArgs&&... args) {
156  if (m_status != status::okay) {
157  throw io_error("Can not write to writer when in status 'closed' or 'error'");
158  }
159 
160  try {
161  func(std::forward<TArgs>(args)...);
162  } catch (...) {
164  detail::add_to_queue(m_output_queue, std::current_exception());
165  detail::add_end_of_data_to_queue(m_output_queue);
166  throw;
167  }
168  }
169 
170  struct options_type {
175  };
176 
177  static void set_option(options_type& options, osmium::thread::Pool& pool) {
178  options.pool = &pool;
179  }
180 
181  static void set_option(options_type& options, const osmium::io::Header& header) {
182  options.header = header;
183  }
184 
185  static void set_option(options_type& options, overwrite value) {
186  options.allow_overwrite = value;
187  }
188 
189  static void set_option(options_type& options, fsync value) {
190  options.sync = value;
191  }
192 
193  void do_close() {
194  if (m_status == status::okay) {
195  ensure_cleanup([&](){
196  do_write(std::move(m_buffer));
197  m_output->write_end();
199  detail::add_end_of_data_to_queue(m_output_queue);
200  });
201  }
202  }
203 
204  public:
205 
236  template <typename... TArgs>
237  explicit Writer(const osmium::io::File& file, TArgs&&... args) :
238  m_file(file.check()) {
239  assert(!m_file.buffer()); // XXX can't handle pseudo-files
240 
241  options_type options;
242  (void)std::initializer_list<int>{
243  (set_option(options, args), 0)...
244  };
245 
246  if (!options.pool) {
248  }
249 
250  m_output = osmium::io::detail::OutputFormatFactory::instance().create_output(*options.pool, m_file, m_output_queue);
251 
252  if (options.header.get("generator").empty()) {
253  options.header.set("generator", "libosmium/" LIBOSMIUM_VERSION_STRING);
254  }
255 
256  std::unique_ptr<osmium::io::Compressor> compressor =
258  osmium::io::detail::open_for_writing(m_file.filename(), options.allow_overwrite),
259  options.sync);
260 
261  std::promise<std::size_t> write_promise;
262  m_write_future = write_promise.get_future();
263  m_thread = osmium::thread::thread_handler{write_thread, std::ref(m_output_queue), std::move(compressor), std::move(write_promise)};
264 
265  ensure_cleanup([&](){
266  m_output->write_header(options.header);
267  });
268  }
269 
270  template <typename... TArgs>
271  explicit Writer(const std::string& filename, TArgs&&... args) :
272  Writer(osmium::io::File{filename}, std::forward<TArgs>(args)...) {
273  }
274 
275  template <typename... TArgs>
276  explicit Writer(const char* filename, TArgs&&... args) :
277  Writer(osmium::io::File{filename}, std::forward<TArgs>(args)...) {
278  }
279 
280  Writer(const Writer&) = delete;
281  Writer& operator=(const Writer&) = delete;
282 
283  Writer(Writer&&) = delete;
284  Writer& operator=(Writer&&) = delete;
285 
286  ~Writer() noexcept {
287  try {
288  do_close();
289  } catch (...) {
290  // Ignore any exceptions because destructor must not throw.
291  }
292  }
293 
297  size_t buffer_size() const noexcept {
298  return m_buffer_size;
299  }
300 
305  void set_buffer_size(size_t size) noexcept {
306  m_buffer_size = size;
307  }
308 
316  void flush() {
317  ensure_cleanup([&](){
318  do_flush();
319  });
320  }
321 
331  ensure_cleanup([&](){
332  do_flush();
333  do_write(std::move(buffer));
334  });
335  }
336 
344  void operator()(const osmium::memory::Item& item) {
345  ensure_cleanup([&](){
346  if (!m_buffer) {
349  }
350  try {
351  m_buffer.push_back(item);
352  } catch (const osmium::buffer_is_full&) {
353  do_flush();
354  m_buffer.push_back(item);
355  }
356  });
357  }
358 
370  std::size_t close() {
371  do_close();
372 
373  if (m_write_future.valid()) {
374  return m_write_future.get();
375  }
376 
377  return 0;
378  }
379 
380  }; // class Writer
381 
382  } // namespace io
383 
384 } // namespace osmium
385 
386 #endif // OSMIUM_IO_WRITER_HPP
osmium::io::File
Definition: file.hpp:72
osmium::io::Writer::m_buffer_size
size_t m_buffer_size
Definition: writer.hpp:114
osmium::thread::Pool
Definition: pool.hpp:90
osmium::io::overwrite
overwrite
Definition: writer_options.hpp:43
osmium::io::Writer::status::closed
@ closed
osmium::io::Writer::status::okay
@ okay
writer_options.hpp
osmium::io::Writer::buffer_size
size_t buffer_size() const noexcept
Definition: writer.hpp:297
osmium::io::Writer::m_write_future
std::future< std::size_t > m_write_future
Definition: writer.hpp:116
osmium::io::Writer::close
std::size_t close()
Definition: writer.hpp:370
osmium::io::CompressionFactory::instance
static CompressionFactory & instance()
Definition: compression.hpp:184
osmium::io::Writer::flush
void flush()
Definition: writer.hpp:316
osmium::io::Writer::set_option
static void set_option(options_type &options, fsync value)
Definition: writer.hpp:189
osmium::io::Header
Definition: header.hpp:68
config.hpp
osmium::thread::thread_handler
Definition: util.hpp:85
compression.hpp
osmium::io::Writer::set_option
static void set_option(options_type &options, osmium::thread::Pool &pool)
Definition: writer.hpp:177
osmium::io::Writer::set_option
static void set_option(options_type &options, overwrite value)
Definition: writer.hpp:185
osmium::io::Writer::m_file
osmium::io::File m_file
Definition: writer.hpp:106
LIBOSMIUM_VERSION_STRING
#define LIBOSMIUM_VERSION_STRING
Definition: version.hpp:40
osmium::io::Writer::options_type
Definition: writer.hpp:170
osmium::memory::Buffer::committed
std::size_t committed() const noexcept
Definition: buffer.hpp:356
detail
Definition: attr.hpp:342
osmium::buffer_is_full
Definition: buffer.hpp:58
osmium::io::Writer::default_buffer_size
@ default_buffer_size
Definition: writer.hpp:103
version.hpp
osmium::io::Writer::Writer
Writer(const char *filename, TArgs &&... args)
Definition: writer.hpp:276
osmium::io::Writer::options_type::pool
osmium::thread::Pool * pool
Definition: writer.hpp:174
osmium::io::Writer::Writer
Writer(const std::string &filename, TArgs &&... args)
Definition: writer.hpp:271
pool.hpp
osmium::io::Writer::Writer
Writer(const osmium::io::File &file, TArgs &&... args)
Definition: writer.hpp:237
osmium::io::Writer::status::error
@ error
osmium::io::Writer::m_status
enum osmium::io::Writer::status m_status
osmium::io::Writer::set_option
static void set_option(options_type &options, const osmium::io::Header &header)
Definition: writer.hpp:181
osmium::memory::Buffer
Definition: buffer.hpp:97
osmium::config::get_max_queue_size
std::size_t get_max_queue_size(const char *queue_name, const std::size_t default_value) noexcept
Definition: config.hpp:83
osmium::io::Writer::operator()
void operator()(const osmium::memory::Item &item)
Definition: writer.hpp:344
osmium::io::Writer::options_type::allow_overwrite
overwrite allow_overwrite
Definition: writer.hpp:172
osmium::io::Writer::m_output_queue
detail::future_string_queue_type m_output_queue
Definition: writer.hpp:108
osmium::thread::Pool::default_instance
static Pool & default_instance()
Definition: pool.hpp:186
osmium
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
osmium::memory::Item
Definition: item.hpp:105
osmium::io::Writer::options_type::sync
fsync sync
Definition: writer.hpp:173
osmium::io::Writer::ensure_cleanup
void ensure_cleanup(TFunction func, TArgs &&... args)
Definition: writer.hpp:155
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< std::size_t > &&write_promise)
Definition: writer.hpp:127
header.hpp
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:204
osmium::io_error
Definition: error.hpp:44
osmium::io::File::buffer
const char * buffer() const noexcept
Definition: file.hpp:143
osmium::io::fsync::no
@ no
osmium::io::Writer::operator=
Writer & operator=(const Writer &)=delete
osmium::io::Writer::~Writer
~Writer() noexcept
Definition: writer.hpp:286
util.hpp
osmium::io::Writer::do_write
void do_write(osmium::memory::Buffer &&buffer)
Definition: writer.hpp:136
osmium::io::fsync
fsync
Definition: writer_options.hpp:51
osmium::io::overwrite::no
@ no
osmium::io::Writer
Definition: writer.hpp:100
osmium::io::Writer::do_close
void do_close()
Definition: writer.hpp:193
osmium::io::Writer::options_type::header
osmium::io::Header header
Definition: writer.hpp:171
osmium::memory::Buffer::auto_grow::no
@ no
osmium::io::Writer::m_thread
osmium::thread::thread_handler m_thread
Definition: writer.hpp:118
osmium::memory::swap
void swap(Buffer &lhs, Buffer &rhs)
Definition: buffer.hpp:885
osmium::io::Writer::do_flush
void do_flush()
Definition: writer.hpp:142
osmium::io::Writer::m_buffer
osmium::memory::Buffer m_buffer
Definition: writer.hpp:112
osmium::io::Writer::m_output
std::unique_ptr< osmium::io::detail::OutputFormat > m_output
Definition: writer.hpp:110
buffer.hpp
osmium::io::File::filename
File & filename(const std::string &filename)
Definition: file.hpp:309
osmium::io::Writer::set_buffer_size
void set_buffer_size(size_t size) noexcept
Definition: writer.hpp:305
osmium::thread::check_for_exception
void check_for_exception(std::future< T > &future)
Definition: util.hpp:55
osmium::io::File::compression
file_compression compression() const noexcept
Definition: file.hpp:291
osmium::io::Writer::status
status
Definition: writer.hpp:120
file.hpp
osmium::io::Writer::operator()
void operator()(osmium::memory::Buffer &&buffer)
Definition: writer.hpp:330
error.hpp
osmium::memory::Buffer::push_back
void push_back(const osmium::memory::Item &item)
Definition: buffer.hpp:636