SyncJournalDb::SyncJournalDb(const QString &dbFilePath, QObject *parent)
: QObject(parent)
, _dbFile(dbFilePath)
+ , _mutex(QMutex::Recursive)
, _transaction(0)
+ , _metadataTableIsEmpty(false)
{
// Allow forcing the journal mode for debugging
static QString envJournalMode = QString::fromLocal8Bit(qgetenv("OWNCLOUD_SQLITE_JOURNAL_MODE"));
// don't start a new transaction now
commitInternal(QString("checkConnect End"), false);
+ // This avoid reading from the DB if we already know it is empty
+ // thereby speeding up the initial discovery significantly.
+ _metadataTableIsEmpty = (getFileRecordCount() == 0);
+
// Hide 'em all!
FileSystem::setFileHidden(databaseFilePath(), true);
FileSystem::setFileHidden(databaseFilePath() + "-wal", true);
_db.close();
_avoidReadFromDbOnNextSyncFilter.clear();
+ _metadataTableIsEmpty = false;
}
return false;
}
+ // Can't be true anymore.
+ _metadataTableIsEmpty = false;
+
return true;
} else {
qCWarning(lcDb) << "Failed to connect database.";
rec->_path.clear();
Q_ASSERT(!rec->isValid());
+ if (_metadataTableIsEmpty)
+ return true; // no error, yet nothing found (rec->isValid() == false)
+
if (!checkConnect())
return false;
_getFileRecordQuery->bindValue(1, getPHash(filename));
if (!_getFileRecordQuery->exec()) {
- locker.unlock();
close();
return false;
}
if (errId != SQLITE_DONE) { // only do this if the problem is different from SQLITE_DONE
QString err = _getFileRecordQuery->error();
qCWarning(lcDb) << "No journal entry found for " << filename << "Error: " << err;
- locker.unlock();
close();
- locker.relock();
}
}
}
rec->_path.clear();
Q_ASSERT(!rec->isValid());
- if (!inode)
+ if (!inode || _metadataTableIsEmpty)
return true; // no error, yet nothing found (rec->isValid() == false)
if (!checkConnect())
rec->_path.clear();
Q_ASSERT(!rec->isValid());
- if (fileId.isEmpty())
+ if (fileId.isEmpty() || _metadataTableIsEmpty)
return true; // no error, yet nothing found (rec->isValid() == false)
if (!checkConnect())
{
QMutexLocker locker(&_mutex);
+ if (_metadataTableIsEmpty)
+ return true; // no error, yet nothing found
+
if (!checkConnect())
return false;
{
QMutexLocker locker(&_mutex);
- if (!checkConnect()) {
- return -1;
- }
-
SqlQuery query(_db);
query.prepare("SELECT COUNT(*) FROM metadata");
if (!query.exec()) {
- return 0;
+ return -1;
}
if (query.next()) {
return count;
}
- return 0;
+ return -1;
}
bool SyncJournalDb::updateFileRecordChecksum(const QString &filename,
// We also need to remove the ETags so the update phase refreshes the directory paths
// on the next sync
- locker.unlock();
avoidReadFromDbOnNextSync(path);
}
bool setFileRecordMetadata(const SyncJournalFileRecord &record);
bool deleteFileRecord(const QString &filename, bool recursively = false);
- int getFileRecordCount();
bool updateFileRecordChecksum(const QString &filename,
const QByteArray &contentChecksum,
const QByteArray &contentChecksumType);
void clearFileTable();
private:
+ int getFileRecordCount();
bool updateDatabaseStructure();
bool updateMetadataTableStructure();
bool updateErrorBlacklistTableStructure();
QString _dbFile;
QMutex _mutex; // Public functions are protected with the mutex.
int _transaction;
+ bool _metadataTableIsEmpty;
// NOTE! when adding a query, don't forget to reset it in SyncJournalDb::close
QScopedPointer<SqlQuery> _getFileRecordQuery;
csync_resume(_csync_ctx.data());
- int fileRecordCount = -1;
if (!_journal->exists()) {
qCInfo(lcEngine) << "New sync (no sync journal exists)";
} else {
verStr.append(" on ").append(Utility::platformName());
qCInfo(lcEngine) << verStr;
- fileRecordCount = _journal->getFileRecordCount(); // this creates the DB if it does not exist yet
-
- if (fileRecordCount == -1) {
+ // This creates the DB if it does not exist yet.
+ if (!_journal->isConnected()) {
qCWarning(lcEngine) << "No way to create a sync journal!";
csyncError(tr("Unable to open or create the local sync database. Make sure you have write access in the sync folder."));
finalize(false);
_csync_ctx->read_remote_from_db = true;
- // This tells csync to never read from the DB if it is empty
- // thereby speeding up the initial discovery significantly.
- _csync_ctx->db_is_empty = (fileRecordCount == 0);
-
bool ok;
auto selectiveSyncBlackList = _journal->getSelectiveSyncList(SyncJournalDb::SelectiveSyncBlackList, &ok);
if (ok) {