Skip to content

Commit afd5d1e

Browse files
committedNov 15, 2016
Recognise that a cache can be filled using a FilterNone request
On behalf of Faunalia, sponsored by ENEL
1 parent 38a4aac commit afd5d1e

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed
 

‎python/core/qgsvectorlayercache.sip

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,18 @@ class QgsVectorLayerCache : QObject
6565
* be used for slow data sources, be aware, that the call to this method might take a long time.
6666
*
6767
* @param fullCache True: enable full caching, False: disable full caching
68+
* @see hasFullCache()
6869
*/
6970
void setFullCache( bool fullCache );
7071

72+
/** Returns true if the cache is complete, ie it contains all features. This may happen as
73+
* a result of a call to setFullCache() or by through a feature request which resulted in
74+
* all available features being cached.
75+
* @see setFullCache()
76+
* @note added in QGIS 3.0
77+
*/
78+
bool hasFullCache() const;
79+
7180
/**
7281
* @brief
7382
* Adds a {@link QgsAbstractCacheIndex} to this cache. Cache indices know about features present

‎src/core/qgsvectorlayercache.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ void QgsVectorLayerCache::requestCompleted( const QgsFeatureRequest& featureRequ
180180
{
181181
idx->requestCompleted( featureRequest, fids );
182182
}
183+
if ( featureRequest.filterType() == QgsFeatureRequest::FilterNone )
184+
{
185+
mFullCache = true;
186+
}
183187
}
184188
}
185189

‎src/core/qgsvectorlayercache.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,18 @@ class CORE_EXPORT QgsVectorLayerCache : public QObject
133133
* be used for slow data sources, be aware, that the call to this method might take a long time.
134134
*
135135
* @param fullCache True: enable full caching, False: disable full caching
136+
* @see hasFullCache()
136137
*/
137138
void setFullCache( bool fullCache );
138139

140+
/** Returns true if the cache is complete, ie it contains all features. This may happen as
141+
* a result of a call to setFullCache() or by through a feature request which resulted in
142+
* all available features being cached.
143+
* @see setFullCache()
144+
* @note added in QGIS 3.0
145+
*/
146+
bool hasFullCache() const { return mFullCache; }
147+
139148
/**
140149
* @brief
141150
* Adds a {@link QgsAbstractCacheIndex} to this cache. Cache indices know about features present

‎tests/src/core/testqgsvectorlayercache.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class TestVectorLayerCache : public QObject
5252
void testCacheAttrActions(); // Test attribute add/ attribute delete
5353
void testFeatureActions(); // Test adding/removing features works
5454
void testSubsetRequest();
55+
void testFullCache();
56+
void testFullCacheThroughRequest();
5557

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

@@ -219,6 +221,52 @@ void TestVectorLayerCache::testSubsetRequest()
219221
QVERIFY( a == f.attribute( 3 ) );
220222
}
221223

224+
void TestVectorLayerCache::testFullCache()
225+
{
226+
// cache is too small to fit all features
227+
QgsVectorLayerCache cache( mPointsLayer, 2 );
228+
QVERIFY( !cache.hasFullCache() );
229+
QVERIFY( cache.cacheSize() < mPointsLayer->featureCount() );
230+
// but we set it to full cache
231+
cache.setFullCache( true );
232+
// so now it should have sufficient size for all features
233+
QVERIFY( cache.cacheSize() >= mPointsLayer->featureCount() );
234+
QVERIFY( cache.hasFullCache() );
235+
236+
// double check that everything is indeed in the cache
237+
QgsFeatureIterator it = mPointsLayer->getFeatures();
238+
QgsFeature f;
239+
while ( it.nextFeature( f ) )
240+
{
241+
QVERIFY( cache.isFidCached( f.id() ) );
242+
}
243+
}
244+
245+
void TestVectorLayerCache::testFullCacheThroughRequest()
246+
{
247+
// make sure cache is sufficient size for all features
248+
QgsVectorLayerCache cache( mPointsLayer, mPointsLayer->featureCount() * 2 );
249+
QVERIFY( !cache.hasFullCache() );
250+
251+
// now request all features from cache
252+
QgsFeatureIterator it = cache.getFeatures( QgsFeatureRequest() );
253+
QgsFeature f;
254+
while ( it.nextFeature( f ) )
255+
{
256+
// suck in all features
257+
}
258+
259+
// cache should now contain all features
260+
it = mPointsLayer->getFeatures();
261+
while ( it.nextFeature( f ) )
262+
{
263+
QVERIFY( cache.isFidCached( f.id() ) );
264+
}
265+
266+
// so it should be a full cache!
267+
QVERIFY( cache.hasFullCache() );
268+
}
269+
222270
void TestVectorLayerCache::onCommittedFeaturesAdded( const QString& layerId, const QgsFeatureList& features )
223271
{
224272
Q_UNUSED( layerId )

0 commit comments

Comments
 (0)
Please sign in to comment.