JSONDB.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef ZT_JSONDB_HPP
  19. #define ZT_JSONDB_HPP
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <string>
  24. #include <map>
  25. #include <stdexcept>
  26. #include <vector>
  27. #include <algorithm>
  28. #include "../node/Constants.hpp"
  29. #include "../node/Utils.hpp"
  30. #include "../node/InetAddress.hpp"
  31. #include "../node/Mutex.hpp"
  32. #include "../ext/json/json.hpp"
  33. #include "../osdep/OSUtils.hpp"
  34. #include "../osdep/Http.hpp"
  35. #include "../osdep/Thread.hpp"
  36. namespace ZeroTier {
  37. /**
  38. * Hierarchical JSON store that persists into the filesystem or via HTTP
  39. */
  40. class JSONDB
  41. {
  42. public:
  43. JSONDB(const std::string &basePath);
  44. bool writeRaw(const std::string &n,const std::string &obj);
  45. bool put(const std::string &n,const nlohmann::json &obj);
  46. inline bool put(const std::string &n1,const std::string &n2,const nlohmann::json &obj) { return this->put((n1 + "/" + n2),obj); }
  47. inline bool put(const std::string &n1,const std::string &n2,const std::string &n3,const nlohmann::json &obj) { return this->put((n1 + "/" + n2 + "/" + n3),obj); }
  48. inline bool put(const std::string &n1,const std::string &n2,const std::string &n3,const std::string &n4,const nlohmann::json &obj) { return this->put((n1 + "/" + n2 + "/" + n3 + "/" + n4),obj); }
  49. inline bool put(const std::string &n1,const std::string &n2,const std::string &n3,const std::string &n4,const std::string &n5,const nlohmann::json &obj) { return this->put((n1 + "/" + n2 + "/" + n3 + "/" + n4 + "/" + n5),obj); }
  50. nlohmann::json get(const std::string &n);
  51. inline nlohmann::json get(const std::string &n1,const std::string &n2) { return this->get((n1 + "/" + n2)); }
  52. inline nlohmann::json get(const std::string &n1,const std::string &n2,const std::string &n3) { return this->get((n1 + "/" + n2 + "/" + n3)); }
  53. inline nlohmann::json get(const std::string &n1,const std::string &n2,const std::string &n3,const std::string &n4) { return this->get((n1 + "/" + n2 + "/" + n3 + "/" + n4)); }
  54. inline nlohmann::json get(const std::string &n1,const std::string &n2,const std::string &n3,const std::string &n4,const std::string &n5) { return this->get((n1 + "/" + n2 + "/" + n3 + "/" + n4 + "/" + n5)); }
  55. void erase(const std::string &n);
  56. inline void erase(const std::string &n1,const std::string &n2) { this->erase(n1 + "/" + n2); }
  57. inline void erase(const std::string &n1,const std::string &n2,const std::string &n3) { this->erase(n1 + "/" + n2 + "/" + n3); }
  58. inline void erase(const std::string &n1,const std::string &n2,const std::string &n3,const std::string &n4) { this->erase(n1 + "/" + n2 + "/" + n3 + "/" + n4); }
  59. inline void erase(const std::string &n1,const std::string &n2,const std::string &n3,const std::string &n4,const std::string &n5) { this->erase(n1 + "/" + n2 + "/" + n3 + "/" + n4 + "/" + n5); }
  60. template<typename F>
  61. inline void filter(const std::string &prefix,F func)
  62. {
  63. while (!_ready) {
  64. Thread::sleep(250);
  65. _reload(_basePath,std::string());
  66. }
  67. {
  68. Mutex::Lock _l(_db_m);
  69. for(std::map<std::string,_E>::iterator i(_db.lower_bound(prefix));i!=_db.end();) {
  70. if ((i->first.length() >= prefix.length())&&(!memcmp(i->first.data(),prefix.data(),prefix.length()))) {
  71. if (!func(i->first,i->second.obj)) {
  72. this->_erase(i->first);
  73. _db.erase(i++);
  74. } else {
  75. ++i;
  76. }
  77. } else break;
  78. }
  79. }
  80. }
  81. private:
  82. void _erase(const std::string &n);
  83. void _reload(const std::string &p,const std::string &b);
  84. std::string _genPath(const std::string &n,bool create);
  85. struct _E
  86. {
  87. nlohmann::json obj;
  88. inline bool operator==(const _E &e) const { return (obj == e.obj); }
  89. inline bool operator!=(const _E &e) const { return (obj != e.obj); }
  90. };
  91. InetAddress _httpAddr;
  92. std::string _basePath;
  93. std::map<std::string,_E> _db;
  94. Mutex _db_m;
  95. volatile bool _ready;
  96. };
  97. } // namespace ZeroTier
  98. #endif