diff --git a/python/core/qgscacheindexfeatureid.sip b/python/core/qgscacheindexfeatureid.sip index b6c7bd16f59..2fc8aec2256 100644 --- a/python/core/qgscacheindexfeatureid.sip +++ b/python/core/qgscacheindexfeatureid.sip @@ -6,42 +6,8 @@ class QgsCacheIndexFeatureId : QgsAbstractCacheIndex public: QgsCacheIndexFeatureId( QgsVectorLayerCache* ); - /** - * Is called, whenever a feature is removed from the cache. You should update your indexes, so - * they become invalid in case this feature was required to successfuly answer a request. - */ virtual void flushFeature( const QgsFeatureId fid ); - - /** - * Sometimes, the whole cache changes its state and its easier to just withdraw everything. - * In this case, this method is issued. Be sure to clear all cache information in here. - */ virtual void flush(); - - /** - * @brief - * Implement this method to update the the indices, in case you need information contained by the request - * to properly index. (E.g. spatial index) - * Does nothing by default - * - * @param featureRequest The feature request that was answered - * @param fids The feature ids that have been returned - */ virtual void requestCompleted( const QgsFeatureRequest& featureRequest, const QgsFeatureIds& fids ); - - /** - * Is called, when a feature request is issued on a cached layer. - * If this cache index is able to completely answer the feature request, it will return true - * and write the list of feature ids of cached features to cachedFeatures. If it is not able - * it will return false and the cachedFeatures state is undefined. - * - * @param featureIterator A reference to a {@link QgsFeatureIterator}. A valid featureIterator will - * be assigned in case this index is able to answer the request and the return - * value is true. - * @param featureRequest The feature request, for which this index is queried. - * - * @return True, if this index holds the information to answer the request. - * - */ virtual bool getCacheIterator( QgsFeatureIterator& featureIterator, const QgsFeatureRequest& featureRequest ); }; diff --git a/python/core/qgsvectorlayercache.sip b/python/core/qgsvectorlayercache.sip index 3fe37c87f07..6ece7982510 100644 --- a/python/core/qgsvectorlayercache.sip +++ b/python/core/qgsvectorlayercache.sip @@ -123,8 +123,15 @@ class QgsVectorLayerCache : QObject * Check if a certain feature id is cached. * @param fid The feature id to look for * @return True if this id is in the cache + * @see cachedFeatureIds() */ - bool isFidCached( const QgsFeatureId fid ); + bool isFidCached( const QgsFeatureId fid ) const; + + /** Returns the set of feature IDs for features which are cached. + * @note added in QGIS 3.0 + * @see isFidCached() + */ + QgsFeatureIds cachedFeatureIds() const; /** * Gets the feature at the given feature id. Considers the changed, added, deleted and permanent features diff --git a/src/core/qgscacheindex.h b/src/core/qgscacheindex.h index 80bd359159a..836b7f5a994 100644 --- a/src/core/qgscacheindex.h +++ b/src/core/qgscacheindex.h @@ -58,8 +58,8 @@ class CORE_EXPORT QgsAbstractCacheIndex /** * Is called, when a feature request is issued on a cached layer. * If this cache index is able to completely answer the feature request, it will return true - * and write the list of feature ids of cached features to cachedFeatures. If it is not able - * it will return false and the cachedFeatures state is undefined. + * and set the iterator to a valid iterator over the cached features. If it is not able + * it will return false. * * @param featureIterator A reference to a {@link QgsFeatureIterator}. A valid featureIterator will * be assigned in case this index is able to answer the request and the return diff --git a/src/core/qgscacheindexfeatureid.cpp b/src/core/qgscacheindexfeatureid.cpp index 496171ae857..9c916344922 100644 --- a/src/core/qgscacheindexfeatureid.cpp +++ b/src/core/qgscacheindexfeatureid.cpp @@ -42,12 +42,36 @@ void QgsCacheIndexFeatureId::requestCompleted( const QgsFeatureRequest& featureR bool QgsCacheIndexFeatureId::getCacheIterator( QgsFeatureIterator &featureIterator, const QgsFeatureRequest &featureRequest ) { - if ( featureRequest.filterType() == QgsFeatureRequest::FilterFid ) + switch ( featureRequest.filterType() ) { - if ( C->isFidCached( featureRequest.filterFid() ) ) + case QgsFeatureRequest::FilterFid: { - featureIterator = QgsFeatureIterator( new QgsCachedFeatureIterator( C, featureRequest ) ); - return true; + if ( C->isFidCached( featureRequest.filterFid() ) ) + { + featureIterator = QgsFeatureIterator( new QgsCachedFeatureIterator( C, featureRequest ) ); + return true; + } + break; + } + case QgsFeatureRequest::FilterFids: + { + if ( C->cachedFeatureIds().contains( featureRequest.filterFids() ) ) + { + featureIterator = QgsFeatureIterator( new QgsCachedFeatureIterator( C, featureRequest ) ); + return true; + } + break; + } + case QgsFeatureRequest::FilterNone: + case QgsFeatureRequest::FilterRect: + case QgsFeatureRequest::FilterExpression: + { + if ( C->hasFullCache() ) + { + featureIterator = QgsFeatureIterator( new QgsCachedFeatureIterator( C, featureRequest ) ); + return true; + } + break; } } diff --git a/src/core/qgscacheindexfeatureid.h b/src/core/qgscacheindexfeatureid.h index 29ab923db32..25a7483cacf 100644 --- a/src/core/qgscacheindexfeatureid.h +++ b/src/core/qgscacheindexfeatureid.h @@ -28,43 +28,9 @@ class CORE_EXPORT QgsCacheIndexFeatureId : public QgsAbstractCacheIndex public: QgsCacheIndexFeatureId( QgsVectorLayerCache* ); - /** - * Is called, whenever a feature is removed from the cache. You should update your indexes, so - * they become invalid in case this feature was required to successfuly answer a request. - */ virtual void flushFeature( const QgsFeatureId fid ) override; - - /** - * Sometimes, the whole cache changes its state and its easier to just withdraw everything. - * In this case, this method is issued. Be sure to clear all cache information in here. - */ virtual void flush() override; - - /** - * @brief - * Implement this method to update the the indices, in case you need information contained by the request - * to properly index. (E.g. spatial index) - * Does nothing by default - * - * @param featureRequest The feature request that was answered - * @param fids The feature ids that have been returned - */ virtual void requestCompleted( const QgsFeatureRequest& featureRequest, const QgsFeatureIds& fids ) override; - - /** - * Is called, when a feature request is issued on a cached layer. - * If this cache index is able to completely answer the feature request, it will return true - * and write the list of feature ids of cached features to cachedFeatures. If it is not able - * it will return false and the cachedFeatures state is undefined. - * - * @param featureIterator A reference to a {@link QgsFeatureIterator}. A valid featureIterator will - * be assigned in case this index is able to answer the request and the return - * value is true. - * @param featureRequest The feature request, for which this index is queried. - * - * @return True, if this index holds the information to answer the request. - * - */ virtual bool getCacheIterator( QgsFeatureIterator& featureIterator, const QgsFeatureRequest& featureRequest ) override; private: diff --git a/src/core/qgsvectorlayercache.cpp b/src/core/qgsvectorlayercache.cpp index 47e4f59db14..9d175f57cd5 100644 --- a/src/core/qgsvectorlayercache.cpp +++ b/src/core/qgsvectorlayercache.cpp @@ -364,7 +364,7 @@ QgsFeatureIterator QgsVectorLayerCache::getFeatures( const QgsFeatureRequest &fe return it; } -bool QgsVectorLayerCache::isFidCached( const QgsFeatureId fid ) +bool QgsVectorLayerCache::isFidCached( const QgsFeatureId fid ) const { return mCache.contains( fid ); } diff --git a/src/core/qgsvectorlayercache.h b/src/core/qgsvectorlayercache.h index 48f4808c729..5287da41119 100644 --- a/src/core/qgsvectorlayercache.h +++ b/src/core/qgsvectorlayercache.h @@ -205,8 +205,15 @@ class CORE_EXPORT QgsVectorLayerCache : public QObject * Check if a certain feature id is cached. * @param fid The feature id to look for * @return True if this id is in the cache + * @see cachedFeatureIds() */ - bool isFidCached( const QgsFeatureId fid ); + bool isFidCached( const QgsFeatureId fid ) const; + + /** Returns the set of feature IDs for features which are cached. + * @note added in QGIS 3.0 + * @see isFidCached() + */ + QgsFeatureIds cachedFeatureIds() const { return mCache.keys().toSet(); } /** * Gets the feature at the given feature id. Considers the changed, added, deleted and permanent features diff --git a/tests/src/core/testqgsvectorlayercache.cpp b/tests/src/core/testqgsvectorlayercache.cpp index 5687e991495..1cdf83eb464 100644 --- a/tests/src/core/testqgsvectorlayercache.cpp +++ b/tests/src/core/testqgsvectorlayercache.cpp @@ -289,6 +289,7 @@ void TestVectorLayerCache::testCanUseCacheForRequest() // get just the first feature into the cache it = cache.getFeatures( QgsFeatureRequest().setFilterFid( id1 ) ); while ( it.nextFeature( f ) ) { } + QCOMPARE( cache.cachedFeatureIds(), QgsFeatureIds() << id1 ); QVERIFY( cache.canUseCacheForRequest( QgsFeatureRequest().setFilterFid( id1 ), it ) ); //verify that the returned iterator was correct QVERIFY( it.nextFeature( f ) ); @@ -302,6 +303,7 @@ void TestVectorLayerCache::testCanUseCacheForRequest() // get feature 2 into cache it = cache.getFeatures( QgsFeatureRequest().setFilterFid( id2 ) ); while ( it.nextFeature( f ) ) { } + QCOMPARE( cache.cachedFeatureIds(), QgsFeatureIds() << id1 << id2 ); QVERIFY( cache.canUseCacheForRequest( QgsFeatureRequest().setFilterFid( id1 ), it ) ); QVERIFY( it.nextFeature( f ) ); QCOMPARE( f.id(), id1 );