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

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

#include "coding/endianness.hpp"
#include "coding/internal/file_data.hpp"

#include "std/algorithm.hpp"
#include "std/cstring.hpp"

#include "base/assert.hpp"
#include "base/logging.hpp"

namespace
{

// Current file format version
uint32_t constexpr kCurrentVersion = 1;

// Header size in bytes, header consists of uint32_t 'version' only
uint32_t constexpr kHeaderSize = sizeof(uint32_t);

// Number of items for batch processing
size_t constexpr kItemBlockSize = 1000;

// TODO
// Now GpsInfo written as plain values, but values can be compressed.

// Size of point in bytes to write in file of read from file
size_t constexpr kPointSize = 8 * sizeof(double) + sizeof(uint8_t);

// Writes value in memory in LittleEndian
template <typename T>
void MemWrite(void * ptr, T value)
{
  value = SwapIfBigEndian(value);
  memcpy(ptr, &value, sizeof(T));
}

// Read value from memory, which is LittleEndian in memory
template <typename T>
T MemRead(void const * ptr)
{
  T value;
  memcpy(&value, ptr, sizeof(T));
  return SwapIfBigEndian(value);
}

void Pack(char * p, location::GpsInfo const & info)
{  
  MemWrite<double>(p + 0 * sizeof(double), info.m_timestamp);
  MemWrite<double>(p + 1 * sizeof(double), info.m_latitude);
  MemWrite<double>(p + 2 * sizeof(double), info.m_longitude);
  MemWrite<double>(p + 3 * sizeof(double), info.m_altitude);
  MemWrite<double>(p + 4 * sizeof(double), info.m_speed);
  MemWrite<double>(p + 5 * sizeof(double), info.m_bearing);
  MemWrite<double>(p + 6 * sizeof(double), info.m_horizontalAccuracy);
  MemWrite<double>(p + 7 * sizeof(double), info.m_verticalAccuracy);
  ASSERT_LESS_OR_EQUAL(static_cast<int>(info.m_source), 255, ());
  uint8_t const source = static_cast<uint8_t>(info.m_source);
  MemWrite<uint8_t>(p + 8 * sizeof(double), source);
}

void Unpack(char const * p, location::GpsInfo & info)
{
  info.m_timestamp = MemRead<double>(p + 0 * sizeof(double));
  info.m_latitude = MemRead<double>(p + 1 * sizeof(double));
  info.m_longitude = MemRead<double>(p + 2 * sizeof(double));
  info.m_altitude = MemRead<double>(p + 3 * sizeof(double));
  info.m_speed = MemRead<double>(p + 4 * sizeof(double));
  info.m_bearing = MemRead<double>(p + 5 * sizeof(double));
  info.m_horizontalAccuracy = MemRead<double>(p + 6 * sizeof(double));
  info.m_verticalAccuracy = MemRead<double>(p + 7 * sizeof(double));
  uint8_t const source = MemRead<uint8_t>(p + 8 * sizeof(double));
  info.m_source = static_cast<location::TLocationSource>(source);
}

inline size_t GetItemOffset(size_t itemIndex)
{
  return kHeaderSize + itemIndex * kPointSize;
}

inline size_t GetItemCount(size_t fileSize)
{
  ASSERT_GREATER_OR_EQUAL(fileSize, kHeaderSize, ());
  return (fileSize - kHeaderSize) / kPointSize;
}

inline bool WriteVersion(fstream & f, uint32_t version)
{
  static_assert(kHeaderSize == sizeof(version), "");
  version = SwapIfBigEndian(version);
  f.write(reinterpret_cast<char const *>(&version), kHeaderSize);
  return f.good();
}

inline bool ReadVersion(fstream & f, uint32_t & version)
{
  static_assert(kHeaderSize == sizeof(version), "");
  f.read(reinterpret_cast<char *>(&version), kHeaderSize);
  version = SwapIfBigEndian(version);
  return f.good();
}

} // namespace

GpsTrackStorage::GpsTrackStorage(string const & filePath, size_t maxItemCount)
  : m_filePath(filePath)
  , m_maxItemCount(maxItemCount)
  , m_itemCount(0)
{
  ASSERT_GREATER(m_maxItemCount, 0, ());

  // Open existing file
  m_stream.open(m_filePath, ios::in | ios::out | ios::binary);

  if (m_stream)
  {
    uint32_t version = 0;
    if (!ReadVersion(m_stream, version))
      MYTHROW(OpenException, ("Read version error.", m_filePath));

    if (version == kCurrentVersion)
    {
      // Seek to end to get file size
      m_stream.seekp(0, ios::end);
      if (!m_stream.good())
        MYTHROW(OpenException, ("Seek to the end error.", m_filePath));

      size_t const fileSize = m_stream.tellp();

      m_itemCount = GetItemCount(fileSize);

      // Set write position after last item position
      size_t const offset = GetItemOffset(m_itemCount);
      m_stream.seekp(offset, ios::beg);
      if (!m_stream.good())
        MYTHROW(OpenException, ("Seek to the offset error:", offset, m_filePath));
    }
    else
    {
      m_stream.close();
      // TODO: migration for file m_filePath from version 'version' to version 'kCurrentVersion'
      // TODO: For now we support only one version, but in future migration may be needed.
    }
  }

  if (!m_stream)
  {
    // Create new file
    m_stream.open(m_filePath, ios::in | ios::out | ios::binary | ios::trunc);

    if (!m_stream)
      MYTHROW(OpenException, ("Open file error.", m_filePath));

    if (!WriteVersion(m_stream, kCurrentVersion))
      MYTHROW(OpenException, ("Write version error.", m_filePath));

    m_itemCount = 0;
  }
}

void GpsTrackStorage::Append(vector<TItem> const & items)
{
  ASSERT(m_stream.is_open(), ());

  if (items.empty())
    return;

  bool const needTrunc = (m_itemCount + items.size()) > (m_maxItemCount * 2); // see NOTE in declaration

  if (needTrunc)
    TruncFile();

  // Write position must be after last item position
  ASSERT_EQUAL(m_stream.tellp(), GetItemOffset(m_itemCount), ());

  vector<char> buff(min(kItemBlockSize, items.size()) * kPointSize);
  for (size_t i = 0; i < items.size();)
  {
    size_t const n = min(items.size() - i, kItemBlockSize);

    for (size_t j = 0; j < n; ++j)
      Pack(&buff[0] + j * kPointSize, items[i + j]);

    m_stream.write(&buff[0], n * kPointSize);
    if (!m_stream.good())
      MYTHROW(WriteException, ("File:", m_filePath));

    i += n;
  }

  m_stream.flush();
  if (!m_stream.good())
    MYTHROW(WriteException, ("File:", m_filePath));

  m_itemCount += items.size();
}

void GpsTrackStorage::Clear()
{
  ASSERT(m_stream.is_open(), ());

  m_itemCount = 0;

  m_stream.close();

  m_stream.open(m_filePath, ios::in | ios::out | ios::binary | ios::trunc);

  if (!m_stream)
    MYTHROW(WriteException, ("File:", m_filePath));

  if (!WriteVersion(m_stream, kCurrentVersion))
    MYTHROW(WriteException, ("File:", m_filePath));

  // Write position is set to the first item in the file
  ASSERT_EQUAL(m_stream.tellp(), GetItemOffset(0), ());
}

void GpsTrackStorage::ForEach(std::function<bool(TItem const & item)> const & fn)
{
  ASSERT(m_stream.is_open(), ());

  size_t i = GetFirstItemIndex();

  // Set read position to the first item
  m_stream.seekg(GetItemOffset(i), ios::beg);
  if (!m_stream.good())
    MYTHROW(ReadException, ("File:", m_filePath));

  vector<char> buff(min(kItemBlockSize, m_itemCount) * kPointSize);
  for (; i < m_itemCount;)
  {
    size_t const n = min(m_itemCount - i, kItemBlockSize);

    m_stream.read(&buff[0], n * kPointSize);
    if (!m_stream.good())
      MYTHROW(ReadException, ("File:", m_filePath));

    for (size_t j = 0; j < n; ++j)
    {      
      TItem item;
      Unpack(&buff[0] + j * kPointSize, item);
      if (!fn(item))
        return;
    }

    i += n;
  }
}

void GpsTrackStorage::TruncFile()
{
  string const tmpFilePath = m_filePath + ".tmp";

  // Create a tmp file
  fstream tmp(tmpFilePath, ios::in | ios::out | ios::binary | ios::trunc);

  if (!tmp)
    MYTHROW(WriteException, ("Unable to create temporary file:", tmpFilePath));

  if (!WriteVersion(tmp, kCurrentVersion))
    MYTHROW(WriteException, ("File:", tmpFilePath));

  size_t i = GetFirstItemIndex();

  // Set read position to the first item
  m_stream.seekg(GetItemOffset(i), ios::beg);
  if (!m_stream.good())
    MYTHROW(ReadException, ("File:", m_filePath));

  size_t newItemCount = 0;

  // Copy items
  vector<char> buff(min(kItemBlockSize, m_itemCount) * kPointSize);
  for (; i < m_itemCount;)
  {
    size_t const n = min(m_itemCount - i, kItemBlockSize);

    m_stream.read(&buff[0], n * kPointSize);
    if (!m_stream.good())
      MYTHROW(ReadException, ("File:", m_filePath));

    tmp.write(&buff[0], n * kPointSize);
    if (!tmp.good())
      MYTHROW(WriteException, ("File:", tmpFilePath));

    i += n;
    newItemCount += n;
  }
  buff.clear();
  buff.shrink_to_fit();

  tmp.close();
  m_stream.close();

  // Replace file
  if (!my::DeleteFileX(m_filePath) ||
      !my::RenameFileX(tmpFilePath, m_filePath))
  {
    MYTHROW(WriteException, ("File:", m_filePath));
  }

  // Reopen stream
  m_stream.open(m_filePath, ios::in | ios::out | ios::binary | ios::ate);

  if (!m_stream)
    MYTHROW(WriteException, ("File:", m_filePath));

  m_itemCount = newItemCount;

  // Write position must be after last item position (end of file)
  ASSERT_EQUAL(m_stream.tellp(), GetItemOffset(m_itemCount), ());
}

size_t GpsTrackStorage::GetFirstItemIndex() const
{
  return (m_itemCount > m_maxItemCount) ? (m_itemCount - m_maxItemCount) : 0; // see NOTE in declaration
}