Skip to content
Permalink
Browse files

Copy data for background threads

reduce requirements to run code on main thread which risks freezes
because of deadlocks
  • Loading branch information
m-kuhn committed Mar 18, 2019
1 parent 89e0ff9 commit c603cfb9caa3299f35fdde4753776901d4cf9262
@@ -10,7 +10,6 @@




class QgsFeaturePool : QgsFeatureSink /Abstract/
{
%Docstring
@@ -76,6 +75,8 @@ The geometry type of this layer.
The coordinate reference system of this layer.
%End

QString layerName() const;

protected:

void insertFeature( const QgsFeature &feature );
@@ -29,9 +29,9 @@
QgsFeaturePool::QgsFeaturePool( QgsVectorLayer *layer )
: mFeatureCache( CACHE_SIZE )
, mLayer( layer )
, mLayerId( layer->id() )
, mGeometryType( layer->geometryType() )
, mCrs( layer->crs() )
, mFeatureSource( qgis::make_unique<QgsVectorLayerFeatureSource>( layer ) )
, mLayerName( layer->name() )
{

}
@@ -55,11 +55,9 @@ bool QgsFeaturePool::getFeature( QgsFeatureId id, QgsFeature &feature, QgsFeedba
}
else
{
std::unique_ptr<QgsVectorLayerFeatureSource> source = QgsVectorLayerUtils::getFeatureSource( mLayer, feedback );

// Feature not in cache, retrieve from layer
// TODO: avoid always querying all attributes (attribute values are needed when merging by attribute)
if ( !source || !source->getFeatures( QgsFeatureRequest( id ) ).nextFeature( feature ) )
if ( !mFeatureSource->getFeatures( QgsFeatureRequest( id ) ).nextFeature( feature ) )
{
return false;
}
@@ -150,12 +148,18 @@ void QgsFeaturePool::setFeatureIds( const QgsFeatureIds &ids )

bool QgsFeaturePool::isFeatureCached( QgsFeatureId fid )
{
QgsReadWriteLocker locker( mCacheLock, QgsReadWriteLocker::Read );
return mFeatureCache.contains( fid );
}

QString QgsFeaturePool::layerName() const
{
return mLayerName;
}

QgsCoordinateReferenceSystem QgsFeaturePool::crs() const
{
return mCrs;
return mFeatureSource->crs();
}

QgsWkbTypes::GeometryType QgsFeaturePool::geometryType() const
@@ -165,5 +169,5 @@ QgsWkbTypes::GeometryType QgsFeaturePool::geometryType() const

QString QgsFeaturePool::layerId() const
{
return mLayerId;
return mFeatureSource->id();
}
@@ -25,8 +25,7 @@
#include "qgsfeature.h"
#include "qgsspatialindex.h"
#include "qgsfeaturesink.h"

class QgsVectorLayer;
#include "qgsvectorlayerfeatureiterator.h"

/**
* \ingroup analysis
@@ -121,6 +120,8 @@ class ANALYSIS_EXPORT QgsFeaturePool : public QgsFeatureSink SIP_ABSTRACT
*/
QgsCoordinateReferenceSystem crs() const;

QString layerName() const;

protected:

/**
@@ -170,9 +171,9 @@ class ANALYSIS_EXPORT QgsFeaturePool : public QgsFeatureSink SIP_ABSTRACT
mutable QReadWriteLock mCacheLock;
QgsFeatureIds mFeatureIds;
QgsSpatialIndex mIndex;
QString mLayerId;
QgsWkbTypes::GeometryType mGeometryType;
QgsCoordinateReferenceSystem mCrs;
std::unique_ptr<QgsVectorLayerFeatureSource> mFeatureSource;
QString mLayerName;
};

#endif // QGS_FEATUREPOOL_H
@@ -74,17 +74,17 @@ QgsGeometry QgsGeometryCheckerUtils::LayerFeature::geometry() const

QString QgsGeometryCheckerUtils::LayerFeature::id() const
{
return QStringLiteral( "%1:%2" ).arg( layer()->name() ).arg( mFeature.id() );
return QStringLiteral( "%1:%2" ).arg( mFeaturePool->layerName() ).arg( mFeature.id() );
}

bool QgsGeometryCheckerUtils::LayerFeature::operator==( const LayerFeature &other ) const
{
return layer()->id() == other.layer()->id() && feature().id() == other.feature().id();
return layerId() == other.layerId() && mFeature.id() == other.mFeature.id();
}

bool QgsGeometryCheckerUtils::LayerFeature::operator!=( const LayerFeature &other ) const
{
return layer()->id() != other.layer()->id() || feature().id() != other.feature().id();
return layerId() != other.layerId() || mFeature.id() != other.mFeature.id();
}

/////////////////////////////////////////////////////////////////////////////
@@ -197,7 +197,7 @@ bool QgsGeometryCheckerUtils::LayerFeatures::iterator::nextFeature( bool begin )
QgsFeature feature;
if ( featurePool->getFeature( *mFeatureIt, feature ) && !feature.geometry().isNull() )
{
mCurrentFeature.reset( new LayerFeature( mParent->mFeaturePools[*mLayerIt], feature, mParent->mContext, mParent->mUseMapCrs ) );
mCurrentFeature = qgis::make_unique<LayerFeature>( featurePool, feature, mParent->mContext, mParent->mUseMapCrs );
return true;
}
++mFeatureIt;

0 comments on commit c603cfb

Please sign in to comment.
You can’t perform that action at this time.