Skip to content

Commit

Permalink
Copy data for background threads
Browse files Browse the repository at this point in the history
reduce requirements to run code on main thread which risks freezes
because of deadlocks
  • Loading branch information
m-kuhn committed Mar 12, 2019
1 parent 857fe53 commit 9a839c9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@




class QgsFeaturePool : QgsFeatureSink /Abstract/
{
%Docstring
Expand Down Expand Up @@ -80,6 +79,8 @@ The geometry type of this layer.
The coordinate reference system of this layer.
%End

QString layerName() const;

protected:

void insertFeature( const QgsFeature &feature );
Expand Down
18 changes: 11 additions & 7 deletions src/analysis/vector/geometry_checker/qgsfeaturepool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() )
{

}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
Expand All @@ -165,5 +169,5 @@ QgsWkbTypes::GeometryType QgsFeaturePool::geometryType() const

QString QgsFeaturePool::layerId() const
{
return mLayerId;
return mFeatureSource->id();
}
9 changes: 5 additions & 4 deletions src/analysis/vector/geometry_checker/qgsfeaturepool.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
#include "qgsfeature.h"
#include "qgsspatialindex.h"
#include "qgsfeaturesink.h"

class QgsVectorLayer;
#include "qgsvectorlayerfeatureiterator.h"

/**
* \ingroup analysis
Expand Down Expand Up @@ -125,6 +124,8 @@ class ANALYSIS_EXPORT QgsFeaturePool : public QgsFeatureSink SIP_ABSTRACT
*/
QgsCoordinateReferenceSystem crs() const;

QString layerName() const;

protected:

/**
Expand Down Expand Up @@ -174,9 +175,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
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 9a839c9

Please sign in to comment.