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

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

#include "indexer/locality_object.hpp"

#include "defines.hpp"

#include "coding/file_container.hpp"
#include "coding/var_record_reader.hpp"

#include "base/logging.hpp"

namespace indexer
{
namespace
{
template <class ReaderT>
class LocalityVector
{
  DISALLOW_COPY(LocalityVector);

public:
  LocalityVector(ReaderT const & reader) : m_recordReader(reader, 256 /* expectedRecordSize */) {}

  template <class ToDo>
  void ForEach(ToDo && toDo) const
  {
    m_recordReader.ForEachRecord([&](uint32_t pos, char const * data, uint32_t /*size*/) {
      LocalityObject o;
      o.Deserialize(data);
      toDo(o);
    });
  }

private:
  friend class LocalityVectorReader;

  VarRecordReader<FilesContainerR::TReader, &VarRecordSizeReaderVarint> m_recordReader;
};

// Test features vector (reader) that combines all the needed data for stand-alone work.
// Used in generator_tool and unit tests.
class LocalityVectorReader
{
  DISALLOW_COPY(LocalityVectorReader);

public:
  explicit LocalityVectorReader(string const & filePath)
    : m_cont(filePath), m_vector(m_cont.GetReader(LOCALITY_DATA_FILE_TAG))
  {
  }

  LocalityVector<ModelReaderPtr> const & GetVector() const { return m_vector; }

private:
  FilesContainerR m_cont;
  LocalityVector<ModelReaderPtr> m_vector;
};
}  // namespace

bool BuildLocalityIndexFromDataFile(string const & dataFile, string const & outFileName)
{
  try
  {
    string const idxFileName(outFileName + LOCALITY_INDEX_TMP_EXT);
    {
      LocalityVectorReader localities(dataFile);
      FileWriter writer(idxFileName);

      covering::BuildLocalityIndex(localities.GetVector(), writer, outFileName);
    }

    FilesContainerW(outFileName, FileWriter::OP_WRITE_TRUNCATE)
        .Write(idxFileName, LOCALITY_INDEX_FILE_TAG);
    FileWriter::DeleteFileX(idxFileName);
  }
  catch (Reader::Exception const & e)
  {
    LOG(LERROR, ("Error while reading file:", e.Msg()));
    return false;
  }
  catch (Writer::Exception const & e)
  {
    LOG(LERROR, ("Error writing index file:", e.Msg()));
    return false;
  }
  return true;
}
}  // namespace indexer