Skip to content

Commit 8af64ce

Browse files
committed
Introduce special "blackhole" file format.
This can only be used for writing. It will throw away all the data without even encoding it. This is useful in some cases, for instance when you are benchmarking. Unlike writing to /dev/null which will encode the data before throwing it away, the "blackhole" file type doesn't have any overhead.
1 parent 53be6ed commit 8af64ce

File tree

4 files changed

+83
-7
lines changed

4 files changed

+83
-7
lines changed

include/osmium/io/detail/output_format.hpp

+30
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,36 @@ namespace osmium {
197197

198198
}; // class OutputFormatFactory
199199

200+
class BlackholeOutputFormat : public osmium::io::detail::OutputFormat {
201+
202+
public:
203+
204+
BlackholeOutputFormat(osmium::thread::Pool& pool, const osmium::io::File& /*file*/, future_string_queue_type& output_queue) :
205+
OutputFormat(pool, output_queue) {
206+
}
207+
208+
BlackholeOutputFormat(const BlackholeOutputFormat&) = delete;
209+
BlackholeOutputFormat& operator=(const BlackholeOutputFormat&) = delete;
210+
211+
~BlackholeOutputFormat() noexcept final = default;
212+
213+
void write_buffer(osmium::memory::Buffer&& /*buffer*/) final {
214+
}
215+
216+
}; // class BlackholeOutputFormat
217+
218+
// we want the register_output_format() function to run, setting
219+
// the variable is only a side-effect, it will never be used
220+
const bool registered_blackhole_output = osmium::io::detail::OutputFormatFactory::instance().register_output_format(osmium::io::file_format::blackhole,
221+
[](osmium::thread::Pool& pool, const osmium::io::File& file, future_string_queue_type& output_queue) {
222+
return new osmium::io::detail::BlackholeOutputFormat(pool, file, output_queue);
223+
});
224+
225+
// dummy function to silence the unused variable warning from above
226+
inline bool get_registered_blackhole_output() noexcept {
227+
return registered_blackhole_output;
228+
}
229+
200230
} // namespace detail
201231

202232
} // namespace io

include/osmium/io/file.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ namespace osmium {
235235
} else if (suffixes.back() == "debug") {
236236
m_file_format = file_format::debug;
237237
suffixes.pop_back();
238+
} else if (suffixes.back() == "blackhole") {
239+
m_file_format = file_format::blackhole;
240+
suffixes.pop_back();
238241
}
239242

240243
if (suffixes.empty()) {

include/osmium/io/file_format.hpp

+10-7
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@ namespace osmium {
4040
namespace io {
4141

4242
enum class file_format {
43-
unknown = 0,
44-
xml = 1,
45-
pbf = 2,
46-
opl = 3,
47-
json = 4,
48-
o5m = 5,
49-
debug = 6
43+
unknown = 0,
44+
xml = 1,
45+
pbf = 2,
46+
opl = 3,
47+
json = 4,
48+
o5m = 5,
49+
debug = 6,
50+
blackhole = 7
5051
};
5152

5253
enum class read_meta {
@@ -68,6 +69,8 @@ namespace osmium {
6869
return "O5M";
6970
case file_format::debug:
7071
return "DEBUG";
72+
case file_format::blackhole:
73+
return "BLACKHOLE";
7174
default: // file_format::unknown
7275
break;
7376
}

test/t/io/test_file_formats.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,46 @@ TEST_CASE("Override file format by suffix 'osh.opl.gz'") {
189189
f.check();
190190
}
191191

192+
TEST_CASE("File format by suffix 'blackhole'") {
193+
const osmium::io::File f{"test.blackhole"};
194+
REQUIRE(osmium::io::file_format::blackhole == f.format());
195+
REQUIRE(osmium::io::file_compression::none == f.compression());
196+
REQUIRE(false == f.has_multiple_object_versions());
197+
f.check();
198+
}
199+
200+
TEST_CASE("Override file format by suffix 'blackhole'") {
201+
const osmium::io::File f{"test", "blackhole"};
202+
REQUIRE(osmium::io::file_format::blackhole == f.format());
203+
REQUIRE(osmium::io::file_compression::none == f.compression());
204+
REQUIRE(false == f.has_multiple_object_versions());
205+
f.check();
206+
}
207+
208+
TEST_CASE("Override file format by suffix 'osm.blackhole'") {
209+
const osmium::io::File f{"test", "osm.blackhole"};
210+
REQUIRE(osmium::io::file_format::blackhole == f.format());
211+
REQUIRE(osmium::io::file_compression::none == f.compression());
212+
REQUIRE(false == f.has_multiple_object_versions());
213+
f.check();
214+
}
215+
216+
TEST_CASE("Override file format by suffix 'osm.blackhole.bz2'") {
217+
const osmium::io::File f{"test", "osm.blackhole.bz2"};
218+
REQUIRE(osmium::io::file_format::blackhole == f.format());
219+
REQUIRE(osmium::io::file_compression::bzip2 == f.compression());
220+
REQUIRE(false == f.has_multiple_object_versions());
221+
f.check();
222+
}
223+
224+
TEST_CASE("Override file format by suffix 'osh.blackhole.gz'") {
225+
const osmium::io::File f{"test", "osh.blackhole.gz"};
226+
REQUIRE(osmium::io::file_format::blackhole == f.format());
227+
REQUIRE(osmium::io::file_compression::gzip == f.compression());
228+
REQUIRE(true == f.has_multiple_object_versions());
229+
f.check();
230+
}
231+
192232
TEST_CASE("Override file format by suffix 'osh.pbf'") {
193233
const osmium::io::File f{"test", "osh.pbf"};
194234
REQUIRE(osmium::io::file_format::pbf == f.format());

0 commit comments

Comments
 (0)