config_parser.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "hdfspp/config_parser.h"
  19. #include "common/hdfs_configuration.h"
  20. #include "common/configuration_loader.h"
  21. #include <string>
  22. #include <memory>
  23. #include <vector>
  24. #include <numeric>
  25. namespace hdfs {
  26. static const char kSearchPathSeparator = ':';
  27. HdfsConfiguration LoadDefault(ConfigurationLoader & loader)
  28. {
  29. optional<HdfsConfiguration> result = loader.LoadDefaultResources<HdfsConfiguration>();
  30. if (result)
  31. {
  32. return result.value();
  33. }
  34. else
  35. {
  36. return loader.NewConfig<HdfsConfiguration>();
  37. }
  38. }
  39. class ConfigParser::impl {
  40. public:
  41. impl() :
  42. config_(loader_.NewConfig<HdfsConfiguration>()) {
  43. }
  44. impl(const std::vector<std::string>& dirs) :
  45. config_(loader_.NewConfig<HdfsConfiguration>()) {
  46. // Convert vector of paths into ':' separated path
  47. std::string path = std::accumulate(dirs.begin(), dirs.end(), std::string(""),
  48. [](std::string cumm, std::string elem) {return cumm + kSearchPathSeparator + elem;});
  49. loader_.SetSearchPath(path);
  50. config_ = LoadDefault(loader_);
  51. }
  52. impl(const std::string& path) :
  53. config_(loader_.NewConfig<HdfsConfiguration>()) {
  54. loader_.SetSearchPath(path);
  55. config_ = LoadDefault(loader_);
  56. }
  57. bool LoadDefaultResources() {
  58. config_ = LoadDefault(loader_);
  59. return true;
  60. }
  61. std::vector<std::pair<std::string, Status> > ValidateResources() const {
  62. return loader_.ValidateDefaultResources<HdfsConfiguration>();
  63. }
  64. bool get_int(const std::string& key, int& outval) const {
  65. auto ret = config_.GetInt(key);
  66. if (!ret) {
  67. return false;
  68. } else {
  69. outval = *ret;
  70. return true;
  71. }
  72. }
  73. bool get_string(const std::string& key, std::string& outval) const {
  74. auto ret = config_.Get(key);
  75. if (!ret) {
  76. return false;
  77. } else {
  78. outval = *ret;
  79. return true;
  80. }
  81. }
  82. bool get_bool(const std::string& key, bool& outval) const {
  83. auto ret = config_.GetBool(key);
  84. if (!ret) {
  85. return false;
  86. } else {
  87. outval = *ret;
  88. return true;
  89. }
  90. }
  91. bool get_double(const std::string& key, double& outval) const {
  92. auto ret = config_.GetDouble(key);
  93. if (!ret) {
  94. return false;
  95. } else {
  96. outval = *ret;
  97. return true;
  98. }
  99. }
  100. bool get_uri(const std::string& key, URI& outval) const {
  101. auto ret = config_.GetUri(key);
  102. if (!ret) {
  103. return false;
  104. } else {
  105. outval = *ret;
  106. return true;
  107. }
  108. }
  109. bool get_options(Options& outval) {
  110. outval = config_.GetOptions();
  111. return true;
  112. }
  113. private:
  114. ConfigurationLoader loader_;
  115. HdfsConfiguration config_;
  116. };
  117. ConfigParser::ConfigParser() {
  118. pImpl.reset(new ConfigParser::impl());
  119. }
  120. ConfigParser::ConfigParser(const std::vector<std::string>& configDirectories) {
  121. pImpl.reset(new ConfigParser::impl(configDirectories));
  122. }
  123. ConfigParser::ConfigParser(const std::string& path) {
  124. pImpl.reset(new ConfigParser::impl(path));
  125. }
  126. ConfigParser::~ConfigParser() = default;
  127. ConfigParser::ConfigParser(ConfigParser&&) = default;
  128. ConfigParser& ConfigParser::operator=(ConfigParser&&) = default;
  129. bool ConfigParser::LoadDefaultResources() { return pImpl->LoadDefaultResources(); }
  130. std::vector<std::pair<std::string, Status> > ConfigParser::ValidateResources() const { return pImpl->ValidateResources();}
  131. bool ConfigParser::get_int(const std::string& key, int& outval) const { return pImpl->get_int(key, outval); }
  132. int ConfigParser::get_int_or(const std::string& key, const int defaultval) const {
  133. int res = 0;
  134. if(get_int(key, res)) {
  135. return res;
  136. } else {
  137. return defaultval;
  138. }
  139. }
  140. bool ConfigParser::get_string(const std::string& key, std::string& outval) const { return pImpl->get_string(key, outval); }
  141. std::string ConfigParser::get_string_or(const std::string& key, const std::string& defaultval) const {
  142. std::string res;
  143. if(get_string(key, res)) {
  144. return res;
  145. } else {
  146. return defaultval;
  147. }
  148. }
  149. bool ConfigParser::get_bool(const std::string& key, bool& outval) const { return pImpl->get_bool(key, outval); }
  150. bool ConfigParser::get_bool_or(const std::string& key, const bool defaultval) const {
  151. bool res = false;
  152. if(get_bool(key, res)) {
  153. return res;
  154. } else {
  155. return defaultval;
  156. }
  157. }
  158. bool ConfigParser::get_double(const std::string& key, double& outval) const { return pImpl->get_double(key, outval); }
  159. double ConfigParser::get_double_or(const std::string& key, const double defaultval) const {
  160. double res = 0;
  161. if(get_double(key, res)) {
  162. return res;
  163. } else {
  164. return defaultval;
  165. }
  166. }
  167. bool ConfigParser::get_uri(const std::string& key, URI& outval) const { return pImpl->get_uri(key, outval); }
  168. URI ConfigParser::get_uri_or(const std::string& key, const URI& defaultval) const {
  169. URI res;
  170. if(get_uri(key, res)) {
  171. return res;
  172. } else {
  173. res = defaultval;
  174. return res;
  175. }
  176. }
  177. bool ConfigParser::get_options(Options& outval) const { return pImpl->get_options(outval); }
  178. Options ConfigParser::get_options_or(const Options& defaultval) const {
  179. Options res;
  180. if(get_options(res)) {
  181. return res;
  182. } else {
  183. res = defaultval;
  184. return res;
  185. }
  186. }
  187. } // end namespace hdfs