Welcome to mirror list, hosted at ThFree Co, Russian Federation.

file_container.hpp « coding - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1307e8e8cd1bb9abf5152c37df677d9ef1d4143a (plain)
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
#pragma once
#include "coding/file_reader.hpp"
#include "coding/file_writer.hpp"

#include "std/vector.hpp"
#include "std/string.hpp"
#include "std/noncopyable.hpp"
#include "std/utility.hpp"


class FilesContainerBase
{
public:
  typedef string Tag;

  /// Alignment of each new section that will be added to a file
  /// container, i.e. section's offset in bytes will be a multiple of
  /// this value.
  ///
  /// WARNING! Existing sections may not be properly aligned.
  static uint64_t const kSectionAlignment = 8;

  bool IsExist(Tag const & tag) const
  {
    return GetInfo(tag) != 0;
  }

protected:
  struct Info
  {
    Tag m_tag;
    uint64_t m_offset;
    uint64_t m_size;

    Info() {}
    Info(Tag const & tag, uint64_t offset) : m_tag(tag), m_offset(offset) {}
  };

  friend string DebugPrint(Info const & info);

  Info const * GetInfo(Tag const & tag) const;

  struct LessInfo
  {
    bool operator() (Info const & t1, Info const & t2) const
    {
      return (t1.m_tag < t2.m_tag);
    }
    bool operator() (Info const & t1, Tag const & t2) const
    {
      return (t1.m_tag < t2);
    }
    bool operator() (Tag const & t1, Info const & t2) const
    {
      return (t1 < t2.m_tag);
    }
  };

  struct LessOffset
  {
    bool operator() (Info const & t1, Info const & t2) const
    {
      if (t1.m_offset == t2.m_offset)
      {
        // Element with nonzero size should be the last one,
        // for correct append writer mode (FilesContainerW::GetWriter).
        return (t1.m_size < t2.m_size);
      }
      else
        return (t1.m_offset < t2.m_offset);
    }
    bool operator() (Info const & t1, uint64_t const & t2) const
    {
      return (t1.m_offset < t2);
    }
    bool operator() (uint64_t const & t1, Info const & t2) const
    {
      return (t1 < t2.m_offset);
    }
  };

  class EqualTag
  {
    Tag const & m_tag;
  public:
    EqualTag(Tag const & tag) : m_tag(tag) {}
    bool operator() (Info const & t) const
    {
      return (t.m_tag == m_tag);
    }
  };

  typedef vector<Info> InfoContainer;
  InfoContainer m_info;

  template <class ReaderT>
  void ReadInfo(ReaderT & reader);

public:
  template <class ToDo> void ForEachTag(ToDo toDo) const
  {
    for_each(m_info.begin(), m_info.end(), toDo);
  }
};

class FilesContainerR : public FilesContainerBase
{
public:
  using TReader = ModelReaderPtr;

  explicit FilesContainerR(string const & filePath,
                           uint32_t logPageSize = 10,
                           uint32_t logPageCount = 10);
  explicit FilesContainerR(TReader const & file);

  TReader GetReader(Tag const & tag) const;

  template <typename F> void ForEachTag(F f) const
  {
    for (size_t i = 0; i < m_info.size(); ++i)
      f(m_info[i].m_tag);
  }

  inline uint64_t GetFileSize() const { return m_source.Size(); }
  inline string const & GetFileName() const { return m_source.GetName(); }

  pair<uint64_t, uint64_t> GetAbsoluteOffsetAndSize(Tag const & tag) const;

private:
  TReader m_source;
};

namespace detail
{
class MappedFile
{
  DISALLOW_COPY(MappedFile);

public:
  MappedFile() = default;
  ~MappedFile() { Close(); }

  void Open(string const & fName);
  void Close();

  class Handle
  {
    DISALLOW_COPY(Handle);

    void Reset();

  public:
    Handle()
      : m_base(0), m_origBase(0), m_size(0), m_origSize(0)
    {
    }
    Handle(char const * base, char const * alignBase, uint64_t size, uint64_t origSize)
      : m_base(base), m_origBase(alignBase), m_size(size), m_origSize(origSize)
    {
    }
    Handle(Handle && h) : Handle()
    {
      Assign(move(h));
    }
    ~Handle();

    void Assign(Handle && h);

    void Unmap();

    bool IsValid() const { return (m_base != 0); }
    uint64_t GetSize() const { return m_size; }

    template <class T> T const * GetData() const
    {
      ASSERT_EQUAL(m_size % sizeof(T), 0, ());
      return reinterpret_cast<T const *>(m_base);
    }
    template <class T> size_t GetDataCount() const
    {
      ASSERT_EQUAL(m_size % sizeof(T), 0, ());
      return (m_size / sizeof(T));
    }

  private:
    char const * m_base;
    char const * m_origBase;
    uint64_t m_size;
    uint64_t m_origSize;
  };

  Handle Map(uint64_t offset, uint64_t size, string const & tag) const;

private:
#ifdef OMIM_OS_WINDOWS
  void * m_hFile = (void *)-1;
  void * m_hMapping = (void *)-1;
#else
  int m_fd = -1;
#endif
};

} // namespace detail

class FilesMappingContainer : public FilesContainerBase
{
public:
  typedef detail::MappedFile::Handle Handle;

  /// Do nothing by default, call Open to attach to file.
  FilesMappingContainer() = default;
  explicit FilesMappingContainer(string const & fName);

  ~FilesMappingContainer();

  void Open(string const & fName);
  void Close();

  Handle Map(Tag const & tag) const;
  FileReader GetReader(Tag const & tag) const;

  string const & GetName() const { return m_name; }

private:
  string m_name;
  detail::MappedFile m_file;
};

class FilesContainerW : public FilesContainerBase
{
public:
  FilesContainerW(string const & fName,
                  FileWriter::Op op = FileWriter::OP_WRITE_TRUNCATE);
  ~FilesContainerW();

  FileWriter GetWriter(Tag const & tag);

  void Write(string const & fPath, Tag const & tag);
  void Write(ModelReaderPtr reader, Tag const & tag);
  void Write(vector<char> const & buffer, Tag const & tag);
  void Write(vector<uint8_t> const & buffer, Tag const & tag);

  void Finish();

  /// Delete section with rewriting file.
  /// @precondition Container should be opened with FileWriter::OP_WRITE_EXISTING.
  void DeleteSection(Tag const & tag);

  inline string const & GetFileName() const { return m_name; }

private:
  uint64_t SaveCurrentSize();

  void Open(FileWriter::Op op);
  void StartNew();

  string m_name;
  bool m_bNeedRewrite;
  bool m_bFinished;
};