HBASE-16612 Use array to cache Types for KeyValue.Type.codeToType (Phil Yang)

This commit is contained in:
tedyu 2016-09-13 09:54:27 -07:00
parent a602aaf9ba
commit 981200bf13
1 changed files with 11 additions and 4 deletions

View File

@ -252,6 +252,14 @@ public class KeyValue implements Cell, HeapSize, Cloneable, SettableSequenceId,
return this.code;
}
private static Type[] codeArray = new Type[256];
static {
for (Type t : Type.values()) {
codeArray[t.code & 0xff] = t;
}
}
/**
* Cannot rely on enum ordinals . They change if item is removed or moved.
* Do our own codes.
@ -259,10 +267,9 @@ public class KeyValue implements Cell, HeapSize, Cloneable, SettableSequenceId,
* @return Type associated with passed code.
*/
public static Type codeToType(final byte b) {
for (Type t : Type.values()) {
if (t.getCode() == b) {
return t;
}
Type t = codeArray[b & 0xff];
if (t != null) {
return t;
}
throw new RuntimeException("Unknown code " + b);
}