Skip to content

Commit 22c8bef

Browse files
committed
Cache tags in QgsStyle to avoid costly db lookups
1 parent e4733bc commit 22c8bef

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

src/core/symbology/qgsstyle.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ void QgsStyle::clear()
8181

8282
mSymbols.clear();
8383
mColorRamps.clear();
84+
mCachedColorRampTags.clear();
85+
mCachedSymbolTags.clear();
8486
}
8587

8688
bool QgsStyle::addSymbol( const QString &name, QgsSymbol *symbol, bool update )
@@ -164,6 +166,7 @@ bool QgsStyle::removeSymbol( const QString &name )
164166
const bool result = remove( SymbolEntity, symbolid );
165167
if ( result )
166168
{
169+
mCachedSymbolTags.remove( name );
167170
emit symbolRemoved( name );
168171
}
169172
return result;
@@ -259,6 +262,8 @@ bool QgsStyle::removeColorRamp( const QString &name )
259262
return false;
260263
}
261264

265+
mCachedColorRampTags.remove( name );
266+
262267
emit rampRemoved( name );
263268

264269
return true;
@@ -494,6 +499,8 @@ bool QgsStyle::renameSymbol( const QString &oldName, const QString &newName )
494499
return false;
495500
}
496501

502+
mCachedSymbolTags.remove( oldName );
503+
497504
const bool result = rename( SymbolEntity, symbolid, newName );
498505
if ( result )
499506
emit symbolRenamed( oldName, newName );
@@ -514,6 +521,7 @@ bool QgsStyle::renameColorRamp( const QString &oldName, const QString &newName )
514521
return false;
515522

516523
mColorRamps.insert( newName, ramp );
524+
mCachedColorRampTags.remove( oldName );
517525

518526
int rampid = 0;
519527
sqlite3_statement_unique_ptr statement;
@@ -681,9 +689,17 @@ bool QgsStyle::rename( StyleEntity type, int id, const QString &newName )
681689
}
682690
else
683691
{
692+
mCachedColorRampTags.clear();
693+
mCachedSymbolTags.clear();
694+
684695
switch ( type )
685696
{
686697
case TagEntity:
698+
{
699+
emit groupsModified();
700+
break;
701+
}
702+
687703
case SmartgroupEntity:
688704
{
689705
emit groupsModified();
@@ -727,6 +743,9 @@ bool QgsStyle::remove( StyleEntity type, int id )
727743
}
728744
else
729745
{
746+
mCachedColorRampTags.clear();
747+
mCachedSymbolTags.clear();
748+
730749
if ( groupRemoved )
731750
{
732751
QgsSettings settings;
@@ -917,6 +936,7 @@ bool QgsStyle::tagSymbol( StyleEntity type, const QString &symbol, const QString
917936
}
918937
}
919938

939+
clearCachedTags( type, symbol );
920940
emit entityTagsChanged( type, symbol, tagsOfSymbol( type, symbol ) );
921941

922942
return true;
@@ -969,6 +989,7 @@ bool QgsStyle::detagSymbol( StyleEntity type, const QString &symbol, const QStri
969989
}
970990
}
971991

992+
clearCachedTags( type, symbol );
972993
emit entityTagsChanged( type, symbol, tagsOfSymbol( type, symbol ) );
973994

974995
// TODO Perform tag cleanup
@@ -1008,6 +1029,7 @@ bool QgsStyle::detagSymbol( StyleEntity type, const QString &symbol )
10081029
: QgsSqlite3Mprintf( "DELETE FROM ctagmap WHERE colorramp_id=%d", symbolid );
10091030
runEmptyQuery( query );
10101031

1032+
clearCachedTags( type, symbol );
10111033
emit entityTagsChanged( type, symbol, QStringList() );
10121034

10131035
// TODO Perform tag cleanup
@@ -1018,6 +1040,23 @@ bool QgsStyle::detagSymbol( StyleEntity type, const QString &symbol )
10181040

10191041
QStringList QgsStyle::tagsOfSymbol( StyleEntity type, const QString &symbol )
10201042
{
1043+
switch ( type )
1044+
{
1045+
case SymbolEntity:
1046+
if ( mCachedSymbolTags.contains( symbol ) )
1047+
return mCachedSymbolTags.value( symbol );
1048+
break;
1049+
1050+
case ColorrampEntity:
1051+
if ( mCachedColorRampTags.contains( symbol ) )
1052+
return mCachedColorRampTags.value( symbol );
1053+
break;
1054+
1055+
case TagEntity:
1056+
case SmartgroupEntity:
1057+
break;
1058+
}
1059+
10211060
if ( !mCurrentDB )
10221061
{
10231062
QgsDebugMsg( QStringLiteral( "Sorry! Cannot open database for getting the tags." ) );
@@ -1050,6 +1089,22 @@ QStringList QgsStyle::tagsOfSymbol( StyleEntity type, const QString &symbol )
10501089
}
10511090
}
10521091

1092+
// update cache
1093+
switch ( type )
1094+
{
1095+
case SymbolEntity:
1096+
mCachedSymbolTags[ symbol ] = tagList;
1097+
break;
1098+
1099+
case ColorrampEntity:
1100+
mCachedColorRampTags[ symbol ] = tagList;
1101+
break;
1102+
1103+
case TagEntity:
1104+
case SmartgroupEntity:
1105+
break;
1106+
}
1107+
10531108
return tagList;
10541109
}
10551110

@@ -1707,3 +1762,21 @@ bool QgsStyle::updateSymbol( StyleEntity type, const QString &name )
17071762
}
17081763
return true;
17091764
}
1765+
1766+
void QgsStyle::clearCachedTags( QgsStyle::StyleEntity type, const QString &name )
1767+
{
1768+
switch ( type )
1769+
{
1770+
case SymbolEntity:
1771+
mCachedSymbolTags.remove( name );
1772+
break;
1773+
1774+
case ColorrampEntity:
1775+
mCachedColorRampTags.remove( name );
1776+
break;
1777+
1778+
case TagEntity:
1779+
case SmartgroupEntity:
1780+
break;
1781+
}
1782+
}

src/core/symbology/qgsstyle.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,9 @@ class CORE_EXPORT QgsStyle : public QObject
515515
QgsSymbolMap mSymbols;
516516
QgsVectorColorRampMap mColorRamps;
517517

518+
QHash< QString, QStringList > mCachedSymbolTags;
519+
QHash< QString, QStringList > mCachedColorRampTags;
520+
518521
QString mErrorString;
519522
QString mFileName;
520523

@@ -549,6 +552,8 @@ class CORE_EXPORT QgsStyle : public QObject
549552
*/
550553
bool updateSymbol( StyleEntity type, const QString &name );
551554

555+
void clearCachedTags( StyleEntity type, const QString &name );
556+
552557
Q_DISABLE_COPY( QgsStyle )
553558
};
554559

0 commit comments

Comments
 (0)