hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SphinxResultTruncater.cpp
Go to the documentation of this file.
4 
5 namespace HCE
6 {
7 namespace sphinx
8 {
9 namespace reduce_task
10 {
11 
12 using namespace Poco;
13 using namespace std;
14 
16 }
17 
19 }
20 
21 SharedPtr<SphinxResultData> SphinxResultTruncater::trancate(
22  SharedPtr<SphinxResultData>& sphinxResultData)
23 {
24  SphinxResultTransformer sphinxResultTransformer;
25  SphinxResultBuilder sphinxResultBuilder;
26  vector<pair<string, unsigned long long> > weightKeyPairs;
27  SphinxResultSorterIf& sorter = selectResultSorter(sphinxResultData);
28 
29  sphinxResultTransformer.extractWeightKeys(sphinxResultData, weightKeyPairs);
30  sorter.sortByWeight(weightKeyPairs);
31  trancateByWeight(weightKeyPairs, sphinxResultData);
32  return sphinxResultBuilder.buildFromUsingKeys(sphinxResultData, weightKeyPairs);
33 }
34 
35 SphinxResultSorterIf& SphinxResultTruncater::selectResultSorter(SharedPtr<SphinxResultData>& sphinxResultData)
36 {
37  unsigned int sortMode = 0;
38  if (sphinxResultData->getRequestInfoCount() > 0){
39  sortMode = sphinxResultData->getRequestInfoItem(0).getOrderBy();
40  }
41  switch (sortMode){
42  case ASC_SORT:
43  return _sphinxResultAscSorter;
44 
45  case DESC_SORT:
46  return _sphinxResultDescSorter;
47  }
48  return _sphinxResultNullSorter;
49 }
50 
51 
52 void SphinxResultTruncater::trancateByWeight
53  (vector<pair<string, unsigned long long> >& sortedWeightDocIds,
54  SharedPtr<SphinxResultData>& sphinxResultData)
55 {
56  unsigned int maxNumberToSave = 0;
57  if (sphinxResultData->getRequestInfoCount() > 0){
58  maxNumberToSave = sphinxResultData->getRequestInfoItem(0).getMaxResultsNumber();
59  }
60  if (sortedWeightDocIds.size() > maxNumberToSave){
61  sortedWeightDocIds.erase(sortedWeightDocIds.begin() + maxNumberToSave,
62  sortedWeightDocIds.end());
63  }
64 }
65 
66 }
67 }
68 }