address review comments and add test

This commit is contained in:
Alexander Bruy 2025-02-27 11:01:12 +00:00 committed by Nyall Dawson
parent bb60704ed0
commit ce1b0c70ca
6 changed files with 64 additions and 28 deletions

View File

@ -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
@ -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
@ -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 );

View File

@ -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
@ -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
@ -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 );

View File

@ -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;

View File

@ -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 )
@ -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
@ -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
@ -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.
@ -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() )
{

View File

@ -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 );
}

View File

@ -64,6 +64,8 @@ class TestVectorLayerCache : public QObject
void onCommittedFeaturesAdded( const QString &, const QgsFeatureList & );
void testCompleteFeatureAtId();
private:
QgsVectorLayerCache *mVectorLayerCache = nullptr;
QgsCacheIndexFeatureId *mFeatureIdIndex = nullptr;
@ -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"