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
|
#include "file_reader.hpp"
#include "reader_cache.hpp"
#include "internal/file_data.hpp"
#include "../../base/string_utils.hpp"
#ifndef LOG_FILE_READER_STATS
#define LOG_FILE_READER_STATS 0
#endif // LOG_FILE_READER_STATS
#if LOG_FILE_READER_STATS && !defined(LOG_FILE_READER_EVERY_N_READS_MASK)
#define LOG_FILE_READER_EVERY_N_READS_MASK 0xFFFFFFFF
#endif
namespace
{
class FileDataWithCachedSize : public FileData
{
public:
explicit FileDataWithCachedSize(string const & fileName)
: FileData(fileName, FileData::OP_READ), m_Size(FileData::Size()) {}
uint64_t Size() const { return m_Size; }
private:
uint64_t m_Size;
};
}
class FileReader::FileReaderData
{
public:
FileReaderData(string const & fileName, uint32_t logPageSize, uint32_t logPageCount)
: m_FileData(fileName), m_ReaderCache(logPageSize, logPageCount)
{
#if LOG_FILE_READER_STATS
m_ReadCallCount = 0;
#endif
}
~FileReaderData()
{
#if LOG_FILE_READER_STATS
LOG(LINFO, ("FileReader", GetName(), m_ReaderCache.GetStatsStr()));
#endif
}
uint64_t Size() const { return m_FileData.Size(); }
void Read(uint64_t pos, void * p, size_t size)
{
#if LOG_FILE_READER_STATS
if (((++m_ReadCallCount) & LOG_FILE_READER_EVERY_N_READS_MASK) == 0)
{
LOG(LINFO, ("FileReader", GetName(), m_ReaderCache.GetStatsStr()));
}
#endif
return m_ReaderCache.Read(m_FileData, pos, p, size);
}
string GetName() const { return m_FileData.GetName(); }
private:
FileDataWithCachedSize m_FileData;
ReaderCache<FileDataWithCachedSize, LOG_FILE_READER_STATS> m_ReaderCache;
#if LOG_FILE_READER_STATS
uint32_t m_ReadCallCount;
#endif
};
FileReader::FileReader(string const & fileName, uint32_t logPageSize, uint32_t logPageCount)
: m_pFileData(new FileReaderData(fileName, logPageSize, logPageCount)),
m_Offset(0), m_Size(m_pFileData->Size())
{
}
FileReader::FileReader(shared_ptr<FileReaderData> const & pFileData, uint64_t offset, uint64_t size)
: m_pFileData(pFileData), m_Offset(offset), m_Size(size)
{
}
uint64_t FileReader::Size() const
{
return m_Size;
}
void FileReader::Read(uint64_t pos, void * p, size_t size) const
{
ASSERT_LESS_OR_EQUAL(pos + size, Size(), (pos, size));
m_pFileData->Read(m_Offset + pos, p, size);
}
FileReader FileReader::SubReader(uint64_t pos, uint64_t size) const
{
ASSERT_LESS_OR_EQUAL(pos + size, Size(), (pos, size));
return FileReader(m_pFileData, m_Offset + pos, size);
}
FileReader * FileReader::CreateSubReader(uint64_t pos, uint64_t size) const
{
ASSERT_LESS_OR_EQUAL(pos + size, Size(), (pos, size));
return new FileReader(m_pFileData, m_Offset + pos, size);
}
bool FileReader::IsEqual(string const & fName) const
{
#if defined(OMIM_OS_WINDOWS)
return utils::equal_no_case(fName, m_pFileData->GetName());
#else
return (fName == m_pFileData->GetName());
#endif
}
string FileReader::GetName() const
{
return m_pFileData->GetName();
}
string FileReader::ReadAsText() const
{
size_t size = static_cast<size_t>(Size());
vector<char> buffer(size);
buffer.resize(size);
Read(0, &buffer[0], size);
return string(reinterpret_cast<char const *>(&buffer[0]), size);
}
|