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