QGIS API Documentation  3.37.0-Master (a5b4d9743e8)
Classes | Signals | Public Member Functions | Protected Member Functions | Friends | List of all members
QgsVectorLayerCache Class Reference

This class caches features of a given QgsVectorLayer. More...

#include <qgsvectorlayercache.h>

Inheritance diagram for QgsVectorLayerCache:
Inheritance graph
[legend]

Signals

void attributeValueChanged (QgsFeatureId fid, int field, const QVariant &value)
 Emitted when an attribute is changed. More...
 
void cachedLayerDeleted ()
 Is emitted when the cached layer is deleted. More...
 
void featureAdded (QgsFeatureId fid)
 Emitted when a new feature has been added to the layer and this cache. More...
 
void finished ()
 When filling the cache, this signal gets emitted once the cache is fully initialized. More...
 
void invalidated ()
 The cache has been invalidated and cleared. More...
 
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. More...
 

Public Member Functions

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

Protected Member Functions

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

Friends

class QgsCachedFeature
 
class QgsCachedFeatureIterator
 
class QgsCachedFeatureWriterIterator
 
class TestVectorLayerCache
 

Detailed Description

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.

Definition at line 46 of file qgsvectorlayercache.h.

Constructor & Destructor Documentation

◆ QgsVectorLayerCache()

QgsVectorLayerCache::QgsVectorLayerCache ( QgsVectorLayer layer,
int  cacheSize,
QObject *  parent = nullptr 
)

Definition at line 27 of file qgsvectorlayercache.cpp.

◆ ~QgsVectorLayerCache()

QgsVectorLayerCache::~QgsVectorLayerCache ( )
override

Definition at line 49 of file qgsvectorlayercache.cpp.

Member Function Documentation

◆ addCacheIndex()

void QgsVectorLayerCache::addCacheIndex ( QgsAbstractCacheIndex cacheIndex)

Adds a QgsAbstractCacheIndex to this cache.

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.

Parameters
cacheIndexThe cache index to add.

Definition at line 138 of file qgsvectorlayercache.cpp.

◆ attributeValueChanged

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

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.

◆ cachedFeatureIds()

QgsFeatureIds QgsVectorLayerCache::cachedFeatureIds ( ) const

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

See also
isFidCached()

Definition at line 514 of file qgsvectorlayercache.cpp.

◆ cachedLayerDeleted

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 nullptr. So call to layer() will still return a valid pointer for cleanup purpose.

◆ cacheGeometry()

bool QgsVectorLayerCache::cacheGeometry ( ) const
inline

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

See also
setCacheGeometry()

Definition at line 129 of file qgsvectorlayercache.h.

◆ cacheSize()

int QgsVectorLayerCache::cacheSize ( )

Returns the maximum number of features this cache will hold.

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

Returns
int

Definition at line 60 of file qgsvectorlayercache.cpp.

◆ cacheSubsetOfAttributes()

QgsAttributeList QgsVectorLayerCache::cacheSubsetOfAttributes ( ) const

Returns the list (possibly a subset) of cached attributes.

Note
By default the cache will store all layer's attributes.
See also
setCacheSubsetOfAttributes()
Since
QGIS 3.32

Definition at line 92 of file qgsvectorlayercache.cpp.

◆ checkInformationCovered()

bool QgsVectorLayerCache::checkInformationCovered ( const QgsFeatureRequest featureRequest)
protected

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

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.

Parameters
featureRequestThe QgsFeatureRequest to be answered
Returns
true if the information is being cached, false if not

Definition at line 520 of file qgsvectorlayercache.cpp.

◆ featureAdded

void QgsVectorLayerCache::featureAdded ( QgsFeatureId  fid)
signal

Emitted when a new feature has been added to the layer and this cache.

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

Parameters
fidThe featureid of the changed feature

◆ featureAtId()

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

Gets the feature at the given feature id.

Considers the changed, added, deleted and permanent features

Parameters
featureIdThe id of the feature to query
featureThe result of the operation will be written to this feature
skipCacheWill query the layer regardless if the feature is in the cache already
Returns
true in case of success

Definition at line 155 of file qgsvectorlayercache.cpp.

◆ featureAtIdWithAllAttributes()

bool QgsVectorLayerCache::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 all attributes, calling this function has the same effect as calling featureAtId().

Considers the changed, added, deleted and permanent features

Parameters
featureIdThe id of the feature to query
featureThe result of the operation will be written to this feature
skipCacheWill query the layer regardless if the feature is in the cache already
Returns
true in case of success
See also
featureAtId()
Since
QGIS 3.32

Definition at line 193 of file qgsvectorlayercache.cpp.

◆ featureCount()

long long QgsVectorLayerCache::featureCount ( ) const

Returns the number of features contained in the source, or -1 if the feature count is unknown.

Definition at line 258 of file qgsvectorlayercache.cpp.

◆ featureRemoved()

void QgsVectorLayerCache::featureRemoved ( QgsFeatureId  fid)
protected

Gets called, whenever a feature has been removed.

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

Parameters
fidThe feature id of the removed feature.

Definition at line 280 of file qgsvectorlayercache.cpp.

◆ fields()

QgsFields QgsVectorLayerCache::fields ( ) const

Returns the fields associated with features in the cache.

Definition at line 253 of file qgsvectorlayercache.cpp.

◆ finished

void QgsVectorLayerCache::finished ( )
signal

When filling the cache, this signal gets emitted once the cache is fully initialized.

◆ getFeature()

QgsFeature QgsVectorLayerCache::getFeature ( QgsFeatureId  fid)
inline

Query the layer for the feature with the given id.

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

Definition at line 212 of file qgsvectorlayercache.h.

◆ getFeatures() [1/4]

QgsFeatureIterator QgsVectorLayerCache::getFeatures ( const QgsFeatureIds fids)
inline

Query the layer for the features with the given ids.

Definition at line 222 of file qgsvectorlayercache.h.

◆ getFeatures() [2/4]

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

Query this VectorLayerCache for features.

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.

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

Definition at line 449 of file qgsvectorlayercache.cpp.

◆ getFeatures() [3/4]

QgsFeatureIterator QgsVectorLayerCache::getFeatures ( const QgsRectangle rectangle)
inline

Query the layer for the features which intersect the specified rectangle.

Definition at line 230 of file qgsvectorlayercache.h.

◆ getFeatures() [4/4]

QgsFeatureIterator QgsVectorLayerCache::getFeatures ( const QString &  expression)
inline

Query the layer for features matching a given expression.

Definition at line 203 of file qgsvectorlayercache.h.

◆ hasFullCache()

bool QgsVectorLayerCache::hasFullCache ( ) const
inline

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.

See also
setFullCache()

Definition at line 177 of file qgsvectorlayercache.h.

◆ invalidated

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.

◆ isFidCached()

bool QgsVectorLayerCache::isFidCached ( QgsFeatureId  fid) const

Check if a certain feature id is cached.

Parameters
fidThe feature id to look for
Returns
true if this id is in the cache
See also
cachedFeatureIds()

Definition at line 509 of file qgsvectorlayercache.cpp.

◆ layer()

QgsVectorLayer * QgsVectorLayerCache::layer ( )

Returns the layer to which this cache belongs.

Definition at line 238 of file qgsvectorlayercache.cpp.

◆ progress

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
iThe number of already fetched features
cancelA reference to a boolean variable. Set to true and the operation will be canceled.
Note
not available in Python bindings

◆ removeCachedFeature()

bool QgsVectorLayerCache::removeCachedFeature ( QgsFeatureId  fid)

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

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

Definition at line 222 of file qgsvectorlayercache.cpp.

◆ requestCompleted()

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.

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

Parameters
featureRequestThe feature request that was answered
fidsThe feature ids that have been returned

Definition at line 263 of file qgsvectorlayercache.cpp.

◆ setCacheAddedAttributes()

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
cacheAddedAttributesAutomatically cache new attributes

Definition at line 143 of file qgsvectorlayercache.cpp.

◆ setCacheGeometry()

void QgsVectorLayerCache::setCacheGeometry ( bool  cacheGeometry)

Enable or disable the caching of geometries.

Parameters
cacheGeometryEnable or disable the caching of geometries
See also
cacheGeometry()

Definition at line 65 of file qgsvectorlayercache.cpp.

◆ setCacheSize()

void QgsVectorLayerCache::setCacheSize ( int  cacheSize)

Sets 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.

Parameters
cacheSizeindicates the maximum number of features to keep in the cache

Definition at line 55 of file qgsvectorlayercache.cpp.

◆ setCacheSubsetOfAttributes()

void QgsVectorLayerCache::setCacheSubsetOfAttributes ( const QgsAttributeList attributes)

Set the list (possibly a subset) of attributes to be cached.

Note
By default the cache will store all layer's attributes.
Parameters
attributesThe attributes to be cached

Definition at line 84 of file qgsvectorlayercache.cpp.

◆ setFullCache()

void QgsVectorLayerCache::setFullCache ( bool  fullCache)

This enables or disables 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.

Parameters
fullCachetrue: enable full caching, false: disable full caching
Note
when a cache is invalidated() (e.g. by adding an attribute to a layer) this setting is reset. A full cache rebuild must be performed by calling setFullCache( true ) again.
See also
hasFullCache()

Definition at line 97 of file qgsvectorlayercache.cpp.

◆ sourceCrs()

QgsCoordinateReferenceSystem QgsVectorLayerCache::sourceCrs ( ) const

Returns the coordinate reference system for features in the cache.

Definition at line 243 of file qgsvectorlayercache.cpp.

◆ wkbType()

Qgis::WkbType QgsVectorLayerCache::wkbType ( ) const

Returns the geometry type for features in the cache.

Definition at line 248 of file qgsvectorlayercache.cpp.

Friends And Related Function Documentation

◆ QgsCachedFeature

friend class QgsCachedFeature
friend

Definition at line 448 of file qgsvectorlayercache.h.

◆ QgsCachedFeatureIterator

friend class QgsCachedFeatureIterator
friend

Definition at line 446 of file qgsvectorlayercache.h.

◆ QgsCachedFeatureWriterIterator

friend class QgsCachedFeatureWriterIterator
friend

Definition at line 447 of file qgsvectorlayercache.h.

◆ TestVectorLayerCache

friend class TestVectorLayerCache
friend

Definition at line 460 of file qgsvectorlayercache.h.


The documentation for this class was generated from the following files: