QgsVectorLayerCache class

This class caches features of a given QgsVectorLayer.

The cached features can be indexed by QgsAbstractCacheIndex.

Proper indexing for a given use-case may speed up performance substantially.

Public functions

void addCacheIndex(QgsAbstractCacheIndex* cacheIndex)
Adds a QgsAbstractCacheIndex to this cache.
auto cachedFeatureIds() const -> QgsFeatureIds
Returns the set of feature IDs for features which are cached.
auto cacheGeometry() const -> bool
Returns true if the cache will fetch and cache feature geometries.
auto cacheSize() -> int
Returns the maximum number of features this cache will hold.
auto featureAtId(QgsFeatureId featureId, QgsFeature& feature, bool skipCache = false) -> bool
Gets the feature at the given feature id.
auto featureCount() const -> long
Returns the number of features contained in the source, or -1 if the feature count is unknown.
auto fields() const -> QgsFields
Returns the fields associated with features in the cache.
auto getFeature(QgsFeatureId fid) -> QgsFeature
Query the layer for the feature with the given id.
auto getFeatures(const QgsFeatureRequest& featureRequest = QgsFeatureRequest()) -> QgsFeatureIterator
Query this VectorLayerCache for features.
auto getFeatures(const QString& expression) -> QgsFeatureIterator
Query the layer for features matching a given expression.
auto getFeatures(const QgsFeatureIds& fids) -> QgsFeatureIterator
Query the layer for the features with the given ids.
auto getFeatures(const QgsRectangle& rectangle) -> QgsFeatureIterator
Query the layer for the features which intersect the specified rectangle.
auto hasFullCache() const -> bool
Returns true if the cache is complete, ie it contains all features.
auto isFidCached(QgsFeatureId fid) const -> bool
Check if a certain feature id is cached.
auto layer() -> QgsVectorLayer*
Returns the layer to which this cache belongs.
auto removeCachedFeature(QgsFeatureId fid) -> bool
Removes the feature identified by fid from the cache if present.
void setCacheAddedAttributes(bool cacheAddedAttributes)
If this is enabled, the subset of cached attributes will automatically be extended to also include newly added attributes.
void setCacheGeometry(bool cacheGeometry)
Enable or disable the caching of geometries.
void setCacheSize(int cacheSize)
Sets the maximum number of features to keep in the cache.
void setCacheSubsetOfAttributes(const QgsAttributeList& attributes)
Set the subset of attributes to be cached.
void setFullCache(bool fullCache)
This enables or disables full caching.
auto sourceCrs() const -> QgsCoordinateReferenceSystem
Returns the coordinate reference system for features in the cache.
auto wkbType() const -> QgsWkbTypes::Type
Returns the geometry type for features in the cache.

Signals

void attributeValueChanged(QgsFeatureId fid, int field, const QVariant& value)
Is emitted when an attribute is changed.
void cachedLayerDeleted()
Is emitted when the cached layer is deleted.
void featureAdded(QgsFeatureId fid)
Is emitted, when a new feature has been added to the layer and this cache.
void finished()
When filling the cache, this signal gets emitted once the cache is fully initialized.
void invalidated()
The cache has been invalidated and cleared.
void progress(int i, bool& cancel)
When filling the cache, this signal gets emitted periodically to notify about the progress and to be able to cancel an operation.

Protected functions

auto checkInformationCovered(const QgsFeatureRequest& featureRequest) -> bool
Checks if the information required to complete the request is cached.
void featureRemoved(QgsFeatureId fid)
Gets called, whenever a feature has been removed.
void requestCompleted(const QgsFeatureRequest& featureRequest, const QgsFeatureIds& fids)
Gets called, whenever the full list of feature ids for a certain request is known.

Function documentation

void QgsVectorLayerCache::addCacheIndex(QgsAbstractCacheIndex* cacheIndex)

Adds a QgsAbstractCacheIndex to this cache.

Parameters
cacheIndex The cache index to add.

Cache indices know about features present in this cache and decide, if enough information is present in the cache to respond to a QgsFeatureRequest. The layer cache will take ownership of the index.

QgsFeatureIds QgsVectorLayerCache::cachedFeatureIds() const

Returns the set of feature IDs for features which are cached.

bool QgsVectorLayerCache::cacheGeometry() const

Returns true if the cache will fetch and cache feature geometries.

int QgsVectorLayerCache::cacheSize()

Returns the maximum number of features this cache will hold.

Returns int

In case full caching is enabled, this number can change, as new features get added.

bool QgsVectorLayerCache::featureAtId(QgsFeatureId featureId, QgsFeature& feature, bool skipCache = false)

Gets the feature at the given feature id.

Parameters
featureId The id of the feature to query
feature The result of the operation will be written to this feature
skipCache Will query the layer regardless if the feature is in the cache already
Returns true in case of success

Considers the changed, added, deleted and permanent features

QgsFeature QgsVectorLayerCache::getFeature(QgsFeatureId fid)

Query the layer for the feature with the given id.

If there is no such feature, the returned feature will be invalid.

QgsFeatureIterator QgsVectorLayerCache::getFeatures(const QgsFeatureRequest& featureRequest = QgsFeatureRequest())

Query this VectorLayerCache for features.

Parameters
featureRequest The request specifying filter and required data.
Returns An iterator over the requested data.

If the VectorLayerCache (and moreover any of its indices) is able to satisfy the request, the returned QgsFeatureIterator will iterate over cached features. If it's not possible to fully satisfy the request from the cache, part or all of the features will be requested from the data provider.

bool QgsVectorLayerCache::hasFullCache() const

Returns true if the cache is complete, ie it contains all features.

This may happen as a result of a call to setFullCache() or by through a feature request which resulted in all available features being cached.

bool QgsVectorLayerCache::isFidCached(QgsFeatureId fid) const

Check if a certain feature id is cached.

Parameters
fid The feature id to look for
Returns True if this id is in the cache

bool QgsVectorLayerCache::removeCachedFeature(QgsFeatureId fid)

Removes the feature identified by fid from the cache if present.

Parameters
fid The id of the feature to delete
Returns true if the feature was removed, false if the feature id was not found in the cache

void QgsVectorLayerCache::setCacheAddedAttributes(bool cacheAddedAttributes)

If this is enabled, the subset of cached attributes will automatically be extended to also include newly added attributes.

Parameters
cacheAddedAttributes Automatically cache new attributes

void QgsVectorLayerCache::setCacheGeometry(bool cacheGeometry)

Enable or disable the caching of geometries.

Parameters
cacheGeometry Enable or disable the caching of geometries

void QgsVectorLayerCache::setCacheSize(int cacheSize)

Sets the maximum number of features to keep in the cache.

Parameters
cacheSize indicates the maximum number of features to keep in the cache

Some features will be removed from the cache if the number is smaller than the previous size of the cache.

void QgsVectorLayerCache::setCacheSubsetOfAttributes(const QgsAttributeList& attributes)

Set the subset of attributes to be cached.

Parameters
attributes The attributes to be cached

void QgsVectorLayerCache::setFullCache(bool fullCache)

This enables or disables full caching.

Parameters
fullCache True: enable full caching, False: disable full caching

If enabled, all features will be held in the cache. The cache size will incrementally be increased to offer space for all features. When enabled, all features will be read into cache. As this feature will most likely be used for slow data sources, be aware, that the call to this method might take a long time.

void QgsVectorLayerCache::attributeValueChanged(QgsFeatureId fid, int field, const QVariant& value) signal

Is emitted when an attribute is changed.

Is re-emitted after the layer itself emits this signal. You should connect to this signal, to be sure, to not get a cached value if querying the cache.

void QgsVectorLayerCache::cachedLayerDeleted() signal

Is emitted when the cached layer is deleted.

Is emitted when the cached layers layerDelete() signal is being emitted, but before the local reference to it has been set to NULL. So call to layer() will still return a valid pointer for cleanup purpose.

void QgsVectorLayerCache::featureAdded(QgsFeatureId fid) signal

Is emitted, when a new feature has been added to the layer and this cache.

Parameters
fid The featureid of the changed feature

You should connect to this signal instead of the layers', if you want to be sure that this cache has updated information for the new feature

void QgsVectorLayerCache::invalidated() signal

The cache has been invalidated and cleared.

Note that when a cache is invalidated the fullCache() setting will be cleared, and a full cache rebuild via setFullCache( true ) will need to be performed.

void QgsVectorLayerCache::progress(int i, bool& cancel) signal

When filling the cache, this signal gets emitted periodically to notify about the progress and to be able to cancel an operation.

Parameters
i The number of already fetched features
cancel A reference to a boolean variable. Set to true and the operation will be canceled.

bool QgsVectorLayerCache::checkInformationCovered(const QgsFeatureRequest& featureRequest) protected

Checks if the information required to complete the request is cached.

Parameters
featureRequest The QgsFeatureRequest to be answered
Returns True if the information is being cached, false if not

i.e. If all attributes required and the geometry is held in the cache. Please note, that this does not check, if the requested features are cached.

void QgsVectorLayerCache::featureRemoved(QgsFeatureId fid) protected

Gets called, whenever a feature has been removed.

Parameters
fid The feature id of the removed feature.

Broadcasts this information to indices, so they can invalidate their cache if required.

void QgsVectorLayerCache::requestCompleted(const QgsFeatureRequest& featureRequest, const QgsFeatureIds& fids) protected

Gets called, whenever the full list of feature ids for a certain request is known.

Parameters
featureRequest The feature request that was answered
fids The feature ids that have been returned

Broadcasts this information to indices, so they can update their tables.