diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/RawCell.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/RawCell.java index ea598d21ca3..85f8b278de4 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/RawCell.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/RawCell.java @@ -18,6 +18,7 @@ package org.apache.hadoop.hbase; import java.util.Iterator; +import java.util.List; import java.util.Optional; import org.apache.yetus.audience.InterfaceAudience; @@ -64,4 +65,11 @@ public interface RawCell extends Cell { throw new IllegalArgumentException("tagslength " + tagsLength + " > " + MAX_TAGS_LENGTH); } } + + /** + * @return A new cell which is having the extra tags also added to it. + */ + public static Cell createCell(Cell cell, List tags) { + return PrivateCellUtil.createCell(cell, tags); + } } diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/TagBuilder.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/TagBuilder.java new file mode 100644 index 00000000000..372144c6c26 --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/TagBuilder.java @@ -0,0 +1,50 @@ +/** + * Copyright The Apache Software Foundation + * + * 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. + */ +package org.apache.hadoop.hbase; + +import java.nio.ByteBuffer; +import org.apache.yetus.audience.InterfaceAudience; + +/** + * Builder implementation to create {@link Tag} + * Call setTagValue(byte[]) method to create {@link ArrayBackedTag} + */ +@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC) +public interface TagBuilder { + /** + * Set type of the tag. + * @param tagType type of the tag + * @return {@link TagBuilder} + */ + TagBuilder setTagType(byte tagType); + + /** + * Set the value of the tag. + * @param tagBytes tag bytes. + * @return {@link TagBuilder} + */ + TagBuilder setTagValue(byte[] tagBytes); + + /** + * Build the tag. + * @return {@link Tag} + */ + Tag build(); +} diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/TagBuilderFactory.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/TagBuilderFactory.java new file mode 100644 index 00000000000..40744f91abf --- /dev/null +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/TagBuilderFactory.java @@ -0,0 +1,73 @@ +/** + * Copyright The Apache Software Foundation + * + * 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. + */ +package org.apache.hadoop.hbase; + +import java.nio.ByteBuffer; +import org.apache.yetus.audience.InterfaceAudience; + +/** + * Factory to create Tags. + */ +@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC) +public final class TagBuilderFactory { + + public static TagBuilder create() { + return new TagBuilderImpl(); + } +} + +/** + * Builder implementation to create {@link Tag}
+ * Call setTagValue(byte[]) method to create {@link ArrayBackedTag} + */ +class TagBuilderImpl implements TagBuilder { + // This assumes that we never create tag with value less than 0. + private byte tagType = (byte)-1; + private byte[] tagBytes = null; + public static final String TAG_TYPE_NOT_SET_EXCEPTION = "Need to set type of the tag."; + public static final String TAG_VALUE_NULL_EXCEPTION = "TagBytes can't be null"; + + @Override + public TagBuilder setTagType(byte tagType) { + this.tagType = tagType; + return this; + } + + @Override + public TagBuilder setTagValue(byte[] tagBytes) { + this.tagBytes = tagBytes; + return this; + } + + private void validate() { + if (tagType == -1) { + throw new IllegalArgumentException(TAG_TYPE_NOT_SET_EXCEPTION); + } + if (tagBytes == null) { + throw new IllegalArgumentException(TAG_VALUE_NULL_EXCEPTION); + } + } + + @Override + public Tag build() { + validate(); + return new ArrayBackedTag(tagType, tagBytes); + } +} diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/TestTagBuilder.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/TestTagBuilder.java new file mode 100644 index 00000000000..b50aa2df645 --- /dev/null +++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/TestTagBuilder.java @@ -0,0 +1,78 @@ +/** + * Copyright The Apache Software Foundation + * + * 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. + */ +package org.apache.hadoop.hbase; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.nio.ByteBuffer; +import org.apache.hadoop.hbase.testclassification.MiscTests; +import org.apache.hadoop.hbase.testclassification.SmallTests; +import org.apache.hadoop.hbase.util.Bytes; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category({MiscTests.class, SmallTests.class}) +public class TestTagBuilder { + + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestTagBuilder.class); + + @Test + public void testArrayBackedTagBuilder() { + byte type = (byte)50; + String value = "Array-Backed-Tag"; + TagBuilder builder = TagBuilderFactory.create(); + assertTrue(builder instanceof TagBuilderImpl); + builder.setTagType(type); + builder.setTagValue(Bytes.toBytes(value)); + Tag tag = builder.build(); + assertEquals(value, Tag.getValueAsString(tag)); + assertEquals(type, tag.getType()); + } + + @Test + public void testErrorMessages() { + String arrayValue = "Array-Backed-Tag"; + TagBuilder builder = TagBuilderFactory.create(); + builder.setTagValue(Bytes.toBytes(arrayValue)); + try { + // Dont set type for the tag. + builder.build(); + fail("Shouldn't have come here."); + } catch(IllegalArgumentException iae) { + assertTrue(iae.getMessage().contains(TagBuilderImpl.TAG_TYPE_NOT_SET_EXCEPTION)); + } + + byte type = (byte)50; + builder = TagBuilderFactory.create(); + builder.setTagType(type); + try { + // Need to Call setTagValue(byte[]) to set the value. + builder.build(); + fail("Shouldn't have come here."); + } catch(IllegalArgumentException iae) { + assertTrue(iae.getMessage().contains(TagBuilderImpl.TAG_VALUE_NULL_EXCEPTION)); + } + } +}