diff FileDBLink.cpp @ 85:1f9e27a0bd7f

Allow for lazy calculation of checksums, ignore them, if only one file of given size.
author Tom Fredrik Blenning Klaussen <bfg@bfgconsult.no>
date Thu, 10 Oct 2013 15:55:30 +0200
parents 9744ec195be3
children af7962f3274b
line wrap: on
line diff
--- 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 <boost/bind.hpp>
 
-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<dbinf_ptr_t> 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<FileDBLink::dbinf_ptr_t >
@@ -172,3 +193,30 @@
 #endif
   return list;
 }
+
+
+const QList<FileDBLink::dbinf_ptr_t> 
+FileDBLink::filesWithSize(qint64 size, const QString& prefix) const
+{
+  QList<dbinf_ptr_t> retVal;
+  const QList<dbinf_ptr_t> 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<dbinf_ptr_t> vals = values();
+  foreach (const dbinf_ptr_t val, vals) {
+    QString vpath = val->path();
+    if (vpath == path) {
+      return val;
+    }
+  }
+  return dbinf_ptr_t();
+}