RideFileCache now refreshed in RideItem

.. and fixed sort order problem
This commit is contained in:
Mark Liversedge
2014-12-25 13:59:38 +00:00
parent 3561188711
commit 7ad2bda418
4 changed files with 55 additions and 6 deletions

View File

@@ -33,6 +33,10 @@
#include "JsonRideFile.h" // for DATETIME_FORMAT
// for sorting
bool rideCacheGreaterThan(const RideItem *a, const RideItem *b) { return a->dateTime > b->dateTime; }
bool rideCacheLessThan(const RideItem *a, const RideItem *b) { return a->dateTime < b->dateTime; }
RideCache::RideCache(Context *context) : context(context)
{
progress_ = 100;
@@ -139,7 +143,7 @@ RideCache::addRide(QString name, bool dosignal)
}
if (!added) rides_ << last;
qSort(rides_); // sort by date
qSort(rides_.begin(), rides_.end(), rideCacheLessThan);
// refresh metrics for *this ride only*
last->refresh();
@@ -321,11 +325,6 @@ RideCache::cancel()
future.waitForFinished();
}
}
// for reverse sorting
bool rideCacheGreaterThan(const RideItem *a, const RideItem *b)
{
return a->dateTime > b->dateTime;
}
// check if we need to refresh the metrics then start the thread if needed
void

View File

@@ -134,6 +134,48 @@ RideFileCache::RideFileCache(Context *context, QString fileName, RideFile *passe
}
}
bool
RideFileCache::checkStale(Context *context, RideItem*item)
{
// check if we're stale ?
// Get info for ride file and cache file
QString rideFileName = context->athlete->home->activities().canonicalPath() + "/" + item->fileName;
QFileInfo rideFileInfo(rideFileName);
QString cacheFileName = context->athlete->home->cache().canonicalPath() + "/" + rideFileInfo.baseName() + ".cpx";
QFileInfo cacheFileInfo(cacheFileName);
// is it up-to-date?
if (cacheFileInfo.exists() && cacheFileInfo.size() >= (int)sizeof(struct RideFileCacheHeader)) {
// we have a file, it is more recent than the ride file
// but is it the latest version?
RideFileCacheHeader head;
QFile cacheFile(cacheFileName);
if (cacheFile.open(QIODevice::ReadOnly) == true) {
// read the header
QDataStream inFile(&cacheFile);
inFile.readRawData((char *) &head, sizeof(head));
cacheFile.close();
// its more recent -or- the crc is the same
if (rideFileInfo.lastModified() <= cacheFileInfo.lastModified() ||
head.crc == RideFile::computeFileCRC(rideFileName)) {
// it is the same ?
if (head.version == RideFileCacheVersion) {
// WE'RE GOOD
return false;
}
}
}
}
// its stale !
return true;
}
// returns offset from end of head
static long offsetForMeanMax(RideFileCacheHeader head, RideFile::SeriesType series)
{

View File

@@ -156,6 +156,9 @@ class RideFileCache
// once a cache is loaded we can refresh from in-memory if needed
void refresh(RideFile*ride = NULL);
// are we stale ?
static bool checkStale(Context *context, RideItem*item);
// Just get mean max values for power & wpk for a ride
static QVector<float> meanMaxPowerFor(Context *context, QVector<float>&wpk, QDate from, QDate to);
static QVector<float> meanMaxPowerFor(Context *context, QVector<float>&wpk, QString filename);

View File

@@ -321,6 +321,8 @@ RideItem::checkStale()
}
}
// still reckon its clean? what about the cache ?
if (isstale == false) isstale = RideFileCache::checkStale(context, this);
return isstale;
}
@@ -377,6 +379,9 @@ RideItem::refresh()
dbversion = DBSchemaVersion;
timestamp = QDateTime::currentDateTime().toTime_t();
// RideFile cache needs refreshing possibly ?
RideFileCache updater(context, context->athlete->home->activities().canonicalPath() + "/" + fileName, ride_, true);
// close if we opened it
if (doclose) {
close();