Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #45314 from domi4484/backport-45170-to-queued_ltr_…
…backports

Cache only joined attributes without colliding names
  • Loading branch information
elpaso committed Sep 30, 2021
2 parents 4811915 + 5a258d7 commit 251c833
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 10 deletions.
40 changes: 30 additions & 10 deletions src/core/qgsvectorlayerjoinbuffer.cpp
Expand Up @@ -76,12 +76,6 @@ bool QgsVectorLayerJoinBuffer::addJoin( const QgsVectorLayerJoinInfo &joinInfo )
return false;
}

//cache joined layer to virtual memory if specified by user
if ( joinInfo.isUsingMemoryCache() )
{
cacheJoinLayer( mVectorJoins.last() );
}

// Wait for notifications about changed fields in joined layer to propagate them.
// During project load the joined layers possibly do not exist yet so the connection will not be created,
// but then QgsProject makes sure to call createJoinCaches() which will do the connection.
Expand All @@ -91,9 +85,16 @@ bool QgsVectorLayerJoinBuffer::addJoin( const QgsVectorLayerJoinInfo &joinInfo )
connectJoinedLayer( vl );
}

mLayer->updateFields();

//cache joined layer to virtual memory if specified by user
if ( joinInfo.isUsingMemoryCache() )
{
cacheJoinLayer( mVectorJoins.last() );
}

locker.unlock();

emit joinedFieldsChanged();
return true;
}

Expand Down Expand Up @@ -170,9 +171,28 @@ void QgsVectorLayerJoinBuffer::cacheJoinLayer( QgsVectorLayerJoinInfo &joinInfo
}
else
{
QgsAttributes attrs2 = attrs;
attrs2.remove( joinFieldIndex ); // skip the join field to avoid double field names (fields often have the same name)
joinInfo.cachedAttributes.insert( key, attrs2 );
QgsAttributes attributesCache;
for ( int i = 0; i < attrs.size(); i++ )
{
if ( i == joinFieldIndex )
continue;

QString joinInfoPrefix = joinInfo.prefix();
if ( joinInfoPrefix.isNull() ) // Default prefix 'layerName_' used
joinInfoPrefix = QString( "%1_" ).arg( cacheLayer->name() );

// Joined field name
const QString joinFieldName = joinInfoPrefix + cacheLayer->fields().names().at( i );

// Check for name collisions
int fieldIndex = mLayer->fields().indexFromName( joinFieldName );
if ( fieldIndex >= 0
&& mLayer->fields().fieldOrigin( fieldIndex ) != QgsFields::OriginJoin )
continue;

attributesCache.append( attrs.at( i ) );
}
joinInfo.cachedAttributes.insert( key, attributesCache );
}
}
joinInfo.cacheDirty = false;
Expand Down
43 changes: 43 additions & 0 deletions tests/src/core/testqgsvectorlayerjoinbuffer.cpp
Expand Up @@ -67,6 +67,7 @@ class TestVectorLayerJoinBuffer : public QObject
void testResolveReferences();
void testSignals();
void testChangeAttributeValues();
void testCollidingNameColumnCached();

private:
QgsProject mProject;
Expand Down Expand Up @@ -840,6 +841,48 @@ void TestVectorLayerJoinBuffer::testChangeAttributeValues()

}

void TestVectorLayerJoinBuffer::testCollidingNameColumnCached()
{
mProject.clear();
QgsVectorLayer *vlA = new QgsVectorLayer( QStringLiteral( "Point?field=id_a:integer&field=name" ), QStringLiteral( "cacheA" ), QStringLiteral( "memory" ) );
QVERIFY( vlA->isValid() );
QgsVectorLayer *vlB = new QgsVectorLayer( QStringLiteral( "Point?field=id_b:integer&field=name&field=value_b&field=value_c" ), QStringLiteral( "cacheB" ), QStringLiteral( "memory" ) );
QVERIFY( vlB->isValid() );
mProject.addMapLayer( vlA );
mProject.addMapLayer( vlB );

QgsFeature fA1( vlA->dataProvider()->fields(), 1 );
fA1.setAttribute( QStringLiteral( "id_a" ), 1 );
fA1.setAttribute( QStringLiteral( "name" ), QStringLiteral( "name_a" ) );

vlA->dataProvider()->addFeatures( QgsFeatureList() << fA1 );

QgsFeature fB1( vlB->dataProvider()->fields(), 1 );
fB1.setAttribute( QStringLiteral( "id_b" ), 1 );
fB1.setAttribute( QStringLiteral( "name" ), QStringLiteral( "name_b" ) );
fB1.setAttribute( QStringLiteral( "value_b" ), QStringLiteral( "value_b" ) );
fB1.setAttribute( QStringLiteral( "value_c" ), QStringLiteral( "value_c" ) );

vlB->dataProvider()->addFeatures( QgsFeatureList() << fB1 );

QgsVectorLayerJoinInfo joinInfo;
joinInfo.setTargetFieldName( QStringLiteral( "id_a" ) );
joinInfo.setJoinLayer( vlB );
joinInfo.setJoinFieldName( QStringLiteral( "id_b" ) );
joinInfo.setPrefix( QStringLiteral( "" ) );
joinInfo.setEditable( true );
joinInfo.setUpsertOnEdit( false );
joinInfo.setUsingMemoryCache( true );
vlA->addJoin( joinInfo );

QgsFeatureIterator fi1 = vlA->getFeatures();
fi1.nextFeature( fA1 );
QCOMPARE( fA1.fields().names(), QStringList( {"id_a", "name", "value_b", "value_c"} ) );
QCOMPARE( fA1.attribute( "id_a" ).toInt(), 1 );
QCOMPARE( fA1.attribute( "name" ).toString(), QStringLiteral( "name_a" ) );
QCOMPARE( fA1.attribute( "value_b" ).toString(), QStringLiteral( "value_b" ) );
QCOMPARE( fA1.attribute( "value_c" ).toString(), QStringLiteral( "value_c" ) );
}

QGSTEST_MAIN( TestVectorLayerJoinBuffer )
#include "testqgsvectorlayerjoinbuffer.moc"
Expand Down

0 comments on commit 251c833

Please sign in to comment.