-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HBASE-25328 : Add builder method to create Tags
Closes #2707 Signed-off-by: Anoop Sam John <anoopsamjohn@apache.org> Signed-off-by: Geoffrey Jacoby <gjacoby@apache.org> Signed-off-by: Viraj Jasani <vjasani@apache.org>
- Loading branch information
1 parent
5d75a52
commit 245b439
Showing
4 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
hbase-common/src/main/java/org/apache/hadoop/hbase/TagBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
73 changes: 73 additions & 0 deletions
73
hbase-common/src/main/java/org/apache/hadoop/hbase/TagBuilderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
hbase-common/src/test/java/org/apache/hadoop/hbase/TestTagBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} | ||
} |