Refactor file loading to use a common class that has proper error handling.

This commit is contained in:
Daniel Patterson
2016-11-11 05:52:21 -08:00
parent e226b52f21
commit 4ad6d88888
6 changed files with 288 additions and 336 deletions
+23 -1
View File
@@ -17,6 +17,7 @@ namespace util
namespace guidance
{
class LaneTuple;
class LaneTupleIdPair;
} // namespace guidance
} // namespace util
} // namespace osrm
@@ -27,6 +28,11 @@ template <> struct hash<::osrm::util::guidance::LaneTuple>
{
inline std::size_t operator()(const ::osrm::util::guidance::LaneTuple &bearing_class) const;
};
template <> struct hash<::osrm::util::guidance::LaneTupleIdPair>
{
inline std::size_t
operator()(const ::osrm::util::guidance::LaneTupleIdPair &bearing_class) const;
};
} // namespace std
namespace osrm
@@ -73,7 +79,23 @@ class LaneTuple
}
};
using LaneTupleIdPair = std::pair<util::guidance::LaneTuple, LaneDescriptionID>;
class LaneTupleIdPair
{
public:
util::guidance::LaneTuple first;
LaneDescriptionID second;
bool operator==(const LaneTupleIdPair &other) const;
friend std::size_t hash_value(const LaneTupleIdPair &pair)
{
std::size_t seed{0};
boost::hash_combine(seed, pair.first);
boost::hash_combine(seed, pair.second);
return seed;
}
};
} // namespace guidance
} // namespace util
} // namespace osrm
+9 -26
View File
@@ -15,6 +15,7 @@
#include <vector>
#include "util/fingerprint.hpp"
#include "storage/io.hpp"
namespace osrm
{
@@ -28,17 +29,6 @@ inline bool writeFingerprint(std::ostream &stream)
return static_cast<bool>(stream);
}
inline bool readAndCheckFingerprint(std::istream &stream)
{
FingerPrint fingerprint;
const auto valid = FingerPrint::GetValid();
stream.read(reinterpret_cast<char *>(&fingerprint), sizeof(fingerprint));
// compare the compilation state stored in the fingerprint
return static_cast<bool>(stream) && valid.IsMagicNumberOK(fingerprint) &&
valid.TestContractor(fingerprint) && valid.TestGraphUtil(fingerprint) &&
valid.TestRTree(fingerprint) && valid.TestQueryObjects(fingerprint);
}
template <typename simple_type>
bool serializeVector(const std::string &filename, const std::vector<simple_type> &data)
{
@@ -66,17 +56,14 @@ bool serializeVector(std::ostream &stream, const std::vector<simple_type> &data)
template <typename simple_type>
bool deserializeVector(const std::string &filename, std::vector<simple_type> &data)
{
std::ifstream stream(filename, std::ios::binary);
if (!readAndCheckFingerprint(stream))
return false;
storage::io::File file(filename, true);
std::uint64_t count = 0;
stream.read(reinterpret_cast<char *>(&count), sizeof(count));
const auto count = file.readElementCount64();
data.resize(count);
if (count)
stream.read(reinterpret_cast<char *>(&data[0]), sizeof(simple_type) * count);
return static_cast<bool>(stream);
file.readInto(data.data(), count);
return true;
}
template <typename simple_type>
@@ -199,13 +186,9 @@ inline bool serializeFlags(const boost::filesystem::path &path, const std::vecto
inline bool deserializeFlags(const boost::filesystem::path &path, std::vector<bool> &flags)
{
SimpleLogger().Write() << "Reading flags from " << path;
std::ifstream flag_stream(path.string(), std::ios::binary);
storage::io::File flag_file(path, true);
if (!readAndCheckFingerprint(flag_stream))
return false;
std::uint32_t number_of_bits;
flag_stream.read(reinterpret_cast<char *>(&number_of_bits), sizeof(number_of_bits));
const auto number_of_bits = flag_file.readOne<std::uint32_t>();
flags.resize(number_of_bits);
// putting bits in ints
std::uint32_t chunks = (number_of_bits + 31) / 32;
@@ -213,14 +196,14 @@ inline bool deserializeFlags(const boost::filesystem::path &path, std::vector<bo
std::uint32_t chunk;
for (std::size_t chunk_id = 0; chunk_id < chunks; ++chunk_id)
{
flag_stream.read(reinterpret_cast<char *>(&chunk), sizeof(chunk));
flag_file.readInto(chunk);
std::bitset<32> chunk_bits(chunk);
for (std::size_t bit = 0; bit < 32 && bit_position < number_of_bits; ++bit, ++bit_position)
flags[bit_position] = chunk_bits[bit];
}
SimpleLogger().Write() << "Read " << number_of_bits << " bits in " << chunks
<< " Chunks from disk.";
return static_cast<bool>(flag_stream);
return true;
}
} // namespace util
} // namespace osrm
+2 -11
View File
@@ -346,18 +346,9 @@ class StaticRTree
const CoordinateListT &coordinate_list)
: m_coordinate_list(coordinate_list)
{
// open tree node file and load into RAM.
if (!boost::filesystem::exists(node_file))
{
throw exception("ram index file does not exist");
}
if (boost::filesystem::file_size(node_file) == 0)
{
throw exception("ram index file is empty");
}
boost::filesystem::ifstream tree_node_file(node_file, std::ios::binary);
storage::io::File tree_node_file(node_file);
const auto tree_size = storage::io::readElementCount64(tree_node_file);
const auto tree_size = tree_node_file.readElementCount64();
m_search_tree.resize(tree_size);
storage::io::readRamIndex(tree_node_file, &m_search_tree[0], tree_size);