http://git-wip-us.apache.org/repos/asf/hbase/blob/24f7f093/hbase-native-client/src/hbase/client/filter-test.cc ---------------------------------------------------------------------- diff --git a/hbase-native-client/src/hbase/client/filter-test.cc b/hbase-native-client/src/hbase/client/filter-test.cc deleted file mode 100644 index 8a23913..0000000 --- a/hbase-native-client/src/hbase/client/filter-test.cc +++ /dev/null @@ -1,141 +0,0 @@ -/* - * 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 <gtest/gtest.h> -#include "hbase/client/client.h" -#include "hbase/client/configuration.h" -#include "hbase/client/get.h" -#include "hbase/client/put.h" -#include "hbase/client/result.h" -#include "hbase/client/table.h" -#include "hbase/if/Comparator.pb.h" -#include "hbase/if/HBase.pb.h" -#include "hbase/serde/table-name.h" -#include "hbase/test-util/test-util.h" - -using hbase::Configuration; -using hbase::Get; -using hbase::Put; -using hbase::FilterFactory; -using hbase::Table; -using hbase::TestUtil; -using hbase::pb::CompareType; -using hbase::ComparatorFactory; -using hbase::Comparator; - -class FilterTest : public ::testing::Test { - protected: - static void SetUpTestCase() { - test_util_ = std::make_unique<TestUtil>(); - test_util_->StartMiniCluster(2); - } - - static void TearDownTestCase() { test_util_.release(); } - - virtual void SetUp() {} - virtual void TearDown() {} - - static std::unique_ptr<TestUtil> test_util_; -}; - -std::unique_ptr<TestUtil> FilterTest::test_util_ = nullptr; - -TEST_F(FilterTest, GetWithColumnPrefixFilter) { - // write row1 with 3 columns (column_1, column_2, and foo_column) - FilterTest::test_util_->CreateTable("t", "d"); - - // Create TableName and Row to be fetched from HBase - auto tn = folly::to<hbase::pb::TableName>("t"); - auto row = "row1"; - - // Gets to be performed on above HBase Table - Get get_all(row); // expected to return all 3 columns - Get get_one(row); // expected to return 1 column - Get get_two(row); // expected to return 2 column - - get_one.SetFilter(FilterFactory::ColumnPrefixFilter("foo_")); - get_two.SetFilter(FilterFactory::ColumnPrefixFilter("column_")); - - // Create a client - hbase::Client client(*(FilterTest::test_util_->conf())); - auto table = client.Table(tn); - - table->Put(Put{"row1"}.AddColumn("d", "column_1", "value1")); - table->Put(Put{"row1"}.AddColumn("d", "column_2", "value2")); - table->Put(Put{"row1"}.AddColumn("d", "foo_column", "value3")); - - // Perform the Get - auto result_all = table->Get(get_all); - auto result_one = table->Get(get_one); - auto result_two = table->Get(get_two); - - table->Close(); - client.Close(); - - // Test the values - ASSERT_TRUE(!result_one->IsEmpty()) << "Result shouldn't be empty."; - ASSERT_TRUE(!result_two->IsEmpty()) << "Result shouldn't be empty."; - ASSERT_TRUE(!result_all->IsEmpty()) << "Result shouldn't be empty."; - EXPECT_EQ(row, result_one->Row()); - EXPECT_EQ(row, result_two->Row()); - EXPECT_EQ(row, result_all->Row()); - EXPECT_EQ(1, result_one->Size()); - EXPECT_EQ(2, result_two->Size()); - EXPECT_EQ(3, result_all->Size()); - EXPECT_EQ("value3", *(result_one->Value("d", "foo_column"))); - EXPECT_EQ("value1", *(result_two->Value("d", "column_1"))); - EXPECT_EQ("value2", *(result_two->Value("d", "column_2"))); -} - -TEST_F(FilterTest, GetWithQualifierFilter) { - // write row1 with 3 columns (a,b,c) - FilterTest::test_util_->CreateTable("t1", "d"); - - // Create TableName and Row to be fetched from HBase - auto tn = folly::to<hbase::pb::TableName>("t1"); - auto row = "row1"; - - // Gets to be performed on above HBase Table - Get get(row); - get.SetFilter(FilterFactory::QualifierFilter(CompareType::GREATER_OR_EQUAL, - *ComparatorFactory::BinaryComparator("b"))); - - // Create a client - hbase::Client client(*(FilterTest::test_util_->conf())); - - // Get connection to HBase Table - auto table = client.Table(tn); - - table->Put(Put{"row1"}.AddColumn("d", "a", "value1")); - table->Put(Put{"row1"}.AddColumn("d", "b", "value2")); - table->Put(Put{"row1"}.AddColumn("d", "c", "value3")); - - // Perform the Get - auto result = table->Get(get); - - table->Close(); - client.Close(); - - // Test the values - ASSERT_TRUE(!result->IsEmpty()) << "Result shouldn't be empty."; - EXPECT_EQ(row, result->Row()); - EXPECT_EQ(2, result->Size()); - EXPECT_EQ("value2", *(result->Value("d", "b"))); - EXPECT_EQ("value3", *(result->Value("d", "c"))); -}
http://git-wip-us.apache.org/repos/asf/hbase/blob/24f7f093/hbase-native-client/src/hbase/client/get-test.cc ---------------------------------------------------------------------- diff --git a/hbase-native-client/src/hbase/client/get-test.cc b/hbase-native-client/src/hbase/client/get-test.cc deleted file mode 100644 index 676d798..0000000 --- a/hbase-native-client/src/hbase/client/get-test.cc +++ /dev/null @@ -1,221 +0,0 @@ -/* - * 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 "hbase/client/get.h" -#include "hbase/client/cell.h" - -#include <glog/logging.h> -#include <gtest/gtest.h> - -using hbase::Cell; -using hbase::Get; - -const int NUMBER_OF_GETS = 5; - -void CheckFamilies(Get &get) { - EXPECT_EQ(false, get.HasFamilies()); - get.AddFamily("family-1"); - EXPECT_EQ(true, get.HasFamilies()); - EXPECT_EQ(1, get.FamilyMap().size()); - for (const auto &family : get.FamilyMap()) { - EXPECT_STREQ("family-1", family.first.c_str()); - EXPECT_EQ(0, family.second.size()); - } - // Not allowed to add the same CF. - get.AddFamily("family-1"); - EXPECT_EQ(1, get.FamilyMap().size()); - get.AddFamily("family-2"); - EXPECT_EQ(2, get.FamilyMap().size()); - get.AddFamily("family-3"); - EXPECT_EQ(3, get.FamilyMap().size()); - int i = 1; - for (const auto &family : get.FamilyMap()) { - std::string family_name = "family-" + std::to_string(i); - EXPECT_STREQ(family_name.c_str(), family.first.c_str()); - EXPECT_EQ(0, family.second.size()); - i += 1; - } - - get.AddColumn("family-1", "column-1"); - get.AddColumn("family-1", "column-2"); - get.AddColumn("family-1", ""); - get.AddColumn("family-1", "column-3"); - get.AddColumn("family-2", "column-X"); - - EXPECT_EQ(3, get.FamilyMap().size()); - auto it = get.FamilyMap().begin(); - EXPECT_STREQ("family-1", it->first.c_str()); - EXPECT_EQ(4, it->second.size()); - EXPECT_STREQ("column-1", it->second[0].c_str()); - EXPECT_STREQ("column-2", it->second[1].c_str()); - EXPECT_STREQ("", it->second[2].c_str()); - EXPECT_STREQ("column-3", it->second[3].c_str()); - ++it; - EXPECT_STREQ("family-2", it->first.c_str()); - EXPECT_EQ(1, it->second.size()); - EXPECT_STREQ("column-X", it->second[0].c_str()); - ++it; - EXPECT_STREQ("family-3", it->first.c_str()); - EXPECT_EQ(0, it->second.size()); - ++it; - EXPECT_EQ(it, get.FamilyMap().end()); -} - -void CheckFamiliesAfterCopy(Get &get) { - EXPECT_EQ(true, get.HasFamilies()); - EXPECT_EQ(3, get.FamilyMap().size()); - int i = 1; - for (const auto &family : get.FamilyMap()) { - std::string family_name = "family-" + std::to_string(i); - EXPECT_STREQ(family_name.c_str(), family.first.c_str()); - i += 1; - } - // Check if the alreaday added CF's and CQ's are as expected - auto it = get.FamilyMap().begin(); - EXPECT_STREQ("family-1", it->first.c_str()); - EXPECT_EQ(4, it->second.size()); - EXPECT_STREQ("column-1", it->second[0].c_str()); - EXPECT_STREQ("column-2", it->second[1].c_str()); - EXPECT_STREQ("", it->second[2].c_str()); - EXPECT_STREQ("column-3", it->second[3].c_str()); - ++it; - EXPECT_STREQ("family-2", it->first.c_str()); - EXPECT_EQ(1, it->second.size()); - EXPECT_STREQ("column-X", it->second[0].c_str()); - ++it; - EXPECT_STREQ("family-3", it->first.c_str()); - EXPECT_EQ(0, it->second.size()); - ++it; - EXPECT_EQ(it, get.FamilyMap().end()); -} - -void GetMethods(Get &get, const std::string &row) { - EXPECT_EQ(row, get.row()); - - CheckFamilies(get); - EXPECT_EQ(true, get.CacheBlocks()); - get.SetCacheBlocks(false); - EXPECT_EQ(false, get.CacheBlocks()); - - EXPECT_EQ(hbase::pb::Consistency::STRONG, get.Consistency()); - get.SetConsistency(hbase::pb::Consistency::TIMELINE); - EXPECT_EQ(hbase::pb::Consistency::TIMELINE, get.Consistency()); - - EXPECT_EQ(1, get.MaxVersions()); - get.SetMaxVersions(2); - EXPECT_EQ(2, get.MaxVersions()); - get.SetMaxVersions(); - EXPECT_EQ(1, get.MaxVersions()); - - // Test initial values - EXPECT_EQ(0, get.Timerange().MinTimeStamp()); - EXPECT_EQ(std::numeric_limits<int64_t>::max(), get.Timerange().MaxTimeStamp()); - - // Set & Test new values using TimeRange and TimeStamp - get.SetTimeRange(1000, 2000); - EXPECT_EQ(1000, get.Timerange().MinTimeStamp()); - EXPECT_EQ(2000, get.Timerange().MaxTimeStamp()); - get.SetTimeStamp(0); - EXPECT_EQ(0, get.Timerange().MinTimeStamp()); - EXPECT_EQ(1, get.Timerange().MaxTimeStamp()); - - // Test some exceptions - ASSERT_THROW(get.SetTimeRange(-1000, 2000), std::runtime_error); - ASSERT_THROW(get.SetTimeRange(1000, -2000), std::runtime_error); - ASSERT_THROW(get.SetTimeRange(1000, 200), std::runtime_error); - ASSERT_THROW(get.SetTimeStamp(std::numeric_limits<int64_t>::max()), std::runtime_error); - - // Test some exceptions - ASSERT_THROW(get.SetMaxVersions(0), std::runtime_error); - ASSERT_THROW(get.SetMaxVersions(std::numeric_limits<uint32_t>::max() + 1), std::runtime_error); -} - -TEST(Get, SingleGet) { - std::string row_str = "row-test"; - Get get(row_str); - GetMethods(get, row_str); - - Get get_tmp(row_str); - Get getcp(get_tmp); - GetMethods(getcp, row_str); - - Get geteq("test"); - geteq = get_tmp; - GetMethods(geteq, row_str); - - // Adding the below tests as there were some concerns raised that the same - // vector of qualifiers in FamilyMap is being shared between copied objects - // Verify the source object's family map size before using it to copy. - EXPECT_EQ(3, get.FamilyMap().size()); - - Get getcp_fam(get); - // address of family maps should be different. - EXPECT_NE(&(get.FamilyMap()), &(getcp_fam.FamilyMap())); - - // Add family to the source object - get.AddColumn("family-4", "column-A"); - get.AddColumn("family-4", "column-B"); - // Verify the source object's family map size - EXPECT_EQ(4, get.FamilyMap().size()); - // Verify the source object's family elements - auto it = get.FamilyMap().begin(); - EXPECT_STREQ("family-1", it->first.c_str()); - EXPECT_EQ(4, it->second.size()); - EXPECT_STREQ("column-1", it->second[0].c_str()); - EXPECT_STREQ("column-2", it->second[1].c_str()); - EXPECT_STREQ("", it->second[2].c_str()); - EXPECT_STREQ("column-3", it->second[3].c_str()); - ++it; - EXPECT_STREQ("family-2", it->first.c_str()); - EXPECT_EQ(1, it->second.size()); - EXPECT_STREQ("column-X", it->second[0].c_str()); - ++it; - EXPECT_STREQ("family-3", it->first.c_str()); - EXPECT_EQ(0, it->second.size()); - ++it; - EXPECT_STREQ("family-4", it->first.c_str()); - EXPECT_EQ(2, it->second.size()); - EXPECT_STREQ("column-A", it->second[0].c_str()); - EXPECT_STREQ("column-B", it->second[1].c_str()); - ++it; - EXPECT_EQ(it, get.FamilyMap().end()); - - // Verifying the copied object's families. It will remain unchanged and below - // tests should pass - CheckFamiliesAfterCopy(getcp_fam); -} - -TEST(Get, MultiGet) { - std::vector<std::unique_ptr<Get>> gets; - for (int i = 0; i < NUMBER_OF_GETS; i++) { - std::string row_str = "row-test"; - row_str += std::to_string(i); - auto get = std::make_unique<Get>(row_str); - - GetMethods(*get, row_str); - gets.push_back(std::move(get)); - } - EXPECT_EQ(NUMBER_OF_GETS, gets.size()); -} - -TEST(Get, Exception) { - std::string row(std::numeric_limits<int16_t>::max() + 1, 'X'); - ASSERT_THROW(Get tmp = Get(row), std::runtime_error); - ASSERT_THROW(Get tmp = Get(""), std::runtime_error); -} http://git-wip-us.apache.org/repos/asf/hbase/blob/24f7f093/hbase-native-client/src/hbase/client/hbase-configuration-test.cc ---------------------------------------------------------------------- diff --git a/hbase-native-client/src/hbase/client/hbase-configuration-test.cc b/hbase-native-client/src/hbase/client/hbase-configuration-test.cc deleted file mode 100644 index afa9c06..0000000 --- a/hbase-native-client/src/hbase/client/hbase-configuration-test.cc +++ /dev/null @@ -1,375 +0,0 @@ -/* - * 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 <fstream> -#include <iostream> - -#include <glog/logging.h> -#include <gtest/gtest.h> -#include <boost/filesystem.hpp> -#include "hbase/client/configuration.h" -#include "hbase/client/hbase-configuration-loader.h" -#include "hbase/utils/optional.h" - -using namespace hbase; - -const std::string kDefHBaseConfPath("./build/test-data/hbase-configuration-test/conf/"); -const std::string kHBaseConfPath("./build/test-data/hbase-configuration-test/custom-conf/"); - -const std::string kHBaseDefaultXml("hbase-default.xml"); -const std::string kHBaseSiteXml("hbase-site.xml"); - -const std::string kHBaseDefaultXmlData( - "<?xml version=\"1.0\"?>\n<?xml-stylesheet type=\"text/xsl\" " - "href=\"configuration.xsl\"?>\n<!--\n/**\n *\n * Licensed to the Apache " - "Software Foundation (ASF) under one\n * or more contributor license " - "agreements. See the NOTICE file\n * distributed with this work for " - "additional information\n * regarding copyright ownership. The ASF " - "licenses this file\n * to you under the Apache License, Version 2.0 " - "(the\n * \"License\"); you may not use this file except in compliance\n * " - "with the License. You may obtain a copy of the License at\n *\n * " - "http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by " - "applicable law or agreed to in writing, software\n * distributed under " - "the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES " - "OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License " - "for the specific language governing permissions and\n * limitations under " - "the License.\n " - "*/\n-->\n<configuration>\n\n<property>\n<name>hbase.rootdir</" - "name>\n<value>/root/hbase-docker/apps/hbase/data</value>\n<final>true</" - "final>\n</" - "property>\n\n<property>\n<name>hbase.zookeeper.property.datadir</" - "name>\n<value>This value will be " - "overwritten</value>\n<final>false</final>\n</" - "property>\n\n<property>\n<name>default-prop</name>\n<value>default-value</" - "value>\n</property>\n\n</configuration>"); -const std::string kHBaseSiteXmlData( - "<?xml version=\"1.0\"?>\n<?xml-stylesheet type=\"text/xsl\" " - "href=\"configuration.xsl\"?>\n<!--\n/**\n *\n * Licensed to the Apache " - "Software Foundation (ASF) under one\n * or more contributor license " - "agreements. See the NOTICE file\n * distributed with this work for " - "additional information\n * regarding copyright ownership. The ASF " - "licenses this file\n * to you under the Apache License, Version 2.0 " - "(the\n * \"License\"); you may not use this file except in compliance\n * " - "with the License. You may obtain a copy of the License at\n *\n * " - "http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by " - "applicable law or agreed to in writing, software\n * distributed under " - "the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES " - "OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License " - "for the specific language governing permissions and\n * limitations under " - "the License.\n " - "*/\n-->\n<configuration>\n\n<property>\n<name>hbase.rootdir</" - "name>\n<value>This value will not be be " - "overwritten</value>\n</" - "property>\n\n<property>\n<name>hbase.zookeeper.property.datadir</" - "name>\n<value>/root/hbase-docker/zookeeper</value>\n</" - "property>\n\n<property>\n<name>hbase-client.user.name</" - "name>\n<value>${user.name}</value>\n</" - "property>\n\n<property>\n<name>hbase-client.user.dir</" - "name>\n<value>${user.dir}</value>\n</" - "property>\n\n<property>\n<name>hbase-client.user.home</" - "name>\n<value>${user.home}</value>\n</" - "property>\n\n<property>\n<name>selfRef</name>\n<value>${selfRef}</" - "value>\n</property>\n\n<property>\n<name>foo.substs</" - "name>\n<value>${bar},${bar},${bar},${bar},${bar},${bar},${bar},${bar},${" - "bar},${bar},</value>\n</" - "property>\n\n<property>\n<name>foo.substs.exception</" - "name>\n<value>${bar},${bar},${bar},${bar},${bar},${bar},${bar},${bar},${" - "bar},${bar},${bar},${bar},${bar},${bar},${bar},${bar},${bar},${bar},${bar}" - ",${bar},${bar}</value>\n</property>\n\n<property>\n<name>bar</" - "name>\n<value>bar-value</value>\n</" - "property>\n\n<property>\n<name>custom-prop</name>\n<value>custom-value</" - "value>\n</property>\n\n<property>\n<name>int</name>\n<value>16000</" - "value>\n</property>\n\n<property>\n<name>int.largevalue</" - "name>\n<value>2147483646</value>\n</" - "property>\n\n<property>\n<name>int.exception</name>\n<value>2147483648</" - "value>\n</property>\n\n<property>\n<name>long</name>\n<value>2147483850</" - "value>\n</property>\n\n<property>\n<name>long.largevalue</" - "name>\n<value>9223372036854775807</value>\n</" - "property>\n\n<property>\n<name>long.exception</" - "name>\n<value>9223372036854775810</value>\n</" - "property>\n\n<property>\n<name>double</name>\n<value>17.9769e+100</" - "value>\n</property>\n\n<property>\n<name>double.largevalue</" - "name>\n<value>170.769e+200</value>\n</" - "property>\n\n<property>\n<name>double.exception</" - "name>\n<value>1.79769e+310</value>\n</" - "property>\n\n<property>\n<name>bool.true</name>\n<value>true</value>\n</" - "property>\n\n<property>\n<name>bool.false</name>\n<value>false</value>\n</" - "property>\n\n<property>\n<name>bool.exception</name>\n<value>unknown " - "bool</value>\n</property>\n\n</configuration>"); - -void WriteDataToFile(const std::string &file, const std::string &xml_data) { - std::ofstream hbase_conf; - hbase_conf.open(file.c_str()); - hbase_conf << xml_data; - hbase_conf.close(); -} - -void CreateHBaseConf(const std::string &dir, const std::string &file, const std::string xml_data) { - // Directory will be created if not present - if (!boost::filesystem::exists(dir)) { - boost::filesystem::create_directories(dir); - } - // Remove temp file always - boost::filesystem::remove((dir + file).c_str()); - WriteDataToFile((dir + file), xml_data); -} - -void CreateHBaseConfWithEnv() { - CreateHBaseConf(kDefHBaseConfPath, kHBaseDefaultXml, kHBaseDefaultXmlData); - CreateHBaseConf(kDefHBaseConfPath, kHBaseSiteXml, kHBaseSiteXmlData); - setenv("HBASE_CONF", kDefHBaseConfPath.c_str(), 1); -} - -/* - * Config will be loaded from $HBASE_CONF. We set it @ kDefHBaseConfPath - * Config values will be loaded from hbase-default.xml and hbase-site.xml - * present in the above path. - */ -TEST(Configuration, LoadConfFromDefaultLocation) { - // Remove already configured env if present. - unsetenv("HBASE_CONF"); - CreateHBaseConf(kDefHBaseConfPath, kHBaseDefaultXml, kHBaseDefaultXmlData); - CreateHBaseConf(kDefHBaseConfPath, kHBaseSiteXml, kHBaseSiteXmlData); - setenv("HBASE_CONF", kDefHBaseConfPath.c_str(), 0); - - HBaseConfigurationLoader loader; - hbase::optional<Configuration> conf = loader.LoadDefaultResources(); - ASSERT_TRUE(conf != none) << "No configuration object present."; - EXPECT_STREQ((*conf).Get("custom-prop", "Set this value").c_str(), "custom-value"); - EXPECT_STREQ((*conf).Get("default-prop", "Set this value").c_str(), "default-value"); -} - -/* - * Config will be loaded from hbase-site.xml defined at - * kHBaseConfPath - */ -TEST(Configuration, LoadConfFromCustomLocation) { - // Remove already configured env if present. - unsetenv("HBASE_CONF"); - CreateHBaseConf(kHBaseConfPath, kHBaseSiteXml, kHBaseSiteXmlData); - - HBaseConfigurationLoader loader; - std::vector<std::string> resources{kHBaseSiteXml}; - hbase::optional<Configuration> conf = loader.LoadResources(kHBaseConfPath, resources); - ASSERT_TRUE(conf != none) << "No configuration object present."; - EXPECT_STREQ((*conf).Get("custom-prop", "").c_str(), "custom-value"); - EXPECT_STRNE((*conf).Get("custom-prop", "").c_str(), "some-value"); -} - -/* - * Config will be loaded from hbase-defualt.xml and hbase-site.xml @ - * kDefHBaseConfPath and kHBaseConfPath respectively. - */ -TEST(Configuration, LoadConfFromMultipleLocatons) { - // Remove already configured env if present. - unsetenv("HBASE_CONF"); - CreateHBaseConf(kDefHBaseConfPath, kHBaseDefaultXml, kHBaseDefaultXmlData); - CreateHBaseConf(kDefHBaseConfPath, kHBaseSiteXml, kHBaseSiteXmlData); - CreateHBaseConf(kHBaseConfPath, kHBaseDefaultXml, kHBaseDefaultXmlData); - CreateHBaseConf(kHBaseConfPath, kHBaseSiteXml, kHBaseSiteXmlData); - - HBaseConfigurationLoader loader; - std::string conf_paths = kDefHBaseConfPath + ":" + kHBaseConfPath; - std::vector<std::string> resources{kHBaseDefaultXml, kHBaseSiteXml}; - hbase::optional<Configuration> conf = loader.LoadResources(conf_paths, resources); - ASSERT_TRUE(conf != none) << "No configuration object present."; - EXPECT_STREQ((*conf).Get("default-prop", "From hbase-default.xml").c_str(), "default-value"); - EXPECT_STREQ((*conf).Get("custom-prop", "").c_str(), "custom-value"); - EXPECT_STRNE((*conf).Get("custom-prop", "").c_str(), "some-value"); -} - -/* - * Config will be loaded from hbase-defualt.xml and hbase-site.xml @ - * $HBASE_CONF. - * We set HBASE_CONF to kDefHBaseConfPath - * Below tests load the conf files in the same way unless specified. - */ -TEST(Configuration, DefaultValues) { - // Remove already configured env if present. - unsetenv("HBASE_CONF"); - CreateHBaseConfWithEnv(); - - HBaseConfigurationLoader loader; - hbase::optional<Configuration> conf = loader.LoadDefaultResources(); - ASSERT_TRUE(conf != none) << "No configuration object present."; - EXPECT_STREQ((*conf).Get("default-prop", "Set this value.").c_str(), "default-value"); - EXPECT_STREQ((*conf).Get("custom-prop", "Set this value.").c_str(), "custom-value"); -} - -TEST(Configuration, FinalValues) { - // Remove already configured env if present. - unsetenv("HBASE_CONF"); - CreateHBaseConfWithEnv(); - - HBaseConfigurationLoader loader; - hbase::optional<Configuration> conf = loader.LoadDefaultResources(); - ASSERT_TRUE(conf != none) << "No configuration object present."; - EXPECT_STREQ((*conf).Get("hbase.rootdir", "").c_str(), "/root/hbase-docker/apps/hbase/data"); - EXPECT_STREQ((*conf).Get("hbase.zookeeper.property.datadir", "").c_str(), - "/root/hbase-docker/zookeeper"); - EXPECT_STRNE((*conf).Get("hbase.rootdir", "").c_str(), "This value will not be be overwritten"); - EXPECT_STRNE((*conf).Get("hbase.zookeeper.property.datadir", "").c_str(), - "This value will be overwritten"); -} - -/* - * Config will be loaded from HBASE_CONF which we set in - * CreateHBaseConfWithEnv(). - * Config values will be loaded from hbase-default.xml and hbase-site.xml in the - * above path. - */ -TEST(Configuration, EnvVars) { - // Remove already configured env if present. - unsetenv("HBASE_CONF"); - CreateHBaseConfWithEnv(); - - HBaseConfigurationLoader loader; - hbase::optional<Configuration> conf = loader.LoadDefaultResources(); - ASSERT_TRUE(conf != none) << "No configuration object present."; - EXPECT_STREQ((*conf).Get("hbase-client.user.name", "").c_str(), "${user.name}"); - EXPECT_STRNE((*conf).Get("hbase-client.user.name", "root").c_str(), "test-user"); -} - -TEST(Configuration, SelfRef) { - // Remove already configured env if present. - unsetenv("HBASE_CONF"); - CreateHBaseConfWithEnv(); - - HBaseConfigurationLoader loader; - hbase::optional<Configuration> conf = loader.LoadDefaultResources(); - ASSERT_TRUE(conf != none) << "No configuration object present."; - EXPECT_STREQ((*conf).Get("selfRef", "${selfRef}").c_str(), "${selfRef}"); -} - -TEST(Configuration, VarExpansion) { - // Remove already configured env if present. - unsetenv("HBASE_CONF"); - CreateHBaseConfWithEnv(); - - HBaseConfigurationLoader loader; - hbase::optional<Configuration> conf = loader.LoadDefaultResources(); - ASSERT_TRUE(conf != none) << "No configuration object present."; - EXPECT_STREQ((*conf).Get("foo.substs", "foo-value").c_str(), - "bar-value,bar-value,bar-value,bar-value,bar-value,bar-value," - "bar-value,bar-value,bar-value,bar-value,"); - EXPECT_STRNE((*conf).Get("foo.substs", "foo-value").c_str(), "bar-value"); -} - -TEST(Configuration, VarExpansionException) { - // Remove already configured env if present. - unsetenv("HBASE_CONF"); - CreateHBaseConfWithEnv(); - - HBaseConfigurationLoader loader; - hbase::optional<Configuration> conf = loader.LoadDefaultResources(); - ASSERT_TRUE(conf != none) << "No configuration object present."; - ASSERT_THROW((*conf).Get("foo.substs.exception", "foo-value").c_str(), std::runtime_error); -} - -TEST(Configuration, GetInt) { - // Remove already configured env if present. - unsetenv("HBASE_CONF"); - CreateHBaseConfWithEnv(); - - HBaseConfigurationLoader loader; - hbase::optional<Configuration> conf = loader.LoadDefaultResources(); - ASSERT_TRUE(conf != none) << "No configuration object present."; - EXPECT_EQ(16000, (*conf).GetInt("int", 0)); - EXPECT_EQ(2147483646, (*conf).GetInt("int.largevalue", 0)); -} - -TEST(Configuration, GetLong) { - // Remove already configured env if present. - unsetenv("HBASE_CONF"); - CreateHBaseConfWithEnv(); - - HBaseConfigurationLoader loader; - hbase::optional<Configuration> conf = loader.LoadDefaultResources(); - ASSERT_TRUE(conf != none) << "No configuration object present."; - EXPECT_EQ(2147483850, (*conf).GetLong("long", 0)); - EXPECT_EQ(9223372036854775807, (*conf).GetLong("long.largevalue", 0)); -} - -TEST(Configuration, GetDouble) { - // Remove already configured env if present. - unsetenv("HBASE_CONF"); - CreateHBaseConfWithEnv(); - - HBaseConfigurationLoader loader; - hbase::optional<Configuration> conf = loader.LoadDefaultResources(); - ASSERT_TRUE(conf != none) << "No configuration object present."; - EXPECT_DOUBLE_EQ(17.9769e+100, (*conf).GetDouble("double", 0.0)); - EXPECT_DOUBLE_EQ(170.769e+200, (*conf).GetDouble("double.largevalue", 0.0)); -} - -TEST(Configuration, GetBool) { - // Remove already configured env if present. - unsetenv("HBASE_CONF"); - CreateHBaseConfWithEnv(); - - HBaseConfigurationLoader loader; - hbase::optional<Configuration> conf = loader.LoadDefaultResources(); - ASSERT_TRUE(conf != none) << "No configuration object present."; - EXPECT_EQ(true, (*conf).GetBool("bool.true", true)); - EXPECT_EQ(false, (*conf).GetBool("bool.false", false)); -} - -TEST(Configuration, GetIntException) { - // Remove already configured env if present. - unsetenv("HBASE_CONF"); - CreateHBaseConfWithEnv(); - - HBaseConfigurationLoader loader; - hbase::optional<Configuration> conf = loader.LoadDefaultResources(); - ASSERT_TRUE(conf != none) << "No configuration object present."; - ASSERT_THROW((*conf).GetInt("int.exception", 0), std::runtime_error); -} - -TEST(Configuration, GetLongException) { - // Remove already configured env if present. - unsetenv("HBASE_CONF"); - CreateHBaseConfWithEnv(); - - HBaseConfigurationLoader loader; - hbase::optional<Configuration> conf = loader.LoadDefaultResources(); - ASSERT_TRUE(conf != none) << "No configuration object present."; - ASSERT_THROW((*conf).GetLong("long.exception", 0), std::runtime_error); -} - -TEST(Configuration, GetDoubleException) { - // Remove already configured env if present. - unsetenv("HBASE_CONF"); - CreateHBaseConfWithEnv(); - - HBaseConfigurationLoader loader; - hbase::optional<Configuration> conf = loader.LoadDefaultResources(); - ASSERT_TRUE(conf != none) << "No configuration object present."; - ASSERT_THROW((*conf).GetDouble("double.exception", 0), std::runtime_error); -} - -TEST(Configuration, GetBoolException) { - // Remove already configured env if present. - unsetenv("HBASE_CONF"); - CreateHBaseConfWithEnv(); - - HBaseConfigurationLoader loader; - hbase::optional<Configuration> conf = loader.LoadDefaultResources(); - ASSERT_TRUE(conf != none) << "No configuration object present."; - ASSERT_THROW((*conf).GetBool("bool.exception", false), std::runtime_error); -} http://git-wip-us.apache.org/repos/asf/hbase/blob/24f7f093/hbase-native-client/src/hbase/client/increment-test.cc ---------------------------------------------------------------------- diff --git a/hbase-native-client/src/hbase/client/increment-test.cc b/hbase-native-client/src/hbase/client/increment-test.cc deleted file mode 100644 index 3c11fbe..0000000 --- a/hbase-native-client/src/hbase/client/increment-test.cc +++ /dev/null @@ -1,129 +0,0 @@ -/* - * 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 <glog/logging.h> -#include <gtest/gtest.h> - -#include "hbase/client/increment.h" -#include "hbase/client/mutation.h" -#include "hbase/client/put.h" -#include "hbase/utils/time-util.h" - -using hbase::Increment; -using hbase::Increment; -using hbase::Cell; -using hbase::CellType; -using hbase::Mutation; -using hbase::TimeUtil; - -const constexpr int64_t Mutation::kLatestTimestamp; - -TEST(Increment, Row) { - Increment incr{"foo"}; - EXPECT_EQ("foo", incr.row()); -} - -TEST(Increment, Durability) { - Increment incr{"row"}; - EXPECT_EQ(hbase::pb::MutationProto_Durability_USE_DEFAULT, incr.Durability()); - - auto skipWal = hbase::pb::MutationProto_Durability_SKIP_WAL; - incr.SetDurability(skipWal); - EXPECT_EQ(skipWal, incr.Durability()); -} - -TEST(Increment, Timestamp) { - Increment incr{"row"}; - - // test default timestamp - EXPECT_EQ(Mutation::kLatestTimestamp, incr.TimeStamp()); - - // set custom timestamp - auto ts = TimeUtil::ToMillis(TimeUtil::GetNowNanos()); - incr.SetTimeStamp(ts); - EXPECT_EQ(ts, incr.TimeStamp()); - - // Add a column with custom timestamp - incr.AddColumn("f", "q", 5l); - auto &cell = incr.FamilyMap().at("f")[0]; - EXPECT_EQ(ts, cell->Timestamp()); -} - -TEST(Increment, HasFamilies) { - Increment incr{"row"}; - - EXPECT_EQ(false, incr.HasFamilies()); - - incr.AddColumn("f", "q", 5l); - EXPECT_EQ(true, incr.HasFamilies()); -} - -TEST(Increment, Add) { - CellType cell_type = CellType::PUT; - std::string row = "row"; - std::string family = "family"; - std::string column = "column"; - std::string value = "value"; - int64_t timestamp = std::numeric_limits<int64_t>::max(); - auto cell = std::make_unique<Cell>(row, family, column, timestamp, value, cell_type); - - // add first cell - Increment incr{"row"}; - incr.Add(std::move(cell)); - EXPECT_EQ(1, incr.FamilyMap().size()); - EXPECT_EQ(1, incr.FamilyMap().at(family).size()); - - // add a non-matching row - auto cell2 = std::make_unique<Cell>(row, family, column, timestamp, value, cell_type); - Increment incr2{"foo"}; - ASSERT_THROW(incr2.Add(std::move(cell2)), std::runtime_error); // rows don't match - - // add a second cell with same family - auto cell3 = std::make_unique<Cell>(row, family, "column-2", timestamp, value, cell_type); - incr.Add(std::move(cell3)); - EXPECT_EQ(1, incr.FamilyMap().size()); - EXPECT_EQ(2, incr.FamilyMap().at(family).size()); - - // add a cell to a different family - auto cell4 = std::make_unique<Cell>(row, "family-2", "column-2", timestamp, value, cell_type); - incr.Add(std::move(cell4)); - EXPECT_EQ(2, incr.FamilyMap().size()); - EXPECT_EQ(1, incr.FamilyMap().at("family-2").size()); -} - -TEST(Increment, AddColumn) { - std::string row = "row"; - std::string family = "family"; - std::string column = "column"; - std::string value = "value"; - - Increment incr{"row"}; - incr.AddColumn(family, column, 5l); - EXPECT_EQ(1, incr.FamilyMap().size()); - EXPECT_EQ(1, incr.FamilyMap().at(family).size()); - - // add a second cell with same family - incr.AddColumn(family, "column-2", 6l); - EXPECT_EQ(1, incr.FamilyMap().size()); - EXPECT_EQ(2, incr.FamilyMap().at(family).size()); - - // add a cell to a different family - incr.AddColumn("family-2", column, 7l); - EXPECT_EQ(2, incr.FamilyMap().size()); - EXPECT_EQ(1, incr.FamilyMap().at("family-2").size()); -} http://git-wip-us.apache.org/repos/asf/hbase/blob/24f7f093/hbase-native-client/src/hbase/client/load-client.cc ---------------------------------------------------------------------- diff --git a/hbase-native-client/src/hbase/client/load-client.cc b/hbase-native-client/src/hbase/client/load-client.cc deleted file mode 100644 index a321845..0000000 --- a/hbase-native-client/src/hbase/client/load-client.cc +++ /dev/null @@ -1,390 +0,0 @@ -/* - * 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 <folly/Logging.h> -#include <folly/Random.h> -#include <gflags/gflags.h> - -#include <atomic> -#include <chrono> -#include <iostream> -#include <thread> - -#include "hbase/client/client.h" -#include "hbase/client/get.h" -#include "hbase/client/put.h" -#include "hbase/client/table.h" -#include "hbase/serde/table-name.h" -#include "hbase/utils/time-util.h" - -using hbase::Client; -using hbase::Configuration; -using hbase::Get; -using hbase::Put; -using hbase::Table; -using hbase::pb::TableName; -using hbase::TimeUtil; -using folly::Random; - -DEFINE_string(table, "load_test_table", "What table to do the reads and writes with"); -DEFINE_string(families, "f", "comma separated list of column family names"); -DEFINE_string(conf, "", "Conf directory to read the config from (optional)"); -DEFINE_string(zookeeper, "localhost:2181", "What zk quorum to talk to"); -DEFINE_string(znode, "/hbase", "parent znode"); -DEFINE_uint64(num_rows, 1'000'000, "How many rows to write and read"); -DEFINE_uint64(num_cols, 1000, "How many columns there are in a row"); -DEFINE_int32(threads, 10, "How many client threads"); -DEFINE_int32(batch_num_rows, 100, "number of rows in one multi-get / multi-put"); -DEFINE_uint64(report_num_rows, 5000, "How frequent we should report the progress"); -DEFINE_bool(gets, true, "perform gets"); -DEFINE_bool(scans, true, "perform scans"); -DEFINE_bool(puts, true, "perform put's"); -DEFINE_bool(appends, true, "perform append's"); - -static constexpr const char *kNumColumn = "num"; -static constexpr const char *incrPrefix = "i"; -static constexpr const char *appendPrefix = "a"; - -std::string PrefixZero(int total_width, int num) { - std::string str = std::to_string(num); - int prefix_len = total_width - str.length(); - if (prefix_len > 0) { - return std::string(prefix_len, '0') + str; - } - return str; -} - -bool Verify(std::shared_ptr<hbase::Result> result, std::string family, int m) { - auto col = std::to_string(m); - if (!result->Value(family, col)) { - LOG(ERROR) << "Column:" << col << " is not found for " << result->Row(); - return false; - } - auto l = *(result->Value(family, col)); - if (l != col) { - LOG(ERROR) << "value " << *(result->Value(family, "1")) << " is not " << col; - return false; - } - if (FLAGS_appends) { - if (!result->Value(family, incrPrefix + col)) { - LOG(ERROR) << "Column:" << (incrPrefix + col) << " is not found for " << result->Row(); - return false; - } - auto int_val = hbase::BytesUtil::ToInt64(*(result->Value(family, incrPrefix + col))); - if (int_val != m) { - LOG(ERROR) << "value is not " << col << " for " << result->Row(); - return false; - } - if (!result->Value(family, appendPrefix + col)) { - LOG(ERROR) << "Column:" << (appendPrefix + col) << " is not found for " << result->Row(); - return false; - } - l = *(result->Value(family, appendPrefix + col)); - if (l != col) { - LOG(ERROR) << "value " << *(result->Value(family, "1")) << " is not " << col; - return false; - } - } - - return true; -} - -bool Verify(std::shared_ptr<hbase::Result> result, const std::string &row, - const std::vector<std::string> &families) { - if (result == nullptr || result->IsEmpty()) { - LOG(ERROR) << "didn't get result"; - return false; - } - if (result->Row().compare(row) != 0) { - LOG(ERROR) << "row " << result->Row() << " is not the expected: " << row; - return false; - } - // Test the values - for (auto family : families) { - if (!result->Value(family, kNumColumn)) { - LOG(ERROR) << "Column:" << kNumColumn << " is not found for " << result->Row(); - return false; - } - auto cols = std::stoi(*(result->Value(family, kNumColumn))); - VLOG(3) << "Result for row:" << row << " contains " << std::to_string(cols) << " columns"; - for (int m = 1; m <= cols; m++) { - if (!Verify(result, family, m)) return false; - } - } - return true; -} - -bool DoScan(int iteration, uint64_t max_row, uint64_t rows, std::unique_ptr<Table> table, - const std::vector<std::string> &families) { - hbase::Scan scan{}; - auto start = iteration * rows; - auto end = start + rows; - auto width = std::to_string(max_row).length(); - scan.SetStartRow(PrefixZero(width, start)); - if (end != max_row && end != max_row + 1) { - scan.SetStopRow(PrefixZero(width, end)); - } - - auto start_ns = TimeUtil::GetNowNanos(); - auto scanner = table->Scan(scan); - - auto cnt = 0; - auto r = scanner->Next(); - while (r != nullptr) { - auto row = PrefixZero(width, start + cnt); - if (!Verify(r, row, families)) { - return false; - } - cnt++; - r = scanner->Next(); - if (cnt != 0 && cnt % FLAGS_report_num_rows == 0) { - LOG(INFO) << "(Thread " << iteration << ") " - << "Scan iterated over " << cnt << " results in " - << TimeUtil::ElapsedMillis(start_ns) << " ms."; - } - } - if (cnt != rows) { - LOG(ERROR) << "(Thread " << iteration << ") " - << "Expected number of results does not match. expected:" << rows - << ", actual:" << cnt; - return false; - } - LOG(INFO) << "(Thread " << iteration << ") " - << "scanned " << std::to_string(cnt) << " rows in " << TimeUtil::ElapsedMillis(start_ns) - << " ms."; - return true; -} - -bool DoGet(int iteration, uint64_t max_row, uint64_t rows, std::unique_ptr<Table> table, - const std::vector<std::string> &families, uint64_t batch_num_rows) { - auto width = std::to_string(max_row).length(); - auto start_ns = TimeUtil::GetNowNanos(); - for (uint64_t k = iteration; k <= max_row;) { - uint64_t total_read = 0; - std::vector<hbase::Get> gets; - for (uint64_t i = 0; i < batch_num_rows && k <= max_row; ++i, k += FLAGS_threads) { - std::string row = PrefixZero(width, k); - hbase::Get get(row); - gets.push_back(get); - } - VLOG(3) << "getting for " << batch_num_rows << " rows"; - auto results = table->Get(gets); - if (results.size() != gets.size()) { - LOG(ERROR) << "(Thread " << iteration << ") " - << "Expected number of results does not match. expected:" << gets.size() - << ", actual:" << results.size(); - return false; - } - for (uint64_t i = 0; i < batch_num_rows && i < results.size(); ++i) { - if (!Verify(results[i], gets[i].row(), families)) { - return false; - } - } - total_read += gets.size(); - if (total_read != 0 && total_read % FLAGS_report_num_rows == 0) { - LOG(INFO) << "(Thread " << iteration << ") " - << "Sent " << total_read << " Multi-Get requests in " - << TimeUtil::ElapsedMillis(start_ns) << " ms."; - } - k += batch_num_rows; - } - LOG(INFO) << "(Thread " << iteration << ") " - << "Sent " << rows << " gets" - << " in " << TimeUtil::ElapsedMillis(start_ns) << " ms."; - return true; -} - -void DoPut(int iteration, uint64_t max_row, uint64_t rows, int cols, std::unique_ptr<Table> table, - const std::vector<std::string> &families) { - auto start_ns = TimeUtil::GetNowNanos(); - auto width = std::to_string(max_row).length(); - for (uint64_t j = 0; j < rows; j++) { - std::string row = PrefixZero(width, iteration * rows + j); - auto put = Put{row}; - for (auto family : families) { - auto n_cols = Random::rand32(1, cols); - put.AddColumn(family, kNumColumn, std::to_string(n_cols)); - for (unsigned int k = 1; k <= n_cols; k++) { - put.AddColumn(family, std::to_string(k), std::to_string(k)); - } - } - table->Put(put); - if ((j + 1) % FLAGS_report_num_rows == 0) { - LOG(INFO) << "(Thread " << iteration << ") " - << "Written " << std::to_string(j + 1) << " rows in " - << TimeUtil::ElapsedMillis(start_ns) << " ms."; - } - } - LOG(INFO) << "(Thread " << iteration << ") " - << "written " << std::to_string(rows) << " rows" - << " in " << TimeUtil::ElapsedMillis(start_ns) << " ms."; -} - -bool DoAppendIncrement(int iteration, uint64_t max_row, uint64_t rows, int cols, - std::unique_ptr<Table> table, const std::vector<std::string> &families) { - auto start_ns = TimeUtil::GetNowNanos(); - auto width = std::to_string(max_row).length(); - for (uint64_t j = 0; j < rows; j++) { - std::string row = PrefixZero(width, iteration * rows + j); - hbase::Get get(row); - auto result = table->Get(get); - for (auto family : families) { - auto n_cols = std::stoi(*(result->Value(family, kNumColumn))); - for (unsigned int k = 1; k <= n_cols; k++) { - table->Increment( - hbase::Increment{row}.AddColumn(family, incrPrefix + std::to_string(k), k)); - if (!table->Append(hbase::Append{row}.Add(family, appendPrefix + std::to_string(k), - std::to_string(k)))) { - LOG(ERROR) << "(Thread " << iteration << ") " - << "append for " << row << " family: " << family << " failed"; - return false; - } - } - } - if ((j + 1) % FLAGS_report_num_rows == 0) - LOG(INFO) << "(Thread " << iteration << ") " - << "Written " << std::to_string(j + 1) << " increments" - << " in " << TimeUtil::ElapsedMillis(start_ns) << " ms."; - } - LOG(INFO) << "(Thread " << iteration << ") " - << "written " << std::to_string(rows) << " increments" - << " in " << TimeUtil::ElapsedMillis(start_ns) << " ms."; - return true; -} - -int main(int argc, char *argv[]) { - gflags::SetUsageMessage("Load client to manipulate multiple rows from HBase on the comamnd line"); - gflags::ParseCommandLineFlags(&argc, &argv, true); - google::InitGoogleLogging(argv[0]); - google::InstallFailureSignalHandler(); - FLAGS_logtostderr = 1; - FLAGS_stderrthreshold = 1; - - if (FLAGS_batch_num_rows < 1) { - LOG(ERROR) << "size of multi get should be positive"; - return -1; - } - if (!FLAGS_gets && !FLAGS_scans && !FLAGS_puts) { - LOG(ERROR) << "Must perform at least Get or Put operations"; - return -1; - } - std::shared_ptr<Configuration> conf = nullptr; - if (FLAGS_conf == "") { - // Configuration - conf = std::make_shared<Configuration>(); - conf->Set("hbase.zookeeper.quorum", FLAGS_zookeeper); - conf->Set("zookeeper.znode.parent", FLAGS_znode); - } else { - setenv("HBASE_CONF", FLAGS_conf.c_str(), 1); - hbase::HBaseConfigurationLoader loader; - conf = std::make_shared<Configuration>(loader.LoadDefaultResources().value()); - } - auto tn = std::make_shared<TableName>(folly::to<TableName>(FLAGS_table)); - auto num_puts = FLAGS_num_rows; - - auto client = std::make_unique<Client>(*conf); - - // Do the Put requests - - std::vector<std::string> families; - std::size_t pos = 0, found; - while ((found = FLAGS_families.find_first_of(',', pos)) != std::string::npos) { - families.push_back(FLAGS_families.substr(pos, found - pos)); - pos = found + 1; - } - families.push_back(FLAGS_families.substr(pos)); - - int rows = FLAGS_num_rows / FLAGS_threads; - if (FLAGS_num_rows % FLAGS_threads != 0) rows++; - int cols = FLAGS_num_cols; - std::atomic<int8_t> succeeded{1}; // not using bool since we want atomic &= - if (FLAGS_puts) { - LOG(INFO) << "Sending put requests"; - auto start_ns = TimeUtil::GetNowNanos(); - std::vector<std::thread> writer_threads; - for (int i = 0; i < FLAGS_threads; i++) { - writer_threads.push_back(std::thread([&, i] { - auto table = client->Table(*tn); - DoPut(i, FLAGS_num_rows - 1, rows, cols, std::move(table), families); - })); - } - for (auto &t : writer_threads) { - t.join(); - } - LOG(INFO) << "Successfully sent " << num_puts << " Put requests in " - << TimeUtil::ElapsedMillis(start_ns) << " ms."; - } - if (FLAGS_appends) { - LOG(INFO) << "Sending append/increment requests"; - auto start_ns = TimeUtil::GetNowNanos(); - std::vector<std::thread> writer_threads; - for (int i = 0; i < FLAGS_threads; i++) { - writer_threads.push_back(std::thread([&, i] { - auto table = client->Table(*tn); - succeeded &= - DoAppendIncrement(i, FLAGS_num_rows - 1, rows, cols, std::move(table), families); - })); - } - for (auto &t : writer_threads) { - t.join(); - } - LOG(INFO) << "Successfully sent " << num_puts << " append requests in " - << TimeUtil::ElapsedMillis(start_ns) << " ms."; - } - - if (FLAGS_scans) { - LOG(INFO) << "Sending scan requests"; - auto start_ns = TimeUtil::GetNowNanos(); - std::vector<std::thread> reader_threads; - for (int i = 0; i < FLAGS_threads; i++) { - reader_threads.push_back(std::thread([&, i] { - auto table1 = client->Table(*tn); - succeeded &= DoScan(i, FLAGS_num_rows - 1, rows, std::move(table1), families); - })); - } - for (auto &t : reader_threads) { - t.join(); - } - - LOG(INFO) << (succeeded.load() ? "Successfully " : "Failed. ") << " scannned " << num_puts - << " rows in " << TimeUtil::ElapsedMillis(start_ns) << " ms."; - } - - if (FLAGS_gets) { - LOG(INFO) << "Sending get requests"; - auto start_ns = TimeUtil::GetNowNanos(); - std::vector<std::thread> reader_threads; - for (int i = 0; i < FLAGS_threads; i++) { - reader_threads.push_back(std::thread([&, i] { - auto table1 = client->Table(*tn); - succeeded &= - DoGet(i, FLAGS_num_rows - 1, rows, std::move(table1), families, FLAGS_batch_num_rows); - })); - } - for (auto &t : reader_threads) { - t.join(); - } - - LOG(INFO) << (succeeded.load() ? "Successful. " : "Failed. ") << " sent multi-get requests for " - << num_puts << " rows in " << TimeUtil::ElapsedMillis(start_ns) << " ms."; - } - client->Close(); - - return succeeded.load() ? 0 : -1; -} http://git-wip-us.apache.org/repos/asf/hbase/blob/24f7f093/hbase-native-client/src/hbase/client/location-cache-retry-test.cc ---------------------------------------------------------------------- diff --git a/hbase-native-client/src/hbase/client/location-cache-retry-test.cc b/hbase-native-client/src/hbase/client/location-cache-retry-test.cc deleted file mode 100644 index 283cf85..0000000 --- a/hbase-native-client/src/hbase/client/location-cache-retry-test.cc +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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 <gtest/gtest.h> - -#include "hbase/client/append.h" -#include "hbase/client/cell.h" -#include "hbase/client/client.h" -#include "hbase/client/configuration.h" -#include "hbase/client/delete.h" -#include "hbase/client/get.h" -#include "hbase/client/hbase-configuration-loader.h" -#include "hbase/client/increment.h" -#include "hbase/client/meta-utils.h" -#include "hbase/client/put.h" -#include "hbase/client/result.h" -#include "hbase/client/table.h" -#include "hbase/exceptions/exception.h" -#include "hbase/serde/table-name.h" -#include "hbase/test-util/test-util.h" -#include "hbase/utils/bytes-util.h" - -using hbase::Cell; -using hbase::Configuration; -using hbase::Get; -using hbase::MetaUtil; -using hbase::RetriesExhaustedException; -using hbase::Put; -using hbase::Table; -using hbase::TestUtil; - -using std::chrono_literals::operator"" s; - -class LocationCacheRetryTest : public ::testing::Test { - public: - static std::unique_ptr<hbase::TestUtil> test_util; - static void SetUpTestCase() { - google::InstallFailureSignalHandler(); - test_util = std::make_unique<hbase::TestUtil>(); - test_util->StartMiniCluster(2); - test_util->conf()->SetInt("hbase.client.retries.number", 5); - } -}; - -std::unique_ptr<hbase::TestUtil> LocationCacheRetryTest::test_util = nullptr; - -TEST_F(LocationCacheRetryTest, GetFromMetaTable) { - auto tn = folly::to<hbase::pb::TableName>("hbase:meta"); - auto row = "test1"; - - hbase::Client client(*LocationCacheRetryTest::test_util->conf()); - - // do a get against the other table, but not the actual table "t". - auto table = client.Table(tn); - hbase::Get get(row); - auto result = table->Get(get); - - LocationCacheRetryTest::test_util->MoveRegion(MetaUtil::kMetaRegion, ""); - - std::this_thread::sleep_for(3s); // sleep 3 sec - - result = table->Get(get); -} - -TEST_F(LocationCacheRetryTest, PutGet) { - LocationCacheRetryTest::test_util->CreateTable("t", "d"); - LocationCacheRetryTest::test_util->CreateTable("t2", "d"); - - auto tn = folly::to<hbase::pb::TableName>("t"); - auto tn2 = folly::to<hbase::pb::TableName>("t2"); - auto row = "test1"; - - hbase::Client client(*LocationCacheRetryTest::test_util->conf()); - - // do a get against the other table, but not the actual table "t". - auto table = client.Table(tn); - auto table2 = client.Table(tn2); - hbase::Get get(row); - auto result = table2->Get(get); - - // we should have already cached the location of meta right now. Now - // move the meta region to the other server so that we will get a NotServingRegionException - // when we do the actual location lookup request. If there is no invalidation - // of the meta's own location, then following put/get will result in retries exhausted. - LocationCacheRetryTest::test_util->MoveRegion(MetaUtil::kMetaRegion, ""); - - std::this_thread::sleep_for(3s); // sleep 3 sec - - table->Put(Put{row}.AddColumn("d", "1", "value1")); - - result = table->Get(get); - - ASSERT_TRUE(!result->IsEmpty()) << "Result shouldn't be empty."; - EXPECT_EQ("test1", result->Row()); - EXPECT_EQ("value1", *(result->Value("d", "1"))); -} http://git-wip-us.apache.org/repos/asf/hbase/blob/24f7f093/hbase-native-client/src/hbase/client/location-cache-test.cc ---------------------------------------------------------------------- diff --git a/hbase-native-client/src/hbase/client/location-cache-test.cc b/hbase-native-client/src/hbase/client/location-cache-test.cc deleted file mode 100644 index af25902..0000000 --- a/hbase-native-client/src/hbase/client/location-cache-test.cc +++ /dev/null @@ -1,164 +0,0 @@ -/* - * 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 "hbase/client/location-cache.h" - -#include <folly/Memory.h> -#include <gtest/gtest.h> - -#include <chrono> - -#include "hbase/client/keyvalue-codec.h" -#include "hbase/if/HBase.pb.h" -#include "hbase/serde/table-name.h" -#include "hbase/test-util/test-util.h" - -using hbase::Cell; -using hbase::Configuration; -using hbase::ConnectionPool; -using hbase::MetaUtil; -using hbase::LocationCache; -using hbase::TestUtil; -using hbase::KeyValueCodec; -using std::chrono::milliseconds; - -class LocationCacheTest : public ::testing::Test { - protected: - static void SetUpTestCase() { - google::InstallFailureSignalHandler(); - test_util_ = std::make_unique<TestUtil>(); - test_util_->StartMiniCluster(2); - } - static void TearDownTestCase() { test_util_.release(); } - - virtual void SetUp() {} - virtual void TearDown() {} - - public: - static std::unique_ptr<TestUtil> test_util_; -}; - -std::unique_ptr<TestUtil> LocationCacheTest::test_util_ = nullptr; - -TEST_F(LocationCacheTest, TestGetMetaNodeContents) { - auto cpu = std::make_shared<wangle::CPUThreadPoolExecutor>(4); - auto io = std::make_shared<wangle::IOThreadPoolExecutor>(4); - auto codec = std::make_shared<KeyValueCodec>(); - auto cp = std::make_shared<ConnectionPool>(io, cpu, codec, LocationCacheTest::test_util_->conf()); - LocationCache cache{LocationCacheTest::test_util_->conf(), io, cpu, cp}; - auto f = cache.LocateMeta(); - auto result = f.get(); - ASSERT_FALSE(f.hasException()); - ASSERT_TRUE(result.has_port()); - ASSERT_TRUE(result.has_host_name()); - cpu->stop(); - io->stop(); -} - -TEST_F(LocationCacheTest, TestGetRegionLocation) { - auto cpu = std::make_shared<wangle::CPUThreadPoolExecutor>(4); - auto io = std::make_shared<wangle::IOThreadPoolExecutor>(4); - auto codec = std::make_shared<KeyValueCodec>(); - auto cp = std::make_shared<ConnectionPool>(io, cpu, codec, LocationCacheTest::test_util_->conf()); - LocationCache cache{LocationCacheTest::test_util_->conf(), io, cpu, cp}; - - // If there is no table this should throw an exception - auto tn = folly::to<hbase::pb::TableName>("t"); - auto row = "test"; - ASSERT_ANY_THROW(cache.LocateFromMeta(tn, row).get(milliseconds(1000))); - LocationCacheTest::test_util_->CreateTable("t", "d"); - auto loc = cache.LocateFromMeta(tn, row).get(milliseconds(1000)); - ASSERT_TRUE(loc != nullptr); - cpu->stop(); - io->stop(); -} - -TEST_F(LocationCacheTest, TestCaching) { - auto cpu = std::make_shared<wangle::CPUThreadPoolExecutor>(4); - auto io = std::make_shared<wangle::IOThreadPoolExecutor>(4); - auto codec = std::make_shared<KeyValueCodec>(); - auto cp = std::make_shared<ConnectionPool>(io, cpu, codec, LocationCacheTest::test_util_->conf()); - LocationCache cache{LocationCacheTest::test_util_->conf(), io, cpu, cp}; - - auto tn_1 = folly::to<hbase::pb::TableName>("t1"); - auto tn_2 = folly::to<hbase::pb::TableName>("t2"); - auto tn_3 = folly::to<hbase::pb::TableName>("t3"); - auto row_a = "a"; - - // test location pulled from meta gets cached - ASSERT_ANY_THROW(cache.LocateRegion(tn_1, row_a).get(milliseconds(1000))); - ASSERT_ANY_THROW(cache.LocateFromMeta(tn_1, row_a).get(milliseconds(1000))); - LocationCacheTest::test_util_->CreateTable("t1", "d"); - - ASSERT_FALSE(cache.IsLocationCached(tn_1, row_a)); - auto loc = cache.LocateRegion(tn_1, row_a).get(milliseconds(1000)); - ASSERT_TRUE(cache.IsLocationCached(tn_1, row_a)); - ASSERT_EQ(loc, cache.GetCachedLocation(tn_1, row_a)); - - // test with two regions - std::vector<std::string> keys; - keys.push_back("b"); - LocationCacheTest::test_util_->CreateTable("t2", "d", keys); - - ASSERT_FALSE(cache.IsLocationCached(tn_2, "a")); - loc = cache.LocateRegion(tn_2, "a").get(milliseconds(1000)); - ASSERT_TRUE(cache.IsLocationCached(tn_2, "a")); - ASSERT_EQ(loc, cache.GetCachedLocation(tn_2, "a")); - - ASSERT_FALSE(cache.IsLocationCached(tn_2, "b")); - loc = cache.LocateRegion(tn_2, "b").get(milliseconds(1000)); - ASSERT_TRUE(cache.IsLocationCached(tn_2, "b")); - ASSERT_EQ(loc, cache.GetCachedLocation(tn_2, "b")); - ASSERT_TRUE(cache.IsLocationCached(tn_2, "ba")); - ASSERT_EQ(loc, cache.GetCachedLocation(tn_2, "ba")); - - // test with three regions - keys.clear(); - keys.push_back("b"); - keys.push_back("c"); - LocationCacheTest::test_util_->CreateTable("t3", "d", keys); - - ASSERT_FALSE(cache.IsLocationCached(tn_3, "c")); - ASSERT_FALSE(cache.IsLocationCached(tn_3, "ca")); - loc = cache.LocateRegion(tn_3, "ca").get(milliseconds(1000)); - ASSERT_TRUE(cache.IsLocationCached(tn_3, "c")); - ASSERT_EQ(loc, cache.GetCachedLocation(tn_3, "c")); - ASSERT_TRUE(cache.IsLocationCached(tn_3, "ca")); - ASSERT_EQ(loc, cache.GetCachedLocation(tn_3, "ca")); - - ASSERT_FALSE(cache.IsLocationCached(tn_3, "b")); - loc = cache.LocateRegion(tn_3, "b").get(milliseconds(1000)); - ASSERT_TRUE(cache.IsLocationCached(tn_3, "b")); - ASSERT_EQ(loc, cache.GetCachedLocation(tn_3, "b")); - ASSERT_TRUE(cache.IsLocationCached(tn_3, "ba")); - ASSERT_EQ(loc, cache.GetCachedLocation(tn_3, "ba")); - - // clear second region - cache.ClearCachedLocation(tn_3, "b"); - ASSERT_FALSE(cache.IsLocationCached(tn_3, "b")); - - ASSERT_FALSE(cache.IsLocationCached(tn_3, "a")); - loc = cache.LocateRegion(tn_3, "a").get(milliseconds(1000)); - ASSERT_TRUE(cache.IsLocationCached(tn_3, "a")); - ASSERT_EQ(loc, cache.GetCachedLocation(tn_3, "a")); - ASSERT_TRUE(cache.IsLocationCached(tn_3, "abc")); - ASSERT_EQ(loc, cache.GetCachedLocation(tn_3, "abc")); - - cpu->stop(); - io->stop(); -} http://git-wip-us.apache.org/repos/asf/hbase/blob/24f7f093/hbase-native-client/src/hbase/client/put-test.cc ---------------------------------------------------------------------- diff --git a/hbase-native-client/src/hbase/client/put-test.cc b/hbase-native-client/src/hbase/client/put-test.cc deleted file mode 100644 index 0657bb6..0000000 --- a/hbase-native-client/src/hbase/client/put-test.cc +++ /dev/null @@ -1,135 +0,0 @@ -/* - * 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 <glog/logging.h> -#include <gtest/gtest.h> - -#include "hbase/client/mutation.h" -#include "hbase/client/put.h" -#include "hbase/utils/time-util.h" - -using hbase::Put; -using hbase::Cell; -using hbase::CellType; -using hbase::Mutation; -using hbase::TimeUtil; - -const constexpr int64_t Mutation::kLatestTimestamp; - -TEST(Put, Row) { - Put put{"foo"}; - EXPECT_EQ("foo", put.row()); -} - -TEST(Put, Durability) { - Put put{"row"}; - EXPECT_EQ(hbase::pb::MutationProto_Durability_USE_DEFAULT, put.Durability()); - - auto skipWal = hbase::pb::MutationProto_Durability_SKIP_WAL; - put.SetDurability(skipWal); - EXPECT_EQ(skipWal, put.Durability()); -} - -TEST(Put, Timestamp) { - Put put{"row"}; - - // test default timestamp - EXPECT_EQ(Mutation::kLatestTimestamp, put.TimeStamp()); - - // set custom timestamp - auto ts = TimeUtil::ToMillis(TimeUtil::GetNowNanos()); - put.SetTimeStamp(ts); - EXPECT_EQ(ts, put.TimeStamp()); - - // Add a column with custom timestamp - put.AddColumn("f", "q", "v"); - auto &cell = put.FamilyMap().at("f")[0]; - EXPECT_EQ(ts, cell->Timestamp()); -} - -TEST(Put, HasFamilies) { - Put put{"row"}; - - EXPECT_EQ(false, put.HasFamilies()); - - put.AddColumn("f", "q", "v"); - EXPECT_EQ(true, put.HasFamilies()); -} - -TEST(Put, Add) { - CellType cell_type = CellType::PUT; - std::string row = "row"; - std::string family = "family"; - std::string column = "column"; - std::string value = "value"; - int64_t timestamp = std::numeric_limits<int64_t>::max(); - auto cell = std::make_unique<Cell>(row, family, column, timestamp, value, cell_type); - - // add first cell - Put put{"row"}; - put.Add(std::move(cell)); - EXPECT_EQ(1, put.FamilyMap().size()); - EXPECT_EQ(1, put.FamilyMap().at(family).size()); - - // add a non-matching row - auto cell2 = std::make_unique<Cell>(row, family, column, timestamp, value, cell_type); - Put put2{"foo"}; - ASSERT_THROW(put2.Add(std::move(cell2)), std::runtime_error); // rows don't match - - // add a second cell with same family - auto cell3 = std::make_unique<Cell>(row, family, "column-2", timestamp, value, cell_type); - put.Add(std::move(cell3)); - EXPECT_EQ(1, put.FamilyMap().size()); - EXPECT_EQ(2, put.FamilyMap().at(family).size()); - - // add a cell to a different family - auto cell4 = std::make_unique<Cell>(row, "family-2", "column-2", timestamp, value, cell_type); - put.Add(std::move(cell4)); - EXPECT_EQ(2, put.FamilyMap().size()); - EXPECT_EQ(1, put.FamilyMap().at("family-2").size()); -} - -TEST(Put, AddColumn) { - std::string row = "row"; - std::string family = "family"; - std::string column = "column"; - std::string value = "value"; - - Put put{"row"}; - put.AddColumn(family, column, value); - EXPECT_EQ(1, put.FamilyMap().size()); - EXPECT_EQ(1, put.FamilyMap().at(family).size()); - - // add a second cell with same family - put.AddColumn(family, "column-2", value); - EXPECT_EQ(1, put.FamilyMap().size()); - EXPECT_EQ(2, put.FamilyMap().at(family).size()); - - // add a cell to a different family - put.AddColumn("family-2", column, value); - EXPECT_EQ(2, put.FamilyMap().size()); - EXPECT_EQ(1, put.FamilyMap().at("family-2").size()); - - // use the AddColumn overload - auto ts = TimeUtil::ToMillis(TimeUtil::GetNowNanos()); - put.AddColumn(family, column, ts, value); - EXPECT_EQ(2, put.FamilyMap().size()); - EXPECT_EQ(3, put.FamilyMap().at(family).size()); - auto &cell = put.FamilyMap().at(family)[2]; - EXPECT_EQ(ts, cell->Timestamp()); -} http://git-wip-us.apache.org/repos/asf/hbase/blob/24f7f093/hbase-native-client/src/hbase/client/request-converter-test.cc ---------------------------------------------------------------------- diff --git a/hbase-native-client/src/hbase/client/request-converter-test.cc b/hbase-native-client/src/hbase/client/request-converter-test.cc deleted file mode 100644 index 0878519..0000000 --- a/hbase-native-client/src/hbase/client/request-converter-test.cc +++ /dev/null @@ -1,126 +0,0 @@ -/* - * 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 "hbase/client/request-converter.h" - -#include <gtest/gtest.h> -#include <limits> -#include "hbase/connection/request.h" -#include "hbase/client/get.h" -#include "hbase/client/scan.h" - -using hbase::Get; -using hbase::Scan; - -using hbase::pb::GetRequest; -using hbase::pb::RegionSpecifier; -using hbase::pb::RegionSpecifier_RegionSpecifierType; -using hbase::pb::ScanRequest; - -TEST(RequestConverter, ToGet) { - std::string row_str = "row-test"; - Get get(row_str); - get.AddFamily("family-1"); - get.AddFamily("family-2"); - get.AddFamily("family-3"); - get.AddColumn("family-2", "qualifier-1"); - get.AddColumn("family-2", "qualifier-2"); - get.AddColumn("family-2", "qualifier-3"); - get.SetCacheBlocks(false); - get.SetConsistency(hbase::pb::Consistency::TIMELINE); - get.SetMaxVersions(2); - get.SetTimeRange(10000, 20000); - std::string region_name("RegionName"); - - auto req = hbase::RequestConverter::ToGetRequest(get, region_name); - auto msg = std::static_pointer_cast<GetRequest>(req->req_msg()); - - // Tests whether the PB object is properly set or not. - ASSERT_TRUE(msg->has_region()); - ASSERT_TRUE(msg->region().has_value()); - EXPECT_EQ(msg->region().value(), region_name); - - ASSERT_TRUE(msg->has_get()); - EXPECT_EQ(msg->get().row(), row_str); - EXPECT_FALSE(msg->get().cache_blocks()); - EXPECT_EQ(msg->get().consistency(), hbase::pb::Consistency::TIMELINE); - EXPECT_EQ(msg->get().max_versions(), 2); - EXPECT_EQ(msg->get().column_size(), 3); - for (int i = 0; i < msg->get().column_size(); ++i) { - EXPECT_EQ(msg->get().column(i).family(), "family-" + std::to_string(i + 1)); - for (int j = 0; j < msg->get().column(i).qualifier_size(); ++j) { - EXPECT_EQ(msg->get().column(i).qualifier(j), "qualifier-" + std::to_string(j + 1)); - } - } -} - -TEST(RequestConverter, ToScan) { - std::string start_row("start-row"); - std::string stop_row("stop-row"); - hbase::Scan scan; - scan.AddFamily("family-1"); - scan.AddFamily("family-2"); - scan.AddFamily("family-3"); - scan.AddColumn("family-2", "qualifier-1"); - scan.AddColumn("family-2", "qualifier-2"); - scan.AddColumn("family-2", "qualifier-3"); - scan.SetReversed(true); - scan.SetStartRow(start_row); - scan.SetStopRow(stop_row); - scan.SetCaching(3); - scan.SetConsistency(hbase::pb::Consistency::TIMELINE); - scan.SetCacheBlocks(true); - scan.SetAllowPartialResults(true); - scan.SetLoadColumnFamiliesOnDemand(true); - scan.SetMaxVersions(5); - scan.SetTimeRange(10000, 20000); - std::string region_name("RegionName"); - - auto req = hbase::RequestConverter::ToScanRequest(scan, region_name); - auto msg = std::static_pointer_cast<ScanRequest>(req->req_msg()); - - // Tests whether the PB object is properly set or not. - ASSERT_TRUE(msg->has_region()); - ASSERT_TRUE(msg->region().has_value()); - EXPECT_EQ(msg->region().value(), region_name); - - ASSERT_TRUE(msg->has_scan()); - EXPECT_TRUE(msg->scan().reversed()); - EXPECT_EQ(msg->scan().start_row(), start_row); - EXPECT_EQ(msg->scan().stop_row(), stop_row); - EXPECT_FALSE(msg->scan().small()); - EXPECT_EQ(msg->scan().caching(), 3); - EXPECT_EQ(msg->scan().consistency(), hbase::pb::Consistency::TIMELINE); - EXPECT_TRUE(msg->scan().cache_blocks()); - EXPECT_TRUE(msg->scan().allow_partial_results()); - EXPECT_TRUE(msg->scan().load_column_families_on_demand()); - EXPECT_EQ(msg->scan().max_versions(), 5); - EXPECT_EQ(msg->scan().max_result_size(), std::numeric_limits<uint64_t>::max()); - - EXPECT_EQ(msg->scan().column_size(), 3); - for (int i = 0; i < msg->scan().column_size(); ++i) { - EXPECT_EQ(msg->scan().column(i).family(), "family-" + std::to_string(i + 1)); - for (int j = 0; j < msg->scan().column(i).qualifier_size(); ++j) { - EXPECT_EQ(msg->scan().column(i).qualifier(j), "qualifier-" + std::to_string(j + 1)); - } - } - ASSERT_FALSE(msg->client_handles_partials()); - ASSERT_FALSE(msg->client_handles_heartbeats()); - ASSERT_FALSE(msg->track_scan_metrics()); -} http://git-wip-us.apache.org/repos/asf/hbase/blob/24f7f093/hbase-native-client/src/hbase/client/result-test.cc ---------------------------------------------------------------------- diff --git a/hbase-native-client/src/hbase/client/result-test.cc b/hbase-native-client/src/hbase/client/result-test.cc deleted file mode 100644 index 3857d7b..0000000 --- a/hbase-native-client/src/hbase/client/result-test.cc +++ /dev/null @@ -1,323 +0,0 @@ -/* - * 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 <glog/logging.h> -#include <gtest/gtest.h> -#include <limits> -#include <memory> -#include <string> -#include <vector> - -#include "hbase/client/cell.h" -#include "hbase/client/result.h" -#include "hbase/utils/optional.h" - -using hbase::Cell; -using hbase::CellType; -using hbase::none; -using hbase::Result; - -void PopulateCells(std::vector<std::shared_ptr<Cell> > &cells) { - // Populate some Results - // We assume that for a single Cell, the corresponding row, families and - // qualifiers are present. - // We have also considered different versions in the test for the same row. - std::string row = "row"; - for (int i = 0; i < 10; i++) { - std::string family = "family-" + std::to_string(i); - std::string column = "column-" + std::to_string(i); - std::string value = "value-" + std::to_string(i); - - switch (i) { - case 5: { - cells.push_back( - std::make_shared<Cell>(row, family, column, 1482113040506, "value-5", CellType::PUT)); - cells.push_back( - std::make_shared<Cell>(row, family, column, 1482111803856, "value-X", CellType::PUT)); - break; - } - case 8: { - cells.push_back( - std::make_shared<Cell>(row, family, column, 1482113040506, "value-8", CellType::PUT)); - cells.push_back( - std::make_shared<Cell>(row, family, column, 1482111803856, "value-X", CellType::PUT)); - cells.push_back( - std::make_shared<Cell>(row, family, column, 1482110969958, "value-Y", CellType::PUT)); - break; - } - case 9: { - cells.push_back( - std::make_shared<Cell>(row, family, column, 1482113040506, "value-9", CellType::PUT)); - cells.push_back( - std::make_shared<Cell>(row, family, column, 1482111803856, "value-X", CellType::PUT)); - cells.push_back( - std::make_shared<Cell>(row, family, column, 1482110969958, "value-Y", CellType::PUT)); - cells.push_back( - std::make_shared<Cell>(row, family, column, 1482110876075, "value-Z", CellType::PUT)); - break; - } - default: { - cells.push_back(std::make_shared<Cell>( - row, family, column, std::numeric_limits<int64_t>::max(), value, CellType::PUT)); - } - } - } - return; -} - -TEST(Result, EmptyResult) { - std::vector<std::shared_ptr<Cell> > cells; - Result result(cells, true, false, false); - EXPECT_EQ(true, result.IsEmpty()); - EXPECT_EQ(0, result.Size()); -} - -TEST(Result, FilledResult) { - std::vector<std::shared_ptr<Cell> > cells; - PopulateCells(cells); - - Result result(cells, true, false, false); - - EXPECT_EQ(false, result.IsEmpty()); - EXPECT_EQ(16, result.Size()); - - // Get Latest Cell for the given family and qualifier. - auto latest_cell(result.ColumnLatestCell("family", "column")); - // Nothing of the above family/qualifier combo is present so it should be - // nullptr - ASSERT_FALSE(latest_cell.get()); - - // Try to get the latest cell for the given family and qualifier. - latest_cell = result.ColumnLatestCell("family-4", "column-4"); - // Now shouldn't be a nullptr - ASSERT_TRUE(latest_cell.get()); - // And Value must match too - EXPECT_EQ("value-4", latest_cell->Value()); - - // Value will be nullptr as no such family and qualifier is present - ASSERT_FALSE(result.Value("family-4", "qualifier")); - // Value will be present as family and qualifier is present - ASSERT_TRUE(result.Value("family-4", "column-4") != none); - // Value should be present and match. - EXPECT_EQ(latest_cell->Value(), (*result.ColumnLatestCell("family-4", "column-4")).Value()); - EXPECT_EQ("value-5", (*result.ColumnLatestCell("family-5", "column-5")).Value()); - EXPECT_EQ("value-8", (*result.ColumnLatestCell("family-8", "column-8")).Value()); - EXPECT_EQ("value-7", *result.Value("family-7", "column-7")); - - // Get cells for the given family and qualifier - auto column_cells = result.ColumnCells("family", "column"); - // Size should be 0 - EXPECT_EQ(0, column_cells.size()); - - // Size shouldn't be 0 and Row() and Value() must match - column_cells = result.ColumnCells("family-0", "column-0"); - EXPECT_EQ(1, column_cells.size()); - EXPECT_EQ("row", column_cells[0]->Row()); - EXPECT_EQ("row", result.Row()); - - // Size shouldn't be 0 and Row() and Value() must match - column_cells = result.ColumnCells("family-5", "column-5"); - EXPECT_EQ(2, column_cells.size()); - EXPECT_EQ("row", column_cells[0]->Row()); - EXPECT_EQ("row", column_cells[1]->Row()); - EXPECT_EQ("value-5", column_cells[0]->Value()); - EXPECT_EQ("value-X", column_cells[1]->Value()); - EXPECT_EQ("row", result.Row()); - - // Size shouldn't be 0 and Row() and Value() must match - column_cells = result.ColumnCells("family-8", "column-8"); - EXPECT_EQ(3, column_cells.size()); - EXPECT_EQ("row", column_cells[0]->Row()); - EXPECT_EQ("row", column_cells[1]->Row()); - EXPECT_EQ("row", column_cells[2]->Row()); - EXPECT_EQ("value-8", column_cells[0]->Value()); - EXPECT_EQ("value-X", column_cells[1]->Value()); - EXPECT_EQ("value-Y", column_cells[2]->Value()); - EXPECT_EQ("row", result.Row()); - - // Size shouldn't be 0 and Row() and Value() must match - column_cells = result.ColumnCells("family-9", "column-9"); - EXPECT_EQ(4, column_cells.size()); - EXPECT_EQ("row", column_cells[0]->Row()); - EXPECT_EQ("row", column_cells[1]->Row()); - EXPECT_EQ("row", column_cells[2]->Row()); - EXPECT_EQ("row", column_cells[3]->Row()); - EXPECT_EQ("value-9", column_cells[0]->Value()); - EXPECT_EQ("value-X", column_cells[1]->Value()); - EXPECT_EQ("value-Y", column_cells[2]->Value()); - EXPECT_EQ("value-Z", column_cells[3]->Value()); - EXPECT_EQ("row", result.Row()); - - // Test all the Cell values - const auto &result_cells = result.Cells(); - int i = 0, j = 0; - for (const auto &cell : result_cells) { - std::string row = "row"; - std::string family = "family-" + std::to_string(i); - std::string column = "column-" + std::to_string(i); - std::string value = "value-" + std::to_string(i); - switch (j) { - case 6: - case 10: - case 13: { - EXPECT_EQ("value-X", cell->Value()); - ++j; - continue; - } - case 11: - case 14: { - EXPECT_EQ("value-Y", cell->Value()); - ++j; - continue; - } - case 15: { - EXPECT_EQ("value-Z", cell->Value()); - ++j; - continue; - } - } - EXPECT_EQ(row, cell->Row()); - EXPECT_EQ(family, cell->Family()); - EXPECT_EQ(column, cell->Qualifier()); - EXPECT_EQ(value, cell->Value()); - ++i; - ++j; - } - - auto result_map_tmp = result.Map(); - result_map_tmp["testf"]["testq"][1] = "value"; - EXPECT_EQ(11, result_map_tmp.size()); - - auto result_map = result.Map(); - EXPECT_EQ(10, result_map.size()); - - i = 0; - for (auto family_map : result_map) { - std::string family = "family-" + std::to_string(i); - std::string qualifier = "column-" + std::to_string(i); - std::string value = "value-" + std::to_string(i); - EXPECT_EQ(family, family_map.first); - for (auto qualifier_map : family_map.second) { - EXPECT_EQ(qualifier, qualifier_map.first); - j = 0; - for (auto version_map : qualifier_map.second) { - switch (i) { - case 5: { - if (1 == j) { - EXPECT_EQ(1482111803856, version_map.first); - EXPECT_EQ("value-X", version_map.second); - } else if (0 == j) { - EXPECT_EQ(1482113040506, version_map.first); - EXPECT_EQ("value-5", version_map.second); - } - break; - } - case 8: { - if (2 == j) { - EXPECT_EQ(1482110969958, version_map.first); - EXPECT_EQ("value-Y", version_map.second); - } else if (1 == j) { - EXPECT_EQ(1482111803856, version_map.first); - EXPECT_EQ("value-X", version_map.second); - } else if (0 == j) { - EXPECT_EQ(1482113040506, version_map.first); - EXPECT_EQ("value-8", version_map.second); - } - break; - } - case 9: { - if (3 == j) { - EXPECT_EQ(1482110876075, version_map.first); - EXPECT_EQ("value-Z", version_map.second); - } else if (2 == j) { - EXPECT_EQ(1482110969958, version_map.first); - EXPECT_EQ("value-Y", version_map.second); - } else if (1 == j) { - EXPECT_EQ(1482111803856, version_map.first); - EXPECT_EQ("value-X", version_map.second); - } else if (0 == j) { - EXPECT_EQ(1482113040506, version_map.first); - EXPECT_EQ("value-9", version_map.second); - } - break; - } - default: { - EXPECT_EQ(std::numeric_limits<int64_t>::max(), version_map.first); - EXPECT_EQ(value, version_map.second); - } - } - ++j; - } - } - ++i; - } - - auto family_map = result.FamilyMap("family-0"); - EXPECT_EQ(1, family_map.size()); - i = 0; - for (auto qual_val_map : family_map) { - EXPECT_EQ("column-0", qual_val_map.first); - EXPECT_EQ("value-0", qual_val_map.second); - } - - family_map = result.FamilyMap("family-1"); - EXPECT_EQ(1, family_map.size()); - i = 0; - for (auto qual_val_map : family_map) { - EXPECT_EQ("column-1", qual_val_map.first); - EXPECT_EQ("value-1", qual_val_map.second); - } - - family_map = result.FamilyMap("family-5"); - EXPECT_EQ(1, family_map.size()); - i = 0; - for (auto qual_val_map : family_map) { - EXPECT_EQ("column-5", qual_val_map.first); - EXPECT_EQ("value-5", qual_val_map.second); - } - - family_map = result.FamilyMap("family-9"); - EXPECT_EQ(1, family_map.size()); - i = 0; - for (auto qual_val_map : family_map) { - EXPECT_EQ("column-9", qual_val_map.first); - EXPECT_EQ("value-9", qual_val_map.second); - } -} - -TEST(Result, ResultEstimatedSize) { - CellType cell_type = CellType::PUT; - int64_t timestamp = std::numeric_limits<int64_t>::max(); - std::vector<std::shared_ptr<Cell> > cells; - Result empty(cells, true, false, false); - - EXPECT_EQ(empty.EstimatedSize(), sizeof(Result)); - - cells.push_back(std::make_shared<Cell>("a", "a", "", timestamp, "", cell_type)); - Result result1(cells, true, false, false); - EXPECT_TRUE(result1.EstimatedSize() > empty.EstimatedSize()); - - cells.push_back(std::make_shared<Cell>("a", "a", "", timestamp, "", cell_type)); - Result result2(cells, true, false, false); - EXPECT_TRUE(result2.EstimatedSize() > result1.EstimatedSize()); - - LOG(INFO) << empty.EstimatedSize(); - LOG(INFO) << result1.EstimatedSize(); - LOG(INFO) << result2.EstimatedSize(); -}