1 #ifndef OSMSCOUT_DATAFILE_H 2 #define OSMSCOUT_DATAFILE_H 26 #include <unordered_map> 84 std::string datafilename;
88 mutable FileScanner scanner;
90 mutable std::mutex accessMutex;
96 bool ReadData(N& data)
const;
101 DataFile(
const std::string& datafile,
113 const std::string& path,
114 bool memoryMappedData);
115 virtual bool IsOpen()
const;
116 virtual bool Close();
129 std::vector<ValueType>& data)
const;
131 template<
typename IteratorIn>
132 bool GetByOffset(IteratorIn begin, IteratorIn end,
size_t size,
133 std::vector<ValueType>& data)
const;
135 template<
typename IteratorIn>
136 bool GetByOffset(IteratorIn begin, IteratorIn end,
size_t size,
137 const GeoBox& boundingBox,
138 std::vector<ValueType>& data)
const;
140 template<
typename IteratorIn>
141 bool GetByOffset(IteratorIn begin, IteratorIn end,
size_t size,
142 std::unordered_map<FileOffset,ValueType>& dataMap)
const;
144 template<
typename IteratorIn>
146 std::vector<ValueType>& data)
const;
151 : datafile(datafile),cache(cacheSize)
174 scanner.SetPos(offset);
176 data.Read(*typeConfig,
179 catch (
const IOException& e) {
193 bool DataFile<N>::ReadData(N& data)
const 196 data.Read(*typeConfig,
199 catch (
const IOException& e) {
214 const std::string& path,
215 bool memoryMappedData)
217 this->typeConfig=typeConfig;
222 scanner.Open(datafilename,
223 FileScanner::LowMemRandom,
228 scanner.CloseFailsafe();
258 if (scanner.IsOpen()) {
264 scanner.CloseFailsafe();
274 std::scoped_lock<std::mutex> lock(accessMutex);
302 template <
typename IteratorIn>
305 std::vector<ValueType>& data)
const 311 data.reserve(data.size()+size);
312 std::scoped_lock<std::mutex> lock(accessMutex);
314 if (cache.GetMaxSize()>0 &&
315 size>cache.GetMaxSize()){
316 log.
Warn() <<
"Cache size (" << cache.GetMaxSize() <<
") for file " << datafile <<
" is smaller than current request (" << size <<
")";
319 for (IteratorIn offsetIter=begin; offsetIter!=end; ++offsetIter) {
322 if (cache.GetEntry(*offsetIter,entryRef)) {
323 data.push_back(entryRef->value);
328 if (!ReadData(*offsetIter,
330 log.
Error() <<
"Error while reading data from offset " << *offsetIter <<
" of file " << datafilename <<
"!";
335 data.push_back(value);
348 template<
typename IteratorIn>
351 const GeoBox& boundingBox,
352 std::vector<ValueType>& data)
const 358 data.reserve(data.size()+size);
359 std::scoped_lock<std::mutex> lock(accessMutex);
361 if (cache.GetMaxSize()>0 &&
362 size>cache.GetMaxSize()){
363 log.
Warn() <<
"Cache size (" << cache.GetMaxSize() <<
") for file " << datafile <<
" is smaller than current request (" << size <<
")";
369 for (IteratorIn offsetIter=begin; offsetIter!=end; ++offsetIter) {
373 if (cache.GetEntry(*offsetIter,entryRef)){
374 value=entryRef->value;
376 if (!ReadData(*offsetIter,
378 log.
Error() <<
"Error while reading data from offset " << *offsetIter <<
" of file " << datafilename <<
"!";
385 if (!value->Intersects(boundingBox)) {
395 data.push_back(value);
398 size_t hitRate=inBoxCount*100/size;
399 if (size>100 && hitRate<50) {
400 log.
Warn() <<
"Bounding box hit rate for file " << datafile <<
" is only " << hitRate <<
"% (" << inBoxCount <<
"/" << size <<
")";
419 template<
typename IteratorIn>
422 std::unordered_map<FileOffset,ValueType>& dataMap)
const 428 std::vector<ValueType> data;
430 if (!GetByOffset(begin,
437 for (
const auto& entry : data) {
438 dataMap.insert(std::make_pair(entry->GetFileOffset(),entry));
453 std::scoped_lock<std::mutex> lock(accessMutex);
456 if (cache.GetEntry(offset,entryRef)){
457 entry=entryRef->value;
461 if (!ReadData(offset,
463 log.
Error() <<
"Error while reading data from offset " << offset <<
" of file " << datafilename <<
"!";
482 std::vector<ValueType>& data)
const 488 std::scoped_lock<std::mutex> lock(accessMutex);
491 bool offsetSetup=
false;
494 data.reserve(data.size()+span.
count);
496 for (uint32_t i=1; i<=span.
count; i++) {
498 if (cache.GetEntry(offset,entryRef)){
499 data.push_back(entryRef->value);
500 offset=entryRef->value->GetNextFileOffset();
504 scanner.SetPos(offset);
509 if (!ReadData(*value)) {
510 log.
Error() <<
"Error while reading data #" << i <<
" starting from offset " << span.
startOffset <<
" of file " << datafilename <<
"!";
515 offset=value->GetNextFileOffset();
517 data.push_back(value);
535 template<
typename IteratorIn>
537 std::vector<ValueType>& data)
const 539 uint32_t overallCount=0;
541 for (IteratorIn spanIter=begin; spanIter!=end; ++spanIter) {
542 overallCount+=spanIter->count;
545 data.reserve(data.size()+overallCount);
548 std::scoped_lock<std::mutex> lock(accessMutex);
549 for (IteratorIn spanIter=begin; spanIter!=end; ++spanIter) {
550 if (spanIter->count==0) {
554 bool offsetSetup=
false;
557 for (uint32_t i=1; i<=spanIter->count; i++) {
559 if (cache.GetEntry(offset,entryRef)){
560 data.push_back(entryRef->value);
561 offset=entryRef->value->GetNextFileOffset();
565 scanner.SetPos(offset);
570 if (!ReadData(*value)) {
571 log.
Error() <<
"Error while reading data #" << i <<
" starting from offset " << spanIter->startOffset <<
572 " of file " << datafilename <<
"!";
577 offset=value->GetNextFileOffset();
579 data.push_back(value);
599 template <
class I,
class N>
613 const std::string& indexfile,
614 size_t indexCacheSize,
615 size_t dataCacheSize);
618 const std::string& path,
619 bool memoryMappedIndex,
620 bool memoryMappedData);
621 bool Close()
override;
623 bool IsOpen()
const override;
631 template<
typename IteratorIn>
632 bool GetOffsets(IteratorIn begin, IteratorIn end,
size_t size,
633 std::vector<FileOffset>& offsets)
const;
635 bool Get(
const std::vector<I>& ids,
636 std::vector<ValueType>& data)
const;
637 bool Get(
const std::list<I>& ids,
638 std::vector<ValueType>& data)
const;
639 bool Get(
const std::set<I>& ids,
640 std::vector<ValueType>& data)
const;
642 bool Get(
const std::set<I>& ids,
643 std::unordered_map<I,ValueType>& data)
const;
647 template <
class I,
class N>
649 const std::string& indexfile,
650 size_t indexCacheSize,
651 size_t dataCacheSize)
652 :
DataFile<N>(datafile,dataCacheSize),
653 index(indexfile,indexCacheSize)
658 template <
class I,
class N>
660 const std::string& path,
661 bool memoryMappedIndex,
662 bool memoryMappedData)
670 return index.
Open(path,
674 template <
class I,
class N>
683 if (!index.Close()) {
690 template <
class I,
class N>
697 template <
class I,
class N>
698 template<
typename IteratorIn>
700 std::vector<FileOffset>& offsets)
const 708 template <
class I,
class N>
715 template <
class I,
class N>
717 std::vector<ValueType>& data)
const 719 std::vector<FileOffset> offsets;
721 if (!index.GetOffsets(ids.begin(),
734 template <
class I,
class N>
736 std::vector<ValueType>& data)
const 738 std::vector<FileOffset> offsets;
740 if (!index.GetOffsets(ids.begin(),
753 template <
class I,
class N>
755 std::vector<ValueType>& data)
const 757 std::vector<FileOffset> offsets;
759 if (!index.GetOffsets(ids.begin(),
772 template <
class I,
class N>
774 std::unordered_map<I,ValueType>& data)
const 776 std::vector<FileOffset> offsets;
777 std::vector<ValueType> d;
779 if (!index.GetOffsets(ids.begin(),
793 for (
const auto& value : d) {
794 data[value->GetId()]=value;
800 template <
class I,
class N>
806 if (!index.GetOffset(
id,offset)) {
uint32_t count
Number of entries to read.
Definition: DataFile.h:47
DataFile & operator=(const DataFile &)=delete
virtual bool Close()
Definition: DataFile.h:252
void FlushCache()
Definition: DataFile.h:272
Definition: Exception.h:72
Definition: DataFile.h:600
Log & Error(bool state)
Definition: Logger.h:469
std::shared_ptr< PTRoute > ValueType
Definition: DataFile.h:76
FileOffset startOffset
Offset for the first data entry referenced in the file. Data will be read starting from this position...
Definition: DataFile.h:46
bool Open(const TypeConfigRef &typeConfig, const std::string &path, bool memoryMappedData)
Definition: DataFile.h:213
OSMSCOUT_API std::string AppendFileToDir(const std::string &dir, const std::string &file)
bool GetOffset(I id, FileOffset &offset) const
Definition: DataFile.h:709
bool Open(const TypeConfigRef &typeConfig, const std::string &path, bool memoryMappedIndex, bool memoryMappedData)
Definition: DataFile.h:659
bool operator!=(const DataBlockSpan &other) const
Definition: DataFile.h:59
TypeConfigRef typeConfig
Definition: DataFile.h:93
typename Cache< FileOffset, ValueType >::CacheEntry ValueCacheEntry
Definition: DataFile.h:79
typename Cache< FileOffset, ValueType >::CacheRef ValueCacheRef
Definition: DataFile.h:80
bool GetByBlockSpans(IteratorIn begin, IteratorIn end, std::vector< ValueType > &data) const
Definition: DataFile.h:536
std::string GetDescription() const override
Definition: DataFile.h:44
Definition: DataFile.h:73
uint64_t FileOffset
Definition: OSMScoutTypes.h:47
Log & Warn(bool state)
Definition: Logger.h:462
DataFile(const std::string &datafile, size_t cacheSize)
Definition: DataFile.h:150
typename OrderList::iterator CacheRef
Definition: Cache.h:98
bool operator<(const DataBlockSpan &other) const
Definition: DataFile.h:49
std::string GetFilename() const
Definition: DataFile.h:120
bool GetByOffset(FileOffset offset, ValueType &entry) const
Definition: DataFile.h:450
virtual ~DataFile()
Definition: DataFile.h:157
std::shared_ptr< TypeConfig > TypeConfigRef
Definition: TypeConfig.h:1227
virtual bool IsOpen() const
Definition: DataFile.h:241
bool GetOffsets(IteratorIn begin, IteratorIn end, size_t size, std::vector< FileOffset > &offsets) const
Definition: DataFile.h:699
bool operator==(const DataBlockSpan &other) const
Definition: DataFile.h:54
bool GetByBlockSpan(const DataBlockSpan &span, std::vector< ValueType > &data) const
Definition: DataFile.h:481