hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
samples.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <sstream>
3 #include <fstream>
4 #include <assert.h>
5 #include <string.h>
6 #include <vector>
7 
10 #include "SphinxSearcher.hpp"
11 #include "SphinxError.hpp"
15 
16 /*
17  {"data":"{\"parameters\":[{\"queryId\":\"777\"},{\"JsonType\":\"7\"}],\"q\":\"test\"}","type":0}
18 */
19 
21 {
22  const std::string inputJson = "{\"data\":\"{\\\"parameters\\\":[{\\\"queryId\\\":\\\"777\\\"},{\\\"JsonType\\\":\\\"7\\\"}],\\\"q\\\":\\\"test\\\"}\",\"type\":0}";
23 
24  // block sample of unserialize
25  HCE::sphinx::SphinxInputJsonMessage inputMsg(inputJson);
26 
27  std::cout << "ErrorMsg = " << inputMsg.getErrorMsg() << std::endl;
28  std::cout << "ErrorCode = " << inputMsg.getErrorCode() << std::endl;
29  assert(!inputMsg.isError());
30 
31  std::cout << "Input json type = " << (unsigned int)(inputMsg.getType()) << std::endl;
32  std::cout << "Json data count = " << inputMsg.getData().size() << std::endl;
33 
34  assert(inputMsg.getType()==HCE::sphinx::SphinxInputJsonMessage::MessageType::mtSearch);
35  assert(!inputMsg.getData().empty());
36 
37  HCE::sphinx::SphinxInputJsonMessageSearch messageSearch(inputMsg.getData());
38  assert(!messageSearch.isError());
39 
40  std::cout << "Query string: '" << messageSearch.getQueryString() << "'" << std::endl;
41  std::cout << "Parameters count = " << messageSearch.getQueryParameters().size() << std::endl;
42 
43  assert(messageSearch.getQueryString()=="test");
44  assert(messageSearch.getQueryParameters().size()==2);
45 
46  for (size_t i=0;i<messageSearch.getQueryParameters().size();++i)
47  {
48  if (messageSearch.getQueryParameters()[i].first=="queryId")
49  assert(messageSearch.getQueryParameters()[i].second=="777");
50 
51  if (messageSearch.getQueryParameters()[i].first=="JsonType")
52  assert(messageSearch.getQueryParameters()[i].second=="7");
53  std::cout << "Parameter name: '" << messageSearch.getQueryParameters()[i].first << "' value: '" << messageSearch.getQueryParameters()[i].second << "'" << std::endl;
54  }
55 
56  // block sample of serialize
57  std::string json;
59  inputSearch.setQueryString("test");
60  inputSearch.addQueryParameters("queryId", "777");
61  inputSearch.addQueryParameters("JsonType", "7");
62  inputSearch.serialize(json);
63 
64  inputMsg.setType(HCE::sphinx::SphinxInputJsonMessage::MessageType::mtSearch);
65  inputMsg.setData(json);
66 
67  std::stringstream str;
68  str << inputMsg;
69 
70  std::cout << " input json: '" << inputJson << "'\n";
71  std::cout << "output json: '" << str.str() << "'\n";
72  assert(str.str()==inputJson);
73  std::cout << "testInputJsonMessage - OK" << std::endl;
74 }
75 //-----------------------------------------------------------------------------
76 /*
77  {"data":"Some content of data","error_code":2000,"error_message":"Some error","time":23}
78 */
79 // cppcheck-suppress unusedFunction
81 {
82  const std::string inputJson = "{\"data\":\"U29tZSBjb250ZW50IG9mIGRhdGE=\",\"error_code\":2000,\"error_message\":\"Some error\",\"time\":23}";
83 
84  // block sample of unserialize
86 
87  assert(outputMessage.unserialize(inputJson));
88  assert(!outputMessage.isError());
89 
90  std::cout << "Error code = " << outputMessage.getErrorCode() << std::endl;
91  std::cout << "Error message: '" << outputMessage.getErrorMessage() << "'" << std::endl;
92  std::cout << "Json data: '" << outputMessage.getData() << "'" << std::endl;
93  std::cout << "Time = " << outputMessage.getTime() << std::endl;
94 
95  assert(outputMessage.getErrorCode()==2000);
96  assert(outputMessage.getErrorMessage()=="Some error");
97 
98  std::cout << "outputMessage.getData(): '" << outputMessage.getData() << "'" << std::endl;
99 
100  assert(outputMessage.getData()=="Some content of data");
101  assert(outputMessage.getTime()==23);
102 
103  // block sample of serialize
104  outputMessage.clear(); // clear object for repeat use
105  outputMessage.setErrorCode(2000);
106  outputMessage.setErrorMessage("Some error");
107  outputMessage.setData("Some content of data");
108  outputMessage.setTime(23);
109 
110  std::string outputJson;
111  assert(outputMessage.serialize(outputJson));
112  assert(!outputMessage.isError());
113 
114  std::cout << " input json: '" << inputJson << "'\n";
115  std::cout << "output json: '" << outputJson << "'\n";
116  assert(inputJson==outputJson);
117  std::cout << "testOutputJsonMessage - OK" << std::endl;
118 }
119 //-----------------------------------------------------------------------------
120 //-----------------------------------------------------------------------------
121 // cppcheck-suppress unusedFunction
123 {
124  const std::string nodeName = "test_node";
125  const std::string homeDir = "/home/alexander/.local/share/hce";
126  const std::string indexName = "i004";
127 
129 
130  std::cout << fObj.logMsg() << std::endl;
131  fObj.setLogStream(std::cout);
132 
133  std::cout << "LOAD CONFIG => " << std::boolalpha << fObj.isLoadedConfig() << "\tIS ERROR => " << std::boolalpha << fObj.isError()
134  << "\nERROR MSG: " << fObj.getErrorMsg() << "\tERROR CODE: " << fObj.getErrorCode() << std::endl;
135 
136  const std::string query = "apple";
137 
138  std::string json;
140 // std::vector<std::string> attrs = {"1"};
141 // msgSearch.addFilter((HCE::sphinx::SphinxFilter::FilterType)0, "mmedia", attrs, (HCE::sphinx::SphinxFilter::ExcludeType)0);
142 
143  msgSearch.setQueryString(query);
144  msgSearch.addQueryParameters("queryId", "123");
145  msgSearch.addQueryParameters("JsonType", "15");
146  msgSearch.addQueryParameters("timeout", "5");
147  msgSearch.addQueryParameters("max_results", "1000");
148  msgSearch.addQueryParameters("offset", "0");
149  msgSearch.addQueryParameters("limit", "0");
150  msgSearch.addQueryParameters("cutoff", "0");
151  msgSearch.addQueryParameters("sort_by", "mmedia");
152  msgSearch.addQueryParameters("order_by", "0");
153  msgSearch.addQueryParameters("ttl", "3");
154 
155  msgSearch.addExternalFields("uid");
156  msgSearch.addExternalFields("sid");
157  msgSearch.addExternalFields("lang");
158 
159  msgSearch.addOrderParameters("algorithm", "0");
160  msgSearch.addOrderParameters("order_by", "2");
161  msgSearch.addOrderFields("uid");
162  msgSearch.addOrderFields("lang");
163  msgSearch.addOrderFields("sid");
164 
165  assert(msgSearch.serialize(json));
166 
168  handler.setType(HCE::sphinx::SphinxInputJsonMessage::MessageType::mtSearch);
169  handler.setData(json);
170 
171  std::stringstream str;
172  str << handler;
173 
174  std::string ret = fObj.Process(str.str()); // run functional object
175 
176  if (!fObj.getErrorMsg().empty())
177  std::cout << "fObj.getErrorMsg() = " << fObj.getErrorMsg() << std::endl;
178  assert(fObj.getErrorMsg().empty());
179 
181  HCE::sphinx::SphinxResultDataSerializator ResultSerializator(Result);
182 
183  HCE::sphinx::SphinxOutputJsonMessage outputMessage(ret);
184  assert(!outputMessage.isError());
185  assert(ResultSerializator.unserialize(outputMessage.getData()));
186 
187  // sample of output search result into stream
188  std::cout << "Result:\n" << Result << std::endl;
189 
190  // sample possibility getting of data out SphinxResultData class
191  const size_t matchInfoCount = Result.getMatchInfoCount();
192  for (size_t i=0;i<matchInfoCount;++i)
193  {
194  unsigned long long docId = Result.getMatchInfoItem(i).getDocId();
195  std::string weight = Result.getMatchInfoItem(i).getWeight();
196 
197  std::cout << "docId: " << docId << " weight: " << weight << std::endl;
198 
199  const size_t attributesCount = Result.getMatchInfoItem(i).getAttributesCount();
200  for (size_t j=0;j<attributesCount;++j)
201  {
202  std::cout << Result.getMatchInfoItem(i).getAttrItem(j).name << " = " << Result.getMatchInfoItem(i).getAttrItem(j).value << std::endl;
203  }
204  }
205 
206  const size_t requestInfoCount = Result.getRequestInfoCount();
207  for (size_t i=0;i<requestInfoCount;++i)
208  {
209  std::cout << "nodeName: " << Result.getRequestInfoItem(i).getNodeName() << std::endl;
210  std::cout << "query: " << Result.getRequestInfoItem(i).getQuery() << std::endl;
211  std::cout << "queryId: " << Result.getRequestInfoItem(i).getQueryId() << std::endl;
212  std::cout << "maxResultsNumber: " << Result.getRequestInfoItem(i).getMaxResultsNumber() << std::endl;
213  std::cout << "orderBy: " << Result.getRequestInfoItem(i).getOrderBy() << std::endl;
214  std::cout << "total: " << Result.getRequestInfoItem(i).getTotal() << std::endl;
215  std::cout << "totalFound: " << Result.getRequestInfoItem(i).getTotalFound() << std::endl;
216  std::cout << "timeMsec: " << Result.getRequestInfoItem(i).getTimeMsec() << std::endl;
217 
218  const size_t wordsCount = Result.getRequestInfoItem(i).getWordsCount();
219  for (size_t j=0;j<wordsCount;++j)
220  {
221  std::cout << "word: " << Result.getRequestInfoItem(i).getWordItem(j).word
222  << ", hits: " << Result.getRequestInfoItem(i).getWordItem(j).hits
223  << ", docs: " << Result.getRequestInfoItem(i).getWordItem(j).docs << std::endl;
224  }
225  }
226 }
227 //-----------------------------------------------------------------------------
228 //-----------------------------------------------------------------------------
229 // cppcheck-suppress unusedFunction
231 {
232  const std::string nodeName = "test_node";
233  const std::string dataDir = "/var/lib/hce_cluster";
234  const std::string defaultIndex = "default_index";
235  HCE::sphinx::SphinxFunctionalObject fObj(nodeName, dataDir, defaultIndex);
236  fObj.log(HCE::LoggerStream::Priority::PRIO_INFORMATION) << "Create object...\n";
237  std::cout << "Log test:\n";
238  std::cout << "--------------------------\n" << fObj.logMsg(false) << std::endl;
239  fObj.log(HCE::LoggerStream::Priority::PRIO_INFORMATION) << "Write some text...\n";
240  std::cout << "--------------------------\n" << fObj.logMsg() << std::endl;
241 
242  // make output stream for save log
243  std::ofstream ofs("./out.log", std::fstream::trunc);
244  // init functional object from stream
245  fObj.setLogStream(ofs);
246  fObj.log(HCE::LoggerStream::Priority::PRIO_INFORMATION) << "first line to cout...\n";
247  fObj.log(HCE::LoggerStream::Priority::PRIO_INFORMATION) << "second line to cout...\n";
248  fObj.log(HCE::LoggerStream::Priority::PRIO_INFORMATION) << "three line to cout...\n";
249  ofs.close();
250  // next don't wrote
251  fObj.log(HCE::LoggerStream::Priority::PRIO_INFORMATION) << "Added some text...\n";
252  // log buffer is empty
253  std::cout << "--------------------------\n" << fObj.logMsg() << std::endl;
254 }
255 //-----------------------------------------------------------------------------
256 //-----------------------------------------------------------------------------
257 
258