19#include "moc_qgsvectorlayercache.cpp"
26#include <QElapsedTimer>
36 connect( mLayer, &QgsVectorLayer::destroyed,
this, &QgsVectorLayerCache::layerDeleted );
47 connectJoinedLayers();
52 qDeleteAll( mCacheIndices );
53 mCacheIndices.clear();
63 return mCache.maxCost();
69 bool mustInvalidate = shouldCacheGeometry && !mCacheGeometry;
70 mCacheGeometry = shouldCacheGeometry;
87 if ( ! mCache.isEmpty() && ! QSet<int>( mCachedAttributes.cbegin(), mCachedAttributes.cend() )
88 .contains( QSet<int>( attributes.cbegin(), attributes.cend( ) ) ) )
90 mCachedAttributes = attributes;
95 return mCachedAttributes;
100 mFullCache = fullCache;
109 .setSubsetOfAttributes( mCachedAttributes )
122 if ( t.elapsed() > 1000 )
141 mCacheIndices.append( cacheIndex );
146 if ( cacheAddedAttributes )
158 bool featureFound =
false;
164 cachedFeature = mCache[ featureId ];
169 feature =
QgsFeature( *cachedFeature->feature() );
175 const bool allAttrsFetched { mCachedAttributes.count( ) == mLayer->
fields().
count() };
176 if ( ! allAttrsFetched )
178 request.setSubsetOfAttributes( mCachedAttributes );
180 if ( !mCacheGeometry )
186 cacheFeature( feature, allAttrsFetched );
197 bool featureFound =
false;
203 cachedFeature = mCache[ featureId ];
206 if ( cachedFeature && cachedFeature->allAttributesFetched() )
208 feature =
QgsFeature( *cachedFeature->feature() );
212 .setFilterFid( featureId )
216 cacheFeature( feature,
true );
225 bool removed = mCache.remove( fid );
228 if (
auto unorderedIt = std::find( mCacheUnorderedKeys.begin(), mCacheUnorderedKeys.end(), fid ); unorderedIt != mCacheUnorderedKeys.end() )
230 mCacheUnorderedKeys.erase( unorderedIt );
232 if (
auto orderedIt = std::find( mCacheOrderedKeys.begin(), mCacheOrderedKeys.end(), fid ); orderedIt != mCacheOrderedKeys.end() )
233 mCacheOrderedKeys.erase( orderedIt );
246 return mLayer->
crs();
267 if ( fids.count() <= mCache.size() )
269 for (
const auto &idx : std::as_const( mCacheIndices ) )
271 idx->requestCompleted( featureRequest, fids );
283 const auto constMCacheIndices = mCacheIndices;
286 idx->flushFeature( fid );
290void QgsVectorLayerCache::onAttributeValueChanged(
QgsFeatureId fid,
int field,
const QVariant &value )
292 QgsCachedFeature *cachedFeat = mCache[ fid ];
296 cachedFeat->mFeature->setAttribute( field, value );
302void QgsVectorLayerCache::onJoinAttributeValueChanged(
QgsFeatureId fid,
int field,
const QVariant &value )
304 const QgsVectorLayer *joinLayer = qobject_cast<const QgsVectorLayer *>( sender() );
306 const auto constVectorJoins = mLayer->
vectorJoins();
309 if ( joinLayer == info.joinLayer() )
313 const QString fieldName = info.prefixedFieldName( joinLayer->
fields().
field( field ) );
316 if ( feature.
isValid() && fieldIndex != -1 )
318 onAttributeValueChanged( feature.
id(), fieldIndex, value );
325void QgsVectorLayerCache::featureDeleted(
QgsFeatureId fid )
327 mCache.remove( fid );
329 if (
auto it = mCacheUnorderedKeys.find( fid ); it != mCacheUnorderedKeys.end() )
331 mCacheUnorderedKeys.erase( it );
332 if (
auto orderedIt = std::find( mCacheOrderedKeys.begin(), mCacheOrderedKeys.end(), fid ); orderedIt != mCacheOrderedKeys.end() )
333 mCacheOrderedKeys.erase( orderedIt );
337void QgsVectorLayerCache::onFeatureAdded(
QgsFeatureId fid )
352void QgsVectorLayerCache::attributeAdded(
int field )
355 mCachedAttributes.append( field );
359void QgsVectorLayerCache::attributeDeleted(
int field )
362 mCachedAttributes.clear();
364 for (
int attr : attrs )
367 mCachedAttributes << attr;
368 else if ( attr > field )
369 mCachedAttributes << attr - 1;
379 cachedFeat->mFeature->setGeometry( geom );
383void QgsVectorLayerCache::layerDeleted()
389void QgsVectorLayerCache::invalidate()
391 if ( ! mCache.isEmpty() )
394 mCacheOrderedKeys.clear();
395 mCacheUnorderedKeys.clear();
404 const auto constMCacheIndices = mCacheIndices;
407 if ( idx->getCacheIterator( it, featureRequest ) )
418 if ( mCache.contains( featureRequest.
filterFid() ) )
452 bool requiresWriterIt =
true;
460 requiresWriterIt =
false;
465 requiresWriterIt = !canUseCacheForRequest( featureRequest, it );
472 requiresWriterIt =
false;
482 if ( mCacheGeometry && mLayer->
isSpatial() )
488 if ( mCachedAttributes.count( ) != mLayer->
fields().
count() )
491 QSet<int> attrs( requestSubset.begin(), requestSubset.end() );
492 for (
int attr : std::as_const( mCachedAttributes ) )
493 attrs.insert( attr );
511 return mCache.contains( fid );
516 const QList< QgsFeatureId > keys = mCache.keys();
534 const auto constRequestedAttributes = requestedAttributes;
535 for (
int attr : constRequestedAttributes )
537 if ( !mCachedAttributes.contains( attr ) )
545 && !mCacheGeometry );
548void QgsVectorLayerCache::connectJoinedLayers()
const
550 const auto constVectorJoins = mLayer->
vectorJoins();
559bool QgsVectorLayerCache::QgsCachedFeature::allAttributesFetched()
const
561 return mAllAttributesFetched;
@ Fid
Filter using feature ID.
@ Fids
Filter using feature IDs.
@ Expression
Filter using expression.
@ NoFilter
No filter is applied.
@ SubsetOfAttributes
Fetch only a subset of attributes (setSubsetOfAttributes sets this flag)
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
@ NoFlags
No flags are set.
@ NoFilter
No spatial filtering of features.
QFlags< FeatureRequestFlag > FeatureRequestFlags
Flags for controlling feature requests.
WkbType
The WKB type describes the number of dimensions a geometry has.
Abstract base class for cache indices.
This class represents a coordinate reference system (CRS).
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
bool close()
Call to end the iteration.
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFlags(Qgis::FeatureRequestFlags flags)
Sets flags that affect how features will be fetched.
QgsRectangle filterRect() const
Returns the rectangle from which features will be taken.
Qgis::FeatureRequestFilterType filterType() const
Returns the attribute/ID filter type which is currently set on this request.
Qgis::FeatureRequestFlags flags() const
Returns the flags which affect how features are fetched.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
Qgis::SpatialFilterType spatialFilterType() const
Returns the spatial filter type which is currently set on this request.
QgsAttributeList subsetOfAttributes() const
Returns the subset of attributes which at least need to be fetched.
const QgsFeatureIds & filterFids() const
Returns the feature IDs that should be fetched.
QgsFeatureId filterFid() const
Returns the feature ID that should be fetched.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
bool isValid() const
Returns the validity of this feature.
Container of fields for a vector layer.
Q_INVOKABLE int indexFromName(const QString &fieldName) const
Gets the field index from the field name.
QgsField field(int fieldIdx) const
Returns the field at particular index (must be in range 0..N-1).
A geometry is the spatial representation of a feature.
QgsCoordinateReferenceSystem crs
void dataChanged()
Data of layer changed.
bool contains(const QgsRectangle &rect) const
Returns true when rectangle contains other rectangle.
bool isFidCached(QgsFeatureId fid) const
Check if a certain feature id is cached.
void setFullCache(bool fullCache)
This enables or disables full caching.
void finished()
When filling the cache, this signal gets emitted once the cache is fully initialized.
void featureRemoved(QgsFeatureId fid)
Gets called, whenever a feature has been removed.
void setCacheAddedAttributes(bool cacheAddedAttributes)
If this is enabled, the subset of cached attributes will automatically be extended to also include ne...
friend class QgsCachedFeature
void invalidated()
The cache has been invalidated and cleared.
void setCacheSize(int cacheSize)
Sets the maximum number of features to keep in the cache.
void setCacheSubsetOfAttributes(const QgsAttributeList &attributes)
Set the list (possibly a subset) of attributes to be cached.
void featureAdded(QgsFeatureId fid)
Emitted when a new feature has been added to the layer and this cache.
QgsFields fields() const
Returns the fields associated with features in the cache.
void cachedLayerDeleted()
Is emitted when the cached layer is deleted.
friend class QgsCachedFeatureWriterIterator
void requestCompleted(const QgsFeatureRequest &featureRequest, const QgsFeatureIds &fids)
Gets called, whenever the full list of feature ids for a certain request is known.
void attributeValueChanged(QgsFeatureId fid, int field, const QVariant &value)
Emitted when an attribute is changed.
bool removeCachedFeature(QgsFeatureId fid)
Removes the feature identified by fid from the cache if present.
void progress(int i, bool &cancel)
When filling the cache, this signal gets emitted periodically to notify about the progress and to be ...
QgsVectorLayer * layer()
Returns the layer to which this cache belongs.
long long featureCount() const
Returns the number of features contained in the source, or -1 if the feature count is unknown.
QgsCoordinateReferenceSystem sourceCrs() const
Returns the coordinate reference system for features in the cache.
bool checkInformationCovered(const QgsFeatureRequest &featureRequest)
Checks if the information required to complete the request is cached.
QgsFeatureIds cachedFeatureIds() const
Returns the set of feature IDs for features which are cached.
bool featureAtIdWithAllAttributes(QgsFeatureId featureId, QgsFeature &feature, bool skipCache=false)
Gets the feature at the given feature id with all attributes, if the cached feature already contains ...
bool cacheGeometry() const
Returns true if the cache will fetch and cache feature geometries.
Qgis::WkbType wkbType() const
Returns the geometry type for features in the cache.
int cacheSize()
Returns the maximum number of features this cache will hold.
friend class QgsCachedFeatureIterator
QgsAttributeList cacheSubsetOfAttributes() const
Returns the list (possibly a subset) of cached attributes.
void addCacheIndex(QgsAbstractCacheIndex *cacheIndex)
Adds a QgsAbstractCacheIndex to this cache.
QgsVectorLayerCache(QgsVectorLayer *layer, int cacheSize, QObject *parent=nullptr)
void setCacheGeometry(bool cacheGeometry)
Enable or disable the caching of geometries.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &featureRequest=QgsFeatureRequest())
Query this VectorLayerCache for features.
~QgsVectorLayerCache() override
bool featureAtId(QgsFeatureId featureId, QgsFeature &feature, bool skipCache=false)
Gets the feature at the given feature id.
QgsFeature targetedFeatureOf(const QgsVectorLayerJoinInfo *info, const QgsFeature &feature) const
Returns the targeted feature corresponding to the joined feature.
Defines left outer join from our vector layer to some other vector layer.
Represents a vector layer which manages a vector based data sets.
Q_INVOKABLE QgsAttributeList attributeList() const
Returns list of attribute indexes.
void attributeAdded(int idx)
Will be emitted, when a new attribute has been added to this vector layer.
long long featureCount(const QString &legendKey) const
Number of features rendered with specified legend key.
bool isSpatial() const FINAL
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
Q_INVOKABLE Qgis::WkbType wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
void attributeDeleted(int idx)
Will be emitted, when an attribute has been deleted from this vector layer.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
QgsVectorLayerJoinBuffer * joinBuffer()
Returns the join buffer object.
void attributeValueChanged(QgsFeatureId fid, int idx, const QVariant &value)
Emitted whenever an attribute value change is done in the edit buffer.
QgsRectangle extent() const FINAL
Returns the extent of the layer.
void updatedFields()
Emitted whenever the fields available from this layer have been changed.
void featureAdded(QgsFeatureId fid)
Emitted when a new feature has been added to the layer.
void featureDeleted(QgsFeatureId fid)
Emitted when a feature has been deleted.
const QList< QgsVectorLayerJoinInfo > vectorJoins() const
void geometryChanged(QgsFeatureId fid, const QgsGeometry &geometry)
Emitted whenever a geometry change is done in the edit buffer.
Q_INVOKABLE QgsFeature getFeature(QgsFeatureId fid) const
Queries the layer for the feature with the given id.
QSet< QgsFeatureId > QgsFeatureIds
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
QList< int > QgsAttributeList