Improved archive header logic for cache files
This commit is contained in:
parent
de3893a08a
commit
977e7db164
3 changed files with 71 additions and 29 deletions
|
|
@ -42,7 +42,8 @@
|
||||||
#include "post.h"
|
#include "post.h"
|
||||||
#include "xact.h"
|
#include "xact.h"
|
||||||
|
|
||||||
#define ARCHIVE_VERSION 0x03000001
|
#define LEDGER_MAGIC 0x4c454447
|
||||||
|
#define ARCHIVE_VERSION 0x03000002
|
||||||
|
|
||||||
//BOOST_IS_ABSTRACT(ledger::scope_t)
|
//BOOST_IS_ABSTRACT(ledger::scope_t)
|
||||||
BOOST_CLASS_EXPORT(ledger::scope_t)
|
BOOST_CLASS_EXPORT(ledger::scope_t)
|
||||||
|
|
@ -63,25 +64,64 @@ template void ledger::journal_t::serialize(boost::archive::binary_iarchive&,
|
||||||
const unsigned int);
|
const unsigned int);
|
||||||
namespace ledger {
|
namespace ledger {
|
||||||
|
|
||||||
void archive_t::read_header()
|
namespace {
|
||||||
|
bool read_header_bits(std::istream& in) {
|
||||||
|
uint32_t bytes;
|
||||||
|
|
||||||
|
assert(sizeof(uint32_t) == 4);
|
||||||
|
in.read(reinterpret_cast<char *>(&bytes), sizeof(uint32_t));
|
||||||
|
if (bytes != LEDGER_MAGIC) {
|
||||||
|
DEBUG("archive.journal", "Magic bytes not present");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
in.read(reinterpret_cast<char *>(&bytes), sizeof(uint32_t));
|
||||||
|
if (bytes != ARCHIVE_VERSION) {
|
||||||
|
DEBUG("archive.journal", "Archive version mismatch");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_header_bits(std::ostream& out) {
|
||||||
|
uint32_t bytes;
|
||||||
|
|
||||||
|
assert(sizeof(uint32_t) == 4);
|
||||||
|
bytes = LEDGER_MAGIC;
|
||||||
|
out.write(reinterpret_cast<char *>(&bytes), sizeof(uint32_t));
|
||||||
|
|
||||||
|
bytes = ARCHIVE_VERSION;
|
||||||
|
out.write(reinterpret_cast<char *>(&bytes), sizeof(uint32_t));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool archive_t::read_header()
|
||||||
{
|
{
|
||||||
if (exists(file)) {
|
uintmax_t size = file_size(file);
|
||||||
// Open the stream, read the version number and the list of sources
|
if (size < 8)
|
||||||
ifstream stream(file, std::ios::binary);
|
return false;
|
||||||
boost::archive::binary_iarchive iarchive(stream);
|
|
||||||
|
|
||||||
DEBUG("archive.journal", "Reading header from archive");
|
// Open the stream, read the version number and the list of sources
|
||||||
iarchive >> *this;
|
ifstream stream(file, std::ios::binary);
|
||||||
|
if (! read_header_bits(stream))
|
||||||
|
return false;
|
||||||
|
|
||||||
DEBUG("archive.journal",
|
boost::archive::binary_iarchive iarchive(stream);
|
||||||
"Version number: " << std::hex << version << std::dec);
|
|
||||||
DEBUG("archive.journal", "Number of sources: " << sources.size());
|
DEBUG("archive.journal", "Reading header from archive");
|
||||||
|
iarchive >> *this;
|
||||||
|
|
||||||
|
DEBUG("archive.journal",
|
||||||
|
"Version number: " << std::hex << ARCHIVE_VERSION << std::dec);
|
||||||
|
DEBUG("archive.journal", "Number of sources: " << sources.size());
|
||||||
|
|
||||||
#if defined(DEBUG_ON)
|
#if defined(DEBUG_ON)
|
||||||
foreach (const journal_t::fileinfo_t& i, sources)
|
foreach (const journal_t::fileinfo_t& i, sources)
|
||||||
DEBUG("archive.journal", "Loaded source: " << *i.filename);
|
DEBUG("archive.journal", "Loaded source: " << *i.filename);
|
||||||
#endif
|
#endif
|
||||||
}
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool archive_t::should_load(const std::list<path>& data_files)
|
bool archive_t::should_load(const std::list<path>& data_files)
|
||||||
|
|
@ -95,8 +135,8 @@ bool archive_t::should_load(const std::list<path>& data_files)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (version != ARCHIVE_VERSION) {
|
if (! read_header()) {
|
||||||
DEBUG("archive.journal", "No, it fails the version check");
|
DEBUG("archive.journal", "No, header failed to read");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -205,10 +245,9 @@ void archive_t::save(shared_ptr<journal_t> journal)
|
||||||
{
|
{
|
||||||
INFO_START(archive, "Saved journal file cache");
|
INFO_START(archive, "Saved journal file cache");
|
||||||
|
|
||||||
ofstream archive(file, std::ios::binary);
|
ofstream stream(file, std::ios::binary);
|
||||||
boost::archive::binary_oarchive oa(archive);
|
|
||||||
|
|
||||||
version = ARCHIVE_VERSION;
|
write_header_bits(stream);
|
||||||
sources = journal->sources;
|
sources = journal->sources;
|
||||||
|
|
||||||
#if defined(DEBUG_ON)
|
#if defined(DEBUG_ON)
|
||||||
|
|
@ -216,8 +255,10 @@ void archive_t::save(shared_ptr<journal_t> journal)
|
||||||
DEBUG("archive.journal", "Saving source: " << *i.filename);
|
DEBUG("archive.journal", "Saving source: " << *i.filename);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
DEBUG("archive.journal",
|
boost::archive::binary_oarchive oa(stream);
|
||||||
"Creating archive with version " << std::hex << version << std::dec);
|
|
||||||
|
DEBUG("archive.journal", "Creating archive with version "
|
||||||
|
<< std::hex << ARCHIVE_VERSION << std::dec);
|
||||||
oa << *this;
|
oa << *this;
|
||||||
|
|
||||||
DEBUG("archive.journal",
|
DEBUG("archive.journal",
|
||||||
|
|
@ -232,6 +273,9 @@ bool archive_t::load(shared_ptr<journal_t> journal)
|
||||||
INFO_START(archive, "Read cached journal file");
|
INFO_START(archive, "Read cached journal file");
|
||||||
|
|
||||||
ifstream stream(file, std::ios::binary);
|
ifstream stream(file, std::ios::binary);
|
||||||
|
if (! read_header_bits(stream))
|
||||||
|
return false;
|
||||||
|
|
||||||
boost::archive::binary_iarchive iarchive(stream);
|
boost::archive::binary_iarchive iarchive(stream);
|
||||||
|
|
||||||
// Skip past the archive header, it was already read in before
|
// Skip past the archive header, it was already read in before
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,6 @@ namespace ledger {
|
||||||
class archive_t
|
class archive_t
|
||||||
{
|
{
|
||||||
path file;
|
path file;
|
||||||
uint32_t version;
|
|
||||||
|
|
||||||
std::list<journal_t::fileinfo_t> sources;
|
std::list<journal_t::fileinfo_t> sources;
|
||||||
|
|
||||||
|
|
@ -66,19 +65,17 @@ public:
|
||||||
archive_t() {
|
archive_t() {
|
||||||
TRACE_CTOR(archive_t, "");
|
TRACE_CTOR(archive_t, "");
|
||||||
}
|
}
|
||||||
archive_t(const path& _file)
|
archive_t(const path& _file) : file(_file) {
|
||||||
: file(_file), version(0) {
|
|
||||||
TRACE_CTOR(archive_t, "const path&");
|
TRACE_CTOR(archive_t, "const path&");
|
||||||
}
|
}
|
||||||
archive_t(const archive_t& ar)
|
archive_t(const archive_t& ar) : file(ar.file) {
|
||||||
: file(ar.file), version(0) {
|
|
||||||
TRACE_CTOR(archive_t, "copy");
|
TRACE_CTOR(archive_t, "copy");
|
||||||
}
|
}
|
||||||
~archive_t() {
|
~archive_t() {
|
||||||
TRACE_DTOR(archive_t);
|
TRACE_DTOR(archive_t);
|
||||||
}
|
}
|
||||||
|
|
||||||
void read_header();
|
bool read_header();
|
||||||
|
|
||||||
bool should_load(const std::list<path>& data_files);
|
bool should_load(const std::list<path>& data_files);
|
||||||
bool should_save(shared_ptr<journal_t> journal);
|
bool should_save(shared_ptr<journal_t> journal);
|
||||||
|
|
@ -94,7 +91,6 @@ private:
|
||||||
|
|
||||||
template<class Archive>
|
template<class Archive>
|
||||||
void serialize(Archive & ar, const unsigned int /* version */) {
|
void serialize(Archive & ar, const unsigned int /* version */) {
|
||||||
ar & version;
|
|
||||||
ar & sources;
|
ar & sources;
|
||||||
}
|
}
|
||||||
#endif // HAVE_BOOST_SERIALIZATION
|
#endif // HAVE_BOOST_SERIALIZATION
|
||||||
|
|
|
||||||
|
|
@ -130,15 +130,17 @@ std::size_t session_t::read_data(const string& master_account)
|
||||||
price_db_path = resolve_path(HANDLER(price_db_).str());
|
price_db_path = resolve_path(HANDLER(price_db_).str());
|
||||||
|
|
||||||
optional<archive_t> cache;
|
optional<archive_t> cache;
|
||||||
|
#if 1
|
||||||
|
// jww (2009-11-01): The binary caching feature is disabled for now.
|
||||||
if (HANDLED(cache_) && master_account.empty()) {
|
if (HANDLED(cache_) && master_account.empty()) {
|
||||||
cache = archive_t(HANDLED(cache_).str());
|
cache = archive_t(HANDLED(cache_).str());
|
||||||
cache->read_header();
|
|
||||||
|
|
||||||
if (price_db_path) {
|
if (price_db_path) {
|
||||||
HANDLER(file_).data_files.push_back(*price_db_path);
|
HANDLER(file_).data_files.push_back(*price_db_path);
|
||||||
populated_price_db = true;
|
populated_price_db = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if (! (cache &&
|
if (! (cache &&
|
||||||
cache->should_load(HANDLER(file_).data_files) &&
|
cache->should_load(HANDLER(file_).data_files) &&
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue