[app] Memory map the book to calculate its hash

It's more efficient
This commit is contained in:
Slava Monich 2018-01-27 01:55:36 +02:00
parent c60b96ecc3
commit 209280a3e4

View file

@ -1,6 +1,6 @@
/* /*
* Copyright (C) 2015-2017 Jolla Ltd. * Copyright (C) 2015-2018 Jolla Ltd.
* Contact: Slava Monich <slava.monich@jolla.com> * Copyright (C) 2015-2018 Slava Monich <slava.monich@jolla.com>
* *
* You may use this file under the terms of the BSD license as follows: * You may use this file under the terms of the BSD license as follows:
* *
@ -139,21 +139,29 @@ QByteArray BooksImportModel::Task::calculateFileHash(QString aPath)
QByteArray result; QByteArray result;
QFile file(aPath); QFile file(aPath);
if (file.open(QIODevice::ReadOnly)) { if (file.open(QIODevice::ReadOnly)) {
qint64 len = 0; const qint64 size = file.size();
QCryptographicHash hash(DIGEST_TYPE); uchar* map = file.map(0, size);
hash.reset(); if (map) {
if (!iBuf) iBuf = new char[iBufSize]; const char* ptr = (char*)map;
while (!isCanceled() && (len = file.read(iBuf, iBufSize)) > 0) { qint64 bytesLeft = size;
hash.addData(iBuf, len); QCryptographicHash hash(DIGEST_TYPE);
} hash.reset();
if (len == 0) { while (!isCanceled() && bytesLeft > DIGEST_SIZE) {
hash.addData(ptr, DIGEST_SIZE);
bytesLeft -= DIGEST_SIZE;
ptr += DIGEST_SIZE;
}
if (!isCanceled()) { if (!isCanceled()) {
if (bytesLeft) {
hash.addData(ptr, bytesLeft);
}
result = hash.result(); result = hash.result();
HASSERT(result.size() == DIGEST_SIZE); HASSERT(result.size() == DIGEST_SIZE);
HDEBUG(qPrintable(aPath) << QString(result.toHex())); HDEBUG(qPrintable(aPath) << QString(result.toHex()));
} }
file.unmap(map);
} else { } else {
HWARN("error reading" << qPrintable(aPath)); HWARN("error mapping" << qPrintable(aPath));
} }
file.close(); file.close();
} }