From 2789c0a95668552c212a551c3a3ef0481fcbc52c Mon Sep 17 00:00:00 2001 From: Sudeep Sunthankar Date: Thu, 23 Jun 2016 17:34:22 +1000 Subject: [PATCH] 1) Cell:- An abstract class haiving methods to get rowkey, column-family, column, value, timestamp and type. 2) CellImpl:- Implements the pure virtual functions specified in Cell class 3) cell-test.cc:- Unit tests for the above functions --- hbase-native-client/core/cell-test.cc | 196 ++++++++++++++++++++++++++++++++++ hbase-native-client/core/cell.cc | 27 +++++ hbase-native-client/core/cell.h | 40 +++++++ hbase-native-client/core/cell_impl.cc | 85 +++++++++++++++ hbase-native-client/core/cell_impl.h | 51 +++++++++ 5 files changed, 399 insertions(+) create mode 100644 hbase-native-client/core/cell-test.cc create mode 100644 hbase-native-client/core/cell.cc create mode 100644 hbase-native-client/core/cell.h create mode 100644 hbase-native-client/core/cell_impl.cc create mode 100644 hbase-native-client/core/cell_impl.h diff --git a/hbase-native-client/core/cell-test.cc b/hbase-native-client/core/cell-test.cc new file mode 100644 index 0000000..05f0b1b --- /dev/null +++ b/hbase-native-client/core/cell-test.cc @@ -0,0 +1,196 @@ +/* + * 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 + +#include +#include + +#include "core/cell_impl.h" + +using namespace hbase; + +TEST (CellTest, CellFailureTest) { + + hbase::pb::CellType cell_type = hbase::pb::CellType::PUT; + + std::string row = "row"; + std::string family = "family"; + std::string column = "column"; + std::string value = "value"; + long timestamp = std::numeric_limits::max(); + std::string tags = ""; + const std::unique_ptr cell( + new CellImpl(row, family, column, timestamp, value, cell_type)); + + if (cell.get()) { + EXPECT_NE("row-value", cell.get()->Row()); + EXPECT_NE("family-value", cell.get()->Family()); + EXPECT_NE("column-value", cell.get()->Qualifier()); + EXPECT_NE("value-value", cell.get()->Value()); + EXPECT_NE(1234567890, cell.get()->Timestamp()); + EXPECT_NE(hbase::pb::CellType::MAXIMUM, cell.get()->Type()); + } +} + +TEST (CellTest, CellSuceessTest) { + + std::string row = "row-value"; + std::string family = "family-value"; + std::string column = "column-value"; + std::string value = "value-value"; + long timestamp = std::numeric_limits::max(); + hbase::pb::CellType cell_type = hbase::pb::CellType::PUT; + + const std::unique_ptr cell( + new CellImpl(row, family, column, timestamp, value, cell_type)); + + if (cell.get()) { + EXPECT_EQ(row, cell.get()->Row()); + EXPECT_EQ(family, cell.get()->Family()); + EXPECT_EQ(column, cell.get()->Qualifier()); + EXPECT_EQ(value, cell.get()->Value()); + EXPECT_EQ(timestamp, cell.get()->Timestamp()); + EXPECT_EQ(cell_type, cell.get()->Type()); + } +} + +TEST (CellTest, MultipleCellsTest) { + + std::vector cells; + for (int i = 0; i < 5; i++) { + std::string row = "row-value"; + std::string family = "family-value"; + std::string column = "column-value"; + std::string value = "value-value"; + long timestamp = std::numeric_limits::max(); + row += std::to_string(i); + value += std::to_string(i); + + hbase::pb::CellType cell_type = hbase::pb::CellType::PUT; + + const Cell *cell = new CellImpl(row, family, column, timestamp, value, + cell_type); + cells.push_back(cell); + } + int i = 0; + for (const auto cell : cells) { + //cell->Display(); + std::string row = "row-value"; + std::string value = "value-value"; + row += std::to_string(i); + value += std::to_string(i); + EXPECT_EQ(row, cell->Row()); + EXPECT_EQ("family-value", cell->Family()); + EXPECT_EQ("column-value", cell->Qualifier()); + EXPECT_EQ(value, cell->Value()); + EXPECT_EQ(std::numeric_limits::max(), cell->Timestamp()); + EXPECT_EQ(hbase::pb::CellType::PUT, cell->Type()); + i += 1; + } + for (const auto cell : cells) { + delete cell; + } + cells.clear(); + +} + +TEST (CellTest, CellRowTest) { + + std::string row = "only-row"; + std::string family = ""; + std::string column = ""; + std::string value = ""; + long timestamp = std::numeric_limits::max(); + hbase::pb::CellType cell_type = hbase::pb::CellType::PUT; + + const std::unique_ptr cell( + new CellImpl(row, family, column, timestamp, value, cell_type)); + if (cell.get()) { + EXPECT_EQ(row, cell.get()->Row()); + EXPECT_EQ(family, cell.get()->Family()); + EXPECT_EQ(column, cell.get()->Qualifier()); + EXPECT_EQ(value, cell.get()->Value()); + EXPECT_EQ(timestamp, cell.get()->Timestamp()); + EXPECT_EQ(cell_type, cell.get()->Type()); + } +} + +TEST (CellTest, CellRowFamilyTest) { + + std::string row = "only-row"; + std::string family = "only-family"; + std::string column = ""; + std::string value = ""; + long timestamp = std::numeric_limits::max(); + hbase::pb::CellType cell_type = hbase::pb::CellType::PUT; + + const std::unique_ptr cell( + new CellImpl(row, family, column, timestamp, value, cell_type)); + if (cell.get()) { + EXPECT_EQ(row, cell.get()->Row()); + EXPECT_EQ(family, cell.get()->Family()); + EXPECT_EQ(column, cell.get()->Qualifier()); + EXPECT_EQ(value, cell.get()->Value()); + EXPECT_EQ(timestamp, cell.get()->Timestamp()); + EXPECT_EQ(cell_type, cell.get()->Type()); + } +} + +TEST (CellTest, CellRowFamilyValueTest) { + + std::string row = "only-row"; + std::string family = "only-family"; + std::string column = ""; + std::string value = "only-value"; + long timestamp = std::numeric_limits::max(); + hbase::pb::CellType cell_type = hbase::pb::CellType::PUT; + + const std::unique_ptr cell( + new CellImpl(row, family, column, timestamp, value, cell_type)); + if (cell.get()) { + EXPECT_EQ(row, cell.get()->Row()); + EXPECT_EQ(family, cell.get()->Family()); + EXPECT_EQ(column, cell.get()->Qualifier()); + EXPECT_EQ(value, cell.get()->Value()); + EXPECT_EQ(timestamp, cell.get()->Timestamp()); + EXPECT_EQ(cell_type, cell.get()->Type()); + } +} + +TEST (CellTest, CellRowFamilyColumnValueTest) { + + std::string row = "only-row"; + std::string family = "only-family"; + std::string column = "only-column"; + std::string value = "only-value"; + long timestamp = std::numeric_limits::max(); + hbase::pb::CellType cell_type = hbase::pb::CellType::PUT; + + const std::unique_ptr cell( + new CellImpl(row, family, column, timestamp, value, cell_type)); + if (cell.get()) { + EXPECT_EQ(row, cell.get()->Row()); + EXPECT_EQ(family, cell.get()->Family()); + EXPECT_EQ(column, cell.get()->Qualifier()); + EXPECT_EQ(value, cell.get()->Value()); + EXPECT_EQ(timestamp, cell.get()->Timestamp()); + EXPECT_EQ(cell_type, cell.get()->Type()); + } +} diff --git a/hbase-native-client/core/cell.cc b/hbase-native-client/core/cell.cc new file mode 100644 index 0000000..2efad55 --- /dev/null +++ b/hbase-native-client/core/cell.cc @@ -0,0 +1,27 @@ +/* + * 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 "cell.h" + +namespace hbase { + +Cell::~Cell() { +} + +} /* namespace hbase */ diff --git a/hbase-native-client/core/cell.h b/hbase-native-client/core/cell.h new file mode 100644 index 0000000..0aed8cd --- /dev/null +++ b/hbase-native-client/core/cell.h @@ -0,0 +1,40 @@ +/* + * 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. + * + */ + +#pragma once + +#include + +#include "if/Cell.pb.h" + +namespace hbase { + +class Cell { + public: + virtual ~Cell(); + virtual const std::string &Row() const = 0; + virtual const std::string &Family() const = 0; + virtual const std::string &Qualifier() const = 0; + virtual const unsigned long &Timestamp() const = 0; + virtual const std::string &Value() const = 0; + virtual const hbase::pb::CellType &Type() const = 0; + virtual const long &SequenceId() const = 0; +}; + +} /* namespace hbase */ diff --git a/hbase-native-client/core/cell_impl.cc b/hbase-native-client/core/cell_impl.cc new file mode 100644 index 0000000..f02fd27 --- /dev/null +++ b/hbase-native-client/core/cell_impl.cc @@ -0,0 +1,85 @@ +/* + * 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 "cell_impl.h" + +namespace hbase { + +CellImpl::CellImpl(const std::string &row, const std::string &family, + const std::string &qualifier, const long ×tamp, + const std::string &value, + const hbase::pb::CellType &cell_type) + : row_(row), + family_(family), + qualifier_(qualifier), + timestamp_(timestamp), + cell_type_(cell_type), + value_(value), + sequence_id_(0){ + + if (0 == row.size()) + throw std::runtime_error("Row size should be greater than 0"); + + if (0 == family.size()) + throw std::runtime_error("Column family should be greater than 0"); + + if (0 >= timestamp) + throw std::runtime_error("Timestamp should be greater than 0"); +} + +CellImpl::~CellImpl() { + +} + +const std::string &CellImpl::Row() const { + + return row_; +} + +const std::string &CellImpl::Family() const { + + return family_; +} + +const std::string &CellImpl::Qualifier() const { + + return qualifier_; +} + +const unsigned long &CellImpl::Timestamp() const { + + return timestamp_; +} + +const std::string &CellImpl::Value() const { + + return value_; +} + +const hbase::pb::CellType &CellImpl::Type() const { + + return cell_type_; +} + +const long &CellImpl::SequenceId() const { + + return sequence_id_; +} + +} /* namespace hbase */ diff --git a/hbase-native-client/core/cell_impl.h b/hbase-native-client/core/cell_impl.h new file mode 100644 index 0000000..fc32fcb --- /dev/null +++ b/hbase-native-client/core/cell_impl.h @@ -0,0 +1,51 @@ +/* + * 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. + * + */ + +#pragma once + +#include "cell.h" + +namespace hbase { + +class CellImpl : public Cell { + public: + CellImpl(const std::string &row, const std::string &family, + const std::string &qualifier, const long ×tamp, + const std::string &value, const hbase::pb::CellType &cell_type); + virtual ~CellImpl(); + + const std::string &Row() const; + const std::string &Family() const; + const std::string &Qualifier() const; + const unsigned long &Timestamp() const; + const std::string &Value() const; + const hbase::pb::CellType &Type() const; + const long &SequenceId() const; + + private: + std::string row_; + std::string family_; + std::string qualifier_; + unsigned long timestamp_; + hbase::pb::CellType cell_type_; + std::string value_; + long sequence_id_; +}; + +} /* namespace hbase */ -- 1.8.3.1