comparison SqliteDBLink.cpp @ 104:6bc013d5788b

Avoid unnecessary updates. Fix problems with wrong subset being selected for update with prefix. Fix some problems with to much verbosity in debug statements.
author Tom Fredrik Blenning Klaussen <bfg@bfgconsult.no>
date Sat, 15 Feb 2014 13:34:10 +0100
parents 6c6f3a5f96ea
children e9b798e80bad
comparison
equal deleted inserted replaced
103:6b997f4f7e19 104:6bc013d5788b
338 338
339 return ""; 339 return "";
340 } 340 }
341 341
342 342
343 bool SqliteDBLink::commit(const QString& prefix) 343 bool SqliteDBLink::commit(const QString& prefix, bool dirty)
344 { 344 {
345 OperationType last = None; 345 OperationType last = None;
346 QVariantList paths, sizes, mtimes, hashes; 346 QVariantList paths, sizes, mtimes, hashes;
347 347
348 foreach(const Operation* operation, operations) { 348 const Operation* operation =
349 (operations.empty()) ? NULL : operations.takeFirst();
350
351 while (operation) {
349 if (operation->type() != last) { 352 if (operation->type() != last) {
350 executeOperation(paths, sizes, mtimes, hashes, last); 353 executeOperation(paths, sizes, mtimes, hashes, last);
351 } 354 }
352 355
353 switch (operation->type()) { 356 switch (operation->type()) {
356 const InfoOperation* iOperation = dynamic_cast<const InfoOperation*>(operation); 359 const InfoOperation* iOperation = dynamic_cast<const InfoOperation*>(operation);
357 paths.push_back(iOperation->info().path()); 360 paths.push_back(iOperation->info().path());
358 sizes.push_back(iOperation->info().size()); 361 sizes.push_back(iOperation->info().size());
359 mtimes.push_back(iOperation->info().mtime()); 362 mtimes.push_back(iOperation->info().mtime());
360 hashes.push_back(iOperation->info().checksum()); 363 hashes.push_back(iOperation->info().checksum());
364 dirty = true;
361 break; 365 break;
362 } 366 }
363 case Delete: { 367 case Delete: {
364 const DeleteOperation* dOperation = dynamic_cast<const DeleteOperation*>(operation); 368 const DeleteOperation* dOperation = dynamic_cast<const DeleteOperation*>(operation);
365 paths.push_back(dOperation->path()); 369 paths.push_back(dOperation->path());
366 } 370 }
367 case None: 371 case None:
368 break; 372 break;
369 } 373 }
370 last = operation->type(); 374 last = operation->type();
375 delete operation;
376 operation = (operations.empty()) ? NULL : operations.takeFirst();
371 } 377 }
372 if (last != None) { 378 if (last != None) {
373 qDebug() << "Execute Operation" << typeString(last); 379 qDebug() << "Execute Operation" << typeString(last);
374 qDebug() << paths; 380 foreach(QVariant path, paths) {
381 qDebug() << path.toString();
382 }
375 executeOperation(paths, sizes, mtimes, hashes, last); 383 executeOperation(paths, sizes, mtimes, hashes, last);
376 qDebug() << "Execute Operation Done"; 384 qDebug() << "Execute Operation Done";
377 } 385 }
378 386
379 QSqlQuery whatToUpdate(db); 387 if (dirty) {
380 QString whatToUpdateQuery = 388 QSqlQuery whatToUpdate(db);
381 "SELECT path FROM files WHERE checksum is NULL AND path in " 389 QString whatToUpdateQuery =
382 "(SELECT path FROM files WHERE size <> 0 %1 " 390 "SELECT path FROM files WHERE checksum is NULL %1 AND size in "
383 "GROUP BY size HAVING count(*) > 1) ORDER BY size"; 391 "(SELECT size FROM files WHERE size <> 0 %2 "
384 if (prefix.isEmpty()) { 392 "GROUP BY size HAVING count(*) > 1) ORDER BY size";
385 whatToUpdateQuery = whatToUpdateQuery.arg(""); 393 if (prefix.isEmpty()) {
386 } 394 whatToUpdateQuery = whatToUpdateQuery.arg("");
387 else { 395 }
388 whatToUpdateQuery = whatToUpdateQuery.arg("AND path LIKE :prefix"); 396 else {
389 } 397 whatToUpdateQuery = whatToUpdateQuery.arg("AND path LIKE :prefix1");
390 whatToUpdate.prepare(whatToUpdateQuery); 398 whatToUpdateQuery = whatToUpdateQuery.arg("AND path LIKE :prefix2");
391 if (!prefix.isEmpty()) { 399 }
392 whatToUpdate.bindValue("prefix", QString("%1%").arg(prefix)); 400 whatToUpdate.prepare(whatToUpdateQuery);
393 } 401 if (!prefix.isEmpty()) {
394 402 whatToUpdate.bindValue("prefix1", QString("%1%").arg(prefix));
395 qDebug() << "Before whatToUpdate"; 403 whatToUpdate.bindValue("prefix2", QString("%1%").arg(prefix));
396 if (!whatToUpdate.exec()) { 404 }
397 throw SQLException(whatToUpdate); 405
398 } 406 qDebug() << "Before whatToUpdate";
399 qDebug() << "After whatToUpdate"; 407 if (!whatToUpdate.exec()) {
400 408 throw SQLException(whatToUpdate);
401 int pathIndex = whatToUpdate.record().indexOf("path"); 409 }
402 QStringList updatePaths; 410 qDebug() << "After whatToUpdate";
403 while (whatToUpdate.next()) { 411
404 updatePaths << whatToUpdate.value(pathIndex).toString(); 412 int pathIndex = whatToUpdate.record().indexOf("path");
405 } 413 QStringList updatePaths;
406 int n = 0; 414 while (whatToUpdate.next()) {
407 int max = updatePaths.size(); 415 updatePaths << whatToUpdate.value(pathIndex).toString();
408 emit progressUpdate(0, max); 416 }
409 QSqlQuery updateChecksum(db); 417 int n = 0;
410 updateChecksum.prepare("UPDATE files " 418 int max = updatePaths.size();
411 "SET checksum=:checksum " 419 emit progressUpdate(0, max);
412 "WHERE path=:path"); 420 QSqlQuery updateChecksum(db);
413 421 updateChecksum.prepare("UPDATE files "
414 foreach (const QString& path, updatePaths) { 422 "SET checksum=:checksum "
415 qDebug() << path; 423 "WHERE path=:path");
416 QByteArray ohash = computeHash(path); 424
417 emit progressUpdate(++n, max); 425 foreach (const QString& path, updatePaths) {
418 updateChecksum.bindValue("checksum", ohash); 426 qDebug() << path;
419 updateChecksum.bindValue("path", path); 427 QByteArray ohash = computeHash(path);
420 if (!updateChecksum.exec()) 428 emit progressUpdate(++n, max);
421 throw SQLException(updateChecksum); 429 updateChecksum.bindValue("checksum", ohash);
422 430 updateChecksum.bindValue("path", path);
431 if (!updateChecksum.exec())
432 throw SQLException(updateChecksum);
433
434 }
423 } 435 }
424 return true; 436 return true;
425 } 437 }