2016-10-17 19:32:52 -04:00
|
|
|
#ifndef OSRM_STORAGE_IO_HPP_
|
|
|
|
#define OSRM_STORAGE_IO_HPP_
|
|
|
|
|
2016-11-16 15:07:45 -05:00
|
|
|
#include "util/exception.hpp"
|
2016-12-06 15:30:46 -05:00
|
|
|
#include "util/exception_utils.hpp"
|
2016-10-17 19:32:52 -04:00
|
|
|
#include "util/fingerprint.hpp"
|
2016-12-06 15:30:46 -05:00
|
|
|
#include "util/log.hpp"
|
2017-01-06 16:45:08 -05:00
|
|
|
#include "util/version.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
|
|
|
|
2016-11-16 15:07:45 -05:00
|
|
|
#include <cstring>
|
|
|
|
#include <tuple>
|
|
|
|
#include <type_traits>
|
2016-10-17 19:32:52 -04:00
|
|
|
|
|
|
|
namespace osrm
|
|
|
|
{
|
|
|
|
namespace storage
|
|
|
|
{
|
|
|
|
namespace io
|
|
|
|
{
|
|
|
|
|
2016-11-14 17:34:50 -05:00
|
|
|
class FileReader
|
2016-11-11 08:52:21 -05:00
|
|
|
{
|
|
|
|
private:
|
2016-12-02 13:17:22 -05:00
|
|
|
const boost::filesystem::path filepath;
|
2016-11-11 08:52:21 -05:00
|
|
|
boost::filesystem::ifstream input_stream;
|
|
|
|
|
|
|
|
public:
|
2016-11-30 22:08:01 -05:00
|
|
|
class LineWrapper : public std::string
|
|
|
|
{
|
|
|
|
friend std::istream &operator>>(std::istream &is, LineWrapper &line)
|
|
|
|
{
|
|
|
|
return std::getline(is, line);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
auto GetLineIteratorBegin() { return std::istream_iterator<LineWrapper>(input_stream); }
|
|
|
|
auto GetLineIteratorEnd() { return std::istream_iterator<LineWrapper>(); }
|
|
|
|
|
2016-11-15 17:49:28 -05:00
|
|
|
enum FingerprintFlag
|
|
|
|
{
|
|
|
|
VerifyFingerprint,
|
|
|
|
HasNoFingerprint
|
|
|
|
};
|
2017-02-02 10:55:19 -05:00
|
|
|
|
2016-11-15 17:49:28 -05:00
|
|
|
FileReader(const std::string &filename, const FingerprintFlag flag)
|
|
|
|
: FileReader(boost::filesystem::path(filename), flag)
|
2016-11-11 08:52:21 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-12-02 13:17:22 -05:00
|
|
|
FileReader(const boost::filesystem::path &filepath_, const FingerprintFlag flag)
|
|
|
|
: filepath(filepath_)
|
2016-11-11 08:52:21 -05:00
|
|
|
{
|
2016-12-02 13:17:22 -05:00
|
|
|
input_stream.open(filepath, std::ios::binary);
|
2016-11-11 08:52:21 -05:00
|
|
|
if (!input_stream)
|
2016-12-02 13:17:22 -05:00
|
|
|
throw util::exception("Error opening " + filepath.string());
|
2016-11-11 08:52:21 -05:00
|
|
|
|
2016-11-15 17:49:28 -05:00
|
|
|
if (flag == VerifyFingerprint && !ReadAndCheckFingerprint())
|
2016-11-11 08:52:21 -05:00
|
|
|
{
|
2016-12-06 15:30:46 -05:00
|
|
|
throw util::exception("Fingerprint mismatch in " + filepath_.string() + SOURCE_REF);
|
2016-11-11 08:52:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 09:14:30 -05:00
|
|
|
std::size_t GetSize()
|
|
|
|
{
|
|
|
|
const boost::filesystem::ifstream::pos_type positon = input_stream.tellg();
|
|
|
|
input_stream.seekg(0, std::ios::end);
|
|
|
|
const boost::filesystem::ifstream::pos_type file_size = input_stream.tellg();
|
|
|
|
|
|
|
|
if (file_size == boost::filesystem::ifstream::pos_type(-1))
|
|
|
|
{
|
|
|
|
throw util::exception("File size for " + filepath.string() + " failed " + SOURCE_REF);
|
|
|
|
}
|
|
|
|
|
|
|
|
// restore the current position
|
|
|
|
input_stream.seekg(positon, std::ios::beg);
|
|
|
|
return file_size;
|
|
|
|
}
|
|
|
|
|
2016-11-11 08:52:21 -05:00
|
|
|
/* Read count objects of type T into pointer dest */
|
2016-11-14 17:34:50 -05:00
|
|
|
template <typename T> void ReadInto(T *dest, const std::size_t count)
|
2016-11-11 08:52:21 -05:00
|
|
|
{
|
2016-11-16 15:07:45 -05:00
|
|
|
#if not defined __GNUC__ or __GNUC__ > 4
|
2017-03-07 13:19:54 -05:00
|
|
|
static_assert(!std::is_pointer<T>::value, "saving pointer types is not allowed");
|
2016-11-11 08:52:21 -05:00
|
|
|
static_assert(std::is_trivially_copyable<T>::value,
|
|
|
|
"bytewise reading requires trivially copyable type");
|
2016-11-16 15:07:45 -05:00
|
|
|
#endif
|
|
|
|
|
2016-11-11 08:52:21 -05:00
|
|
|
if (count == 0)
|
|
|
|
return;
|
|
|
|
|
2016-11-14 21:43:30 -05:00
|
|
|
const auto &result = input_stream.read(reinterpret_cast<char *>(dest), count * sizeof(T));
|
2017-01-17 02:58:55 -05:00
|
|
|
const std::size_t bytes_read = input_stream.gcount();
|
2016-11-30 22:08:01 -05:00
|
|
|
|
2017-02-07 09:34:15 -05:00
|
|
|
if (bytes_read != count * sizeof(T) && !result)
|
2016-11-11 08:52:21 -05:00
|
|
|
{
|
2016-11-14 21:43:30 -05:00
|
|
|
if (result.eof())
|
|
|
|
{
|
2016-12-02 13:17:22 -05:00
|
|
|
throw util::exception("Error reading from " + filepath.string() +
|
2016-12-06 15:30:46 -05:00
|
|
|
": Unexpected end of file " + SOURCE_REF);
|
2016-11-14 21:43:30 -05:00
|
|
|
}
|
2016-12-06 15:30:46 -05:00
|
|
|
throw util::exception("Error reading from " + filepath.string() + " " + SOURCE_REF);
|
2016-11-11 08:52:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-30 22:08:01 -05:00
|
|
|
template <typename T> void ReadInto(std::vector<T> &target)
|
|
|
|
{
|
|
|
|
ReadInto(target.data(), target.size());
|
|
|
|
}
|
|
|
|
|
2016-11-14 17:34:50 -05:00
|
|
|
template <typename T> void ReadInto(T &target) { ReadInto(&target, 1); }
|
2016-11-11 08:52:21 -05:00
|
|
|
|
2016-11-14 17:34:50 -05:00
|
|
|
template <typename T> T ReadOne()
|
2016-11-11 08:52:21 -05:00
|
|
|
{
|
|
|
|
T tmp;
|
2016-11-14 17:34:50 -05:00
|
|
|
ReadInto(tmp);
|
2016-11-11 08:52:21 -05:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2016-11-14 17:34:50 -05:00
|
|
|
template <typename T> void Skip(const std::size_t element_count)
|
2016-11-11 08:52:21 -05:00
|
|
|
{
|
|
|
|
boost::iostreams::seek(input_stream, element_count * sizeof(T), BOOST_IOS::cur);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************/
|
|
|
|
|
2016-11-14 17:34:50 -05:00
|
|
|
std::uint32_t ReadElementCount32() { return ReadOne<std::uint32_t>(); }
|
|
|
|
std::uint64_t ReadElementCount64() { return ReadOne<std::uint64_t>(); }
|
2016-11-11 08:52:21 -05:00
|
|
|
|
2016-11-14 17:34:50 -05:00
|
|
|
template <typename T> void DeserializeVector(std::vector<T> &data)
|
2016-11-11 08:52:21 -05:00
|
|
|
{
|
2016-11-14 17:34:50 -05:00
|
|
|
const auto count = ReadElementCount64();
|
2016-11-11 08:52:21 -05:00
|
|
|
data.resize(count);
|
2016-11-14 17:34:50 -05:00
|
|
|
ReadInto(data.data(), count);
|
2016-11-11 08:52:21 -05:00
|
|
|
}
|
|
|
|
|
2017-02-23 10:39:29 -05:00
|
|
|
template <typename T> std::size_t GetVectorMemorySize()
|
|
|
|
{
|
|
|
|
const auto count = ReadElementCount64();
|
|
|
|
Skip<T>(count);
|
|
|
|
return sizeof(count) + sizeof(T) * count;
|
|
|
|
}
|
|
|
|
|
2017-03-01 17:55:18 -05:00
|
|
|
template <typename T> std::size_t ReadVectorSize()
|
|
|
|
{
|
|
|
|
const auto count = ReadElementCount64();
|
|
|
|
Skip<T>(count);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2017-02-23 10:39:29 -05:00
|
|
|
template <typename T> void *DeserializeVector(void *begin, const void *end)
|
|
|
|
{
|
|
|
|
auto count = ReadElementCount64();
|
|
|
|
auto required = reinterpret_cast<char *>(begin) + sizeof(count) + sizeof(T) * count;
|
|
|
|
if (required > end)
|
|
|
|
throw util::exception("Not enough memory ");
|
|
|
|
|
|
|
|
*reinterpret_cast<decltype(count) *>(begin) = count;
|
|
|
|
ReadInto(reinterpret_cast<T *>(reinterpret_cast<char *>(begin) + sizeof(decltype(count))),
|
|
|
|
count);
|
|
|
|
return required;
|
|
|
|
}
|
|
|
|
|
2016-11-14 17:34:50 -05:00
|
|
|
bool ReadAndCheckFingerprint()
|
2016-11-11 08:52:21 -05:00
|
|
|
{
|
2017-01-06 16:45:08 -05:00
|
|
|
auto loaded_fingerprint = ReadOne<util::FingerPrint>();
|
|
|
|
const auto expected_fingerprint = util::FingerPrint::GetValid();
|
|
|
|
|
|
|
|
if (!loaded_fingerprint.IsValid())
|
|
|
|
{
|
|
|
|
util::Log(logERROR) << "Fingerprint magic number or checksum is invalid in "
|
|
|
|
<< filepath.string();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!expected_fingerprint.IsDataCompatible(loaded_fingerprint))
|
|
|
|
{
|
|
|
|
util::Log(logERROR) << filepath.string()
|
|
|
|
<< " is not compatible with this version of OSRM";
|
|
|
|
|
|
|
|
util::Log(logERROR) << "It was prepared with OSRM "
|
|
|
|
<< loaded_fingerprint.GetMajorVersion() << "."
|
|
|
|
<< loaded_fingerprint.GetMinorVersion() << "."
|
|
|
|
<< loaded_fingerprint.GetPatchVersion() << " but you are running "
|
|
|
|
<< OSRM_VERSION;
|
|
|
|
util::Log(logERROR) << "Data is only compatible between minor releases.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2016-11-11 08:52:21 -05:00
|
|
|
}
|
|
|
|
|
2016-11-14 17:34:50 -05:00
|
|
|
std::size_t Size()
|
2016-11-11 08:52:21 -05:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-11-14 17:34:50 -05:00
|
|
|
std::vector<std::string> ReadLines()
|
2016-11-11 08:52:21 -05:00
|
|
|
{
|
|
|
|
std::vector<std::string> result;
|
|
|
|
std::string thisline;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
while (std::getline(input_stream, thisline))
|
|
|
|
{
|
|
|
|
result.push_back(thisline);
|
|
|
|
}
|
|
|
|
}
|
2017-01-06 07:21:54 -05:00
|
|
|
catch (const std::ios_base::failure &)
|
2016-11-11 08:52:21 -05:00
|
|
|
{
|
|
|
|
// EOF is OK here, everything else, re-throw
|
|
|
|
if (!input_stream.eof())
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2016-10-18 15:00:02 -04:00
|
|
|
|
2016-11-30 22:08:01 -05:00
|
|
|
std::string ReadLine()
|
2016-10-18 15:00:02 -04:00
|
|
|
{
|
2016-11-30 22:08:01 -05:00
|
|
|
std::string thisline;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::getline(input_stream, thisline);
|
|
|
|
}
|
2016-12-08 05:12:08 -05:00
|
|
|
catch (const std::ios_base::failure & /*e*/)
|
2016-11-30 22:08:01 -05:00
|
|
|
{
|
|
|
|
// EOF is OK here, everything else, re-throw
|
|
|
|
if (!input_stream.eof())
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
return thisline;
|
2016-10-18 15:00:02 -04:00
|
|
|
}
|
2016-10-19 21:52:57 -04:00
|
|
|
};
|
2016-12-02 13:17:22 -05:00
|
|
|
|
|
|
|
class FileWriter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum FingerprintFlag
|
|
|
|
{
|
|
|
|
GenerateFingerprint,
|
|
|
|
HasNoFingerprint
|
|
|
|
};
|
|
|
|
|
|
|
|
FileWriter(const std::string &filename, const FingerprintFlag flag)
|
|
|
|
: FileWriter(boost::filesystem::path(filename), flag)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FileWriter(const boost::filesystem::path &filepath_, const FingerprintFlag flag)
|
2017-02-02 10:55:19 -05:00
|
|
|
: filepath(filepath_), fingerprint(flag)
|
2016-12-02 13:17:22 -05:00
|
|
|
{
|
|
|
|
output_stream.open(filepath, std::ios::binary);
|
|
|
|
if (!output_stream)
|
|
|
|
throw util::exception("Error opening " + filepath.string());
|
|
|
|
|
|
|
|
if (flag == GenerateFingerprint)
|
|
|
|
{
|
|
|
|
WriteFingerprint();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write count objects of type T from pointer src to output stream */
|
2017-01-31 13:18:26 -05:00
|
|
|
template <typename T> void WriteFrom(const T *src, const std::size_t count)
|
2016-12-02 13:17:22 -05:00
|
|
|
{
|
|
|
|
#if not defined __GNUC__ or __GNUC__ > 4
|
|
|
|
static_assert(std::is_trivially_copyable<T>::value,
|
|
|
|
"bytewise writing requires trivially copyable type");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (count == 0)
|
2017-01-31 13:18:26 -05:00
|
|
|
return;
|
2016-12-02 13:17:22 -05:00
|
|
|
|
2017-01-05 18:10:47 -05:00
|
|
|
const auto &result =
|
|
|
|
output_stream.write(reinterpret_cast<const char *>(src), count * sizeof(T));
|
2017-01-31 13:18:26 -05:00
|
|
|
|
2016-12-02 13:17:22 -05:00
|
|
|
if (!result)
|
|
|
|
{
|
|
|
|
throw util::exception("Error writing to " + filepath.string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-04 05:52:40 -05:00
|
|
|
template <typename T> void WriteFrom(const T &src) { WriteFrom(&src, 1); }
|
2016-12-02 13:17:22 -05:00
|
|
|
|
2017-01-31 13:18:26 -05:00
|
|
|
template <typename T> void WriteOne(const T tmp) { WriteFrom(tmp); }
|
2016-12-02 13:17:22 -05:00
|
|
|
|
2017-01-31 13:18:26 -05:00
|
|
|
void WriteElementCount32(const std::uint32_t count) { WriteOne<std::uint32_t>(count); }
|
|
|
|
void WriteElementCount64(const std::uint64_t count) { WriteOne<std::uint64_t>(count); }
|
2016-12-02 13:17:22 -05:00
|
|
|
|
2017-01-31 13:18:26 -05:00
|
|
|
template <typename T> void SerializeVector(const std::vector<T> &data)
|
2016-12-02 13:17:22 -05:00
|
|
|
{
|
|
|
|
const auto count = data.size();
|
|
|
|
WriteElementCount64(count);
|
|
|
|
return WriteFrom(data.data(), count);
|
|
|
|
}
|
|
|
|
|
2017-01-31 13:18:26 -05:00
|
|
|
void WriteFingerprint()
|
2016-12-02 13:17:22 -05:00
|
|
|
{
|
|
|
|
const auto fingerprint = util::FingerPrint::GetValid();
|
|
|
|
return WriteOne(fingerprint);
|
|
|
|
}
|
2017-02-02 10:55:19 -05:00
|
|
|
|
|
|
|
template <typename T> void Skip(const std::size_t element_count)
|
|
|
|
{
|
|
|
|
boost::iostreams::seek(output_stream, element_count * sizeof(T), BOOST_IOS::cur);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkipToBeginning()
|
|
|
|
{
|
|
|
|
boost::iostreams::seek(output_stream, 0, std::ios::beg);
|
|
|
|
|
|
|
|
// If we wrote a Fingerprint, skip over it
|
|
|
|
if (fingerprint == FingerprintFlag::GenerateFingerprint)
|
|
|
|
Skip<util::FingerPrint>(1);
|
|
|
|
|
|
|
|
// Should probably return a functor for jumping back to the current pos.
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const boost::filesystem::path filepath;
|
|
|
|
boost::filesystem::ofstream output_stream;
|
|
|
|
FingerprintFlag fingerprint;
|
2016-12-02 13:17:22 -05:00
|
|
|
};
|
2017-02-02 10:55:19 -05:00
|
|
|
} // ns io
|
|
|
|
} // ns storage
|
|
|
|
} // ns osrm
|
2016-10-17 19:32:52 -04:00
|
|
|
|
|
|
|
#endif
|