29
29
QgsFeaturePool::QgsFeaturePool ( QgsVectorLayer *layer )
30
30
: mFeatureCache( CACHE_SIZE )
31
31
, mLayer( layer )
32
- , mLayerId( layer->id () )
33
32
, mGeometryType( layer->geometryType () )
34
- , mCrs( layer->crs () )
33
+ , mFeatureSource( qgis::make_unique<QgsVectorLayerFeatureSource>( layer ) )
34
+ , mLayerName( layer->name () )
35
35
{
36
36
37
37
}
38
38
39
- bool QgsFeaturePool::getFeature ( QgsFeatureId id, QgsFeature &feature, QgsFeedback *feedback )
39
+ bool QgsFeaturePool::getFeature ( QgsFeatureId id, QgsFeature &feature )
40
40
{
41
41
// Why is there a write lock acquired here? Weird, we only want to read a feature from the cache, right?
42
42
// A method like `QCache::object(const Key &key) const` certainly would not modify its internals.
@@ -55,11 +55,9 @@ bool QgsFeaturePool::getFeature( QgsFeatureId id, QgsFeature &feature, QgsFeedba
55
55
}
56
56
else
57
57
{
58
- std::unique_ptr<QgsVectorLayerFeatureSource> source = QgsVectorLayerUtils::getFeatureSource ( mLayer , feedback );
59
-
60
58
// Feature not in cache, retrieve from layer
61
59
// TODO: avoid always querying all attributes (attribute values are needed when merging by attribute)
62
- if ( !source || !source ->getFeatures ( QgsFeatureRequest ( id ) ).nextFeature ( feature ) )
60
+ if ( !mFeatureSource ->getFeatures ( QgsFeatureRequest ( id ) ).nextFeature ( feature ) )
63
61
{
64
62
return false ;
65
63
}
@@ -72,15 +70,22 @@ bool QgsFeaturePool::getFeature( QgsFeatureId id, QgsFeature &feature, QgsFeedba
72
70
73
71
QgsFeatureIds QgsFeaturePool::getFeatures ( const QgsFeatureRequest &request, QgsFeedback *feedback )
74
72
{
73
+ QgsReadWriteLocker ( mCacheLock , QgsReadWriteLocker::Write );
74
+ Q_UNUSED ( feedback )
75
+ Q_ASSERT ( QThread::currentThread () == qApp->thread () );
76
+
77
+ mFeatureCache .clear ();
78
+ mIndex = QgsSpatialIndex ();
79
+
75
80
QgsFeatureIds fids;
76
81
77
- std::unique_ptr<QgsVectorLayerFeatureSource> source = QgsVectorLayerUtils::getFeatureSource ( mLayer , feedback );
82
+ mFeatureSource = qgis::make_unique<QgsVectorLayerFeatureSource> ( mLayer );
78
83
79
- QgsFeatureIterator it = source ->getFeatures ( request );
84
+ QgsFeatureIterator it = mFeatureSource ->getFeatures ( request );
80
85
QgsFeature feature;
81
86
while ( it.nextFeature ( feature ) )
82
87
{
83
- insertFeature ( feature );
88
+ insertFeature ( feature, true );
84
89
fids << feature.id ();
85
90
}
86
91
@@ -111,9 +116,11 @@ QPointer<QgsVectorLayer> QgsFeaturePool::layerPtr() const
111
116
return mLayer ;
112
117
}
113
118
114
- void QgsFeaturePool::insertFeature ( const QgsFeature &feature )
119
+ void QgsFeaturePool::insertFeature ( const QgsFeature &feature, bool skipLock )
115
120
{
116
- QgsReadWriteLocker locker ( mCacheLock , QgsReadWriteLocker::Write );
121
+ QgsReadWriteLocker locker ( mCacheLock , QgsReadWriteLocker::Unlocked );
122
+ if ( !skipLock )
123
+ locker.changeMode ( QgsReadWriteLocker::Write );
117
124
mFeatureCache .insert ( feature.id (), new QgsFeature ( feature ) );
118
125
QgsFeature indexFeature ( feature );
119
126
mIndex .addFeature ( indexFeature );
@@ -150,12 +157,19 @@ void QgsFeaturePool::setFeatureIds( const QgsFeatureIds &ids )
150
157
151
158
bool QgsFeaturePool::isFeatureCached ( QgsFeatureId fid )
152
159
{
160
+ QgsReadWriteLocker locker ( mCacheLock , QgsReadWriteLocker::Read );
153
161
return mFeatureCache .contains ( fid );
154
162
}
155
163
164
+ QString QgsFeaturePool::layerName () const
165
+ {
166
+ return mLayerName ;
167
+ }
168
+
156
169
QgsCoordinateReferenceSystem QgsFeaturePool::crs () const
157
170
{
158
- return mCrs ;
171
+ QgsReadWriteLocker ( mCacheLock , QgsReadWriteLocker::Read );
172
+ return mFeatureSource ->crs ();
159
173
}
160
174
161
175
QgsWkbTypes::GeometryType QgsFeaturePool::geometryType () const
@@ -165,5 +179,6 @@ QgsWkbTypes::GeometryType QgsFeaturePool::geometryType() const
165
179
166
180
QString QgsFeaturePool::layerId () const
167
181
{
168
- return mLayerId ;
182
+ QgsReadWriteLocker ( mCacheLock , QgsReadWriteLocker::Read );
183
+ return mFeatureSource ->id ();
169
184
}
0 commit comments