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

map_uint32_to_val.hpp « coding - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 29a97c009b3be674bf03bb5db8344b28e11e85be (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
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
#pragma once

#include "coding/endianness.hpp"
#include "coding/file_container.hpp"
#include "coding/memory_region.hpp"
#include "coding/reader.hpp"
#include "coding/succinct_mapper.hpp"
#include "coding/varint.hpp"
#include "coding/write_to_sink.hpp"
#include "coding/writer.hpp"

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

#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-private-field"
#endif

#include "3party/succinct/elias_fano.hpp"
#include "3party/succinct/rs_bit_vector.hpp"

#if defined(__clang__)
#pragma clang diagnostic pop
#endif

#include <algorithm>
#include <cstdint>
#include <functional>
#include <memory>
#include <type_traits>
#include <unordered_map>
#include <vector>

// Format:
// File offset (bytes)  Field name          Field size (bytes)
// 0                    version             2
// 2                    endianness          2
// 4                    positions offset    4
// 8                    variables offset    4
// 12                   end of section      4
// 16                   identifiers table   positions offset - 16
// positions offset     positions table     variables offset - positions offset
// variables offset     variables blocks    end of section - variables offset
//
// Version and endianness is always in stored little-endian format.  0
// value of endianness means little endian, whereas 1 means big
// endian.
//
// All offsets are in little-endian format.
//
// Identifiers table is a bit-vector with rank-select table, where set
// bits denote that centers for the corresponding features are in
// table.  Identifiers table is stored in the native endianness.
//
// Positions table is a Elias-Fano table, where each entry corresponds
// to the start position of the variables block. Positions table is
// stored in the native endianness.
//
// Variables is a sequence of blocks, where each block (with the
// exception of the last one) is a sequence of kBlockSize variables
// encoded by block encoding callback.

// On Get call kBlockSize consecutive variables are decoded and cached in RAM.

template <typename Value>
class MapUint32ToValue
{
public:
  using ReadBlockCallback =
      std::function<void(NonOwningReaderSource &, uint32_t, std::vector<Value> &)>;

  static uint32_t constexpr kBlockSize = 64;

  struct Header
  {
    void Read(Reader & reader)
    {
      NonOwningReaderSource source(reader);
      m_version = ReadPrimitiveFromSource<uint16_t>(source);
      m_endianness = ReadPrimitiveFromSource<uint16_t>(source);

      m_positionsOffset = ReadPrimitiveFromSource<uint32_t>(source);
      m_variablesOffset = ReadPrimitiveFromSource<uint32_t>(source);
      m_endOffset = ReadPrimitiveFromSource<uint32_t>(source);
    }

    void Write(Writer & writer)
    {
      WriteToSink(writer, m_version);
      WriteToSink(writer, m_endianness);
      WriteToSink(writer, m_positionsOffset);
      WriteToSink(writer, m_variablesOffset);
      WriteToSink(writer, m_endOffset);
    }

    bool IsValid() const
    {
      if (m_version != 0)
      {
        LOG(LERROR, ("Unknown version."));
        return false;
      }

      if (m_endianness > 1)
      {
        LOG(LERROR, ("Wrong endianness value."));
        return false;
      }

      if (m_positionsOffset < sizeof(m_header))
      {
        LOG(LERROR, ("Positions before header:", m_positionsOffset, sizeof(m_header)));
        return false;
      }

      if (m_variablesOffset < m_positionsOffset)
      {
        LOG(LERROR, ("Deltas before positions:", m_variablesOffset, m_positionsOffset));
        return false;
      }

      if (m_endOffset < m_variablesOffset)
      {
        LOG(LERROR, ("End of section before variables:", m_endOffset, m_variablesOffset));
        return false;
      }

      return true;
    }

    uint16_t m_version = 0;
    uint16_t m_endianness = 0;
    uint32_t m_positionsOffset = 0;
    uint32_t m_variablesOffset = 0;
    uint32_t m_endOffset = 0;
  };

  static_assert(sizeof(Header) == 16, "Wrong header size");

  MapUint32ToValue(Reader & reader, ReadBlockCallback const & readBlockCallback)
    : m_reader(reader), m_readBlockCallback(readBlockCallback)
  {
  }

  ~MapUint32ToValue() = default;

  // Tries to get |value| for key identified by |id|.  Returns
  // false if table does not have entry for this id.
  WARN_UNUSED_RESULT bool Get(uint32_t id, Value & value)
  {
    if (id >= m_ids.size() || !m_ids[id])
      return false;

    uint32_t const rank = static_cast<uint32_t>(m_ids.rank(id));
    uint32_t const base = rank / kBlockSize;
    uint32_t const offset = rank % kBlockSize;

    auto & entry = m_cache[base];
    if (entry.empty())
    {
      entry.resize(kBlockSize);

      auto const start = m_offsets.select(base);
      auto const end = base + 1 < m_offsets.num_ones()
                           ? m_offsets.select(base + 1)
                           : m_header.m_endOffset - m_header.m_variablesOffset;

      std::vector<uint8_t> data(end - start);

      m_reader.Read(m_header.m_variablesOffset + start, data.data(), data.size());

      MemReader mreader(data.data(), data.size());
      NonOwningReaderSource msource(mreader);

      m_readBlockCallback(msource, kBlockSize, entry);
    }

    value = entry[offset];
    return true;
  }

  // Loads MapUint32ToValue instance. Note that |reader| must be alive
  // until the destruction of loaded table. Returns nullptr if
  // MapUint32ToValue can't be loaded.
  // It's guaranteed that |readBlockCallback| will not be called for empty block.
  static std::unique_ptr<MapUint32ToValue> Load(Reader & reader,
                                                ReadBlockCallback const & readBlockCallback)
  {
    uint16_t const version = ReadPrimitiveFromPos<uint16_t>(reader, 0 /* pos */);
    if (version != 0)
      return {};

    // Only single version of centers table is supported now.  If you need to implement new
    // versions, implement dispatching based on first-four-bytes version.
    auto table = std::make_unique<MapUint32ToValue>(reader, readBlockCallback);
    if (!table->Init())
      return {};
    return table;
  }

private:
  bool Init()
  {
    m_header.Read(m_reader);

    if (!m_header.IsValid())
      return false;

    bool const isHostBigEndian = IsBigEndianMacroBased();
    bool const isDataBigEndian = m_header.m_endianness == 1;
    bool const endiannesMismatch = isHostBigEndian != isDataBigEndian;

    {
      uint32_t const idsSize = m_header.m_positionsOffset - sizeof(m_header);
      std::vector<uint8_t> data(idsSize);
      m_reader.Read(sizeof(m_header), data.data(), data.size());
      m_idsRegion = std::make_unique<CopiedMemoryRegion>(move(data));
      EndiannessAwareMap(endiannesMismatch, *m_idsRegion, m_ids);
    }

    {
      uint32_t const offsetsSize = m_header.m_variablesOffset - m_header.m_positionsOffset;
      std::vector<uint8_t> data(offsetsSize);
      m_reader.Read(m_header.m_positionsOffset, data.data(), data.size());
      m_offsetsRegion = std::make_unique<CopiedMemoryRegion>(move(data));
      EndiannessAwareMap(endiannesMismatch, *m_offsetsRegion, m_offsets);
    }

    return true;
  }

  template <typename Cont>
  void EndiannessAwareMap(bool endiannesMismatch, CopiedMemoryRegion & region, Cont & cont)
  {
    Cont c;
    if (endiannesMismatch)
    {
      coding::ReverseMapVisitor visitor(region.MutableData());
      c.map(visitor);
    }
    else
    {
      coding::MapVisitor visitor(region.ImmutableData());
      c.map(visitor);
    }

    c.swap(cont);
  }

  Header m_header;
  Reader & m_reader;

  std::unique_ptr<CopiedMemoryRegion> m_idsRegion;
  std::unique_ptr<CopiedMemoryRegion> m_offsetsRegion;

  succinct::rs_bit_vector m_ids;
  succinct::elias_fano m_offsets;

  ReadBlockCallback m_readBlockCallback;

  std::unordered_map<uint32_t, std::vector<Value>> m_cache;
};

template <typename Value>
class MapUint32ToValueBuilder
{
public:
  using Iter = typename std::vector<Value>::const_iterator;
  using WriteBlockCallback = std::function<void(Writer &, Iter, Iter)>;
  using Map = MapUint32ToValue<Value>;

  void Put(uint32_t id, Value value)
  {
    if (!m_ids.empty())
      CHECK_LESS(m_ids.back(), id, ());

    m_values.push_back(value);
    m_ids.push_back(id);
  }

  // It's guaranteed that |writeBlockCallback| will not be called for empty block.
  void Freeze(Writer & writer, WriteBlockCallback const & writeBlockCallback) const
  {
    typename Map::Header header;

    auto const startOffset = writer.Pos();
    header.Write(writer);

    {
      uint64_t const numBits = m_ids.empty() ? 0 : m_ids.back() + 1;

      succinct::bit_vector_builder builder(numBits);
      for (auto const & id : m_ids)
        builder.set(id, true);

      coding::FreezeVisitor<Writer> visitor(writer);
      succinct::rs_bit_vector(&builder).map(visitor);
    }

    std::vector<uint32_t> offsets;
    std::vector<uint8_t> variables;

    {
      MemWriter<std::vector<uint8_t>> writer(variables);
      for (size_t i = 0; i < m_values.size(); i += Map::kBlockSize)
      {
        offsets.push_back(static_cast<uint32_t>(variables.size()));

        auto const endOffset = std::min(i + Map::kBlockSize, m_values.size());
        CHECK_GREATER(endOffset, i, ());
        writeBlockCallback(writer, m_values.cbegin() + i, m_values.cbegin() + endOffset);
      }
    }

    {
      succinct::elias_fano::elias_fano_builder builder(offsets.empty() ? 0 : offsets.back() + 1,
                                                       offsets.size());
      for (auto const & offset : offsets)
        builder.push_back(offset);

      header.m_positionsOffset = base::checked_cast<uint32_t>(writer.Pos() - startOffset);
      coding::FreezeVisitor<Writer> visitor(writer);
      succinct::elias_fano(&builder).map(visitor);
    }

    {
      header.m_variablesOffset = base::checked_cast<uint32_t>(writer.Pos() - startOffset);
      writer.Write(variables.data(), variables.size());
      header.m_endOffset = base::checked_cast<uint32_t>(writer.Pos() - startOffset);
    }

    auto const endOffset = writer.Pos();

    writer.Seek(startOffset);
    CHECK_EQUAL(header.m_endianness, 0, ("|m_endianness| should be set to little-endian."));
    header.Write(writer);
    writer.Seek(endOffset);
  }

private:
  std::vector<Value> m_values;
  std::vector<uint32_t> m_ids;
};