Destructor should fail hard if an exception is raised.

This commit is contained in:
Daniel Patterson
2016-11-14 14:34:50 -08:00
parent be496eb4e3
commit 2f9b5788d0
5 changed files with 127 additions and 154 deletions
+39 -66
View File
@@ -24,33 +24,33 @@ namespace storage
namespace io
{
class File
class FileReader
{
private:
std::string filename;
const std::string filename;
boost::filesystem::ifstream input_stream;
public:
File(const std::string &filename, const bool check_fingerprint = false)
: File(boost::filesystem::path(filename), check_fingerprint)
FileReader(const std::string &filename, const bool check_fingerprint = false)
: FileReader(boost::filesystem::path(filename), check_fingerprint)
{
}
File(const boost::filesystem::path &filename_, const bool check_fingerprint = false)
FileReader(const boost::filesystem::path &filename_, const bool check_fingerprint = false)
: filename(filename_.string())
{
filename = filename_.string();
input_stream.open(filename_, std::ios::binary);
if (!input_stream)
throw util::exception("Error opening " + filename + ":" + std::strerror(errno));
if (check_fingerprint && !readAndCheckFingerprint())
if (check_fingerprint && !ReadAndCheckFingerprint())
{
throw util::exception("Fingerprint mismatch in " + filename);
}
}
/* Read count objects of type T into pointer dest */
template <typename T> void readInto(T *dest, const std::size_t count)
template <typename T> void ReadInto(T *dest, const std::size_t count)
{
static_assert(std::is_trivially_copyable<T>::value,
"bytewise reading requires trivially copyable type");
@@ -73,35 +73,35 @@ class File
}
}
template <typename T> void readInto(T &target) { readInto(&target, 1); }
template <typename T> void ReadInto(T &target) { ReadInto(&target, 1); }
template <typename T> T readOne()
template <typename T> T ReadOne()
{
T tmp;
readInto(tmp);
ReadInto(tmp);
return tmp;
}
template <typename T> void skip(const std::size_t element_count)
template <typename T> void Skip(const std::size_t element_count)
{
boost::iostreams::seek(input_stream, element_count * sizeof(T), BOOST_IOS::cur);
}
/*******************************************/
std::uint32_t readElementCount32() { return readOne<std::uint32_t>(); }
std::uint64_t readElementCount64() { return readOne<std::uint64_t>(); }
std::uint32_t ReadElementCount32() { return ReadOne<std::uint32_t>(); }
std::uint64_t ReadElementCount64() { return ReadOne<std::uint64_t>(); }
template <typename T> void deserializeVector(std::vector<T> &data)
template <typename T> void DeserializeVector(std::vector<T> &data)
{
const auto count = readElementCount64();
const auto count = ReadElementCount64();
data.resize(count);
readInto(data.data(), count);
ReadInto(data.data(), count);
}
bool readAndCheckFingerprint()
bool ReadAndCheckFingerprint()
{
auto fingerprint = readOne<util::FingerPrint>();
auto fingerprint = ReadOne<util::FingerPrint>();
const auto valid = util::FingerPrint::GetValid();
// compare the compilation state stored in the fingerprint
return valid.IsMagicNumberOK(fingerprint) && valid.TestContractor(fingerprint) &&
@@ -109,7 +109,7 @@ class File
valid.TestQueryObjects(fingerprint);
}
std::size_t size()
std::size_t Size()
{
auto current_pos = input_stream.tellg();
input_stream.seekg(0, input_stream.end);
@@ -118,7 +118,7 @@ class File
return length;
}
std::vector<std::string> readLines()
std::vector<std::string> ReadLines()
{
std::vector<std::string> result;
std::string thisline;
@@ -140,21 +140,6 @@ class File
}
};
// Reads the count of elements that is written in the file header and returns the number
inline std::uint64_t readElementCount64(boost::filesystem::ifstream &input_stream)
{
std::uint64_t number_of_elements = 0;
input_stream.read(reinterpret_cast<char *>(&number_of_elements), sizeof(number_of_elements));
return number_of_elements;
}
inline std::uint32_t readElementCount32(boost::filesystem::ifstream &input_stream)
{
std::uint32_t number_of_elements = 0;
input_stream.read(reinterpret_cast<char *>(&number_of_elements), sizeof(number_of_elements));
return number_of_elements;
}
// To make function calls consistent, this function returns the fixed number of properties
inline std::size_t readPropertiesCount() { return 1; }
@@ -179,10 +164,10 @@ static_assert(sizeof(HSGRHeader) == 20, "HSGRHeader is not packed");
// Reads the checksum, number of nodes and number of edges written in the header file of a `.hsgr`
// file and returns them in a HSGRHeader struct
inline HSGRHeader readHSGRHeader(io::File &input_file)
inline HSGRHeader readHSGRHeader(io::FileReader &input_file)
{
const util::FingerPrint fingerprint_valid = util::FingerPrint::GetValid();
const auto fingerprint_loaded = input_file.readOne<util::FingerPrint>();
const auto fingerprint_loaded = input_file.ReadOne<util::FingerPrint>();
if (!fingerprint_loaded.TestGraphUtil(fingerprint_valid))
{
util::SimpleLogger().Write(logWARNING) << ".hsgr was prepared with different build.\n"
@@ -190,9 +175,9 @@ inline HSGRHeader readHSGRHeader(io::File &input_file)
}
HSGRHeader header;
input_file.readInto(header.checksum);
input_file.readInto(header.number_of_nodes);
input_file.readInto(header.number_of_edges);
input_file.ReadInto(header.checksum);
input_file.ReadInto(header.number_of_nodes);
input_file.ReadInto(header.number_of_edges);
BOOST_ASSERT_MSG(0 != header.number_of_nodes, "number of nodes is zero");
// number of edges can be zero, this is the case in a few test fixtures
@@ -204,7 +189,7 @@ inline HSGRHeader readHSGRHeader(io::File &input_file)
// Needs to be called after readHSGRHeader() to get the correct offset in the stream
using NodeT = typename util::StaticGraph<contractor::QueryEdge::EdgeData>::NodeArrayEntry;
using EdgeT = typename util::StaticGraph<contractor::QueryEdge::EdgeData>::EdgeArrayEntry;
inline void readHSGR(File &input_file,
inline void readHSGR(io::FileReader &input_file,
NodeT *node_buffer,
const std::uint64_t number_of_nodes,
EdgeT *edge_buffer,
@@ -212,17 +197,17 @@ inline void readHSGR(File &input_file,
{
BOOST_ASSERT(node_buffer);
BOOST_ASSERT(edge_buffer);
input_file.readInto(node_buffer, number_of_nodes);
input_file.readInto(edge_buffer, number_of_edges);
input_file.ReadInto(node_buffer, number_of_nodes);
input_file.ReadInto(edge_buffer, number_of_edges);
}
// Loads properties from a `.properties` file into memory
inline void readProperties(File &properties_file,
inline void readProperties(io::FileReader &properties_file,
extractor::ProfileProperties *properties,
const std::size_t properties_size)
{
BOOST_ASSERT(properties);
properties_file.readInto(properties, properties_size);
properties_file.ReadInto(properties, properties_size);
}
// Reads the timestamp in a `.timestamp` file
@@ -237,18 +222,18 @@ inline void readTimestamp(boost::filesystem::ifstream &timestamp_input_stream,
// Loads datasource_indexes from .datasource_indexes into memory
// Needs to be called after readElementCount() to get the correct offset in the stream
inline void readDatasourceIndexes(File &datasource_indexes_file,
inline void readDatasourceIndexes(io::FileReader &datasource_indexes_file,
uint8_t *datasource_buffer,
const std::uint64_t number_of_datasource_indexes)
{
BOOST_ASSERT(datasource_buffer);
datasource_indexes_file.readInto(datasource_buffer, number_of_datasource_indexes);
datasource_indexes_file.ReadInto(datasource_buffer, number_of_datasource_indexes);
}
// Loads edge data from .edge files into memory which includes its
// geometry, name ID, turn instruction, lane data ID, travel mode, entry class ID
// Needs to be called after readElementCount() to get the correct offset in the stream
inline void readEdges(File &edges_input_file,
inline void readEdges(io::FileReader &edges_input_file,
GeometryID *geometry_list,
NameID *name_id_list,
extractor::guidance::TurnInstruction *turn_instruction_list,
@@ -268,7 +253,7 @@ inline void readEdges(File &edges_input_file,
extractor::OriginalEdgeData current_edge_data;
for (std::uint64_t i = 0; i < number_of_edges; ++i)
{
edges_input_file.readInto(current_edge_data);
edges_input_file.ReadInto(current_edge_data);
geometry_list[i] = current_edge_data.via_geometry;
name_id_list[i] = current_edge_data.name_id;
@@ -284,7 +269,7 @@ inline void readEdges(File &edges_input_file,
// Loads coordinates and OSM node IDs from .nodes files into memory
// Needs to be called after readElementCount() to get the correct offset in the stream
template <typename OSMNodeIDVectorT>
void readNodes(io::File &nodes_file,
void readNodes(io::FileReader &nodes_file,
util::Coordinate *coordinate_list,
OSMNodeIDVectorT &osmnodeid_list,
const std::uint64_t number_of_coordinates)
@@ -293,7 +278,7 @@ void readNodes(io::File &nodes_file,
extractor::QueryNode current_node;
for (std::uint64_t i = 0; i < number_of_coordinates; ++i)
{
nodes_file.readInto(current_node);
nodes_file.ReadInto(current_node);
coordinate_list[i] = util::Coordinate(current_node.lon, current_node.lat);
osmnodeid_list.push_back(current_node.node_id);
BOOST_ASSERT(coordinate_list[i].IsValid());
@@ -308,10 +293,10 @@ struct DatasourceNamesData
std::vector<std::size_t> offsets;
std::vector<std::size_t> lengths;
};
inline DatasourceNamesData readDatasourceNames(io::File &datasource_names_file)
inline DatasourceNamesData readDatasourceNames(io::FileReader &datasource_names_file)
{
DatasourceNamesData datasource_names_data;
std::vector<std::string> lines = datasource_names_file.readLines();
std::vector<std::string> lines = datasource_names_file.ReadLines();
for (const auto &name : lines)
{
datasource_names_data.offsets.push_back(datasource_names_data.names.size());
@@ -322,18 +307,6 @@ inline DatasourceNamesData readDatasourceNames(io::File &datasource_names_file)
}
return datasource_names_data;
}
// Loads ram indexes of R-Trees from `.ramIndex` files into memory
// Needs to be called after readElementCount() to get the correct offset in the stream
// template <bool UseSharedMemory>
// NB Cannot be written without templated type because of cyclic depencies between
// `static_rtree.hpp` and `io.hpp`
template <typename RTreeNodeT>
void readRamIndex(File &ram_index_file, RTreeNodeT *rtree_buffer, const std::uint64_t tree_size)
{
BOOST_ASSERT(rtree_buffer);
ram_index_file.readInto(rtree_buffer, tree_size);
}
}
}
}