comparison 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
comparison
equal deleted inserted replaced
84:848496a57039 85:1f9e27a0bd7f
6 6
7 #include <QtCore/QtConcurrentMap> 7 #include <QtCore/QtConcurrentMap>
8 8
9 #include <boost/bind.hpp> 9 #include <boost/bind.hpp>
10 10
11 void FileDBLink::updateIfModified(const QString& path) 11 void FileDBLink::updateIfModified(const QString& path, bool lazy)
12 { 12 {
13 QFileInfo fileinfo(path); 13 QFileInfo fileinfo(path);
14 FileDBLink::DBStatus status = existsWithMtime(path, fileinfo.lastModified()); 14 FileDBLink::DBStatus status = existsWithMtime(path, fileinfo.lastModified());
15 15
16 switch (status) { 16 switch (status) {
17 case FileDBLink::NONE: { 17 case FileDBLink::NONE: {
18 addFile(fileinfo); 18 addFile(fileinfo, lazy);
19 break; 19 break;
20 } 20 }
21 case FileDBLink::MTIME_DIFFERENT: { 21 case FileDBLink::MTIME_DIFFERENT: {
22 updateFile(fileinfo); 22 updateFile(fileinfo, lazy);
23 } 23 }
24 default: { 24 default: {
25 } 25 }
26 } 26 }
27 } 27 }
28 28
29 void FileDBLink::addFile(const QFileInfo& fileinfo) 29 void FileDBLink::addFile(const QFileInfo& fileinfo, bool lazy)
30 { 30 {
31 addFile(fileinfo.absoluteFilePath(), fileinfo.size(), 31 addFile(fileinfo.absoluteFilePath(), fileinfo.size(),
32 fileinfo.lastModified()); 32 fileinfo.lastModified(), lazy);
33 } 33 }
34 34
35 QByteArray FileDBLink::computeHash(const QString& path, 35 QByteArray FileDBLink::computeHash(const QString& path,
36 QCryptographicHash::Algorithm algorithm) 36 QCryptographicHash::Algorithm algorithm)
37 { 37 {
54 } 54 }
55 } 55 }
56 return hash.result(); 56 return hash.result();
57 } 57 }
58 58
59 bool FileDBLink::updateAllWithSize(qint64 size)
60 {
61 const QList<dbinf_ptr_t> others = filesWithSize(size);
62 if (!others.empty()) {
63 foreach( const dbinf_ptr_t other, others) {
64 if (other->checksum().isEmpty()) {
65 QByteArray ohash = computeHash(other->path());
66 updateFile(other->path(), other->size(), other->mtime(), ohash);
67 }
68 }
69 return true;
70 }
71 return false;
72 }
59 73
60 void FileDBLink::addFile(const QString& path, qint64 size, 74 void FileDBLink::addFile(const QString& path, qint64 size,
61 const QDateTime& lastModified) 75 const QDateTime& lastModified, bool lazy)
62 { 76 {
63 addFile(path, size, lastModified, computeHash(path)); 77 QByteArray hash;
64 } 78 // std::cout << path.toStdString() << "::" << lazy << std::endl;
65 79 if (!lazy || updateAllWithSize(size))
66 void FileDBLink::updateFile(const QFileInfo& fileinfo) 80 hash = computeHash(path);
81 addFile(path, size, lastModified, hash);
82 }
83
84 void FileDBLink::updateFile(const QFileInfo& fileinfo, bool lazy)
67 { 85 {
68 updateFile(fileinfo.absoluteFilePath(), fileinfo.size(), 86 updateFile(fileinfo.absoluteFilePath(), fileinfo.size(),
69 fileinfo.lastModified()); 87 fileinfo.lastModified(), lazy);
70 } 88 }
71 89
72 void FileDBLink::updateFile(const QString& path, qint64 size, 90 void FileDBLink::updateFile(const QString& path, qint64 size,
73 const QDateTime& lastModified) 91 const QDateTime& lastModified, bool lazy)
74 { 92 {
75 updateFile(path, size, lastModified, computeHash(path)); 93 QByteArray hash;
94 if (!lazy || updateAllWithSize(size))
95 hash = computeHash(path);
96 updateFile(path, size, lastModified, hash);
76 } 97 }
77 98
78 const QList<FileDBLink::dbinf_ptr_t > 99 const QList<FileDBLink::dbinf_ptr_t >
79 FileDBLink::sortOn(const QString& prefix, SORTORDER order, bool extended) 100 FileDBLink::sortOn(const QString& prefix, SORTORDER order, bool extended)
80 { 101 {
170 list.push_back(ext); 191 list.push_back(ext);
171 } 192 }
172 #endif 193 #endif
173 return list; 194 return list;
174 } 195 }
196
197
198 const QList<FileDBLink::dbinf_ptr_t>
199 FileDBLink::filesWithSize(qint64 size, const QString& prefix) const
200 {
201 QList<dbinf_ptr_t> retVal;
202 const QList<dbinf_ptr_t> vals = values(prefix);
203 foreach (const dbinf_ptr_t val, vals) {
204 QString path = val->path();
205 if (val->size() == size) {
206 retVal << val;
207 }
208 }
209 return retVal;
210 }
211
212 FileDBLink::dbinf_ptr_t FileDBLink::value(const QString& path) const
213 {
214 const QList<dbinf_ptr_t> vals = values();
215 foreach (const dbinf_ptr_t val, vals) {
216 QString vpath = val->path();
217 if (vpath == path) {
218 return val;
219 }
220 }
221 return dbinf_ptr_t();
222 }