Libosmium  2.15.5
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-2020 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 
114  size_t m_buffer_size = default_buffer_size;
115 
116  std::future<bool> 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
124  } m_status = status::okay;
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<bool>&& 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() {
143  osmium::thread::check_for_exception(m_write_future);
144  if (m_buffer && m_buffer.committed() > 0) {
145  osmium::memory::Buffer buffer{m_buffer_size,
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 (...) {
163  m_status = status::error;
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 {
172  overwrite allow_overwrite = overwrite::no;
173  fsync sync = fsync::no;
174  osmium::thread::Pool* pool = nullptr;
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();
198  m_status = status::closed;
199  detail::add_end_of_data_to_queue(m_output_queue);
200  });
201  }
202  }
203 
204  public:
205 
229  template <typename... TArgs>
230  explicit Writer(const osmium::io::File& file, TArgs&&... args) :
231  m_file(file.check()) {
232  assert(!m_file.buffer()); // XXX can't handle pseudo-files
233 
234  options_type options;
235  (void)std::initializer_list<int>{
236  (set_option(options, args), 0)...
237  };
238 
239  if (!options.pool) {
241  }
242 
243  m_output = osmium::io::detail::OutputFormatFactory::instance().create_output(*options.pool, m_file, m_output_queue);
244 
245  if (options.header.get("generator").empty()) {
246  options.header.set("generator", "libosmium/" LIBOSMIUM_VERSION_STRING);
247  }
248 
249  std::unique_ptr<osmium::io::Compressor> compressor =
250  CompressionFactory::instance().create_compressor(file.compression(),
251  osmium::io::detail::open_for_writing(m_file.filename(), options.allow_overwrite),
252  options.sync);
253 
254  std::promise<bool> write_promise;
255  m_write_future = write_promise.get_future();
256  m_thread = osmium::thread::thread_handler{write_thread, std::ref(m_output_queue), std::move(compressor), std::move(write_promise)};
257 
258  ensure_cleanup([&](){
259  m_output->write_header(options.header);
260  });
261  }
262 
263  template <typename... TArgs>
264  explicit Writer(const std::string& filename, TArgs&&... args) :
265  Writer(osmium::io::File{filename}, std::forward<TArgs>(args)...) {
266  }
267 
268  template <typename... TArgs>
269  explicit Writer(const char* filename, TArgs&&... args) :
270  Writer(osmium::io::File{filename}, std::forward<TArgs>(args)...) {
271  }
272 
273  Writer(const Writer&) = delete;
274  Writer& operator=(const Writer&) = delete;
275 
276  Writer(Writer&&) = delete;
277  Writer& operator=(Writer&&) = delete;
278 
279  ~Writer() noexcept {
280  try {
281  do_close();
282  } catch (...) {
283  // Ignore any exceptions because destructor must not throw.
284  }
285  }
286 
290  size_t buffer_size() const noexcept {
291  return m_buffer_size;
292  }
293 
298  void set_buffer_size(size_t size) noexcept {
299  m_buffer_size = size;
300  }
301 
309  void flush() {
310  ensure_cleanup([&](){
311  do_flush();
312  });
313  }
314 
324  ensure_cleanup([&](){
325  do_flush();
326  do_write(std::move(buffer));
327  });
328  }
329 
337  void operator()(const osmium::memory::Item& item) {
338  ensure_cleanup([&](){
339  if (!m_buffer) {
340  m_buffer = osmium::memory::Buffer{m_buffer_size,
342  }
343  try {
344  m_buffer.push_back(item);
345  } catch (const osmium::buffer_is_full&) {
346  do_flush();
347  m_buffer.push_back(item);
348  }
349  });
350  }
351 
361  void close() {
362  do_close();
363 
364  if (m_write_future.valid()) {
365  m_write_future.get();
366  }
367  }
368 
369  }; // class Writer
370 
371  } // namespace io
372 
373 } // namespace osmium
374 
375 #endif // OSMIUM_IO_WRITER_HPP
fsync sync
Definition: writer.hpp:173
~Writer() noexcept
Definition: writer.hpp:279
Definition: writer.hpp:170
static Pool & default_instance()
Definition: pool.hpp:180
void do_write(osmium::memory::Buffer &&buffer)
Definition: writer.hpp:136
void do_flush()
Definition: writer.hpp:142
void swap(Buffer &lhs, Buffer &rhs)
Definition: buffer.hpp:885
void do_close()
Definition: writer.hpp:193
osmium::io::Header header
Definition: writer.hpp:171
void set_buffer_size(size_t size) noexcept
Definition: writer.hpp:298
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:127
void operator()(osmium::memory::Buffer &&buffer)
Definition: writer.hpp:323
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
status
Definition: writer.hpp:120
#define LIBOSMIUM_VERSION_STRING
Definition: version.hpp:40
Definition: attr.hpp:342
size_t buffer_size() const noexcept
Definition: writer.hpp:290
fsync
Definition: writer_options.hpp:51
static void set_option(options_type &options, fsync value)
Definition: writer.hpp:189
Definition: error.hpp:44
Definition: pool.hpp:90
void close()
Definition: writer.hpp:361
void flush()
Definition: writer.hpp:309
osmium::io::File m_file
Definition: writer.hpp:106
std::size_t get_max_queue_size(const char *queue_name, const std::size_t default_value) noexcept
Definition: config.hpp:83
static void set_option(options_type &options, osmium::thread::Pool &pool)
Definition: writer.hpp:177
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:185
Definition: writer.hpp:100
Writer(const char *filename, TArgs &&... args)
Definition: writer.hpp:269
osmium::thread::Pool * pool
Definition: writer.hpp:174
Writer(const osmium::io::File &file, TArgs &&... args)
Definition: writer.hpp:230
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:264
overwrite allow_overwrite
Definition: writer.hpp:172
static void set_option(options_type &options, const osmium::io::Header &header)
Definition: writer.hpp:181
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:337
void ensure_cleanup(TFunction func, TArgs &&... args)
Definition: writer.hpp:155
Definition: util.hpp:85
overwrite
Definition: writer_options.hpp:43