hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SphinxWeightCalculator.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <sstream>
3 #include <utility>
4 #include <string.h>
5 
8 
9 namespace HCE
10 {
11 namespace sphinx
12 {
13 //-----------------------------------------------------------------------------
14 //-----------------------------------------------------------------------------
15 void CalculateStrategy::weightToStream(const std::string& input, std::ostream& os) throw (std::exception)
16 {
17  if (input.empty())
18  os << HexString(0);
19  else
20  os << HexString(std::stoull(input));
21 }
22 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
24 Poco::SharedPtr<CalculateStrategy> CalculateStrategyFactory::create(const std::string& weightAlgirithm)
25 {
26  Poco::SharedPtr<CalculateStrategy> strategy = nullptr;
27  try
28  {
29  if (weightAlgirithm.empty())
30  throw 1;
31 
32  switch(std::stoul(weightAlgirithm))
33  {
34  case CalculateStrategy::StrategyTypes::stUseFieldNames: strategy = new CalculateStrategyUseFieldNames();
35  break;
36  default: strategy = new CalculateStrategyDefault();
37  }
38  }
39  catch(...)
40  {
41  strategy = new CalculateStrategyDefault();
42  }
43  return strategy;
44 }
45 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
47 void SphinxWeightCalculator::calculate(const std::vector<std::string>& orderFields, SphinxResultData& resultData)
48 {
49  if (!strategy.isNull())
50  {
51  strategy->calculate(orderFields, resultData);
52  logString = strategy->getLogString();
53  }
54 }
55 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
57 void CalculateStrategyDefault::calculate(const std::vector<std::string>& orderFields, SphinxResultData& resultData)
58 {
59  std::stringstream logMsg;
60 
61  try
62  {
63  const size_t matchesCount = resultData.getMatchInfoCount();
64  for (size_t i=0;i<matchesCount;++i)
65  {
66  std::stringstream stringValue;
67  stringValue << resultData.getMatchInfoItem(i).getWeight();
68 
69  SphinxMatchInfo matchInfo(std::forward<const SphinxMatchInfo&>(resultData.getMatchInfoItem(i)));
70  matchInfo.setWeight(stringValue.str());
71  resultData.setMatchInfoItem(i, std::forward<SphinxMatchInfo>(matchInfo));
72  }
73  }
74  catch(std::exception& e)
75  {
76  logMsg.str("");
77  logMsg << e.what();
78  }
79  logString = logMsg.str();
80 }
81 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
83 void CalculateStrategyUseFieldNames::calculate(const std::vector<std::string>& orderFields, SphinxResultData& resultData)
84 {
85  std::stringstream logMsg;
86 
87  try
88  {
89  const size_t matchesCount = resultData.getMatchInfoCount();
90  for (size_t i=0;i<matchesCount;++i)
91  {
92  std::stringstream stringValue;
93  if (orderFields.empty())
94  {
95  weightToStream(resultData.getMatchInfoItem(i).getWeight(), stringValue);
96  }
97  else
98  {
99  for (size_t k=0;k<orderFields.size();++k)
100  {
101  // search_result_json_const::calcWeight;
102  // TODO
103  weightToStream(resultData.getMatchInfoItem(i).getFieldByName(orderFields[k]).value, stringValue);
104  }
105  }
106  SphinxMatchInfo matchInfo(std::forward<const SphinxMatchInfo&>(resultData.getMatchInfoItem(i)));
107  matchInfo.setWeight(stringValue.str());
108  resultData.setMatchInfoItem(i, std::forward<SphinxMatchInfo>(matchInfo));
109  }
110  }
111  catch(std::exception& e)
112  {
113  logMsg.str("");
114  logMsg << e.what();
115  }
116  logString = logMsg.str();
117 }
118 //-----------------------------------------------------------------------------
119 //-----------------------------------------------------------------------------
120 } // namespace sphinx
121 } // namespace HCE