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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
|
#include "coding/file_container.hpp"
#include "coding/read_write_utils.hpp"
#include "coding/write_to_sink.hpp"
#include "coding/internal/file_data.hpp"
#include "std/cstring.hpp"
#include "std/sstream.hpp"
#ifndef OMIM_OS_WINDOWS
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#ifdef OMIM_OS_ANDROID
#include <fcntl.h>
#else
#include <sys/fcntl.h>
#endif
#else
#include <windows.h>
#endif
#include <errno.h>
template <class TSource, class InfoT> void Read(TSource & src, InfoT & i)
{
rw::Read(src, i.m_tag);
i.m_offset = ReadVarUint<uint64_t>(src);
i.m_size = ReadVarUint<uint64_t>(src);
}
template <class TSink, class InfoT> void Write(TSink & sink, InfoT const & i)
{
rw::Write(sink, i.m_tag);
WriteVarUint(sink, i.m_offset);
WriteVarUint(sink, i.m_size);
}
string DebugPrint(FilesContainerBase::Info const & info)
{
ostringstream ss;
ss << "{ " << info.m_tag << ", " << info.m_offset << ", " << info.m_size << " }";
return ss.str();
}
/////////////////////////////////////////////////////////////////////////////
// FilesContainerBase
/////////////////////////////////////////////////////////////////////////////
template <class ReaderT>
void FilesContainerBase::ReadInfo(ReaderT & reader)
{
uint64_t offset = ReadPrimitiveFromPos<uint64_t>(reader, 0);
ReaderSource<ReaderT> src(reader);
src.Skip(offset);
rw::Read(src, m_info);
}
/////////////////////////////////////////////////////////////////////////////
// FilesContainerR
/////////////////////////////////////////////////////////////////////////////
FilesContainerR::FilesContainerR(string const & filePath,
uint32_t logPageSize,
uint32_t logPageCount)
: m_source(make_unique<FileReader>(filePath, logPageSize, logPageCount))
{
ReadInfo(m_source);
}
FilesContainerR::FilesContainerR(TReader const & file)
: m_source(file)
{
ReadInfo(m_source);
}
FilesContainerR::TReader FilesContainerR::GetReader(Tag const & tag) const
{
Info const * p = GetInfo(tag);
if (!p)
MYTHROW(Reader::OpenException, ("Can't find section:", GetFileName(), tag));
return m_source.SubReader(p->m_offset, p->m_size);
}
pair<uint64_t, uint64_t> FilesContainerR::GetAbsoluteOffsetAndSize(Tag const & tag) const
{
Info const * p = GetInfo(tag);
if (!p)
MYTHROW(Reader::OpenException, ("Can't find section:", GetFileName(), tag));
auto reader = dynamic_cast<FileReader const *>(m_source.GetPtr());
uint64_t const offset = reader ? reader->GetOffset() : 0;
return make_pair(offset + p->m_offset, p->m_size);
}
FilesContainerBase::Info const * FilesContainerBase::GetInfo(Tag const & tag) const
{
auto i = lower_bound(m_info.begin(), m_info.end(), tag, LessInfo());
if (i != m_info.end() && i->m_tag == tag)
return &(*i);
else
return 0;
}
namespace detail
{
/////////////////////////////////////////////////////////////////////////////
// MappedFile
/////////////////////////////////////////////////////////////////////////////
void MappedFile::Open(string const & fName)
{
Close();
#ifdef OMIM_OS_WINDOWS
m_hFile = CreateFileA(fName.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
if (m_hFile == INVALID_HANDLE_VALUE)
MYTHROW(Reader::OpenException, ("Can't open file:", fName, "win last error:", GetLastError()));
m_hMapping = CreateFileMappingA(m_hFile, NULL, PAGE_READONLY, 0, 0, NULL);
if (m_hMapping == NULL)
MYTHROW(Reader::OpenException, ("Can't create file's Windows mapping:", fName, "win last error:", GetLastError()));
#else
m_fd = open(fName.c_str(), O_RDONLY | O_NONBLOCK);
if (m_fd == -1)
{
if (errno == EMFILE || errno == ENFILE)
{
MYTHROW(Reader::TooManyFilesException,
("Can't open file:", fName, ", reason:", strerror(errno)));
}
else
{
MYTHROW(Reader::OpenException, ("Can't open file:", fName, ", reason:", strerror(errno)));
}
}
#endif
}
void MappedFile::Close()
{
#ifdef OMIM_OS_WINDOWS
if (m_hMapping != INVALID_HANDLE_VALUE)
{
CloseHandle(m_hMapping);
m_hMapping = INVALID_HANDLE_VALUE;
}
if (m_hFile != INVALID_HANDLE_VALUE)
{
CloseHandle(m_hFile);
m_hFile = INVALID_HANDLE_VALUE;
}
#else
if (m_fd != -1)
{
close(m_fd);
m_fd = -1;
}
#endif
}
MappedFile::Handle MappedFile::Map(uint64_t offset, uint64_t size, string const & tag) const
{
#ifdef OMIM_OS_WINDOWS
SYSTEM_INFO sysInfo;
memset(&sysInfo, 0, sizeof(sysInfo));
GetSystemInfo(&sysInfo);
long const align = sysInfo.dwAllocationGranularity;
#else
long const align = sysconf(_SC_PAGE_SIZE);
#endif
uint64_t const alignedOffset = (offset / align) * align;
ASSERT_LESS_OR_EQUAL(alignedOffset, offset, ());
uint64_t const length = size + (offset - alignedOffset);
ASSERT_GREATER_OR_EQUAL(length, size, ());
#ifdef OMIM_OS_WINDOWS
void * pMap = MapViewOfFile(m_hMapping, FILE_MAP_READ, alignedOffset >> (sizeof(DWORD) * 8), DWORD(alignedOffset), length);
if (pMap == NULL)
MYTHROW(Reader::OpenException, ("Can't map section:", tag, "with [offset, size]:", offset, size, "win last error:", GetLastError()));
#else
void * pMap = mmap(0, length, PROT_READ, MAP_SHARED, m_fd, alignedOffset);
if (pMap == MAP_FAILED)
MYTHROW(Reader::OpenException, ("Can't map section:", tag, "with [offset, size]:", offset, size, "errno:", strerror(errno)));
#endif
char const * data = reinterpret_cast<char const *>(pMap);
char const * d = data + (offset - alignedOffset);
return Handle(d, data, size, length);
}
} // namespace detail
/////////////////////////////////////////////////////////////////////////////
// FilesMappingContainer
/////////////////////////////////////////////////////////////////////////////
FilesMappingContainer::FilesMappingContainer(string const & fName)
{
Open(fName);
}
FilesMappingContainer::~FilesMappingContainer()
{
Close();
}
void FilesMappingContainer::Open(string const & fName)
{
{
FileReader reader(fName);
ReadInfo(reader);
}
m_file.Open(fName);
m_name = fName;
}
void FilesMappingContainer::Close()
{
m_file.Close();
m_name.clear();
}
FilesMappingContainer::Handle FilesMappingContainer::Map(Tag const & tag) const
{
Info const * p = GetInfo(tag);
if (!p)
MYTHROW(Reader::OpenException, ("Can't find section:", m_name, tag));
ASSERT_EQUAL(tag, p->m_tag, ());
return m_file.Map(p->m_offset, p->m_size, tag);
}
FileReader FilesMappingContainer::GetReader(Tag const & tag) const
{
Info const * p = GetInfo(tag);
if (!p)
MYTHROW(Reader::OpenException, ("Can't find section:", m_name, tag));
return FileReader(m_name).SubReader(p->m_offset, p->m_size);
}
/////////////////////////////////////////////////////////////////////////////
// FilesMappingContainer::Handle
/////////////////////////////////////////////////////////////////////////////
FilesMappingContainer::Handle::~Handle()
{
Unmap();
}
void FilesMappingContainer::Handle::Assign(Handle && h)
{
Unmap();
m_base = h.m_base;
m_origBase = h.m_origBase;
m_size = h.m_size;
m_origSize = h.m_origSize;
h.Reset();
}
void FilesMappingContainer::Handle::Unmap()
{
if (IsValid())
{
#ifdef OMIM_OS_WINDOWS
VERIFY(UnmapViewOfFile(m_origBase), ());
#else
VERIFY(0 == munmap((void*)m_origBase, m_origSize), ());
#endif
Reset();
}
}
void FilesMappingContainer::Handle::Reset()
{
m_base = m_origBase = 0;
m_size = m_origSize = 0;
}
/////////////////////////////////////////////////////////////////////////////
// FilesContainerW
/////////////////////////////////////////////////////////////////////////////
FilesContainerW::FilesContainerW(string const & fName, FileWriter::Op op)
: m_name(fName), m_bFinished(false)
{
Open(op);
}
void FilesContainerW::Open(FileWriter::Op op)
{
m_bNeedRewrite = true;
switch (op)
{
case FileWriter::OP_WRITE_TRUNCATE:
break;
case FileWriter::OP_WRITE_EXISTING:
{
// read an existing service info
FileReader reader(m_name);
ReadInfo(reader);
}
// Important: in append mode we should sort info-vector by offsets
sort(m_info.begin(), m_info.end(), LessOffset());
// Check that all offsets are unique
#ifdef DEBUG
for (size_t i = 1; i < m_info.size(); ++i)
ASSERT(m_info[i-1].m_offset < m_info[i].m_offset ||
m_info[i-1].m_size == 0 ||
m_info[i].m_size == 0, ());
#endif
break;
default:
ASSERT ( false, ("Unsupported options") );
break;
}
if (m_info.empty())
StartNew();
}
void FilesContainerW::StartNew()
{
// leave space for offset to service info
FileWriter writer(m_name);
uint64_t skip = 0;
writer.Write(&skip, sizeof(skip));
m_bNeedRewrite = false;
}
FilesContainerW::~FilesContainerW()
{
if (!m_bFinished)
Finish();
}
uint64_t FilesContainerW::SaveCurrentSize()
{
ASSERT(!m_bFinished, ());
uint64_t const curr = FileReader(m_name).Size();
if (!m_info.empty())
m_info.back().m_size = curr - m_info.back().m_offset;
return curr;
}
void FilesContainerW::DeleteSection(Tag const & tag)
{
{
// rewrite files on disk
FilesContainerR contR(m_name);
FilesContainerW contW(m_name + ".tmp");
for (size_t i = 0; i < m_info.size(); ++i)
{
if (m_info[i].m_tag != tag)
contW.Write(contR.GetReader(m_info[i].m_tag), m_info[i].m_tag);
}
}
// swap files
if (!my::DeleteFileX(m_name) || !my::RenameFileX(m_name + ".tmp", m_name))
MYTHROW(RootException, ("Can't rename file", m_name, "Sharing violation or disk error!"));
// do open to update m_info
Open(FileWriter::OP_WRITE_EXISTING);
}
FileWriter FilesContainerW::GetWriter(Tag const & tag)
{
ASSERT(!m_bFinished, ());
InfoContainer::const_iterator it = find_if(m_info.begin(), m_info.end(), EqualTag(tag));
if (it != m_info.end())
{
if (it+1 == m_info.end())
{
m_info.pop_back();
if (m_info.empty())
StartNew();
else
m_bNeedRewrite = true;
}
else
{
DeleteSection(it->m_tag);
}
}
if (m_bNeedRewrite)
{
m_bNeedRewrite = false;
ASSERT(!m_info.empty(), ());
uint64_t const curr = m_info.back().m_offset + m_info.back().m_size;
FileWriter writer(m_name, FileWriter::OP_WRITE_EXISTING, true);
writer.Seek(curr);
writer.WritePaddingByPos(kSectionAlignment);
m_info.push_back(Info(tag, writer.Pos()));
ASSERT(m_info.back().m_offset % kSectionAlignment == 0, ());
return writer;
}
else
{
SaveCurrentSize();
FileWriter writer(m_name, FileWriter::OP_APPEND);
writer.WritePaddingByPos(kSectionAlignment);
m_info.push_back(Info(tag, writer.Pos()));
ASSERT(m_info.back().m_offset % kSectionAlignment == 0, ());
return writer;
}
}
void FilesContainerW::Write(string const & fPath, Tag const & tag)
{
Write(ModelReaderPtr(make_unique<FileReader>(fPath)), tag);
}
void FilesContainerW::Write(ModelReaderPtr reader, Tag const & tag)
{
ReaderSource<ModelReaderPtr> src(reader);
FileWriter writer = GetWriter(tag);
rw::ReadAndWrite(src, writer);
}
void FilesContainerW::Write(vector<char> const & buffer, Tag const & tag)
{
if (!buffer.empty())
GetWriter(tag).Write(&buffer[0], buffer.size());
}
void FilesContainerW::Finish()
{
ASSERT(!m_bFinished, ());
uint64_t const curr = SaveCurrentSize();
FileWriter writer(m_name, FileWriter::OP_WRITE_EXISTING);
writer.Seek(0);
WriteToSink(writer, curr);
writer.Seek(curr);
sort(m_info.begin(), m_info.end(), LessInfo());
rw::Write(writer, m_info);
m_bFinished = true;
}
|