Add buffer reader/writer for per-element serialization
This commit is contained in:
parent
d4300e73f3
commit
4f454a3761
@ -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(
|
||||
"<buffer>", ErrorCode::FileOpenError, SOURCE_REF, std::strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T> void ReadInto(T *dest, const std::size_t count)
|
||||
{
|
||||
#if !defined(__GNUC__) || (__GNUC__ > 4)
|
||||
static_assert(!std::is_pointer<T>::value, "saving pointer types is not allowed");
|
||||
static_assert(std::is_trivially_copyable<T>::value,
|
||||
"bytewise reading requires trivially copyable type");
|
||||
#endif
|
||||
|
||||
if (count == 0)
|
||||
return;
|
||||
|
||||
const auto &result = input_stream.read(reinterpret_cast<char *>(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("<buffer>", ErrorCode::UnexpectedEndOfFile, SOURCE_REF);
|
||||
}
|
||||
throw util::RuntimeError(
|
||||
"<buffer>", ErrorCode::FileReadError, SOURCE_REF, std::strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T> T ReadOne()
|
||||
{
|
||||
T tmp;
|
||||
ReadInto(&tmp, 1);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
std::uint64_t ReadElementCount64() { return ReadOne<std::uint64_t>(); }
|
||||
|
||||
private:
|
||||
std::istringstream input_stream;
|
||||
};
|
||||
|
||||
class BufferWriter
|
||||
{
|
||||
public:
|
||||
BufferWriter() : output_stream(std::ios::binary)
|
||||
{
|
||||
if (!output_stream)
|
||||
{
|
||||
throw util::RuntimeError(
|
||||
"<buffer>", ErrorCode::FileOpenError, SOURCE_REF, std::strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T> void WriteFrom(const T *src, const std::size_t count)
|
||||
{
|
||||
#if !defined(__GNUC__) || (__GNUC__ > 4)
|
||||
static_assert(std::is_trivially_copyable<T>::value,
|
||||
"bytewise writing requires trivially copyable type");
|
||||
#endif
|
||||
|
||||
if (count == 0)
|
||||
return;
|
||||
|
||||
const auto &result =
|
||||
output_stream.write(reinterpret_cast<const char *>(src), count * sizeof(T));
|
||||
|
||||
if (!result)
|
||||
{
|
||||
throw util::RuntimeError(
|
||||
"<buffer>", ErrorCode::FileWriteError, SOURCE_REF, std::strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T> void WriteOne(const T &tmp) { WriteFrom(&tmp, 1); }
|
||||
|
||||
void WriteElementCount64(const std::uint64_t count) { WriteOne<std::uint64_t>(count); }
|
||||
|
||||
std::string GetBuffer() const { return output_stream.str(); }
|
||||
|
||||
private:
|
||||
std::ostringstream output_stream;
|
||||
};
|
||||
} // ns io
|
||||
} // ns storage
|
||||
} // ns osrm
|
||||
|
@ -104,6 +104,22 @@ inline void write(storage::io::FileWriter &writer, const stxxl::vector<T> &vec)
|
||||
}
|
||||
#endif
|
||||
|
||||
template <typename T>
|
||||
void read(io::BufferReader &reader, std::vector<T> &data)
|
||||
{
|
||||
const auto count = reader.ReadElementCount64();
|
||||
data.resize(count);
|
||||
reader.ReadInto(data.data(), count);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void write(io::BufferWriter &writer, const std::vector<T> &data)
|
||||
{
|
||||
const auto count = data.size();
|
||||
writer.WriteElementCount64(count);
|
||||
writer.WriteFrom(data.data(), count);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void read(tar::FileReader &reader, const std::string &name, std::vector<T> &data)
|
||||
{
|
||||
|
@ -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<int> 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
|
||||
|
Loading…
Reference in New Issue
Block a user