29 mCache.setMaxCost( cacheSize );
33 connect( mLayer, &QgsVectorLayer::destroyed,
this, &QgsVectorLayerCache::layerDeleted );
44 connectJoinedLayers();
49 qDeleteAll( mCacheIndices );
50 mCacheIndices.clear();
55 mCache.setMaxCost( cacheSize );
60 return mCache.maxCost();
65 bool shouldCacheGeometry = cacheGeometry && mLayer->
isSpatial();
66 bool mustInvalidate = shouldCacheGeometry && !mCacheGeometry;
67 mCacheGeometry = shouldCacheGeometry;
84 mCachedAttributes = attributes;
89 mFullCache = fullCache;
98 .setSubsetOfAttributes( mCachedAttributes )
107 while ( it.nextFeature( f ) )
111 if ( t.elapsed() > 1000 )
130 mCacheIndices.append( cacheIndex );
135 if ( cacheAddedAttributes )
147 bool featureFound =
false;
153 cachedFeature = mCache[ featureId ];
158 feature =
QgsFeature( *cachedFeature->feature() );
162 .setFilterFid( featureId )
163 .setSubsetOfAttributes( mCachedAttributes )
167 cacheFeature( feature );
176 return mCache.remove( fid );
186 return mLayer->
crs();
207 if ( fids.count() <= mCache.size() )
209 for (
const auto &idx : qgis::as_const( mCacheIndices ) )
211 idx->requestCompleted( featureRequest, fids );
229 void QgsVectorLayerCache::onAttributeValueChanged(
QgsFeatureId fid,
int field,
const QVariant &value )
235 cachedFeat->mFeature->setAttribute( field, value );
241 void QgsVectorLayerCache::onJoinAttributeValueChanged(
QgsFeatureId fid,
int field,
const QVariant &value )
254 if ( feature.
isValid() && fieldIndex != -1 )
256 onAttributeValueChanged( feature.
id(), fieldIndex, value );
263 void QgsVectorLayerCache::featureDeleted(
QgsFeatureId fid )
265 mCache.remove( fid );
268 void QgsVectorLayerCache::onFeatureAdded(
QgsFeatureId fid )
283 void QgsVectorLayerCache::attributeAdded(
int field )
286 mCachedAttributes.append( field );
290 void QgsVectorLayerCache::attributeDeleted(
int field )
293 mCachedAttributes.clear();
295 Q_FOREACH (
int attr, attrs )
298 mCachedAttributes << attr;
299 else if ( attr > field )
300 mCachedAttributes << attr - 1;
310 cachedFeat->mFeature->setGeometry( geom );
314 void QgsVectorLayerCache::layerDeleted()
320 void QgsVectorLayerCache::invalidate()
343 if ( mCache.contains( featureRequest.
filterFid() ) )
352 if ( mCache.keys().toSet().contains( featureRequest.
filterFids() ) )
377 bool requiresWriterIt =
true;
385 requiresWriterIt =
false;
390 requiresWriterIt = !canUseCacheForRequest( featureRequest, it );
397 requiresWriterIt =
false;
407 if ( mCacheGeometry && mLayer->
isSpatial() )
411 QSet<int> attrs = featureRequest.
subsetOfAttributes().toSet() + mCachedAttributes.toSet();
422 return mCache.contains( fid );
439 Q_FOREACH (
int attr, requestedAttributes )
441 if ( !mCachedAttributes.contains( attr ) )
449 && !mCacheGeometry );
452 void QgsVectorLayerCache::connectJoinedLayers()
const QString prefixedFieldName(const QgsField &field) const
Returns the prefixed name of the field.
const QList< QgsVectorLayerJoinInfo > vectorJoins() const
Wrapper for iterator of features from vector data provider or vector layer.
friend class QgsCachedFeatureWriterIterator
QgsWkbTypes::Type wkbType() const
Returns the geometry type for features in the cache.
QgsAttributeList attributeList() const
Returns list of attribute indexes.
const Flags & flags() const
Filter using feature IDs.
QSet< QgsFeatureId > QgsFeatureIds
QgsVectorLayerCache(QgsVectorLayer *layer, int cacheSize, QObject *parent=nullptr)
QgsFeatureId filterFid() const
Gets the feature ID that should be fetched.
bool isValid() const
Returns the validity of this feature.
void invalidated()
The cache has been invalidated and cleared.
const QgsRectangle & filterRect() const
Returns the rectangle from which features will be taken.
QgsWkbTypes::Type wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
bool removeCachedFeature(QgsFeatureId fid)
Removes the feature identified by fid from the cache if present.
const QgsFeatureIds & filterFids() const
Gets feature IDs that should be fetched.
bool contains(const QgsRectangle &rect) const
Returns true when rectangle contains other rectangle.
QgsAttributeList subsetOfAttributes() const
Returns the subset of attributes which at least need to be fetched.
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
~QgsVectorLayerCache() override
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
Container of fields for a vector layer.
A geometry is the spatial representation of a feature.
void requestCompleted(const QgsFeatureRequest &featureRequest, const QgsFeatureIds &fids)
Gets called, whenever the full list of feature ids for a certain request is known.
QgsVectorLayer * layer()
Returns the layer to which this cache belongs.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
void attributeValueChanged(QgsFeatureId fid, int field, const QVariant &value)
Is emitted when an attribute is changed.
void featureDeleted(QgsFeatureId fid)
Emitted when a feature has been deleted.
bool isSpatial() const FINAL
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
QgsFields fields() const
Returns the fields associated with features in the cache.
void setCacheSubsetOfAttributes(const QgsAttributeList &attributes)
Set the subset of attributes to be cached.
void featureAdded(QgsFeatureId fid)
Is emitted, when a new feature has been added to the layer and this cache.
QgsFeature getFeature(QgsFeatureId fid) const
Query the layer for the feature with the given id.
Type
The WKB type describes the number of dimensions a geometry has.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
bool isFidCached(QgsFeatureId fid) const
Check if a certain feature id is cached.
void progress(int i, bool &cancel)
When filling the cache, this signal gets emitted periodically to notify about the progress and to be ...
Defines left outer join from our vector layer to some other vector layer.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QgsFeature targetedFeatureOf(const QgsVectorLayerJoinInfo *info, const QgsFeature &feature) const
Returns the targeted feature corresponding to the joined feature.
long featureCount(const QString &legendKey) const
Number of features rendered with specified legend key.
void geometryChanged(QgsFeatureId fid, const QgsGeometry &geometry)
Is emitted whenever a geometry change is done in the edit buffer.
FilterType filterType() const
Returns the filter type which is currently set on this request.
void attributeValueChanged(QgsFeatureId fid, int idx, const QVariant &value)
Is emitted whenever an attribute value change is done in the edit buffer.
void featureAdded(QgsFeatureId fid)
Emitted when a new feature has been added to the layer.
Fetch only a subset of attributes (setSubsetOfAttributes sets this flag)
void cachedLayerDeleted()
Is emitted when the cached layer is deleted.
QgsField field(int fieldIdx) const
Gets field at particular index (must be in range 0..N-1)
QgsRectangle extent() const FINAL
Returns the extent of the layer.
bool checkInformationCovered(const QgsFeatureRequest &featureRequest)
Checks if the information required to complete the request is cached.
QgsVectorLayerJoinBuffer * joinBuffer()
Returns the join buffer object.
int indexFromName(const QString &fieldName) const
Gets the field index from the field name.
QgsCoordinateReferenceSystem sourceCrs() const
Returns the coordinate reference system for features in the cache.
bool cacheGeometry() const
Returns true if the cache will fetch and cache feature geometries.
virtual bool getCacheIterator(QgsFeatureIterator &featureIterator, const QgsFeatureRequest &featureRequest)=0
Is called, when a feature request is issued on a cached layer.
void attributeAdded(int idx)
Will be emitted, when a new attribute has been added to this vector layer.
Abstract base class for cache indices.
void setCacheGeometry(bool cacheGeometry)
Enable or disable the caching of geometries.
void setFullCache(bool fullCache)
This enables or disables full caching.
bool featureAtId(QgsFeatureId featureId, QgsFeature &feature, bool skipCache=false)
Gets the feature at the given feature id.
void setCacheSize(int cacheSize)
Sets the maximum number of features to keep in the cache.
This class represents a coordinate reference system (CRS).
void dataChanged()
Data of layer changed.
QgsVectorLayer * joinLayer() const
Returns joined layer (may be null if the reference was set by layer ID and not resolved yet) ...
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Query the layer for features specified in request.
void featureRemoved(QgsFeatureId fid)
Gets called, whenever a feature has been removed.
int cacheSize()
Returns the maximum number of features this cache will hold.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider.
QList< int > QgsAttributeList
friend class QgsCachedFeature
bool nextFeature(QgsFeature &f)
void attributeDeleted(int idx)
Will be emitted, when an attribute has been deleted from this vector layer.
Geometry is not required. It may still be returned if e.g. required for a filter condition.
void addCacheIndex(QgsAbstractCacheIndex *cacheIndex)
Adds a QgsAbstractCacheIndex to this cache.
Represents a vector layer which manages a vector based data sets.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &featureRequest=QgsFeatureRequest())
Query this VectorLayerCache for features.
void updatedFields()
Is emitted, whenever the fields available from this layer have been changed.
virtual void flushFeature(QgsFeatureId fid)=0
Is called, whenever a feature is removed from the cache.
void finished()
When filling the cache, this signal gets emitted once the cache is fully initialized.
friend class QgsCachedFeatureIterator
QgsCoordinateReferenceSystem crs
void setCacheAddedAttributes(bool cacheAddedAttributes)
If this is enabled, the subset of cached attributes will automatically be extended to also include ne...
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.
long featureCount() const
Returns the number of features contained in the source, or -1 if the feature count is unknown...