1#ifndef OSMSCOUT_DATAFILE_H
2#define OSMSCOUT_DATAFILE_H
26#include <unordered_map>
85 std::string datafilename;
89 mutable FileScanner scanner;
91 mutable std::mutex accessMutex;
97 bool ReadData(N& data)
const;
114 const std::string& path,
115 bool memoryMappedData);
130 std::vector<ValueType>& data)
const;
132 template<
typename IteratorIn>
133 bool GetByOffset(IteratorIn begin, IteratorIn end,
size_t size,
134 std::vector<ValueType>& data)
const;
136 template<
typename IteratorIn>
137 bool GetByOffset(IteratorIn begin, IteratorIn end,
size_t size,
138 const GeoBox& boundingBox,
139 std::vector<ValueType>& data)
const;
141 template<
typename IteratorIn>
142 bool GetByOffset(IteratorIn begin, IteratorIn end,
size_t size,
143 std::unordered_map<FileOffset,ValueType>& dataMap)
const;
145 template<
typename IteratorIn>
147 std::vector<ValueType>& data)
const;
152 : datafile(datafile),cache(cacheSize)
175 scanner.SetPos(offset);
177 data.Read(*typeConfig,
180 catch (
const IOException& e) {
194 bool DataFile<N>::ReadData(N& data)
const
197 data.Read(*typeConfig,
201 log.Error() << e.GetDescription();
215 const std::string& path,
216 bool memoryMappedData)
223 scanner.Open(datafilename,
224 FileScanner::LowMemRandom,
229 scanner.CloseFailsafe();
244 return scanner.IsOpen();
259 if (scanner.IsOpen()) {
265 scanner.CloseFailsafe();
275 std::scoped_lock<std::mutex> lock(accessMutex);
303 template <
typename IteratorIn>
306 std::vector<ValueType>& data)
const
312 data.reserve(data.size()+size);
313 std::scoped_lock<std::mutex> lock(accessMutex);
315 if (cache.GetMaxSize()>0 &&
316 size>cache.GetMaxSize()){
317 log.Warn() <<
"Cache size (" << cache.GetMaxSize() <<
") for file " << datafile <<
" is smaller than current request (" << size <<
")";
320 for (IteratorIn offsetIter=begin; offsetIter!=end; ++offsetIter) {
323 if (cache.GetEntry(*offsetIter,entryRef)) {
324 data.push_back(entryRef->value);
329 if (!ReadData(*offsetIter,
331 log.Error() <<
"Error while reading data from offset " << *offsetIter <<
" of file " << datafilename <<
"!";
336 data.push_back(value);
349 template<
typename IteratorIn>
352 const GeoBox& boundingBox,
353 std::vector<ValueType>& data)
const
359 data.reserve(data.size()+size);
360 std::scoped_lock<std::mutex> lock(accessMutex);
362 if (cache.GetMaxSize()>0 &&
363 size>cache.GetMaxSize()){
364 log.Warn() <<
"Cache size (" << cache.GetMaxSize() <<
") for file " << datafile <<
" is smaller than current request (" << size <<
")";
370 for (IteratorIn offsetIter=begin; offsetIter!=end; ++offsetIter) {
374 if (cache.GetEntry(*offsetIter,entryRef)){
375 value=entryRef->value;
377 if (!ReadData(*offsetIter,
379 log.Error() <<
"Error while reading data from offset " << *offsetIter <<
" of file " << datafilename <<
"!";
386 if (!value->Intersects(boundingBox)) {
396 data.push_back(value);
399 size_t hitRate=inBoxCount*100/size;
400 if (size>100 && hitRate<50) {
401 log.Warn() <<
"Bounding box hit rate for file " << datafile <<
" is only " << hitRate <<
"% (" << inBoxCount <<
"/" << size <<
")";
420 template<
typename IteratorIn>
423 std::unordered_map<FileOffset,ValueType>& dataMap)
const
429 std::vector<ValueType> data;
438 for (
const auto& entry : data) {
439 dataMap.emplace(entry->GetFileOffset(),entry);
454 std::scoped_lock<std::mutex> lock(accessMutex);
457 if (cache.GetEntry(offset,entryRef)){
458 entry=entryRef->value;
462 if (!ReadData(offset,
464 log.Error() <<
"Error while reading data from offset " << offset <<
" of file " << datafilename <<
"!";
483 std::vector<ValueType>& data)
const
489 std::scoped_lock<std::mutex> lock(accessMutex);
492 bool offsetSetup=
false;
495 data.reserve(data.size()+span.
count);
497 for (uint32_t i=1; i<=span.
count; i++) {
499 if (cache.GetEntry(offset,entryRef)){
500 data.push_back(entryRef->value);
501 offset=entryRef->value->GetNextFileOffset();
505 scanner.SetPos(offset);
510 if (!ReadData(*value)) {
511 log.Error() <<
"Error while reading data #" << i <<
" starting from offset " << span.
startOffset <<
" of file " << datafilename <<
"!";
516 offset=value->GetNextFileOffset();
518 data.push_back(value);
536 template<
typename IteratorIn>
538 std::vector<ValueType>& data)
const
540 uint32_t overallCount=0;
542 for (IteratorIn spanIter=begin; spanIter!=end; ++spanIter) {
543 overallCount+=spanIter->count;
546 data.reserve(data.size()+overallCount);
549 std::scoped_lock<std::mutex> lock(accessMutex);
550 for (IteratorIn spanIter=begin; spanIter!=end; ++spanIter) {
551 if (spanIter->count==0) {
555 bool offsetSetup=
false;
558 for (uint32_t i=1; i<=spanIter->count; i++) {
560 if (cache.GetEntry(offset,entryRef)){
561 data.push_back(entryRef->value);
562 offset=entryRef->value->GetNextFileOffset();
566 scanner.SetPos(offset);
571 if (!ReadData(*value)) {
572 log.Error() <<
"Error while reading data #" << i <<
" starting from offset " << spanIter->startOffset <<
573 " of file " << datafilename <<
"!";
578 offset=value->GetNextFileOffset();
580 data.push_back(value);
600 template <
class I,
class N>
614 const std::string& indexfile,
615 size_t indexCacheSize,
616 size_t dataCacheSize);
619 const std::string& path,
620 bool memoryMappedIndex,
621 bool memoryMappedData);
632 template<
typename IteratorIn>
633 bool GetOffsets(IteratorIn begin, IteratorIn end,
size_t size,
634 std::vector<FileOffset>& offsets)
const;
636 bool Get(
const std::vector<I>& ids,
637 std::vector<ValueType>& data)
const;
638 bool Get(
const std::list<I>& ids,
639 std::vector<ValueType>& data)
const;
640 bool Get(
const std::set<I>& ids,
641 std::vector<ValueType>& data)
const;
643 bool Get(
const std::set<I>& ids,
644 std::unordered_map<I,ValueType>& data)
const;
648 template <
class I,
class N>
650 const std::string& indexfile,
651 size_t indexCacheSize,
652 size_t dataCacheSize)
653 :
DataFile<N>(datafile,dataCacheSize),
654 index(indexfile,indexCacheSize)
659 template <
class I,
class N>
661 const std::string& path,
662 bool memoryMappedIndex,
663 bool memoryMappedData)
671 return index.Open(path,
675 template <
class I,
class N>
684 if (!index.Close()) {
691 template <
class I,
class N>
698 template <
class I,
class N>
699 template<
typename IteratorIn>
701 std::vector<FileOffset>& offsets)
const
703 return index.GetOffsets(begin,
709 template <
class I,
class N>
713 return index.GetOffset(
id,offset);
716 template <
class I,
class N>
718 std::vector<ValueType>& data)
const
720 std::vector<FileOffset> offsets;
722 if (!index.GetOffsets(ids.begin(),
735 template <
class I,
class N>
737 std::vector<ValueType>& data)
const
739 std::vector<FileOffset> offsets;
741 if (!index.GetOffsets(ids.begin(),
754 template <
class I,
class N>
756 std::vector<ValueType>& data)
const
758 std::vector<FileOffset> offsets;
760 if (!index.GetOffsets(ids.begin(),
773 template <
class I,
class N>
775 std::unordered_map<I,ValueType>& data)
const
777 std::vector<FileOffset> offsets;
778 std::vector<ValueType> d;
780 if (!index.GetOffsets(ids.begin(),
794 for (
const auto& value : d) {
795 data[value->GetId()]=value;
801 template <
class I,
class N>
807 if (!index.GetOffset(
id,offset)) {
typename OrderList::iterator CacheRef
Definition Cache.h:98
DataFile(DataFile &&)=delete
std::shared_ptr< N > ValueType
Definition DataFile.h:77
bool GetByBlockSpans(IteratorIn begin, IteratorIn end, std::vector< ValueType > &data) const
Definition DataFile.h:537
bool GetByOffset(FileOffset offset, ValueType &entry) const
Definition DataFile.h:451
DataFile(const DataFile &)=delete
void FlushCache()
Definition DataFile.h:273
bool Open(const TypeConfigRef &typeConfig, const std::string &path, bool memoryMappedData)
Definition DataFile.h:214
DataFile(const std::string &datafile, size_t cacheSize)
Definition DataFile.h:151
DataFile & operator=(DataFile &&)=delete
typename Cache< FileOffset, ValueType >::CacheRef ValueCacheRef
Definition DataFile.h:81
DataFile & operator=(const DataFile &)=delete
virtual bool Close()
Definition DataFile.h:253
typename Cache< FileOffset, ValueType >::CacheEntry ValueCacheEntry
Definition DataFile.h:80
virtual bool IsOpen() const
Definition DataFile.h:242
bool GetByBlockSpan(const DataBlockSpan &span, std::vector< ValueType > &data) const
Definition DataFile.h:482
std::string GetFilename() const
Definition DataFile.h:121
virtual ~DataFile()
Definition DataFile.h:158
TypeConfigRef typeConfig
Definition DataFile.h:94
Cache< FileOffset, std::shared_ptr< N > > ValueCache
Definition DataFile.h:78
Definition Exception.h:73
std::string GetDescription() const override
std::shared_ptr< N > ValueType
Definition DataFile.h:604
bool Open(const TypeConfigRef &typeConfig, const std::string &path, bool memoryMappedIndex, bool memoryMappedData)
Definition DataFile.h:660
bool Close() override
Definition DataFile.h:676
bool IsOpen() const override
Definition DataFile.h:692
IndexedDataFile(const std::string &datafile, const std::string &indexfile, size_t indexCacheSize, size_t dataCacheSize)
Definition DataFile.h:649
bool GetOffsets(IteratorIn begin, IteratorIn end, size_t size, std::vector< FileOffset > &offsets) const
Definition DataFile.h:700
bool GetOffset(I id, FileOffset &offset) const
Definition DataFile.h:710
bool Get(I id, ValueType &entry) const
Definition DataFile.h:802
Log & Error(bool state)
Definition Logger.h:414
Definition NumericIndex.h:43
OSMSCOUT_API std::string AppendFileToDir(const std::string &dir, const std::string &file)
OSMSCOUT_API Log log
Definition LoggerImpl.h:95
uint64_t FileOffset
Definition OSMScoutTypes.h:46
std::shared_ptr< TypeConfig > TypeConfigRef
Definition TypeConfig.h:1396
uint32_t count
Number of entries to read.
Definition DataFile.h:48
bool operator<(const DataBlockSpan &other) const
Definition DataFile.h:50
bool operator==(const DataBlockSpan &other) const
Definition DataFile.h:55
FileOffset startOffset
Offset for the first data entry referenced in the file. Data will be read starting from this position...
Definition DataFile.h:47
bool operator!=(const DataBlockSpan &other) const
Definition DataFile.h:60