From a2cf29f671012b72ca8df6532f97ac4b9cb22674 Mon Sep 17 00:00:00 2001 From: Sudeep Sunthankar Date: Thu, 19 Jan 2017 19:56:23 +1100 Subject: [PATCH] 1) CellScanner is an Abstract class now. 2) KeyValueCodec class implements the methods defined in CellScanner 3) We are passing IOBUF instead of copying the data from IOBUF diff --git a/hbase-native-client/core/BUCK b/hbase-native-client/core/BUCK index 0d1bc93..42a7112 100644 --- a/hbase-native-client/core/BUCK +++ b/hbase-native-client/core/BUCK @@ -36,6 +36,8 @@ cxx_library( "request_converter.h", "response_converter.h", "table.h", + "cell_scanner.h", + "keyvalue_codec.h", ], srcs=[ "cell.cc", @@ -51,6 +53,8 @@ cxx_library( "request_converter.cc", "response_converter.cc", "table.cc", + "cell_scanner.cc", + "keyvalue_codec.cc", ], deps=[ "//connection:connection", diff --git a/hbase-native-client/core/cell_scanner.cc b/hbase-native-client/core/cell_scanner.cc new file mode 100644 index 0000000..e81bfb1 --- /dev/null +++ b/hbase-native-client/core/cell_scanner.cc @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "core/cell_scanner.h" + +namespace hbase { + +CellScanner::CellScanner(std::shared_ptr cell_block_meta, + uint32_t cell_block_meta_start_offset, uint32_t cell_block_meta_length) + : cell_block_meta_(cell_block_meta), + cell_block_meta_start_offset_(cell_block_meta_start_offset), + cell_block_meta_length_(cell_block_meta_length) {} + +CellScanner::~CellScanner() {} + +uint32_t CellScanner::CellBlockMetaLength() const { return cell_block_meta_length_; } + +bool CellScanner::Advance() { return false; } + +const std::shared_ptr& CellScanner::Current() const { return current_cell_; } + +} /* namespace hbase */ diff --git a/hbase-native-client/core/cell_scanner.h b/hbase-native-client/core/cell_scanner.h new file mode 100644 index 0000000..0c3b47b --- /dev/null +++ b/hbase-native-client/core/cell_scanner.h @@ -0,0 +1,69 @@ +/* + * 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 +#include "core/cell.h" + +namespace hbase { +/** + * @brief Interface for parsing sequence of Cells + * + * This interface will be used for implementation of codec classes for decoding this sequence of + *cells. + * Sequence of cells are obtained from cell_meta_block. + * Various Codec classes can be used to implement the methods defined here + */ +class CellScanner { + public: + /** + * Constructor + */ + CellScanner(std::shared_ptr cell_block_meta, uint32_t cell_block_meta_start_offset_, + uint32_t cell_block_meta_length_); + virtual ~CellScanner(); + + /** + * @brief returns the total length of cell_meta_block + */ + uint32_t CellBlockMetaLength() const; + + /** + * @brief This method will be used to iterate the cells. Implementation will be defined in Codec + * classes. + * Typical usage will be :- + * while(cell_scanner.Advance()){ auto current_cell = cell_scanner.Current(); } + */ + virtual bool Advance() = 0; + + /** + * @brief returns the current cell + */ + const std::shared_ptr& Current() const; + + protected: + bool beyond_block_data_ = false; + uint32_t cell_block_meta_start_offset_ = 0; + std::shared_ptr cell_block_meta_ = nullptr; + uint32_t cell_block_meta_length_ = 0; + std::shared_ptr current_cell_ = nullptr; +}; + +} /* namespace hbase */ diff --git a/hbase-native-client/core/keyvalue_codec.cc b/hbase-native-client/core/keyvalue_codec.cc new file mode 100644 index 0000000..8521f60 --- /dev/null +++ b/hbase-native-client/core/keyvalue_codec.cc @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "core/keyvalue_codec.h" +#include +#include + +namespace hbase { + +KeyValueCodec::KeyValueCodec(std::shared_ptr cell_block, + uint32_t cell_block_start_offset, uint32_t cell_block_length) + : CellScanner(cell_block, cell_block_start_offset, cell_block_length) {} + +KeyValueCodec::~KeyValueCodec() {} + +Cell *KeyValueCodec::Decode(folly::io::Cursor &cursor) { + uint32_t key_length = cursor.readBE(); + uint32_t value_length = cursor.readBE(); + uint16_t row_length = cursor.readBE(); + std::string row = cursor.readFixedString(row_length); + uint8_t column_family_length = cursor.readBE(); + std::string column_family = cursor.readFixedString(column_family_length); + int qualifier_length = + key_length - (row_length + column_family_length + kHBaseSizeOfKeyInfrastructure_); + std::string column_qualifier = cursor.readFixedString(qualifier_length); + uint64_t timestamp = cursor.readBE(); + uint8_t key_type = cursor.readBE(); + std::string value = cursor.readFixedString(value_length); + + return new Cell(row, column_family, column_qualifier, timestamp, value, + static_cast(key_type)); +} + +bool KeyValueCodec::Advance() { + if (beyond_block_data_) { + return false; + } + + if (total_cell_size_ == cell_block_meta_length_) { + beyond_block_data_ = true; + return false; + } + + folly::io::Cursor cursor(cell_block_meta_.get()); + cursor.skip(cell_block_meta_start_offset_ + total_cell_size_); + uint32_t cell_size = cursor.readBE(); + current_cell_.reset(Decode(cursor)); + total_cell_size_ += cell_size + kHBaseSizeOfInt_; + return true; +} + +} /* namespace hbase */ diff --git a/hbase-native-client/core/keyvalue_codec.h b/hbase-native-client/core/keyvalue_codec.h new file mode 100644 index 0000000..d941fc2 --- /dev/null +++ b/hbase-native-client/core/keyvalue_codec.h @@ -0,0 +1,92 @@ +/* + * 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 +#include +#include "core/cell.h" +#include "core/cell_scanner.h" +namespace hbase { +/** + * @brief Interface for parsing sequence of Cells + * + * This interface will be used for implementation of codec classes for decoding this sequence of + *cells. + * Sequence of cells are obtained from cell_meta_block. + * Various Codec classes can be used to implement the methods defined here + */ +class KeyValueCodec : public CellScanner { + public: + /** + * Constructor + */ + KeyValueCodec(std::shared_ptr cell_block, uint32_t cell_block_start_offset, + uint32_t cell_block_length); + ~KeyValueCodec(); + + /** + * @brief Overridden from CellScanner. This method parses cell_meta_block and value of current + * cell can be obtained using cell_scanner.Current(); + */ + bool Advance(); + + private: + Cell* Decode(folly::io::Cursor& cursor); + /** + * Size of boolean in bytes + */ + const int kHBaseSizeOfBoolean_ = sizeof(uint8_t) / sizeof(uint8_t); + + /** + * Size of byte in bytes + */ + const uint8_t kHBaseSizeOfByte_ = kHBaseSizeOfBoolean_; + + /** + * Size of int in bytes + */ + const uint32_t kHBaseSizeOfInt_ = sizeof(uint32_t) / kHBaseSizeOfByte_; + + /** + * Size of long in bytes + */ + const uint64_t kHBaseSizeOfLong_ = sizeof(uint64_t) / kHBaseSizeOfByte_; + + /** + * Size of Short in bytes + */ + const uint16_t kHBaseSizeOfShort_ = sizeof(uint16_t) / kHBaseSizeOfByte_; + + const uint32_t kHBaseSizeOfKeyLength_ = kHBaseSizeOfInt_; + const uint32_t kHBaseSizeOfValueLength_ = kHBaseSizeOfInt_; + const uint16_t kHBaseSizeOfRowLength_ = kHBaseSizeOfShort_; + const uint8_t kHBaseSizeOfFamilyLength_ = kHBaseSizeOfByte_; + const uint64_t kHBaseSizeOfTimestamp_ = kHBaseSizeOfLong_; + const uint8_t kHBaseSizeOfKeyType_ = kHBaseSizeOfByte_; + const uint32_t kHBaseSizeOfTimestampAndKey_ = kHBaseSizeOfTimestamp_ + kHBaseSizeOfKeyType_; + const uint32_t kHBaseSizeOfKeyInfrastructure_ = + kHBaseSizeOfRowLength_ + kHBaseSizeOfFamilyLength_ + kHBaseSizeOfTimestampAndKey_; + const uint32_t kHBaseSizeOfKeyValueInfrastructure_ = + kHBaseSizeOfKeyLength_ + kHBaseSizeOfValueLength_; + uint32_t total_cell_size_ = 0; +}; + +} /* namespace hbase */ -- 1.8.3.1