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

file_container.cpp « coding - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c17c7cb811b8a4ec645a70e9d06a524f9177c751 (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
#include "../base/SRC_FIRST.hpp"

#include "file_container.hpp"
#include "varint.hpp"
#include "write_to_sink.hpp"


/////////////////////////////////////////////////////////////////////////////
// FilesContainerBase
/////////////////////////////////////////////////////////////////////////////

void FilesContainerBase::ReadInfo(FileReader & reader)
{
  uint64_t offset = ReadPrimitiveFromPos<uint64_t>(reader, 0);

  ReaderSource<FileReader> src(reader);
  src.Skip(offset);

  uint32_t const count = ReadVarUint<uint32_t>(src);
  m_info.resize(count);

  for (uint32_t i = 0; i < count; ++i)
  {
    uint32_t const tagSize = ReadVarUint<uint32_t>(src);
    m_info[i].m_tag.resize(tagSize);
    src.Read(&m_info[i].m_tag[0], tagSize);

    m_info[i].m_offset = ReadVarUint<uint64_t>(src);
    m_info[i].m_size = ReadVarUint<uint64_t>(src);
  }
}

/////////////////////////////////////////////////////////////////////////////
// FilesContainerR
/////////////////////////////////////////////////////////////////////////////

FilesContainerR::FilesContainerR(string const & fName,
                                 uint32_t logPageSize,
                                 uint32_t logPageCount)
: m_source(fName, logPageSize, logPageCount)
{
  ReadInfo(m_source);
}

FileReader FilesContainerR::GetReader(Tag const & tag) const
{
  InfoContainer::const_iterator i =
    lower_bound(m_info.begin(), m_info.end(), tag, LessInfo());

  if (i != m_info.end() && i->m_tag == tag)
    return m_source.SubReader(i->m_offset, i->m_size);
  else
    MYTHROW(Reader::OpenException, (tag));
}

/////////////////////////////////////////////////////////////////////////////
// FilesContainerW
/////////////////////////////////////////////////////////////////////////////

FilesContainerW::FilesContainerW(string const & fName, FileWriter::Op op)
: m_name(fName), m_bFinished(false)
{
  switch (op)
  {
  case FileWriter::OP_WRITE_TRUNCATE: // default usage
    break;

  case FileWriter::OP_APPEND:
    m_bNeedRewrite = true;    // need to override service info after appending
                              // 'break' doesn't present!

  case FileWriter::OP_WRITE_EXISTING:
    {
      // read an existing service info
      FileReader reader(fName);
      ReadInfo(reader);
    }
  }

  if (m_info.empty())
  {
    // leave space for offset to service info
    FileWriter writer(fName);
    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;
}

FileWriter FilesContainerW::GetWriter(Tag const & tag)
{
  ASSERT(!m_bFinished, ());
  if (m_bNeedRewrite)
  {
    m_bNeedRewrite = false;
    ASSERT ( !m_info.empty(), () );

    uint64_t const curr = m_info.back().m_offset + m_info.back().m_size;
    m_info.push_back(Info(tag, curr));

    FileWriter writer(m_name, FileWriter::OP_WRITE_EXISTING);
    writer.Seek(curr);
    return writer;
  }
  else
  {
    uint64_t const curr = SaveCurrentSize();
    m_info.push_back(Info(tag, curr));
    return FileWriter(m_name, FileWriter::OP_APPEND);
  }
}

FileWriter FilesContainerW::GetExistingWriter(Tag const & tag)
{
  InfoContainer::const_iterator i =
    lower_bound(m_info.begin(), m_info.end(), tag, LessInfo());

  if (i != m_info.end() && i->m_tag == tag)
  {
    FileWriter writer(m_name, FileWriter::OP_WRITE_EXISTING);
    writer.Seek(i->m_offset);
    return writer;
  }
  else
    MYTHROW(Writer::OpenException, (tag));
}

void FilesContainerW::Append(string const & fName, Tag const & tag)
{
  ASSERT(!m_bFinished, ());
  uint64_t const bufferSize = 4*1024;
  char buffer[bufferSize];

  FileReader reader(fName);
  ReaderSource<FileReader> src(reader);
  FileWriter writer = GetWriter(tag);

  uint64_t size = reader.Size();
  while (size > 0)
  {
    size_t const curr = static_cast<size_t>(min(bufferSize, size));

    src.Read(&buffer[0], curr);
    writer.Write(&buffer[0], curr);

    size -= curr;
  }
}

void FilesContainerW::Append(vector<char> const & buffer, Tag const & tag)
{
  ASSERT(!m_bFinished, ());
  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);
  }

  sort(m_info.begin(), m_info.end(), LessInfo());

  FileWriter writer(m_name, FileWriter::OP_APPEND);

  uint32_t const count = m_info.size();
  WriteVarUint(writer, count);

  for (uint32_t i = 0; i < count; ++i)
  {
    size_t const tagSize = m_info[i].m_tag.size();
    WriteVarUint(writer, tagSize);
    writer.Write(&m_info[i].m_tag[0], tagSize);

    WriteVarUint(writer, m_info[i].m_offset);
    WriteVarUint(writer, m_info[i].m_size);
  }

  m_bFinished = true;
}