libosmscout  1.1.1
DataTileCache.h
Go to the documentation of this file.
1 #ifndef OSMSCOUT_TILEDDATACACHE_H
2 #define OSMSCOUT_TILEDDATACACHE_H
3 
4 /*
5  This source is part of the libosmscout library
6  Copyright (C) 2015 Tim Teulings
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22 
23 #include <functional>
24 #include <map>
25 #include <memory>
26 #include <mutex>
27 #include <vector>
28 
30 
31 #include <osmscout/Node.h>
32 #include <osmscout/Way.h>
33 #include <osmscout/Area.h>
34 #include <osmscout/Route.h>
35 
36 #include <osmscout/TypeInfoSet.h>
37 
38 #include <osmscout/util/GeoBox.h>
40 #include <osmscout/util/TileId.h>
41 
42 #include <osmscout/system/Assert.h>
43 
44 namespace osmscout {
45 
51  template<typename O>
53  {
54  private:
55  mutable std::mutex mutex;
56 
57  TypeInfoSet types;
58 
59  std::vector<O> prefillData;
60  std::vector<O> data;
61 
62  bool complete=false;
63 
64  public:
68  TileData() = default;
69 
70  bool IsEmpty() const
71  {
72  std::scoped_lock<std::mutex> guard(mutex);
73 
74  return types.Empty();
75  }
76 
80  void Invalidate()
81  {
82  std::scoped_lock<std::mutex> guard(mutex);
83 
84  complete=false;
85  }
86 
91  void AddPrefillData(const TypeInfoSet& types,
92  const std::vector<O>& data)
93  {
94  std::scoped_lock<std::mutex> guard(mutex);
95 
96  if (this->types.Empty()) {
97  this->types=types;
98  }
99  else {
100  this->types.Add(types);
101  }
102 
103  if (this->prefillData.empty()) {
104  this->prefillData=data;
105  }
106  else {
107  this->prefillData.reserve(this->prefillData.size()+data.size());
108  this->prefillData.insert(this->prefillData.end(),data.begin(),data.end());
109  }
110 
111  complete=false;
112  }
113 
118  void AddPrefillData(const TypeInfoSet& types,
119  std::vector<O>&& data)
120  {
121  std::scoped_lock<std::mutex> guard(mutex);
122 
123  if (this->types.Empty()) {
124  this->types=types;
125  }
126  else {
127  this->types.Add(types);
128  }
129 
130  if (this->prefillData.empty()) {
131  this->prefillData=std::move(data);
132  }
133  else {
134  this->prefillData.reserve(this->prefillData.size()+data.size());
135  this->prefillData.insert(this->prefillData.end(),data.begin(),data.end());
136  }
137 
138  complete=false;
139  }
140 
144  void AddData(const TypeInfoSet& types,
145  const std::vector<O>& data)
146  {
147  std::scoped_lock<std::mutex> guard(mutex);
148 
149  this->data.insert(this->data.end(), data.begin(), data.end());
150  this->types.Add(types);
151 
152  complete=true;
153  }
154 
158  void SetData(const TypeInfoSet& types,
159  const std::vector<O>& data)
160  {
161  std::scoped_lock<std::mutex> guard(mutex);
162 
163  this->data=data;
164  this->types=types;
165 
166  complete=true;
167  }
168 
172  void SetData(const TypeInfoSet& types,
173  std::vector<O>&& data)
174  {
175  std::scoped_lock<std::mutex> guard(mutex);
176 
177  this->data=std::move(data);
178  this->types=types;
179 
180  complete=true;
181  }
182 
187  void SetComplete()
188  {
189  std::scoped_lock<std::mutex> guard(mutex);
190 
191  complete=true;
192  }
193 
197  bool IsComplete() const
198  {
199  std::scoped_lock<std::mutex> guard(mutex);
200 
201  return complete;
202  }
203 
210  TypeInfoSet GetTypes() const
211  {
212  std::scoped_lock<std::mutex> guard(mutex);
213 
214  return types;
215  }
216 
217  size_t GetDataSize() const
218  {
219  std::scoped_lock<std::mutex> guard(mutex);
220 
221  return prefillData.size()+data.size();
222  }
223 
224  void CopyData(std::function<void(const O&)> function) const
225  {
226  std::scoped_lock<std::mutex> guard(mutex);
227 
228  std::for_each(prefillData.begin(),prefillData.end(),function);
229  std::for_each(data.begin(),data.end(),function);
230  }
231  };
232 
239 
246 
253 
260 
261  // Forward declaration of DataTileCache for friend declaration in Tile
262  class DataTileCache;
263 
272  {
273  private:
274  TileKey key;
275  GeoBox boundingBox;
276  TileNodeData nodeData;
277  TileWayData wayData;
278  TileAreaData areaData;
279  TileRouteData routeData;
280  TileWayData optimizedWayData;
281  TileAreaData optimizedAreaData;
282 
283  private:
284  explicit Tile(const TileKey& key);
285 
286  public:
287  friend class DataTileCache;
288 
289  ~Tile() = default;
290 
294  TileKey GetKey() const
295  {
296  return key;
297  }
298 
302  uint32_t GetLevel() const
303  {
304  return key.GetLevel();
305  }
306 
310  GeoBox GetBoundingBox() const
311  {
312  return boundingBox;
313  }
314 
318  const TileNodeData& GetNodeData() const
319  {
320  return nodeData;
321  }
322 
326  const TileWayData& GetWayData() const
327  {
328  return wayData;
329  }
330 
334  const TileAreaData& GetAreaData() const
335  {
336  return areaData;
337  }
338 
343  {
344  return routeData;
345  }
346 
351  {
352  return optimizedWayData;
353  }
354 
359  {
360  return optimizedAreaData;
361  }
362 
367  {
368  return nodeData;
369  }
370 
375  {
376  return wayData;
377  }
378 
383  {
384  return areaData;
385  }
386 
391  {
392  return routeData;
393  }
394 
399  {
400  return optimizedWayData;
401  }
402 
407  {
408  return optimizedAreaData;
409  }
410 
414  bool IsComplete() const
415  {
416  return nodeData.IsComplete() &&
417  wayData.IsComplete() &&
418  areaData.IsComplete() &&
419  routeData.IsComplete() &&
420  optimizedWayData.IsComplete() &&
421  optimizedAreaData.IsComplete();
422  }
423 
427  bool IsEmpty() const
428  {
429  return nodeData.IsEmpty() &&
430  wayData.IsEmpty() &&
431  areaData.IsEmpty() &&
432  routeData.IsEmpty() &&
433  optimizedWayData.IsEmpty() &&
434  optimizedAreaData.IsEmpty();
435  }
436  };
437 
443  using TileRef = std::shared_ptr<Tile>;
444 
457  {
458  private:
462  struct OSMSCOUT_MAP_API CacheEntry
463  {
464  TileKey key;
465  //TileWeakRef tile;
466  TileRef tile;
467 
468  CacheEntry(const TileKey& key,
469  const TileRef /*TileWeakRef*/& tile)
470  : key(key),
471  tile(tile)
472  {
473  // no code
474  }
475  };
476 
478  using Cache = std::list<CacheEntry>;
479 
481  using CacheRef = Cache::iterator;
482 
484  using CacheIndex = std::map<TileKey, CacheRef>;
485 
486  private:
487  size_t cacheSize;
488 
489  mutable CacheIndex tileIndex;
490  mutable Cache tileCache;
491 
492  void ResolveNodesFromParent(Tile& tile,
493  const Tile& parentTile,
494  const GeoBox& boundingBox,
495  const TypeInfoSet& nodeTypes);
496  void ResolveWaysFromParent(Tile& tile,
497  const Tile& parentTile,
498  const GeoBox& boundingBox,
499  const TypeInfoSet& wayTypes);
500  void ResolveAreasFromParent(Tile& tile,
501  const Tile& parentTile,
502  const GeoBox& boundingBox,
503  const TypeInfoSet& areaTypes);
504  void ResolveRoutesFromParent(Tile& route,
505  const Tile& parentTile,
506  const GeoBox& boundingBox,
507  const TypeInfoSet& routeTypes);
508 
509  public:
510  explicit DataTileCache(size_t cacheSize);
511 
512  void SetSize(size_t cacheSize);
513 
514  size_t GetSize() const
515  {
516  return cacheSize;
517  }
518 
519  size_t GetCurrentSize() const
520  {
521  return tileCache.size();
522  }
523 
524  void CleanupCache();
525 
526  void InvalidateCache();
527 
528  TileRef GetCachedTile(const TileKey& id) const;
529  TileRef GetTile(const TileKey& id) const;
530 
531  void GetTilesForBoundingBox(const Magnification& magnification,
532  const GeoBox& boundingBox,
533  std::list<TileRef>& tiles) const;
534 
535  void PrefillDataFromCache(Tile& tile,
536  const TypeInfoSet& nodeTypes,
537  const TypeInfoSet& wayTypes,
538  const TypeInfoSet& areaTypes,
539  const TypeInfoSet& routeTypes,
540  const TypeInfoSet& optimizedWayTypes,
541  const TypeInfoSet& optimizedAreaTypes);
542  };
543 
549  using TiledDataCacheRef = std::shared_ptr<DataTileCache>;
550 
554 }
555 
556 #endif
const TileWayData & GetWayData() const
Definition: DataTileCache.h:326
void SetComplete()
Definition: DataTileCache.h:187
void Invalidate()
Definition: DataTileCache.h:80
TileAreaData & GetAreaData()
Definition: DataTileCache.h:382
std::shared_ptr< Tile > TileRef
Definition: DataTileCache.h:443
const TileAreaData & GetAreaData() const
Definition: DataTileCache.h:334
TileKey GetKey() const
Definition: DataTileCache.h:294
TileRouteData & GetRouteData()
Definition: DataTileCache.h:390
size_t GetCurrentSize() const
Definition: DataTileCache.h:519
bool IsEmpty() const
Definition: DataTileCache.h:427
TileWayData & GetOptimizedWayData()
Definition: DataTileCache.h:398
uint32_t GetLevel() const
Definition: DataTileCache.h:302
TileAreaData & GetOptimizedAreaData()
Definition: DataTileCache.h:406
void CopyData(std::function< void(const O &)> function) const
Definition: DataTileCache.h:224
GeoBox GetBoundingBox() const
Definition: DataTileCache.h:310
Definition: Area.h:38
void AddPrefillData(const TypeInfoSet &types, std::vector< O > &&data)
Definition: DataTileCache.h:118
bool IsEmpty() const
Definition: DataTileCache.h:70
TileWayData & GetWayData()
Definition: DataTileCache.h:374
TileNodeData & GetNodeData()
Definition: DataTileCache.h:366
#define OSMSCOUT_MAP_API
Definition: MapImportExport.h:45
Definition: DataTileCache.h:456
Definition: DataTileCache.h:52
size_t GetDataSize() const
Definition: DataTileCache.h:217
size_t GetSize() const
Definition: DataTileCache.h:514
void AddData(const TypeInfoSet &types, const std::vector< O > &data)
Definition: DataTileCache.h:144
void SetData(const TypeInfoSet &types, const std::vector< O > &data)
Definition: DataTileCache.h:158
const TileRouteData & GetRouteData() const
Definition: DataTileCache.h:342
TypeInfoSet GetTypes() const
Definition: DataTileCache.h:210
bool IsComplete() const
Definition: DataTileCache.h:197
Definition: DataTileCache.h:271
const TileAreaData & GetOptimizedAreaData() const
Definition: DataTileCache.h:358
const TileNodeData & GetNodeData() const
Definition: DataTileCache.h:318
bool IsComplete() const
Definition: DataTileCache.h:414
Definition: TileId.h:129
const TileWayData & GetOptimizedWayData() const
Definition: DataTileCache.h:350
void AddPrefillData(const TypeInfoSet &types, const std::vector< O > &data)
Definition: DataTileCache.h:91
std::shared_ptr< DataTileCache > TiledDataCacheRef
Definition: DataTileCache.h:549
void SetData(const TypeInfoSet &types, std::vector< O > &&data)
Definition: DataTileCache.h:172
uint32_t GetLevel() const
Definition: TileId.h:139