From 4f454a37617e531664ddca042792cd5ed72111b7 Mon Sep 17 00:00:00 2001 From: Patrick Niklaus Date: Mon, 19 Mar 2018 17:38:41 +0000 Subject: [PATCH] Add buffer reader/writer for per-element serialization --- include/storage/io.hpp | 92 +++++++++++++++++++++++++++++++ include/storage/serialization.hpp | 16 ++++++ unit_tests/util/io.cpp | 21 +++++++ 3 files changed, 129 insertions(+) diff --git a/include/storage/io.hpp b/include/storage/io.hpp index 00a3ab2e5..d94ff93be 100644 --- a/include/storage/io.hpp +++ b/include/storage/io.hpp @@ -260,6 +260,98 @@ class FileWriter boost::filesystem::ofstream output_stream; FingerprintFlag fingerprint; }; + +class BufferReader +{ + public: + BufferReader(const std::string &buffer) : input_stream(buffer, std::ios::binary) + { + if (!input_stream) + { + throw util::RuntimeError( + "", ErrorCode::FileOpenError, SOURCE_REF, std::strerror(errno)); + } + } + + template void ReadInto(T *dest, const std::size_t count) + { +#if !defined(__GNUC__) || (__GNUC__ > 4) + static_assert(!std::is_pointer::value, "saving pointer types is not allowed"); + static_assert(std::is_trivially_copyable::value, + "bytewise reading requires trivially copyable type"); +#endif + + if (count == 0) + return; + + const auto &result = input_stream.read(reinterpret_cast(dest), count * sizeof(T)); + const std::size_t bytes_read = input_stream.gcount(); + + if (bytes_read != count * sizeof(T) && !result) + { + if (result.eof()) + { + throw util::RuntimeError("", ErrorCode::UnexpectedEndOfFile, SOURCE_REF); + } + throw util::RuntimeError( + "", ErrorCode::FileReadError, SOURCE_REF, std::strerror(errno)); + } + } + + template T ReadOne() + { + T tmp; + ReadInto(&tmp, 1); + return tmp; + } + + std::uint64_t ReadElementCount64() { return ReadOne(); } + + private: + std::istringstream input_stream; +}; + +class BufferWriter +{ + public: + BufferWriter() : output_stream(std::ios::binary) + { + if (!output_stream) + { + throw util::RuntimeError( + "", ErrorCode::FileOpenError, SOURCE_REF, std::strerror(errno)); + } + } + + template void WriteFrom(const T *src, const std::size_t count) + { +#if !defined(__GNUC__) || (__GNUC__ > 4) + static_assert(std::is_trivially_copyable::value, + "bytewise writing requires trivially copyable type"); +#endif + + if (count == 0) + return; + + const auto &result = + output_stream.write(reinterpret_cast(src), count * sizeof(T)); + + if (!result) + { + throw util::RuntimeError( + "", ErrorCode::FileWriteError, SOURCE_REF, std::strerror(errno)); + } + } + + template void WriteOne(const T &tmp) { WriteFrom(&tmp, 1); } + + void WriteElementCount64(const std::uint64_t count) { WriteOne(count); } + + std::string GetBuffer() const { return output_stream.str(); } + + private: + std::ostringstream output_stream; +}; } // ns io } // ns storage } // ns osrm diff --git a/include/storage/serialization.hpp b/include/storage/serialization.hpp index f3202433f..9b5f2f46f 100644 --- a/include/storage/serialization.hpp +++ b/include/storage/serialization.hpp @@ -104,6 +104,22 @@ inline void write(storage::io::FileWriter &writer, const stxxl::vector &vec) } #endif +template +void read(io::BufferReader &reader, std::vector &data) +{ + const auto count = reader.ReadElementCount64(); + data.resize(count); + reader.ReadInto(data.data(), count); +} + +template +void write(io::BufferWriter &writer, const std::vector &data) +{ + const auto count = data.size(); + writer.WriteElementCount64(count); + writer.WriteFrom(data.data(), count); +} + template void read(tar::FileReader &reader, const std::string &name, std::vector &data) { diff --git a/unit_tests/util/io.cpp b/unit_tests/util/io.cpp index 58a11abaa..f56ee814d 100644 --- a/unit_tests/util/io.cpp +++ b/unit_tests/util/io.cpp @@ -20,6 +20,8 @@ const static std::string IO_INCOMPATIBLE_FINGERPRINT_FILE = "incompatible_fingerprint_file_test_io.tmp"; const static std::string IO_TEXT_FILE = "plain_text_file.tmp"; +using namespace osrm; + BOOST_AUTO_TEST_SUITE(osrm_io) BOOST_AUTO_TEST_CASE(io_data) @@ -41,6 +43,25 @@ BOOST_AUTO_TEST_CASE(io_data) BOOST_CHECK_EQUAL_COLLECTIONS(data_out.begin(), data_out.end(), data_in.begin(), data_in.end()); } +BOOST_AUTO_TEST_CASE(io_buffered_data) +{ + std::vector data_in(53), data_out; + std::iota(begin(data_in), end(data_in), 0); + + std::string result; + { + storage::io::BufferWriter writer; + storage::serialization::write(writer, data_in); + result = writer.GetBuffer(); + } + + storage::io::BufferReader reader(result); + storage::serialization::read(reader, data_out); + + BOOST_REQUIRE_EQUAL(data_in.size(), data_out.size()); + BOOST_CHECK_EQUAL_COLLECTIONS(data_out.begin(), data_out.end(), data_in.begin(), data_in.end()); +} + BOOST_AUTO_TEST_CASE(io_nonexistent_file) { try