Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Ensure that cached vector getFeatures request respect provider ordering
(cherry picked from commit 56f7812)
  • Loading branch information
nirvn authored and nyalldawson committed Apr 18, 2021
1 parent fb8c8b2 commit 8bbb5f1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/core/qgscachedfeatureiterator.cpp
Expand Up @@ -45,15 +45,15 @@ QgsCachedFeatureIterator::QgsCachedFeatureIterator( QgsVectorLayerCache *vlCache
switch ( featureRequest.filterType() )
{
case QgsFeatureRequest::FilterFids:
mFeatureIds = featureRequest.filterFids();
mFeatureIds = QList< QgsFeatureId >( qgis::setToList( featureRequest.filterFids() ) );
break;

case QgsFeatureRequest::FilterFid:
mFeatureIds = QgsFeatureIds() << featureRequest.filterFid();
mFeatureIds = QList< QgsFeatureId >() << featureRequest.filterFid();
break;

default:
mFeatureIds = qgis::listToSet( mVectorLayerCache->mCache.keys() );
mFeatureIds = mVectorLayerCache->mCacheOrderedKeys;
break;
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/qgscachedfeatureiterator.h
Expand Up @@ -76,9 +76,9 @@ class CORE_EXPORT QgsCachedFeatureIterator : public QgsAbstractFeatureIterator
bool nextFeatureFilterFids( QgsFeature &f ) override { return fetchFeature( f ); }

private:
QgsFeatureIds mFeatureIds;
QList< QgsFeatureId > mFeatureIds;
QgsVectorLayerCache *mVectorLayerCache = nullptr;
QgsFeatureIds::ConstIterator mFeatureIdIterator;
QList< QgsFeatureId >::ConstIterator mFeatureIdIterator;
QgsCoordinateTransform mTransform;
QgsRectangle mFilterRect;
};
Expand Down
7 changes: 6 additions & 1 deletion src/core/qgsvectorlayercache.cpp
Expand Up @@ -173,7 +173,10 @@ bool QgsVectorLayerCache::featureAtId( QgsFeatureId featureId, QgsFeature &featu

bool QgsVectorLayerCache::removeCachedFeature( QgsFeatureId fid )
{
return mCache.remove( fid );
bool removed = mCache.remove( fid );
if ( removed )
mCacheOrderedKeys.removeOne( fid );
return removed;
}

QgsVectorLayer *QgsVectorLayerCache::layer()
Expand Down Expand Up @@ -265,6 +268,7 @@ void QgsVectorLayerCache::onJoinAttributeValueChanged( QgsFeatureId fid, int fie
void QgsVectorLayerCache::featureDeleted( QgsFeatureId fid )
{
mCache.remove( fid );
mCacheOrderedKeys.removeOne( fid );
}

void QgsVectorLayerCache::onFeatureAdded( QgsFeatureId fid )
Expand Down Expand Up @@ -323,6 +327,7 @@ void QgsVectorLayerCache::layerDeleted()
void QgsVectorLayerCache::invalidate()
{
mCache.clear();
mCacheOrderedKeys.clear();
mFullCache = false;
emit invalidated();
}
Expand Down
3 changes: 3 additions & 0 deletions src/core/qgsvectorlayercache.h
Expand Up @@ -393,10 +393,13 @@ class CORE_EXPORT QgsVectorLayerCache : public QObject
{
QgsCachedFeature *cachedFeature = new QgsCachedFeature( feat, this );
mCache.insert( feat.id(), cachedFeature );
if ( !mCacheOrderedKeys.contains( feat.id() ) )
mCacheOrderedKeys << feat.id();
}

QgsVectorLayer *mLayer = nullptr;
QCache< QgsFeatureId, QgsCachedFeature > mCache;
QList< QgsFeatureId > mCacheOrderedKeys;

bool mCacheGeometry = true;
bool mFullCache = false;
Expand Down
6 changes: 5 additions & 1 deletion tests/src/core/testqgsvectorlayercache.cpp
Expand Up @@ -267,11 +267,15 @@ void TestVectorLayerCache::testFullCacheThroughRequest()
// suck in all features
}

// cache should now contain all features
// cache should now contain all features, and should iterate through in the same order as the non-cached feature ordering
it = mPointsLayer->getFeatures();
QgsFeatureIterator itCached = cache.getFeatures( QgsFeatureRequest() );
QgsFeature fCached;
while ( it.nextFeature( f ) )
{
QVERIFY( cache.isFidCached( f.id() ) );
itCached.nextFeature( fCached );
QCOMPARE( f.id(), fCached.id() );
}

// so it should be a full cache!
Expand Down

0 comments on commit 8bbb5f1

Please sign in to comment.