/
qgsmslayercache.cpp
156 lines (136 loc) · 4.39 KB
/
qgsmslayercache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/***************************************************************************
qgsmslayercache.cpp
-------------------
begin : September 22, 2007
copyright : (C) 2007 by Marco Hugentobler
email : marco dot hugentobler at karto dot baug dot ethz dot ch
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsmslayercache.h"
#include "qgsvectorlayer.h"
#include "qgslogger.h"
#include <QFile>
//maximum number of layers in the cache
#define DEFAULT_MAX_N_LAYERS 100
QgsMSLayerCache* QgsMSLayerCache::mInstance = 0;
QgsMSLayerCache* QgsMSLayerCache::instance()
{
if ( !mInstance )
{
mInstance = new QgsMSLayerCache();
}
return mInstance;
}
QgsMSLayerCache::QgsMSLayerCache()
{
}
QgsMSLayerCache::~QgsMSLayerCache()
{
QgsDebugMsg( "removing all entries" );
foreach( QgsMSLayerCacheEntry entry, mEntries )
{
delete entry.layerPointer;
}
delete mInstance;
}
void QgsMSLayerCache::insertLayer( const QString& url, const QString& layerName, QgsMapLayer* layer, const QList<QString>& tempFiles )
{
QgsDebugMsg( "inserting layer" );
if ( mEntries.size() > std::max( DEFAULT_MAX_N_LAYERS, mProjectMaxLayers ) ) //force cache layer examination after 10 inserted layers
{
updateEntries();
}
QPair<QString, QString> urlLayerPair = qMakePair( url, layerName );
QHash<QPair<QString, QString>, QgsMSLayerCacheEntry>::iterator it = mEntries.find( urlLayerPair );
if ( it != mEntries.end() )
{
delete it.value().layerPointer;
}
QgsMSLayerCacheEntry newEntry;
newEntry.layerPointer = layer;
newEntry.url = url;
newEntry.creationTime = time( NULL );
newEntry.lastUsedTime = time( NULL );
newEntry.temporaryFiles = tempFiles;
mEntries.insert( urlLayerPair, newEntry );
}
QgsMapLayer* QgsMSLayerCache::searchLayer( const QString& url, const QString& layerName )
{
QPair<QString, QString> urlNamePair = qMakePair( url, layerName );
if ( !mEntries.contains( urlNamePair ) )
{
QgsDebugMsg( "Layer not found in cache" );
return 0;
}
else
{
QgsMSLayerCacheEntry &entry = mEntries[ urlNamePair ];
entry.lastUsedTime = time( NULL );
#ifdef DIAGRAMSERVER
//delete any existing diagram overlays in vectorlayers
QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( entry.layerPointer );
if ( vl )
{
vl->removeOverlay( "diagram" );
}
#endif //DIAGRAMSERVER
QgsDebugMsg( "Layer found in cache" );
return entry.layerPointer;
}
}
void QgsMSLayerCache::updateEntries()
{
QgsDebugMsg( "updateEntries" );
int entriesToDelete = mEntries.size() - std::max( DEFAULT_MAX_N_LAYERS, mProjectMaxLayers );
if ( entriesToDelete < 1 )
{
return;
}
for ( int i = 0; i < entriesToDelete; ++i )
{
removeLeastUsedEntry();
}
}
void QgsMSLayerCache::removeLeastUsedEntry()
{
if ( mEntries.size() < 1 )
{
return;
}
QgsDebugMsg( "removeLeastUsedEntry" );
QHash<QPair<QString, QString>, QgsMSLayerCacheEntry>::iterator it = mEntries.begin();
QHash<QPair<QString, QString>, QgsMSLayerCacheEntry>::iterator lowest_it = it;
time_t lowest_time = it->lastUsedTime;
for ( ; it != mEntries.end(); ++it )
{
if ( it->lastUsedTime < lowest_time )
{
lowest_it = it;
lowest_time = it->lastUsedTime;
}
}
freeEntryRessources( *lowest_it );
mEntries.erase( lowest_it );
}
void QgsMSLayerCache::freeEntryRessources( QgsMSLayerCacheEntry& entry )
{
delete entry.layerPointer;
//todo: remove the temporary files of a layer
foreach( QString file, entry.temporaryFiles )
{
//remove the temporary file
QFile removeFile( file );
if ( !removeFile.remove() )
{
QgsDebugMsg( "could not remove file: " + file );
QgsDebugMsg( removeFile.errorString() );
}
}
}