2016-10-17 19:32:52 -04:00
|
|
|
#ifndef OSRM_STORAGE_IO_HPP_
|
|
|
|
#define OSRM_STORAGE_IO_HPP_
|
|
|
|
|
2016-10-21 18:24:55 -04:00
|
|
|
#include "contractor/query_edge.hpp"
|
|
|
|
#include "extractor/extractor.hpp"
|
2016-10-21 14:25:38 -04:00
|
|
|
#include "extractor/original_edge_data.hpp"
|
|
|
|
#include "extractor/query_node.hpp"
|
2016-10-17 19:32:52 -04:00
|
|
|
#include "util/fingerprint.hpp"
|
|
|
|
#include "util/simple_logger.hpp"
|
2016-10-21 18:24:55 -04:00
|
|
|
#include "util/static_graph.hpp"
|
2016-11-11 08:52:21 -05:00
|
|
|
#include "util/exception.hpp"
|
2016-10-17 19:32:52 -04:00
|
|
|
|
|
|
|
#include <boost/filesystem/fstream.hpp>
|
2016-11-11 08:52:21 -05:00
|
|
|
#include <boost/iostreams/seek.hpp>
|
2016-10-17 19:32:52 -04:00
|
|
|
|
|
|
|
#include <tuple>
|
2016-11-11 08:52:21 -05:00
|
|
|
#include <cstring>
|
|
|
|
#include <cerrno>
|
2016-10-17 19:32:52 -04:00
|
|
|
|
|
|
|
namespace osrm
|
|
|
|
{
|
|
|
|
namespace storage
|
|
|
|
{
|
|
|
|
namespace io
|
|
|
|
{
|
|
|
|
|
2016-11-11 08:52:21 -05:00
|
|
|
class File
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
File(const boost::filesystem::path &filename_, const bool check_fingerprint = false)
|
|
|
|
{
|
|
|
|
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())
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
static_assert(std::is_trivially_copyable<T>::value,
|
|
|
|
"bytewise reading requires trivially copyable type");
|
|
|
|
if (count == 0)
|
|
|
|
return;
|
|
|
|
input_stream.read(reinterpret_cast<char *>(dest), count * sizeof(T));
|
|
|
|
|
|
|
|
// safe to cast here, according to CPP docs, negative values for gcount
|
|
|
|
// are never used.
|
|
|
|
const unsigned long bytes_read = static_cast<unsigned long>(input_stream.gcount());
|
|
|
|
const auto expected_bytes = count * sizeof(T);
|
|
|
|
|
|
|
|
if (bytes_read == 0)
|
|
|
|
{
|
|
|
|
throw util::exception("Error reading from " + filename + ": " + std::strerror(errno));
|
|
|
|
}
|
|
|
|
else if (bytes_read < expected_bytes)
|
|
|
|
{
|
|
|
|
throw util::exception("Error reading from " + filename + ": Unexpected end of file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> void readInto(T &target) { readInto(&target, 1); }
|
|
|
|
|
|
|
|
template <typename T> T readOne()
|
|
|
|
{
|
|
|
|
T tmp;
|
|
|
|
readInto(tmp);
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
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>(); }
|
|
|
|
|
|
|
|
template <typename T> void deserializeVector(std::vector<T> &data)
|
|
|
|
{
|
|
|
|
const auto count = readElementCount64();
|
|
|
|
data.resize(count);
|
|
|
|
readInto(data.data(), count);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool readAndCheckFingerprint()
|
|
|
|
{
|
|
|
|
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) &&
|
|
|
|
valid.TestGraphUtil(fingerprint) && valid.TestRTree(fingerprint) &&
|
|
|
|
valid.TestQueryObjects(fingerprint);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t size()
|
|
|
|
{
|
|
|
|
auto current_pos = input_stream.tellg();
|
|
|
|
input_stream.seekg(0, input_stream.end);
|
|
|
|
auto length = input_stream.tellg();
|
|
|
|
input_stream.seekg(current_pos, input_stream.beg);
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> readLines()
|
|
|
|
{
|
|
|
|
std::vector<std::string> result;
|
|
|
|
std::string thisline;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
while (std::getline(input_stream, thisline))
|
|
|
|
{
|
|
|
|
std::clog << "Read " << thisline << std::endl;
|
|
|
|
result.push_back(thisline);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::ios_base::failure &e)
|
|
|
|
{
|
|
|
|
// EOF is OK here, everything else, re-throw
|
|
|
|
if (!input_stream.eof())
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-21 18:24:55 -04:00
|
|
|
// Reads the count of elements that is written in the file header and returns the number
|
2016-11-11 02:04:32 -05:00
|
|
|
inline std::uint64_t readElementCount64(boost::filesystem::ifstream &input_stream)
|
2016-10-21 18:24:55 -04:00
|
|
|
{
|
|
|
|
std::uint64_t number_of_elements = 0;
|
2016-11-11 02:04:32 -05:00
|
|
|
input_stream.read(reinterpret_cast<char *>(&number_of_elements), sizeof(number_of_elements));
|
|
|
|
return number_of_elements;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::uint64_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));
|
2016-10-21 18:24:55 -04:00
|
|
|
return number_of_elements;
|
|
|
|
}
|
2016-10-20 20:50:05 -04:00
|
|
|
|
2016-10-21 18:24:55 -04:00
|
|
|
// To make function calls consistent, this function returns the fixed number of properties
|
|
|
|
inline std::size_t readPropertiesCount() { return 1; }
|
|
|
|
|
|
|
|
// Returns the number of bytes in a file
|
|
|
|
inline std::size_t readNumberOfBytes(boost::filesystem::ifstream &input_stream)
|
2016-10-20 20:50:05 -04:00
|
|
|
{
|
2016-10-21 18:24:55 -04:00
|
|
|
input_stream.seekg(0, input_stream.end);
|
|
|
|
auto length = input_stream.tellg();
|
|
|
|
input_stream.seekg(0, input_stream.beg);
|
|
|
|
return length;
|
2016-10-20 20:50:05 -04:00
|
|
|
}
|
|
|
|
|
2016-10-17 19:32:52 -04:00
|
|
|
#pragma pack(push, 1)
|
|
|
|
struct HSGRHeader
|
|
|
|
{
|
|
|
|
std::uint32_t checksum;
|
2016-10-21 18:24:55 -04:00
|
|
|
std::uint64_t number_of_nodes;
|
|
|
|
std::uint64_t number_of_edges;
|
2016-10-17 19:32:52 -04:00
|
|
|
};
|
|
|
|
#pragma pack(pop)
|
2016-10-21 18:24:55 -04:00
|
|
|
static_assert(sizeof(HSGRHeader) == 20, "HSGRHeader is not packed");
|
2016-10-17 19:32:52 -04:00
|
|
|
|
2016-10-21 18:24:55 -04:00
|
|
|
// 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
|
2016-11-11 08:52:21 -05:00
|
|
|
inline HSGRHeader readHSGRHeader(io::File &input_file)
|
2016-10-17 19:32:52 -04:00
|
|
|
{
|
|
|
|
const util::FingerPrint fingerprint_valid = util::FingerPrint::GetValid();
|
2016-11-11 08:52:21 -05:00
|
|
|
const auto fingerprint_loaded = input_file.readOne<util::FingerPrint>();
|
2016-10-17 19:32:52 -04:00
|
|
|
if (!fingerprint_loaded.TestGraphUtil(fingerprint_valid))
|
|
|
|
{
|
|
|
|
util::SimpleLogger().Write(logWARNING) << ".hsgr was prepared with different build.\n"
|
|
|
|
"Reprocess to get rid of this warning.";
|
|
|
|
}
|
|
|
|
|
|
|
|
HSGRHeader header;
|
2016-11-11 08:52:21 -05:00
|
|
|
input_file.readInto(header.checksum);
|
|
|
|
input_file.readInto(header.number_of_nodes);
|
|
|
|
input_file.readInto(header.number_of_edges);
|
2016-10-17 19:32:52 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
2016-10-21 18:24:55 -04:00
|
|
|
// Reads the graph data of a `.hsgr` file into memory
|
2016-10-20 20:50:05 -04:00
|
|
|
// Needs to be called after readHSGRHeader() to get the correct offset in the stream
|
2016-10-21 18:24:55 -04:00
|
|
|
using NodeT = typename util::StaticGraph<contractor::QueryEdge::EdgeData>::NodeArrayEntry;
|
|
|
|
using EdgeT = typename util::StaticGraph<contractor::QueryEdge::EdgeData>::EdgeArrayEntry;
|
2016-11-11 08:52:21 -05:00
|
|
|
inline void readHSGR(File &input_file,
|
2016-10-28 17:45:05 -04:00
|
|
|
NodeT *node_buffer,
|
|
|
|
const std::uint64_t number_of_nodes,
|
|
|
|
EdgeT *edge_buffer,
|
|
|
|
const std::uint64_t number_of_edges)
|
2016-10-17 19:32:52 -04:00
|
|
|
{
|
2016-10-21 14:25:38 -04:00
|
|
|
BOOST_ASSERT(node_buffer);
|
|
|
|
BOOST_ASSERT(edge_buffer);
|
2016-11-11 08:52:21 -05:00
|
|
|
input_file.readInto(node_buffer, number_of_nodes);
|
|
|
|
input_file.readInto(edge_buffer, number_of_edges);
|
2016-10-17 19:32:52 -04:00
|
|
|
}
|
2016-10-21 18:24:55 -04:00
|
|
|
|
|
|
|
// Loads properties from a `.properties` file into memory
|
2016-11-11 08:52:21 -05:00
|
|
|
inline void readProperties(File &properties_file,
|
2016-10-21 18:24:55 -04:00
|
|
|
extractor::ProfileProperties *properties,
|
|
|
|
const std::size_t properties_size)
|
2016-10-18 14:21:41 -04:00
|
|
|
{
|
2016-10-21 18:24:55 -04:00
|
|
|
BOOST_ASSERT(properties);
|
2016-11-11 08:52:21 -05:00
|
|
|
properties_file.readInto(properties, properties_size);
|
2016-10-18 14:21:41 -04:00
|
|
|
}
|
|
|
|
|
2016-10-21 18:24:55 -04:00
|
|
|
// Reads the timestamp in a `.timestamp` file
|
|
|
|
// Use readNumberOfBytes() beforehand to get the length of the file
|
2016-10-19 14:55:35 -04:00
|
|
|
inline void readTimestamp(boost::filesystem::ifstream ×tamp_input_stream,
|
2016-10-21 18:24:55 -04:00
|
|
|
char *timestamp,
|
|
|
|
const std::size_t timestamp_length)
|
2016-10-18 14:21:41 -04:00
|
|
|
{
|
2016-10-21 14:25:38 -04:00
|
|
|
BOOST_ASSERT(timestamp);
|
2016-10-18 14:21:41 -04:00
|
|
|
timestamp_input_stream.read(timestamp, timestamp_length * sizeof(char));
|
|
|
|
}
|
|
|
|
|
2016-10-21 18:24:55 -04:00
|
|
|
// Loads datasource_indexes from .datasource_indexes into memory
|
|
|
|
// Needs to be called after readElementCount() to get the correct offset in the stream
|
2016-11-11 08:52:21 -05:00
|
|
|
inline void readDatasourceIndexes(File &datasource_indexes_file,
|
2016-10-21 18:24:55 -04:00
|
|
|
uint8_t *datasource_buffer,
|
|
|
|
const std::uint64_t number_of_datasource_indexes)
|
2016-10-18 15:00:02 -04:00
|
|
|
{
|
2016-10-21 18:24:55 -04:00
|
|
|
BOOST_ASSERT(datasource_buffer);
|
2016-11-11 08:52:21 -05:00
|
|
|
datasource_indexes_file.readInto(datasource_buffer, number_of_datasource_indexes);
|
2016-10-18 15:00:02 -04:00
|
|
|
}
|
|
|
|
|
2016-10-21 18:24:55 -04:00
|
|
|
// Loads edge data from .edge files into memory which includes its
|
2016-10-18 15:00:02 -04:00
|
|
|
// geometry, name ID, turn instruction, lane data ID, travel mode, entry class ID
|
2016-10-21 18:24:55 -04:00
|
|
|
// Needs to be called after readElementCount() to get the correct offset in the stream
|
2016-11-11 08:52:21 -05:00
|
|
|
inline void readEdges(File &edges_input_file,
|
2016-10-21 18:24:55 -04:00
|
|
|
GeometryID *geometry_list,
|
|
|
|
NameID *name_id_list,
|
|
|
|
extractor::guidance::TurnInstruction *turn_instruction_list,
|
|
|
|
LaneDataID *lane_data_id_list,
|
|
|
|
extractor::TravelMode *travel_mode_list,
|
|
|
|
EntryClassID *entry_class_id_list,
|
|
|
|
util::guidance::TurnBearing *pre_turn_bearing_list,
|
|
|
|
util::guidance::TurnBearing *post_turn_bearing_list,
|
|
|
|
const std::uint64_t number_of_edges)
|
2016-10-18 15:00:02 -04:00
|
|
|
{
|
2016-10-21 14:25:38 -04:00
|
|
|
BOOST_ASSERT(geometry_list);
|
|
|
|
BOOST_ASSERT(name_id_list);
|
|
|
|
BOOST_ASSERT(turn_instruction_list);
|
|
|
|
BOOST_ASSERT(lane_data_id_list);
|
|
|
|
BOOST_ASSERT(travel_mode_list);
|
|
|
|
BOOST_ASSERT(entry_class_id_list);
|
2016-10-18 15:00:02 -04:00
|
|
|
extractor::OriginalEdgeData current_edge_data;
|
2016-10-21 18:24:55 -04:00
|
|
|
for (std::uint64_t i = 0; i < number_of_edges; ++i)
|
2016-10-18 15:00:02 -04:00
|
|
|
{
|
2016-11-11 08:52:21 -05:00
|
|
|
edges_input_file.readInto(current_edge_data);
|
2016-10-21 18:24:55 -04:00
|
|
|
|
2016-10-18 15:00:02 -04:00
|
|
|
geometry_list[i] = current_edge_data.via_geometry;
|
|
|
|
name_id_list[i] = current_edge_data.name_id;
|
|
|
|
turn_instruction_list[i] = current_edge_data.turn_instruction;
|
|
|
|
lane_data_id_list[i] = current_edge_data.lane_data_id;
|
|
|
|
travel_mode_list[i] = current_edge_data.travel_mode;
|
|
|
|
entry_class_id_list[i] = current_edge_data.entry_classid;
|
|
|
|
pre_turn_bearing_list[i] = current_edge_data.pre_turn_bearing;
|
|
|
|
post_turn_bearing_list[i] = current_edge_data.post_turn_bearing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-21 18:24:55 -04:00
|
|
|
// 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>
|
2016-11-11 08:52:21 -05:00
|
|
|
void readNodes(io::File &nodes_file,
|
2016-10-21 18:24:55 -04:00
|
|
|
util::Coordinate *coordinate_list,
|
|
|
|
OSMNodeIDVectorT &osmnodeid_list,
|
|
|
|
const std::uint64_t number_of_coordinates)
|
2016-10-18 15:00:02 -04:00
|
|
|
{
|
2016-10-21 14:25:38 -04:00
|
|
|
BOOST_ASSERT(coordinate_list);
|
2016-10-18 15:00:02 -04:00
|
|
|
extractor::QueryNode current_node;
|
2016-10-21 18:24:55 -04:00
|
|
|
for (std::uint64_t i = 0; i < number_of_coordinates; ++i)
|
2016-10-18 15:00:02 -04:00
|
|
|
{
|
2016-11-11 08:52:21 -05:00
|
|
|
nodes_file.readInto(current_node);
|
2016-10-21 18:24:55 -04:00
|
|
|
coordinate_list[i] = util::Coordinate(current_node.lon, current_node.lat);
|
2016-10-18 15:00:02 -04:00
|
|
|
osmnodeid_list.push_back(current_node.node_id);
|
|
|
|
BOOST_ASSERT(coordinate_list[i].IsValid());
|
|
|
|
}
|
|
|
|
}
|
2016-10-19 21:52:57 -04:00
|
|
|
|
|
|
|
// Reads datasource names out of .datasource_names files and metadata such as
|
|
|
|
// the length and offset of each name
|
|
|
|
struct DatasourceNamesData
|
|
|
|
{
|
|
|
|
std::vector<char> names;
|
2016-10-21 18:24:55 -04:00
|
|
|
std::vector<std::size_t> offsets;
|
|
|
|
std::vector<std::size_t> lengths;
|
2016-10-19 21:52:57 -04:00
|
|
|
};
|
2016-11-11 08:52:21 -05:00
|
|
|
inline DatasourceNamesData readDatasourceNames(io::File &datasource_names_file)
|
2016-10-19 21:52:57 -04:00
|
|
|
{
|
|
|
|
DatasourceNamesData datasource_names_data;
|
2016-11-11 08:52:21 -05:00
|
|
|
std::vector<std::string> lines = datasource_names_file.readLines();
|
|
|
|
for (const auto &name : lines)
|
2016-10-19 21:52:57 -04:00
|
|
|
{
|
2016-10-21 18:24:55 -04:00
|
|
|
datasource_names_data.offsets.push_back(datasource_names_data.names.size());
|
|
|
|
datasource_names_data.lengths.push_back(name.size());
|
|
|
|
std::copy(name.c_str(),
|
|
|
|
name.c_str() + name.size(),
|
|
|
|
std::back_inserter(datasource_names_data.names));
|
2016-10-19 21:52:57 -04:00
|
|
|
}
|
|
|
|
return datasource_names_data;
|
|
|
|
}
|
2016-10-21 14:25:38 -04:00
|
|
|
|
2016-10-21 18:24:55 -04:00
|
|
|
// 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`
|
2016-10-21 14:25:38 -04:00
|
|
|
template <typename RTreeNodeT>
|
2016-11-11 08:52:21 -05:00
|
|
|
void readRamIndex(File &ram_index_file, RTreeNodeT *rtree_buffer, const std::uint64_t tree_size)
|
2016-10-21 14:25:38 -04:00
|
|
|
{
|
|
|
|
BOOST_ASSERT(rtree_buffer);
|
2016-11-11 08:52:21 -05:00
|
|
|
ram_index_file.readInto(rtree_buffer, tree_size);
|
2016-10-21 14:25:38 -04:00
|
|
|
}
|
2016-10-17 19:32:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|