Skip to content

Commit f1930cb

Browse files
committedNov 23, 2016
QgsCacheIndexFeatureId can also handle non-FilterFid requests
in certain circumstances On behalf of Faunalia, sponsored by ENEL (cherry-picked from b5c1d0f)
1 parent 31c1be1 commit f1930cb

File tree

8 files changed

+49
-77
lines changed

8 files changed

+49
-77
lines changed
 

‎python/core/qgscacheindexfeatureid.sip

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,8 @@ class QgsCacheIndexFeatureId : QgsAbstractCacheIndex
66
public:
77
QgsCacheIndexFeatureId( QgsVectorLayerCache* );
88

9-
/**
10-
* Is called, whenever a feature is removed from the cache. You should update your indexes, so
11-
* they become invalid in case this feature was required to successfuly answer a request.
12-
*/
139
virtual void flushFeature( const QgsFeatureId fid );
14-
15-
/**
16-
* Sometimes, the whole cache changes its state and its easier to just withdraw everything.
17-
* In this case, this method is issued. Be sure to clear all cache information in here.
18-
*/
1910
virtual void flush();
20-
21-
/**
22-
* @brief
23-
* Implement this method to update the the indices, in case you need information contained by the request
24-
* to properly index. (E.g. spatial index)
25-
* Does nothing by default
26-
*
27-
* @param featureRequest The feature request that was answered
28-
* @param fids The feature ids that have been returned
29-
*/
3011
virtual void requestCompleted( const QgsFeatureRequest& featureRequest, const QgsFeatureIds& fids );
31-
32-
/**
33-
* Is called, when a feature request is issued on a cached layer.
34-
* If this cache index is able to completely answer the feature request, it will return true
35-
* and write the list of feature ids of cached features to cachedFeatures. If it is not able
36-
* it will return false and the cachedFeatures state is undefined.
37-
*
38-
* @param featureIterator A reference to a {@link QgsFeatureIterator}. A valid featureIterator will
39-
* be assigned in case this index is able to answer the request and the return
40-
* value is true.
41-
* @param featureRequest The feature request, for which this index is queried.
42-
*
43-
* @return True, if this index holds the information to answer the request.
44-
*
45-
*/
4612
virtual bool getCacheIterator( QgsFeatureIterator& featureIterator, const QgsFeatureRequest& featureRequest );
4713
};

‎python/core/qgsvectorlayercache.sip

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,15 @@ class QgsVectorLayerCache : QObject
102102
* Check if a certain feature id is cached.
103103
* @param fid The feature id to look for
104104
* @return True if this id is in the cache
105+
* @see cachedFeatureIds()
105106
*/
106-
bool isFidCached( const QgsFeatureId fid );
107+
bool isFidCached( const QgsFeatureId fid ) const;
108+
109+
/** Returns the set of feature IDs for features which are cached.
110+
* @note added in QGIS 3.0
111+
* @see isFidCached()
112+
*/
113+
QgsFeatureIds cachedFeatureIds() const;
107114

108115
/**
109116
* Gets the feature at the given feature id. Considers the changed, added, deleted and permanent features

‎src/core/qgscacheindex.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ class CORE_EXPORT QgsAbstractCacheIndex
5858
/**
5959
* Is called, when a feature request is issued on a cached layer.
6060
* If this cache index is able to completely answer the feature request, it will return true
61-
* and write the list of feature ids of cached features to cachedFeatures. If it is not able
62-
* it will return false and the cachedFeatures state is undefined.
61+
* and set the iterator to a valid iterator over the cached features. If it is not able
62+
* it will return false.
6363
*
6464
* @param featureIterator A reference to a {@link QgsFeatureIterator}. A valid featureIterator will
6565
* be assigned in case this index is able to answer the request and the return

‎src/core/qgscacheindexfeatureid.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,36 @@ void QgsCacheIndexFeatureId::requestCompleted( const QgsFeatureRequest& featureR
4242

4343
bool QgsCacheIndexFeatureId::getCacheIterator( QgsFeatureIterator &featureIterator, const QgsFeatureRequest &featureRequest )
4444
{
45-
if ( featureRequest.filterType() == QgsFeatureRequest::FilterFid )
45+
switch ( featureRequest.filterType() )
4646
{
47-
if ( C->isFidCached( featureRequest.filterFid() ) )
47+
case QgsFeatureRequest::FilterFid:
4848
{
49-
featureIterator = QgsFeatureIterator( new QgsCachedFeatureIterator( C, featureRequest ) );
50-
return true;
49+
if ( C->isFidCached( featureRequest.filterFid() ) )
50+
{
51+
featureIterator = QgsFeatureIterator( new QgsCachedFeatureIterator( C, featureRequest ) );
52+
return true;
53+
}
54+
break;
55+
}
56+
case QgsFeatureRequest::FilterFids:
57+
{
58+
if ( C->cachedFeatureIds().contains( featureRequest.filterFids() ) )
59+
{
60+
featureIterator = QgsFeatureIterator( new QgsCachedFeatureIterator( C, featureRequest ) );
61+
return true;
62+
}
63+
break;
64+
}
65+
case QgsFeatureRequest::FilterNone:
66+
case QgsFeatureRequest::FilterRect:
67+
case QgsFeatureRequest::FilterExpression:
68+
{
69+
if ( C->hasFullCache() )
70+
{
71+
featureIterator = QgsFeatureIterator( new QgsCachedFeatureIterator( C, featureRequest ) );
72+
return true;
73+
}
74+
break;
5175
}
5276
}
5377

‎src/core/qgscacheindexfeatureid.h

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,43 +28,9 @@ class CORE_EXPORT QgsCacheIndexFeatureId : public QgsAbstractCacheIndex
2828
public:
2929
QgsCacheIndexFeatureId( QgsVectorLayerCache* );
3030

31-
/**
32-
* Is called, whenever a feature is removed from the cache. You should update your indexes, so
33-
* they become invalid in case this feature was required to successfuly answer a request.
34-
*/
3531
virtual void flushFeature( const QgsFeatureId fid ) override;
36-
37-
/**
38-
* Sometimes, the whole cache changes its state and its easier to just withdraw everything.
39-
* In this case, this method is issued. Be sure to clear all cache information in here.
40-
*/
4132
virtual void flush() override;
42-
43-
/**
44-
* @brief
45-
* Implement this method to update the the indices, in case you need information contained by the request
46-
* to properly index. (E.g. spatial index)
47-
* Does nothing by default
48-
*
49-
* @param featureRequest The feature request that was answered
50-
* @param fids The feature ids that have been returned
51-
*/
5233
virtual void requestCompleted( const QgsFeatureRequest& featureRequest, const QgsFeatureIds& fids ) override;
53-
54-
/**
55-
* Is called, when a feature request is issued on a cached layer.
56-
* If this cache index is able to completely answer the feature request, it will return true
57-
* and write the list of feature ids of cached features to cachedFeatures. If it is not able
58-
* it will return false and the cachedFeatures state is undefined.
59-
*
60-
* @param featureIterator A reference to a {@link QgsFeatureIterator}. A valid featureIterator will
61-
* be assigned in case this index is able to answer the request and the return
62-
* value is true.
63-
* @param featureRequest The feature request, for which this index is queried.
64-
*
65-
* @return True, if this index holds the information to answer the request.
66-
*
67-
*/
6834
virtual bool getCacheIterator( QgsFeatureIterator& featureIterator, const QgsFeatureRequest& featureRequest ) override;
6935

7036
private:

‎src/core/qgsvectorlayercache.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ QgsFeatureIterator QgsVectorLayerCache::getFeatures( const QgsFeatureRequest &fe
364364
return it;
365365
}
366366

367-
bool QgsVectorLayerCache::isFidCached( const QgsFeatureId fid )
367+
bool QgsVectorLayerCache::isFidCached( const QgsFeatureId fid ) const
368368
{
369369
return mCache.contains( fid );
370370
}

‎src/core/qgsvectorlayercache.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,15 @@ class CORE_EXPORT QgsVectorLayerCache : public QObject
168168
* Check if a certain feature id is cached.
169169
* @param fid The feature id to look for
170170
* @return True if this id is in the cache
171+
* @see cachedFeatureIds()
171172
*/
172-
bool isFidCached( const QgsFeatureId fid );
173+
bool isFidCached( const QgsFeatureId fid ) const;
174+
175+
/** Returns the set of feature IDs for features which are cached.
176+
* @note added in QGIS 3.0
177+
* @see isFidCached()
178+
*/
179+
QgsFeatureIds cachedFeatureIds() const { return mCache.keys().toSet(); }
173180

174181
/**
175182
* Gets the feature at the given feature id. Considers the changed, added, deleted and permanent features

‎tests/src/core/testqgsvectorlayercache.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ void TestVectorLayerCache::testCanUseCacheForRequest()
288288
// get just the first feature into the cache
289289
it = cache.getFeatures( QgsFeatureRequest().setFilterFid( id1 ) );
290290
while ( it.nextFeature( f ) ) { }
291+
QCOMPARE( cache.cachedFeatureIds(), QgsFeatureIds() << id1 );
291292
QVERIFY( cache.canUseCacheForRequest( QgsFeatureRequest().setFilterFid( id1 ), it ) );
292293
//verify that the returned iterator was correct
293294
QVERIFY( it.nextFeature( f ) );
@@ -301,6 +302,7 @@ void TestVectorLayerCache::testCanUseCacheForRequest()
301302
// get feature 2 into cache
302303
it = cache.getFeatures( QgsFeatureRequest().setFilterFid( id2 ) );
303304
while ( it.nextFeature( f ) ) { }
305+
QCOMPARE( cache.cachedFeatureIds(), QgsFeatureIds() << id1 << id2 );
304306
QVERIFY( cache.canUseCacheForRequest( QgsFeatureRequest().setFilterFid( id1 ), it ) );
305307
QVERIFY( it.nextFeature( f ) );
306308
QCOMPARE( f.id(), id1 );

0 commit comments

Comments
 (0)
Please sign in to comment.