add version header to .icebergs file

This commit is contained in:
John Colvin committed 2020-10-27 17:46:15 -04:00
1 parent c78731e033
commit 4bc5f07815
2 files changed
+35 -3

No files matched your search

@@ -7,6 +7,9 @@
#include <vector>
namespace bts::spartan::mdp3::iceberg_file_cache {
// increment this anytime the deduced icebergs change
// so old .iceberg files will be regenerated
const ushort VERSION = 1;
template <typename K, typename V>
using flat_map = boost::container::flat_map<K, V>;
@@ -41,7 +44,7 @@ flat_map<MDP3OrderKey, DeducedIceberg> find_icebergs(const std::string& pcap) {
d.filled = file::read_from_bin<bool>(in);
return in;
};
if (!bts::spartan::file::read_from_file(ibs_vect, path, read_op)) {
if (!bts::spartan::file::read_from_file(ibs_vect, path, read_op, VERSION)) {
rv = _find_icebergs(pcap);
auto write_op =
[](std::ostream& out,
@@ -53,7 +56,7 @@ flat_map<MDP3OrderKey, DeducedIceberg> find_icebergs(const std::string& pcap) {
file::write_to_bin(out, d.filled);
return out;
};
bts::spartan::file::write_to_file(rv.begin(), rv.end(), path, write_op);
bts::spartan::file::write_to_file(rv.begin(), rv.end(), path, write_op, VERSION);
} else {
LOG(
OrderMan, Info, "{} read {} icebergs from file '{}'", _log_header(),
@@ -30,7 +30,7 @@ inline std::string current_working_dir() {
}
inline bool file_exists(const std::string& name) {
struct stat buffer;
struct stat buffer;
return (stat (name.c_str(), &buffer) == 0);
}
@@ -86,6 +86,20 @@ inline bool read_from_file(std::vector<T>& values, const std::string& path, TOpe
return false;
}
template<class T, class TOper>
inline bool read_from_file(std::vector<T>& values, const std::string& path, TOper op, ushort version) {
std::ifstream istr(path, std::ios::binary);
if (istr) {
if (version != read_from_bin<ushort>(istr))
return false;
T item;
while (op(istr, item))
values.push_back(item);
return true;
}
return false;
}
template<class Iterator, class TOper>
inline void write_to_file(Iterator first, Iterator last, const std::string& path, TOper op) {
std::cout << "opening file '" << path << "'" << std::endl;
@@ -100,6 +114,21 @@ inline void write_to_file(Iterator first, Iterator last, const std::string& path
}
}
template<class Iterator, class TOper>
inline void write_to_file(Iterator first, Iterator last, const std::string& path, TOper op, ushort version) {
std::cout << "opening file '" << path << "'" << std::endl;
std::ofstream o(path, std::ios::binary);
if (!o) {
std::cout << "unable to open file '" << path << "'" << std::endl;
return;
}
write_to_bin(o, version);
while (first != last) {
op(o, *first);
first++;
}
}
template <class Iter>
inline void write_to_file(Iter first, Iter last, const std::string& path) {
std::ofstream o(path, std::ios::binary);