Skip to content

Commit

Permalink
HBASE-13977 - Convert getKey and related APIs to Cell (Ram)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramkrish86 committed Jul 3, 2015
1 parent 1b75fd2 commit 74e82c6
Show file tree
Hide file tree
Showing 31 changed files with 159 additions and 164 deletions.
10 changes: 7 additions & 3 deletions hbase-common/src/main/java/org/apache/hadoop/hbase/CellUtil.java
Expand Up @@ -894,9 +894,13 @@ public static String toString(Cell cell, boolean verbose) {
String value = null;
if (verbose) {
// TODO: pretty print tags as well
tag = Bytes.toStringBinary(cell.getTagsArray(), cell.getTagsOffset(), cell.getTagsLength());
value = Bytes.toStringBinary(cell.getValueArray(), cell.getValueOffset(),
cell.getValueLength());
if (cell.getTagsLength() > 0) {
tag = Bytes.toStringBinary(cell.getTagsArray(), cell.getTagsOffset(), cell.getTagsLength());
}
if (!(cell instanceof KeyValue.KeyOnlyKeyValue)) {
value = Bytes.toStringBinary(cell.getValueArray(), cell.getValueOffset(),
cell.getValueLength());
}
}

builder
Expand Down
Expand Up @@ -641,11 +641,10 @@ public void setCurrentBuffer(ByteBuffer buffer) {
}

@Override
public ByteBuffer getKeyDeepCopy() {
ByteBuffer keyBuffer = ByteBuffer.allocate(current.keyLength);
keyBuffer.put(current.keyBuffer, 0, current.keyLength);
keyBuffer.rewind();
return keyBuffer;
public Cell getKey() {
byte[] key = new byte[current.keyLength];
System.arraycopy(current.keyBuffer, 0, key, 0, current.keyLength);
return new KeyValue.KeyOnlyKeyValue(key);
}

@Override
Expand Down
Expand Up @@ -140,11 +140,11 @@ interface EncodedSeeker {
void setCurrentBuffer(ByteBuffer buffer);

/**
* Does a deep copy of the key at the current position. A deep copy is
* necessary because buffers are reused in the decoder.
* From the current position creates a cell using the key part
* of the current buffer
* @return key at current position
*/
ByteBuffer getKeyDeepCopy();
Cell getKey();

/**
* Does a shallow copy of the value at the current position. A shallow
Expand Down
Expand Up @@ -74,8 +74,8 @@ public void releaseCurrentSearcher(){


@Override
public ByteBuffer getKeyDeepCopy() {
return KeyValueUtil.copyKeyToNewByteBuffer(ptSearcher.current());
public Cell getKey() {
return ptSearcher.current();
}


Expand Down
Expand Up @@ -125,7 +125,7 @@ public HFileScanner getScanner(final boolean cacheBlocks,
final HFileScanner delegate = s;
public boolean atEnd = false;

public ByteBuffer getKey() {
public Cell getKey() {
if (atEnd) return null;
return delegate.getKey();
}
Expand All @@ -148,10 +148,10 @@ public String getValueString() {
return delegate.getValueString();
}

public Cell getKeyValue() {
public Cell getCell() {
if (atEnd) return null;

return delegate.getKeyValue();
return delegate.getCell();
}

public boolean next() throws IOException {
Expand All @@ -163,9 +163,7 @@ public boolean next() throws IOException {
}
// constrain the bottom.
if (!top) {
ByteBuffer bb = getKey();
if (getComparator().compare(splitCell, bb.array(), bb.arrayOffset(),
bb.limit()) <= 0) {
if (getComparator().compare(splitCell, getKey()) <= 0) {
atEnd = true;
return false;
}
Expand Down Expand Up @@ -195,10 +193,7 @@ public boolean seekTo() throws IOException {
return b;
}
// Check key.
ByteBuffer k = this.delegate.getKey();
return (this.delegate.getReader().getComparator().
compare(splitCell, k.array(), k.arrayOffset(), k.limit()
)) > 0;
return (this.delegate.getReader().getComparator().compare(splitCell, getKey())) > 0;
}

public org.apache.hadoop.hbase.io.hfile.HFile.Reader getReader() {
Expand Down Expand Up @@ -307,15 +302,15 @@ public boolean passesKeyRangeFilter(Scan scan) {
}

@Override
public byte[] getLastKey() {
public Cell getLastKey() {
if (top) {
return super.getLastKey();
}
// Get a scanner that caches the block and that uses pread.
HFileScanner scanner = getScanner(true, true);
try {
if (scanner.seekBefore(this.splitCell)) {
return Bytes.toBytes(scanner.getKey());
return scanner.getKey();
}
} catch (IOException e) {
LOG.warn("Failed seekBefore " + Bytes.toStringBinary(this.splitkey), e);
Expand All @@ -335,7 +330,7 @@ public Cell getFirstKey() {
HFileScanner scanner = getScanner(true, true, false);
try {
if (scanner.seekTo()) {
this.firstKey = new KeyValue.KeyOnlyKeyValue(Bytes.toBytes(scanner.getKey()));
this.firstKey = scanner.getKey();
}
firstKeySeeked = true;
} catch (IOException e) {
Expand Down
Expand Up @@ -393,7 +393,7 @@ public interface Reader extends Closeable, CachingBlockReader {

Map<byte[], byte[]> loadFileInfo() throws IOException;

byte[] getLastKey();
Cell getLastKey();

Cell midkey() throws IOException;

Expand Down
Expand Up @@ -314,7 +314,7 @@ private void scanKeysValues(Path file, KeyValueStatsCollector fileStats,
HFileScanner scanner, byte[] row) throws IOException {
Cell pCell = null;
do {
Cell cell = scanner.getKeyValue();
Cell cell = scanner.getCell();
if (row != null && row.length != 0) {
int result = CellComparator.COMPARATOR.compareRows(cell, row, 0, row.length);
if (result > 0) {
Expand Down Expand Up @@ -482,7 +482,7 @@ private void collectRow() {
keyLen.update(curRowKeyLength);

if (curRowBytes > maxRowBytes && prevCell != null) {
biggestRow = prevCell.getRow();
biggestRow = CellUtil.cloneRow(prevCell);
maxRowBytes = curRowBytes;
}

Expand Down
Expand Up @@ -39,7 +39,6 @@
import org.apache.hadoop.hbase.SizeCachedNoTagsKeyValue;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.KeyValueUtil;
import org.apache.hadoop.hbase.fs.HFileSystem;
import org.apache.hadoop.hbase.io.FSDataInputStreamWrapper;
import org.apache.hadoop.hbase.io.compress.Compression;
Expand Down Expand Up @@ -90,7 +89,7 @@ public class HFileReaderImpl implements HFile.Reader, Configurable {
private HFileDataBlockEncoder dataBlockEncoder = NoOpDataBlockEncoder.INSTANCE;

/** Last key in the file. Filled in when we read in the file info */
private byte [] lastKey = null;
private Cell lastKeyCell = null;

/** Average key length read from file info */
private int avgKeyLen = -1;
Expand Down Expand Up @@ -216,7 +215,9 @@ public HFileReaderImpl(final Path path, FixedFileTrailer trailer,
byte[] creationTimeBytes = fileInfo.get(FileInfo.CREATE_TIME_TS);
this.hfileContext.setFileCreateTime(creationTimeBytes == null? 0:
Bytes.toLong(creationTimeBytes));
lastKey = fileInfo.get(FileInfo.LASTKEY);
if (fileInfo.get(FileInfo.LASTKEY) != null) {
lastKeyCell = new KeyValue.KeyOnlyKeyValue(fileInfo.get(FileInfo.LASTKEY));
}
avgKeyLen = Bytes.toInt(fileInfo.get(FileInfo.AVG_KEY_LEN));
avgValueLen = Bytes.toInt(fileInfo.get(FileInfo.AVG_VALUE_LEN));
byte [] keyValueFormatVersion = fileInfo.get(HFileWriterImpl.KEY_VALUE_VERSION);
Expand Down Expand Up @@ -314,7 +315,7 @@ private String toStringFirstKey() {
}

private String toStringLastKey() {
return KeyValue.keyToString(getLastKey());
return CellUtil.toString(getLastKey(), false);
}

@Override
Expand Down Expand Up @@ -371,8 +372,8 @@ public byte[] getFirstRowKey() {
*/
@Override
public byte[] getLastRowKey() {
byte[] lastKey = getLastKey();
return lastKey == null? null: KeyValueUtil.createKeyValueFromKey(lastKey).getRow();
Cell lastKey = getLastKey();
return lastKey == null? null: CellUtil.cloneRow(lastKey);
}

/** @return number of KV entries in this HFile */
Expand Down Expand Up @@ -819,7 +820,7 @@ public DataBlockEncoding getEffectiveDataBlockEncoding() {
}

@Override
public Cell getKeyValue() {
public Cell getCell() {
if (!isSeeked())
return null;

Expand All @@ -838,12 +839,11 @@ public Cell getKeyValue() {
}

@Override
public ByteBuffer getKey() {
public Cell getKey() {
assertSeeked();
return ByteBuffer.wrap(
blockBuffer.array(),
return new KeyValue.KeyOnlyKeyValue(blockBuffer.array(),
blockBuffer.arrayOffset() + blockBuffer.position()
+ KEY_VALUE_LEN_SIZE, currKeyLen).slice();
+ KEY_VALUE_LEN_SIZE, currKeyLen);
}

@Override
Expand Down Expand Up @@ -1365,13 +1365,13 @@ private void validateBlockType(HFileBlock block,
}

/**
* @return Last key in the file. May be null if file has no entries. Note that
* this is not the last row key, but rather the byte form of the last
* KeyValue.
* @return Last key as cell in the file. May be null if file has no entries. Note that
* this is not the last row key, but it is the Cell representation of the last
* key
*/
@Override
public byte[] getLastKey() {
return dataBlockIndexReader.isEmpty() ? null : lastKey;
public Cell getLastKey() {
return dataBlockIndexReader.isEmpty() ? null : lastKeyCell;
}

/**
Expand Down Expand Up @@ -1516,9 +1516,9 @@ public boolean next() throws IOException {
}

@Override
public ByteBuffer getKey() {
public Cell getKey() {
assertValidSeek();
return seeker.getKeyDeepCopy();
return seeker.getKey();
}

@Override
Expand All @@ -1528,7 +1528,7 @@ public ByteBuffer getValue() {
}

@Override
public Cell getKeyValue() {
public Cell getCell() {
if (block == null) {
return null;
}
Expand All @@ -1537,9 +1537,7 @@ public Cell getKeyValue() {

@Override
public String getKeyString() {
ByteBuffer keyBuffer = getKey();
return Bytes.toStringBinary(keyBuffer.array(),
keyBuffer.arrayOffset(), keyBuffer.limit());
return CellUtil.toString(getKey(), true);
}

@Override
Expand Down
Expand Up @@ -105,12 +105,11 @@ public interface HFileScanner extends Shipper {
*/
boolean next() throws IOException;
/**
* Gets a buffer view to the current key. You must call
* Gets the current key in the form of a cell. You must call
* {@link #seekTo(Cell)} before this method.
* @return byte buffer for the key. The limit is set to the key size, and the
* position is 0, the start of the buffer view.
* @return gets the current key as a Cell.
*/
ByteBuffer getKey();
Cell getKey();
/**
* Gets a buffer view to the current value. You must call
* {@link #seekTo(Cell)} before this method.
Expand All @@ -120,9 +119,9 @@ public interface HFileScanner extends Shipper {
*/
ByteBuffer getValue();
/**
* @return Instance of {@link org.apache.hadoop.hbase.KeyValue}.
* @return Instance of {@link org.apache.hadoop.hbase.Cell}.
*/
Cell getKeyValue();
Cell getCell();
/**
* Convenience method to get a copy of the key as a string - interpreting the
* bytes as UTF8. You must call {@link #seekTo(Cell)} before this method.
Expand Down
Expand Up @@ -827,8 +827,7 @@ private static void copyHFileHalf(
HFileScanner scanner = halfReader.getScanner(false, false, false);
scanner.seekTo();
do {
KeyValue kv = KeyValueUtil.ensureKeyValue(scanner.getKeyValue());
halfWriter.append(kv);
halfWriter.append(scanner.getCell());
} while (scanner.next());

for (Map.Entry<byte[],byte[]> entry : fileInfo.entrySet()) {
Expand Down
Expand Up @@ -587,12 +587,12 @@ Path splitStoreFile(final HRegionInfo hri, final String familyName, final StoreF
if (top) {
//check if larger than last key.
KeyValue splitKey = KeyValueUtil.createFirstOnRow(splitRow);
byte[] lastKey = f.createReader().getLastKey();
Cell lastKey = f.createReader().getLastKey();
// If lastKey is null means storefile is empty.
if (lastKey == null) {
return null;
}
if (f.getReader().getComparator().compare(splitKey, lastKey, 0, lastKey.length) > 0) {
if (f.getReader().getComparator().compare(splitKey, lastKey) > 0) {
return null;
}
} else {
Expand Down

0 comments on commit 74e82c6

Please sign in to comment.