hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SphinxIndexer.cpp
Go to the documentation of this file.
1 #include <sstream>
2 #include <fstream>
3 #include <stdio.h>
4 #include <errno.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <dirent.h>
8 #include <time.h>
9 #include <sys/time.h>
10 #include <unistd.h>
11 #include <Poco/AutoPtr.h>
12 
13 #include "EncodeDecodeBase64.hpp"
14 #include "SphinxIndexer.hpp"
19 #include "SphinxAdminCommand.hpp"
20 #include "SphinxDataSource.hpp"
21 #include "SphinxError.hpp"
22 #include "SphinxConfigCreator.hpp"
23 #include "ProcExec.hpp"
24 #include "SphinxMessageConst.hpp"
25 
26 namespace HCE
27 {
28 namespace sphinx
29 {
30 //-----------------------------------------------------------------------------
32 : inherited(), fObj(fObj_), message(fObj.getCustomMessage())
33 {
34 }
35 //-----------------------------------------------------------------------------
36 std::string SphinxIndexer::Process(const std::string& json)
37 {
38  _isError = false;
39  errorMsg.clear();
41  std::string resultData;
42  timeval tvStart, tvStop;
43  gettimeofday(&tvStart, 0); //gettimeofday does not support TZ adjust on Linux.
44 
45  try
46  {
47  SphinxInputJsonMessage inputMessage(json);
48  if (inputMessage.isError())
49  throw Poco::Exception(inputMessage.getErrorMsg(), PARSE_ERROR);
50 
51  if (inputMessage.getType() == SphinxInputJsonMessage::MessageType::mtIndex)
52  {
53  makeIndexCommand(inputMessage.getData());
54  }
55  else if (inputMessage.getType() == SphinxInputJsonMessage::MessageType::mtManage)
56  {
57  makeAdminCommand(inputMessage.getData(), resultData);
58  }
59  else
61 
62  }
63  catch(Poco::Exception& e)
64  {
65  _isError = true;
66  errorCode = e.code();
67  errorMsg = e.displayText();
68  fObj.log(LoggerStream::Priority::PRIO_INFORMATION) << message(message_const::ERROR, errorMsg) << flush;
69  }
70 
71  std::string resJson;
72  gettimeofday(&tvStop, 0);
73 
74  SphinxOutputJsonMessage outputMessage;
75  outputMessage.setErrorCode(errorCode);
76  outputMessage.setErrorMessage(errorMsg);
77  outputMessage.setData(resultData);
78  outputMessage.setTime(getTimeInterval(tvStart, tvStop)/1000);
79  bool success = outputMessage.serialize(resJson);
80  if (!_isError)
81  _isError = !success;
82 
83  return resJson;
84 }
85 //-----------------------------------------------------------------------------
86 void SphinxIndexer::makeIndexCommand(const std::string& json) throw (Poco::Exception)
87 {
88  _isError = false;
89  errorMsg.clear();
91 
92  fObj.log(LoggerStream::Priority::PRIO_INFORMATION) << message(message_const::TRY_MAKE_INDEX_COMMAND) << flush;
93  SphinxInputJsonMessageIndex messageIndex(json);
94  if (messageIndex.isError())
95  throw Poco::Exception(messageIndex.getErrorMsg(), COMMAND_ERROR);
96 
97  if (messageIndex.getDocumentFile().empty())
98  throw Poco::Exception(message(message_const::DOCUMENT_CONTENT_IS_EMPTY), COMMAND_ERROR);
99 
100  const std::string targetIndexName = (messageIndex.getIndexName().empty())?fObj.getIndexName():messageIndex.getIndexName();
101  SphinxFunctionalObject fObject(fObj.getNodeName(), fObj.getHomeDir(), targetIndexName,
104 
105  fObj.log(LoggerStream::Priority::PRIO_INFORMATION) << message(message_const::INDEX_NAME, targetIndexName) << flush;
106  fObject.setLogStream(fObj.log(LoggerStream::Priority::PRIO_INFORMATION));
107 
108  IndexCheck indexCheck(fObject);
109  indexCheck.setIndexName(targetIndexName);
110  if (!indexCheck.execute())
111  throw Poco::Exception(indexCheck.getErrorMsg(), ERROR_INDEXATION_REQUEST);
112 
113  IndexAppendDataFile indexAppendDataFile(fObject);
114  indexAppendDataFile.setIndexName(targetIndexName);
116  indexAppendDataFile.setDataContent(messageIndex.getDocumentFile());
117  if (!indexAppendDataFile.execute())
118  throw Poco::Exception(indexAppendDataFile.getErrorMsg(), ERROR_INDEXATION_REQUEST);
119 
120  IndexPackDocData indexPackDocData(fObject);
121  indexPackDocData.setIndexName(targetIndexName);
123  if (!indexPackDocData.execute())
124  throw Poco::Exception(indexPackDocData.getErrorMsg(), ERROR_INDEXATION_REQUEST);
125 
126  IndexRebuild indexRebuild(fObject);
127  indexRebuild.setIndexName(targetIndexName);
128  indexRebuild.addBranch("*");
129  if (!indexRebuild.execute())
130  throw Poco::Exception(indexRebuild.getErrorMsg(), ERROR_INDEXATION_REQUEST);
131 
132  IndexMerge indexMerge(fObject);
133  indexMerge.setIndexName(targetIndexName);
134  indexMerge.addBranch("*");
135  if (!indexMerge.execute())
136  throw Poco::Exception(indexMerge.getErrorMsg(), ERROR_INDEXATION_REQUEST);
137 
138  fObj.log(LoggerStream::Priority::PRIO_INFORMATION) << message(message_const::MAKE_INDEX_COMMAND_SUCCESS) << flush;
139 }
140 //-----------------------------------------------------------------------------
141 void SphinxIndexer::makeAdminCommand(const std::string& json, std::string& resultData) throw (Poco::Exception)
142 {
143  _isError = false;
144  errorMsg.clear();
146 
147  fObj.log(LoggerStream::Priority::PRIO_INFORMATION) << message(message_const::TRY_MAKE_ADMIN_COMMAND) << flush;
148  SphinxInputJsonMessageManage messageManage(json);
149  if (messageManage.isError())
150  throw Poco::Exception(messageManage.getErrorMsg(), COMMAND_ERROR);
151 
152  if (messageManage.getCommandString().empty())
153  throw Poco::Exception(message(message_const::RECEIVED_EMPTY_COMMAND_STRING), COMMAND_ERROR);
154 
155  SphinxAdminCommandFactory commandFactory;
156  Poco::SharedPtr<SphinxAdminCommand> pCmd = commandFactory.create(fObj, messageManage.getCommandString());
157  if (pCmd.isNull())
158  throw Poco::Exception(message(message_const::UNKNOWN_COMMAND, messageManage.getCommandString()), COMMAND_ERROR);
159 
160  std::string localJson = messageManage.getCommandOptionsString();
161  if (!pCmd->unserialize(localJson))
162  throw Poco::Exception(pCmd->getErrorMsg(), pCmd->getErrorCode());
163 
164  bool res = pCmd->execute();
165  resultData = pCmd->getResultData();
166  if (!res)
167  throw Poco::Exception(pCmd->getErrorMsg(), pCmd->getErrorCode());
168 
169  fObj.log(LoggerStream::Priority::PRIO_INFORMATION) << message(message_const::MAKE_ADMIN_COMMAND_SUCCESS) << flush;
170 }
171 //-----------------------------------------------------------------------------
173 {
174  _isError = false;
175  errorMsg.clear();
177  std::string resultData;
178  timeval tvStart, tvStop;
179  gettimeofday(&tvStart, 0); //gettimeofday does not support TZ adjust on Linux.
180 
181  try
182  {
183  if (inputJsonMessage.getType() == SphinxInputJsonMessage::MessageType::mtIndex)
184  {
185  makeIndexCommand(inputJsonMessage.getData());
186  }
187  else if (inputJsonMessage.getType() == SphinxInputJsonMessage::MessageType::mtManage)
188  {
189  makeAdminCommand(inputJsonMessage.getData(), resultData);
190  }
191  else
193  }
194  catch(Poco::Exception& e)
195  {
196  _isError = true;
197  errorCode = e.code();
198  errorMsg = e.displayText();
199  fObj.log(LoggerStream::Priority::PRIO_INFORMATION) << message(message_const::ERROR, errorMsg) << flush;
200  }
201 
202  gettimeofday(&tvStop, 0);
203 
204  SphinxOutputJsonMessage outputMessage;
205  outputMessage.setErrorCode(errorCode);
206  outputMessage.setErrorMessage(errorMsg);
207  outputMessage.setData(resultData);
208  outputMessage.setTime(getTimeInterval(tvStart, tvStop)/1000);
209 
210  return outputMessage;
211 }
212 //-----------------------------------------------------------------------------
213 //-----------------------------------------------------------------------------
214 } // end namespace sphinx
215 } // end namespace HCE
216 
217 
218