hbase-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zg...@apache.org
Subject [hbase] 59/133: HBASE-17503 [C++] Configuration should be settable and used w/o XML files
Date Tue, 12 Mar 2019 12:45:47 GMT
This is an automated email from the ASF dual-hosted git repository.

zghao pushed a commit to branch HBASE-14850
in repository https://gitbox.apache.org/repos/asf/hbase.git

commit bb816bff9f9fe12606cf3048c5727eb5934e719e
Author: Enis Soztutar <enis@apache.org>
AuthorDate: Tue Jan 24 15:38:43 2017 -0800

    HBASE-17503 [C++] Configuration should be settable and used w/o XML files
---
 hbase-native-client/core/BUCK                  |  5 ++
 hbase-native-client/core/configuration-test.cc | 63 ++++++++++++++++++++++++++
 hbase-native-client/core/configuration.cc      | 22 +++++++++
 hbase-native-client/core/configuration.h       | 43 ++++++++++++++++++
 4 files changed, 133 insertions(+)

diff --git a/hbase-native-client/core/BUCK b/hbase-native-client/core/BUCK
index 0d1bc93..f1880a4 100644
--- a/hbase-native-client/core/BUCK
+++ b/hbase-native-client/core/BUCK
@@ -86,6 +86,11 @@ cxx_test(
     deps=[":core",],
     run_test_separately=True,)
 cxx_test(
+    name="configuration-test",
+    srcs=["configuration-test.cc",],
+    deps=[":core",],
+    run_test_separately=True,)
+cxx_test(
     name="hbase_configuration-test",
     srcs=["hbase_configuration-test.cc",],
     deps=[":core",],
diff --git a/hbase-native-client/core/configuration-test.cc b/hbase-native-client/core/configuration-test.cc
new file mode 100644
index 0000000..192ed46
--- /dev/null
+++ b/hbase-native-client/core/configuration-test.cc
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include "core/configuration.h"
+#include <gtest/gtest.h>
+
+using hbase::Configuration;
+
+TEST(Configuration, SetGet) {
+  Configuration conf;
+
+  EXPECT_EQ(conf.Get("foo", "default"), "default");
+  conf.Set("foo", "bar");
+  EXPECT_EQ(conf.Get("foo", "default"), "bar");
+}
+
+TEST(Configuration, SetGetInt) {
+  Configuration conf;
+
+  EXPECT_EQ(conf.GetInt("foo", 0), 0);
+  conf.SetInt("foo", 42);
+  EXPECT_EQ(conf.GetInt("foo", 0), 42);
+}
+
+TEST(Configuration, SetGetLong) {
+  Configuration conf;
+
+  EXPECT_EQ(conf.GetLong("foo", 0), 0);
+  conf.SetLong("foo", 42);
+  EXPECT_EQ(conf.GetLong("foo", 0), 42);
+}
+
+TEST(Configuration, SetGetDouble) {
+  Configuration conf;
+
+  EXPECT_EQ(conf.GetDouble("foo", 0), 0);
+  conf.SetDouble("foo", 42.0);
+  EXPECT_EQ(conf.GetDouble("foo", 0), 42.0);
+}
+
+TEST(Configuration, SetGetBool) {
+  Configuration conf;
+
+  EXPECT_EQ(conf.GetBool("foo", false), false);
+  conf.SetInt("foo", true);
+  EXPECT_EQ(conf.GetInt("foo", false), true);
+}
diff --git a/hbase-native-client/core/configuration.cc b/hbase-native-client/core/configuration.cc
index bd582d8..afca122 100644
--- a/hbase-native-client/core/configuration.cc
+++ b/hbase-native-client/core/configuration.cc
@@ -28,6 +28,8 @@
 
 namespace hbase {
 
+Configuration::Configuration() : hb_property_() {}
+
 Configuration::Configuration(ConfigMap &config_map) : hb_property_(std::move(config_map))
{}
 
 Configuration::~Configuration() {}
@@ -215,4 +217,24 @@ bool Configuration::GetBool(const std::string &key, bool default_value)
const {
   return GetBool(key).value_or(default_value);
 }
 
+void Configuration::Set(const std::string &key, const std::string &value) {
+  hb_property_[key] = value;
+}
+
+void Configuration::SetInt(const std::string &key, int32_t value) {
+  Set(key, boost::lexical_cast<std::string>(value));
+}
+
+void Configuration::SetLong(const std::string &key, int64_t value) {
+  Set(key, boost::lexical_cast<std::string>(value));
+}
+
+void Configuration::SetDouble(const std::string &key, double value) {
+  Set(key, boost::lexical_cast<std::string>(value));
+}
+
+void Configuration::SetBool(const std::string &key, bool value) {
+  Set(key, boost::lexical_cast<std::string>(value));
+}
+
 } /* namespace hbase */
diff --git a/hbase-native-client/core/configuration.h b/hbase-native-client/core/configuration.h
index 69bef75..d70941c 100644
--- a/hbase-native-client/core/configuration.h
+++ b/hbase-native-client/core/configuration.h
@@ -33,6 +33,14 @@ using optional = std::experimental::optional<T>;
 
 class Configuration {
  public:
+  /**
+   * @brief Create Configuration object using an empty map. This
+   * object WILL NOT be initialized from reading XML configuration (hbase-site.xml, etc).
+   * Use HBaseConfigurationLoader to initialize the configuration from
+   * hbase-default.xml and hbase-site.xml files.
+   */
+  Configuration();
+
   ~Configuration();
 
   /**
@@ -79,6 +87,41 @@ class Configuration {
    */
   bool GetBool(const std::string &key, bool default_value) const;
 
+  /**
+   * @brief This method sets the given key to the value.
+   * @param key property name
+   * @param value property value
+   */
+  void Set(const std::string &key, const std::string &value);
+
+  /**
+   * @brief This method sets the given key to the value.
+   * @param key property name
+   * @param value property value
+   */
+  void SetInt(const std::string &key, int32_t value);
+
+  /**
+   * @brief This method sets the given key to the value.
+   * @param key property name
+   * @param value property value
+   */
+  void SetLong(const std::string &key, int64_t value);
+
+  /**
+   * @brief This method sets the given key to the value.
+   * @param key property name
+   * @param value property value
+   */
+  void SetDouble(const std::string &key, double value);
+
+  /**
+   * @brief This method sets the given key to the value.
+   * @param key property name
+   * @param value property value
+   */
+  void SetBool(const std::string &key, bool value);
+
  private:
   friend class HBaseConfigurationLoader;
 


Mime
View raw message