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