Improve loading of all data · digitalarche/sqlitebrowser@28b8652 · GitHub
Skip to content

Commit 28b8652

Browse files
committed
Improve loading of all data
The multithreading patch didn't properly load all data into the cache when this was necessary. It would only do so if the chunk size was sufficiently high. This is fixed in this commit. Show a progress dialog while loading all data which can be cancelled by the user. When cancelling the loading of all data in those cases which require all data to be loaded, stop whatever process needs the data too.
1 parent 115d1f1 commit 28b8652

5 files changed

Lines changed: 62 additions & 25 deletions

File tree

‎src/DbStructureModel.cpp‎

Lines changed: 12 additions & 8 deletions

‎src/EditTableDialog.cpp‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,12 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column)
388388
.arg(sqlb::escapeIdentifier(pdb.getObjectByName(curTable).dynamicCast<sqlb::Table>()->rowidColumn()))
389389
.arg(curTable.toString())
390390
.arg(sqlb::escapeIdentifier(field->name())));
391-
m.completeCache();
391+
if(!m.completeCache())
392+
{
393+
// If we couldn't load all data because the cancel button was clicked, just unset the checkbox again and stop.
394+
item->setCheckState(column, Qt::Unchecked);
395+
return;
396+
}
392397
if(m.data(m.index(0, 0)).toInt() > 0)
393398
{
394399
// There is a NULL value, so print an error message, uncheck the combobox, and return here
@@ -416,7 +421,12 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column)
416421
.arg(curTable.toString())
417422
.arg(sqlb::escapeIdentifier(field->name()))
418423
.arg(sqlb::escapeIdentifier(field->name())));
419-
m.completeCache();
424+
if(!m.completeCache())
425+
{
426+
// If we couldn't load all data because the cancel button was clicked, just unset the checkbox again and stop.
427+
item->setCheckState(column, Qt::Unchecked);
428+
return;
429+
}
420430
if(m.data(m.index(0, 0)).toInt() > 0)
421431
{
422432
// There is a non-integer value, so print an error message, uncheck the combobox, and return here
@@ -458,10 +468,20 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column)
458468
// Because our renameColumn() function fails when setting a column to unique when it already contains the same values
459469
SqliteTableModel m(pdb, this);
460470
m.setQuery(QString("SELECT COUNT(%2) FROM %1;").arg(curTable.toString()).arg(sqlb::escapeIdentifier(field->name())));
461-
m.completeCache();
471+
if(!m.completeCache())
472+
{
473+
// If we couldn't load all data because the cancel button was clicked, just unset the checkbox again and stop.
474+
item->setCheckState(column, Qt::Unchecked);
475+
return;
476+
}
462477
int rowcount = m.data(m.index(0, 0)).toInt();
463478
m.setQuery(QString("SELECT COUNT(DISTINCT %2) FROM %1;").arg(curTable.toString()).arg(sqlb::escapeIdentifier(field->name())));
464-
m.completeCache();
479+
if(!m.completeCache())
480+
{
481+
// If we couldn't load all data because the cancel button was clicked, just unset the checkbox again and stop.
482+
item->setCheckState(column, Qt::Unchecked);
483+
return;
484+
}
465485
int uniquecount = m.data(m.index(0, 0)).toInt();
466486
if(rowcount != uniquecount)
467487
{

‎src/PlotDock.cpp‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -733,15 +733,7 @@ void PlotDock::fetchAllData()
733733
{
734734
if(m_currentPlotModel)
735735
{
736-
// Show progress dialog because fetching all data might take some time
737-
QProgressDialog progress(tr("Fetching all data..."),
738-
tr("Cancel"), m_currentPlotModel->rowCount(), m_currentPlotModel->rowCount());
739-
progress.setWindowModality(Qt::ApplicationModal);
740-
progress.show();
741-
qApp->processEvents();
742-
743736
// Make sure all data is loaded
744-
// TODO make this cancellable & show progress
745737
m_currentPlotModel->completeCache();
746738

747739
// Update plot

‎src/sqlitetablemodel.cpp‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <QFile>
1212
#include <QUrl>
1313
#include <QtConcurrent/QtConcurrentRun>
14+
#include <QProgressDialog>
1415

1516
#include "RowLoader.h"
1617

@@ -939,10 +940,30 @@ void SqliteTableModel::triggerCacheLoad (int row_begin, int row_end) const
939940
triggerCacheLoad((row_begin + row_end) / 2);
940941
}
941942

942-
void SqliteTableModel::completeCache () const
943+
bool SqliteTableModel::completeCache () const
943944
{
944-
triggerCacheLoad(0, rowCount());
945-
worker->waitUntilIdle();
945+
// Show progress dialog because fetching all data might take some time but only show
946+
// cancel button if we allow cancellation here. This isn't
947+
QProgressDialog progress(tr("Fetching data..."),
948+
tr("Cancel"), 0, rowCount());
949+
progress.setWindowModality(Qt::ApplicationModal);
950+
progress.show();
951+
952+
waitUntilIdle();
953+
954+
// This loop fetches all data by loading it block by block into the cache
955+
for(int i=0;i<rowCount()+static_cast<int>(m_chunkSize)/2;i+=m_chunkSize)
956+
{
957+
progress.setValue(i);
958+
qApp->processEvents();
959+
if(progress.wasCanceled())
960+
return false;
961+
962+
triggerCacheLoad(i);
963+
worker->waitUntilIdle();
964+
}
965+
966+
return true;
946967
}
947968

948969
bool SqliteTableModel::isCacheComplete () const

‎src/sqlitetablemodel.h‎

Lines changed: 2 additions & 2 deletions

0 commit comments

Comments
 (0)