25 #include <Poco/SharedPtr.h>
26 #include <Poco/NumberFormatter.h>
27 #include <Poco/NumberParser.h>
38 _errorCode(int()),_errorMsg(),_status(),
command(),_dbDriver(0),_dbConfig(dbConfig),_factory() {
41 std::cerr <<
"ERROR: create ORMFunctionalObject failure" <<
std::endl;
48 void ORMFunctionalObject::init() {
50 #ifdef _USE_VOLDEMORT_DB_
53 if (!_factory.isClass(this->_dbConfig.getDriverName())) _factory.registerClass<
HCE::LevelDB>(this->_dbConfig.
getDriverName());
54 #elif _USE_AEROSPIKE_DB_
57 this->createDriverByName();
59 this->setDatabaseName();
67 Poco::SharedPtr<Response> response;
73 catch(std::exception& ex ) {
74 Poco::Message msg (
"KVDBManager.cpp",ex.what(),Poco::Message::PRIO_ERROR,__FILE__,__LINE__);
76 response = Poco::SharedPtr<Response>(
new Response);
78 response->setTime(Poco::NumberFormatter::format(timer.
elapsed()));
93 Poco::SharedPtr<Response> ORMFunctionalObject::putData(
const std::string&
data) {
94 std::map<std::string, std::string> resources;
95 std::map<std::string, std::string> statuses;
96 STATUS status = loadResourcesFromJSON(data, resources);
98 for(
auto resource : resources) {
99 std::string key = resource.first;
100 std::string value = resource.second;
102 statuses[resource.first] = Poco::NumberFormatter::format(getLastError());
103 status |= getLastError();
105 Poco::SharedPtr<Response> response(
new Response(statuses,status));
112 Poco::SharedPtr<Response> ORMFunctionalObject::getData(
const std::string& data) {
113 std::vector<std::string> document_ids;
114 std::map<std::string, std::string>
documents;
115 STATUS status = loadDocIdsFromJSON(data, document_ids);
116 for(
auto document_id : document_ids) {
117 std::string s =
get(document_id);
118 documents[document_id] = s;
119 status |= getLastError();
121 Poco::SharedPtr<Response> response(
new Response(documents,status));
128 Poco::SharedPtr<Response> ORMFunctionalObject::delData(
const std::string& data) {
129 std::vector<std::string> document_ids;
130 std::map<std::string, std::string> statuses;
131 STATUS status = loadDocIdsFromJSON(data, document_ids);
132 for(
auto document_id : document_ids) {
134 statuses[document_id] = Poco::NumberFormatter::format(getLastError());
141 Poco::SharedPtr<Response> response(
new Response(statuses,status));
149 Poco::SharedPtr<Response> ORMFunctionalObject::checkData(
const std::string& data) {
150 std::vector<std::string> document_ids;
151 std::map<std::string, std::string> statuses;
152 STATUS status = loadDocIdsFromJSON(data, document_ids);
153 for(
auto document_id : document_ids) {
155 statuses[document_id] = Poco::NumberFormatter::format(getLastError());
162 Poco::SharedPtr<Response> response(
new Response(statuses,status));
170 Poco::SharedPtr<Response> ORMFunctionalObject::adminData(
const std::string& data) {
171 std::vector<std::string> command_ids;
172 std::map<std::string, std::string> statuses;
173 STATUS status = loadDocIdsFromJSON(data, command_ids);
174 for(
auto command_id : command_ids) {
176 switch(static_cast<CommandId>(Poco::NumberParser::parse(command_id))) {
177 case CommandId::DB_ENGINE_NAME:
178 statuses[command_id] = getDBEngine();
187 catch (Poco::SyntaxException& ex) {
191 Poco::SharedPtr<Response> response(
new Response(statuses,status));
198 STATUS ORMFunctionalObject::loadResourcesFromJSON(
const std::string& json, std::map<std::string, std::string>& resources) {
200 Poco::Message msg (
"ORMFunctionalObject",json,Poco::Message::PRIO_INFORMATION,__FILE__,__LINE__);
206 Poco::JSON::Array::Ptr obj;
207 Poco::JSON::Parser parser;
208 Poco::Dynamic::Var result = parser.parse(json);
209 if (result.type()==
typeid(Poco::JSON::Object::Ptr)) {
210 std::string documents = result.extract<Poco::JSON::Object::Ptr>()->getValue<std::string>(
"documents");
212 Poco::Dynamic::Var
docs = parser.parse(documents);
213 if (docs.type()==
typeid(Poco::JSON::Object::Ptr)) {
214 std::vector<std::string> keys;
215 Poco::JSON::Object::Ptr pdocs = docs.extract<Poco::JSON::Object::Ptr>();
216 pdocs->getNames(keys);
218 std::string base64_encode = pdocs->getValue<std::string>(key);
220 resources[key] = value;
226 catch(
const Poco::JSON::JSONException& jsone) {
227 Poco::Message msg (
"ORMFunctionalObject",jsone.displayText(),Poco::Message::PRIO_CRITICAL,__FILE__,__LINE__);
239 STATUS ORMFunctionalObject::loadDocIdsFromJSON(
const std::string& json, std::vector<std::string>& resources) {
242 Poco::Message msg (
"ORMFunctionalObject",json,Poco::Message::PRIO_INFORMATION,__FILE__,__LINE__);
248 Poco::JSON::Array::Ptr obj;
249 Poco::JSON::Parser parser;
250 Poco::Dynamic::Var result = parser.parse(json);
251 if (result.type()==
typeid(Poco::JSON::Object::Ptr)) {
252 std::string documents = result.extract<Poco::JSON::Object::Ptr>()->getValue<std::string>(
"documents");
254 Poco::Dynamic::Var docs = parser.parse(documents);
255 if (docs.type()==
typeid(Poco::JSON::Array::Ptr)) {
256 Poco::JSON::Array::Ptr pdocs = docs.extract<Poco::JSON::Array::Ptr>();
257 resources.reserve(pdocs->size());
258 for(
unsigned i=0; i<pdocs->size(); ++i) {
259 resources.push_back(pdocs->get(i).extract<std::string>());
265 catch(Poco::JSON::JSONException& jsone) {
266 Poco::Message msg (
"Application",jsone.displayText(),Poco::Message::PRIO_CRITICAL,__FILE__,__LINE__);
276 void ORMFunctionalObject::init_command()
280 command.push_back(&HCE::ORMFunctionalObject::putData);
281 command.push_back(&HCE::ORMFunctionalObject::getData);
282 command.push_back(&HCE::ORMFunctionalObject::delData);
283 command.push_back(&HCE::ORMFunctionalObject::checkData);
284 command.push_back(&HCE::ORMFunctionalObject::adminData);
286 catch (
const std::exception& ex) {
287 Poco::Message msg_ex (
"ORMFunctionalObject",ex.what(),Poco::Message::PRIO_CRITICAL,__FILE__,__LINE__);