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