Add reindex action

This commit is contained in:
2022-04-22 22:01:21 +02:00
parent 95e4774507
commit 903ec65d52
4 changed files with 91 additions and 53 deletions
+80 -53
View File
@@ -139,6 +139,28 @@ void Database::indexDir(const QDir &dir, QProgressDialog *progress)
database.rollback();
}
void Database::reindex(QProgressDialog *progress)
{
QVariantList deleteids;
QSqlDatabase database = QSqlDatabase::database();
QSqlQuery files = database.exec("SELECT id,file,mtime FROM fits_files");
progress->setMaximum(files.size());
int i = 0;
while(files.next())
{
QString path = files.value(1).toString();
QFileInfo file(path);
if(file.exists() && file.fileTime(QFileDevice::FileModificationTime).toUTC().toString(Qt::ISODate) != files.value(2).toString())
indexFile(file);
if(!file.exists())
deleteids.append(files.value(0));
progress->setValue(i++);
}
QSqlQuery deleteFiles("DELETE FROM fits_files WHERE id = ?", database);
deleteFiles.bindValue(0, deleteids);
deleteFiles.execBatch();
}
QStringList Database::getFitsKeywords()
{
m_headerKeywords.exec();
@@ -161,63 +183,68 @@ bool Database::indexDir2(const QDir &dir, QProgressDialog *progress)
if(!indexDir2(dir.filePath(d), progress))
return false;
}
ImageInfoData info;
for(const QFileInfo &file : files)
{
progress->setValue(m_progress++);
if(progress->wasCanceled())return false;
QString filePath = file.absoluteFilePath();
QString mtime = file.fileTime(QFileDevice::FileModificationTime).toUTC().toString(Qt::ISODate);
m_checkFile.bindValue(0, filePath);
m_checkFile.exec();
if(m_checkFile.next())
{
if(m_checkFile.value(1).toString() == mtime)
continue;
else
{
m_deleteFile.bindValue(0, m_checkFile.value(0).toLongLong());
m_deleteFile.exec();
}
}
bool ok;
if(filePath.endsWith(".xisf", Qt::CaseInsensitive))
ok = readXISFHeader(filePath, info);
else
ok = readFITSHeader(filePath, info);
qlonglong last_id = -1;
if(ok)
{
m_insertFile.bindValue(0, filePath);
m_insertFile.bindValue(1, mtime);
if(!m_insertFile.exec())
{
qDebug() << m_insertFile.lastError();
return false;
}
last_id = m_insertFile.lastInsertId().toLongLong();
QVariantList file_id, keys, values, comments;
for(auto &record : info.fitsHeader)
{
file_id << last_id;
keys << QString(record.key);
values << record.value.toString();
comments << QString(record.comment);
}
m_insertFitsHeader.bindValue(0, file_id);
m_insertFitsHeader.bindValue(1, keys);
m_insertFitsHeader.bindValue(2, values);
m_insertFitsHeader.bindValue(3, comments);
if(!m_insertFitsHeader.execBatch())
{
qDebug() << m_insertFitsHeader.lastError();
return false;
}
}
qDebug() << filePath << last_id;
info.fitsHeader.clear();
if(!indexFile(file))return false;
}
return true;
}
bool Database::indexFile(const QFileInfo &file)
{
ImageInfoData info;
QString filePath = file.absoluteFilePath();
QString mtime = file.fileTime(QFileDevice::FileModificationTime).toUTC().toString(Qt::ISODate);
m_checkFile.bindValue(0, filePath);
m_checkFile.exec();
if(m_checkFile.next())
{
if(m_checkFile.value(1).toString() == mtime)
return true;
else
{
m_deleteFile.bindValue(0, m_checkFile.value(0).toLongLong());
m_deleteFile.exec();
}
}
bool ok;
if(filePath.endsWith(".xisf", Qt::CaseInsensitive))
ok = readXISFHeader(filePath, info);
else
ok = readFITSHeader(filePath, info);
qlonglong last_id = -1;
if(ok)
{
m_insertFile.bindValue(0, filePath);
m_insertFile.bindValue(1, mtime);
if(!m_insertFile.exec())
{
qDebug() << m_insertFile.lastError();
return false;
}
last_id = m_insertFile.lastInsertId().toLongLong();
QVariantList file_id, keys, values, comments;
for(auto &record : info.fitsHeader)
{
file_id << last_id;
keys << QString(record.key);
values << record.value.toString();
comments << QString(record.comment);
}
m_insertFitsHeader.bindValue(0, file_id);
m_insertFitsHeader.bindValue(1, keys);
m_insertFitsHeader.bindValue(2, values);
m_insertFitsHeader.bindValue(3, comments);
if(!m_insertFitsHeader.execBatch())
{
qDebug() << m_insertFitsHeader.lastError();
return false;
}
}
qDebug() << filePath << last_id;
return true;
}