JSONDB.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 <string>
  21. #include <map>
  22. #include <stdexcept>
  23. #include <vector>
  24. #include <algorithm>
  25. #include "../node/Constants.hpp"
  26. #include "../node/Utils.hpp"
  27. #include "../ext/json/json.hpp"
  28. #include "../osdep/OSUtils.hpp"
  29. namespace ZeroTier {
  30. /**
  31. * Hierarchical JSON store that persists into the filesystem
  32. */
  33. class JSONDB
  34. {
  35. public:
  36. JSONDB(const std::string &basePath) :
  37. _basePath(basePath)
  38. {
  39. _reload(_basePath);
  40. }
  41. inline void reload()
  42. {
  43. _db.clear();
  44. _reload(_basePath);
  45. }
  46. bool put(const std::string &n,const nlohmann::json &obj);
  47. inline bool put(const std::string &n1,const std::string &n2,const nlohmann::json &obj) { return this->put((n1 + "/" + n2),obj); }
  48. 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); }
  49. 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); }
  50. 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); }
  51. const nlohmann::json &get(const std::string &n,unsigned long maxSinceCheck = 0);
  52. inline const nlohmann::json &get(const std::string &n1,const std::string &n2,unsigned long maxSinceCheck = 0) { return this->get((n1 + "/" + n2),maxSinceCheck); }
  53. inline const nlohmann::json &get(const std::string &n1,const std::string &n2,const std::string &n3,unsigned long maxSinceCheck = 0) { return this->get((n1 + "/" + n2 + "/" + n3),maxSinceCheck); }
  54. inline const nlohmann::json &get(const std::string &n1,const std::string &n2,const std::string &n3,const std::string &n4,unsigned long maxSinceCheck = 0) { return this->get((n1 + "/" + n2 + "/" + n3 + "/" + n4),maxSinceCheck); }
  55. inline const nlohmann::json &get(const std::string &n1,const std::string &n2,const std::string &n3,const std::string &n4,const std::string &n5,unsigned long maxSinceCheck = 0) { return this->get((n1 + "/" + n2 + "/" + n3 + "/" + n4 + "/" + n5),maxSinceCheck); }
  56. void erase(const std::string &n);
  57. inline void erase(const std::string &n1,const std::string &n2) { this->erase(n1 + "/" + n2); }
  58. inline void erase(const std::string &n1,const std::string &n2,const std::string &n3) { this->erase(n1 + "/" + n2 + "/" + n3); }
  59. 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); }
  60. 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); }
  61. template<typename F>
  62. inline void filter(const std::string &prefix,unsigned long maxSinceCheck,F func)
  63. {
  64. for(std::map<std::string,_E>::iterator i(_db.lower_bound(prefix));i!=_db.end();) {
  65. if (i->first.substr(0,prefix.length()) == prefix) {
  66. if (!func(i->first,get(i->second.obj,maxSinceCheck))) {
  67. std::map<std::string,_E>::iterator i2(i); ++i2;
  68. this->erase(i->first);
  69. i = i2;
  70. } else ++i;
  71. } else break;
  72. }
  73. }
  74. private:
  75. void _reload(const std::string &p);
  76. bool _isValidObjectName(const std::string &n);
  77. std::string _genPath(const std::string &n,bool create);
  78. struct _E
  79. {
  80. nlohmann::json obj;
  81. uint64_t lastModifiedOnDisk;
  82. uint64_t lastCheck;
  83. };
  84. std::string _basePath;
  85. std::map<std::string,_E> _db;
  86. };
  87. } // namespace ZeroTier
  88. #endif