hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SphinxResultBuilder.cpp
Go to the documentation of this file.
2 #include "Exceptions.hpp"
3 #include <map>
4 
5 namespace HCE {
6 namespace sphinx{
7 namespace reduce_task {
8 
9 using namespace Poco;
10 
12 }
13 
15 }
16 
17 Poco::SharedPtr<SphinxResultData> SphinxResultBuilder::build(
18  Poco::SharedPtr<SphinxReduceDataStorage> &sphinxDataStorage,
19  const Poco::SharedPtr<HCE::reduce::core::ReduceResultIf> &sphinxReduceResult,
20  const std::vector<SphinxRequestInfo>& requestInfo)
21 {
22  SharedPtr<SphinxResultData> newSphinxResult(new SphinxResultData());
23  fillMatchInfo(sphinxDataStorage, sphinxReduceResult, newSphinxResult);
24  fillRequestInfo(requestInfo, newSphinxResult);
25  return newSphinxResult;
26 }
27 
28 Poco::SharedPtr<SphinxResultData> SphinxResultBuilder::buildFromUsingKeys(
29  const Poco::SharedPtr<SphinxResultData> &sphinxResultData,
30  std::vector<std::pair<std::string, unsigned long long> > &selectWeightKeys)
31 {
32  SharedPtr<SphinxResultData> newSphinxResult(new SphinxResultData());
33  fillMatchDataBaseOnKeys(sphinxResultData, selectWeightKeys, newSphinxResult);
34  fillRequestInfoBaseOnResultData(sphinxResultData, newSphinxResult);
35  return newSphinxResult;
36 }
37 
38 void SphinxResultBuilder::fillMatchInfo(
39  Poco::SharedPtr<SphinxReduceDataStorage> &sphinxDataStorage,
40  Poco::SharedPtr<HCE::reduce::core::ReduceResultIf> sphinxReduceResult,
41  Poco::SharedPtr<SphinxResultData>& newSphinxResultData)
42 {
43  std::string key = "";
44  std::string value = "";
46 
47  sphinxReduceResult->reset();
48  while(sphinxReduceResult->hasNext()){
49  sphinxReduceResult->getNext(key, value);
50  matchInfoIter = sphinxDataStorage->getData(key + value);
51  if (matchInfoIter.first == matchInfoIter.second){
52  throw reduce::NotFoundByKeyException("SphinxResultBuilder - not found " + key + value);
53  }
54  newSphinxResultData->addMatchInfo(matchInfoIter.first->second);
55  }
56 }
57 
58 void SphinxResultBuilder::fillRequestInfo(
59  const std::vector<SphinxRequestInfo>& requestInfo,
60  Poco::SharedPtr<SphinxResultData>& newSphinxResultData)
61 {
62  for (size_t infoIndex = 0; infoIndex < requestInfo.size(); ++infoIndex){
63  newSphinxResultData->addRequestInfo(requestInfo[infoIndex]);
64  }
65 }
66 
67 void SphinxResultBuilder::fillMatchDataBaseOnKeys(
68  const Poco::SharedPtr<SphinxResultData> &sphinxResultData,
69  std::vector<std::pair<std::string, unsigned long long> > &selectWeightKeys,
70  Poco::SharedPtr<SphinxResultData> &newSphinxResult)
71 {
72  std::map<unsigned long long, SphinxMatchInfo> keysMatchInfo;
73  std::map<unsigned long long, SphinxMatchInfo>::iterator bestMatchInfoIt;
74 
75  buildMatchInfoMap(keysMatchInfo, sphinxResultData);
76 
77  for (size_t matchIndex = 0; matchIndex < selectWeightKeys.size(); ++matchIndex){
78  bestMatchInfoIt = keysMatchInfo.find(selectWeightKeys[matchIndex].second);
79  if (bestMatchInfoIt != keysMatchInfo.end()){
80  newSphinxResult->addMatchInfo(bestMatchInfoIt->second);
81  }
82  }
83 }
84 
85 void SphinxResultBuilder::fillRequestInfoBaseOnResultData(
86  const Poco::SharedPtr<SphinxResultData> &sphinxResultData,
87  Poco::SharedPtr<SphinxResultData> &newSphinxResult)
88 {
89  size_t totalRequestInfoNumber = sphinxResultData->getRequestInfoCount();
90  for (size_t requestInfoIndex = 0; requestInfoIndex < totalRequestInfoNumber;
91  ++requestInfoIndex){
92  newSphinxResult->addRequestInfo(sphinxResultData->getRequestInfoItem(requestInfoIndex));
93  }
94 }
95 
96 void SphinxResultBuilder::buildMatchInfoMap(std::map<unsigned long long, SphinxMatchInfo> &keysMatchInfo,
97  const Poco::SharedPtr<SphinxResultData> &sphinxResultData)
98 {
99  size_t totalMatchInfoNumber = sphinxResultData->getMatchInfoCount();
100  for(size_t keyIndex = 0; keyIndex < totalMatchInfoNumber; ++keyIndex){
101  SphinxMatchInfo matchInfo = sphinxResultData->getMatchInfoItem(keyIndex);
102  keysMatchInfo.insert(std::make_pair(matchInfo.getDocId(), matchInfo));
103  }
104 }
105 
106 
107 }
108 }
109 }