Skip to content
Permalink
Browse files

QgsCacheIndexFeatureId can also handle non-FilterFid requests

in certain circumstances

On behalf of Faunalia, sponsored by ENEL

(cherry-picked from b5c1d0f)
  • Loading branch information
nyalldawson committed Nov 23, 2016
1 parent 31c1be1 commit f1930cbbf16017265d2e64125087e3c3fea504af
@@ -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 );
};
@@ -102,8 +102,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
@@ -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
@@ -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;
}
}

@@ -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:
@@ -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 );
}
@@ -168,8 +168,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
@@ -288,6 +288,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 ) );
@@ -301,6 +302,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 );

0 comments on commit f1930cb

Please sign in to comment.
You can’t perform that action at this time.