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

sloynik_engine.cpp « words - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 73377fdc37776dc9aec4136f8fe8d742dbdc5679 (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
#include "sloynik_engine.hpp"
#include "slof_dictionary.hpp"
#include "sloynik_index.hpp"

#include "../platform/platform.hpp"

#include "../coding/file_reader.hpp"
#include "../coding/file_writer.hpp"

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

sl::SloynikEngine::SloynikEngine(string const & dictionary,
                                 string const & index,
                                 StrFn const & strFn)
{
  Platform & pl = GetPlatform();
  Reader * pDicFileReader = pl.GetReader(dictionary);
  // m_pDictionary takes ownership of pDicFileReader.
  m_pDictionary.reset(new sl::SlofDictionary(pDicFileReader));

  vector<uint64_t> stamp;
  stamp.push_back(strFn.m_PrimaryCompareId);
  stamp.push_back(strFn.m_SecondaryCompareId);
  stamp.push_back(m_pDictionary->KeyCount());
  stamp.push_back(pDicFileReader->Size());

  string const stampFile = index + ".stamp";
  bool needIndexBuild = false;
  try
  {
    ReaderPtr<Reader> stampReader(pl.GetReader(stampFile));
    if (stampReader.Size() != stamp.size() * sizeof(stamp[0]))
      needIndexBuild = true;
    else
    {
      vector<uint64_t> currentStamp(stamp.size());
      stampReader.Read(0, &currentStamp[0], stamp.size() * sizeof(stamp[0]));
      if (currentStamp != stamp)
        needIndexBuild = true;
    }
  }
  catch (RootException const &)
  {
    needIndexBuild = true;
  }

  LOG(LDEBUG, ("Started sloynik engine. Words in the dictionary:", m_pDictionary->KeyCount()));

  // Uncomment to always rebuild the index: needIndexBuild = true;
  if (needIndexBuild)
  {
    string const stampPath = pl.WritablePathForFile(stampFile);
    FileWriter::DeleteFileX(stampPath);
    sl::SortedIndex::Build(*m_pDictionary, strFn, index);

    FileWriter stampWriter(stampPath);
    stampWriter.Write(&stamp[0], stamp.size() * sizeof(stamp[0]));
  }

  // By VNG: SortedIndex takes ownership of index reader, so no need to store it here.
  //m_pIndexReader.reset(new FileReader(indexPath + ".idx"));
  m_pSortedIndex.reset(new sl::SortedIndex(*m_pDictionary,
                       new FileReader(pl.WritablePathForFile(index + ".idx")),
                       strFn));
}

sl::SloynikEngine::~SloynikEngine()
{
}

void sl::SloynikEngine::Search(string const & prefix, sl::SloynikEngine::SearchResult & res) const
{
  res.m_FirstMatched = m_pSortedIndex->PrefixSearch(prefix);
}

void sl::SloynikEngine::GetWordInfo(WordId word, sl::SloynikEngine::WordInfo & res) const
{
  m_pDictionary->KeyById(m_pSortedIndex->KeyIdByPos(word), res.m_Word);
}

uint32_t sl::SloynikEngine::WordCount() const
{
  return m_pDictionary->KeyCount();
}

void sl::SloynikEngine::GetArticleData(WordId wordId, ArticleData & data) const
{
  m_pDictionary->ArticleById(m_pSortedIndex->KeyIdByPos(wordId), data.m_HTML);
}