Repository navigation
Expand file tree
/
Copy pathWMTSLayer.cpp
More file actions
345 lines (272 loc) · 6.51 KB
/
Copy pathWMTSLayer.cpp
File metadata and controls
345 lines (272 loc) · 6.51 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "StdAfx.h"
#include <memory.h>
#include <string.h>
#include <stdio.h>
#include <io.h>
#include <CLogThreadMgr.h>
#include "WMTSLevel.h"
#include "LayerLRUCache.h"
#include "Tile.h"
#include "WMTSLayer.h"
#include "Bundle.h"
#include "bdi/bdiapi.h"
#pragma warning(once:4996)
using namespace thp;
thp::TBundleRecord::TBundleRecord()
{
}
thp::TBundleRecord::~TBundleRecord()
{
}
std::tr1::shared_ptr<Bundle> thp::TBundleRecord::loadBundle(WMTSLevel* pLv)
{
try
{
{
QReadLocker locker(&rwLocker);
if( !wpBundle.expired() )
{
std::tr1::shared_ptr<Bundle> sp = wpBundle.lock();
return sp;
}
}
QWriteLocker locker(&rwLocker);
std::tr1::shared_ptr<Bundle> sp = pLv->getBundle( *this );
wpBundle = sp;
return sp;
}
catch(...)
{
//LOG(ERROR) << "分配资源错误";
std::cerr << "分配资源错误";
std::tr1::shared_ptr<Bundle> sp;
return sp;
}
}
thp::WMTSLayer::WMTSLayer()
{
memset(m_pLvl, 0, THP_MAX_LEVEL);
memset(m_szPath, 0, THP_MAX_PATH);
m_pLyrLRU = new LayerLRUCache(THP_WMTS_DEFAULT_LRU_CACHE);
m_pBundleRecords = NULL;
m_eGTS = GTS_IO;
#ifdef _THP_TJ
m_nCount = 0;
m_nENum = 0;
m_nMNum = 0;
#endif// _THP_TJ
// 哈希表中资源超过1000万(含)时到整理时间会整理资源
m_nMaintainLine = 100000000;
m_nSpan = 200;
_initLogWriter();
}
thp::WMTSLayer::~WMTSLayer()
{
delete m_pLyrLRU;
m_pLyrLRU = NULL;
int nCount = HASH_COUNT(m_pBundleRecords);
std::cout << "delete hash..." << std::endl;
std::cout << "bundle count:" << nCount << std::endl;
TBundleRecord* p = NULL, *pTemp = NULL;
HASH_ITER(hh, m_pBundleRecords, p, pTemp)
{
HASH_DEL(m_pBundleRecords, p);
// 释放bundle资源
delete p;
}
std::cout << "hash deleted" << std::endl;
std::cout << "delete level..." << std::endl;
for (int i = 0; i < THP_MAX_LEVEL; ++i)
{
WMTSLevel* pLv = m_pLvl[i];
delete pLv;
pLv = NULL;
}
std::cout << "level deleted" << std::endl;
}
bool WMTSLayer::_initLogWriter()
{
// 获取写日志对象
m_pLogWriter = CLogThreadMgr::instance()->getLogWriter("WMTSLayer.csv");
// 如果不存在,创建日志文件,加载日志配置
if (m_pLogWriter == NULL)
{
CLogAppender * pLogAppender = new CLogAppender("WMTSLayer", "WMTSLayer.csv", "", "DebugLog");
// 获取写日志对象
m_pLogWriter = CLogThreadMgr::instance()->createLogWriter(pLogAppender);
}
return true;
}
void thp::WMTSLayer::setCacheMbSize(unsigned int nMemByMB)
{
// 换算成kb
m_pLyrLRU->setCapacity( nMemByMB<<10 );
}
thp::WMTSLevel* thp::WMTSLayer::getLevel(int nLvl)
{
if(nLvl<0 || nLvl >= THP_MAX_LEVEL)
return 0;
return m_pLvl[nLvl];
}
int thp::WMTSLayer::loadData(int nLvl)
{
if( nLvl > THP_MAX_LEVEL)
{
// log
return 0;
}// 超出定义最大范围
int nCount = _clacBundleCount(nLvl);
int i = 0;
TBundleIDex tId;
return 0;
}
void WMTSLayer::_calcBundleNo(int nLvl, int nRow, int nCol, TBundleIDex& tNo)
{
tNo.tID.unLv = (unsigned int)nLvl;
if(nLvl < 8)
{
tNo.tID.unBundleIDinLv = 0;
tNo.nBundleRow = 0;
tNo.nBundleCol = 0;
return ;
}
// 该等级下Bundle的行数
int nBundleRowNum = 1 << (nLvl - 8);
tNo.nBundleRow = nRow >> 7;
// 瓦片所在Bundle所在的列索引
tNo.nBundleCol = nCol >> 7;
tNo.tID.unBundleIDinLv = (unsigned int)( (tNo.nBundleCol * nBundleRowNum) + tNo.nBundleRow );
}
int thp::WMTSLayer::_clacBundleCount(int nLvl)
{
if(nLvl < 8)
return 1;
// 该等级下Bundle的行数
return 1 << (2*nLvl - 15);
}
void thp::WMTSLayer::setMemStrategy(MemStrategy eGTS)
{
m_eGTS = eGTS;
}
thp::WMTSLayer::MemStrategy thp::WMTSLayer::getMemStrategy() const
{
return m_eGTS;
}
bool thp::WMTSLayer::setPath(const char* szPath)
{
unsigned int unLen = (unsigned int)strlen(szPath);
if(0 == unLen || unLen > THP_MAX_PATH)
return false;
memcpy(m_szPath, szPath, unLen);
return true;
}
void thp::WMTSLayer::showStatus()
{
QMutexLocker locker(&m_pRecordsMutex);
TBundleRecord* p = NULL, *pTemp = NULL;
std::cout << "-----------hash status---------" << std::endl;
int nCount = HASH_COUNT(m_pBundleRecords);
std::cout << "hash count :" << nCount << std::endl;
std::cout << "--in memory--" << std::endl;
int nCountInMem = 0;
HASH_ITER(hh, m_pBundleRecords, p, pTemp)
{
if( !p->wpBundle.expired() )
{
std::tr1::shared_ptr<Bundle> spBundle = p->wpBundle.lock();
if( spBundle->isCached() )
{
std::cout << spBundle->getPath() << std::endl;
++nCountInMem;
}
}
}
std::cout << "memory count :" << nCountInMem << std::endl;
std::cout << "-----------hash END-------------------" << std::endl;
std::cout << "-----------LRU status-------------------" << std::endl;
m_pLyrLRU->showStatus();
std::cout << "-----------LRU END-------------------" << std::endl;
}
void thp::WMTSLayer::maintain(bool bForce)
{
QMutexLocker locker(&m_pRecordsMutex);
int nRecordCount = HASH_COUNT(m_pBundleRecords);
if(nRecordCount < m_nMaintainLine)
return ;
TBundleRecord* p = NULL, *pTemp = NULL;
std::string sLayerName = getName();
std::cout << "正在维护图层["<< sLayerName << "]..." << std::endl;
bool bMaintainedData = false;
if(bForce)
{
// 记录要删除的记录
HASH_ITER(hh, m_pBundleRecords, p, pTemp)
{
// 删除不再内存中的所有记录
if( p->wpBundle.expired() )
{
HASH_DEL(m_pBundleRecords, p);
// 释放bundle资源
delete p;
bMaintainedData = true;
}// 已经不再内存中直接删除
else
{
std::tr1::shared_ptr<Bundle> spBundle = p->wpBundle.lock();
if( !spBundle->isCached() )
{
HASH_DEL(m_pBundleRecords, p);
// 释放bundle资源
delete p;
bMaintainedData = true;
}
}// 清楚不在缓存记录中的
}
}
else
{
clock_t tBegin = clock();
clock_t tMsBase = CLOCKS_PER_SEC / 1000;
// 记录要删除的记录
HASH_ITER(hh, m_pBundleRecords, p, pTemp)
{
// 删除不再内存中的所有记录
if( p->wpBundle.expired() )
{
HASH_DEL(m_pBundleRecords, p);
// 释放bundle资源
delete p;
bMaintainedData = true;
}// 已经不再内存中直接删除
else
{
std::tr1::shared_ptr<Bundle> spBundle = p->wpBundle.lock();
if( !spBundle->isCached() )
{
HASH_DEL(m_pBundleRecords, p);
// 释放bundle资源
delete p;
bMaintainedData = true;
}
}// 清楚不在缓存记录中的
if ( (clock() - tBegin)/tMsBase > m_nSpan )
break;
}
}
if(bMaintainedData)
{
std::cout << "图层["<< sLayerName << "]维护完成" << std::endl;
}
}
std::string thp::WMTSLayer::getName() const
{
std::string sPath( m_szPath );
if(sPath.empty())
return "";
size_t nSize = sPath.size();
size_t nPos0 = sPath.rfind('\\');
if(std::string::npos == nPos0)
nPos0 = sPath.rfind('/');
std::string sName = sPath.substr(nPos0+1, (nSize - nPos0));
return sName;
}