Mercurial > dedupe
changeset 93:308a718812ba
Small refactoring to allow lazy commits.
| author | Tom Fredrik Blenning Klaussen <bfg@bfgconsult.no> |
|---|---|
| date | Tue, 22 Oct 2013 11:53:54 +0200 |
| parents | f49023c61dac |
| children | 93981e675d67 |
| files | CMakeLists.txt DataController.cpp DataController.hpp FileDBLink.cpp FileDBLink.hpp SqliteDBLink.cpp SqliteDBLink.hpp |
| diffstat | 7 files changed, 80 insertions(+), 26 deletions(-) [+] |
line wrap: on
line diff
--- a/CMakeLists.txt Mon Oct 21 20:03:39 2013 +0200 +++ b/CMakeLists.txt Tue Oct 22 11:53:54 2013 +0200 @@ -105,6 +105,7 @@ SET(MOC_HEADERS DataController.hpp + FileDBLink.hpp ) # Returns the moc_xxx.cpp files in the foo_MOC_SRCS variable
--- a/DataController.cpp Mon Oct 21 20:03:39 2013 +0200 +++ b/DataController.cpp Tue Oct 22 11:53:54 2013 +0200 @@ -99,11 +99,12 @@ } int n = 0; - emit populateProgress(n); + int max = paths.size(); + emit populateProgress(n, max); foreach(QString filename, paths) { try { - dblink.updateIfModified(filename); + dblink.updateIfModified(filename, true); } catch (const PermissionException& e) { dblink.deleteFileFromDB(filename); @@ -117,7 +118,7 @@ e.raise(); } - emit populateProgress(++n); + emit populateProgress(++n, max); QDateTime now = QDateTime::currentDateTime(); if (last.msecsTo(now) > 1000) { @@ -125,6 +126,7 @@ last = now; } } + dblink.commit(); } @@ -186,6 +188,8 @@ QMultiMap<QDateTime, QSharedPointer<FileDBLink::DBInfo> > mtimeLUP; QMultiMap<QByteArray, QSharedPointer<FileDBLink::DBInfo> > checksumLUP; + int max = elems.size() - 1; + foreach(QSharedPointer<FileDBLink::DBInfo> line, elems) { if (showNameDups) { nameLUP.insertMulti(line->name(), line); @@ -304,7 +308,7 @@ if (item) tw->addTopLevelItem(item); - emit populateProgress(++n); + emit populateProgress(++n, max); if (n % 64 == 0) { QCoreApplication::processEvents(); } @@ -422,22 +426,28 @@ setup(QString(), QString(), showGUI); } -void DataController::progressUpdate(int p) +void DataController::progressUpdate(int p, int max) { QString str; if (p == 0) - str.sprintf("Progress %6.2f%%", p * 100.0 / progressMax); - else if (p == progressMax) { + str.sprintf("Progress %6.2f%%", p * 100.0 / max); + else if (p == max) { str.sprintf("\b\b\b\b\b\b\b%6.2f%%\n", 100.0); } else { - str.sprintf("\b\b\b\b\b\b\b%6.2f%%", p * 100.0 / progressMax); + str.sprintf("\b\b\b\b\b\b\b%6.2f%%", p * 100.0 / max); } std::cout<<str.toStdString(); std::cout.flush(); } +void DataController::progressUpdate(int p) +{ + progressUpdate(p, progressMax); +} + + void DataController::deleteFile() { QString path = contextMenuItem->data(0, 32).toString(); @@ -475,7 +485,7 @@ contextMenu = 0; - connect(this, SIGNAL(populateProgress(int)), + connect(this, SIGNAL(populateProgress(int, int)), this, SLOT(progressUpdate(int))); QString dbpath; @@ -492,6 +502,11 @@ dblink = new MemoryDBLink(); #endif + connect(dblink, SIGNAL(progressUpdate(int, int)), + this, SLOT(progressUpdate(int, int))); + + + setDir((searchPath_in.size() > 0) ? searchPath_in : QDir(".")); showFullPath = false;
--- a/DataController.hpp Mon Oct 21 20:03:39 2013 +0200 +++ b/DataController.hpp Tue Oct 22 11:53:54 2013 +0200 @@ -41,11 +41,12 @@ signals: - void populateProgress(int); + void populateProgress(int, int); private slots: void initialPopulate(); void progressUpdate(int); + void progressUpdate(int, int); void deleteFile(); void itemDoubleClicked (QTreeWidgetItem * item, int column); void contextMenuRequested(const QPoint&);
--- a/FileDBLink.cpp Mon Oct 21 20:03:39 2013 +0200 +++ b/FileDBLink.cpp Tue Oct 22 11:53:54 2013 +0200 @@ -97,7 +97,7 @@ QByteArray hash; if (!lazy) hash = computeHash(path); - updateFile(path, size, lastModified, hash); + updateFile(path, size, lastModified, hash, lazy); } const QList<FileDBLink::dbinf_ptr_t >
--- a/FileDBLink.hpp Mon Oct 21 20:03:39 2013 +0200 +++ b/FileDBLink.hpp Tue Oct 22 11:53:54 2013 +0200 @@ -5,7 +5,8 @@ #include <QtCore/QDateTime> #include <QtCore/QFileInfo> -class FileDBLink { +class FileDBLink : public QObject { + Q_OBJECT public: class DBInfo { public: @@ -108,6 +109,9 @@ computeHash(const QString& path, QCryptographicHash::Algorithm = QCryptographicHash::Sha1); +signals: + void progressUpdate(int, int); + public: enum DBStatus { NONE = 0, MTIME_DIFFERENT, SAME};
--- a/SqliteDBLink.cpp Mon Oct 21 20:03:39 2013 +0200 +++ b/SqliteDBLink.cpp Tue Oct 22 11:53:54 2013 +0200 @@ -114,23 +114,33 @@ void SqliteDBLink::updateFile(const DBInfo& dbinfo, bool lazy) { - QSqlQuery query(db); - query.prepare("UPDATE files " - "SET size=:size, mtime=:mtime, checksum=:checksum " - "WHERE path=:path"); - query.bindValue(":path", dbinfo.path()); - query.bindValue(":size", dbinfo.size()); - query.bindValue(":mtime", dbinfo.mtime()); - query.bindValue(":checksum", dbinfo.checksum()); - if (!query.exec()) { - throw SQLException(query); + if (lazy) { + operations.push_back(Operation(dbinfo, Update)); + } + else { + QSqlQuery query(db); + query.prepare("UPDATE files " + "SET size=:size, mtime=:mtime, checksum=:checksum " + "WHERE path=:path"); + query.bindValue(":path", dbinfo.path()); + query.bindValue(":size", dbinfo.size()); + query.bindValue(":mtime", dbinfo.mtime()); + query.bindValue(":checksum", dbinfo.checksum()); + if (!query.exec()) { + throw SQLException(query); + } } } void SqliteDBLink::addFile(const DBInfo& dbinfo, bool lazy) { - if (!tryAddFile(dbinfo)) { - abort(); //Should throw exception + if (lazy) { + operations.push_back(Operation(dbinfo, Add)); + } + else { + if (!tryAddFile(dbinfo)) { + abort(); //Should throw exception + } } } @@ -243,3 +253,20 @@ return values; } + +bool SqliteDBLink::commit() +{ + int n = 0; + int max = operations.size(); + foreach(const Operation& operation, operations) { + switch (operation.second) { + case Add: + addFile(operation.first); + break; + case Update: + updateFile(operation.first); + } + emit progressUpdate(++n, max); + } + return true; +}
--- a/SqliteDBLink.hpp Mon Oct 21 20:03:39 2013 +0200 +++ b/SqliteDBLink.hpp Tue Oct 22 11:53:54 2013 +0200 @@ -3,6 +3,7 @@ #include "FileDBLink.hpp" #include <QtSql/QSqlDatabase> +#include <QtCore/QPair> class SqliteDBLink : public FileDBLink { public: @@ -26,11 +27,15 @@ virtual void keepOnlyFromPrefix(const QString& prefix, const QStringList& files); virtual void deleteFileFromDB(const QString& path); + bool commit(); private: - void addFile(const DBInfo& info, bool lazy); + typedef enum {Add, Update } OperationType; + typedef QPair<DBInfo, OperationType> Operation; + + void addFile(const DBInfo& info, bool lazy = false); bool tryAddFile(const DBInfo& info); - void updateFile(const DBInfo& dbinfo, bool lazy); + void updateFile(const DBInfo& dbinfo, bool lazy = false); QSqlDatabase db; static const QString connectionName; @@ -38,6 +43,7 @@ QSqlQuery* preparedSizePrefixQuery; QSqlQuery* preparedSizeQuery; QSqlQuery* preparedTryAddQuery; + QList<Operation> operations; }; #endif //MEMORYDBLINK_HPP
