/
qgsvectorlayercache.cpp
409 lines (348 loc) · 11 KB
/
qgsvectorlayercache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/***************************************************************************
qgsvectorlayercache.cpp
Cache features of a vector layer
-------------------
begin : January 2013
copyright : (C) Matthias Kuhn
email : matthias at opengis dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsvectorlayercache.h"
#include "qgscacheindex.h"
#include "qgscachedfeatureiterator.h"
QgsVectorLayerCache::QgsVectorLayerCache( QgsVectorLayer *layer, int cacheSize, QObject *parent )
: QObject( parent )
, mLayer( layer )
, mFullCache( false )
{
mCache.setMaxCost( cacheSize );
connect( mLayer, &QgsVectorLayer::featureDeleted, this, &QgsVectorLayerCache::featureDeleted );
connect( mLayer, &QgsVectorLayer::featureAdded, this, &QgsVectorLayerCache::onFeatureAdded );
connect( mLayer, &QgsVectorLayer::destroyed, this, &QgsVectorLayerCache::layerDeleted );
setCacheGeometry( true );
setCacheSubsetOfAttributes( mLayer->attributeList() );
setCacheAddedAttributes( true );
connect( mLayer, &QgsVectorLayer::attributeDeleted, this, &QgsVectorLayerCache::attributeDeleted );
connect( mLayer, &QgsVectorLayer::updatedFields, this, &QgsVectorLayerCache::invalidate );
connect( mLayer, &QgsVectorLayer::dataChanged, this, &QgsVectorLayerCache::invalidate );
connect( mLayer, &QgsVectorLayer::attributeValueChanged, this, &QgsVectorLayerCache::onAttributeValueChanged );
}
QgsVectorLayerCache::~QgsVectorLayerCache()
{
qDeleteAll( mCacheIndices );
mCacheIndices.clear();
}
void QgsVectorLayerCache::setCacheSize( int cacheSize )
{
mCache.setMaxCost( cacheSize );
}
int QgsVectorLayerCache::cacheSize()
{
return mCache.maxCost();
}
void QgsVectorLayerCache::setCacheGeometry( bool cacheGeometry )
{
bool shouldCacheGeometry = cacheGeometry && mLayer->hasGeometryType();
bool mustInvalidate = shouldCacheGeometry && !mCacheGeometry; // going from no geometry -> geometry, so have to clear existing cache entries
mCacheGeometry = shouldCacheGeometry;
if ( cacheGeometry )
{
connect( mLayer, &QgsVectorLayer::geometryChanged, this, &QgsVectorLayerCache::geometryChanged );
}
else
{
disconnect( mLayer, &QgsVectorLayer::geometryChanged, this, &QgsVectorLayerCache::geometryChanged );
}
if ( mustInvalidate )
{
invalidate();
}
}
void QgsVectorLayerCache::setCacheSubsetOfAttributes( const QgsAttributeList &attributes )
{
mCachedAttributes = attributes;
}
void QgsVectorLayerCache::setFullCache( bool fullCache )
{
mFullCache = fullCache;
if ( mFullCache )
{
// Add a little more than necessary...
setCacheSize( mLayer->featureCount() + 100 );
// Initialize the cache...
QgsFeatureIterator it( new QgsCachedFeatureWriterIterator( this, QgsFeatureRequest()
.setSubsetOfAttributes( mCachedAttributes )
.setFlags( mCacheGeometry ? QgsFeatureRequest::NoFlags : QgsFeatureRequest::NoGeometry ) ) );
int i = 0;
QTime t;
t.start();
QgsFeature f;
while ( it.nextFeature( f ) )
{
++i;
if ( t.elapsed() > 1000 )
{
bool cancel = false;
emit progress( i, cancel );
if ( cancel )
break;
t.restart();
}
}
it.close();
emit finished();
}
}
void QgsVectorLayerCache::addCacheIndex( QgsAbstractCacheIndex *cacheIndex )
{
mCacheIndices.append( cacheIndex );
}
void QgsVectorLayerCache::setCacheAddedAttributes( bool cacheAddedAttributes )
{
if ( cacheAddedAttributes )
{
connect( mLayer, &QgsVectorLayer::attributeAdded, this, &QgsVectorLayerCache::attributeAdded );
}
else
{
disconnect( mLayer, &QgsVectorLayer::attributeAdded, this, &QgsVectorLayerCache::attributeAdded );
}
}
bool QgsVectorLayerCache::featureAtId( QgsFeatureId featureId, QgsFeature &feature, bool skipCache )
{
bool featureFound = false;
QgsCachedFeature *cachedFeature = nullptr;
if ( !skipCache )
{
cachedFeature = mCache[ featureId ];
}
if ( cachedFeature )
{
feature = QgsFeature( *cachedFeature->feature() );
featureFound = true;
}
else if ( mLayer->getFeatures( QgsFeatureRequest()
.setFilterFid( featureId )
.setSubsetOfAttributes( mCachedAttributes )
.setFlags( !mCacheGeometry ? QgsFeatureRequest::NoGeometry : QgsFeatureRequest::Flags( nullptr ) ) )
.nextFeature( feature ) )
{
cacheFeature( feature );
featureFound = true;
}
return featureFound;
}
bool QgsVectorLayerCache::removeCachedFeature( QgsFeatureId fid )
{
return mCache.remove( fid );
}
QgsVectorLayer *QgsVectorLayerCache::layer()
{
return mLayer;
}
void QgsVectorLayerCache::requestCompleted( const QgsFeatureRequest &featureRequest, const QgsFeatureIds &fids )
{
// If a request is too large for the cache don't notify to prevent from indexing incomplete requests
if ( fids.count() <= mCache.size() )
{
Q_FOREACH ( QgsAbstractCacheIndex *idx, mCacheIndices )
{
idx->requestCompleted( featureRequest, fids );
}
if ( featureRequest.filterType() == QgsFeatureRequest::FilterNone )
{
mFullCache = true;
}
}
}
void QgsVectorLayerCache::featureRemoved( QgsFeatureId fid )
{
Q_FOREACH ( QgsAbstractCacheIndex *idx, mCacheIndices )
{
idx->flushFeature( fid );
}
}
void QgsVectorLayerCache::onAttributeValueChanged( QgsFeatureId fid, int field, const QVariant &value )
{
QgsCachedFeature *cachedFeat = mCache[ fid ];
if ( cachedFeat )
{
cachedFeat->mFeature->setAttribute( field, value );
}
emit attributeValueChanged( fid, field, value );
}
void QgsVectorLayerCache::featureDeleted( QgsFeatureId fid )
{
mCache.remove( fid );
}
void QgsVectorLayerCache::onFeatureAdded( QgsFeatureId fid )
{
if ( mFullCache )
{
if ( cacheSize() <= mLayer->featureCount() )
{
setCacheSize( mLayer->featureCount() + 100 );
}
QgsFeature feat;
featureAtId( fid, feat );
}
emit featureAdded( fid );
}
void QgsVectorLayerCache::attributeAdded( int field )
{
Q_UNUSED( field )
mCachedAttributes.append( field );
invalidate();
}
void QgsVectorLayerCache::attributeDeleted( int field )
{
QgsAttributeList attrs = mCachedAttributes;
mCachedAttributes.clear();
Q_FOREACH ( int attr, attrs )
{
if ( attr < field )
mCachedAttributes << attr;
else if ( attr > field )
mCachedAttributes << attr - 1;
}
}
void QgsVectorLayerCache::geometryChanged( QgsFeatureId fid, const QgsGeometry &geom )
{
QgsCachedFeature *cachedFeat = mCache[ fid ];
if ( cachedFeat )
{
cachedFeat->mFeature->setGeometry( geom );
}
}
void QgsVectorLayerCache::layerDeleted()
{
emit cachedLayerDeleted();
mLayer = nullptr;
}
void QgsVectorLayerCache::invalidate()
{
mCache.clear();
mFullCache = false;
emit invalidated();
}
bool QgsVectorLayerCache::canUseCacheForRequest( const QgsFeatureRequest &featureRequest, QgsFeatureIterator &it )
{
// check first for available indices
Q_FOREACH ( QgsAbstractCacheIndex *idx, mCacheIndices )
{
if ( idx->getCacheIterator( it, featureRequest ) )
{
return true;
}
}
// no indexes available, but maybe we have already cached all required features anyway?
switch ( featureRequest.filterType() )
{
case QgsFeatureRequest::FilterFid:
{
if ( mCache.contains( featureRequest.filterFid() ) )
{
it = QgsFeatureIterator( new QgsCachedFeatureIterator( this, featureRequest ) );
return true;
}
break;
}
case QgsFeatureRequest::FilterFids:
{
if ( mCache.keys().toSet().contains( featureRequest.filterFids() ) )
{
it = QgsFeatureIterator( new QgsCachedFeatureIterator( this, featureRequest ) );
return true;
}
break;
}
case QgsFeatureRequest::FilterNone:
case QgsFeatureRequest::FilterRect:
case QgsFeatureRequest::FilterExpression:
{
if ( mFullCache )
{
it = QgsFeatureIterator( new QgsCachedFeatureIterator( this, featureRequest ) );
return true;
}
break;
}
}
return false;
}
QgsFeatureIterator QgsVectorLayerCache::getFeatures( const QgsFeatureRequest &featureRequest )
{
QgsFeatureIterator it;
bool requiresWriterIt = true; // If a not yet cached, but cachable request is made, this stays true.
if ( checkInformationCovered( featureRequest ) )
{
// If we have a full cache available, run on this
if ( mFullCache )
{
it = QgsFeatureIterator( new QgsCachedFeatureIterator( this, featureRequest ) );
requiresWriterIt = false;
}
else
{
// may still be able to satisfy request using cache
requiresWriterIt = !canUseCacheForRequest( featureRequest, it );
}
}
else
{
// Let the layer answer the request, so no caching of requests
// we don't want to cache is done
requiresWriterIt = false;
it = mLayer->getFeatures( featureRequest );
}
if ( requiresWriterIt && mLayer->dataProvider() )
{
// No index was able to satisfy the request
QgsFeatureRequest myRequest = QgsFeatureRequest( featureRequest );
// Make sure if we cache the geometry, it gets fetched
if ( mCacheGeometry && mLayer->hasGeometryType() )
myRequest.setFlags( featureRequest.flags() & ~QgsFeatureRequest::NoGeometry );
// Make sure, all the cached attributes are requested as well
QSet<int> attrs = featureRequest.subsetOfAttributes().toSet() + mCachedAttributes.toSet();
myRequest.setSubsetOfAttributes( attrs.toList() );
it = QgsFeatureIterator( new QgsCachedFeatureWriterIterator( this, myRequest ) );
}
return it;
}
bool QgsVectorLayerCache::isFidCached( const QgsFeatureId fid ) const
{
return mCache.contains( fid );
}
bool QgsVectorLayerCache::checkInformationCovered( const QgsFeatureRequest &featureRequest )
{
QgsAttributeList requestedAttributes;
if ( !featureRequest.flags().testFlag( QgsFeatureRequest::SubsetOfAttributes ) )
{
requestedAttributes = mLayer->attributeList();
}
else
{
requestedAttributes = featureRequest.subsetOfAttributes();
}
// Check if we even cache the information requested
Q_FOREACH ( int attr, requestedAttributes )
{
if ( !mCachedAttributes.contains( attr ) )
{
return false;
}
}
// If the request needs geometry but we don't cache this...
if ( !featureRequest.flags().testFlag( QgsFeatureRequest::NoGeometry )
&& !mCacheGeometry )
{
return false;
}
return true;
}