Skip to content

Commit

Permalink
Cache tags in QgsStyle to avoid costly db lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 13, 2018
1 parent e4733bc commit 22c8bef
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/core/symbology/qgsstyle.cpp
Expand Up @@ -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 )
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -259,6 +262,8 @@ bool QgsStyle::removeColorRamp( const QString &name )
return false;
}

mCachedColorRampTags.remove( name );

emit rampRemoved( name );

return true;
Expand Down Expand Up @@ -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 );
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -727,6 +743,9 @@ bool QgsStyle::remove( StyleEntity type, int id )
}
else
{
mCachedColorRampTags.clear();
mCachedSymbolTags.clear();

if ( groupRemoved )
{
QgsSettings settings;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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." ) );
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
5 changes: 5 additions & 0 deletions src/core/symbology/qgsstyle.h
Expand Up @@ -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;

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

Expand Down

0 comments on commit 22c8bef

Please sign in to comment.