hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SphinxResultDataSerializator.hpp
Go to the documentation of this file.
1 
14 #ifndef SPHINX_RESULT_DATA_SERIALIZATOR_HPP
15 #define SPHINX_RESULT_DATA_SERIALIZATOR_HPP
16 
17 #include "SphinxResultData.hpp"
18 #include "JsonSerializable.hpp"
19 
20 namespace HCE
21 {
22 namespace sphinx
23 {
24 //-----------------------------------------------------------------------------
25 namespace search_result_json_const
26 {
27  // match info field names
28  const std::string docId = "Id";
29  const std::string weight = "W";
30  const std::string attrArray = "At";
31  const std::string matchInfo = "MI";
32 
33  // request info field names
34  const std::string nodeName = "node";
35  const std::string query = "q";
36  const std::string queryId = "qid";
37  const std::string maxResultsNumber = "max";
38  const std::string orderBy = "order";
39  const std::string total = "r";
40  const std::string totalFound = "f";
41  const std::string timeMsec = "time";
42 
43  // word info field names
44  const std::string word = "w";
45  const std::string hits = "h";
46  const std::string docs = "d";
47  const std::string wordArray = "WI";
48  const std::string requestInfo = "RI";
49 
50  // sphinx weight field added in attributes
51  const std::string sphinxWeight = "sw";
52  const std::string sphinxDocumentId = "id";
53 
54  // fields for extend culculate weight algirithm
55  const std::string calcWeight = "weight";
56  const std::string calcDocId = "doc_id";
57  const std::string calcNodeNumber = "node_number";
58  const std::string calcNodeName = "node_name";
59 
60 } // end namespace search_result_json_const
61 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
64 {
65  const char delimiter = ':';
66  unsigned int fieldsCount;
67  unsigned int fieldWidth;
68 public:
69  explicit SphinxWeightPacker(unsigned int fieldsCount_=0, unsigned int fieldWidth_=HEX_STRING_LENGTH) : fieldsCount(fieldsCount_), fieldWidth(fieldWidth_) {}
70  std::string pack(const std::string& data, unsigned int minNumberFieldsPacking);
71  std::string unpack(const std::string& data);
72 };
73 //-----------------------------------------------------------------------------
74 //-----------------------------------------------------------------------------
75 enum // list const use for setting JSON type of SphinxResultDataSerializator
76 {
82 };
83 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
86 {
87 public:
88  typedef unsigned int JsonType;
89 
90 public:
91  explicit SphinxResultDataSerializator(SphinxResultData& resultData);
92  SphinxResultDataSerializator(SphinxResultData& resultData, const std::string& json);
95 
96  bool serialize(SphinxResultDataSerializator::JsonType jsonType, std::string& json, unsigned int fieldCount=0, unsigned int minNumberFieldsPacking=0);
97  bool serialize(std::string& json, unsigned int fieldCount, unsigned int minNumberFieldsPacking);
98  bool serialize(std::string& json);
99  bool unserialize(const std::string& json);
100 
101  void setJsonType(SphinxResultDataSerializator::JsonType jsonType_) {jsonType=jsonType_;}
103 
104 private:
105  SphinxResultData& resultData;
106  JsonType jsonType;
107 
108 typedef IJsonSerializable inherited;
109 };
110 //-----------------------------------------------------------------------------
111 //-----------------------------------------------------------------------------
113 {
114 public:
115  explicit SphinxDefaultJSON(unsigned int matchesCount=0, unsigned int attributesCount=0,
116  unsigned int maxResultNumber=0);
118 
119  bool makeJSON(unsigned int matchesCount=0, unsigned int attributesCount=0,
120  unsigned int maxResultNumber=0);
121 
122  const std::string& getJSON(void) const {return json;}
123  const std::string& getErrorMsg(void) const {return errorMsg;}
124  unsigned int getErrorCode(void) const {return errorCode;}
125  bool isError(void) const {return _isError;}
126 private:
127  bool fillResultData(SphinxResultData& resultData, unsigned int matchesCount, unsigned int attributesCount, unsigned int maxResultNumber);
128 private:
129  std::string json;
130  std::string errorMsg;
131  unsigned int errorCode;
132  bool _isError;
133 
134  friend std::ostream& operator<<(std::ostream& os, const SphinxDefaultJSON& defaultJson);
135 };
136 //-----------------------------------------------------------------------------
137 //-----------------------------------------------------------------------------
138 } // namespace sphinx
139 } // namespace HCE
140 
141 #endif // SPHINX_RESULT_DATA_SERIALIZATOR_HPP