HBASE-19746 Add default impl to Cell#getType

This commit is contained in:
Chia-Ping Tsai 2018-01-11 15:03:20 -08:00 committed by Michael Stack
parent 1a11fc92b1
commit 3e6f80dcd5
No known key found for this signature in database
GPG Key ID: 9816C7FC8ACC93D2
6 changed files with 47 additions and 34 deletions

View File

@ -152,10 +152,6 @@ public class ByteBufferKeyOnlyKeyValue extends ByteBufferExtendedCell {
return ByteBufferUtils.toByte(this.buf, this.offset + this.length - 1);
}
public Type getType() {
return PrivateCellUtil.toType(getTypeByte());
}
@Override
public void setSequenceId(long seqId) throws IOException {
throw new IllegalArgumentException("This is a key only Cell");

View File

@ -201,10 +201,19 @@ public interface Cell {
int getTagsLength();
/**
* Returns the type of cell in a human readable format using {@link Type}
* Returns the type of cell in a human readable format using {@link Type}.
* Note : This does not expose the internal types of Cells like {@link KeyValue.Type#Maximum} and
* {@link KeyValue.Type#Minimum}
* @return The data type this cell: one of Put, Delete, etc
*/
Type getType();
default Type getType() {
byte byteType = getTypeByte();
Type t = Type.CODE_ARRAY[byteType & 0xff];
if (t != null) {
return t;
}
throw new UnsupportedOperationException("Invalid type of cell " + byteType);
}
/**
* The valid types for user to build the cell. Currently, This is subset of {@link KeyValue.Type}.
@ -229,5 +238,13 @@ public interface Cell {
public byte getCode() {
return this.code;
}
private static final Type[] CODE_ARRAY = new Type[256];
static {
for (Type t : Type.values()) {
CODE_ARRAY[t.code & 0xff] = t;
}
}
}
}

View File

@ -162,17 +162,6 @@ public interface ExtendedCell extends RawCell, HeapSize, Cloneable {
*/
int getTagsLength();
/**
* {@inheritDoc}
* <p>
* Note : This does not expose the internal types of Cells like {@link KeyValue.Type#Maximum} and
* {@link KeyValue.Type#Minimum}
*/
@Override
default Type getType() {
return PrivateCellUtil.toType(getTypeByte());
}
/**
* @return The byte representation of the KeyValue.TYPE of this cell: one of Put, Delete, etc
*/

View File

@ -21,6 +21,7 @@ package org.apache.hadoop.hbase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@ -36,6 +37,7 @@ import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.mockito.Mockito;
@Category({MiscTests.class, SmallTests.class})
public class TestCellUtil {
@ -195,11 +197,6 @@ public class TestCellUtil {
// TODO Auto-generated method stub
return 0;
}
@Override
public Type getType() {
return PrivateCellUtil.toType(getTypeByte());
}
}
/**
@ -522,6 +519,30 @@ public class TestCellUtil {
assertTrue(CellUtil.equals(kv, res));
}
@Test
public void testGetType() throws IOException {
Cell c = Mockito.mock(Cell.class);
Mockito.when(c.getType()).thenCallRealMethod();
for (Cell.Type type : Cell.Type.values()) {
Mockito.when(c.getTypeByte()).thenReturn(type.getCode());
assertEquals(type, c.getType());
}
try {
Mockito.when(c.getTypeByte()).thenReturn(KeyValue.Type.Maximum.getCode());
c.getType();
fail("The code of Maximum can't be handled by Cell.Type");
} catch(UnsupportedOperationException e) {
}
try {
Mockito.when(c.getTypeByte()).thenReturn(KeyValue.Type.Minimum.getCode());
c.getType();
fail("The code of Maximum can't be handled by Cell.Type");
} catch(UnsupportedOperationException e) {
}
}
private static class NonExtendedCell implements Cell {
private KeyValue kv;
@ -618,10 +639,5 @@ public class TestCellUtil {
public int getTagsLength() {
return this.kv.getTagsLength();
}
@Override
public Type getType() {
return PrivateCellUtil.toType(getTypeByte());
}
}
}

View File

@ -737,10 +737,5 @@ public class TestKeyValue extends TestCase {
public byte[] getTagsArray() {
return this.kv.getTagsArray();
}
@Override
public Type getType() {
return PrivateCellUtil.toType(getTypeByte());
}
}
}