Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Recognise that a cache can be filled using a FilterNone request
On behalf of Faunalia, sponsored by ENEL

(cherry-picked from afd5d1e)
  • Loading branch information
nyalldawson committed Nov 23, 2016
1 parent 0d6c2f0 commit e82f8a4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
9 changes: 9 additions & 0 deletions python/core/qgsvectorlayercache.sip
Expand Up @@ -65,9 +65,18 @@ class QgsVectorLayerCache : QObject
* be used for slow data sources, be aware, that the call to this method might take a long time.
*
* @param fullCache True: enable full caching, False: disable full caching
* @see hasFullCache()
*/
void setFullCache( bool fullCache );

/** Returns true if the cache is complete, ie it contains all features. This may happen as
* a result of a call to setFullCache() or by through a feature request which resulted in
* all available features being cached.
* @see setFullCache()
* @note added in QGIS 3.0
*/
bool hasFullCache() const;

/**
* @brief
* Adds a {@link QgsAbstractCacheIndex} to this cache. Cache indices know about features present
Expand Down
4 changes: 4 additions & 0 deletions src/core/qgsvectorlayercache.cpp
Expand Up @@ -180,6 +180,10 @@ void QgsVectorLayerCache::requestCompleted( const QgsFeatureRequest& featureRequ
{
idx->requestCompleted( featureRequest, fids );
}
if ( featureRequest.filterType() == QgsFeatureRequest::FilterNone )
{
mFullCache = true;
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/core/qgsvectorlayercache.h
Expand Up @@ -131,9 +131,18 @@ class CORE_EXPORT QgsVectorLayerCache : public QObject
* be used for slow data sources, be aware, that the call to this method might take a long time.
*
* @param fullCache True: enable full caching, False: disable full caching
* @see hasFullCache()
*/
void setFullCache( bool fullCache );

/** Returns true if the cache is complete, ie it contains all features. This may happen as
* a result of a call to setFullCache() or by through a feature request which resulted in
* all available features being cached.
* @see setFullCache()
* @note added in QGIS 3.0
*/
bool hasFullCache() const { return mFullCache; }

/**
* @brief
* Adds a {@link QgsAbstractCacheIndex} to this cache. Cache indices know about features present
Expand Down
48 changes: 48 additions & 0 deletions tests/src/core/testqgsvectorlayercache.cpp
Expand Up @@ -51,6 +51,8 @@ class TestVectorLayerCache : public QObject
void testCacheAttrActions(); // Test attribute add/ attribute delete
void testFeatureActions(); // Test adding/removing features works
void testSubsetRequest();
void testFullCache();
void testFullCacheThroughRequest();

void onCommittedFeaturesAdded( const QString&, const QgsFeatureList& );

Expand Down Expand Up @@ -218,6 +220,52 @@ void TestVectorLayerCache::testSubsetRequest()
QVERIFY( a == f.attribute( 3 ) );
}

void TestVectorLayerCache::testFullCache()
{
// cache is too small to fit all features
QgsVectorLayerCache cache( mPointsLayer, 2 );
QVERIFY( !cache.hasFullCache() );
QVERIFY( cache.cacheSize() < mPointsLayer->featureCount() );
// but we set it to full cache
cache.setFullCache( true );
// so now it should have sufficient size for all features
QVERIFY( cache.cacheSize() >= mPointsLayer->featureCount() );
QVERIFY( cache.hasFullCache() );

// double check that everything is indeed in the cache
QgsFeatureIterator it = mPointsLayer->getFeatures();
QgsFeature f;
while ( it.nextFeature( f ) )
{
QVERIFY( cache.isFidCached( f.id() ) );
}
}

void TestVectorLayerCache::testFullCacheThroughRequest()
{
// make sure cache is sufficient size for all features
QgsVectorLayerCache cache( mPointsLayer, mPointsLayer->featureCount() * 2 );
QVERIFY( !cache.hasFullCache() );

// now request all features from cache
QgsFeatureIterator it = cache.getFeatures( QgsFeatureRequest() );
QgsFeature f;
while ( it.nextFeature( f ) )
{
// suck in all features
}

// cache should now contain all features
it = mPointsLayer->getFeatures();
while ( it.nextFeature( f ) )
{
QVERIFY( cache.isFidCached( f.id() ) );
}

// so it should be a full cache!
QVERIFY( cache.hasFullCache() );
}

void TestVectorLayerCache::onCommittedFeaturesAdded( const QString& layerId, const QgsFeatureList& features )
{
Q_UNUSED( layerId )
Expand Down

0 comments on commit e82f8a4

Please sign in to comment.