highlighter application  1.1
HCE project utils : highlighter
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
ContentsStorageSimple.cpp
Go to the documentation of this file.
1 
13 #include <Poco/Timestamp.h>
14 
16 #include "Defs.hpp"
17 
18 #define INTERNAL_SLEEP 4000
19 
20 void ContentsStorageSimple::fillWordPosMap(Poco::SharedPtr<HCE::OutDataRefine> outDataRefine, ContentsStorageBase::WordPosPtrType ret)
21 {
22  if(!outDataRefine.isNull() && !ret.isNull())
23  {
24  CRC64 crc64;
25  std::map<unsigned long long, std::vector<WordPos> > wordPos;
26  unsigned long long key = 0llu;
27  std::vector<HCE::CWords> cwords = outDataRefine->getCWords();
28  unsigned int realWordPos = 0;
29  for(unsigned int i = 0; i < cwords.size(); i++)
30  {
31  if(cwords[i].getWordType() != HCE::WORD_TYPE::WORD && cwords[i].getWordType() != HCE::WORD_TYPE::NUMBER)
32  {
33  continue;
34  }
35  key = crc64.calc(cwords[i].getNormWord().c_str(), cwords[i].getNormWord().length());
36  auto it = wordPos.find(key);
37  if(it == wordPos.end())
38  {
39  std::vector<WordPos> localVec;
40  localVec.push_back(WordPos(cwords[i].getOffset(), cwords[i].getOffset() + cwords[i].getInitWordLen(), realWordPos, 0));
41  wordPos.insert(std::pair<unsigned long long, std::vector<WordPos> >(key, localVec));
42  }
43  else
44  {
45  (*it).second.push_back(WordPos(cwords[i].getOffset(), cwords[i].getOffset() + cwords[i].getInitWordLen(), realWordPos, 0));
46  }
47  realWordPos++;
48  }
49  ret->push_back(wordPos);
50  }
51 }
52 
53 ContentsStorageBase::WordPosPtrType ContentsStorageSimple::wordMapExctract(unsigned long long key, std::string &incomeContent)
54 {
55  WordPosPtrType ret = nullptr;
56  auto it = wordPosCache.find(key);
57  if(it == wordPosCache.end())
58  {
60  if(!refinePtr.isNull())
61  {
62  ret = WordPosPtrType(new std::vector<std::map<unsigned long long, std::vector<WordPos> > >);
63  Poco::SharedPtr<HCE::DataBase> inData = nullptr;
64  Poco::SharedPtr<HCE::DataBase> outDataContent = nullptr;
65 
66  inData = Poco::SharedPtr<HCE::DataBase>(new HCE::InDataRefine(HCE::CT_REFINE));
67  inData.cast<HCE::InDataRefine>()->setNormalizationId(DEFAULT_NORMALIZATION_WITHOUT);
68  inData.cast<HCE::InDataRefine>()->setSubjectMask(DEFAULT_SUBJECT_MASK);
69  inData.cast<HCE::InDataRefine>()->setContent(incomeContent);
70  outDataContent = refinePtr->process(inData);
71 
72  fillWordPosMap(outDataContent.cast<HCE::OutDataRefine>(), ret);
73 
74  inData = Poco::SharedPtr<HCE::DataBase>(new HCE::InDataRefine(HCE::CT_REFINE));
75  inData.cast<HCE::InDataRefine>()->setNormalizationId(DEFAULT_NORMALIZATION_WITH);
76  inData.cast<HCE::InDataRefine>()->setSubjectMask(DEFAULT_SUBJECT_MASK);
77  inData.cast<HCE::InDataRefine>()->setContent(incomeContent);
78  outDataContent = refinePtr->process(inData);
80 
81  fillWordPosMap(outDataContent.cast<HCE::OutDataRefine>(), ret);
82 /* TODO
83  std::pair<unsigned long long, std::pair<WordPosPtrType, Poco::SharedPtr<std::atomic_flag> > > element;
84  element.first = key;
85  element.second.first = ret;
86  element.second.second->test_and_set(std::memory_order_acquire);
87  wordPosCache.insert(element);
88 */
89  }
90  }
91  else
92  {
93  while((*it).second.second->test_and_set(std::memory_order_acquire))
94  {
95  usleep(INTERNAL_SLEEP);
96  }
97  ret = (*it).second.first;
98  }
99  return ret;
100 }
101 
103 {
104  auto it = wordPosCache.find(key);
105  if(it != wordPosCache.end())
106  {
107  (*it).second.second->clear(std::memory_order_release);
108  }
109  refinePtr = nullptr;
110 }
111 
113 {
114  auto it = wordPosCache.find(key);
115  if(it == wordPosCache.end())
116  {
117  std::pair<unsigned long long, std::pair<WordPosPtrType, Poco::SharedPtr<std::atomic_flag> > > element;
118  element.first = key;
119  element.second.first = wordPos;
120  element.second.second = Poco::SharedPtr<std::atomic_flag>(new std::atomic_flag());
121  element.second.second->clear(std::memory_order_release);
122  wordPosCache.insert(element);
123  }
124 }
125 
126 // cppcheck-suppress unusedFunction
128 {
129  for(auto it = wordPosCache.begin(); it != wordPosCache.end(); ++it)
130  {
131  while((*it).second.second->test_and_set(std::memory_order_acquire))
132  {
133  usleep(INTERNAL_SLEEP);
134  }
135  (*it).second.second->clear(std::memory_order_release);
136  }
137  wordPosCache.clear();
138 }
139 
141 {
142 }
143 
145 {
146 }