Skip to content

Commit e47e592

Browse files
author
mhugent
committed
Fetch features asynchronously from the database server. This improves performance, especially if the db is located on a remote computer. Plus fixed a memory leak in qgsfeature that is important for postgres provider
git-svn-id: http://svn.osgeo.org/qgis/trunk@7269 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 721c2a7 commit e47e592

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

src/core/qgsfeature.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ QgsFeature & QgsFeature::operator=( QgsFeature const & rhs )
124124
mTypeName = rhs.mTypeName;
125125

126126
// copy embedded geometry
127+
delete mGeometry;
127128
if ( rhs.mGeometry )
128129
{
129130
mGeometry = new QgsGeometry( *(rhs.mGeometry) );

src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const QString POSTGRES_DESCRIPTION = "PostgreSQL/PostGIS data provider";
6868
QgsPostgresProvider::QgsPostgresProvider(QString const & uri)
6969
: QgsVectorDataProvider(uri),
7070
geomType(QGis::WKBUnknown),
71-
mFeatureQueueSize(200),
71+
mFeatureQueueSize(200),
7272
gotPostgisVersion(FALSE)
7373
{
7474
// assume this is a valid layer until we determine otherwise
@@ -397,9 +397,18 @@ bool QgsPostgresProvider::getNextFeature(QgsFeature& feature)
397397
{
398398
QString fetch = QString("fetch forward %1 from qgisf")
399399
.arg(mFeatureQueueSize);
400-
401-
queryResult = PQexec(connection, (const char *)fetch);
402-
400+
401+
if(mFirstFetch)
402+
{
403+
if(PQsendQuery(connection, (const char *)fetch) == 0) //fetch features in asynchronously
404+
{
405+
qWarning("PQsendQuery failed (1)");
406+
}
407+
}
408+
mFirstFetch = false;
409+
queryResult = PQgetResult(connection);
410+
PGresult* bla = PQgetResult(connection); //just to get the 0 pointer...
411+
403412
int rows = PQntuples(queryResult);
404413

405414
if (rows == 0)
@@ -434,7 +443,7 @@ bool QgsPostgresProvider::getNextFeature(QgsFeature& feature)
434443
char* attribute = PQgetvalue(queryResult, row, PQfnumber(queryResult,*name_it));
435444

436445
QString val = QString::fromUtf8(attribute);
437-
switch (attributeFields[*index_it].type())
446+
switch (attributeFields[*index_it].type())
438447
{
439448
case QVariant::Int:
440449
feature.addAttribute(*index_it, val.toInt());
@@ -463,19 +472,20 @@ bool QgsPostgresProvider::getNextFeature(QgsFeature& feature)
463472
}
464473
else
465474
{
466-
//QgsDebugMsg("Couldn't get the feature geometry in binary form");
475+
QgsDebugMsg("Couldn't get the feature geometry in binary form");
467476
}
468-
}
469-
470-
//QgsDebugMsg(" pushing " + QString::number(f->featureId());
477+
}
471478

472479
mFeatureQueue.push(feature);
473480

474481
} // for each row in queue
475-
476-
//QgsDebugMsg("retrieved batch of features.");
477482

478483
PQclear(queryResult);
484+
485+
if(PQsendQuery(connection, (const char *)fetch) == 0) //already fetch the next couple of features asynchronously
486+
{
487+
qWarning("PQsendQuery failed (2)");
488+
}
479489

480490
} // if new queue is required
481491

@@ -486,11 +496,9 @@ bool QgsPostgresProvider::getNextFeature(QgsFeature& feature)
486496
}
487497
else
488498
{
489-
//QgsDebugMsg("Read attempt on an invalid postgresql data source");
499+
QgsDebugMsg("Read attempt on an invalid postgresql data source");
490500
return false;
491501
}
492-
493-
//QgsDebugMsg("returning feature " + QString::number(feat.featureId()));
494502

495503
return true;
496504
}
@@ -583,6 +591,7 @@ void QgsPostgresProvider::select(QgsAttributeList fetchAttributes,
583591
PQexec(connection, (const char *)(declare.utf8()));
584592

585593
mFeatureQueue.empty();
594+
mFirstFetch = true;
586595
}
587596

588597
bool QgsPostgresProvider::getFeatureAtId(int featureId,

src/providers/postgres/qgspostgresprovider.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ class QgsPostgresProvider:public QgsVectorDataProvider
328328

329329
private:
330330

331+
bool mFirstFetch; //true if fetch forward is called the first time after select
331332
std::vector < QgsFeature > features;
332333
QgsFieldMap attributeFields;
333334
QString mDataComment;

0 commit comments

Comments
 (0)