Libosmium  2.14.2
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-2018 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 size_t get_output_queue_size() noexcept {
72  size_t n = osmium::config::get_max_queue_size("OUTPUT", 20);
73  return n > 2 ? n : 2;
74  }
75 
76  } // namespace detail
77 
101  class Writer {
102 
103  static constexpr size_t default_buffer_size = 10 * 1024 * 1024;
104 
106 
107  detail::future_string_queue_type m_output_queue{detail::get_output_queue_size(), "raw_output"};
108 
109  std::unique_ptr<osmium::io::detail::OutputFormat> m_output{nullptr};
110 
112 
114 
115  std::future<bool> m_write_future{};
116 
118 
119  enum class status {
120  okay = 0, // normal writing
121  error = 1, // some error occurred while writing
122  closed = 2 // close() called successfully
124 
125  // This function will run in a separate thread.
126  static void write_thread(detail::future_string_queue_type& output_queue,
127  std::unique_ptr<osmium::io::Compressor>&& compressor,
128  std::promise<bool>&& write_promise) {
129  detail::WriteThread write_thread{output_queue,
130  std::move(compressor),
131  std::move(write_promise)};
132  write_thread();
133  }
134 
136  if (buffer && buffer.committed() > 0) {
137  m_output->write_buffer(std::move(buffer));
138  }
139  }
140 
141  void do_flush() {
143  if (m_buffer && m_buffer.committed() > 0) {
146  using std::swap;
147  swap(m_buffer, buffer);
148 
149  m_output->write_buffer(std::move(buffer));
150  }
151  }
152 
153  template <typename TFunction, typename... TArgs>
154  void ensure_cleanup(TFunction func, TArgs&&... args) {
155  if (m_status != status::okay) {
156  throw io_error("Can not write to writer when in status 'closed' or 'error'");
157  }
158 
159  try {
160  func(std::forward<TArgs>(args)...);
161  } catch (...) {
163  detail::add_to_queue(m_output_queue, std::current_exception());
164  detail::add_end_of_data_to_queue(m_output_queue);
165  throw;
166  }
167  }
168 
169  struct options_type {
174  };
175 
176  static void set_option(options_type& options, osmium::thread::Pool& pool) {
177  options.pool = &pool;
178  }
179 
180  static void set_option(options_type& options, const osmium::io::Header& header) {
181  options.header = header;
182  }
183 
184  static void set_option(options_type& options, overwrite value) {
185  options.allow_overwrite = value;
186  }
187 
188  static void set_option(options_type& options, fsync value) {
189  options.sync = value;
190  }
191 
192  void do_close() {
193  if (m_status == status::okay) {
194  ensure_cleanup([&](){
195  do_write(std::move(m_buffer));
196  m_output->write_end();
198  detail::add_end_of_data_to_queue(m_output_queue);
199  });
200  }
201  }
202 
203  public:
204 
228  template <typename... TArgs>
229  explicit Writer(const osmium::io::File& file, TArgs&&... args) :
230  m_file(file.check()) {
231  assert(!m_file.buffer()); // XXX can't handle pseudo-files
232 
233  options_type options;
234  (void)std::initializer_list<int>{
235  (set_option(options, args), 0)...
236  };
237 
238  if (!options.pool) {
240  }
241 
242  m_output = osmium::io::detail::OutputFormatFactory::instance().create_output(*options.pool, m_file, m_output_queue);
243 
244  if (options.header.get("generator").empty()) {
245  options.header.set("generator", "libosmium/" LIBOSMIUM_VERSION_STRING);
246  }
247 
248  std::unique_ptr<osmium::io::Compressor> compressor =
250  osmium::io::detail::open_for_writing(m_file.filename(), options.allow_overwrite),
251  options.sync);
252 
253  std::promise<bool> write_promise;
254  m_write_future = write_promise.get_future();
255  m_thread = osmium::thread::thread_handler{write_thread, std::ref(m_output_queue), std::move(compressor), std::move(write_promise)};
256 
257  ensure_cleanup([&](){
258  m_output->write_header(options.header);
259  });
260  }
261 
262  template <typename... TArgs>
263  explicit Writer(const std::string& filename, TArgs&&... args) :
264  Writer(osmium::io::File{filename}, std::forward<TArgs>(args)...) {
265  }
266 
267  template <typename... TArgs>
268  explicit Writer(const char* filename, TArgs&&... args) :
269  Writer(osmium::io::File{filename}, std::forward<TArgs>(args)...) {
270  }
271 
272  Writer(const Writer&) = delete;
273  Writer& operator=(const Writer&) = delete;
274 
275  Writer(Writer&&) = delete;
276  Writer& operator=(Writer&&) = delete;
277 
278  ~Writer() noexcept {
279  try {
280  do_close();
281  } catch (...) {
282  // Ignore any exceptions because destructor must not throw.
283  }
284  }
285 
289  size_t buffer_size() const noexcept {
290  return m_buffer_size;
291  }
292 
297  void set_buffer_size(size_t size) noexcept {
298  m_buffer_size = size;
299  }
300 
308  void flush() {
309  ensure_cleanup([&](){
310  do_flush();
311  });
312  }
313 
323  ensure_cleanup([&](){
324  do_flush();
325  do_write(std::move(buffer));
326  });
327  }
328 
336  void operator()(const osmium::memory::Item& item) {
337  ensure_cleanup([&](){
338  if (!m_buffer) {
341  }
342  try {
343  m_buffer.push_back(item);
344  } catch (const osmium::buffer_is_full&) {
345  do_flush();
346  m_buffer.push_back(item);
347  }
348  });
349  }
350 
360  void close() {
361  do_close();
362 
363  if (m_write_future.valid()) {
364  m_write_future.get();
365  }
366  }
367 
368  }; // class Writer
369 
370  } // namespace io
371 
372 } // namespace osmium
373 
374 #endif // OSMIUM_IO_WRITER_HPP
fsync sync
Definition: writer.hpp:172
~Writer() noexcept
Definition: writer.hpp:278
std::size_t get_max_queue_size(const char *queue_name, std::size_t default_value) noexcept
Definition: config.hpp:83
Definition: writer.hpp:169
static Pool & default_instance()
Definition: pool.hpp:174
static CompressionFactory & instance()
Definition: compression.hpp:184
void do_write(osmium::memory::Buffer &&buffer)
Definition: writer.hpp:135
void do_flush()
Definition: writer.hpp:141
void swap(Buffer &lhs, Buffer &rhs)
Definition: buffer.hpp:755
void do_close()
Definition: writer.hpp:192
osmium::thread::thread_handler m_thread
Definition: writer.hpp:117
osmium::io::Header header
Definition: writer.hpp:170
osmium::memory::Buffer m_buffer
Definition: writer.hpp:111
std::unique_ptr< osmium::io::detail::OutputFormat > m_output
Definition: writer.hpp:109
std::size_t committed() const noexcept
Definition: buffer.hpp:261
enum osmium::io::Writer::status m_status
void set_buffer_size(size_t size) noexcept
Definition: writer.hpp:297
Definition: file.hpp:72
Definition: item.hpp:103
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:126
void operator()(osmium::memory::Buffer &&buffer)
Definition: writer.hpp:322
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
status
Definition: writer.hpp:119
#define LIBOSMIUM_VERSION_STRING
Definition: version.hpp:40
Definition: attr.hpp:333
size_t buffer_size() const noexcept
Definition: writer.hpp:289
fsync
Definition: writer_options.hpp:51
static constexpr size_t default_buffer_size
Definition: writer.hpp:103
static void set_option(options_type &options, fsync value)
Definition: writer.hpp:188
void push_back(const osmium::memory::Item &item)
Definition: buffer.hpp:511
Definition: error.hpp:44
size_t m_buffer_size
Definition: writer.hpp:113
Definition: pool.hpp:89
void close()
Definition: writer.hpp:360
void flush()
Definition: writer.hpp:308
osmium::io::File m_file
Definition: writer.hpp:105
static void set_option(options_type &options, osmium::thread::Pool &pool)
Definition: writer.hpp:176
Definition: buffer.hpp:97
Definition: buffer.hpp:58
const char * buffer() const noexcept
Definition: file.hpp:143
static void set_option(options_type &options, overwrite value)
Definition: writer.hpp:184
Definition: writer.hpp:101
Writer(const char *filename, TArgs &&... args)
Definition: writer.hpp:268
std::future< bool > m_write_future
Definition: writer.hpp:115
osmium::thread::Pool * pool
Definition: writer.hpp:173
Writer(const osmium::io::File &file, TArgs &&... args)
Definition: writer.hpp:229
void check_for_exception(std::future< T > &future)
Definition: util.hpp:55
std::unique_ptr< osmium::io::Compressor > create_compressor(osmium::io::file_compression compression, TArgs &&... args) const
Definition: compression.hpp:204
Definition: header.hpp:68
Writer(const std::string &filename, TArgs &&... args)
Definition: writer.hpp:263
overwrite allow_overwrite
Definition: writer.hpp:171
static void set_option(options_type &options, const osmium::io::Header &header)
Definition: writer.hpp:180
detail::future_string_queue_type m_output_queue
Definition: writer.hpp:107
file_compression compression() const noexcept
Definition: file.hpp:291
File & filename(const std::string &filename)
Definition: file.hpp:309
void operator()(const osmium::memory::Item &item)
Definition: writer.hpp:336
void ensure_cleanup(TFunction func, TArgs &&... args)
Definition: writer.hpp:154
Definition: util.hpp:85
overwrite
Definition: writer_options.hpp:43
Writer & operator=(const Writer &)=delete