QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Classes | Signals | Public Member Functions | Protected Member Functions | Private Slots | Private Member Functions | Private Attributes | Friends | List of all members
QgsVectorLayerCache Class Reference

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

#include <qgsvectorlayercache.h>

Collaboration diagram for QgsVectorLayerCache:
Collaboration graph
[legend]

Classes

class  QgsCachedFeature
 This is a wrapper class around a cached QgsFeature, which will inform the cache, when it has been deleted, so indexes can be updated that the wrapped feature needs to be fetched again if needed. More...

Signals

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.
void finished ()
 When filling the cache, this signal gets emitted once the cache is fully initialized.
void cachedLayerDeleted ()
 Is emitted when the cached layer is deleted.
void attributeValueChanged (const QgsFeatureId &fid, const int &field, const QVariant &value)
 Is emitted when an attribute is changed.

Public Member Functions

 QgsVectorLayerCache (QgsVectorLayer *layer, int cacheSize, QObject *parent=NULL)
void setCacheSize (int cacheSize)
 Sets the maximum number of features to keep in the cache.
int cacheSize ()
 Returns the maximum number of features this cache will hold.
void setCacheGeometry (bool cacheGeometry)
 Enable or disable the caching of geometries.
void setCacheSubsetOfAttributes (const QgsAttributeList &attributes)
 Set the subset of attributes to be cached.
void setCacheAddedAttributes (bool cacheAddedAttributes)
 If this is enabled, the subset of cached attributes will automatically be extended to also include newly added attributes.
void setFullCache (bool fullCache)
 This enables or disables full caching.
void addCacheIndex (QgsAbstractCacheIndex *cacheIndex)
 Adds a QgsAbstractCacheIndex to this cache.
QgsFeatureIterator getFeatures (const QgsFeatureRequest &featureRequest)
 Query this VectorLayerCache for features.
bool isFidCached (const QgsFeatureId fid)
 Check if a certain feature id is cached.
bool featureAtId (QgsFeatureId featureId, QgsFeature &feature, bool skipCache=false)
 Gets the feature at the given feature id.
bool removeCachedFeature (QgsFeatureId fid)
 Removes the feature identified by fid from the cache if present.
QgsVectorLayerlayer ()
 Returns the layer to which this cache belongs.

Protected Member Functions

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

Private Slots

void onAttributeValueChanged (QgsFeatureId fid, int field, const QVariant &value)
void featureDeleted (QgsFeatureId fid)
void featureAdded (QgsFeatureId fid)
void attributeAdded (int field)
void attributeDeleted (int field)
void geometryChanged (QgsFeatureId fid, QgsGeometry &geom)
void layerDeleted ()
void updatedFields ()

Private Member Functions

void cacheFeature (QgsFeature &feat)

Private Attributes

QgsVectorLayermLayer
QCache< QgsFeatureId,
QgsCachedFeature
mCache
bool mCacheGeometry
bool mFullCache
QList< QgsAbstractCacheIndex * > mCacheIndices
QgsAttributeList mCachedAttributes

Friends

class QgsCachedFeatureIterator
class QgsCachedFeatureWriterIterator
class QgsCachedFeature

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 38 of file qgsvectorlayercache.h.

Constructor & Destructor Documentation

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

Member Function Documentation

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.

Parameters
cacheIndexThe cache index to add.

Definition at line 110 of file qgsvectorlayercache.cpp.

References mCacheIndices.

void QgsVectorLayerCache::attributeAdded ( int  field)
privateslot

Definition at line 217 of file qgsvectorlayercache.cpp.

References mCache, and mCachedAttributes.

Referenced by setCacheAddedAttributes().

void QgsVectorLayerCache::attributeDeleted ( int  field)
privateslot

Definition at line 224 of file qgsvectorlayercache.cpp.

References mCache.

Referenced by QgsVectorLayerCache().

void QgsVectorLayerCache::attributeValueChanged ( const QgsFeatureId fid,
const 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.

Referenced by onAttributeValueChanged(), and QgsVectorLayerCache().

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.

Referenced by layerDeleted().

void QgsVectorLayerCache::cacheFeature ( QgsFeature feat)
inlineprivate
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 46 of file qgsvectorlayercache.cpp.

References mCache.

Referenced by featureAdded().

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 302 of file qgsvectorlayercache.cpp.

References QgsFeatureRequest::flags(), mCachedAttributes, mCacheGeometry, mLayer, QgsFeatureRequest::NoGeometry, QgsVectorLayer::pendingAllAttributesList(), QgsFeatureRequest::SubsetOfAttributes, and QgsFeatureRequest::subsetOfAttributes().

Referenced by getFeatures().

void QgsVectorLayerCache::featureAdded ( QgsFeatureId  fid)
privateslot
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 127 of file qgsvectorlayercache.cpp.

References cacheFeature(), QgsVectorLayerCache::QgsCachedFeature::feature(), QgsVectorLayer::getFeatures(), mCache, mCachedAttributes, mCacheGeometry, mLayer, QgsFeatureIterator::nextFeature(), and QgsFeatureRequest::NoGeometry.

Referenced by QgsDualView::attributeAdded(), QgsFeatureListModel::data(), featureAdded(), QgsFeatureListModel::featureByIndex(), QgsAttributeTableModel::loadFeatureAtId(), QgsDualView::reloadAttribute(), and QgsDualView::saveEditChanges().

void QgsVectorLayerCache::featureDeleted ( QgsFeatureId  fid)
privateslot

Definition at line 198 of file qgsvectorlayercache.cpp.

References mCache.

Referenced by QgsVectorLayerCache().

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 178 of file qgsvectorlayercache.cpp.

References QgsAbstractCacheIndex::flushFeature(), and mCacheIndices.

void QgsVectorLayerCache::finished ( )
signal

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

Referenced by setFullCache().

void QgsVectorLayerCache::geometryChanged ( QgsFeatureId  fid,
QgsGeometry geom 
)
privateslot
QgsFeatureIterator QgsVectorLayerCache::getFeatures ( const QgsFeatureRequest featureRequest)

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 253 of file qgsvectorlayercache.cpp.

References checkInformationCovered(), QgsVectorLayer::dataProvider(), QgsFeatureRequest::flags(), QgsAbstractCacheIndex::getCacheIterator(), QgsVectorLayer::getFeatures(), QgsVectorLayer::hasGeometryType(), mCachedAttributes, mCacheGeometry, mCacheIndices, mLayer, QgsCachedFeatureWriterIterator, QgsAbstractFeatureIterator::QgsFeatureIterator, QgsFeatureRequest::setFlags(), QgsFeatureRequest::setSubsetOfAttributes(), and QgsFeatureRequest::subsetOfAttributes().

Referenced by QgsAttributeTableFilterModel::generateListOfVisibleFeatures(), QgsAttributeTableModel::loadLayer(), QgsAttributeTableModel::prefetchColumnData(), and setFullCache().

bool QgsVectorLayerCache::isFidCached ( const QgsFeatureId  fid)

Check if a certain feature id is cached.

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

Definition at line 297 of file qgsvectorlayercache.cpp.

References mCache.

Referenced by QgsCacheIndexFeatureId::getCacheIterator().

QgsVectorLayer * QgsVectorLayerCache::layer ( )
void QgsVectorLayerCache::layerDeleted ( )
privateslot

Definition at line 242 of file qgsvectorlayercache.cpp.

References cachedLayerDeleted(), and mLayer.

Referenced by QgsVectorLayerCache().

void QgsVectorLayerCache::onAttributeValueChanged ( QgsFeatureId  fid,
int  field,
const QVariant &  value 
)
privateslot
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.

Referenced by setFullCache().

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 156 of file qgsvectorlayercache.cpp.

References mCache.

Referenced by QgsAttributeTableModel::reload().

void QgsVectorLayerCache::requestCompleted ( QgsFeatureRequest  featureRequest,
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 166 of file qgsvectorlayercache.cpp.

References mCache, mCacheIndices, and QgsAbstractCacheIndex::requestCompleted().

Referenced by QgsCachedFeatureWriterIterator::nextFeature().

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 115 of file qgsvectorlayercache.cpp.

References attributeAdded(), and mLayer.

Referenced by QgsVectorLayerCache().

void QgsVectorLayerCache::setCacheGeometry ( bool  cacheGeometry)

Enable or disable the caching of geometries.

Parameters
cacheGeometryEnable or disable the caching of geometries

Definition at line 51 of file qgsvectorlayercache.cpp.

References geometryChanged(), QgsVectorLayer::hasGeometryType(), mCacheGeometry, and mLayer.

Referenced by QgsVectorLayerCache().

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 41 of file qgsvectorlayercache.cpp.

References mCache.

Referenced by featureAdded(), and setFullCache().

void QgsVectorLayerCache::setCacheSubsetOfAttributes ( const QgsAttributeList attributes)

Set the subset of attributes to be cached.

Parameters
attributesThe attributes to be cached

Definition at line 64 of file qgsvectorlayercache.cpp.

References mCachedAttributes.

Referenced by QgsVectorLayerCache().

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

Definition at line 69 of file qgsvectorlayercache.cpp.

References QgsVectorLayer::featureCount(), finished(), getFeatures(), mCachedAttributes, mCacheGeometry, mFullCache, mLayer, QgsFeatureRequest::NoGeometry, progress(), and setCacheSize().

void QgsVectorLayerCache::updatedFields ( )
privateslot

Definition at line 248 of file qgsvectorlayercache.cpp.

References mCache.

Referenced by QgsVectorLayerCache().

Friends And Related Function Documentation

friend class QgsCachedFeature
friend

Definition at line 274 of file qgsvectorlayercache.h.

Referenced by cacheFeature().

friend class QgsCachedFeatureIterator
friend

Definition at line 272 of file qgsvectorlayercache.h.

friend class QgsCachedFeatureWriterIterator
friend

Definition at line 273 of file qgsvectorlayercache.h.

Referenced by getFeatures().

Member Data Documentation

QCache< QgsFeatureId, QgsCachedFeature > QgsVectorLayerCache::mCache
private
QgsAttributeList QgsVectorLayerCache::mCachedAttributes
private
bool QgsVectorLayerCache::mCacheGeometry
private
QList<QgsAbstractCacheIndex*> QgsVectorLayerCache::mCacheIndices
private
bool QgsVectorLayerCache::mFullCache
private

Definition at line 267 of file qgsvectorlayercache.h.

Referenced by featureAdded(), and setFullCache().

QgsVectorLayer* QgsVectorLayerCache::mLayer
private

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