libosmscout  1.1.1
LocationIndex.h
Go to the documentation of this file.
1 #ifndef OSMSCOUT_LOCATIONINDEX_H
2 #define OSMSCOUT_LOCATIONINDEX_H
3 
4 /*
5  This source is part of the libosmscout library
6  Copyright (C) 2009 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 <list>
24 #include <map>
25 #include <memory>
26 #include <set>
27 #include <unordered_set>
28 
29 #include <osmscout/Location.h>
30 #include <osmscout/TypeConfig.h>
31 
34 
35 namespace osmscout {
36 
48  {
49  public:
50  static const char* const FILENAME_LOCATION_IDX;
51 
55  class OSMSCOUT_API ScopeCacheCleaner CLASS_FINAL {
56  std::shared_ptr<LocationIndex> index;
57  public:
58  explicit ScopeCacheCleaner(std::shared_ptr<LocationIndex> index):
59  index(std::move(index))
60  {}
61 
62  ScopeCacheCleaner(const ScopeCacheCleaner&) = delete;
63  ScopeCacheCleaner(ScopeCacheCleaner&&) = delete;
64  ScopeCacheCleaner& operator=(const ScopeCacheCleaner &) = delete;
65  ScopeCacheCleaner& operator=(ScopeCacheCleaner &&) = delete;
66 
68  if (index) {
69  index->FlushCache();
70  }
71  }
72  };
73 
74  private:
75 
88  class FileScannerPool: public ObjectPool<FileScanner>
89  {
90  public:
91  std::string path;
92  bool memoryMappedData=false;
93  public:
94  FileScannerPool():
95  ObjectPool<FileScanner>(4) // 4 should be enough for recursive visitors
96  {}
97 
98  ~FileScannerPool() override {
99  Clear(); // we have Close method override...
100  }
101 
102  Ptr Borrow() override;
103 
104  FileScanner* MakeNew() noexcept override;
105 
106  void Destroy(FileScanner*) noexcept override;
107 
108  bool IsValid(FileScanner* o) noexcept override;
109  };
110 
111  using FileScannerPtr = FileScannerPool::Ptr;
112 
113  private:
114  mutable FileScannerPool fileScannerPool;
115  uint8_t bytesForNodeFileOffset;
116  uint8_t bytesForAreaFileOffset;
117  uint8_t bytesForWayFileOffset;
118  std::vector<std::string> regionIgnoreTokens;
119  std::unordered_set<std::string> regionIgnoreTokenSet;
120  std::vector<std::string> poiIgnoreTokens;
121  std::unordered_set<std::string> poiIgnoreTokenSet;
122  std::vector<std::string> locationIgnoreTokens;
123  std::unordered_set<std::string> locationIgnoreTokenSet;
124  uint32_t minRegionChars;
125  uint32_t maxRegionChars;
126  uint32_t minRegionWords;
127  uint32_t maxRegionWords;
128  uint32_t maxPOIWords;
129  uint32_t minLocationChars;
130  uint32_t maxLocationChars;
131  uint32_t minLocationWords;
132  uint32_t maxLocationWords;
133  uint32_t maxAddressWords;
134  FileOffset indexOffset;
135 
136  private:
137  void Read(FileScanner& scanner,
138  ObjectFileRef& object) const;
139 
140  bool LoadAdminRegion(FileScanner& scanner,
141  AdminRegion& region) const;
142 
143  AdminRegionVisitor::Action VisitRegionEntries(const AdminRegion& region,
144  FileScanner& scanner,
145  AdminRegionVisitor& visitor) const;
146 
147  bool VisitRegionPOIs(const AdminRegion& region,
148  FileScanner& scanner,
149  POIVisitor& visitor,
150  bool recursive,
151  bool& stopped) const;
152 
153  bool VisitPostalArea(const AdminRegion& adminRegion,
154  const PostalArea& postalArea,
155  FileScanner& scanner,
156  LocationVisitor& visitor,
157  bool recursive,
158  bool& stopped) const;
159 
160  bool VisitLocations(const AdminRegion& adminRegion,
161  FileScanner& scanner,
162  LocationVisitor& visitor,
163  bool recursive,
164  bool& stopped) const;
165 
166  bool VisitPostalAreaLocations(const AdminRegion& adminRegion,
167  const PostalArea& postalArea,
168  FileScanner& scanner,
169  LocationVisitor& visitor,
170  bool& stopped) const;
171 
172  bool VisitLocation(FileScanner& scanner,
173  const AdminRegion& region,
174  const PostalArea& postalArea,
175  const Location& location,
176  AddressVisitor& visitor,
177  bool& stopped) const;
178 
179  public:
180  LocationIndex() = default;
181  virtual ~LocationIndex() = default;
182 
183  bool Load(const std::string& path, bool memoryMappedData);
184 
185  const std::vector<std::string>& GetRegionIgnoreTokens() const
186  {
187  return regionIgnoreTokens;
188  }
189 
190  const std::vector<std::string>& GetPOIIgnoreTokens() const
191  {
192  return poiIgnoreTokens;
193  }
194 
195  const std::vector<std::string>& GetLocationIgnoreTokens() const
196  {
197  return locationIgnoreTokens;
198  }
199 
200  bool IsRegionIgnoreToken(const std::string& token) const;
201  bool IsLocationIgnoreToken(const std::string& token) const;
202 
203  inline uint32_t GetRegionMaxWords() const
204  {
205  return maxRegionWords;
206  }
207 
208  inline uint32_t GetPOIMaxWords() const
209  {
210  return maxPOIWords;
211  }
212 
213  inline uint32_t GetLocationMaxWords() const
214  {
215  return maxLocationWords;
216  }
217 
218  inline uint32_t GetAddressMaxWords() const
219  {
220  return maxAddressWords;
221  }
222 
226  bool VisitAdminRegions(AdminRegionVisitor& visitor) const;
227 
231  bool VisitAdminRegions(const AdminRegion& adminRegion,
232  AdminRegionVisitor& visitor) const;
233 
237  bool VisitPOIs(const AdminRegion& region,
238  POIVisitor& visitor,
239  bool recursive=true) const;
240 
244  bool VisitLocations(const AdminRegion& adminRegion,
245  LocationVisitor& visitor,
246  bool recursive=true) const;
247 
251  bool VisitLocations(const AdminRegion& adminRegion,
252  const PostalArea& postalArea,
253  LocationVisitor& visitor,
254  bool recursive=true) const;
255 
259  bool VisitAddresses(const AdminRegion& region,
260  const PostalArea& postalArea,
261  const Location& location,
262  AddressVisitor& visitor) const;
263 
264  bool ResolveAdminRegionHierachie(const AdminRegionRef& region,
265  std::map<FileOffset,AdminRegionRef>& refs) const;
266 
267  void DumpStatistics() const;
268 
269  void FlushCache() const;
270  };
271 
272  using LocationIndexRef = std::shared_ptr<LocationIndex>;
273 }
274 
275 #endif
static const char *const FILENAME_LOCATION_IDX
Definition: LocationIndex.h:50
Definition: Location.h:130
Definition: Location.h:146
Prints details for debugging, if debug flag (performance, data) is set in renderer parameter...
Definition: MapPainter.h:58
uint32_t GetRegionMaxWords() const
Definition: LocationIndex.h:203
STL namespace.
const std::vector< std::string > & GetPOIIgnoreTokens() const
Definition: LocationIndex.h:190
Definition: ObjectPool.h:33
~ScopeCacheCleaner()
Definition: LocationIndex.h:67
Definition: Location.h:58
ScopeCacheCleaner(std::shared_ptr< LocationIndex > index)
Definition: LocationIndex.h:58
Definition: Area.h:38
Definition: Location.h:38
uint32_t GetPOIMaxWords() const
Definition: LocationIndex.h:208
#define OSMSCOUT_API
Definition: CoreImportExport.h:45
Definition: LocationIndex.h:55
Definition: Location.h:163
uint32_t GetLocationMaxWords() const
Definition: LocationIndex.h:213
Definition: Location.h:90
uint64_t FileOffset
Definition: OSMScoutTypes.h:47
const std::vector< std::string > & GetLocationIgnoreTokens() const
Definition: LocationIndex.h:195
Definition: Location.h:198
Definition: LocationIndex.h:47
uint32_t GetAddressMaxWords() const
Definition: LocationIndex.h:218
std::shared_ptr< LocationIndex > LocationIndexRef
Definition: LocationIndex.h:272
std::shared_ptr< AdminRegion > AdminRegionRef
Definition: Location.h:83