TeBkRestController.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * TeBkRestController.cpp
  3. *
  4. * Created on: 11-Mar-2015
  5. * Author: sumeetc
  6. */
  7. #include "TeBkRestController.h"
  8. TeBkRestController::TeBkRestController() {
  9. }
  10. TeBkRestController::~TeBkRestController() {
  11. }
  12. TeBkMessage TeBkRestController::json() {
  13. TeBkMessage msg;
  14. msg.setMessage("Hello, World!");
  15. return msg;
  16. }
  17. TeBkWorld TeBkRestController::db() {
  18. DataSourceInterface* sqli = DataSourceManager::getImpl();
  19. int rid = rand() % 10000 + 1;
  20. GenericObject id;
  21. id << rid;
  22. TeBkWorld w = sqli->get<TeBkWorld>(id);
  23. delete sqli;
  24. return w;
  25. }
  26. std::vector<TeBkWorld> TeBkRestController::queries(std::string queries) {
  27. std::vector<TeBkWorld> wlst;
  28. int queryCount = 1;
  29. try {
  30. queryCount = CastUtil::lexical_cast<int>(queries);
  31. } catch(...) {
  32. }
  33. if(queryCount<1)queryCount=1;
  34. else if(queryCount>500)queryCount=500;
  35. DataSourceInterface* sqli = DataSourceManager::getImpl();
  36. std::string tbName = "world";
  37. sqli->startSession(&tbName);
  38. for (int c = 0; c < queryCount; ++c) {
  39. int rid = rand() % 10000 + 1;
  40. GenericObject id;
  41. id << rid;
  42. TeBkWorld w = sqli->get<TeBkWorld>(id);
  43. wlst.push_back(w);
  44. }
  45. sqli->endSession();
  46. delete sqli;
  47. return wlst;
  48. }
  49. std::vector<TeBkWorld> TeBkRestController::updates(std::string queries) {
  50. std::vector<TeBkWorld> wlst;
  51. int queryCount = 1;
  52. try {
  53. queryCount = CastUtil::lexical_cast<int>(queries);
  54. } catch(...) {
  55. }
  56. if(queryCount<1)queryCount=1;
  57. else if(queryCount>500)queryCount=500;
  58. DataSourceInterface* sqli = DataSourceManager::getImpl();
  59. std::string tbName = "world";
  60. sqli->startSession(&tbName);
  61. for (int c = 0; c < queryCount; ++c) {
  62. int rid = rand() % 10000 + 1;
  63. GenericObject id;
  64. id << rid;
  65. TeBkWorld w = sqli->get<TeBkWorld>(id);
  66. int newRandomNumber = rand() % 10000 + 1;
  67. if(w.getRandomNumber() == newRandomNumber) {
  68. newRandomNumber -= 1;
  69. }
  70. w.setRandomNumber(newRandomNumber);
  71. wlst.push_back(w);
  72. }
  73. sqli->startTransaction();
  74. sqli->bulkUpdate<TeBkWorld>(wlst);
  75. sqli->commit();
  76. sqli->endSession();
  77. delete sqli;
  78. return wlst;
  79. }
  80. std::string TeBkRestController::plaintext() {
  81. return "Hello, World!";
  82. }
  83. void TeBkRestController::updateCache() {
  84. CacheInterface* cchi = CacheManager::getImpl();
  85. DataSourceInterface* sqli = DataSourceManager::getImpl();
  86. std::string tbName = "world";
  87. sqli->startSession(&tbName);
  88. for (int c = 1; c <= 10000; ++c) {
  89. GenericObject id;
  90. id << c;
  91. TeBkWorld w = sqli->get<TeBkWorld>(id);
  92. cchi->setO(CastUtil::lexical_cast<std::string>(c), w);
  93. }
  94. sqli->endSession();
  95. delete sqli;
  96. delete cchi;
  97. }
  98. std::vector<TeBkWorld> TeBkRestController::cachedWorlds(std::string count) {
  99. int queryCount = 1;
  100. try {
  101. queryCount = CastUtil::lexical_cast<int>(count);
  102. } catch(...) {
  103. }
  104. if(queryCount<1)queryCount=1;
  105. else if(queryCount>500)queryCount=500;
  106. CacheInterface* cchi = CacheManager::getImpl();
  107. std::vector<std::string> keys;
  108. for (int c = 0; c < queryCount; ++c) {
  109. int rid = rand() % 10000 + 1;
  110. keys.push_back(CastUtil::lexical_cast<std::string>(rid));
  111. }
  112. std::vector<TeBkWorld> wlst = cchi->mgetO<TeBkWorld>(keys);
  113. delete cchi;
  114. return wlst;
  115. }