You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by vj...@apache.org on 2020/12/08 12:54:44 UTC

[hbase] branch branch-2.2 updated: HBASE-25328 : Add builder method to create Tags

This is an automated email from the ASF dual-hosted git repository.

vjasani pushed a commit to branch branch-2.2
in repository https://gitbox.apache.org/repos/asf/hbase.git


The following commit(s) were added to refs/heads/branch-2.2 by this push:
     new 245b439  HBASE-25328 : Add builder method to create Tags
245b439 is described below

commit 245b439eedcff1a7b02c907f1f0a6d5935be567f
Author: shahrs87 <sh...@gmail.com>
AuthorDate: Tue Dec 8 17:58:00 2020 +0530

    HBASE-25328 : Add builder method to create Tags
    
    Closes #2707
    
    Signed-off-by: Anoop Sam John <an...@apache.org>
    Signed-off-by: Geoffrey Jacoby <gj...@apache.org>
    Signed-off-by: Viraj Jasani <vj...@apache.org>
---
 .../main/java/org/apache/hadoop/hbase/RawCell.java |  8 +++
 .../java/org/apache/hadoop/hbase/TagBuilder.java   | 50 ++++++++++++++
 .../org/apache/hadoop/hbase/TagBuilderFactory.java | 73 ++++++++++++++++++++
 .../org/apache/hadoop/hbase/TestTagBuilder.java    | 78 ++++++++++++++++++++++
 4 files changed, 209 insertions(+)

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 ea598d2..85f8b27 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<Tag> 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 0000000..372144c
--- /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 0000000..40744f9
--- /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}<br>
+ * 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 0000000..b50aa2d
--- /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));
+    }
+  }
+}