diff --git hbase-native-client/core/BUCK hbase-native-client/core/BUCK index 0d1bc93..f1880a4 100644 --- hbase-native-client/core/BUCK +++ 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 hbase-native-client/core/configuration-test.cc hbase-native-client/core/configuration-test.cc new file mode 100644 index 0000000..192ed46 --- /dev/null +++ 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 + +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 hbase-native-client/core/configuration.cc hbase-native-client/core/configuration.cc index bd582d8..7508c5e 100644 --- hbase-native-client/core/configuration.cc +++ 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, const int32_t value) { + Set(key, boost::lexical_cast(value)); +} + +void Configuration::SetLong(const std::string &key, const int64_t value) { + Set(key, boost::lexical_cast(value)); +} + +void Configuration::SetDouble(const std::string &key, const double value) { + Set(key, boost::lexical_cast(value)); +} + +void Configuration::SetBool(const std::string &key, const bool value) { + Set(key, boost::lexical_cast(value)); +} + } /* namespace hbase */ diff --git hbase-native-client/core/configuration.h hbase-native-client/core/configuration.h index 69bef75..51ecb60 100644 --- hbase-native-client/core/configuration.h +++ hbase-native-client/core/configuration.h @@ -33,6 +33,14 @@ using optional = std::experimental::optional; 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, const 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, const 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, const 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, const bool value); + private: friend class HBaseConfigurationLoader;