Cache tags in QgsStyle to avoid costly db lookups

This commit is contained in:
Nyall Dawson 2018-09-13 12:52:34 +10:00
parent e4733bcb29
commit 22c8befe6e
2 changed files with 78 additions and 0 deletions

View File

@ -81,6 +81,8 @@ void QgsStyle::clear()
mSymbols.clear();
mColorRamps.clear();
mCachedColorRampTags.clear();
mCachedSymbolTags.clear();
}
bool QgsStyle::addSymbol( const QString &name, QgsSymbol *symbol, bool update )
@ -164,6 +166,7 @@ bool QgsStyle::removeSymbol( const QString &name )
const bool result = remove( SymbolEntity, symbolid );
if ( result )
{
mCachedSymbolTags.remove( name );
emit symbolRemoved( name );
}
return result;
@ -259,6 +262,8 @@ bool QgsStyle::removeColorRamp( const QString &name )
return false;
}
mCachedColorRampTags.remove( name );
emit rampRemoved( name );
return true;
@ -494,6 +499,8 @@ bool QgsStyle::renameSymbol( const QString &oldName, const QString &newName )
return false;
}
mCachedSymbolTags.remove( oldName );
const bool result = rename( SymbolEntity, symbolid, newName );
if ( result )
emit symbolRenamed( oldName, newName );
@ -514,6 +521,7 @@ bool QgsStyle::renameColorRamp( const QString &oldName, const QString &newName )
return false;
mColorRamps.insert( newName, ramp );
mCachedColorRampTags.remove( oldName );
int rampid = 0;
sqlite3_statement_unique_ptr statement;
@ -681,9 +689,17 @@ bool QgsStyle::rename( StyleEntity type, int id, const QString &newName )
}
else
{
mCachedColorRampTags.clear();
mCachedSymbolTags.clear();
switch ( type )
{
case TagEntity:
{
emit groupsModified();
break;
}
case SmartgroupEntity:
{
emit groupsModified();
@ -727,6 +743,9 @@ bool QgsStyle::remove( StyleEntity type, int id )
}
else
{
mCachedColorRampTags.clear();
mCachedSymbolTags.clear();
if ( groupRemoved )
{
QgsSettings settings;
@ -917,6 +936,7 @@ bool QgsStyle::tagSymbol( StyleEntity type, const QString &symbol, const QString
}
}
clearCachedTags( type, symbol );
emit entityTagsChanged( type, symbol, tagsOfSymbol( type, symbol ) );
return true;
@ -969,6 +989,7 @@ bool QgsStyle::detagSymbol( StyleEntity type, const QString &symbol, const QStri
}
}
clearCachedTags( type, symbol );
emit entityTagsChanged( type, symbol, tagsOfSymbol( type, symbol ) );
// TODO Perform tag cleanup
@ -1008,6 +1029,7 @@ bool QgsStyle::detagSymbol( StyleEntity type, const QString &symbol )
: QgsSqlite3Mprintf( "DELETE FROM ctagmap WHERE colorramp_id=%d", symbolid );
runEmptyQuery( query );
clearCachedTags( type, symbol );
emit entityTagsChanged( type, symbol, QStringList() );
// TODO Perform tag cleanup
@ -1018,6 +1040,23 @@ bool QgsStyle::detagSymbol( StyleEntity type, const QString &symbol )
QStringList QgsStyle::tagsOfSymbol( StyleEntity type, const QString &symbol )
{
switch ( type )
{
case SymbolEntity:
if ( mCachedSymbolTags.contains( symbol ) )
return mCachedSymbolTags.value( symbol );
break;
case ColorrampEntity:
if ( mCachedColorRampTags.contains( symbol ) )
return mCachedColorRampTags.value( symbol );
break;
case TagEntity:
case SmartgroupEntity:
break;
}
if ( !mCurrentDB )
{
QgsDebugMsg( QStringLiteral( "Sorry! Cannot open database for getting the tags." ) );
@ -1050,6 +1089,22 @@ QStringList QgsStyle::tagsOfSymbol( StyleEntity type, const QString &symbol )
}
}
// update cache
switch ( type )
{
case SymbolEntity:
mCachedSymbolTags[ symbol ] = tagList;
break;
case ColorrampEntity:
mCachedColorRampTags[ symbol ] = tagList;
break;
case TagEntity:
case SmartgroupEntity:
break;
}
return tagList;
}
@ -1707,3 +1762,21 @@ bool QgsStyle::updateSymbol( StyleEntity type, const QString &name )
}
return true;
}
void QgsStyle::clearCachedTags( QgsStyle::StyleEntity type, const QString &name )
{
switch ( type )
{
case SymbolEntity:
mCachedSymbolTags.remove( name );
break;
case ColorrampEntity:
mCachedColorRampTags.remove( name );
break;
case TagEntity:
case SmartgroupEntity:
break;
}
}

View File

@ -515,6 +515,9 @@ class CORE_EXPORT QgsStyle : public QObject
QgsSymbolMap mSymbols;
QgsVectorColorRampMap mColorRamps;
QHash< QString, QStringList > mCachedSymbolTags;
QHash< QString, QStringList > mCachedColorRampTags;
QString mErrorString;
QString mFileName;
@ -549,6 +552,8 @@ class CORE_EXPORT QgsStyle : public QObject
*/
bool updateSymbol( StyleEntity type, const QString &name );
void clearCachedTags( StyleEntity type, const QString &name );
Q_DISABLE_COPY( QgsStyle )
};