# HG changeset patch # User Tom Fredrik Blenning Klaussen # Date 1381413330 -7200 # Node ID 1f9e27a0bd7fefe60264d1ff0e4f10dd91e5de62 # Parent 848496a57039bd2ba22406ae1af512539cf37e9a Allow for lazy calculation of checksums, ignore them, if only one file of given size. diff -r 848496a57039 -r 1f9e27a0bd7f FileDBLink.cpp --- a/FileDBLink.cpp Thu Oct 10 14:14:20 2013 +0200 +++ b/FileDBLink.cpp Thu Oct 10 15:55:30 2013 +0200 @@ -8,28 +8,28 @@ #include -void FileDBLink::updateIfModified(const QString& path) +void FileDBLink::updateIfModified(const QString& path, bool lazy) { QFileInfo fileinfo(path); FileDBLink::DBStatus status = existsWithMtime(path, fileinfo.lastModified()); switch (status) { case FileDBLink::NONE: { - addFile(fileinfo); + addFile(fileinfo, lazy); break; } case FileDBLink::MTIME_DIFFERENT: { - updateFile(fileinfo); + updateFile(fileinfo, lazy); } default: { } } } -void FileDBLink::addFile(const QFileInfo& fileinfo) +void FileDBLink::addFile(const QFileInfo& fileinfo, bool lazy) { addFile(fileinfo.absoluteFilePath(), fileinfo.size(), - fileinfo.lastModified()); + fileinfo.lastModified(), lazy); } QByteArray FileDBLink::computeHash(const QString& path, @@ -56,23 +56,44 @@ return hash.result(); } +bool FileDBLink::updateAllWithSize(qint64 size) +{ + const QList others = filesWithSize(size); + if (!others.empty()) { + foreach( const dbinf_ptr_t other, others) { + if (other->checksum().isEmpty()) { + QByteArray ohash = computeHash(other->path()); + updateFile(other->path(), other->size(), other->mtime(), ohash); + } + } + return true; + } + return false; +} void FileDBLink::addFile(const QString& path, qint64 size, - const QDateTime& lastModified) + const QDateTime& lastModified, bool lazy) { - addFile(path, size, lastModified, computeHash(path)); + QByteArray hash; + // std::cout << path.toStdString() << "::" << lazy << std::endl; + if (!lazy || updateAllWithSize(size)) + hash = computeHash(path); + addFile(path, size, lastModified, hash); } -void FileDBLink::updateFile(const QFileInfo& fileinfo) +void FileDBLink::updateFile(const QFileInfo& fileinfo, bool lazy) { updateFile(fileinfo.absoluteFilePath(), fileinfo.size(), - fileinfo.lastModified()); + fileinfo.lastModified(), lazy); } void FileDBLink::updateFile(const QString& path, qint64 size, - const QDateTime& lastModified) + const QDateTime& lastModified, bool lazy) { - updateFile(path, size, lastModified, computeHash(path)); + QByteArray hash; + if (!lazy || updateAllWithSize(size)) + hash = computeHash(path); + updateFile(path, size, lastModified, hash); } const QList @@ -172,3 +193,30 @@ #endif return list; } + + +const QList +FileDBLink::filesWithSize(qint64 size, const QString& prefix) const +{ + QList retVal; + const QList vals = values(prefix); + foreach (const dbinf_ptr_t val, vals) { + QString path = val->path(); + if (val->size() == size) { + retVal << val; + } + } + return retVal; +} + +FileDBLink::dbinf_ptr_t FileDBLink::value(const QString& path) const +{ + const QList vals = values(); + foreach (const dbinf_ptr_t val, vals) { + QString vpath = val->path(); + if (vpath == path) { + return val; + } + } + return dbinf_ptr_t(); +} diff -r 848496a57039 -r 1f9e27a0bd7f FileDBLink.hpp --- a/FileDBLink.hpp Thu Oct 10 14:14:20 2013 +0200 +++ b/FileDBLink.hpp Thu Oct 10 15:55:30 2013 +0200 @@ -113,7 +113,7 @@ virtual ~FileDBLink() {} - void updateIfModified(const QString& path); + void updateIfModified(const QString& path, bool lazy = false); virtual void addFile(const QString& path, qint64 size, const QDateTime& dtime, const QByteArray& hash) = 0; @@ -121,13 +121,19 @@ const QStringList& files) = 0; virtual void deleteFileFromDB(const QString& path) = 0; - void addFile(const QString& path, qint64 size, const QDateTime& dtime); - void addFile(const QFileInfo& fileinfo); + virtual const QList filesWithSize(qint64 size, const QString& prefix = QString()) const; + + bool updateAllWithSize(qint64 size); + + virtual dbinf_ptr_t value(const QString& path) const; + + void addFile(const QString& path, qint64 size, const QDateTime& dtime, bool lazy = false); + void addFile(const QFileInfo& fileinfo, bool lazy = false); virtual void updateFile(const QString& path, qint64 size, const QDateTime& dtime, const QByteArray& hash) = 0; - void updateFile(const QString& path, qint64 size, const QDateTime& dtime); - void updateFile(const QFileInfo& fileinfo); + void updateFile(const QString& path, qint64 size, const QDateTime& dtime, bool lazy = false); + void updateFile(const QFileInfo& fileinfo, bool lazy = false); virtual bool exists(const QString& path) = 0; virtual DBStatus existsWithMtime(const QString& path, diff -r 848496a57039 -r 1f9e27a0bd7f SqliteDBLink.cpp --- a/SqliteDBLink.cpp Thu Oct 10 14:14:20 2013 +0200 +++ b/SqliteDBLink.cpp Thu Oct 10 15:55:30 2013 +0200 @@ -197,3 +197,11 @@ deleteFileFromDB(path); } } + +const QList +SqliteDBLink::filesWithSize(qint64 size, const QString& prefix) const +{ + //This is incredibly inefficient and should be reimplemented + return FileDBLink::filesWithSize(size, prefix); +} + diff -r 848496a57039 -r 1f9e27a0bd7f SqliteDBLink.hpp --- a/SqliteDBLink.hpp Thu Oct 10 14:14:20 2013 +0200 +++ b/SqliteDBLink.hpp Thu Oct 10 15:55:30 2013 +0200 @@ -16,6 +16,9 @@ bool exists(const QString& path); DBStatus existsWithMtime(const QString& path, const QDateTime& mtime); + virtual const QList filesWithSize(qint64 size, const QString& prefix = QString()) const; + + QStringList toStringList(); const QList values(const QString& prefix = QString() ) const; virtual void keepOnlyFromPrefix(const QString& prefix,