Skip to content

Commit 0cf064c

Browse files
committed
Fix file_size()/offset() functions for PBF files
There were broken with the new PBF reader implementation that didn't go through the Decompressor.
1 parent 15d594f commit 0cf064c

4 files changed

Lines changed: 17 additions & 6 deletions

File tree

include/osmium/io/detail/input_format.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ namespace osmium {
6262
future_string_queue_type& input_queue;
6363
future_buffer_queue_type& output_queue;
6464
std::promise<osmium::io::Header>& header_promise;
65+
std::atomic<std::size_t>* offset_ptr;
6566
osmium::osm_entity_bits::type read_which_entities;
6667
osmium::io::read_meta read_metadata;
6768
osmium::io::buffers_type buffers_kind;

include/osmium/io/detail/pbf_input_format.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ namespace osmium {
6666
class PBFParser final : public Parser {
6767

6868
std::string m_input_buffer{};
69-
std::size_t m_offset = 0;
69+
std::atomic<std::size_t>* m_offset_ptr;
7070
int m_fd;
7171
bool m_want_buffered_pages_removed;
7272

@@ -131,13 +131,13 @@ namespace osmium {
131131
to_read -= read_size;
132132
}
133133

134-
m_offset += size;
134+
*m_offset_ptr += size;
135135

136136
if (m_want_buffered_pages_removed && size > 100) {
137137
// The size check is there to avoid the system call
138138
// when we have only been reading a small number of
139139
// bytes, i.e. for a header.
140-
osmium::io::detail::remove_buffered_pages(m_fd, m_offset);
140+
osmium::io::detail::remove_buffered_pages(m_fd, *m_offset_ptr);
141141
}
142142

143143
return true;
@@ -274,6 +274,7 @@ namespace osmium {
274274

275275
explicit PBFParser(parser_arguments& args) :
276276
Parser(args),
277+
m_offset_ptr(args.offset_ptr),
277278
m_fd(args.fd),
278279
m_want_buffered_pages_removed(args.want_buffered_pages_removed) {
279280
}

include/osmium/io/reader.hpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ namespace osmium {
126126
osmium::thread::thread_handler m_thread{};
127127

128128
std::size_t m_file_size = 0;
129+
std::atomic<std::size_t> m_offset{0};
129130

130131
osmium::osm_entity_bits::type m_read_which_entities = osmium::osm_entity_bits::all;
131132
osmium::io::read_meta m_read_metadata = osmium::io::read_meta::yes;
@@ -159,6 +160,7 @@ namespace osmium {
159160
detail::future_string_queue_type& input_queue,
160161
detail::future_buffer_queue_type& osmdata_queue,
161162
std::promise<osmium::io::Header>&& header_promise,
163+
std::atomic<std::size_t>* offset_ptr,
162164
osmium::osm_entity_bits::type read_which_entities,
163165
osmium::io::read_meta read_metadata,
164166
osmium::io::buffers_type buffers_kind,
@@ -170,6 +172,7 @@ namespace osmium {
170172
input_queue,
171173
osmdata_queue,
172174
promise,
175+
offset_ptr,
173176
read_which_entities,
174177
read_metadata,
175178
buffers_kind,
@@ -319,7 +322,7 @@ namespace osmium {
319322
m_read_thread_manager(*m_decompressor, m_input_queue),
320323
m_osmdata_queue(detail::get_osmdata_queue_size(), "parser_results"),
321324
m_osmdata_queue_wrapper(m_osmdata_queue),
322-
m_file_size(m_decompressor->file_size()) {
325+
m_file_size(m_decompressor->is_real() ? m_decompressor->file_size() : osmium::file_size(m_fd)) {
323326

324327
(void)std::initializer_list<int>{
325328
(set_option(args), 0)...
@@ -340,7 +343,7 @@ namespace osmium {
340343
const int fd_for_parser = m_decompressor->is_real() ? -1 : m_fd;
341344
m_thread = osmium::thread::thread_handler{parser_thread, std::ref(*m_pool), fd_for_parser, std::ref(m_creator),
342345
std::ref(m_input_queue), std::ref(m_osmdata_queue),
343-
std::move(header_promise), m_read_which_entities,
346+
std::move(header_promise), &m_offset, m_read_which_entities,
344347
m_read_metadata, m_buffers_kind,
345348
m_decompressor->want_buffered_pages_removed()};
346349
}
@@ -518,7 +521,10 @@ namespace osmium {
518521
* do an expensive system call.
519522
*/
520523
std::size_t offset() const noexcept {
521-
return m_decompressor->offset();
524+
if (m_decompressor->is_real()) {
525+
return m_decompressor->offset();
526+
}
527+
return m_offset;
522528
}
523529

524530
}; // class Reader

test/data-tests/testdata-xml.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,15 @@ static header_buffer_type parse_xml(std::string input) {
8585
osmium::io::detail::add_to_queue(input_queue, std::move(input));
8686
osmium::io::detail::add_to_queue(input_queue, std::string{});
8787

88+
std::atomic<std::size_t> offset{0};
89+
8890
osmium::io::detail::parser_arguments args = {
8991
pool,
9092
-1,
9193
input_queue,
9294
output_queue,
9395
header_promise,
96+
&offset,
9497
osmium::osm_entity_bits::all,
9598
osmium::io::read_meta::yes,
9699
osmium::io::buffers_type::any,

0 commit comments

Comments
 (0)