HCE project C++ developers source code library  1.1.1
HCE project developer library
 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 // logMsg << "\nCalculateStrategyDefault::calculate\n";
61 
62  try
63  {
64  const size_t matchesCount = resultData.getMatchInfoCount();
65  for (size_t i=0;i<matchesCount;++i)
66  {
67  std::stringstream stringValue;
68 
69  // std::cout << "CalculateStrategyDefault::calculate weight = " << resultData.getMatchInfoItem(i).getWeight() << std::endl;
70  // weightToStream(resultData.getMatchInfoItem(i).getWeight(), stringValue); // obsolete
71 
72  stringValue << resultData.getMatchInfoItem(i).getWeight();
73 // std::cout << "CalculateStrategyDefault::calculate stringValue = " << stringValue.str() << std::endl;
74 
75  SphinxMatchInfo matchInfo(std::forward<const SphinxMatchInfo&>(resultData.getMatchInfoItem(i)));
76  matchInfo.setWeight(stringValue.str());
77  resultData.setMatchInfoItem(i, std::forward<SphinxMatchInfo>(matchInfo));
78  }
79  }
80  catch(std::exception& e)
81  {
82  logMsg.str("");
83  logMsg << e.what();
84  }
85  logString = logMsg.str();
86 }
87 //-----------------------------------------------------------------------------
88 //-----------------------------------------------------------------------------
89 void CalculateStrategyUseFieldNames::calculate(const std::vector<std::string>& orderFields, SphinxResultData& resultData)
90 {
91  std::stringstream logMsg;
92 // logMsg << "\nCalculateStrategyUseFieldNames::calculate\n";
93 
94  try
95  {
96  const size_t matchesCount = resultData.getMatchInfoCount();
97  for (size_t i=0;i<matchesCount;++i)
98  {
99  std::stringstream stringValue;
100  if (orderFields.empty())
101  {
102  weightToStream(resultData.getMatchInfoItem(i).getWeight(), stringValue);
103  }
104  else
105  {
106  for (size_t k=0;k<orderFields.size();++k)
107  {
108  // search_result_json_const::calcWeight;
109  // TODO
110  weightToStream(resultData.getMatchInfoItem(i).getFieldByName(orderFields[k]).value, stringValue);
111  }
112 
113  // logMsg << std::dec << "Old (dec): " << resultData.getMatchInfoItem(i).getWeight() << "\tNew (hex): " << stringValue.str() << std::endl;
114  }
115  SphinxMatchInfo matchInfo(std::forward<const SphinxMatchInfo&>(resultData.getMatchInfoItem(i)));
116  matchInfo.setWeight(stringValue.str());
117  resultData.setMatchInfoItem(i, std::forward<SphinxMatchInfo>(matchInfo));
118  }
119  }
120  catch(std::exception& e)
121  {
122  logMsg.str("");
123  logMsg << e.what();
124  }
125  logString = logMsg.str();
126 }
127 //-----------------------------------------------------------------------------
128 //-----------------------------------------------------------------------------
129 } // namespace sphinx
130 } // namespace HCE