Index: hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/TestCacheOnWrite.java =================================================================== --- hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/TestCacheOnWrite.java (revision 1521751) +++ hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/TestCacheOnWrite.java (working copy) @@ -261,7 +261,7 @@ BlockType cachedDataBlockType = encoderType.encodeInCache ? BlockType.ENCODED_DATA : BlockType.DATA; assertEquals("{" + cachedDataBlockType - + "=1379, LEAF_INDEX=173, BLOOM_CHUNK=9, INTERMEDIATE_INDEX=24}", + + "=1379, LEAF_INDEX=154, BLOOM_CHUNK=9, INTERMEDIATE_INDEX=18}", countByType); reader.close(); Index: hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValue.java =================================================================== --- hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValue.java (revision 1521751) +++ hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValue.java (working copy) @@ -1954,34 +1954,25 @@ && leftKey[ROW_LENGTH_SIZE + diffIdx] == rightKey[ROW_LENGTH_SIZE + diffIdx]) { diffIdx++; } + byte[] newRowKey = null; if (diffIdx >= minLength) { - // leftKey's row is prefix of rightKey's. we can optimize it in future - return Arrays.copyOf(rightKey, rightKey.length); - } - int diffByte = leftKey[ROW_LENGTH_SIZE + diffIdx]; - if ((0xff & diffByte) < 0xff && (diffByte + 1) < - (rightKey[ROW_LENGTH_SIZE + diffIdx] & 0xff)) { - byte[] newRowKey = new byte[diffIdx + 1]; - System.arraycopy(leftKey, ROW_LENGTH_SIZE, newRowKey, 0, diffIdx); - newRowKey[diffIdx] = (byte) (diffByte + 1); - int rightFamilyLength = rightKey[rightCommonLength - 1]; - byte[] family = null; - if (rightFamilyLength > 0) { - family = new byte[rightFamilyLength]; - System.arraycopy(rightKey, rightCommonLength, family, 0, rightFamilyLength); + // leftKey's row is prefix of rightKey's. + newRowKey = new byte[diffIdx + 1]; + System.arraycopy(rightKey, ROW_LENGTH_SIZE, newRowKey, 0, diffIdx + 1); + } else { + int diffByte = leftKey[ROW_LENGTH_SIZE + diffIdx]; + if ((0xff & diffByte) < 0xff && (diffByte + 1) < + (rightKey[ROW_LENGTH_SIZE + diffIdx] & 0xff)) { + newRowKey = new byte[diffIdx + 1]; + System.arraycopy(leftKey, ROW_LENGTH_SIZE, newRowKey, 0, diffIdx); + newRowKey[diffIdx] = (byte) (diffByte + 1); + } else { + newRowKey = new byte[diffIdx + 1]; + System.arraycopy(rightKey, ROW_LENGTH_SIZE, newRowKey, 0, diffIdx + 1); } - int rightQualifierLength = rightColumnLength - rightFamilyLength; - byte[] qualifier = null; - if (rightQualifierLength > 0) { - qualifier = new byte[rightQualifierLength]; - System.arraycopy(rightKey, rightCommonLength + rightFamilyLength, qualifier, 0, - rightQualifierLength); - } - return new KeyValue(newRowKey, null, null, HConstants.LATEST_TIMESTAMP, - Type.Maximum).getKey(); } - // the following is optimizable in future - return Arrays.copyOf(rightKey, rightKey.length); + return new KeyValue(newRowKey, null, null, HConstants.LATEST_TIMESTAMP, + Type.Maximum).getKey(); } @Override Index: hbase-common/src/test/java/org/apache/hadoop/hbase/TestKeyValue.java =================================================================== --- hbase-common/src/test/java/org/apache/hadoop/hbase/TestKeyValue.java (revision 1521751) +++ hbase-common/src/test/java/org/apache/hadoop/hbase/TestKeyValue.java (working copy) @@ -504,6 +504,21 @@ assertTrue(keyComparator.compareFlatKey(kv1.getKey(), kv2.getKey()) < 0); newKey = keyComparator.getShortMidpointKey(kv1.getKey(), kv2.getKey()); assertTrue(keyComparator.compareFlatKey(kv1.getKey(), newKey) < 0); - assertTrue(keyComparator.compareFlatKey(newKey, kv2.getKey()) == 0); + assertTrue(keyComparator.compareFlatKey(newKey, kv2.getKey()) < 0); + newRowLength = Bytes.toShort(newKey, 0); + expectedArray = Bytes.toBytes("ilovehbasea"); + Bytes.equals(newKey, KeyValue.ROW_LENGTH_SIZE, newRowLength, expectedArray, 0, + expectedArray.length); + //verify only 1 offset scenario + kv1 = new KeyValue(Bytes.toBytes("100abcdefg"), family, qualA, ts, Type.Put); + kv2 = new KeyValue(Bytes.toBytes("101abcdefg"), family, qualA, ts, Type.Put); + assertTrue(keyComparator.compareFlatKey(kv1.getKey(), kv2.getKey()) < 0); + newKey = keyComparator.getShortMidpointKey(kv1.getKey(), kv2.getKey()); + assertTrue(keyComparator.compareFlatKey(kv1.getKey(), newKey) < 0); + assertTrue(keyComparator.compareFlatKey(newKey, kv2.getKey()) < 0); + newRowLength = Bytes.toShort(newKey, 0); + expectedArray = Bytes.toBytes("101"); + Bytes.equals(newKey, KeyValue.ROW_LENGTH_SIZE, newRowLength, expectedArray, 0, + expectedArray.length); } }