Skip to content
Permalink
Browse files

Utilise expression context cache to store some more expensive

expression calculation results

(cherry-picked from 0f1c8df)
  • Loading branch information
nyalldawson committed Apr 9, 2018
1 parent 7978bfd commit cc87fb1c7312846a85b1df1ee7404b1cc4fb54d0
Showing with 25 additions and 4 deletions.
  1. +25 −4 src/core/expression/qgsexpressionfunction.cpp
@@ -3418,7 +3418,7 @@ static QVariant fcnGetFeatureById( const QVariantList &values, const QgsExpressi
return result;
}

static QVariant fcnGetFeature( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
static QVariant fcnGetFeature( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
//arguments: 1. layer id / name, 2. key attribute, 3. eq value
QgsVectorLayer *vl = QgsExpressionUtils::getVectorLayer( values.at( 0 ), parent );
@@ -3437,6 +3437,13 @@ static QVariant fcnGetFeature( const QVariantList &values, const QgsExpressionCo
}

const QVariant &attVal = values.at( 2 );

const QString cacheValueKey = QStringLiteral( "getfeature:%1:%2:%3" ).arg( vl->id() ).arg( attributeId ).arg( attVal.toString() );
if ( context && context->hasCachedValue( cacheValueKey ) )
{
return context->cachedValue( cacheValueKey );
}

QgsFeatureRequest req;
req.setFilterExpression( QStringLiteral( "%1=%2" ).arg( QgsExpression::quotedColumnRef( attribute ),
QgsExpression::quotedString( attVal.toString() ) ) );
@@ -3448,10 +3455,15 @@ static QVariant fcnGetFeature( const QVariantList &values, const QgsExpressionCo
QgsFeatureIterator fIt = vl->getFeatures( req );

QgsFeature fet;
QVariant res;
if ( fIt.nextFeature( fet ) )
return QVariant::fromValue( fet );
{
res = QVariant::fromValue( fet );
}

return QVariant();
if ( context )
context->setCachedValue( cacheValueKey, res );
return res;
}

static QVariant fcnRepresentValue( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node )
@@ -3481,7 +3493,14 @@ static QVariant fcnRepresentValue( const QVariantList &values, const QgsExpressi
}
else
{
QgsVectorLayer *layer = QgsExpressionUtils::getVectorLayer( context->variable( "layer" ), parent );
QgsVectorLayer *layer = QgsExpressionUtils::getVectorLayer( context->variable( QStringLiteral( "layer" ) ), parent );

const QString cacheValueKey = QStringLiteral( "repvalfcnval:%1:%2:%3" ).arg( layer ? layer->id() : QStringLiteral( "[None]" ), fieldName, value.toString() );
if ( context->hasCachedValue( cacheValueKey ) )
{
return context->cachedValue( cacheValueKey );
}

const QgsEditorWidgetSetup setup = fields.at( fieldIndex ).editorWidgetSetup();
const QgsFieldFormatter *formatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() );

@@ -3497,6 +3516,8 @@ static QVariant fcnRepresentValue( const QVariantList &values, const QgsExpressi
cache = context->cachedValue( cacheKey );

result = formatter->representValue( layer, fieldIndex, setup.config(), cache, value );

context->setCachedValue( cacheValueKey, result );
}
}
else

0 comments on commit cc87fb1

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