Skip to content
Permalink
Browse files
Ensure that cached vector getFeatures request respect provider ordering
(cherry picked from commit 56f7812)
  • Loading branch information
nirvn authored and nyalldawson committed Apr 27, 2021
1 parent 6f62458 commit cb46a821240296af05d0368d0e0a7e05a32079c2
@@ -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;
}

@@ -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;
};
@@ -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()
@@ -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 )
@@ -323,6 +327,7 @@ void QgsVectorLayerCache::layerDeleted()
void QgsVectorLayerCache::invalidate()
{
mCache.clear();
mCacheOrderedKeys.clear();
mFullCache = false;
emit invalidated();
}
@@ -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;
@@ -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!

0 comments on commit cb46a82

Please sign in to comment.