From 86f7f9dc296decb320d0fe843c9c5e2469aeadc8 Mon Sep 17 00:00:00 2001 From: Sudeep Sunthankar Date: Fri, 27 May 2016 15:27:30 +1000 Subject: [PATCH] Delete object creation. Delete objects will be used to delete rowkeys from the hbase table --- hbase-native-client/Makefile | 147 +++++- hbase-native-client/Makefile.protos | 70 +++ hbase-native-client/core/bytes.cc | 539 +++++++++++++++++++++ hbase-native-client/core/bytes.h | 120 +++++ hbase-native-client/core/cell.cc | 203 ++++++++ hbase-native-client/core/cell.h | 72 +++ hbase-native-client/core/delete.cc | 126 +++++ hbase-native-client/core/delete.h | 48 ++ hbase-native-client/core/exception.cc | 121 +++++ hbase-native-client/core/exception.h | 64 +++ hbase-native-client/core/hconstants.cc | 191 ++++++++ hbase-native-client/core/hconstants.h | 233 +++++++++ hbase-native-client/core/key_value.cc | 503 +++++++++++++++++++ hbase-native-client/core/key_value.h | 188 +++++++ hbase-native-client/core/mutation.cc | 174 +++++++ hbase-native-client/core/mutation.h | 80 +++ .../core/operation_with_attributes.cc | 87 ++++ .../core/operation_with_attributes.h | 45 ++ hbase-native-client/core/permission.cc | 42 ++ hbase-native-client/core/permission.h | 44 ++ hbase-native-client/core/return_codes.h | 33 ++ hbase-native-client/core/tag.cc | 36 ++ hbase-native-client/core/tag.h | 39 ++ hbase-native-client/utils/Utils.cc | 42 ++ hbase-native-client/utils/Utils.h | 28 ++ 25 files changed, 3250 insertions(+), 25 deletions(-) create mode 100644 hbase-native-client/Makefile.protos create mode 100644 hbase-native-client/core/bytes.cc create mode 100644 hbase-native-client/core/bytes.h 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/delete.cc create mode 100644 hbase-native-client/core/delete.h create mode 100644 hbase-native-client/core/exception.cc create mode 100644 hbase-native-client/core/exception.h create mode 100644 hbase-native-client/core/hconstants.cc create mode 100644 hbase-native-client/core/hconstants.h create mode 100644 hbase-native-client/core/key_value.cc create mode 100644 hbase-native-client/core/key_value.h create mode 100644 hbase-native-client/core/mutation.cc create mode 100644 hbase-native-client/core/mutation.h create mode 100644 hbase-native-client/core/operation_with_attributes.cc create mode 100644 hbase-native-client/core/operation_with_attributes.h create mode 100644 hbase-native-client/core/permission.cc create mode 100644 hbase-native-client/core/permission.h create mode 100644 hbase-native-client/core/return_codes.h create mode 100644 hbase-native-client/core/tag.cc create mode 100644 hbase-native-client/core/tag.h create mode 100644 hbase-native-client/utils/Utils.cc create mode 100644 hbase-native-client/utils/Utils.h diff --git a/hbase-native-client/Makefile b/hbase-native-client/Makefile index 826233f..fda97df 100644 --- a/hbase-native-client/Makefile +++ b/hbase-native-client/Makefile @@ -1,37 +1,134 @@ -## -# 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 +# 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. # -# 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. -build: - $(shell buck build core/... ) +#use "gcc" to compile source files +CC:=g++ +LD:=g++ + +DEBUG_PATH = build/debug +RELEASE_PATH = build/release +PROTO_SRC_DIR = build/if +MODULES = connection core serde test-util utils +SRC_DIR = $(MODULES) +DEBUG_BUILD_DIR = $(addprefix $(DEBUG_PATH)/,$(MODULES)) +RELEASE_BUILD_DIR = $(addprefix $(RELEASE_PATH)/,$(MODULES)) +INCLUDE_DIR = . build/ -check: - $(shell buck test --all --no-results-cache ) +#flags to pass to the CPP compiler & linker +CPPFLAGS_DEBUG = -D_GLIBCXX_USE_CXX11_ABI=0 -g -Wall -std=c++14 -pedantic -fPIC +CPPFLAGS_RELEASE = -D_GLIBCXX_USE_CXX11_ABI=0 -DNDEBUG -O2 -Wall -std=c++14 -pedantic -fPIC +LDFLAGS = -lprotobuf -lzookeeper_mt -lsasl2 -lfolly -lwangle +LINKFLAG = -shared -doc: - $(shell doxygen hbase.doxygen > /dev/null ) +#define list of source files and object files +SRC = $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.cc)) +PROTOSRC = $(patsubst %.proto, $(addprefix build/,%.pb.cc),$(wildcard if/*.proto)) +DEPS = $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.h)) +PROTODEPS = $(patsubst %.proto, $(addprefix build/,%.pb.h),$(wildcard if/*.proto)) +DEBUG_OBJ = $(patsubst %.cc,$(DEBUG_PATH)/%.o,$(SRC)) +DEBUG_OBJ += $(patsubst %.cc,$(DEBUG_PATH)/%.o,$(PROTOSRC)) +RELEASE_OBJ = $(patsubst %.cc,$(RELEASE_PATH)/%.o,$(SRC)) +INCLUDES = $(addprefix -I,$(INCLUDE_DIR)) + +LIB_DIR = /usr/local +LIB_LIBDIR = $(LIB_DIR)/lib +LIB_INCDIR = $(LIB_DIR)/include +LIB_RELEASE=$(RELEASE_PATH)/libHbaseClient.so +ARC_RELEASE=$(RELEASE_PATH)/libHbaseClient.a +LIB_DEBUG=$(DEBUG_PATH)/libHbaseClient_d.so +ARC_DEBUG=$(DEBUG_PATH)/libHbaseClient_d.a + +vpath %.cc $(SRC_DIR) + +default : $(LIB_DEBUG) $(LIB_RELEASE) + +$(LIB_DEBUG): $(DEBUG_BUILD_DIR) +define make-goal-dbg +$1/%.o: %.cc $(DEPS) $(PROTODEPS) $(PROTOSRC) + $(CC) -c $$< -o $$@ $(CPPFLAGS_DEBUG) $(INCLUDES) +endef + +$(LIB_RELEASE): $(RELEASE_BUILD_DIR) +define make-goal-rel +$1/%.o: %.cc $(DEPS) $(PROTODEPS) $(PROTOSRC) + $(CC) -c $$< -o $$@ $(CPPFLAGS_RELEASE) $(INCLUDES) +endef + +.PHONY: all clean install default help + +build: checkdirs protos $(LIB_DEBUG) $(LIB_RELEASE) $(ARC_DEBUG) $(ARC_RELEASE) + +checkdirs: $(DEBUG_BUILD_DIR) $(RELEASE_BUILD_DIR) $(PROTO_SRC_DIR) + +protos: createprotosrc + @make all -f Makefile.protos + +createprotosrc: $(PROTO_SRC_DIR) + @protoc --proto_path=if --cpp_out=$(PROTO_SRC_DIR) if/*.proto + +install: + cp $(LIB_RELEASE) $(LIB_LIBDIR)/libHbaseClient.so + cp $(ARC_RELEASE) $(LIB_LIBDIR)/libHbaseClient.a + cp $(LIB_DEBUG) $(LIB_LIBDIR)/libHbaseClient_d.so + cp $(ARC_DEBUG) $(LIB_LIBDIR)/libHbaseClient_d.a + +$(PROTO_SRC_DIR): + @mkdir -p $@ +$(DEBUG_BUILD_DIR): + @mkdir -p $@ + +$(RELEASE_BUILD_DIR): + @mkdir -p $@ + +$(ARC_DEBUG): $(DEBUG_OBJ) + ar rcs $@ $^ + +$(ARC_RELEASE): $(RELEASE_OBJ) + ar rcs $@ $^ + +$(LIB_RELEASE): $(RELEASE_OBJ) + $(LD) $(LINKFLAG) -o $@ $(LDFLAGS) $(RELEASE_OBJ) + +$(LIB_DEBUG): $(DEBUG_OBJ) + $(LD) $(LINKFLAG) -o $@ $(LDFLAGS) $(DEBUG_OBJ) + +#to clean re-compilable files clean: - $(shell rm -rf docs ) + @rm -rf $(DEBUG_BUILD_DIR) $(RELEASE_BUILD_DIR) $(LIB_RELEASE) $(LIB_DEBUG) $(ARC_RELEASE) $(ARC_DEBUG) docs buck-out build + +$(foreach bdir,$(DEBUG_BUILD_DIR), $(eval $(call make-goal-dbg,$(bdir)))) + +$(foreach bdir,$(RELEASE_BUILD_DIR),$(eval $(call make-goal-rel,$(bdir)))) + +check: + $(shell buck test --all --no-results-cache) + +doc: + $(shell doxygen hbase.doxygen > /dev/null) help: @echo "Available targets:" @echo "" - @echo " build : will build everything." - @echo " clean : will remove the docs folder" - @echo " check : will test everything." + @echo " build : will build everything." + @echo " clean : will remove the docs folder, object files and local libraries" + @echo " install : will copy the libs to $(LIB_LIBDIR). super user priviliege would be required." + @echo " check : will test everything." + @echo " protos : will build the corresponding sources for protobufs present in if/ directory." all: build doc check + diff --git a/hbase-native-client/Makefile.protos b/hbase-native-client/Makefile.protos new file mode 100644 index 0000000..223eb05 --- /dev/null +++ b/hbase-native-client/Makefile.protos @@ -0,0 +1,70 @@ +# 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. +# + +#use "gcc" to compile source files +CC:=g++ +LD:=g++ + +DEBUG_PATH = build/debug +RELEASE_PATH = build/release +MODULES = build/if +SRC_DIR = $(MODULES) +DEBUG_BUILD_DIR = $(addprefix $(DEBUG_PATH)/,$(MODULES)) +RELEASE_BUILD_DIR = $(addprefix $(RELEASE_PATH)/,$(MODULES)) +INCLUDE_DIR = . + +#flags to pass to the CPP compiler & linker +CPPFLAGS_DEBUG = -D_GLIBCXX_USE_CXX11_ABI=0 -g -Wall -std=c++14 -pedantic -fPIC +CPPFLAGS_RELEASE = -D_GLIBCXX_USE_CXX11_ABI=0 -DNDEBUG -O2 -Wall -std=c++14 -pedantic -fPIC + +#define list of source files and object files +SRC = $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.cc)) +DEPS = $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.h)) +DEBUG_OBJ = $(patsubst %.cc,$(DEBUG_PATH)/%.o,$(SRC)) +RELEASE_OBJ = $(patsubst %.cc,$(RELEASE_PATH)/%.o,$(SRC)) +INCLUDES = $(addprefix -I,$(INCLUDE_DIR)) + +vpath %.cc $(SRC_DIR) + +$(DEBUG_OBJ): $(DEBUG_BUILD_DIR) +define make-goal-dbg +$1/%.o: %.cc $(DEPS) + $(CC) -c $$< -o $$@ $(CPPFLAGS_DEBUG) $(INCLUDES) +endef + +$(RELEASE_OBJ): $(RELEASE_BUILD_DIR) +define make-goal-rel +$1/%.o: %.cc $(DEPS) + $(CC) -c $$< -o $$@ $(CPPFLAGS_RELEASE) $(INCLUDES) +endef + +$(DEBUG_BUILD_DIR): + @mkdir -p $@ + +$(RELEASE_BUILD_DIR): + @mkdir -p $@ + +.PHONY: all clean + +all: $(DEBUG_OBJ) $(RELEASE_OBJ) + +clean: + @rm -rf $(DEBUG_OBJ) $(RELEASE_OBJ) + +$(foreach bdir,$(DEBUG_BUILD_DIR), $(eval $(call make-goal-dbg,$(bdir)))) + +$(foreach bdir,$(RELEASE_BUILD_DIR),$(eval $(call make-goal-rel,$(bdir)))) diff --git a/hbase-native-client/core/bytes.cc b/hbase-native-client/core/bytes.cc new file mode 100644 index 0000000..384b2b6 --- /dev/null +++ b/hbase-native-client/core/bytes.cc @@ -0,0 +1,539 @@ +/* + * 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 "bytes.h" + +#include +#include +#include + +#include "exception.h" + +/** + * Size of boolean in bytes + */ +const int Bytes::SIZEOF_BOOLEAN = sizeof(BYTE_TYPE) / sizeof(BYTE_TYPE); +/** + * Size of byte in bytes + */ +const int Bytes::SIZEOF_BYTE = Bytes::SIZEOF_BOOLEAN; +/** + * Size of char in bytes + */ +const int Bytes::SIZEOF_CHAR = sizeof(short) / sizeof(BYTE_TYPE); +/** + * Size of double in bytes + */ +const int Bytes::SIZEOF_DOUBLE = sizeof(double) / sizeof(BYTE_TYPE); +/** + * Size of float in bytes + */ +const int Bytes::SIZEOF_FLOAT = sizeof(float) / sizeof(BYTE_TYPE); +/** + * Size of int in bytes + */ +const int Bytes::SIZEOF_INT = sizeof(int) / sizeof(BYTE_TYPE); +/** + * Size of long in bytes + */ +const int Bytes::SIZEOF_LONG = sizeof(long) / sizeof(BYTE_TYPE); +/** + * Size of short in bytes + */ +const int Bytes::SIZEOF_SHORT = sizeof(short) / sizeof(BYTE_TYPE); + +const char Bytes::HEX_CHARS[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', + '9', 'a', 'b', 'c', 'd', 'e', 'f', '\0' }; + +Bytes::Bytes() + : offset_(-1), + length_(0) { + + this->byte_array_ = {}; +} + +Bytes::Bytes(const BYTE_ARRAY &bytes) + : Bytes(bytes, 0, bytes.size()) { + +} + +Bytes::Bytes(const BYTE_ARRAY &bytes, const int &offset, const int &length) { + this->byte_array_ = bytes; + this->offset_ = offset; + this->length_ = length; +} + +Bytes::Bytes(const Bytes &cbytes) { + this->byte_array_ = cbytes.byte_array_; + this->offset_ = cbytes.offset_; + this->length_ = cbytes.length_; +} + +Bytes& Bytes::operator=(const Bytes &cbytes) { + this->byte_array_ = cbytes.byte_array_; + this->offset_ = cbytes.offset_; + this->length_ = cbytes.length_; + return *this; +} + +const BYTE_ARRAY &Bytes::Get() const { + return this->byte_array_; +} + +void Bytes::Set(const BYTE_ARRAY &bytes) { + + this->Set(bytes, 0, bytes.size()); +} + +void Bytes::Set(const BYTE_ARRAY &bytes, const int &offset, const int &length) { + + this->byte_array_ = bytes; + this->offset_ = offset; + this->length_ = length; +} + +const int &Bytes::GetOffset() { + + return this->offset_; +} + +const int &Bytes::GetLength() { + + return this->length_; +} + +Bytes::~Bytes() { + // TODO Auto-generated destructor stub +} + +bool Bytes::ByteCompare(const BYTE_TYPE &left, const BYTE_TYPE &right) { + + return (left == right); +} + +void Bytes::DisplayBytes(const BYTE_ARRAY &bytes) { + + std::stringstream bytes_str; + bytes_str.str(""); + bytes_str << "Byte Array["; + for (uint i = 0; i < bytes.size(); ++i) { + bytes_str << bytes[i]; + if (i != bytes.size() - 1) + bytes_str << " "; + } + bytes_str << "]"; + DLOG(INFO)<< bytes_str.str(); + bytes_str.str(""); +} + +size_t Bytes::StrLen(const char *s) { + if (!s || NULL == s) + return 0; + else + return ::strlen(s); +} + +int Bytes::ToBytes(const std::string &str_to_bytes, BYTE_ARRAY &bytes) { + + if (str_to_bytes.size() > 0) + bytes.insert(bytes.end(), str_to_bytes.begin(), str_to_bytes.end()); + + return bytes.size(); +} + +int Bytes::ToBytes(const int &val, BYTE_ARRAY &bytes) { + + for (int i = 0, j = Bytes::SIZEOF_INT - 1; i < Bytes::SIZEOF_INT; i++, j--) { + bytes.push_back(static_cast((val >> (j * 8)) & 0xFF)); + } + return bytes.size(); +} + +int Bytes::ToBytes(const short &val, BYTE_ARRAY &bytes) { + + for (int i = 0, j = Bytes::SIZEOF_SHORT - 1; i < Bytes::SIZEOF_SHORT; + i++, j--) { + bytes.push_back(static_cast((val >> (j * 8)) & 0xFF)); + } + + return bytes.size(); +} + +int Bytes::ToBytes(const long &val, BYTE_ARRAY &bytes) { + for (int i = 0, j = Bytes::SIZEOF_LONG - 1; i < Bytes::SIZEOF_LONG; + i++, j--) { + bytes.push_back(static_cast((val >> (j * 8)) & 0xFF)); + } + return bytes.size(); +} + +int Bytes::ToString(const BYTE_ARRAY &byte, std::string &bytes_to_str) { + + if (0 == byte.size()) + bytes_to_str = ""; + else + bytes_to_str.insert(bytes_to_str.end(), byte.begin(), byte.end()); + return bytes_to_str.size(); + +} + +std::string Bytes::ToString(const BYTE_ARRAY &byte) { + + return Bytes::ToString(byte, 0, byte.size()); +} + +std::string Bytes::ToString(const BYTE_ARRAY &byte, const int &offset) { + + return Bytes::ToString(byte, offset, byte.size()); +} + +std::string Bytes::ToString(const BYTE_ARRAY &byte, const int &offset, + const int &length) { + + std::string bytes_to_str(""); + if (0 == byte.size()) + bytes_to_str = ""; + else + bytes_to_str.insert(bytes_to_str.end(), byte.begin(), byte.end()); + return bytes_to_str; +} + +bool Bytes::Equals(const BYTE_ARRAY &left, const BYTE_ARRAY &right) { + + if (left.size() != right.size()) + return false; + if (std::equal(left.begin(), left.end(), right.begin(), Bytes::ByteCompare)) + return true; + else + return false; + +} + +int Bytes::CopyByteArray(const BYTE_ARRAY &ip_bytes, BYTE_ARRAY &op_bytes, + const int &offset, const int &length) { + int ip_size = ip_bytes.size(); + + if (0 == ip_size) + throw HbaseException("Can't copy empty byte array"); + + if (offset < 0) + throw HbaseException("Offset must be >= 0"); + + if (0 == length) { + throw HbaseException("Cant copy 0 bytes"); + } + + int max_bytes_can_copy = ip_size - offset; + + if (length > max_bytes_can_copy) { + std::stringstream str_error; + str_error.str(""); + str_error << "Cant copy " << length << " bytes as only " + << max_bytes_can_copy << " can be copied." << std::endl; + throw HbaseException(str_error.str()); + } + + op_bytes.resize(length); + BYTE_ARRAY::const_iterator start_itr = ip_bytes.begin() + offset; + BYTE_ARRAY::const_iterator end_itr = start_itr + length; + std::copy(start_itr, end_itr, op_bytes.begin()); + + return op_bytes.size(); +} + +int Bytes::PutInt(BYTE_ARRAY &bytes, const int &offset, const int &val) { + + if ((bytes.size() - offset) < Bytes::SIZEOF_INT) { + std::stringstream str_error; + str_error.str(""); + str_error << "Not enough room to put an int of size " << Bytes::SIZEOF_INT + << " at offset " << offset << " in a " << bytes.size() + << " byte array."; + throw HbaseException(str_error.str()); + } + + for (int i = 0, j = Bytes::SIZEOF_INT - 1; i < Bytes::SIZEOF_INT; i++, j--) { + bytes[offset + i] = static_cast((val >> (j * 8)) & 0xFF); + } + + return offset + Bytes::SIZEOF_INT; +} + +int Bytes::PutShort(BYTE_ARRAY &bytes, const int &offset, const short &val) { + + if ((bytes.size() - offset) < Bytes::SIZEOF_SHORT) { + std::stringstream str_error; + str_error.str(""); + str_error << "Not enough room to put a short of size " + << Bytes::SIZEOF_SHORT << " at offset " << offset << " in a " + << bytes.size() << " byte array."; + throw HbaseException(str_error.str()); + } + + for (int i = 0, j = Bytes::SIZEOF_SHORT - 1; i < Bytes::SIZEOF_SHORT; + i++, j--) { + bytes[offset + i] = static_cast((val >> (j * 8)) & 0xFF); + } + + return offset + Bytes::SIZEOF_SHORT; +} + +int Bytes::PutLong(BYTE_ARRAY &bytes, const int &offset, const long &val) { + + if ((bytes.size() - offset) < Bytes::SIZEOF_LONG) { + std::stringstream str_error; + str_error.str(""); + str_error << "Not enough room to put a long of size " << Bytes::SIZEOF_LONG + << " at offset " << offset << " in a " << bytes.size() + << " byte array."; + throw HbaseException(str_error.str()); + } + + for (int i = 0, j = Bytes::SIZEOF_LONG - 1; i < Bytes::SIZEOF_LONG; + i++, j--) { + bytes[offset + i] = static_cast((val >> (j * 8)) & 0xFF); + } + + return offset + SIZEOF_LONG; +} + +int Bytes::PutByte(BYTE_ARRAY &bytes, const int &offset, const BYTE_TYPE &val) { + + if ((bytes.size() - offset) < Bytes::SIZEOF_BYTE) { + std::stringstream str_error; + str_error.str(""); + str_error << "Not enough room to put a byte of size " << Bytes::SIZEOF_BYTE + << " at offset " << offset << " in a " << bytes.size() + << " byte array."; + throw HbaseException(str_error.str()); + } + + bytes[offset] = static_cast((val)); + return offset + Bytes::SIZEOF_BYTE; +} + +int Bytes::PutBytes(BYTE_ARRAY &dst_bytes, const int &dst_offset, + const BYTE_ARRAY &src_bytes, const int &src_offset, + const int &src_num_bytes) { + + if (src_num_bytes <= 0) { + std::stringstream str_error; + str_error.str(""); + str_error << "Cant add " << src_num_bytes << std::endl; + throw HbaseException(str_error.str()); + } + if (src_num_bytes > static_cast(src_bytes.size())) { + std::stringstream str_error; + str_error.str(""); + str_error << "Cant add " << src_num_bytes + << " bytes from source byte array size of " << src_bytes.size() + << " bytes."; + throw HbaseException(str_error.str()); + } + if (static_cast(dst_bytes.size()) < src_num_bytes) { + + std::stringstream str_error; + str_error.str(""); + str_error << "Cant add " << src_num_bytes + << " bytes to destinaion byte array size of " << dst_bytes.size() + << " bytes."; + throw HbaseException(str_error.str()); + } + + BYTE_ARRAY::const_iterator start_itr = src_bytes.begin() + src_offset; + BYTE_ARRAY::const_iterator end_itr = start_itr + src_num_bytes; + BYTE_ARRAY::iterator start_dest_itr = dst_bytes.begin() + dst_offset; + std::copy(start_itr, end_itr, start_dest_itr); + + return dst_offset + src_num_bytes; +} + +int Bytes::ToInt(const BYTE_ARRAY &bytes) { + return Bytes::ToInt(bytes, 0, SIZEOF_INT); +} + +int Bytes::ToInt(const BYTE_ARRAY &bytes, const int &offset) { + return Bytes::ToInt(bytes, offset, SIZEOF_INT); + +} + +int Bytes::ToInt(const BYTE_ARRAY &bytes, const int &offset, + const int &length) { + int int_val = 0; + if (length != SIZEOF_INT + || static_cast(offset + length) > bytes.size()) { + throw "WrongLengthOrOffset"; //explainWrongLengthOrOffset(bytes, offset, length, SIZEOF_INT); + } else { + for (int i = offset, j = Bytes::SIZEOF_INT - 1; i < (offset + length); + i++, j--) { + int_val |= (bytes[i] & 0XFF) << (j * 8); + } + } + return int_val; +} + +short Bytes::ToShort(const BYTE_ARRAY &bytes) { + return Bytes::ToShort(bytes, 0, SIZEOF_SHORT); +} + +short Bytes::ToShort(const BYTE_ARRAY &bytes, const int &offset) { + return Bytes::ToShort(bytes, offset, SIZEOF_SHORT); + +} + +short Bytes::ToShort(const BYTE_ARRAY &bytes, const int &offset, + const int &length) { + short short_val = 0; + if (length != SIZEOF_SHORT + || static_cast(offset + length) > bytes.size()) { + throw "WrongLengthOrOffset"; //explainWrongLengthOrOffset(bytes, offset, length, SIZEOF_SHORT); + } else { + for (int i = offset, j = Bytes::SIZEOF_SHORT - 1; i < (offset + length); + i++, j--) { + short_val |= (bytes[i] & 0XFF) << (j * 8); + } + } + return short_val; +} + +long Bytes::ToLong(const BYTE_ARRAY &bytes) { + return Bytes::ToLong(bytes, 0, SIZEOF_LONG); +} + +long Bytes::ToLong(const BYTE_ARRAY &bytes, const int &offset) { + return Bytes::ToLong(bytes, offset, SIZEOF_LONG); + +} + +long Bytes::ToLong(const BYTE_ARRAY &bytes, const int &offset, + const int &length) { + + long long_val = 0L; + if (length != SIZEOF_LONG + || static_cast(offset + length) > bytes.size()) { + throw "WrongLengthOrOffset"; //explainWrongLengthOrOffset(bytes, offset, length, SIZEOF_SHORT); + } else { + for (int i = offset, j = Bytes::SIZEOF_LONG - 1; i < (offset + length); + i++, j--) { + long_val <<= 8; + long_val ^= bytes[i] & 0xFF; + } + } + /*const char *pCurrent = &bytes[offset]; + PocoXXXX::UInt64 *pts = (PocoXXXX::UInt64*) pCurrent; + long_val = *pts; + long_val = PocoXXXX::ByteOrder::flipBytes(long_val); + */ + return long_val; +} + +unsigned long Bytes::ToULong(const BYTE_ARRAY &bytes) { + return Bytes::ToULong(bytes, 0, SIZEOF_LONG); +} + +unsigned long Bytes::ToULong(const BYTE_ARRAY &bytes, const int &offset) { + return Bytes::ToULong(bytes, offset, SIZEOF_LONG); + +} + +unsigned long Bytes::ToULong(const BYTE_ARRAY &bytes, const int &offset, + const int &length) { + + unsigned long long_val = 0L; + //const char *pCurrent = &bytes[offset]; + if (length != SIZEOF_LONG + || static_cast(offset + length) > bytes.size()) { + throw "WrongLengthOrOffset"; //explainWrongLengthOrOffset(bytes, offset, length, SIZEOF_SHORT); + } else { + for (int i = offset, j = Bytes::SIZEOF_LONG - 1; i < (offset + length); + i++, j--) { + long_val <<= 8; + long_val ^= bytes[i] & 0xFF; + } + } + /* + DLOG(INFO)<< "SIZEOF_LONG is " << SIZEOF_LONG; + + DLOG(INFO)<< "long_val_tmp is " << long_val_tmp; + + PocoXXXX::UInt64 *pts = (PocoXXXX::UInt64*) pCurrent; + long_val = *pts; + long_val = PocoXXXX::ByteOrder::flipBytes(long_val); + DLOG(INFO)<< "long_val is " << long_val; + */ + return long_val; +} + +std::string Bytes::ToHex(const BYTE_ARRAY &bytes) { + return Bytes::ToHex(bytes, 0, bytes.size()); +} + +std::string Bytes::ToHex(const BYTE_ARRAY &bytes, const int &offset, + const int &length) { + + if (length > (std::numeric_limits::max() / 2)) { + + throw HbaseException( + "Illegal Argument Exception while converting byte array to hex, Check the length passed"); + } + + int num_chars = length * 2; + std::string hex_str; + hex_str.resize(num_chars); + for (int i = 0; i < num_chars; i += 2) { + BYTE_TYPE d = bytes[offset + i / 2]; + hex_str[i] = HEX_CHARS[(d >> 4) & 0x0F]; + hex_str[i + 1] = HEX_CHARS[d & 0x0F]; + } + return hex_str; +} + +BYTE_ARRAY Bytes::ToBytes(const char *charstr_to_bytes) { + + std::string str_to_bytes(""); + if (nullptr != charstr_to_bytes) + str_to_bytes = charstr_to_bytes; + return Bytes::ToBytes(str_to_bytes); +} + +BYTE_ARRAY Bytes::ToBytes(const std::string &str_to_bytes, const bool &change_case_tolower) { + + BYTE_ARRAY bytes; + if (str_to_bytes.size() > 0){ + if(change_case_tolower){ + std::string lower_str = str_to_bytes; + std::transform(lower_str.begin(), lower_str.end(), lower_str.begin(), ::tolower); + bytes.insert(bytes.end(), lower_str.begin(), lower_str.end()); + }else{ + bytes.insert(bytes.end(), str_to_bytes.begin(), str_to_bytes.end()); + } + } + + return bytes; +} + +BYTE_ARRAY Bytes::ToBytes(const bool &val) { + + BYTE_ARRAY bytes; + for (int i = 0, j = Bytes::SIZEOF_BOOLEAN; i < Bytes::SIZEOF_BOOLEAN; + i++, j--) { + bytes.push_back(static_cast((val >> (j * 8)) & 0xFF)); + } + + return bytes; +} diff --git a/hbase-native-client/core/bytes.h b/hbase-native-client/core/bytes.h new file mode 100644 index 0000000..16952de --- /dev/null +++ b/hbase-native-client/core/bytes.h @@ -0,0 +1,120 @@ +/* + * 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 + +#include +#include + +using byte = char; +using BYTE_TYPE = char; +using BYTE_ARRAY = std::vector; +using ByteBuffer = std::vector; + +class Bytes { + public: + + static const int SIZEOF_BOOLEAN; + static const int SIZEOF_BYTE; + static const int SIZEOF_CHAR; + static const int SIZEOF_DOUBLE; + static const int SIZEOF_FLOAT; + static const int SIZEOF_INT; + static const int SIZEOF_LONG; + static const int SIZEOF_SHORT; + static const char HEX_CHARS[]; + + Bytes(); + Bytes(const Bytes &bytes); + Bytes& operator= (const Bytes &bytes); + + Bytes(const BYTE_ARRAY &bytes); + Bytes(const BYTE_ARRAY &bytes, const int &offset, const int &length); + const BYTE_ARRAY &Get() const; + void Set(const BYTE_ARRAY &bytes); + void Set(const BYTE_ARRAY &bytes, const int &offset, const int &length); + const int &GetOffset(); + const int &GetLength(); + + virtual ~Bytes(); + + static void DisplayBytes(const BYTE_ARRAY &bytes); + static size_t StrLen(const char *); + + static BYTE_ARRAY ToBytes(const std::string &val, const bool &change_case_tolower = false); + static BYTE_ARRAY ToBytes(const char *charstr_to_bytes); + static BYTE_ARRAY ToBytes(const bool &val); + + + static int ToBytes(const std::string &val, BYTE_ARRAY &bytes); + static int ToBytes(const int &val, BYTE_ARRAY &bytes); + static int ToBytes(const short &val, BYTE_ARRAY &bytes); + static int ToBytes(const long &val, BYTE_ARRAY &bytes); + + static int ToString(const BYTE_ARRAY &byte, std::string &bytes_to_str); + static std::string ToString(const BYTE_ARRAY &byte); + static std::string ToString(const BYTE_ARRAY &bytes, const int &offset); + static std::string ToString(const BYTE_ARRAY &byte, const int &offset, const int &length); + static char ToByte(const BYTE_ARRAY &bytes); + + static int ToInt(const BYTE_ARRAY &bytes); + static int ToInt(const BYTE_ARRAY &bytes, const int &offset); + static int ToInt(const BYTE_ARRAY &bytes, const int &offset, const int &length); + + static short ToShort(const BYTE_ARRAY &bytes); + static short ToShort(const BYTE_ARRAY &bytes, const int &offset); + static short ToShort(const BYTE_ARRAY &bytes, const int &offset, const int &length); + + static long ToLong(const BYTE_ARRAY &bytes); + static long ToLong(const BYTE_ARRAY &bytes, const int &offset); + static long ToLong(const BYTE_ARRAY &bytes, const int &offset, const int &length); + + static unsigned long ToULong(const BYTE_ARRAY &bytes); + static unsigned long ToULong(const BYTE_ARRAY &bytes, const int &offset); + static unsigned long ToULong(const BYTE_ARRAY &bytes, const int &offset, const int &length); + + static float ToFloat(const BYTE_ARRAY &bytes); + static double ToDouble(const BYTE_ARRAY &bytes); + + static std::string ToHex(const BYTE_ARRAY &bytes); + static std::string ToHex(const BYTE_ARRAY &bytes, const int &offset, const int &length); + + static int PutInt(BYTE_ARRAY &bytes, const int &offset, const int &val); + static int PutShort(BYTE_ARRAY &bytes, const int &offset, const short &val); + static int PutLong(BYTE_ARRAY &bytes, const int &offset, const long &val); + static int PutByte(BYTE_ARRAY &bytes, const int &offset, const BYTE_TYPE &val); + static int PutBytes(BYTE_ARRAY &bytes, const int &from_index, const BYTE_ARRAY &bytes_to_put, + const int &offset, const int &num_bytes); + + + static bool Equals(const BYTE_ARRAY &left, const BYTE_ARRAY &right); + static int CopyByteArray( const BYTE_ARRAY &ip_bytes, BYTE_ARRAY &op_bytes, + const int &offset, const int &length); + private: + static bool ByteCompare(const BYTE_TYPE &left, const BYTE_TYPE &right); + + BYTE_ARRAY byte_array_; + int offset_; + int length_; +}; diff --git a/hbase-native-client/core/cell.cc b/hbase-native-client/core/cell.cc new file mode 100644 index 0000000..1731d77 --- /dev/null +++ b/hbase-native-client/core/cell.cc @@ -0,0 +1,203 @@ +/* + * 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" +#include +#include "../utils/Utils.h" + +Cell::~Cell() { + if (nullptr != key_value_) + delete key_value_; +} + +Cell::Cell(const std::string &row, const std::string &family, const std::string &qualifier, + const long ×tamp, const std::string &value, const std::string &tags, const CellType::cellType &cellType): + row_(row), family_(family), qualifier_(qualifier), timestamp_(timestamp), value_(value), tags_(tags), + cellType_(cellType), key_value_(nullptr) { + this->qualifier_ = (0 == this->qualifier_.length() ? "" : this->qualifier_); + this->value_ = (0 == this->value_.length() ? "" : this->value_); + + key_value_ = new KeyValue( Bytes::ToBytes(row), Bytes::ToBytes(family), + (0 == this->qualifier_.length() ? HBaseConstants::HConstants::EMPTY_BYTE_ARRAY : Bytes::ToBytes(qualifier)), + timestamp, static_cast(cellType), + (0 == this->value_.length() ? HBaseConstants::HConstants::EMPTY_BYTE_ARRAY : Bytes::ToBytes(value))); + +} + +Cell::Cell(): row_(""), family_(""), qualifier_(""), timestamp_(HBaseConstants::HConstants::LATEST_TIMESTAMP), + value_(""), tags_(""), cellType_(CellType::UNKNOWN), key_value_(nullptr) { + +} + +Cell::Cell(const Cell &cell) { + + this->row_ = cell.row_; + this->family_ = cell.family_; + this->qualifier_ = cell.qualifier_; + this->timestamp_ = cell.timestamp_; + this->value_ = cell.value_; + this->tags_ = cell.tags_; + this->cellType_ = cell.cellType_; + if (nullptr != cell.key_value_) + this->key_value_ = new KeyValue(*cell.key_value_); + else + this->key_value_ = nullptr; +} + +Cell& Cell::operator= (const Cell &cell) { + + this->row_ = cell.row_; + this->family_ = cell.family_; + this->qualifier_ = cell.qualifier_; + this->timestamp_ = cell.timestamp_; + this->value_ = cell.value_; + this->tags_ = cell.tags_; + this->cellType_ = cell.cellType_; + if (nullptr!= cell.key_value_) + this->key_value_ = new KeyValue(*cell.key_value_); + else + this->key_value_ = nullptr; + return *this; + +} +void Cell::Display() { + + LOG(INFO) << "Cell Contents: " + << " Row: " << (row_.size() > 0 ? row_ : "") + << "; Family: " << (family_.size() > 0 ? family_ : "") + << "; Qualifer: " << (qualifier_.size() > 0 ? qualifier_ : "") + << "; Tags: " << (tags_.size() > 0 ? tags_ : "") + << "; Value: " << (value_.size() > 0 ? value_ : "") + << "; Timestamp: " << timestamp_ + << "; CellType: " << static_cast(cellType_); +} + +const CellType::cellType &Cell::TypeByte() { + return this->cellType_; +} + +const std::string &Cell::Row() { + return this->row_; +} + +const std::string &Cell::Family() { + return this->family_; +} + +const std::string &Cell::Qualifier() { + return this->qualifier_; +} + +const std::string &Cell::Value() { + return this->value_; +} + +const long &Cell::Timestamp() { + return this->timestamp_; +} + + +void Cell::DisplayKeyValueDetails() { + + BYTE_ARRAY tmp_rowbytes; + Bytes::CopyByteArray( this->key_value_->GetRowArray(), tmp_rowbytes, this->key_value_->GetRowOffset(), this->key_value_->GetRowLength()); + + BYTE_ARRAY tmp_fambytes; + Bytes::CopyByteArray( this->key_value_->GetFamilyArray(), tmp_fambytes, this->key_value_->GetFamilyOffset(), this->key_value_->GetFamilyLength()); + + + BYTE_ARRAY tmp_qualbytes; + Bytes::CopyByteArray( this->key_value_->GetQualifierArray(), tmp_qualbytes, this->key_value_->GetQualifierOffset(), this->key_value_->GetQualifierLength()); + + + BYTE_ARRAY tmp_valbytes; + Bytes::CopyByteArray( this->key_value_->GetValueArray(), tmp_valbytes, this->key_value_->GetValueOffset(), this->key_value_->GetValueLength()); + + + Bytes::DisplayBytes(tmp_rowbytes); + Bytes::DisplayBytes(tmp_fambytes); + Bytes::DisplayBytes(tmp_qualbytes); + DLOG(INFO) << "Timestamp " << this->key_value_->GetTimestamp(); + DLOG(INFO) << "Type " << static_cast(this->key_value_->GetTypeByte()); + Bytes::DisplayBytes(tmp_valbytes); +} + +Cell* Cell::CreateCell(const BYTE_ARRAY &bytes) { + + KeyValue *key_value = new KeyValue(bytes); + + BYTE_ARRAY tmp_rowbytes; + if (key_value->GetRowLength()) + Bytes::CopyByteArray( key_value->GetRowArray(), tmp_rowbytes, key_value->GetRowOffset(), key_value->GetRowLength()); + + BYTE_ARRAY tmp_fambytes; + if(key_value->GetFamilyLength()) + Bytes::CopyByteArray( key_value->GetFamilyArray(), tmp_fambytes, key_value->GetFamilyOffset(), key_value->GetFamilyLength()); + + BYTE_ARRAY tmp_qualbytes; + if (key_value->GetQualifierLength()) + Bytes::CopyByteArray( key_value->GetQualifierArray(), tmp_qualbytes, key_value->GetQualifierOffset(), key_value->GetQualifierLength()); + + BYTE_ARRAY tmp_valbytes; + if (key_value->GetValueLength()) + Bytes::CopyByteArray( key_value->GetValueArray(), tmp_valbytes, key_value->GetValueOffset(), key_value->GetValueLength()); + + long timestamp = key_value->GetTimestamp(); + unsigned int key_type = static_cast(key_value->GetTypeByte()); + CellType::cellType cell_type = static_cast(key_type); + std::string tags(""); + delete key_value; + + Cell *cell = new Cell( Bytes::ToString(tmp_rowbytes), Bytes::ToString(tmp_fambytes), Bytes::ToString(tmp_qualbytes), + timestamp, Bytes::ToString(tmp_valbytes), tags, cell_type); + + DLOG(INFO) << "row: " << Bytes::ToHex(tmp_rowbytes); + DLOG(INFO) << "family: " << Bytes::ToHex(tmp_fambytes); + DLOG(INFO) << "quaifier: " << Bytes::ToHex(tmp_qualbytes); + DLOG(INFO) << "timestamp: " << timestamp; + DLOG(INFO) << "value: " << Bytes::ToHex(tmp_valbytes); + DLOG(INFO) << "cell_type: " << cell_type; + + return cell; +} + +const KeyValue* Cell::GetKeyValue() const { + + return this->key_value_; +} + +Cell* Cell::Parse(ByteBuffer &cell_data) { + + DLOG(INFO) << "cell_data.size() = " << cell_data.size(); + DLOG(INFO) << "cell_data: " << Bytes::ToHex(cell_data); + int offset = 0; + unsigned int cell_size_length; + const char *pCurrent = &cell_data[offset]; + unsigned int *pSize = (unsigned int*) pCurrent; + cell_size_length = *pSize; + Utils::swapByteOrder(cell_size_length); + pCurrent += Bytes::SIZEOF_INT; + offset += Bytes::SIZEOF_INT; + DLOG(INFO) << "cell_size: " << cell_size_length; + + BYTE_ARRAY bytes; + bytes.insert(bytes.end(), pCurrent, pCurrent + cell_size_length); + return Cell::CreateCell(bytes); +} + diff --git a/hbase-native-client/core/cell.h b/hbase-native-client/core/cell.h new file mode 100644 index 0000000..5fd0b93 --- /dev/null +++ b/hbase-native-client/core/cell.h @@ -0,0 +1,72 @@ +/* + * 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 "bytes.h" +#include "hconstants.h" +#include "key_value.h" + +namespace CellType { +enum cellType{ + UNKNOWN = -1, + MINIMUM = 0, + PUT = 4, + DELETE = 8, + DELETE_COLUMN = 12, + DELETE_FAMILY = 14, + DELETE_FAMILYVERSION = 16, + MAXIMUM = 255 +} ; +} + +class Cell{ + + public: + Cell(); + Cell(const std::string &row, const std::string &family, const std::string &qualifier, + const long ×tamp, const std::string &value, const std::string &tags, const CellType::cellType &cellType); + Cell(const Cell &cell); + Cell& operator= (const Cell &cell); + + virtual ~Cell(); + void Display(); + const std::string &Row(); + const std::string &Family(); + const std::string &Qualifier(); + const std::string &Value(); + const CellType::cellType &TypeByte(); + const long &Timestamp(); + const KeyValue *GetKeyValue() const; + static Cell *CreateCell(const BYTE_ARRAY &bytes); + static Cell * Parse(ByteBuffer &cell_data); + + private: + void DisplayKeyValueDetails(); + std::string row_; + std::string family_; + std::string qualifier_; + long timestamp_; + std::string value_; + std::string tags_; + CellType::cellType cellType_; + KeyValue *key_value_; +}; diff --git a/hbase-native-client/core/delete.cc b/hbase-native-client/core/delete.cc new file mode 100644 index 0000000..d1f561d --- /dev/null +++ b/hbase-native-client/core/delete.cc @@ -0,0 +1,126 @@ +/* + * 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 "delete.h" +#include "exception.h" + +Delete::Delete(const BYTE_ARRAY &row): Delete(row, HBaseConstants::HConstants::LATEST_TIMESTAMP) { + +} + +Delete::Delete(const BYTE_ARRAY &row, const long ×tamp): Delete(row, 0, row.size(), timestamp) { + +} + +Delete::Delete(const BYTE_ARRAY &row, const int &row_offset, const int &row_length): + Delete(row, row_offset, row_length, HBaseConstants::HConstants::LATEST_TIMESTAMP) { + +} + +Delete::Delete(const BYTE_ARRAY &row, const int &row_offset, const int &row_length, const long &ts) { + + if (ts < 0) { + throw HbaseException("Timestamp cannot be negative."); + } + Mutation::CheckRow(row); + this->row_ = row; +} + +Delete::Delete(const Delete &cDelete) { + + this->row_ = cDelete.row_; + this->ts_ = cDelete.ts_; + this->family_map_ = cDelete.family_map_; + +} + +Delete& Delete::operator= (const Delete &cDelete) { + + this->row_ = cDelete.row_; + this->ts_ = cDelete.ts_; + this->family_map_ = cDelete.family_map_; + return *this; + +} + +Delete::~Delete() {} + +Delete &Delete::AddColumn(const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier) { + return AddColumn(family, qualifier, this->ts_); +} + +Delete &Delete::AddColumn(const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier, const long ×tamp) { + + if (timestamp < 0) + throw HbaseException("Timestamp cannot be negative."); + + KeyValue *key_value = nullptr; + key_value = new KeyValue(this->row_, family, qualifier, timestamp, static_cast(KeyValue::KEY_TYPE::Delete)); + this->family_map_[family].push_back(*key_value); + delete key_value; + this->Display(); + return *this; +} + +Delete& Delete::AddColumns(const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier) { + + return AddColumns( family, qualifier,this->ts_); +} + +Delete& Delete::AddColumns(const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier, const long ×tamp) { + + if (timestamp < 0) + throw HbaseException("Timestamp cannot be negative."); + + KeyValue *key_value = nullptr; + key_value = new KeyValue(this->row_, family, qualifier, timestamp, static_cast(KeyValue::KEY_TYPE::DeleteColumn)); + this->family_map_[family].push_back(*key_value); + delete key_value; + return *this; +} + +Delete& Delete::AddFamily(const BYTE_ARRAY &family) { + + return AddFamily( family,this->ts_); +} + +Delete& Delete::AddFamily(const BYTE_ARRAY &family, const long ×tamp) { + + if (timestamp < 0) + throw HbaseException("Timestamp cannot be negative."); + + KeyValue *key_value = nullptr; + key_value = new KeyValue(this->row_, family, HBaseConstants::HConstants::EMPTY_BYTE_ARRAY, + timestamp, static_cast(KeyValue::KEY_TYPE::DeleteFamily)); + this->family_map_[family].push_back(*key_value); + delete key_value; + return *this; +} + +Delete& Delete::AddFamilyVersion(const BYTE_ARRAY &family, const long ×tamp) { + + if (timestamp < 0) + throw HbaseException("Timestamp cannot be negative."); + + KeyValue *key_value = nullptr; + key_value = new KeyValue(this->row_, family, HBaseConstants::HConstants::EMPTY_BYTE_ARRAY, + timestamp, static_cast(KeyValue::KEY_TYPE::DeleteFamilyVersion)); + this->family_map_[family].push_back(*key_value); + delete key_value; + return *this; +} diff --git a/hbase-native-client/core/delete.h b/hbase-native-client/core/delete.h new file mode 100644 index 0000000..f5bade7 --- /dev/null +++ b/hbase-native-client/core/delete.h @@ -0,0 +1,48 @@ +/* + * 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 "bytes.h" +#include "cell.h" +#include "key_value.h" +#include "mutation.h" + + +class Delete : public Mutation { + + public: + Delete(const byte *row); + Delete(const BYTE_ARRAY &row); + Delete(const BYTE_ARRAY &row, const long ×tamp); + Delete(const BYTE_ARRAY &row, const int &row_offset, const int &row_length); + Delete(const BYTE_ARRAY &row, const int &row_offset, const int &row_length, const long &ts); + Delete(const Delete &cDelete); + Delete &operator=(const Delete &cDelete); + + virtual ~Delete(); + Delete& AddColumn(const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier); + Delete& AddColumn(const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier, const long ×tamp); + Delete& AddColumns(const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier); + Delete& AddColumns(const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier, const long ×tamp); + Delete& AddFamily(const BYTE_ARRAY &family); + Delete& AddFamily(const BYTE_ARRAY &family, const long ×tamp); + Delete& AddFamilyVersion(const BYTE_ARRAY &family, const long ×tamp); + +}; diff --git a/hbase-native-client/core/exception.cc b/hbase-native-client/core/exception.cc new file mode 100644 index 0000000..5af0c9c --- /dev/null +++ b/hbase-native-client/core/exception.cc @@ -0,0 +1,121 @@ +/* + * 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 "exception.h" +#include + +const HbaseException::EXCEPTIONLIST_MAP HbaseException::EXCEPTIONS = HbaseException::GetExceptionList(); + +HbaseException::EXCEPTIONLIST_MAP HbaseException::GetExceptionList() { + + EXCEPTIONLIST_MAP exc_list; + exc_list.insert(EXCEPTIONLIST_PAIR("org.apache.hadoop.hbase.NotServingRegionException:", + HbaseException::HBASEERRCODES::ERR_REGION_SERVER_NOT_SERVING)); + exc_list.insert(EXCEPTIONLIST_PAIR("org.apache.hadoop.hbase.exceptions.RegionMovedException:", + HbaseException::HBASEERRCODES::ERR_REGION_MOVED)); + return exc_list; +} + +const HbaseException::RETRY_EXCEPTIONLIST_MAP HbaseException::RETRY_EXCEPTIONS = HbaseException::GetRetryExceptionList(); + +HbaseException::RETRY_EXCEPTIONLIST_MAP HbaseException::GetRetryExceptionList() { + + RETRY_EXCEPTIONLIST_MAP retry_list; + retry_list.insert(RETRY_EXCEPTIONLIST_PAIR(HbaseException::HBASEERRCODES::ERR_REGION_SERVER_NOT_SERVING, false)); + retry_list.insert(RETRY_EXCEPTIONLIST_PAIR(HbaseException::HBASEERRCODES::ERR_REGION_MOVED, true)); + return retry_list; +} + +HbaseException::HbaseException(const std::string &error, bool stack_trace): error_(error), + stack_trace_(""), + show_stack_trace_(false), + retry_(false), + err_code_(HbaseException::HBASEERRCODES::ERR_UNKNOWN), + retry_count_(0){ + + if (stack_trace) { + this->stack_trace_ = error; + } + + size_t pos = this->error_.find("at "); + if (std::string::npos != pos){ + this->error_.erase(pos); + } + + for (const auto exception : EXCEPTIONS) { + pos = this->stack_trace_.find(exception.first); + if (std::string::npos != pos){ + this->err_code_ = exception.second; + try { + this->retry_ = RETRY_EXCEPTIONS.at(this->err_code_); + } catch(const std::out_of_range &oor) { + //By default no retries. + this->retry_ = false; + DLOG(WARNING) << "Can't find definition of the occured exception. No retries will ever occur:- " << this->stack_trace_; + } + + break; + } + + } + + this->error_ += "\n"; + this->stack_trace_ += "\n"; +} + +HbaseException::~HbaseException() { + // TODO Auto-generated destructor stub +} + +const char *HbaseException::what() const throw() { + + if (ShowStackTrace()) + return StackTrace(); + + else + return this->error_.c_str(); +} + +const char *HbaseException::StackTrace() const { + + return this->stack_trace_.c_str(); +} + +const bool &HbaseException::ShowStackTrace() const { + + return this->show_stack_trace_; +} + +void HbaseException::SetStackTrace(const bool &show_stack_trace) { + + this->show_stack_trace_ = show_stack_trace; +} + +const HbaseException::HBASEERRCODES &HbaseException::GetHbaseErrorCode() const { + + return this->err_code_; +} + +const bool &HbaseException::RetryException() const { + + return this->retry_; +} + + diff --git a/hbase-native-client/core/exception.h b/hbase-native-client/core/exception.h new file mode 100644 index 0000000..512ae46 --- /dev/null +++ b/hbase-native-client/core/exception.h @@ -0,0 +1,64 @@ +/* + * 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 + +class HbaseException : public std::exception { + + public: + + enum class HBASEERRCODES{ + ERR_REGION_SERVER_NOT_SERVING, + ERR_REGION_MOVED, + ERR_UNKNOWN + }; + + using EXCEPTIONLIST_MAP = std::map; + using EXCEPTIONLIST_PAIR = std::pair; + using RETRY_EXCEPTIONLIST_MAP = std::map; + using RETRY_EXCEPTIONLIST_PAIR = std::pair; + HbaseException(const std::string &error, bool stack_trace = false); + virtual ~HbaseException(); + virtual const char *what () const throw(); + static RETRY_EXCEPTIONLIST_MAP GetRetryExceptionList(); + static EXCEPTIONLIST_MAP GetExceptionList(); + void SetStackTrace(const bool &show_stack_trace); + const HbaseException::HBASEERRCODES &GetHbaseErrorCode() const; + const bool &RetryException() const; + + + + private: + + static const RETRY_EXCEPTIONLIST_MAP RETRY_EXCEPTIONS; + static const EXCEPTIONLIST_MAP EXCEPTIONS; + const char *StackTrace () const; + const bool &ShowStackTrace() const; + + std::string error_; + std::string stack_trace_; + bool show_stack_trace_; + bool retry_; + HbaseException::HBASEERRCODES err_code_; + mutable int retry_count_; +}; + diff --git a/hbase-native-client/core/hconstants.cc b/hbase-native-client/core/hconstants.cc new file mode 100644 index 0000000..986589a --- /dev/null +++ b/hbase-native-client/core/hconstants.cc @@ -0,0 +1,191 @@ +/* + * 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 "hconstants.h" + +const int HBaseConstants::HConstants::DEFAULT_BLOCKSIZE = 64 * 1024; +/** Used as a magic return value while optimized index key feature enabled(HBASE-7845) */ +const int HBaseConstants::HConstants::INDEX_KEY_MAGIC = -2; +/* + * Name of directory that holds recovered edits written by the wal log + * splitting code, one per region + */ +const std::string HBaseConstants::HConstants::RECOVERED_EDITS_DIR = "recovered.edits"; +/** + * The first four bytes of Hadoop RPC connections + */ +const char HBaseConstants::HConstants::RPC_HEADER[] = { 'H', 'B', 'a', 's', '\0'}; +const char HBaseConstants::HConstants::RPC_CURRENT_VERSION = 0; +const int HBaseConstants::HConstants::MAX_ROW_LENGTH = std::numeric_limits< short >::max(); +const long HBaseConstants::HConstants::NO_NONCE = 0; +const long HBaseConstants::HConstants::LATEST_TIMESTAMP = std::numeric_limits< long >::max(); +/** + * An empty instance. + */ +const BYTE_ARRAY HBaseConstants::HConstants::EMPTY_BYTE_ARRAY {}; + +const ByteBuffer HBaseConstants::HConstants::EMPTY_BYTE_BUFFER;// = ByteBuffer.wrap(EMPTY_BYTE_ARRAY); +const long HBaseConstants::HConstants::DEFAULT_MAX_FILE_SIZE = 10 * 1024 * 1024 * 1024L; +const int HBaseConstants::HConstants::FOREVER = std::numeric_limits< int >::max(); +const int HBaseConstants::HConstants::REPLICATION_SCOPE_LOCAL = 0; +const int HBaseConstants::HConstants::REPLICATION_SCOPE_GLOBAL = 1; + +const char *HBaseConstants::HConstants::NAME = "NAME"; +const char *HBaseConstants::HConstants::VERSIONS("VERSIONS"); +const char *HBaseConstants::HConstants::IN_MEMORY = "IN_MEMORY"; +const char *HBaseConstants::HConstants::METADATA = "METADATA"; +const char *HBaseConstants::HConstants::CONFIGURATION = "CONFIGURATION"; + + +/** + * Configuration option that toggles whether EXEC permission checking is + * performed during coprocessor endpoint invocations. + */ +const std::string HBaseConstants::AccessControlConstants::EXEC_PERMISSION_CHECKS_KEY = "hbase.security.exec.permission.checks"; +/** Default setting for hbase.security.exec.permission.checks; false */ +const bool HBaseConstants::AccessControlConstants::DEFAULT_EXEC_PERMISSION_CHECKS = false; + +/** + * Configuration or CF schema option for early termination of access checks + * if table or CF permissions grant access. Pre-0.98 compatible behavior + */ +const std::string HBaseConstants::AccessControlConstants::CF_ATTRIBUTE_EARLY_OUT = "hbase.security.access.early_out"; +/** Default setting for hbase.security.access.early_out */ +const bool HBaseConstants::AccessControlConstants::DEFAULT_ATTRIBUTE_EARLY_OUT = true; + +// Operation attributes for cell level security + +/** Cell level ACL */ +const std::string HBaseConstants::AccessControlConstants::OP_ATTRIBUTE_ACL = "acl"; +/** Cell level ACL evaluation strategy */ +const std::string HBaseConstants::AccessControlConstants::OP_ATTRIBUTE_ACL_STRATEGY = "acl.strategy"; +/** Default cell ACL evaluation strategy: Table and CF first, then ACL */ +const char * HBaseConstants::AccessControlConstants::OP_ATTRIBUTE_ACL_STRATEGY_DEFAULT = "0"; +/** Alternate cell ACL evaluation strategy: Cell ACL first, then table and CF */ +const char * HBaseConstants::AccessControlConstants::OP_ATTRIBUTE_ACL_STRATEGY_CELL_FIRST = "1"; + +const std::string HBaseConstants::EnumToString::ToString(const HBaseConstants::BLOOM_TYPE &enum_type) { + std::string enum_string; + switch (enum_type) { + case HBaseConstants::BLOOM_TYPE::NONE: + enum_string = "NONE"; + break; + case HBaseConstants::BLOOM_TYPE::ROW: + enum_string = "ROW"; + break; + case HBaseConstants::BLOOM_TYPE::ROWCOL: + enum_string = "ROWCOL"; + break; + default: + enum_string = "NONE"; + break; + } + return enum_string; +} + +const std::string HBaseConstants::EnumToString::ToString(const HBaseConstants::DATA_BLOCK_ENCODING &enum_type) { + std::string enum_string; + switch (enum_type) { + case HBaseConstants::DATA_BLOCK_ENCODING::DIFF: + enum_string = "DIFF"; + break; + case HBaseConstants::DATA_BLOCK_ENCODING::FAST_DIFF: + enum_string = "FAST_DIFF"; + break; + case HBaseConstants::DATA_BLOCK_ENCODING::NONE: + enum_string = "NONE"; + break; + case HBaseConstants::DATA_BLOCK_ENCODING::PREFIX: + enum_string = "PREFIX"; + break; + case HBaseConstants::DATA_BLOCK_ENCODING::PREFIX_TREE: + enum_string = "PREFIX_TREE"; + break; + default: + enum_string = "NONE"; + break; + } + return enum_string; +} + +const std::string HBaseConstants::EnumToString::ToString(const HBaseConstants::COMPRESSION_ALGORITHM &enum_type) { + + std::string enum_string; + switch (enum_type) { + case HBaseConstants::COMPRESSION_ALGORITHM::GZ: + enum_string = "GZ"; + break; + case HBaseConstants::COMPRESSION_ALGORITHM::LZ4: + enum_string = "LZ4"; + break; + case HBaseConstants::COMPRESSION_ALGORITHM::LZO: + enum_string = "LZO"; + break; + case HBaseConstants::COMPRESSION_ALGORITHM::NONE: + enum_string = "NONE"; + break; + case HBaseConstants::COMPRESSION_ALGORITHM::SNAPPY: + enum_string = "SNAPPY"; + break; + default: + enum_string = "NONE"; + break; + } + return enum_string; +} + +const std::string HBaseConstants::EnumToString::ToString(const HBaseConstants::KEEP_DELETED_CELLS &enum_type) { + + std::string enum_string; + switch (enum_type) { + case HBaseConstants::KEEP_DELETED_CELLS::FALSE: + enum_string = "FALSE"; + break; + case HBaseConstants::KEEP_DELETED_CELLS::TRUE: + enum_string = "TRUE"; + break; + case HBaseConstants::KEEP_DELETED_CELLS::TTL: + enum_string = "TTL"; + break; + } + return enum_string; +} + +const std::string HBaseConstants::EnumToString::ToString(const HBaseConstants::DURABILITY_TYPE &enum_type) { + + std::string enum_string; + switch (enum_type){ + case HBaseConstants::DURABILITY_TYPE::ASYNC_WAL: + enum_string = "ASYNC_WAL"; + break; + case HBaseConstants::DURABILITY_TYPE::FSYNC_WAL: + enum_string = "FSYNC_WAL"; + break; + case HBaseConstants::DURABILITY_TYPE::SKIP_WAL: + enum_string = "SKIP_WAL"; + break; + case HBaseConstants::DURABILITY_TYPE::SYNC_WAL: + enum_string = "SYNC_WAL"; + break; + case HBaseConstants::DURABILITY_TYPE::USE_DEFAULT: + enum_string = "USE_DEFAULT"; + break; + } + return enum_string; +} diff --git a/hbase-native-client/core/hconstants.h b/hbase-native-client/core/hconstants.h new file mode 100644 index 0000000..b74bc64 --- /dev/null +++ b/hbase-native-client/core/hconstants.h @@ -0,0 +1,233 @@ +/* + * 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 +#include + + +#include "bytes.h" + +namespace HBaseConstants { +class HConstants { + public: + /** + * Default block size for an HFile. + */ + static const int DEFAULT_BLOCKSIZE; + /** Used as a magic return value while optimized index key feature enabled(HBASE-7845) */ + static const int INDEX_KEY_MAGIC; + /* + * Name of directory that holds recovered edits written by the wal log + * splitting code, one per region + */ + static const std::string RECOVERED_EDITS_DIR; + /** + * The first four bytes of Hadoop RPC connections + */ + static const char RPC_HEADER[]; + static const char RPC_CURRENT_VERSION; + /** + * Max length a row can have because of the limitation in TFile. + */ + static const int MAX_ROW_LENGTH; + static const long NO_NONCE; + /** + * Timestamp to use when we want to refer to the latest cell. + * This is the timestamp sent by clients when no timestamp is specified on + * commit. + */ + static const long LATEST_TIMESTAMP; + /** + * An empty instance. + */ + static const BYTE_ARRAY EMPTY_BYTE_ARRAY; + + static const ByteBuffer EMPTY_BYTE_BUFFER; + + /** Default maximum file size */ + static const long DEFAULT_MAX_FILE_SIZE; + + /** + * Unlimited time-to-live. + */ + static const int FOREVER; + + /** + * Scope tag for locally scoped data. + * This data will not be replicated. + */ + static const int REPLICATION_SCOPE_LOCAL; + + /** + * Scope tag for globally scoped data. + * This data will be replicated to all peers. + */ + static const int REPLICATION_SCOPE_GLOBAL; + //TODO: although the following are referenced widely to format strings for + // the shell. They really aren't a part of the public API. It would be + // nice if we could put them somewhere where they did not need to be + // public. They could have package visibility + static const char *NAME; + static const char *VERSIONS; + static const char *IN_MEMORY; + static const char *METADATA; + static const char *CONFIGURATION; + +}; + + +class AccessControlConstants { + + public: + /** + * Configuration option that toggles whether EXEC permission checking is + * performed during coprocessor endpoint invocations. + */ + static const std::string EXEC_PERMISSION_CHECKS_KEY; + /** Default setting for hbase.security.exec.permission.checks; false */ + static const bool DEFAULT_EXEC_PERMISSION_CHECKS; + + /** + * Configuration or CF schema option for early termination of access checks + * if table or CF permissions grant access. Pre-0.98 compatible behavior + */ + static const std::string CF_ATTRIBUTE_EARLY_OUT; + /** Default setting for hbase.security.access.early_out */ + static const bool DEFAULT_ATTRIBUTE_EARLY_OUT; + + // Operation attributes for cell level security + /** Cell level ACL */ + static const std::string OP_ATTRIBUTE_ACL; + /** Cell level ACL evaluation strategy */ + static const std::string OP_ATTRIBUTE_ACL_STRATEGY; + /** Default cell ACL evaluation strategy: Table and CF first, then ACL */ + static const char * OP_ATTRIBUTE_ACL_STRATEGY_DEFAULT; + /** Alternate cell ACL evaluation strategy: Cell ACL first, then table and CF */ + static const char * OP_ATTRIBUTE_ACL_STRATEGY_CELL_FIRST; +}; + +/** + * Ways to keep cells marked for delete around. + */ +/* + * Don't change the TRUE/FALSE labels below, these have to be called + * this way for backwards compatibility. + */ +enum class KEEP_DELETED_CELLS { + /** Deleted Cells are not retained. */ + FALSE, + /** + * Deleted Cells are retained until they are removed by other means + * such TTL or VERSIONS. + * If no TTL is specified or no new versions of delete cells are + * written, they are retained forever. + */ + TRUE, + /** + * Deleted Cells are retained until the delete marker expires due to TTL. + * This is useful when TTL is combined with MIN_VERSIONS and one + * wants to keep a minimum number of versions around but at the same + * time remove deleted cells after the TTL. + */ + TTL +}; + +enum class BLOOM_TYPE { + /** + * Bloomfilters disabled + */ + NONE, + /** + * Bloom enabled with Table row as Key + */ + ROW, + /** + * Bloom enabled with Table row & column (family+qualifier) as Key + */ + ROWCOL +}; + +enum class DATA_BLOCK_ENCODING { + + /** Disable data block encoding. */ + NONE = 0, + // id 1 is reserved for the BITSET algorithm to be added later + PREFIX = 2, + DIFF = 3, + FAST_DIFF = 4, + // COPY_KEY(5, "org.apache.hadoop.hbase.io.encoding.CopyKeyDataBlockEncoder"), + PREFIX_TREE = 6 +}; + +enum class COMPRESSION_ALGORITHM { + LZO, + GZ, + NONE, + SNAPPY, + LZ4 +}; + +enum class DURABILITY_TYPE { + /* Developer note: Do not rename the enum field names. They are serialized in HTableDescriptor */ + /** + * If this is for tables durability, use HBase's global default value (SYNC_WAL). + * Otherwise, if this is for mutation, use the table's default setting to determine durability. + * This must remain the first option. + */ + USE_DEFAULT, + /** + * Do not write the Mutation to the WAL + */ + SKIP_WAL, + /** + * Write the Mutation to the WAL asynchronously + */ + ASYNC_WAL, + /** + * Write the Mutation to the WAL synchronously. + * The data is flushed to the filesystem implementation, but not necessarily to disk. + * For HDFS this will flush the data to the designated number of DataNodes. + * See HADOOP-6313 + */ + SYNC_WAL, + /** + * Write the Mutation to the WAL synchronously and force the entries to disk. + * (Note: this is currently not supported and will behave identical to {@link #SYNC_WAL}) + * See HADOOP-6313 + */ + FSYNC_WAL +}; + +class EnumToString { + + public: + static const std::string ToString(const HBaseConstants::BLOOM_TYPE &enum_type); + static const std::string ToString(const HBaseConstants::DATA_BLOCK_ENCODING &enum_type); + static const std::string ToString(const HBaseConstants::COMPRESSION_ALGORITHM &enum_type); + static const std::string ToString(const HBaseConstants::KEEP_DELETED_CELLS &enum_type); + static const std::string ToString(const HBaseConstants::DURABILITY_TYPE &enum_type); +}; + + +} diff --git a/hbase-native-client/core/key_value.cc b/hbase-native-client/core/key_value.cc new file mode 100644 index 0000000..c72a6eb --- /dev/null +++ b/hbase-native-client/core/key_value.cc @@ -0,0 +1,503 @@ +/* + * 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 "key_value.h" + +#include +#include +#include + +#include "hconstants.h" + +const byte KeyValue::COLUMN_FAMILY_DELIMITER = ':'; + +const int KeyValue::KEY_LENGTH_SIZE = Bytes::SIZEOF_INT; + + +const int KeyValue::ROW_LENGTH_SIZE = Bytes::SIZEOF_SHORT; +const int KeyValue::FAMILY_LENGTH_SIZE = Bytes::SIZEOF_BYTE; +const int KeyValue::TIMESTAMP_SIZE = Bytes::SIZEOF_LONG; +const int KeyValue::TYPE_SIZE = Bytes::SIZEOF_BYTE; +const int KeyValue::TIMESTAMP_TYPE_SIZE = TIMESTAMP_SIZE + TYPE_SIZE; +const int KeyValue::KEY_INFRASTRUCTURE_SIZE = ROW_LENGTH_SIZE + FAMILY_LENGTH_SIZE + TIMESTAMP_TYPE_SIZE; + +const int KeyValue::VALUE_LENGTH_SIZE = Bytes::SIZEOF_INT; +const int KeyValue::KEYVALUE_INFRASTRUCTURE_SIZE = KeyValue::KEY_LENGTH_SIZE /*keylength*/ + KeyValue::VALUE_LENGTH_SIZE /*valuelength*/; + +const int KeyValue::ROW_OFFSET = KeyValue::KEYVALUE_INFRASTRUCTURE_SIZE; +const int KeyValue::ROW_KEY_OFFSET = ROW_OFFSET + ROW_LENGTH_SIZE; + +const int KeyValue::TAGS_LENGTH_SIZE = Bytes::SIZEOF_SHORT; +const int KeyValue::KEYVALUE_WITH_TAGS_INFRASTRUCTURE_SIZE = ROW_OFFSET + TAGS_LENGTH_SIZE; + +KeyValue::KeyValue():offset_(-1), length_(-1), seqid_(0) { + +} + +KeyValue::KeyValue(const KeyValue& ckey_value) { + + //DLOG(INFO) << "KV::Copy Constr"; + this->offset_ = ckey_value.offset_; + this->length_ = ckey_value.length_; + Bytes::CopyByteArray(ckey_value.bytes_, this->bytes_, this->offset_, this->length_); + this->seqid_ = ckey_value.seqid_; +} + +KeyValue& KeyValue::operator= (const KeyValue &ckey_value) { + + //DLOG(INFO) << "KV::Assign Constr" ; + this->offset_ = ckey_value.offset_; + this->length_ = ckey_value.length_; + Bytes::CopyByteArray(ckey_value.bytes_, this->bytes_, this->offset_, this->length_); + this->seqid_ = ckey_value.seqid_; + + return *this; + +} + +KeyValue::KeyValue(const BYTE_ARRAY &bytes): KeyValue(bytes, 0) { + +} + +KeyValue::KeyValue(const BYTE_ARRAY &bytes, const int &offset):KeyValue(bytes, offset, GetLength(bytes, offset)) { + +} + +KeyValue::KeyValue( const BYTE_ARRAY &bytes, const int &offset, const int &length):offset_(offset), length_(length), seqid_(0) { + + Bytes::CopyByteArray(bytes, this->bytes_, this->offset_, this->length_); +} + + +KeyValue::KeyValue(const BYTE_ARRAY &bytes, const int &offset, const int &length, const long &ts): + KeyValue(bytes, offset, length, HBaseConstants::HConstants::EMPTY_BYTE_ARRAY, 0, 0, HBaseConstants::HConstants::EMPTY_BYTE_ARRAY, 0, 0, ts, + static_cast(KeyValue::KEY_TYPE::Maximum), HBaseConstants::HConstants::EMPTY_BYTE_ARRAY, 0, 0, null_tags_) { + +} + + +KeyValue::KeyValue(const BYTE_ARRAY &row, const long ×tamp): + KeyValue(row, HBaseConstants::HConstants::EMPTY_BYTE_ARRAY, HBaseConstants::HConstants::EMPTY_BYTE_ARRAY, timestamp, static_cast(KeyValue::KEY_TYPE::Maximum), HBaseConstants::HConstants::EMPTY_BYTE_ARRAY) { + +} + +KeyValue::KeyValue(const BYTE_ARRAY &row, const long ×tamp, const BYTE_TYPE &type): + KeyValue(row, HBaseConstants::HConstants::EMPTY_BYTE_ARRAY, HBaseConstants::HConstants::EMPTY_BYTE_ARRAY, timestamp, type, HBaseConstants::HConstants::EMPTY_BYTE_ARRAY) { + +} + +KeyValue::KeyValue(const BYTE_ARRAY &row, const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier): + KeyValue(row, family, qualifier, HBaseConstants::HConstants::LATEST_TIMESTAMP, static_cast(KeyValue::KEY_TYPE::Maximum)) { + +} + +KeyValue::KeyValue( const BYTE_ARRAY &row, const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier, + const BYTE_ARRAY &value): + KeyValue(row, family, qualifier, + HBaseConstants::HConstants::LATEST_TIMESTAMP, static_cast(KeyValue::KEY_TYPE::Put), value) { + + +} + +KeyValue::KeyValue( const BYTE_ARRAY &row, const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier, + const long ×tamp, const BYTE_TYPE &type): + KeyValue(row, family, qualifier, timestamp, type, HBaseConstants::HConstants::EMPTY_BYTE_ARRAY) { + +} + +KeyValue::KeyValue( const BYTE_ARRAY &row, const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier, + const long ×tamp, const BYTE_ARRAY &value):KeyValue(row, family, qualifier, + timestamp, static_cast(KeyValue::KEY_TYPE::Put), value) { + +} + +KeyValue::KeyValue( const BYTE_ARRAY &row, const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier, + const long ×tamp, const BYTE_ARRAY &value, const Tag *tag_ptr): + KeyValue(row, family, qualifier, timestamp, value, null_tags_){ + +} + +KeyValue::KeyValue( const BYTE_ARRAY &row, const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier, + const long ×tamp, const BYTE_ARRAY &value, const std::vector &tags): + KeyValue( row, 0, row.size(), family, 0, family.size(), qualifier, 0, qualifier.size(), + timestamp, static_cast(KeyValue::KEY_TYPE::Put), value, 0, value.size(), tags) { + +} + +KeyValue::KeyValue( const BYTE_ARRAY &row, const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier, + const long ×tamp, const BYTE_TYPE &type, const BYTE_ARRAY &value): + KeyValue(row, 0, row.size(), family, 0, family.size(), qualifier, 0, qualifier.size(), timestamp, type, value, 0, value.size()) { + +} + +KeyValue::KeyValue( const BYTE_ARRAY &row, const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier, + const long ×tamp, const BYTE_TYPE &type, const BYTE_ARRAY &value, + const std::vector &tags):offset_(-1), length_(-1), seqid_(0) { + + KeyValue( row, family, qualifier, 0, qualifier.size(), timestamp, type, value, 0, value.size(), tags); +} + +KeyValue::KeyValue( const BYTE_ARRAY &row, const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier, + const int &qoffset, const int &qlength, const long ×tamp, + const BYTE_TYPE &type, const BYTE_ARRAY &value, const int &voffset, + const int &vlength, const std::vector &tags):offset_(-1), length_(-1), seqid_(0) { + KeyValue(row, 0, row.size(), family, 0, family.size(), qualifier, qoffset, qlength, timestamp, type, value, voffset, vlength, tags); + +} + +KeyValue::KeyValue( const BYTE_ARRAY &row, const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier, + const long ×tamp, const BYTE_TYPE &type, const BYTE_ARRAY &value, + const BYTE_ARRAY &tag_bytes): + KeyValue(row, family, qualifier, 0, qualifier.size(), timestamp, type, value, 0, value.size(), null_tags_) { + +} + +KeyValue::KeyValue( BYTE_ARRAY &buffer, const int &boffset, + const BYTE_ARRAY &row, const int &roffset, const int &rlength, + const BYTE_ARRAY &family, const int &foffset, const int &flength, + const BYTE_ARRAY &qualifier, const int &qoffset, const int &qlength, + const long ×tamp, const BYTE_TYPE &type, + const BYTE_ARRAY &value, const int &voffset, const int &vlength, + const BYTE_ARRAY &tag_bytes):offset_(-1), length_(-1), seqid_(0) { + +} + +KeyValue::KeyValue( const BYTE_ARRAY &row, const int &row_offset, const int &row_length, + const BYTE_ARRAY &family, const int &family_offset, const int &family_length, + const BYTE_ARRAY &qualifier, const int &qualifier_offset, const int &qualifier_length, + const long ×tamp, const BYTE_TYPE &type, + const BYTE_ARRAY &value, const int &value_offset, const int &value_length, + const BYTE_ARRAY &tag_bytes, const int &tagsOffset, const int &tagsLength):offset_(-1), length_(-1), seqid_(0){ + + + this->length_ = CreateByteArray( this->bytes_, row, row_offset, row_length, + family, family_offset, family_length, + qualifier, qualifier_offset, qualifier_length, + timestamp, type, value, value_offset, value_length, tag_bytes, tagsOffset, tagsLength); + this->offset_ = 0; +} + +KeyValue::KeyValue( const int &rlength, const int &flength, const int &qlength, const long ×tamp, const BYTE_TYPE &type, + const int &vlength):offset_(-1), length_(-1), seqid_(0) { + KeyValue(rlength, flength, qlength, timestamp, type, vlength, 0); +} + +KeyValue::KeyValue( const int &rlength, const int &flength, const int &qlength, + const long ×tamp, const BYTE_TYPE &type, const int &vlength, + const int &tags_length):offset_(-1), length_(-1), seqid_(0) { + +} + +KeyValue::KeyValue( const BYTE_ARRAY &row, const int &roffset, const int &rlength, + const BYTE_ARRAY &family, const int &foffset, const int &flength, + const BYTE_ARRAY &qualifier, const int &qoffset, const int &qlength, + const long ×tamp, const BYTE_TYPE &type, + const BYTE_ARRAY &value, const int &voffset, const int &vlength): + KeyValue( row, roffset, rlength, family, foffset, flength, qualifier, + qoffset, qlength, timestamp, type, value, voffset, vlength, null_tags_){ + +} + +KeyValue::KeyValue( const BYTE_ARRAY &row, const int &row_offset, const int &row_length, + const BYTE_ARRAY &family, const int &family_offset, const int &family_length, + const BYTE_ARRAY &qualifier, const int &qualifier_offset, const int &qualifier_length, + const long ×tamp, const BYTE_TYPE &type, + const BYTE_ARRAY &value, const int &value_offset, const int &value_length, + const std::vector &tags):offset_(-1), length_(-1), seqid_(0){ + + this->length_ = CreateByteArray( this->bytes_, row, row_offset, row_length, + family, family_offset, family_length, + qualifier, qualifier_offset, qualifier_length, + timestamp, type, value, value_offset, value_length, tags); + this->offset_ = 0; + +} + +int KeyValue::WriteByteArray(BYTE_ARRAY &key_value, const int &boffset, + const BYTE_ARRAY &row, const int &roffset, const int &rlength, + const BYTE_ARRAY &family, const int &foffset, const int &flength, + const BYTE_ARRAY &qualifier, const int &qoffset, const int &qlength, + const long timestamp, const BYTE_TYPE &type, + const BYTE_ARRAY &value, const int &voffset, const int &vlength, const BYTE_ARRAY &tags) { + + int key_length = static_cast(GetKeyDataStructureSize(rlength, flength, qlength)); + int key_value_length = static_cast(GetKeyValueDataStructureSize( rlength, flength, qlength, vlength, tags.size())); + key_value.resize(key_value_length); + // Write key, value and key row length. + int pos = boffset; + pos = Bytes::PutInt(key_value, pos, key_length); + pos = Bytes::PutInt(key_value, pos, vlength); + pos = Bytes::PutShort(key_value, pos, (short)(rlength & 0x0000ffff)); + pos = Bytes::PutBytes(key_value, pos, row, roffset, rlength); + pos = Bytes::PutByte(key_value, pos, (byte) (flength & 0x0000ff)); + if (flength != 0) { + pos = Bytes::PutBytes(key_value, pos, family, foffset, flength); + } + if (qlength != 0) { + pos = Bytes::PutBytes(key_value, pos, qualifier, qoffset, qlength); + } + pos = Bytes::PutLong(key_value, pos, timestamp); + pos = Bytes::PutByte(key_value, pos, type); + if (value.size()> 0) { + pos = Bytes::PutBytes(key_value, pos, value, voffset, vlength); + } + return key_value_length; +} + +int KeyValue::CreateByteArray( BYTE_ARRAY &key_value, + const BYTE_ARRAY &row, const int &row_offset, const int &row_length, + const BYTE_ARRAY &family, const int &family_offset, const int &family_length, + const BYTE_ARRAY &qualifier, const int &qualifier_offset, const int &qualifier_length, + const long ×tamp, const BYTE_TYPE &type, + const BYTE_ARRAY &value, const int &value_offset, const int &value_length, + const std::vector &tags){ + + int key_length = static_cast(GetKeyDataStructureSize(row_length, family_length, qualifier_length)); + int key_value_length = static_cast(GetKeyValueDataStructureSize( row_length, family_length, qualifier_length, + value_length, tags.size())); + key_value.resize(key_value_length); + + int pos = 0; + pos = Bytes::PutInt(key_value, pos, key_length); + pos = Bytes::PutInt(key_value, pos, value_length); + pos = Bytes::PutShort(key_value, pos, static_cast(row_length & 0x0000ffff)); + pos = Bytes::PutBytes(key_value, pos, row, row_offset, row_length); + pos = Bytes::PutByte(key_value, pos, static_cast(family_length & 0x0000FF)); + if(family_length != 0) { + pos = Bytes::PutBytes(key_value, pos, family, family_offset, family_length); + } + if (qualifier_length > 0) { + pos = Bytes::PutBytes(key_value, pos, qualifier, qualifier_offset, qualifier_length); + } + pos = Bytes::PutLong(key_value, pos, timestamp); + pos = Bytes::PutByte(key_value, pos, type); + if (value_length > 0) { + pos = Bytes::PutBytes(key_value, pos, value, value_offset, value_length); + } + return key_value.size(); + +} + +int KeyValue::CreateByteArray( BYTE_ARRAY &key_value, + const BYTE_ARRAY &row, const int &row_offset, const int &row_length, + const BYTE_ARRAY &family, const int &family_offset, const int &family_length, + const BYTE_ARRAY &qualifier, const int &qualifier_offset, const int &qualifier_length, + const long ×tamp, const BYTE_TYPE &type, + const BYTE_ARRAY &value, const int &value_offset, const int &value_length, + const BYTE_ARRAY &tag_bytes, const int &tag_offset, const int &tag_length){ + + int key_length = static_cast(GetKeyDataStructureSize(row_length, family_length, qualifier_length)); + int key_value_length = static_cast(GetKeyValueDataStructureSize( row_length, family_length, qualifier_length, + value_length, tag_length)); + key_value.resize(key_value_length); + + int pos = 0; + pos = Bytes::PutInt(key_value, pos, key_length); + pos = Bytes::PutInt(key_value, pos, value_length); + pos = Bytes::PutShort(key_value, pos, static_cast(row_length & 0x0000ffff)); + pos = Bytes::PutBytes(key_value, pos, row, row_offset, row_length); + pos = Bytes::PutByte(key_value, pos, static_cast(family_length & 0x0000FF)); + if(family_length != 0) { + pos = Bytes::PutBytes(key_value, pos, family, family_offset, family_length); + } + if (qualifier_length > 0) { + pos = Bytes::PutBytes(key_value, pos, qualifier, qualifier_offset, qualifier_length); + } + pos = Bytes::PutLong(key_value, pos, timestamp); + pos = Bytes::PutByte(key_value, pos, type); + if (value_length > 0) { + pos = Bytes::PutBytes(key_value, pos, value, value_offset, value_length); + } + + return key_value.size(); +} + +KeyValue::~KeyValue() { +} + + +long KeyValue::GetKeyValueDataStructureSize( const int &rlength, + const int &flength, + const int &qlength, + const int &vlength, + const int &tagsLength){ + + long kv_ds_size = 0L; + kv_ds_size = GetKeyDataStructureSize(rlength, flength, qlength) + vlength; + if (0 == tagsLength) { + kv_ds_size += KeyValue::KEYVALUE_INFRASTRUCTURE_SIZE; + }else{ + kv_ds_size += KeyValue::KEYVALUE_WITH_TAGS_INFRASTRUCTURE_SIZE + tagsLength; + } + return kv_ds_size; +} + + +long KeyValue::GetKeyDataStructureSize( const int &rlength, + const int &flength, + const int &qlength) { + return KeyValue::KEY_INFRASTRUCTURE_SIZE + rlength + flength + qlength; +} + +void KeyValue::SetSequenceId(const long &sequence_id){ + + this->seqid_ = sequence_id; +} + + +void KeyValue::DisplayBytes(){ + + Bytes::DisplayBytes(this->bytes_); + DLOG(INFO) << "Hex Vals:: " << Bytes::ToHex(this->bytes_); + +} + +int KeyValue::GetLength(const BYTE_ARRAY &bytes, const int &offset){ + + int klength = KeyValue::ROW_OFFSET + Bytes::ToInt(bytes, offset); + int vlength = Bytes::ToInt(bytes, offset + Bytes::SIZEOF_INT); + return klength + vlength; +} + +const int KeyValue::GetKeyOffset() const{ + return this->offset_ + KeyValue::ROW_OFFSET; +} + +const int KeyValue::GetKeyLength() const{ + + return Bytes::ToInt(this->bytes_, this->offset_); +} + + +const BYTE_ARRAY &KeyValue::GetRowArray() const{ + return this->bytes_; +} + +const int KeyValue::GetRowOffset() const{ + + return this->offset_ + KeyValue::ROW_KEY_OFFSET; +} + +const int KeyValue::GetRowLength() const{ + + return Bytes::ToShort(this->bytes_, GetKeyOffset()); +} + +const BYTE_ARRAY &KeyValue::GetFamilyArray() const{ + return this->bytes_; +} + +const int KeyValue::GetFamilyOffset() const{ + + return GetFamilyOffset(GetRowLength()); +} + +const int KeyValue::GetFamilyOffset(const int &row_length) const{ + + return this->offset_ + KeyValue::ROW_KEY_OFFSET + row_length + KeyValue::FAMILY_LENGTH_SIZE; +} + +const BYTE_TYPE KeyValue::GetFamilyLength() const{ + + return GetFamilyLength(GetFamilyOffset()); +} + +const BYTE_TYPE KeyValue::GetFamilyLength(const int &foffset) const{ + + return this->bytes_[foffset-1]; +} + +const BYTE_ARRAY &KeyValue::GetQualifierArray() const{ + + return this->bytes_; +} + +const int KeyValue::GetQualifierOffset() const{ + + return GetQualifierOffset(GetFamilyOffset()); +} + +const int KeyValue::GetQualifierOffset(const int &foffset) const{ + + return foffset + GetFamilyLength(foffset); +} + +const int KeyValue::GetQualifierLength() const{ + + return GetQualifierLength(GetRowLength(),GetFamilyLength()); + +} + +const int KeyValue::GetQualifierLength(const int &rlength, const int &flength) const{ + + return GetKeyLength() - static_cast(GetKeyDataStructureSize(rlength, flength, 0)); +} + +const int KeyValue::GetTimestampOffset() { + + return GetTimestampOffset(GetKeyLength()); +} + +const int KeyValue::GetTimestampOffset(const int &key_length) const{ + return GetKeyOffset() + key_length - KeyValue::TIMESTAMP_TYPE_SIZE; +} + +const unsigned long KeyValue::GetTimestamp() const{ + + return GetTimestamp(GetKeyLength()); +} + +const unsigned long KeyValue::GetTimestamp(const int &key_length) const{ + + int ts_offset = GetTimestampOffset(key_length); + return Bytes::ToLong(this->bytes_, ts_offset); +} + +const BYTE_TYPE KeyValue::GetTypeByte() const{ + + return this->bytes_[this->offset_ + GetKeyLength() - 1 + KeyValue::ROW_OFFSET]; +} + +const long KeyValue::GetSequenceId(){ + + return this->seqid_; +} + +const BYTE_ARRAY &KeyValue::GetValueArray() const{ + + return this->bytes_; +} + +const int KeyValue::GetValueOffset() const{ + + int voffset = GetKeyOffset() + GetKeyLength(); + return voffset; +} + +const int KeyValue::GetValueLength() const{ + + int vlength = Bytes::ToInt(this->bytes_, this->offset_ + KeyValue::KEY_LENGTH_SIZE); + return vlength; +} + + +const int &KeyValue::GetKeyValueLength() const{ + + return this->length_; +} diff --git a/hbase-native-client/core/key_value.h b/hbase-native-client/core/key_value.h new file mode 100644 index 0000000..b4b18bf --- /dev/null +++ b/hbase-native-client/core/key_value.h @@ -0,0 +1,188 @@ +/* + * 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 "bytes.h" +#include "tag.h" + +class KeyValue { + public: + enum class KEY_TYPE{ + Minimum = static_cast(0), + Put = static_cast(4), + Delete = static_cast(8), + DeleteFamilyVersion = static_cast(10), + DeleteColumn = static_cast(12), + DeleteFamily = static_cast(14), + Maximum = static_cast(255) + }; + const static byte COLUMN_FAMILY_DELIMITER; + /** Size of the key length field in bytes*/ + const static int KEY_LENGTH_SIZE; + /** Size of the row length field in bytes */ + const static int ROW_LENGTH_SIZE; + /** Size of the family length field in bytes */ + const static int FAMILY_LENGTH_SIZE; + /** Size of the timestamp field in bytes */ + const static int TIMESTAMP_SIZE; + /** Size of the key type field in bytes */ + const static int TYPE_SIZE; + // Size of the timestamp and type byte on end of a key -- a long + a byte. + const static int TIMESTAMP_TYPE_SIZE; + // Size of the length shorts and bytes in key. + const static int KEY_INFRASTRUCTURE_SIZE; + // How far into the key the row starts at. First thing to read is the short + // that says how long the row is. + const static int ROW_OFFSET; + const static int ROW_KEY_OFFSET; + // Size of the length ints in a KeyValue datastructure. + const static int KEYVALUE_INFRASTRUCTURE_SIZE; + /** Size of the tags length field in bytes */ + const static int TAGS_LENGTH_SIZE; + const static int KEYVALUE_WITH_TAGS_INFRASTRUCTURE_SIZE; + + const static int VALUE_LENGTH_SIZE; + + KeyValue(); + KeyValue(const KeyValue& ckey_value); + KeyValue& operator= (const KeyValue &ckey_value); + KeyValue( const BYTE_ARRAY &bytes); + KeyValue( const BYTE_ARRAY &bytes, const int &offset); + KeyValue( const BYTE_ARRAY &bytes, const int &offset, const int &length); + KeyValue( const BYTE_ARRAY &bytes, const int &offset, const int &length, const long &ts); + KeyValue( const BYTE_ARRAY &row, const long ×tamp); + KeyValue(const BYTE_ARRAY &row, const long ×tamp, const BYTE_TYPE &type); + KeyValue( const BYTE_ARRAY &row, const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier); + KeyValue( const BYTE_ARRAY &row, const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier, + const BYTE_ARRAY &value); + KeyValue( const BYTE_ARRAY &row, const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier, + const long ×tamp, const BYTE_TYPE &type); + KeyValue( const BYTE_ARRAY &row, const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier, + const long ×tamp, const BYTE_ARRAY &value); + KeyValue( const BYTE_ARRAY &row, const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier, + const long ×tamp, const BYTE_ARRAY &value, const Tag *tag_ptr); + KeyValue( const BYTE_ARRAY &row, const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier, + const long ×tamp, const BYTE_ARRAY &value, const std::vector &tags); + KeyValue( const BYTE_ARRAY &row, const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier, + const long ×tamp, const BYTE_TYPE &type, const BYTE_ARRAY &value); + KeyValue( const BYTE_ARRAY &row, const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier, + const long ×tamp, const BYTE_TYPE &type, const BYTE_ARRAY &value, + const std::vector &tags); + KeyValue( const BYTE_ARRAY &row, const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier, + const int &qoffset, const int &qlength, const long ×tamp, + const BYTE_TYPE &type, const BYTE_ARRAY &value, const int &voffset, + const int & vlength, const std::vector &tags); + KeyValue( const BYTE_ARRAY &row, const BYTE_ARRAY &family, const BYTE_ARRAY &qualifier, + const long ×tamp, const BYTE_TYPE &type, const BYTE_ARRAY &value, + const BYTE_ARRAY &tag_bytes); + KeyValue( BYTE_ARRAY &buffer, const int &boffset, + const BYTE_ARRAY &row, const int &roffset, const int &rlength, + const BYTE_ARRAY &family, const int &foffset, const int &flength, + const BYTE_ARRAY &qualifier, const int &qoffset, const int &qlength, + const long ×tamp, const BYTE_TYPE &type, + const BYTE_ARRAY &value, const int &voffset, const int &vlength, + const BYTE_ARRAY &tag_bytes); + KeyValue( const BYTE_ARRAY &row, const int &row_offset, const int &row_length, + const BYTE_ARRAY &family, const int &family_offset, const int &family_length, + const BYTE_ARRAY &qualifier, const int &qualifier_offset, const int &qualifier_length, + const long ×tamp, const BYTE_TYPE &type, + const BYTE_ARRAY &value, const int &value_offset, const int &value_length, + const BYTE_ARRAY &tag_bytes, const int &tagsOffset, const int &tagsLength); + KeyValue( const int &rlength, const int &flength, const int &qlength, + const long ×tamp, const BYTE_TYPE &type, const int &vlength); + KeyValue( const int &rlength, const int &flength, const int &qlength, + const long ×tamp, const BYTE_TYPE &type, const int &vlength, + const int &tags_length); + KeyValue( const BYTE_ARRAY &row, const int &roffset, const int &rlength, + const BYTE_ARRAY &family, const int &foffset, const int &flength, + const BYTE_ARRAY &qualifier, const int &qoffset, const int &qlength, + const long ×tamp, const BYTE_TYPE &type, + const BYTE_ARRAY &value, const int &voffset, const int &vlength); + KeyValue( const BYTE_ARRAY &row, const int &row_offset, const int &row_length, + const BYTE_ARRAY &family, const int &family_offset, const int &family_length, + const BYTE_ARRAY &qualifier, const int &qualifier_offset, const int &qualifier_length, + const long ×tamp, const BYTE_TYPE &type, + const BYTE_ARRAY &value, const int &value_offset, const int &value_length, + const std::vector &tags); + const int GetKeyOffset() const; + const int GetKeyLength() const; + const BYTE_ARRAY &GetRowArray() const; + const int GetRowOffset() const; + const int GetRowLength() const; + const BYTE_ARRAY &GetFamilyArray() const; + const int GetFamilyOffset() const; + const BYTE_TYPE GetFamilyLength() const; + const BYTE_ARRAY &GetQualifierArray() const; + const int GetQualifierOffset() const; + const int GetQualifierLength() const; + const int GetTimestampOffset(); + const unsigned long GetTimestamp() const; + const BYTE_TYPE GetTypeByte() const; + const long GetSequenceId(); + const BYTE_ARRAY &GetValueArray() const; + const int GetValueOffset() const; + const int GetValueLength() const; + virtual ~KeyValue(); + void SetSequenceId(const long &sequence_id); + void DisplayBytes(); + static long GetKeyValueDataStructureSize( const int &rlength, const int &flength, + const int &qlength, const int &vlength, const int &tagsLength = 0); + static long GetKeyDataStructureSize( const int &rlength, const int &flength, const int &qlength); + static int CreateByteArray( BYTE_ARRAY &key_value, + const BYTE_ARRAY &row, const int &row_offset, const int &row_length, + const BYTE_ARRAY &family, const int &family_offset, const int &family_length, + const BYTE_ARRAY &qualifier, const int &qualifier_offset, const int &qualifier_length, + const long ×tamp, const BYTE_TYPE &type, + const BYTE_ARRAY &value, const int &value_offset, const int &value_length, + const std::vector &tags); + static int CreateByteArray( BYTE_ARRAY &key_value, + const BYTE_ARRAY &row, const int &row_offset, const int &row_length, + const BYTE_ARRAY &family, const int &family_offset, const int &family_length, + const BYTE_ARRAY &qualifier, const int &qualifier_offset, const int &qualifier_length, + const long ×tamp, const BYTE_TYPE &type, + const BYTE_ARRAY &value, const int &value_offset, const int &value_length, + const BYTE_ARRAY &tag_bytes, const int &tag_offset, const int &tag_length); + static int WriteByteArray( BYTE_ARRAY &buffer, const int &boffset, + const BYTE_ARRAY &row, const int &roffset, const int &rlength, + const BYTE_ARRAY &family, const int &foffset, const int &flength, + const BYTE_ARRAY &qualifier, const int &qoffset, const int &qlength, + const long timestamp, const BYTE_TYPE & type, + const BYTE_ARRAY &value, const int &voffset, const int &vlength, const BYTE_ARRAY &tag_bytes); + + const int &GetKeyValueLength() const; + + protected: + int offset_; // offset into bytes buffer KV starts at + int length_; // length of the KV starting from offset. + BYTE_ARRAY bytes_; // an immutable byte array that contains the KV + std::vector null_tags_; + + private: + const static int MAX_TAGS_LENGTH; + long seqid_; + const int GetFamilyOffset(const int &row_length) const; + const BYTE_TYPE GetFamilyLength(const int &foffset) const; + const int GetQualifierOffset(const int &foffset) const; + const int GetQualifierLength(const int &rlength, const int &flength) const; + const int GetTimestampOffset(const int &key_length) const; + const unsigned long GetTimestamp(const int &key_length) const; + static int GetLength(const BYTE_ARRAY &bytes, const int &offset); +}; + diff --git a/hbase-native-client/core/mutation.cc b/hbase-native-client/core/mutation.cc new file mode 100644 index 0000000..648a173 --- /dev/null +++ b/hbase-native-client/core/mutation.cc @@ -0,0 +1,174 @@ +/* + * 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 "mutation.h" + +#include +#include + +#include "exception.h" + +/** + * The attribute for storing the list of clusters that have consumed the change. + */ +const std::string Mutation::CONSUMED_CLUSTER_IDS = "_cs.id"; + +/** + * The attribute for storing TTL for the result of the mutation. + */ +const std::string Mutation::OP_ATTRIBUTE_TTL = "_ttl"; + +Mutation::~Mutation() { + +} + +Mutation::Mutation() + : ts_(HBaseConstants::HConstants::LATEST_TIMESTAMP), + durability_(HBaseConstants::DURABILITY_TYPE::USE_DEFAULT) { + +} + +Mutation::Mutation(const Mutation &cmutation) { + + this->row_ = cmutation.row_; + this->ts_ = cmutation.ts_; + this->family_map_ = cmutation.family_map_; +} + +Mutation& Mutation::operator=(const Mutation &cmutation) { + + this->row_ = cmutation.row_; + this->ts_ = cmutation.ts_; + this->family_map_ = cmutation.family_map_; + + return *this; + +} +int Mutation::GetAcl(BYTE_ARRAY &attr_value) { + return GetAttribute(HBaseConstants::AccessControlConstants::OP_ATTRIBUTE_ACL, + attr_value); +} + +Mutation Mutation::SetAcl(const std::string &user, Permission perms) { + BYTE_ARRAY attr_value; + SetAttribute(HBaseConstants::AccessControlConstants::OP_ATTRIBUTE_ACL, + attr_value); + return *this; +} + +Mutation Mutation::SetAcl(const std::map &perms) { + BYTE_ARRAY attr_value; + std::map::const_iterator itr; + for (itr = perms.begin(); itr != perms.end(); ++itr) { + + } + SetAttribute(HBaseConstants::AccessControlConstants::OP_ATTRIBUTE_ACL, + attr_value); + return *this; +} + +const long Mutation::GetTtl(BYTE_ARRAY &attr_value) { + + long ttl = HBaseConstants::HConstants::LATEST_TIMESTAMP; + int attr_size = GetAttribute(Mutation::OP_ATTRIBUTE_TTL, attr_value); + if (attr_size > 0) { + ttl = Bytes::ToLong(attr_value); + } + return ttl; +} + +Mutation Mutation::SetTtl(const long &ttl) { + + BYTE_ARRAY attr_value; + Bytes::ToBytes(ttl, attr_value); + SetAttribute(Mutation::OP_ATTRIBUTE_TTL, attr_value); + return *this; +} + +ReturnCodes Mutation::CheckRow(const BYTE_ARRAY &row) { + + int row_length = row.size(); + if (0 == row_length) { + throw HbaseException("Row length can't be 0"); + } + if (row_length > HBaseConstants::HConstants::MAX_ROW_LENGTH) { + std::stringstream str_error; + str_error.str(""); + str_error << "Row length: " << row_length + << " is greater than max row size: " + << HBaseConstants::HConstants::MAX_ROW_LENGTH << std::endl; + throw HbaseException(str_error.str()); + } + return ReturnCodes::SUCCESS; +} + +KeyValue *Mutation::CreatePutKeyValue(const BYTE_ARRAY &family, + const BYTE_ARRAY &qualifier, + const long &ts, const BYTE_ARRAY &value) { + KeyValue *key_value = nullptr; + key_value = new KeyValue(this->row_, family, qualifier, ts, value); + return key_value; +} + +const std::string Mutation::GetRowAsString() const { + const std::string row_str(Bytes::ToString(this->row_)); + return row_str; +} + +const FAMILY_MAP &Mutation::GetFamilyMap() const { + + return this->family_map_; +} + +void Mutation::Display() { + + Bytes::DisplayBytes(this->row_); + DLOG(INFO) << "Mutate TS: " << this->ts_; + for (const auto &family : this->family_map_) { + std::string qual_val_ts(""); + for (const auto &key_value : family.second) { + BYTE_ARRAY qualifier; + BYTE_ARRAY value; + if (key_value.GetQualifierLength() > 0) { + Bytes::CopyByteArray(key_value.GetQualifierArray(), qualifier, + key_value.GetQualifierOffset(), + key_value.GetQualifierLength()); + } + if (key_value.GetValueLength() > 0) { + Bytes::CopyByteArray(key_value.GetValueArray(), value, + key_value.GetValueOffset(), + key_value.GetValueLength()); + } + const long ×tamp = key_value.GetTimestamp(); + qual_val_ts += ( + (key_value.GetQualifierLength() > 0) ? + Bytes::ToString(qualifier) : "NO_QUALIFIER"); + qual_val_ts += "::"; + qual_val_ts += ( + (key_value.GetQualifierLength() > 0) ? + Bytes::ToString(value) : "NO_VALUE"); + qual_val_ts += "::"; + qual_val_ts += std::to_string(timestamp); + qual_val_ts += ";"; + } + DLOG(INFO) << Bytes::ToString(family.first) << "[" << qual_val_ts << "]"; + } + +} + diff --git a/hbase-native-client/core/mutation.h b/hbase-native-client/core/mutation.h new file mode 100644 index 0000000..2c4242c --- /dev/null +++ b/hbase-native-client/core/mutation.h @@ -0,0 +1,80 @@ +/* + * 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 "bytes.h" +#include "hconstants.h" +#include "key_value.h" +#include "operation_with_attributes.h" +#include "permission.h" +#include "return_codes.h" + +using FAMILY_MAP = std::map>; + +enum class DELETE_TYPE { + DELETE_ONE_VERSION, + DELETE_MULTIPLE_VERSIONS, + DELETE_FAMILY, + DELETE_FAMILY_VERSION +}; + +class Mutation : public OperationWithAttributes { + + public: + + Mutation(); + Mutation(const Mutation &cmutation); + Mutation& operator=(const Mutation &cmutation); + virtual ~Mutation(); + int GetAcl(BYTE_ARRAY &attr_value); + Mutation SetAcl(const std::string &user, Permission perms); + Mutation SetAcl(const std::map &perms); + const long GetTtl(BYTE_ARRAY &attr_value); + Mutation SetTtl(const long &ttl); + KeyValue *CreatePutKeyValue(const BYTE_ARRAY &family, + const BYTE_ARRAY &qualifier, const long &ts, + const BYTE_ARRAY &value); + const std::string GetRowAsString() const; + const FAMILY_MAP &GetFamilyMap() const; + void Display(); + static ReturnCodes CheckRow(const BYTE_ARRAY &row); + + protected: + + BYTE_ARRAY row_; + long ts_; + HBaseConstants::DURABILITY_TYPE durability_; + FAMILY_MAP family_map_; + + private: + + /** + * The attribute for storing the list of clusters that have consumed the change. + */ + static const std::string CONSUMED_CLUSTER_IDS; + + /** + * The attribute for storing TTL for the result of the mutation. + */ + static const std::string OP_ATTRIBUTE_TTL; +}; diff --git a/hbase-native-client/core/operation_with_attributes.cc b/hbase-native-client/core/operation_with_attributes.cc new file mode 100644 index 0000000..9dadd35 --- /dev/null +++ b/hbase-native-client/core/operation_with_attributes.cc @@ -0,0 +1,87 @@ +/* + * 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 "operation_with_attributes.h" + +#include + +const std::string OperationWithAttributes::ID_ATRIBUTE = "_operation.attributes.id"; + +OperationWithAttributes::OperationWithAttributes() { + // TODO Auto-generated constructor stub + +} + +OperationWithAttributes::~OperationWithAttributes() { + // TODO Auto-generated destructor stub +} + + +int OperationWithAttributes::GetAttribute(const std::string &name, BYTE_ARRAY &attr_value) { + + int attr_size = 0; + if (attributes_.end() != attributes_.find(name)) { + attr_size = attributes_[name].size(); + } + return attr_size; +} + +const OperationWithAttributes::ATTRIBUTE_MAP& OperationWithAttributes::GetAttributesMap() { + + return attributes_; +} + +const long OperationWithAttributes::GetAttributeSize() { + + return attributes_.size(); // total of num map elements + num of keys + vector size in Java + +} + +OperationWithAttributes OperationWithAttributes::SetAttribute(const std::string &name, const BYTE_ARRAY &value) { + + if (0 == value.size()) { + if(attributes_.size() > 0){ + attributes_[name].clear(); + attributes_.erase(name); + } + } else { + BYTE_ARRAY attr_value; + attr_value.insert (attr_value.end(), value.begin(), value.end()); + attributes_[name] = attr_value; + } + return *this; +} + +OperationWithAttributes OperationWithAttributes::SetId(const std::string &id) { + BYTE_ARRAY id_value; + id_value.insert(id_value.end(), id.begin(), id.end()); + SetAttribute(OperationWithAttributes::ID_ATRIBUTE, id_value); + return *this; +} + +const int OperationWithAttributes::GetId(std::string &id_string) { + BYTE_ARRAY id_value; + int attr_size = GetAttribute(OperationWithAttributes::ID_ATRIBUTE, id_value); + id_string = ""; + if (id_value.size() > 0) { + id_string.insert(id_string.end(), id_value.begin(), id_value.end()); + } + return attr_size; +} + diff --git a/hbase-native-client/core/operation_with_attributes.h b/hbase-native-client/core/operation_with_attributes.h new file mode 100644 index 0000000..e09031d --- /dev/null +++ b/hbase-native-client/core/operation_with_attributes.h @@ -0,0 +1,45 @@ +/* + * 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 "bytes.h" + + +class OperationWithAttributes { + + public: + typedef std::map ATTRIBUTE_MAP; + static const std::string ID_ATRIBUTE; + OperationWithAttributes(); + virtual ~OperationWithAttributes(); + int GetAttribute(const std::string & name, BYTE_ARRAY &attr_value); + const OperationWithAttributes::ATTRIBUTE_MAP& GetAttributesMap(); + const long GetAttributeSize(); + OperationWithAttributes SetAttribute(const std::string &name, const BYTE_ARRAY &value); + OperationWithAttributes SetId(const std::string &id); + const int GetId(std::string &id_string); + + private: + ATTRIBUTE_MAP attributes_; +}; diff --git a/hbase-native-client/core/permission.cc b/hbase-native-client/core/permission.cc new file mode 100644 index 0000000..1269a1e --- /dev/null +++ b/hbase-native-client/core/permission.cc @@ -0,0 +1,42 @@ +/* + * 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 "permission.h" + +Permission::ACTION_BY_CODE Permission::actionMap; + +void Permission::InitActionMap() { + + Permission::actionMap['R'] = Permission::READ; + Permission::actionMap['W'] = Permission::WRITE; + Permission::actionMap['X'] = Permission::EXEC; + Permission::actionMap['C'] = Permission::CREATE; + Permission::actionMap['A'] = Permission::ADMIN; +} + +Permission::Permission() { + // TODO Auto-generated constructor stub + +} + +Permission::~Permission() { + // TODO Auto-generated destructor stub +} + diff --git a/hbase-native-client/core/permission.h b/hbase-native-client/core/permission.h new file mode 100644 index 0000000..d82c1b9 --- /dev/null +++ b/hbase-native-client/core/permission.h @@ -0,0 +1,44 @@ +/* + * 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 "bytes.h" + +class Permission { + public: + typedef enum { + READ, + WRITE, + EXEC, + CREATE, + ADMIN + }ActionType; + + typedef std::map ACTION_BY_CODE; + static ACTION_BY_CODE actionMap; + Permission(); + virtual ~Permission(); + + private: + static void InitActionMap(); +}; + diff --git a/hbase-native-client/core/return_codes.h b/hbase-native-client/core/return_codes.h new file mode 100644 index 0000000..4533286 --- /dev/null +++ b/hbase-native-client/core/return_codes.h @@ -0,0 +1,33 @@ +/* + * 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 + +const std::string UNDEFINED_API("Undefined API Implementation"); + +enum class ReturnCodes { + SUCCESS = 0, + FAILURE = -1 +}; diff --git a/hbase-native-client/core/tag.cc b/hbase-native-client/core/tag.cc new file mode 100644 index 0000000..698d317 --- /dev/null +++ b/hbase-native-client/core/tag.cc @@ -0,0 +1,36 @@ +/* + * 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 "tag.h" +#include + +const int Tag::TYPE_LENGTH_SIZE = Bytes::SIZEOF_BYTE; +const int Tag::TAG_LENGTH_SIZE = Bytes::SIZEOF_SHORT; +const int Tag::INFRASTRUCTURE_SIZE = Tag::TYPE_LENGTH_SIZE + Tag::TAG_LENGTH_SIZE; +const int Tag::MAX_TAG_LENGTH = (2 * std::numeric_limits< short >::max()) + 1 - Tag::TAG_LENGTH_SIZE; + +Tag::Tag() { + // TODO Auto-generated constructor stub + +} + +Tag::~Tag() { + // TODO Auto-generated destructor stub +} + diff --git a/hbase-native-client/core/tag.h b/hbase-native-client/core/tag.h new file mode 100644 index 0000000..efe1b0c --- /dev/null +++ b/hbase-native-client/core/tag.h @@ -0,0 +1,39 @@ +/* + * 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 "bytes.h" + +class Tag { + + public: + const static int TYPE_LENGTH_SIZE; + const static int TAG_LENGTH_SIZE; + const static int INFRASTRUCTURE_SIZE; + const static int MAX_TAG_LENGTH; + Tag(); + virtual ~Tag(); + + private: + BYTE_TYPE type_; + BYTE_ARRAY bytes_; + int offset_; + int length_; +}; diff --git a/hbase-native-client/utils/Utils.cc b/hbase-native-client/utils/Utils.cc new file mode 100644 index 0000000..9f4b717 --- /dev/null +++ b/hbase-native-client/utils/Utils.cc @@ -0,0 +1,42 @@ +/* + * 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 "Utils.h" + +Utils::Utils() { + // TODO Auto-generated constructor stub + +} + +Utils::~Utils() { + // TODO Auto-generated destructor stub +} + +void Utils::swapByteOrder(unsigned int &ui) +{ + ui = (ui >> 24) | + ((ui<<8) & 0x00FF0000) | + ((ui>>8) & 0x0000FF00) | + (ui << 24); +} + +void Utils::swapByteOrder2Bytes(unsigned short &us) { + us = ( (((us) >> 8) & 0x00FF) | (((us) << 8) & 0xFF00) ); +} + diff --git a/hbase-native-client/utils/Utils.h b/hbase-native-client/utils/Utils.h new file mode 100644 index 0000000..0850959 --- /dev/null +++ b/hbase-native-client/utils/Utils.h @@ -0,0 +1,28 @@ +/* + * 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 + +class Utils { + public: + Utils(); + virtual ~Utils(); + static void swapByteOrder(unsigned int &ui); + static void swapByteOrder2Bytes(unsigned short &us); +}; -- 1.8.3.1