# HG changeset patch # User Tom Fredrik Blenning Klaussen # Date 1382435634 -7200 # Node ID 308a718812ba8283138d7c39cfaf350d4ea86be5 # Parent f49023c61dac68113fd544a027b3d099850384bb Small refactoring to allow lazy commits. diff -r f49023c61dac -r 308a718812ba CMakeLists.txt --- 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 diff -r f49023c61dac -r 308a718812ba DataController.cpp --- 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 > mtimeLUP; QMultiMap > checksumLUP; + int max = elems.size() - 1; + foreach(QSharedPointer 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<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; diff -r f49023c61dac -r 308a718812ba DataController.hpp --- 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&); diff -r f49023c61dac -r 308a718812ba FileDBLink.cpp --- 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 diff -r f49023c61dac -r 308a718812ba FileDBLink.hpp --- 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 #include -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}; diff -r f49023c61dac -r 308a718812ba SqliteDBLink.cpp --- 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; +} diff -r f49023c61dac -r 308a718812ba SqliteDBLink.hpp --- 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 +#include 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 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 operations; }; #endif //MEMORYDBLINK_HPP