@@ -53,6 +53,7 @@ class TestVectorLayerCache : public QObject
53
53
void testSubsetRequest ();
54
54
void testFullCache ();
55
55
void testFullCacheThroughRequest ();
56
+ void testCanUseCacheForRequest ();
56
57
57
58
void onCommittedFeaturesAdded ( const QString&, const QgsFeatureList& );
58
59
@@ -266,6 +267,67 @@ void TestVectorLayerCache::testFullCacheThroughRequest()
266
267
QVERIFY ( cache.hasFullCache () );
267
268
}
268
269
270
+ void TestVectorLayerCache::testCanUseCacheForRequest ()
271
+ {
272
+ // first get some feature ids from layer
273
+ QgsFeature f;
274
+ QgsFeatureIterator it = mPointsLayer ->getFeatures ();
275
+ it.nextFeature ( f );
276
+ QgsFeatureId id1 = f.id ();
277
+ it.nextFeature ( f );
278
+ QgsFeatureId id2 = f.id ();
279
+
280
+ QgsVectorLayerCache cache ( mPointsLayer , 10 );
281
+ // initially nothing in cache, so can't use it to fulfill the request
282
+ QVERIFY ( !cache.canUseCacheForRequest ( QgsFeatureRequest ().setFilterFid ( id1 ), it ) );
283
+ QVERIFY ( !cache.canUseCacheForRequest ( QgsFeatureRequest ().setFilterFid ( id2 ), it ) );
284
+ QVERIFY ( !cache.canUseCacheForRequest ( QgsFeatureRequest ().setFilterFids ( QgsFeatureIds () << id1 << id2 ), it ) );
285
+ QVERIFY ( !cache.canUseCacheForRequest ( QgsFeatureRequest ().setFilterRect ( QgsRectangle ( 1 , 2 , 3 , 4 ) ), it ) );
286
+ QVERIFY ( !cache.canUseCacheForRequest ( QgsFeatureRequest ().setFilterExpression ( " $x<5" ), it ) );
287
+
288
+ // get just the first feature into the cache
289
+ it = cache.getFeatures ( QgsFeatureRequest ().setFilterFid ( id1 ) );
290
+ while ( it.nextFeature ( f ) ) { }
291
+ QVERIFY ( cache.canUseCacheForRequest ( QgsFeatureRequest ().setFilterFid ( id1 ), it ) );
292
+ // verify that the returned iterator was correct
293
+ QVERIFY ( it.nextFeature ( f ) );
294
+ QCOMPARE ( f.id (), id1 );
295
+ QVERIFY ( !it.nextFeature ( f ) );
296
+ QVERIFY ( !cache.canUseCacheForRequest ( QgsFeatureRequest ().setFilterFid ( id2 ), it ) );
297
+ QVERIFY ( !cache.canUseCacheForRequest ( QgsFeatureRequest ().setFilterFids ( QgsFeatureIds () << id1 << id2 ), it ) );
298
+ QVERIFY ( !cache.canUseCacheForRequest ( QgsFeatureRequest ().setFilterRect ( QgsRectangle ( 1 , 2 , 3 , 4 ) ), it ) );
299
+ QVERIFY ( !cache.canUseCacheForRequest ( QgsFeatureRequest ().setFilterExpression ( " $x<5" ), it ) );
300
+
301
+ // get feature 2 into cache
302
+ it = cache.getFeatures ( QgsFeatureRequest ().setFilterFid ( id2 ) );
303
+ while ( it.nextFeature ( f ) ) { }
304
+ QVERIFY ( cache.canUseCacheForRequest ( QgsFeatureRequest ().setFilterFid ( id1 ), it ) );
305
+ QVERIFY ( it.nextFeature ( f ) );
306
+ QCOMPARE ( f.id (), id1 );
307
+ QVERIFY ( !it.nextFeature ( f ) );
308
+ QVERIFY ( cache.canUseCacheForRequest ( QgsFeatureRequest ().setFilterFid ( id2 ), it ) );
309
+ QVERIFY ( it.nextFeature ( f ) );
310
+ QCOMPARE ( f.id (), id2 );
311
+ QVERIFY ( !it.nextFeature ( f ) );
312
+ QVERIFY ( cache.canUseCacheForRequest ( QgsFeatureRequest ().setFilterFids ( QgsFeatureIds () << id1 << id2 ), it ) );
313
+ QVERIFY ( it.nextFeature ( f ) );
314
+ QgsFeatureIds result;
315
+ result << f.id ();
316
+ QVERIFY ( it.nextFeature ( f ) );
317
+ result << f.id ();
318
+ QCOMPARE ( result, QgsFeatureIds () << id1 << id2 );
319
+ QVERIFY ( !cache.canUseCacheForRequest ( QgsFeatureRequest ().setFilterRect ( QgsRectangle ( 1 , 2 , 3 , 4 ) ), it ) );
320
+ QVERIFY ( !cache.canUseCacheForRequest ( QgsFeatureRequest ().setFilterExpression ( " $x<5" ), it ) );
321
+
322
+ // can only use rect/expression requests if cache has everything
323
+ cache.setFullCache ( true );
324
+ QVERIFY ( cache.canUseCacheForRequest ( QgsFeatureRequest ().setFilterFid ( id1 ), it ) );
325
+ QVERIFY ( cache.canUseCacheForRequest ( QgsFeatureRequest ().setFilterFid ( id2 ), it ) );
326
+ QVERIFY ( cache.canUseCacheForRequest ( QgsFeatureRequest ().setFilterFids ( QgsFeatureIds () << id1 << id2 ), it ) );
327
+ QVERIFY ( cache.canUseCacheForRequest ( QgsFeatureRequest ().setFilterRect ( QgsRectangle ( 1 , 2 , 3 , 4 ) ), it ) );
328
+ QVERIFY ( cache.canUseCacheForRequest ( QgsFeatureRequest ().setFilterExpression ( " $x<5" ), it ) );
329
+ }
330
+
269
331
void TestVectorLayerCache::onCommittedFeaturesAdded ( const QString& layerId, const QgsFeatureList& features )
270
332
{
271
333
Q_UNUSED ( layerId )
0 commit comments