hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ORMFunctionalObject.cpp
Go to the documentation of this file.
1 
15 
16 
17 
18 #include "ORMFunctionalObject.hpp"
19 #include "HCETimer.hpp"
20 #include "EncodeDecodeBase64.hpp"
21 
25 #include <Poco/SharedPtr.h>
26 #include <Poco/NumberFormatter.h>
27 #include <Poco/NumberParser.h>
28 
29 
30 namespace HCE
31 {
32 
38  _errorCode(int()),_errorMsg(),_status(),command(),_dbDriver(0),_dbConfig(dbConfig),_factory() {
39  this->init();
40  if(this->_status == ACTION_GENERAL_FAILURE) {
41  std::cerr << "ERROR: create ORMFunctionalObject failure" << std::endl;
42  }
43 }
44 
46 }
47 
48 void ORMFunctionalObject::init() {
49  this->_status = ACTION_SUCCESS;
50  #ifdef _USE_VOLDEMORT_DB_
51  if (!_factory.isClass(this->_dbConfig.getDriverName())) _factory.registerClass<HCE::VoldemortDB>(this->_dbConfig.getDriverName());
52  #elif _USE_LEVEL_DB_
53  if (!_factory.isClass(this->_dbConfig.getDriverName())) _factory.registerClass<HCE::LevelDB>(this->_dbConfig.getDriverName());
54  #elif _USE_AEROSPIKE_DB_
55  if (!_factory.isClass(this->_dbConfig.getDriverName())) _factory.registerClass<HCE::AerospikeDB>(this->_dbConfig.getDriverName());
56  #endif
57  this->createDriverByName();
58  this->setDriverUrl();
59  this->setDatabaseName();
60  this->setWritePolicy();
61  this->setWriteUnique();
62  init_command();
63 }
64 
68 Poco::SharedPtr<Response> ORMFunctionalObject::processObj(const Request& request) {
69  Poco::SharedPtr<Response> response;
70  HCETimer timer;
71  try {
72  response = (request.initialized) ?
73  (this->command.at(request.type))(this,request.data) : Poco::SharedPtr<Response>(new Response(INVALID_MESSAGE_BODY));
74  }
75  catch(std::exception& ex ) {
76  Poco::Message msg ("KVDBManager.cpp",ex.what(),Poco::Message::PRIO_ERROR,__FILE__,__LINE__);
78  response = Poco::SharedPtr<Response>(new Response);
79  }
80  response->setTime(Poco::NumberFormatter::format(timer.elapsed()));
81  return response;
82 }
83 
87 std::string ORMFunctionalObject::processJSON(const std::string& inputMessage) {
88  Request request(inputMessage);
89  return this->processObj(request)->json();
90 }
91 
95 Poco::SharedPtr<Response> ORMFunctionalObject::putData(const std::string& data) {
96  std::map<std::string, std::string> resources;
97  std::map<std::string, std::string> statuses;
98  STATUS status = loadResourcesFromJSON(data, resources);
99  //STATUS status = loadResourceFromJSON<std::map<std::string, std::string> >(data, resources);
100  for(auto resource : resources) {
101  std::string key = resource.first;
102  std::string value = resource.second;
103  put(key, value);
104  statuses[resource.first] = Poco::NumberFormatter::format(getLastError());
105  status |= getLastError();
106  }
107  Poco::SharedPtr<Response> response(new Response(statuses,status));
108  return response;
109 }
110 
114 Poco::SharedPtr<Response> ORMFunctionalObject::getData(const std::string& data) {
115  std::vector<std::string> document_ids;
116  std::map<std::string, std::string> documents;
117  STATUS status = loadDocIdsFromJSON(data, document_ids);
118  for(auto document_id : document_ids) {
119  std::string s = get(document_id);
120  documents[document_id] = s;
121  status |= getLastError();
122  }
123  Poco::SharedPtr<Response> response(new Response(documents,status));
124  return response;
125 }
126 
130 Poco::SharedPtr<Response> ORMFunctionalObject::delData(const std::string& data) {
131  std::vector<std::string> document_ids;
132  std::map<std::string, std::string> statuses;
133  STATUS status = loadDocIdsFromJSON(data, document_ids);
134  for(auto document_id : document_ids) {
135  del(document_id);
136  statuses[document_id] = Poco::NumberFormatter::format(getLastError());
140  //status |= getLastError();
141  status |= ACTION_SUCCESS;
142  }
143  Poco::SharedPtr<Response> response(new Response(statuses,status));
144  return response;
145 }
146 
147 
151 Poco::SharedPtr<Response> ORMFunctionalObject::checkData(const std::string& data) {
152  std::vector<std::string> document_ids;
153  std::map<std::string, std::string> statuses;
154  STATUS status = loadDocIdsFromJSON(data, document_ids);
155  for(auto document_id : document_ids) {
156  get(document_id);
157  statuses[document_id] = Poco::NumberFormatter::format(getLastError());
161  //status |= getLastError();
162  status |= DOCUMENT_PRESENT;
163  }
164  Poco::SharedPtr<Response> response(new Response(statuses,status));
165  return response;
166 }
167 
168 
172 Poco::SharedPtr<Response> ORMFunctionalObject::adminData(const std::string& data) {
173  std::vector<std::string> command_ids;
174  std::map<std::string, std::string> statuses;
175  STATUS status = loadDocIdsFromJSON(data, command_ids);
176  for(auto command_id : command_ids) {
177  try {
178  switch(static_cast<CommandId>(Poco::NumberParser::parse(command_id))) {
179  case CommandId::DB_ENGINE_NAME:
180  statuses[command_id] = getDBEngine();
181  status |= ACTION_SUCCESS;
182  break;
186  default: break;
187  }
188  }
189  catch (Poco::SyntaxException& ex) {
190  status |= INVALID_ADMIN_COMMAND;
191  }
192  }
193  Poco::SharedPtr<Response> response(new Response(statuses,status));
194  return response;
195 }
196 
197 
201 Poco::SharedPtr<Response> ORMFunctionalObject::getAllKeys(const std::string&) {
202  std::vector<std::string> document_ids;
203  std::map<std::string, std::string> doc_ids;
204  document_ids = get_all();
205  for(auto doc_id : document_ids) {
206  doc_ids[doc_id] = Poco::NumberFormatter::format(getLastError());
207  }
208  STATUS status = DOCUMENT_PRESENT;
209  Poco::SharedPtr<Response> response(new Response(doc_ids,status));
210  return response;
211 }
212 
213 
217 STATUS ORMFunctionalObject::loadResourcesFromJSON(const std::string& json, std::map<std::string, std::string>& resources) {
218 #ifdef _DEBUG_
219  Poco::Message msg ("ORMFunctionalObject",json,Poco::Message::PRIO_INFORMATION,__FILE__,__LINE__);
221 #endif
222 
223  STATUS actionStatus = INVALID_MESSAGE_TYPE;
224  try {
225  Poco::JSON::Array::Ptr obj;
226  Poco::JSON::Parser parser;
227  Poco::Dynamic::Var result = parser.parse(json);
228  if (result.type()==typeid(Poco::JSON::Object::Ptr)) {
229  std::string documents = result.extract<Poco::JSON::Object::Ptr>()->getValue<std::string>("documents");
230  parser.reset();
231  Poco::Dynamic::Var docs = parser.parse(documents);
232  if (docs.type()==typeid(Poco::JSON::Object::Ptr)) {
233  std::vector<std::string> keys;
234  Poco::JSON::Object::Ptr pdocs = docs.extract<Poco::JSON::Object::Ptr>();
235  pdocs->getNames(keys);
236  for(auto key:keys) {
237  std::string base64_encode = pdocs->getValue<std::string>(key);
238  std::string value = HCE::decodeBase64(base64_encode);
239  resources[key] = value;
240  }
241  actionStatus = ACTION_SUCCESS;
242  }
243  }
244  }
245  catch(const Poco::JSON::JSONException& jsone) {
246  Poco::Message msg ("ORMFunctionalObject",jsone.displayText(),Poco::Message::PRIO_CRITICAL,__FILE__,__LINE__);
248  actionStatus = ACTION_PARSE_ERROR;
249  }
250  return actionStatus;
251 }
252 
253 
258 STATUS ORMFunctionalObject::loadDocIdsFromJSON(const std::string& json, std::vector<std::string>& resources) {
259 
260 #ifdef _DEBUG_
261  Poco::Message msg ("ORMFunctionalObject",json,Poco::Message::PRIO_INFORMATION,__FILE__,__LINE__);
263 #endif
264 
265  STATUS actionStatus = INVALID_MESSAGE_TYPE;
266  try {
267  Poco::JSON::Array::Ptr obj;
268  Poco::JSON::Parser parser;
269  Poco::Dynamic::Var result = parser.parse(json);
270  if (result.type()==typeid(Poco::JSON::Object::Ptr)) {
271  std::string documents = result.extract<Poco::JSON::Object::Ptr>()->getValue<std::string>("documents");
272  parser.reset();
273  Poco::Dynamic::Var docs = parser.parse(documents);
274  if (docs.type()==typeid(Poco::JSON::Array::Ptr)) {
275  Poco::JSON::Array::Ptr pdocs = docs.extract<Poco::JSON::Array::Ptr>();
276  resources.reserve(pdocs->size());
277  for(unsigned i=0; i<pdocs->size(); ++i) {
278  resources.push_back(pdocs->get(i).extract<std::string>());
279  }
280  actionStatus = ACTION_SUCCESS;
281  }
282  }
283  }
284  catch(Poco::JSON::JSONException& jsone) {
285  Poco::Message msg ("Application",jsone.displayText(),Poco::Message::PRIO_CRITICAL,__FILE__,__LINE__);
287  actionStatus = ACTION_PARSE_ERROR;
288  }
289  return actionStatus;
290 }
291 
295 void ORMFunctionalObject::init_command()
296 {
297  this->_status = ACTION_SUCCESS;
298  try {
299  command.push_back(&HCE::ORMFunctionalObject::putData);
300  command.push_back(&HCE::ORMFunctionalObject::getData);
301  command.push_back(&HCE::ORMFunctionalObject::delData);
302  command.push_back(&HCE::ORMFunctionalObject::checkData);
303  command.push_back(&HCE::ORMFunctionalObject::adminData);
304  command.push_back(&HCE::ORMFunctionalObject::getAllKeys);
305  }
306  catch (const std::exception& ex) {
307  Poco::Message msg_ex ("ORMFunctionalObject",ex.what(),Poco::Message::PRIO_CRITICAL,__FILE__,__LINE__);
308  HCE::Logger::writeLog(msg_ex);
309  this->_status = ACTION_GENERAL_FAILURE;
310  }
311 }
312 
313 }