Skip to content

Commit 0971879

Browse files
committedMay 11, 2012
Invalidate cache layers if project files are changed
1 parent 19a5a47 commit 0971879

File tree

6 files changed

+71
-10
lines changed

6 files changed

+71
-10
lines changed
 

‎src/mapserver/qgshostedrdsbuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ QgsMapLayer* QgsHostedRDSBuilder::createMapLayer( const QDomElement& elem,
6767
rl = new QgsRasterLayer( uri, layerNameFromUri( uri ) );
6868
if ( allowCaching )
6969
{
70-
QgsMSLayerCache::instance()->insertLayer( uri, layerName, rl );
70+
QgsMSLayerCache::instance()->insertLayer( uri, layerName, rl, "" /*todo: add project file path*/ );
7171
}
7272
else
7373
{

‎src/mapserver/qgshostedvdsbuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ QgsMapLayer* QgsHostedVDSBuilder::createMapLayer( const QDomElement& elem,
7575

7676
if ( allowCaching )
7777
{
78-
QgsMSLayerCache::instance()->insertLayer( uri, layerName, ml );
78+
QgsMSLayerCache::instance()->insertLayer( uri, layerName, ml, "" /*todo: add project file path*/ );
7979
}
8080
else
8181
{

‎src/mapserver/qgsmslayercache.cpp

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ QgsMSLayerCache* QgsMSLayerCache::instance()
3636

3737
QgsMSLayerCache::QgsMSLayerCache()
3838
{
39+
QObject::connect( &mFileSystemWatcher, SIGNAL( fileChanged( const QString& ) ), this, SLOT( removeProjectFileLayers( const QString& ) ) );
3940
}
4041

4142
QgsMSLayerCache::~QgsMSLayerCache()
@@ -48,7 +49,7 @@ QgsMSLayerCache::~QgsMSLayerCache()
4849
delete mInstance;
4950
}
5051

51-
void QgsMSLayerCache::insertLayer( const QString& url, const QString& layerName, QgsMapLayer* layer, const QList<QString>& tempFiles )
52+
void QgsMSLayerCache::insertLayer( const QString& url, const QString& layerName, QgsMapLayer* layer, const QString& configFile, const QList<QString>& tempFiles )
5253
{
5354
QgsDebugMsg( "inserting layer" );
5455
if ( mEntries.size() > std::max( DEFAULT_MAX_N_LAYERS, mProjectMaxLayers ) ) //force cache layer examination after 10 inserted layers
@@ -69,8 +70,21 @@ void QgsMSLayerCache::insertLayer( const QString& url, const QString& layerName,
6970
newEntry.creationTime = time( NULL );
7071
newEntry.lastUsedTime = time( NULL );
7172
newEntry.temporaryFiles = tempFiles;
73+
newEntry.configFile = configFile;
7274

7375
mEntries.insert( urlLayerPair, newEntry );
76+
77+
//update config file map
78+
QHash< QString, int >::iterator configIt = mConfigFiles.find( configFile );
79+
if ( configIt == mConfigFiles.end() )
80+
{
81+
mConfigFiles.insert( configFile, 1 );
82+
mFileSystemWatcher.addPath( configFile );
83+
}
84+
else
85+
{
86+
mConfigFiles[configFile] = configIt.value() + 1; //increment reference counter
87+
}
7488
}
7589

7690
QgsMapLayer* QgsMSLayerCache::searchLayer( const QString& url, const QString& layerName )
@@ -98,6 +112,27 @@ QgsMapLayer* QgsMSLayerCache::searchLayer( const QString& url, const QString& la
98112
}
99113
}
100114

115+
void QgsMSLayerCache::removeProjectFileLayers( const QString& project )
116+
{
117+
QList< QPair< QString, QString > > removeEntries;
118+
119+
QHash<QPair<QString, QString>, QgsMSLayerCacheEntry>::iterator entryIt = mEntries.begin();
120+
for ( ; entryIt != mEntries.end(); ++entryIt )
121+
{
122+
if ( entryIt.value().configFile == project )
123+
{
124+
removeEntries.push_back( entryIt.key() );
125+
freeEntryRessources( entryIt.value() );
126+
}
127+
}
128+
129+
QList< QPair< QString, QString > >::const_iterator removeIt = removeEntries.constBegin();
130+
for ( ; removeIt != removeEntries.constEnd(); ++removeIt )
131+
{
132+
mEntries.remove( *removeIt );
133+
}
134+
}
135+
101136
void QgsMSLayerCache::updateEntries()
102137
{
103138
QgsDebugMsg( "updateEntries" );
@@ -142,7 +177,7 @@ void QgsMSLayerCache::freeEntryRessources( QgsMSLayerCacheEntry& entry )
142177
{
143178
delete entry.layerPointer;
144179

145-
//todo: remove the temporary files of a layer
180+
//remove the temporary files of a layer
146181
foreach( QString file, entry.temporaryFiles )
147182
{
148183
//remove the temporary file
@@ -153,4 +188,16 @@ void QgsMSLayerCache::freeEntryRessources( QgsMSLayerCacheEntry& entry )
153188
QgsDebugMsg( removeFile.errorString() );
154189
}
155190
}
191+
192+
//counter
193+
int configFileCount = mConfigFiles[entry.configFile];
194+
if ( configFileCount < 2 )
195+
{
196+
mConfigFiles.remove( entry.configFile );
197+
mFileSystemWatcher.removePath( entry.configFile );
198+
}
199+
else
200+
{
201+
mConfigFiles[entry.configFile] = configFileCount - 1;
202+
}
156203
}

‎src/mapserver/qgsmslayercache.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
#define QGSMSLAYERCACHE_H
2020

2121
#include <time.h>
22+
#include <QFileSystemWatcher>
2223
#include <QHash>
24+
#include <QObject>
2325
#include <QPair>
2426
#include <QString>
2527

@@ -32,11 +34,12 @@ struct QgsMSLayerCacheEntry
3234
QString url; //datasource url
3335
QgsMapLayer* layerPointer;
3436
QList<QString> temporaryFiles; //path to the temporary files written for the layer
37+
QString configFile; //path to the project file associated with the layer
3538
};
3639

3740
/**A singleton class that caches layer objects for the
3841
QGIS mapserver*/
39-
class QgsMSLayerCache
42+
class QgsMSLayerCache: public QObject
4043
{
4144
public:
4245
static QgsMSLayerCache* instance();
@@ -46,7 +49,7 @@ class QgsMSLayerCache
4649
@param url the layer datasource
4750
@param layerName the layer name (to distinguish between different layers in a request using the same datasource
4851
@param tempFiles some layers have temporary files. The cash makes sure they are removed when removing the layer from the cash*/
49-
void insertLayer( const QString& url, const QString& layerName, QgsMapLayer* layer, const QList<QString>& tempFiles = QList<QString>() );
52+
void insertLayer( const QString& url, const QString& layerName, QgsMapLayer* layer, const QString& configFile, const QList<QString>& tempFiles = QList<QString>() );
5053
/**Searches for the layer with the given url.
5154
@return a pointer to the layer or 0 if no such layer*/
5255
QgsMapLayer* searchLayer( const QString& url, const QString& layerName );
@@ -75,8 +78,19 @@ class QgsMSLayerCache
7578
layer names*/
7679
QHash<QPair<QString, QString>, QgsMSLayerCacheEntry> mEntries;
7780

81+
/**Config files used in the cache (with reference counter)*/
82+
QHash< QString, int > mConfigFiles;
83+
84+
/**Check for configuration file updates (remove layers from cache if configuration file changes)*/
85+
QFileSystemWatcher mFileSystemWatcher;
86+
7887
/**Maximum number of layers in the cache, overrides DEFAULT_MAX_N_LAYERS if larger*/
7988
int mProjectMaxLayers;
89+
90+
private slots:
91+
92+
/**Removes entries from a project (e.g. if a project file has changed)*/
93+
void removeProjectFileLayers( const QString& project );
8094
};
8195

8296
#endif

‎src/mapserver/qgsprojectparser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ QgsMapLayer* QgsProjectParser::createLayerFromElement( const QDomElement& elem,
10051005
layer->setLayerName( layerName( elem ) );
10061006
if ( useCache )
10071007
{
1008-
QgsMSLayerCache::instance()->insertLayer( absoluteUri, id, layer );
1008+
QgsMSLayerCache::instance()->insertLayer( absoluteUri, id, layer, mProjectPath );
10091009
}
10101010
else
10111011
{

‎src/mapserver/qgsremoteowsbuilder.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ QgsMapLayer* QgsRemoteOWSBuilder::createMapLayer(
115115
{
116116
if ( allowCaching )
117117
{
118-
QgsMSLayerCache::instance()->insertLayer( url, layerName, result );
118+
QgsMSLayerCache::instance()->insertLayer( url, layerName, result, "" /*todo: add project file path*/ );
119119
}
120120
else
121121
{
@@ -215,7 +215,7 @@ QgsRasterLayer* QgsRemoteOWSBuilder::wmsLayerFromUrl( const QString& url, const
215215
//insert into cache
216216
if ( allowCaching )
217217
{
218-
QgsMSLayerCache::instance()->insertLayer( url, layerName, result );
218+
QgsMSLayerCache::instance()->insertLayer( url, layerName, result, "" /*todo: add project file path*/ );
219219
}
220220
else
221221
{
@@ -414,7 +414,7 @@ QgsVectorLayer* QgsRemoteOWSBuilder::sosLayer( const QDomElement& remoteOWSElem,
414414
{
415415
if ( allowCaching )
416416
{
417-
QgsMSLayerCache::instance()->insertLayer( providerUrl, layerName, sosLayer );
417+
QgsMSLayerCache::instance()->insertLayer( providerUrl, layerName, sosLayer, "" /*todo: add project file path*/ );
418418
}
419419
else
420420
{

0 commit comments

Comments
 (0)
Please sign in to comment.