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();
60 this->setWritePolicy();
61 this->setWriteUnique();
69 Poco::SharedPtr<Response> response;
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);
80 response->setTime(Poco::NumberFormatter::format(timer.
elapsed()));
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);
100 for(
auto resource : resources) {
101 std::string
key = resource.first;
102 std::string value = resource.second;
104 statuses[resource.first] = Poco::NumberFormatter::format(getLastError());
105 status |= getLastError();
107 Poco::SharedPtr<Response> response(
new Response(statuses,status));
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();
123 Poco::SharedPtr<Response> response(
new Response(documents,status));
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) {
136 statuses[document_id] = Poco::NumberFormatter::format(getLastError());
143 Poco::SharedPtr<Response> response(
new Response(statuses,status));
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) {
157 statuses[document_id] = Poco::NumberFormatter::format(getLastError());
164 Poco::SharedPtr<Response> response(
new Response(statuses,status));
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) {
178 switch(static_cast<CommandId>(Poco::NumberParser::parse(command_id))) {
179 case CommandId::DB_ENGINE_NAME:
180 statuses[command_id] = getDBEngine();
189 catch (Poco::SyntaxException& ex) {
193 Poco::SharedPtr<Response> response(
new Response(statuses,status));
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());
209 Poco::SharedPtr<Response> response(
new Response(doc_ids,status));
217 STATUS ORMFunctionalObject::loadResourcesFromJSON(
const std::string& json, std::map<std::string, std::string>& resources) {
219 Poco::Message msg (
"ORMFunctionalObject",json,Poco::Message::PRIO_INFORMATION,__FILE__,__LINE__);
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");
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);
237 std::string base64_encode = pdocs->getValue<std::string>(
key);
239 resources[
key] = value;
245 catch(
const Poco::JSON::JSONException& jsone) {
246 Poco::Message msg (
"ORMFunctionalObject",jsone.displayText(),Poco::Message::PRIO_CRITICAL,__FILE__,__LINE__);
258 STATUS ORMFunctionalObject::loadDocIdsFromJSON(
const std::string& json, std::vector<std::string>& resources) {
261 Poco::Message msg (
"ORMFunctionalObject",json,Poco::Message::PRIO_INFORMATION,__FILE__,__LINE__);
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");
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>());
284 catch(Poco::JSON::JSONException& jsone) {
285 Poco::Message msg (
"Application",jsone.displayText(),Poco::Message::PRIO_CRITICAL,__FILE__,__LINE__);
295 void ORMFunctionalObject::init_command()
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);
306 catch (
const std::exception& ex) {
307 Poco::Message msg_ex (
"ORMFunctionalObject",ex.what(),Poco::Message::PRIO_CRITICAL,__FILE__,__LINE__);