RegisterQueryManager.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // Created by jarod on 2022/9/13.
  3. //
  4. #include "RegisterQueryManager.h"
  5. #include "servant/QueryPushF.h"
  6. void RegisterQueryManager::registerChange(const vector<string> &ids, const string &name, CurrentPtr current)
  7. {
  8. std::lock_guard<std::mutex> lock(_mutex);
  9. for(auto &id : ids)
  10. {
  11. _uidToChangeIds[current->getUId()].insert(id);
  12. _changes[id][current->getUId()] = current;
  13. }
  14. }
  15. void RegisterQueryManager::registerQuery(const string &id, const string &name, CurrentPtr current)
  16. {
  17. std::lock_guard<std::mutex> lock(_mutex);
  18. _uidToQueryIds[current->getUId()].insert(id);
  19. _queries[id][current->getUId()] = current;
  20. }
  21. void RegisterQueryManager::closeQuery(CurrentPtr current)
  22. {
  23. std::lock_guard<std::mutex> lock(_mutex);
  24. {
  25. auto it = _uidToChangeIds.find(current->getUId());
  26. if (it != _uidToChangeIds.end())
  27. {
  28. for (auto e: it->second)
  29. {
  30. auto idIt = _changes.find(e);
  31. if (idIt != _changes.end())
  32. {
  33. idIt->second.erase(current->getUId());
  34. if (idIt->second.empty())
  35. {
  36. _changes.erase(idIt);
  37. }
  38. }
  39. }
  40. }
  41. }
  42. {
  43. auto it = _uidToQueryIds.find(current->getUId());
  44. if (it != _uidToQueryIds.end())
  45. {
  46. for (auto e: it->second)
  47. {
  48. auto idIt = _queries.find(e);
  49. if (idIt != _queries.end())
  50. {
  51. idIt->second.erase(current->getUId());
  52. if (idIt->second.empty())
  53. {
  54. _queries.erase(idIt);
  55. }
  56. }
  57. }
  58. }
  59. }
  60. }
  61. void RegisterQueryManager::pushAll()
  62. {
  63. std::lock_guard<std::mutex> lock(_mutex);
  64. LOG_CONSOLE_DEBUG << _queries.size() << endl;
  65. for(auto e : _queries)
  66. {
  67. LOG_CONSOLE_DEBUG << e.first << ", " << e.second.size() << endl;
  68. for(auto it : e.second)
  69. {
  70. QueryPushF::async_response_push_onQuery(it.second, e.first);
  71. }
  72. }
  73. }
  74. unordered_map<int, CurrentPtr> RegisterQueryManager::getChanges(const string &id)
  75. {
  76. std::lock_guard<std::mutex> lock(_mutex);
  77. auto it = _changes.find(id);
  78. if(it != _changes.end())
  79. {
  80. return it->second;
  81. }
  82. return unordered_map<int, CurrentPtr>();
  83. }
  84. unordered_map<int, CurrentPtr> RegisterQueryManager::getQueries(const string &id)
  85. {
  86. std::lock_guard<std::mutex> lock(_mutex);
  87. auto it = _queries.find(id);
  88. if(it != _queries.end())
  89. {
  90. return it->second;
  91. }
  92. return unordered_map<int, CurrentPtr>();
  93. }