mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-16 00:03:12 -04:00
QgsCacheIndexFeatureId can also handle non-FilterFid requests
in certain circumstances On behalf of Faunalia, sponsored by ENEL
This commit is contained in:
parent
53460232bb
commit
b5c1d0f24e
@ -6,42 +6,8 @@ class QgsCacheIndexFeatureId : QgsAbstractCacheIndex
|
|||||||
public:
|
public:
|
||||||
QgsCacheIndexFeatureId( QgsVectorLayerCache* );
|
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 );
|
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();
|
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 );
|
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 );
|
virtual bool getCacheIterator( QgsFeatureIterator& featureIterator, const QgsFeatureRequest& featureRequest );
|
||||||
};
|
};
|
||||||
|
@ -123,8 +123,15 @@ class QgsVectorLayerCache : QObject
|
|||||||
* Check if a certain feature id is cached.
|
* Check if a certain feature id is cached.
|
||||||
* @param fid The feature id to look for
|
* @param fid The feature id to look for
|
||||||
* @return True if this id is in the cache
|
* @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
|
* 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.
|
* 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
|
* 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
|
* and set the iterator to a valid iterator over the cached features. If it is not able
|
||||||
* it will return false and the cachedFeatures state is undefined.
|
* it will return false.
|
||||||
*
|
*
|
||||||
* @param featureIterator A reference to a {@link QgsFeatureIterator}. A valid featureIterator will
|
* @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
|
* be assigned in case this index is able to answer the request and the return
|
||||||
|
@ -42,13 +42,37 @@ void QgsCacheIndexFeatureId::requestCompleted( const QgsFeatureRequest& featureR
|
|||||||
|
|
||||||
bool QgsCacheIndexFeatureId::getCacheIterator( QgsFeatureIterator &featureIterator, const QgsFeatureRequest &featureRequest )
|
bool QgsCacheIndexFeatureId::getCacheIterator( QgsFeatureIterator &featureIterator, const QgsFeatureRequest &featureRequest )
|
||||||
{
|
{
|
||||||
if ( featureRequest.filterType() == QgsFeatureRequest::FilterFid )
|
switch ( featureRequest.filterType() )
|
||||||
|
{
|
||||||
|
case QgsFeatureRequest::FilterFid:
|
||||||
{
|
{
|
||||||
if ( C->isFidCached( featureRequest.filterFid() ) )
|
if ( C->isFidCached( featureRequest.filterFid() ) )
|
||||||
{
|
{
|
||||||
featureIterator = QgsFeatureIterator( new QgsCachedFeatureIterator( C, featureRequest ) );
|
featureIterator = QgsFeatureIterator( new QgsCachedFeatureIterator( C, featureRequest ) );
|
||||||
return true;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
@ -28,43 +28,9 @@ class CORE_EXPORT QgsCacheIndexFeatureId : public QgsAbstractCacheIndex
|
|||||||
public:
|
public:
|
||||||
QgsCacheIndexFeatureId( QgsVectorLayerCache* );
|
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;
|
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;
|
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;
|
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;
|
virtual bool getCacheIterator( QgsFeatureIterator& featureIterator, const QgsFeatureRequest& featureRequest ) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -364,7 +364,7 @@ QgsFeatureIterator QgsVectorLayerCache::getFeatures( const QgsFeatureRequest &fe
|
|||||||
return it;
|
return it;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QgsVectorLayerCache::isFidCached( const QgsFeatureId fid )
|
bool QgsVectorLayerCache::isFidCached( const QgsFeatureId fid ) const
|
||||||
{
|
{
|
||||||
return mCache.contains( fid );
|
return mCache.contains( fid );
|
||||||
}
|
}
|
||||||
|
@ -205,8 +205,15 @@ class CORE_EXPORT QgsVectorLayerCache : public QObject
|
|||||||
* Check if a certain feature id is cached.
|
* Check if a certain feature id is cached.
|
||||||
* @param fid The feature id to look for
|
* @param fid The feature id to look for
|
||||||
* @return True if this id is in the cache
|
* @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
|
* Gets the feature at the given feature id. Considers the changed, added, deleted and permanent features
|
||||||
|
@ -289,6 +289,7 @@ void TestVectorLayerCache::testCanUseCacheForRequest()
|
|||||||
// get just the first feature into the cache
|
// get just the first feature into the cache
|
||||||
it = cache.getFeatures( QgsFeatureRequest().setFilterFid( id1 ) );
|
it = cache.getFeatures( QgsFeatureRequest().setFilterFid( id1 ) );
|
||||||
while ( it.nextFeature( f ) ) { }
|
while ( it.nextFeature( f ) ) { }
|
||||||
|
QCOMPARE( cache.cachedFeatureIds(), QgsFeatureIds() << id1 );
|
||||||
QVERIFY( cache.canUseCacheForRequest( QgsFeatureRequest().setFilterFid( id1 ), it ) );
|
QVERIFY( cache.canUseCacheForRequest( QgsFeatureRequest().setFilterFid( id1 ), it ) );
|
||||||
//verify that the returned iterator was correct
|
//verify that the returned iterator was correct
|
||||||
QVERIFY( it.nextFeature( f ) );
|
QVERIFY( it.nextFeature( f ) );
|
||||||
@ -302,6 +303,7 @@ void TestVectorLayerCache::testCanUseCacheForRequest()
|
|||||||
// get feature 2 into cache
|
// get feature 2 into cache
|
||||||
it = cache.getFeatures( QgsFeatureRequest().setFilterFid( id2 ) );
|
it = cache.getFeatures( QgsFeatureRequest().setFilterFid( id2 ) );
|
||||||
while ( it.nextFeature( f ) ) { }
|
while ( it.nextFeature( f ) ) { }
|
||||||
|
QCOMPARE( cache.cachedFeatureIds(), QgsFeatureIds() << id1 << id2 );
|
||||||
QVERIFY( cache.canUseCacheForRequest( QgsFeatureRequest().setFilterFid( id1 ), it ) );
|
QVERIFY( cache.canUseCacheForRequest( QgsFeatureRequest().setFilterFid( id1 ), it ) );
|
||||||
QVERIFY( it.nextFeature( f ) );
|
QVERIFY( it.nextFeature( f ) );
|
||||||
QCOMPARE( f.id(), id1 );
|
QCOMPARE( f.id(), id1 );
|
||||||
|
Loading…
x
Reference in New Issue
Block a user