Libosmium  2.13.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 (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2017 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 
36 #include <cassert>
37 #include <cstddef>
38 #include <exception>
39 #include <functional>
40 #include <future>
41 #include <initializer_list>
42 #include <memory>
43 #include <string>
44 #include <utility>
45 
47 #include <osmium/io/detail/output_format.hpp>
48 #include <osmium/io/detail/queue_util.hpp>
49 #include <osmium/io/detail/read_write.hpp>
50 #include <osmium/io/detail/write_thread.hpp>
51 #include <osmium/io/error.hpp>
52 #include <osmium/io/file.hpp>
53 #include <osmium/io/header.hpp>
55 #include <osmium/memory/buffer.hpp>
56 #include <osmium/thread/pool.hpp>
57 #include <osmium/thread/util.hpp>
58 #include <osmium/util/config.hpp>
59 #include <osmium/version.hpp>
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;
108 
109  std::unique_ptr<osmium::io::detail::OutputFormat> m_output;
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
123  } m_status;
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() {
142  osmium::thread::check_for_exception(m_write_future);
143  if (m_buffer && m_buffer.committed() > 0) {
144  osmium::memory::Buffer buffer{m_buffer_size,
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 (...) {
162  m_status = status::error;
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 {
171  overwrite allow_overwrite = overwrite::no;
172  fsync sync = fsync::no;
173  osmium::thread::Pool* pool = nullptr;
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();
197  m_status = status::closed;
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  m_output_queue(detail::get_output_queue_size(), "raw_output"),
232  m_output(nullptr),
233  m_buffer(),
234  m_buffer_size(default_buffer_size),
235  m_write_future(),
236  m_thread(),
237  m_status(status::okay) {
238  assert(!m_file.buffer()); // XXX can't handle pseudo-files
239 
240  options_type options;
241  (void)std::initializer_list<int>{
242  (set_option(options, args), 0)...
243  };
244 
245  if (!options.pool) {
247  }
248 
249  m_output = osmium::io::detail::OutputFormatFactory::instance().create_output(*options.pool, m_file, m_output_queue);
250 
251  if (options.header.get("generator") == "") {
252  options.header.set("generator", "libosmium/" LIBOSMIUM_VERSION_STRING);
253  }
254 
255  std::unique_ptr<osmium::io::Compressor> compressor =
256  CompressionFactory::instance().create_compressor(file.compression(),
257  osmium::io::detail::open_for_writing(m_file.filename(), options.allow_overwrite),
258  options.sync);
259 
260  std::promise<bool> write_promise;
261  m_write_future = write_promise.get_future();
262  m_thread = osmium::thread::thread_handler{write_thread, std::ref(m_output_queue), std::move(compressor), std::move(write_promise)};
263 
264  ensure_cleanup([&](){
265  m_output->write_header(options.header);
266  });
267  }
268 
269  template <typename... TArgs>
270  explicit Writer(const std::string& filename, TArgs&&... args) :
271  Writer(osmium::io::File{filename}, std::forward<TArgs>(args)...) {
272  }
273 
274  template <typename... TArgs>
275  explicit Writer(const char* filename, TArgs&&... args) :
276  Writer(osmium::io::File{filename}, std::forward<TArgs>(args)...) {
277  }
278 
279  Writer(const Writer&) = delete;
280  Writer& operator=(const Writer&) = delete;
281 
282  Writer(Writer&&) = default;
283  Writer& operator=(Writer&&) = default;
284 
285  ~Writer() noexcept {
286  try {
287  do_close();
288  } catch (...) {
289  // Ignore any exceptions because destructor must not throw.
290  }
291  }
292 
296  size_t buffer_size() const noexcept {
297  return m_buffer_size;
298  }
299 
304  void set_buffer_size(size_t size) noexcept {
305  m_buffer_size = size;
306  }
307 
315  void flush() {
316  ensure_cleanup([&](){
317  do_flush();
318  });
319  }
320 
330  ensure_cleanup([&](){
331  do_flush();
332  do_write(std::move(buffer));
333  });
334  }
335 
343  void operator()(const osmium::memory::Item& item) {
344  ensure_cleanup([&](){
345  if (!m_buffer) {
346  m_buffer = osmium::memory::Buffer{m_buffer_size,
348  }
349  try {
350  m_buffer.push_back(item);
351  } catch (const osmium::buffer_is_full&) {
352  do_flush();
353  m_buffer.push_back(item);
354  }
355  });
356  }
357 
367  void close() {
368  do_close();
369 
370  if (m_write_future.valid()) {
371  m_write_future.get();
372  }
373  }
374 
375  }; // class Writer
376 
377  } // namespace io
378 
379 } // namespace osmium
380 
381 #endif // OSMIUM_IO_WRITER_HPP
fsync sync
Definition: writer.hpp:172
~Writer() noexcept
Definition: writer.hpp:285
std::size_t get_max_queue_size(const char *queue_name, std::size_t default_value) noexcept
Definition: config.hpp:71
Definition: writer.hpp:169
static Pool & default_instance()
Definition: pool.hpp:169
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:762
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:268
void set_buffer_size(size_t size) noexcept
Definition: writer.hpp:304
Definition: file.hpp:72
Definition: item.hpp:105
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:329
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:296
fsync
Definition: writer_options.hpp:51
static void set_option(options_type &options, fsync value)
Definition: writer.hpp:188
void push_back(const osmium::memory::Item &item)
Definition: buffer.hpp:518
Definition: error.hpp:44
size_t m_buffer_size
Definition: writer.hpp:113
Definition: pool.hpp:89
void close()
Definition: writer.hpp:367
void flush()
Definition: writer.hpp:315
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:98
Definition: buffer.hpp:59
const char * buffer() const noexcept
Definition: file.hpp:156
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:275
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
Definition: header.hpp:68
Writer(const std::string &filename, TArgs &&... args)
Definition: writer.hpp:270
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:301
File & filename(const std::string &filename)
Definition: file.hpp:319
void operator()(const osmium::memory::Item &item)
Definition: writer.hpp:343
void ensure_cleanup(TFunction func, TArgs &&... args)
Definition: writer.hpp:154
Definition: util.hpp:85
overwrite
Definition: writer_options.hpp:43