throw errors after every write stream (#3843)

This commit is contained in:
Kajari Ghosh
2017-03-21 16:43:48 -04:00
committed by Daniel Patterson
parent cc0c28f366
commit 3b0b896501
10 changed files with 289 additions and 3 deletions
+34
View File
@@ -2,6 +2,7 @@
#define OSRM_INDEXED_DATA_HPP
#include "util/exception.hpp"
#include "util/exception_utils.hpp"
#include "util/string_view.hpp"
#include <boost/assert.hpp>
@@ -107,6 +108,11 @@ template <int N, typename T = std::string> struct VariableGroupBlock
out.write((const char *)&refernce, sizeof(refernce));
if (!out)
{
throw util::exception(std::string("Error writing block reference: ") + SOURCE_REF);
}
return prefix_length;
}
@@ -128,6 +134,11 @@ template <int N, typename T = std::string> struct VariableGroupBlock
continue;
out.write((const char *)&data_length, byte_length);
if (!out)
{
throw util::exception(std::string("Error writing block prefix: ") + SOURCE_REF);
}
}
}
@@ -183,6 +194,12 @@ template <int N, typename T = std::string> struct FixedGroupBlock
BlockReference refernce{static_cast<decltype(BlockReference::offset)>(data_offset)};
out.write((const char *)&refernce, sizeof(refernce));
if (!out)
{
throw util::exception(std::string("Error writing group block reference: ") +
SOURCE_REF);
}
return BLOCK_SIZE;
}
@@ -201,6 +218,12 @@ template <int N, typename T = std::string> struct FixedGroupBlock
block_prefix[index++] = static_cast<ValueType>(data_length);
}
out.write((const char *)block_prefix.data(), block_prefix.size());
if (!out)
{
throw util::exception(std::string("Error writing a fixed length block prefix: ") +
SOURCE_REF);
}
}
/// Advances the range to an item stored in the referenced block.
@@ -261,6 +284,11 @@ template <typename GroupBlock> struct IndexedData
: 1 + (std::distance(first, sentinel) - 1) / (BLOCK_SIZE + 1);
out.write((const char *)&number_of_blocks, sizeof(number_of_blocks));
if (!out)
{
throw util::exception(std::string("Error writing indexed data: ") + SOURCE_REF);
}
// Write block references and compute the total data size that includes prefix and data
const GroupBlock block;
DataSizeType data_size = 0;
@@ -275,6 +303,12 @@ template <typename GroupBlock> struct IndexedData
// Write the total data size
out.write((const char *)&data_size, sizeof(data_size));
if (!out)
{
throw util::exception(std::string("Error writing the total indexed data size: ") +
SOURCE_REF);
}
// Write data blocks that are (prefix, data)
for (OffsetIterator curr = first, next = first; next != sentinel; curr = next)
{
+29
View File
@@ -15,6 +15,7 @@
#include <vector>
#include "storage/io.hpp"
#include "util/exception_utils.hpp"
#include "util/fingerprint.hpp"
namespace osrm
@@ -26,6 +27,10 @@ inline bool writeFingerprint(std::ostream &stream)
{
const auto fingerprint = FingerPrint::GetValid();
stream.write(reinterpret_cast<const char *>(&fingerprint), sizeof(fingerprint));
if (!stream)
{
throw util::exception(std::string("Error writing fingerprint: ") + SOURCE_REF);
}
return static_cast<bool>(stream);
}
@@ -36,6 +41,10 @@ bool serializeVector(std::ostream &stream, const std::vector<simple_type> &data)
stream.write(reinterpret_cast<const char *>(&count), sizeof(count));
if (!data.empty())
stream.write(reinterpret_cast<const char *>(&data[0]), sizeof(simple_type) * count);
if (!stream)
{
throw util::exception(std::string("Error serializing vector: ") + SOURCE_REF);
}
return static_cast<bool>(stream);
}
@@ -82,6 +91,11 @@ bool serializeVector(std::ofstream &out_stream, const stxxl::vector<simple_type>
{
const std::uint64_t size = data.size();
out_stream.write(reinterpret_cast<const char *>(&size), sizeof(size));
if (!out_stream)
{
throw util::exception(std::string("Error serializing vector. Writing size step: ") +
SOURCE_REF);
}
simple_type write_buffer[WRITE_BLOCK_BUFFER_SIZE];
std::size_t buffer_len = 0;
@@ -102,6 +116,11 @@ bool serializeVector(std::ofstream &out_stream, const stxxl::vector<simple_type>
if (buffer_len > 0)
out_stream.write(reinterpret_cast<const char *>(write_buffer),
buffer_len * sizeof(simple_type));
if (!out_stream)
{
throw util::exception(std::string("Error serializing vector. Writing data step: ") +
SOURCE_REF);
}
return static_cast<bool>(out_stream);
}
@@ -133,6 +152,11 @@ inline bool serializeFlags(const boost::filesystem::path &path, const std::vecto
std::uint32_t number_of_bits = flags.size();
flag_stream.write(reinterpret_cast<const char *>(&number_of_bits), sizeof(number_of_bits));
if (!flag_stream)
{
throw util::exception(std::string("Error on serializing flags. Write size step: ") +
SOURCE_REF);
}
// putting bits in ints
std::uint32_t chunk = 0;
std::size_t chunk_count = 0;
@@ -147,6 +171,11 @@ inline bool serializeFlags(const boost::filesystem::path &path, const std::vecto
++chunk_count;
flag_stream.write(reinterpret_cast<const char *>(&chunk), sizeof(chunk));
}
if (!flag_stream)
{
throw util::exception(std::string("Error on serializing flags. Write data step: ") +
SOURCE_REF);
}
Log() << "Wrote " << number_of_bits << " bits in " << chunk_count << " chunks (Flags).";
return static_cast<bool>(flag_stream);
}
+23
View File
@@ -2,6 +2,7 @@
#define RANGE_TABLE_HPP
#include "storage/io.hpp"
#include "util/exception_utils.hpp"
#include "util/integer_range.hpp"
#include "util/shared_memory_vector_wrapper.hpp"
@@ -221,12 +222,34 @@ std::ostream &operator<<(std::ostream &out, const RangeTable<BLOCK_SIZE, USE_SHA
// write number of block
const unsigned number_of_blocks = table.diff_blocks.size();
out.write((char *)&number_of_blocks, sizeof(unsigned));
if (!out)
{
throw util::exception(
std::string("Error writing block size of range table to shared memory: ") + SOURCE_REF);
}
// write total length
out.write((char *)&table.sum_lengths, sizeof(unsigned));
if (!out)
{
throw util::exception(
std::string("Error writing total length of range table to shared memory: ") +
SOURCE_REF);
}
// write block offsets
out.write((char *)table.block_offsets.data(), sizeof(unsigned) * table.block_offsets.size());
if (!out)
{
throw util::exception(
std::string("Error writing block offsets of range table to shared memory: ") +
SOURCE_REF);
}
// write blocks
out.write((char *)table.diff_blocks.data(), BLOCK_SIZE * table.diff_blocks.size());
if (!out)
{
throw util::exception(
std::string("Error writing blocks of range table to shared memory: ") + SOURCE_REF);
}
return out;
}
+18
View File
@@ -263,6 +263,12 @@ class StaticRTree
// write leaf_node to leaf node file
leaf_node_file.write((char *)&current_leaf, sizeof(current_leaf));
if (!leaf_node_file)
{
throw util::exception(std::string("Error in static_rtree, writing leaf_node to "
"leaf node file (`.fileIndex`): ") +
SOURCE_REF);
}
}
tree_nodes_in_level.emplace_back(current_node);
@@ -336,7 +342,19 @@ class StaticRTree
std::uint64_t size_of_tree = m_search_tree.size();
BOOST_ASSERT_MSG(0 < size_of_tree, "tree empty");
tree_node_file.write((char *)&size_of_tree, sizeof(size_of_tree));
if (!tree_node_file)
{
throw util::exception(
std::string("Error in static_rtree, writing size of tree in `.fileIndex` file: ") +
SOURCE_REF);
}
tree_node_file.write((char *)&m_search_tree[0], sizeof(TreeNode) * size_of_tree);
if (!tree_node_file)
{
throw util::exception(
std::string("Error in static_rtree, writing tree data in `.fileIndex` file: ") +
SOURCE_REF);
}
MapLeafNodesFile(leaf_node_filename);
}