Skip to content

Commit

Permalink
HBASE-14063 Use BufferBackedCell in read path after HBASE-12213 and
Browse files Browse the repository at this point in the history
  • Loading branch information
ramkrish86 committed Jul 27, 2015
1 parent ac08b99 commit 3f80e0e
Show file tree
Hide file tree
Showing 14 changed files with 663 additions and 145 deletions.
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,4 @@
/** /**
* Copyright The Apache Software Foundation
* *
* Licensed to the Apache Software Foundation (ASF) under one * Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file * or more contributor license agreements. See the NOTICE file
Expand Down Expand Up @@ -27,31 +26,55 @@


/** /**
* This is a key only Cell implementation which is identical to {@link KeyValue.KeyOnlyKeyValue} * This is a key only Cell implementation which is identical to {@link KeyValue.KeyOnlyKeyValue}
* with respect to key serialization but have its data in off heap memory. * with respect to key serialization but have its data in the form of Byte buffer
* (onheap and offheap).
*/ */
@InterfaceAudience.Private @InterfaceAudience.Private
public class OffheapKeyOnlyKeyValue extends ByteBufferedCell { public class ByteBufferedKeyOnlyKeyValue extends ByteBufferedCell {


private ByteBuffer buf; private ByteBuffer buf;
private int offset = 0; // offset into buffer where key starts at private int offset = 0; // offset into buffer where key starts at
private int length = 0; // length of this. private int length = 0; // length of this.
private short rowLen; private short rowLen;


public OffheapKeyOnlyKeyValue(ByteBuffer buf, int offset, int length) { /**
assert buf.isDirect(); * Used in cases where we want to avoid lot of garbage by allocating new objects with different
this.buf = buf; * keys. Use the emtpy construtor and set the keys using {@link #setKey(ByteBuffer, int, int)}
*/
public ByteBufferedKeyOnlyKeyValue() {
}

public ByteBufferedKeyOnlyKeyValue(ByteBuffer buf, int offset, int length) {
setKey(buf, offset, length);
}

/**
* A setter that helps to avoid object creation every time and whenever
* there is a need to create new OffheapKeyOnlyKeyValue.
* @param key
* @param offset
* @param length
*/
public void setKey(ByteBuffer key, int offset, int length) {
this.buf = key;
this.offset = offset; this.offset = offset;
this.length = length; this.length = length;
this.rowLen = ByteBufferUtils.toShort(this.buf, this.offset); this.rowLen = ByteBufferUtils.toShort(this.buf, this.offset);
} }


@Override @Override
public byte[] getRowArray() { public byte[] getRowArray() {
if (this.buf.hasArray()) {
return this.buf.array();
}
return CellUtil.cloneRow(this); return CellUtil.cloneRow(this);
} }


@Override @Override
public int getRowOffset() { public int getRowOffset() {
if (this.buf.hasArray()) {
return getRowPositionInByteBuffer() + this.buf.arrayOffset();
}
return 0; return 0;
} }


Expand All @@ -62,11 +85,17 @@ public short getRowLength() {


@Override @Override
public byte[] getFamilyArray() { public byte[] getFamilyArray() {
if (this.buf.hasArray()) {
return this.buf.array();
}
return CellUtil.cloneFamily(this); return CellUtil.cloneFamily(this);
} }


@Override @Override
public int getFamilyOffset() { public int getFamilyOffset() {
if (this.buf.hasArray()) {
return getFamilyPositionInByteBuffer() + this.buf.arrayOffset();
}
return 0; return 0;
} }


Expand All @@ -81,11 +110,17 @@ private byte getFamilyLength(int famLenPos) {


@Override @Override
public byte[] getQualifierArray() { public byte[] getQualifierArray() {
if (this.buf.hasArray()) {
return this.buf.array();
}
return CellUtil.cloneQualifier(this); return CellUtil.cloneQualifier(this);
} }


@Override @Override
public int getQualifierOffset() { public int getQualifierOffset() {
if (this.buf.hasArray()) {
return getQualifierPositionInByteBuffer() + this.buf.arrayOffset();
}
return 0; return 0;
} }


Expand Down
16 changes: 2 additions & 14 deletions hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValue.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.hbase.classification.InterfaceAudience; import org.apache.hadoop.hbase.classification.InterfaceAudience;
import org.apache.hadoop.hbase.io.ByteBufferOutputStream;
import org.apache.hadoop.hbase.io.HeapSize; import org.apache.hadoop.hbase.io.HeapSize;
import org.apache.hadoop.hbase.io.util.StreamUtils; import org.apache.hadoop.hbase.util.ByteBufferUtils;
import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.ClassSize; import org.apache.hadoop.hbase.util.ClassSize;
import org.apache.hadoop.io.IOUtils; import org.apache.hadoop.io.IOUtils;
Expand Down Expand Up @@ -2475,22 +2474,11 @@ public int write(OutputStream out, boolean withTags) throws IOException {
if (!withTags) { if (!withTags) {
length = this.getKeyLength() + this.getValueLength() + KEYVALUE_INFRASTRUCTURE_SIZE; length = this.getKeyLength() + this.getValueLength() + KEYVALUE_INFRASTRUCTURE_SIZE;
} }
writeInt(out, length); ByteBufferUtils.putInt(out, length);
out.write(this.bytes, this.offset, length); out.write(this.bytes, this.offset, length);
return length + Bytes.SIZEOF_INT; return length + Bytes.SIZEOF_INT;
} }


// This does same as DataOuput#writeInt (big-endian, etc.)
public static void writeInt(OutputStream out, int v) throws IOException {
// We have writeInt in ByteBufferOutputStream so that it can directly write int to underlying
// ByteBuffer in one step.
if (out instanceof ByteBufferOutputStream) {
((ByteBufferOutputStream) out).writeInt(v);
} else {
StreamUtils.writeInt(out, v);
}
}

/** /**
* Comparator that compares row component only of a KeyValue. * Comparator that compares row component only of a KeyValue.
*/ */
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -589,11 +589,11 @@ public static void oswrite(final Cell cell, final OutputStream out, final boolea
int tlen = cell.getTagsLength(); int tlen = cell.getTagsLength();


// write total length // write total length
KeyValue.writeInt(out, length(rlen, flen, qlen, vlen, tlen, withTags)); ByteBufferUtils.putInt(out, length(rlen, flen, qlen, vlen, tlen, withTags));
// write key length // write key length
KeyValue.writeInt(out, keyLength(rlen, flen, qlen)); ByteBufferUtils.putInt(out, keyLength(rlen, flen, qlen));
// write value length // write value length
KeyValue.writeInt(out, vlen); ByteBufferUtils.putInt(out, vlen);
// Write rowkey - 2 bytes rk length followed by rowkey bytes // Write rowkey - 2 bytes rk length followed by rowkey bytes
StreamUtils.writeShort(out, rlen); StreamUtils.writeShort(out, rlen);
out.write(cell.getRowArray(), cell.getRowOffset(), rlen); out.write(cell.getRowArray(), cell.getRowOffset(), rlen);
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.OutputStream; import java.io.OutputStream;


import org.apache.hadoop.hbase.classification.InterfaceAudience; import org.apache.hadoop.hbase.classification.InterfaceAudience;
import org.apache.hadoop.hbase.util.ByteBufferUtils;
import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Bytes;


/** /**
Expand All @@ -43,7 +44,7 @@ public int getTagsLength() {
public int write(OutputStream out, boolean withTags) throws IOException { public int write(OutputStream out, boolean withTags) throws IOException {
// In KeyValueUtil#oswrite we do a Cell serialization as KeyValue. Any changes doing here, pls // In KeyValueUtil#oswrite we do a Cell serialization as KeyValue. Any changes doing here, pls
// check KeyValueUtil#oswrite also and do necessary changes. // check KeyValueUtil#oswrite also and do necessary changes.
writeInt(out, this.length); ByteBufferUtils.putInt(out, this.length);
out.write(this.bytes, this.offset, this.length); out.write(this.bytes, this.offset, this.length);
return this.length + Bytes.SIZEOF_INT; return this.length + Bytes.SIZEOF_INT;
} }
Expand Down
Loading

0 comments on commit 3f80e0e

Please sign in to comment.