diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/io/encoding/BufferedDataBlockEncoder.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/io/encoding/BufferedDataBlockEncoder.java index a4b3857..d261fa1 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/io/encoding/BufferedDataBlockEncoder.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/io/encoding/BufferedDataBlockEncoder.java @@ -137,7 +137,7 @@ abstract class BufferedDataBlockEncoder implements DataBlockEncoder { } } - protected void createKeyOnlyKeyValue(byte[] keyBuffer, long memTS) { + protected void setKey(byte[] keyBuffer, long memTS) { currentKey.setKey(keyBuffer, 0, keyLength); memstoreTS = memTS; } @@ -302,11 +302,8 @@ abstract class BufferedDataBlockEncoder implements DataBlockEncoder { @Override public String toString() { - KeyValue kv = KeyValueUtil.copyToNewKeyValue(this); - if (kv == null) { - return "null"; - } - return kv.toString(); + return KeyValue.keyToString(this.keyBuffer, 0, KeyValueUtil.keyLength(this)) + "/vlen=" + + getValueLength() + "/seqid=" + memstoreTS; } public Cell shallowCopy() { @@ -314,7 +311,7 @@ abstract class BufferedDataBlockEncoder implements DataBlockEncoder { currentKey.getFamilyOffset(), currentKey.getFamilyLength(), keyLength, currentKey.getQualifierOffset(), currentKey.getQualifierLength(), currentKey.getTimestamp(), currentKey.getTypeByte(), valueLength, valueOffset, - memstoreTS, tagsOffset, tagsLength, tagCompressionContext, tagsBuffer); + memstoreTS, tagsOffset, tagsLength, (tagCompressionContext != null), tagsBuffer); } } @@ -347,16 +344,16 @@ abstract class BufferedDataBlockEncoder implements DataBlockEncoder { private int tagsOffset; private byte[] cloneTagsBuffer; private long seqId; - private TagCompressionContext tagCompressionContext; + private boolean tagsCompressed; protected ClonedSeekerState(ByteBuffer currentBuffer, byte[] keyBuffer, short rowLength, int familyOffset, byte familyLength, int keyLength, int qualOffset, int qualLength, long timeStamp, byte typeByte, int valueLen, int valueOffset, long seqId, - int tagsOffset, int tagsLength, TagCompressionContext tagCompressionContext, + int tagsOffset, int tagsLength, boolean tagsCompressed, byte[] tagsBuffer) { this.currentBuffer = currentBuffer; keyOnlyBuffer = new byte[keyLength]; - this.tagCompressionContext = tagCompressionContext; + this.tagsCompressed = tagsCompressed; this.rowLength = rowLength; this.familyOffset = familyOffset; this.familyLength = familyLength; @@ -369,7 +366,7 @@ abstract class BufferedDataBlockEncoder implements DataBlockEncoder { this.tagsOffset = tagsOffset; this.tagsLength = tagsLength; System.arraycopy(keyBuffer, 0, keyOnlyBuffer, 0, keyLength); - if (tagCompressionContext != null) { + if (tagsCompressed) { this.cloneTagsBuffer = new byte[tagsLength]; System.arraycopy(tagsBuffer, 0, this.cloneTagsBuffer, 0, tagsLength); } @@ -459,7 +456,7 @@ abstract class BufferedDataBlockEncoder implements DataBlockEncoder { @Override public byte[] getTagsArray() { - if (tagCompressionContext != null) { + if (tagsCompressed) { return cloneTagsBuffer; } return currentBuffer.array(); @@ -467,7 +464,7 @@ abstract class BufferedDataBlockEncoder implements DataBlockEncoder { @Override public int getTagsOffset() { - if (tagCompressionContext != null) { + if (tagsCompressed) { return 0; } return currentBuffer.arrayOffset() + tagsOffset; @@ -576,7 +573,7 @@ abstract class BufferedDataBlockEncoder implements DataBlockEncoder { current.tagCompressionContext = tagCompressionContext; } decodeFirst(); - current.createKeyOnlyKeyValue(current.keyBuffer, current.memstoreTS); + current.setKey(current.keyBuffer, current.memstoreTS); previous.invalidate(); } @@ -589,9 +586,10 @@ abstract class BufferedDataBlockEncoder implements DataBlockEncoder { @Override public ByteBuffer getValueShallowCopy() { - return ByteBuffer.wrap(currentBuffer.array(), - currentBuffer.arrayOffset() + current.valueOffset, - current.valueLength); + ByteBuffer dup = currentBuffer.duplicate(); + dup.position(current.valueOffset); + dup.limit(current.valueOffset + current.valueLength); + return dup.slice(); } @Override @@ -600,8 +598,7 @@ abstract class BufferedDataBlockEncoder implements DataBlockEncoder { kvBuffer.putInt(current.keyLength); kvBuffer.putInt(current.valueLength); kvBuffer.put(current.keyBuffer, 0, current.keyLength); - kvBuffer.put(currentBuffer.array(), - currentBuffer.arrayOffset() + current.valueOffset, + ByteBufferUtils.copyFromBufferToBuffer(kvBuffer, currentBuffer, current.valueOffset, current.valueLength); if (current.tagsLength > 0) { // Put short as unsigned @@ -610,7 +607,7 @@ abstract class BufferedDataBlockEncoder implements DataBlockEncoder { if (current.tagsOffset != -1) { // the offset of the tags bytes in the underlying buffer is marked. So the temp // buffer,tagsBuffer was not been used. - kvBuffer.put(currentBuffer.array(), currentBuffer.arrayOffset() + current.tagsOffset, + ByteBufferUtils.copyFromBufferToBuffer(kvBuffer, currentBuffer, current.tagsOffset, current.tagsLength); } else { // When tagsOffset is marked as -1, tag compression was present and so the tags were @@ -640,7 +637,7 @@ abstract class BufferedDataBlockEncoder implements DataBlockEncoder { tagCompressionContext.clear(); } decodeFirst(); - current.createKeyOnlyKeyValue(current.keyBuffer, current.memstoreTS); + current.setKey(current.keyBuffer, current.memstoreTS); previous.invalidate(); } @@ -650,7 +647,7 @@ abstract class BufferedDataBlockEncoder implements DataBlockEncoder { return false; } decodeNext(); - current.createKeyOnlyKeyValue(current.keyBuffer, current.memstoreTS); + current.setKey(current.keyBuffer, current.memstoreTS); previous.invalidate(); return true; } @@ -673,7 +670,7 @@ abstract class BufferedDataBlockEncoder implements DataBlockEncoder { } current.tagsOffset = -1; } else { - // When tag compress is not used, let us not do temp copying of tags bytes into tagsBuffer. + // When tag compress is not used, let us not do copying of tags bytes into tagsBuffer. // Just mark the tags Offset so as to create the KV buffer later in getKeyValueBuffer() current.tagsOffset = currentBuffer.position(); ByteBufferUtils.skip(currentBuffer, current.tagsLength); @@ -784,7 +781,7 @@ abstract class BufferedDataBlockEncoder implements DataBlockEncoder { if (currentBuffer.hasRemaining()) { previous.copyFromNext(current); decodeNext(); - current.createKeyOnlyKeyValue(current.keyBuffer, current.memstoreTS); + current.setKey(current.keyBuffer, current.memstoreTS); } else { break; } diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/io/encoding/CopyKeyDataBlockEncoder.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/io/encoding/CopyKeyDataBlockEncoder.java index 3baebc2..1c757f5 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/io/encoding/CopyKeyDataBlockEncoder.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/io/encoding/CopyKeyDataBlockEncoder.java @@ -68,11 +68,13 @@ public class CopyKeyDataBlockEncoder extends BufferedDataBlockEncoder { @Override public ByteBuffer getFirstKeyInBlock(ByteBuffer block) { int keyLength = block.getInt(Bytes.SIZEOF_INT); - return ByteBuffer.wrap(block.array(), - block.arrayOffset() + 3 * Bytes.SIZEOF_INT, keyLength).slice(); + ByteBuffer dup = block.duplicate(); + int pos = 3 * Bytes.SIZEOF_INT; + dup.position(pos); + dup.limit(pos + keyLength); + return dup.slice(); } - @Override public String toString() { return CopyKeyDataBlockEncoder.class.getSimpleName(); @@ -123,5 +125,4 @@ public class CopyKeyDataBlockEncoder extends BufferedDataBlockEncoder { return buffer; } - } diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/io/encoding/DiffKeyDeltaEncoder.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/io/encoding/DiffKeyDeltaEncoder.java index c39822f..21de7cb 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/io/encoding/DiffKeyDeltaEncoder.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/io/encoding/DiffKeyDeltaEncoder.java @@ -317,21 +317,25 @@ public class DiffKeyDeltaEncoder extends BufferedDataBlockEncoder { ByteBuffer result = ByteBuffer.allocate(keyLength); // copy row + // result is HBB. So no problem of calling array()/ arrayOffset() int pos = result.arrayOffset(); block.get(result.array(), pos, Bytes.SIZEOF_SHORT); pos += Bytes.SIZEOF_SHORT; short rowLength = result.getShort(); + // result is HBB. So no problem of calling array() block.get(result.array(), pos, rowLength); pos += rowLength; // copy family int savePosition = block.position(); block.position(Bytes.SIZEOF_INT); + // result is HBB. So no problem of calling array() block.get(result.array(), pos, familyLength + Bytes.SIZEOF_BYTE); pos += familyLength + Bytes.SIZEOF_BYTE; // copy qualifier block.position(savePosition); + // result is HBB. So no problem of calling array()/ arrayOffset() int qualifierLength = keyLength - pos + result.arrayOffset() - KeyValue.TIMESTAMP_TYPE_SIZE; block.get(result.array(), pos, qualifierLength); @@ -346,6 +350,7 @@ public class DiffKeyDeltaEncoder extends BufferedDataBlockEncoder { } result.putLong(pos, timestamp); pos += Bytes.SIZEOF_LONG; + // result is HBB. So no problem of calling array() block.get(result.array(), pos, Bytes.SIZEOF_BYTE); block.reset(); diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/io/encoding/FastDiffDeltaEncoder.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/io/encoding/FastDiffDeltaEncoder.java index 94069bf..5e28479 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/io/encoding/FastDiffDeltaEncoder.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/io/encoding/FastDiffDeltaEncoder.java @@ -362,8 +362,10 @@ public class FastDiffDeltaEncoder extends BufferedDataBlockEncoder { ByteBufferUtils.readCompressedInt(block); // commonLength int pos = block.position(); block.reset(); - return ByteBuffer.wrap(block.array(), block.arrayOffset() + pos, keyLength) - .slice(); + ByteBuffer dup = block.duplicate(); + dup.position(pos); + dup.limit(pos + keyLength); + return dup.slice(); } @Override diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/io/encoding/PrefixKeyDeltaEncoder.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/io/encoding/PrefixKeyDeltaEncoder.java index 8ff71a9..8c17102 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/io/encoding/PrefixKeyDeltaEncoder.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/io/encoding/PrefixKeyDeltaEncoder.java @@ -184,8 +184,10 @@ public class PrefixKeyDeltaEncoder extends BufferedDataBlockEncoder { } int pos = block.position(); block.reset(); - return ByteBuffer.wrap(block.array(), block.arrayOffset() + pos, keyLength) - .slice(); + ByteBuffer dup = block.duplicate(); + dup.position(pos); + dup.limit(pos + keyLength); + return dup.slice(); } @Override diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/ByteBufferUtils.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/ByteBufferUtils.java index b4c6690..d4aa25a 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/ByteBufferUtils.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/ByteBufferUtils.java @@ -331,7 +331,10 @@ public final class ByteBufferUtils { } /** - * Copy from one buffer to another from given offset + * Copy from one buffer to another from given offset. + *
+ * Note : This will advance the position in out buffer but no change in position for
+ * the position in in buffer.
* @param out destination buffer
* @param in source buffer
* @param sourceOffset offset in the source buffer