Skip to content

Commit

Permalink
address review comments and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Feb 27, 2025
1 parent 1157abb commit cf2203f
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 28 deletions.
20 changes: 10 additions & 10 deletions python/PyQt6/core/auto_generated/vector/qgsvectorlayercache.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -181,18 +181,18 @@ Returns the set of feature IDs for features which are cached.
.. seealso:: :py:func:`isFidCached`
%End

bool featureAtId( QgsFeatureId featureId, QgsFeature &feature, bool skipCache = false );
bool featureAtId( QgsFeatureId featureId, QgsFeature &feature /Out/, bool skipCache = false );
%Docstring
Gets the feature at the given feature id. Considers the changed, added, deleted and permanent features

:param featureId: The id of the feature to query
:param feature: The result of the operation will be written to this feature
:param skipCache: Will query the layer regardless if the feature is in the cache already

:return: ``True`` in case of success
:return: - ``True`` in case of success
- feature: The result of the operation will be written to this feature
%End

bool featureAtIdWithAllAttributes( QgsFeatureId featureId, QgsFeature &feature, bool skipCache = false );
bool featureAtIdWithAllAttributes( QgsFeatureId featureId, QgsFeature &feature /Out/, bool skipCache = false );
%Docstring
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
Expand All @@ -201,17 +201,17 @@ already contains all attributes, calling this function has the same effect as ca
Considers the changed, added, deleted and permanent features

:param featureId: The id of the feature to query
:param feature: The result of the operation will be written to this feature
:param skipCache: Will query the layer regardless if the feature is in the cache already

:return: ``True`` in case of success
:return: - ``True`` in case of success
- feature: The result of the operation will be written to this feature

.. seealso:: :py:func:`featureAtId`

.. versionadded:: 3.32
%End

bool featureAtIdWithAllAttributesAndGeometry( QgsFeatureId featureId, QgsFeature &feature, bool skipCache = false );
bool completeFeatureAtId( QgsFeatureId featureId, QgsFeature &feature /Out/, bool skipCache = false );
%Docstring
Gets the feature at the given feature id with all attributes and geometry, if the cached feature
already contains all attributes and geometry, calling this function has the same effect as calling
Expand All @@ -220,14 +220,14 @@ already contains all attributes and geometry, calling this function has the same
Considers the changed, added, deleted and permanent features

:param featureId: The id of the feature to query
:param feature: The result of the operation will be written to this feature
:param skipCache: Will query the layer regardless if the feature is in the cache already

:return: ``True`` in case of success
:return: - ``True`` in case of success
- feature: The result of the operation will be written to this feature

.. seealso:: :py:func:`featureAtId`

.. versionadded:: 3.42
.. versionadded:: 3.44
%End

bool removeCachedFeature( QgsFeatureId fid );
Expand Down
20 changes: 10 additions & 10 deletions python/core/auto_generated/vector/qgsvectorlayercache.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -181,18 +181,18 @@ Returns the set of feature IDs for features which are cached.
.. seealso:: :py:func:`isFidCached`
%End

bool featureAtId( QgsFeatureId featureId, QgsFeature &feature, bool skipCache = false );
bool featureAtId( QgsFeatureId featureId, QgsFeature &feature /Out/, bool skipCache = false );
%Docstring
Gets the feature at the given feature id. Considers the changed, added, deleted and permanent features

:param featureId: The id of the feature to query
:param feature: The result of the operation will be written to this feature
:param skipCache: Will query the layer regardless if the feature is in the cache already

:return: ``True`` in case of success
:return: - ``True`` in case of success
- feature: The result of the operation will be written to this feature
%End

bool featureAtIdWithAllAttributes( QgsFeatureId featureId, QgsFeature &feature, bool skipCache = false );
bool featureAtIdWithAllAttributes( QgsFeatureId featureId, QgsFeature &feature /Out/, bool skipCache = false );
%Docstring
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
Expand All @@ -201,17 +201,17 @@ already contains all attributes, calling this function has the same effect as ca
Considers the changed, added, deleted and permanent features

:param featureId: The id of the feature to query
:param feature: The result of the operation will be written to this feature
:param skipCache: Will query the layer regardless if the feature is in the cache already

:return: ``True`` in case of success
:return: - ``True`` in case of success
- feature: The result of the operation will be written to this feature

.. seealso:: :py:func:`featureAtId`

.. versionadded:: 3.32
%End

bool featureAtIdWithAllAttributesAndGeometry( QgsFeatureId featureId, QgsFeature &feature, bool skipCache = false );
bool completeFeatureAtId( QgsFeatureId featureId, QgsFeature &feature /Out/, bool skipCache = false );
%Docstring
Gets the feature at the given feature id with all attributes and geometry, if the cached feature
already contains all attributes and geometry, calling this function has the same effect as calling
Expand All @@ -220,14 +220,14 @@ already contains all attributes and geometry, calling this function has the same
Considers the changed, added, deleted and permanent features

:param featureId: The id of the feature to query
:param feature: The result of the operation will be written to this feature
:param skipCache: Will query the layer regardless if the feature is in the cache already

:return: ``True`` in case of success
:return: - ``True`` in case of success
- feature: The result of the operation will be written to this feature

.. seealso:: :py:func:`featureAtId`

.. versionadded:: 3.42
.. versionadded:: 3.44
%End

bool removeCachedFeature( QgsFeatureId fid );
Expand Down
2 changes: 1 addition & 1 deletion src/core/vector/qgsvectorlayercache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ bool QgsVectorLayerCache::featureAtIdWithAllAttributes( QgsFeatureId featureId,
return featureFound;
}

bool QgsVectorLayerCache::featureAtIdWithAllAttributesAndGeometry( QgsFeatureId featureId, QgsFeature &feature, bool skipCache )
bool QgsVectorLayerCache::completeFeatureAtId( QgsFeatureId featureId, QgsFeature &feature, bool skipCache )
{
bool featureFound = false;

Expand Down
12 changes: 6 additions & 6 deletions src/core/vector/qgsvectorlayercache.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class CORE_EXPORT QgsVectorLayerCache : public QObject
* \param feat The feature to cache. A copy will be made.
* \param vlCache The cache to inform when the feature has been removed from the cache.
* \param allAttributesFetched TRUE if the feature was fetched with all attributes (and not a subset)
* \param geometryFetched TRUE if the feature was fetched with geometry, \since QGIS 3.42
* \param geometryFetched TRUE if the feature was fetched with geometry, \since QGIS 3.44
*/
QgsCachedFeature( const QgsFeature &feat, QgsVectorLayerCache *vlCache, bool allAttributesFetched, bool geometryFetched )
: mCache( vlCache )
Expand Down Expand Up @@ -258,7 +258,7 @@ class CORE_EXPORT QgsVectorLayerCache : public QObject
* \param skipCache Will query the layer regardless if the feature is in the cache already
* \returns TRUE in case of success
*/
bool featureAtId( QgsFeatureId featureId, QgsFeature &feature, bool skipCache = false );
bool featureAtId( QgsFeatureId featureId, QgsFeature &feature SIP_OUT, bool skipCache = false );

/**
* Gets the feature at the given feature id with all attributes, if the cached feature
Expand All @@ -273,7 +273,7 @@ class CORE_EXPORT QgsVectorLayerCache : public QObject
* \see featureAtId()
* \since QGIS 3.32
*/
bool featureAtIdWithAllAttributes( QgsFeatureId featureId, QgsFeature &feature, bool skipCache = false );
bool featureAtIdWithAllAttributes( QgsFeatureId featureId, QgsFeature &feature SIP_OUT, bool skipCache = false );

/**
* Gets the feature at the given feature id with all attributes and geometry, if the cached feature
Expand All @@ -286,9 +286,9 @@ class CORE_EXPORT QgsVectorLayerCache : public QObject
* \param skipCache Will query the layer regardless if the feature is in the cache already
* \returns TRUE in case of success
* \see featureAtId()
* \since QGIS 3.42
* \since QGIS 3.44
*/
bool featureAtIdWithAllAttributesAndGeometry( QgsFeatureId featureId, QgsFeature &feature, bool skipCache = false );
bool completeFeatureAtId( QgsFeatureId featureId, QgsFeature &feature SIP_OUT, bool skipCache = false );

/**
* Removes the feature identified by fid from the cache if present.
Expand Down Expand Up @@ -439,7 +439,7 @@ class CORE_EXPORT QgsVectorLayerCache : public QObject

inline void cacheFeature( QgsFeature &feat, bool allAttributesFetched, bool geometryFetched = false )
{
QgsCachedFeature *cachedFeature = new QgsCachedFeature( feat, this, allAttributesFetched, geometryFetched );
QgsCachedFeature *cachedFeature = new QgsCachedFeature( feat, this, allAttributesFetched, geometryFetched || mCacheGeometry );
mCache.insert( feat.id(), cachedFeature );
if ( mCacheUnorderedKeys.find( feat.id() ) == mCacheUnorderedKeys.end() )
{
Expand Down
2 changes: 1 addition & 1 deletion src/gui/attributetable/qgsfeaturelistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ QVariant QgsFeatureListModel::data( const QModelIndex &index, int role ) const
{
QgsFeature feat;

mFilterModel->layerCache()->featureAtIdWithAllAttributesAndGeometry( idxToFid( index ), feat );
mFilterModel->layerCache()->completeFeatureAtId( idxToFid( index ), feat );

return QVariant::fromValue( feat );
}
Expand Down
36 changes: 36 additions & 0 deletions tests/src/core/testqgsvectorlayercache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class TestVectorLayerCache : public QObject

void onCommittedFeaturesAdded( const QString &, const QgsFeatureList & );

void testCompleteFeatureAtId();

private:
QgsVectorLayerCache *mVectorLayerCache = nullptr;
QgsCacheIndexFeatureId *mFeatureIdIndex = nullptr;
Expand Down Expand Up @@ -493,5 +495,39 @@ void TestVectorLayerCache::onCommittedFeaturesAdded( const QString &layerId, con
mAddedFeatures.append( features );
}

void TestVectorLayerCache::testCompleteFeatureAtId()
{
QgsVectorLayerCache cache( mPointsLayer, static_cast<int>( mPointsLayer->dataProvider()->featureCount() ) );
// cache only attributes
cache.setCacheGeometry( false );
cache.invalidate();
cache.setFullCache( true );

QgsFeature f;
QgsFeatureIterator it = cache.getFeatures();
it.nextFeature( f );

QVERIFY( cache.isFidCached( f.id() ) );
QVERIFY( cache.mCache[f.id()]->allAttributesFetched() );
QVERIFY( !cache.mCache[f.id()]->geometryFetched() );

cache.featureAtIdWithAllAttributes( 0, f );
QVERIFY( cache.mCache[0]->allAttributesFetched() );
QVERIFY( !cache.mCache[0]->geometryFetched() );
QVERIFY( !f.attribute( 0 ).isNull() );
QVERIFY( !f.attribute( 1 ).isNull() );
QVERIFY( !f.attribute( 2 ).isNull() );
QVERIFY( f.geometry().isNull() );

cache.completeFeatureAtId( 0, f );
QVERIFY( cache.isFidCached( 0 ) );
QVERIFY( cache.mCache[0]->allAttributesFetched() );
QVERIFY( cache.mCache[0]->geometryFetched() );
QVERIFY( !f.attribute( 0 ).isNull() );
QVERIFY( !f.attribute( 1 ).isNull() );
QVERIFY( !f.attribute( 2 ).isNull() );
QVERIFY( !f.geometry().isNull() );
}

QGSTEST_MAIN( TestVectorLayerCache )
#include "testqgsvectorlayercache.moc"

0 comments on commit cf2203f

Please sign in to comment.