JSONDB.hpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. this->_reloadAll(_basePath);
  40. }
  41. bool put(const std::string &n,const nlohmann::json &obj);
  42. inline bool put(const std::string &n1,const std::string &n2,const nlohmann::json &obj) { return this->put((n1 + "/" + n2),obj); }
  43. 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); }
  44. const nlohmann::json &get(const std::string &n,unsigned long maxSinceCheck = 0);
  45. inline const nlohmann::json &get(const std::string &n1,const std::string &n2,unsigned long maxSinceCheck = 0) { return this->get((n1 + "/" + n2),maxSinceCheck); }
  46. 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); }
  47. 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); }
  48. 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); }
  49. template<typename F>
  50. inline void each(F func,unsigned long maxSinceCheck = 0)
  51. {
  52. const uint64_t now = OSUtils::now();
  53. for(std::map<std::string,_E>::const_iterator i(_db.begin());i!=_db.end();++i) {
  54. if ((now - i->second.lastCheck) > (uint64_t)maxSinceCheck)
  55. this->get(i->first);
  56. func(i->first,i->second.obj);
  57. }
  58. }
  59. private:
  60. bool _isValidObjectName(const std::string &n);
  61. std::string _genPath(const std::string &n,bool create);
  62. void _reloadAll(const std::string &path);
  63. struct _E
  64. {
  65. uint64_t lastModifiedOnDisk;
  66. uint64_t lastCheck;
  67. nlohmann::json obj;
  68. };
  69. std::string _basePath;
  70. std::map<std::string,_E> _db;
  71. };
  72. } // namespace ZeroTier
  73. #endif