Skip to content

Commit

Permalink
HBASE-19629 RawCell#getTags should return the Iterator<Tag> in order …
Browse files Browse the repository at this point in the history
…to avoid iterating through whole tag array at once

Signed-off-by: Chia-Ping Tsai <chia7712@gmail.com>
  • Loading branch information
Vasudevan authored and chia7712 committed Dec 27, 2017
1 parent 7145d98 commit 467a466
Show file tree
Hide file tree
Showing 19 changed files with 46 additions and 360 deletions.
Expand Up @@ -18,8 +18,6 @@

package org.apache.hadoop.hbase.client;

import static org.apache.hadoop.hbase.Tag.TAG_LENGTH_SIZE;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
Expand All @@ -33,7 +31,6 @@
import java.util.TreeMap;
import java.util.UUID;
import java.util.stream.Collectors;
import org.apache.hadoop.hbase.ArrayBackedTag;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellScannable;
import org.apache.hadoop.hbase.CellScanner;
Expand All @@ -60,7 +57,6 @@
import org.apache.hadoop.hbase.shaded.com.google.common.base.Preconditions;
import org.apache.hadoop.hbase.shaded.com.google.common.collect.ArrayListMultimap;
import org.apache.hadoop.hbase.shaded.com.google.common.collect.ListMultimap;
import org.apache.hadoop.hbase.shaded.com.google.common.collect.Lists;
import org.apache.hadoop.hbase.shaded.com.google.common.io.ByteArrayDataInput;
import org.apache.hadoop.hbase.shaded.com.google.common.io.ByteArrayDataOutput;
import org.apache.hadoop.hbase.shaded.com.google.common.io.ByteStreams;
Expand Down Expand Up @@ -915,35 +911,23 @@ public Optional<Tag> getTag(byte type) {
if (cell instanceof RawCell) {
return ((RawCell) cell).getTag(type);
}
int length = getTagsLength();
int offset = getTagsOffset();
int pos = offset;
while (pos < offset + length) {
int tagLen = Bytes.readAsInt(getTagsArray(), pos, TAG_LENGTH_SIZE);
if (getTagsArray()[pos + TAG_LENGTH_SIZE] == type) {
return Optional.of(new ArrayBackedTag(getTagsArray(), pos,
tagLen + TAG_LENGTH_SIZE));
}
pos += TAG_LENGTH_SIZE + tagLen;
}
return Optional.empty();
return PrivateCellUtil.getTag(cell, type);
}

@Override
public List<Tag> getTags() {
public Iterator<Tag> getTags() {
if (cell instanceof RawCell) {
return ((RawCell) cell).getTags();
}
return Lists.newArrayList(PrivateCellUtil.tagsIterator(cell));
return PrivateCellUtil.tagsIterator(cell);
}

@Override
public byte[] cloneTags() {
if (cell instanceof RawCell) {
return ((RawCell) cell).cloneTags();
} else {
return PrivateCellUtil.cloneTags(cell);
}
return PrivateCellUtil.cloneTags(cell);
}

private long heapOverhead() {
Expand Down
Expand Up @@ -17,16 +17,9 @@
*/
package org.apache.hadoop.hbase;

import static org.apache.hadoop.hbase.Tag.TAG_LENGTH_SIZE;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

import org.apache.hadoop.hbase.util.ByteBufferUtils;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.ClassSize;
Expand Down Expand Up @@ -350,31 +343,4 @@ private int calculateHashForKey(ByteBufferCell cell) {
hash = 31 * hash + cell.getTypeByte();
return hash;
}

@Override
public Optional<Tag> getTag(byte type) {
int length = getTagsLength();
int offset = getTagsPosition();
int pos = offset;
int tagLen;
while (pos < offset + length) {
ByteBuffer tagsBuffer = getTagsByteBuffer();
tagLen = ByteBufferUtils.readAsInt(tagsBuffer, pos, TAG_LENGTH_SIZE);
if (ByteBufferUtils.toByte(tagsBuffer, pos + TAG_LENGTH_SIZE) == type) {
return Optional.ofNullable(new ByteBufferTag(tagsBuffer, pos, tagLen + TAG_LENGTH_SIZE));
}
pos += TAG_LENGTH_SIZE + tagLen;
}
return Optional.ofNullable(null);
}

@Override
public List<Tag> getTags() {
List<Tag> tags = new ArrayList<>();
Iterator<Tag> tagsItr = PrivateCellUtil.tagsIterator(this);
while (tagsItr.hasNext()) {
tags.add(tagsItr.next());
}
return tags;
}
}
Expand Up @@ -18,13 +18,6 @@

package org.apache.hadoop.hbase;

import static org.apache.hadoop.hbase.Tag.TAG_LENGTH_SIZE;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.ClassSize;
Expand Down Expand Up @@ -304,30 +297,4 @@ public void setTimestamp(byte[] ts) {
public String toString() {
return CellUtil.toString(this, true);
}

@Override
public Optional<Tag> getTag(byte type) {
int length = getTagsLength();
int offset = getTagsOffset();
int pos = offset;
while (pos < offset + length) {
int tagLen = Bytes.readAsInt(getTagsArray(), pos, TAG_LENGTH_SIZE);
if (getTagsArray()[pos + TAG_LENGTH_SIZE] == type) {
return Optional
.ofNullable(new ArrayBackedTag(getTagsArray(), pos, tagLen + TAG_LENGTH_SIZE));
}
pos += TAG_LENGTH_SIZE + tagLen;
}
return Optional.ofNullable(null);
}

@Override
public List<Tag> getTags() {
List<Tag> tags = new ArrayList<>();
Iterator<Tag> tagsItr = PrivateCellUtil.tagsIterator(this);
while (tagsItr.hasNext()) {
tags.add(tagsItr.next());
}
return tags;
}
}
37 changes: 4 additions & 33 deletions hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValue.java
Expand Up @@ -19,7 +19,6 @@
*/
package org.apache.hadoop.hbase;

import static org.apache.hadoop.hbase.Tag.TAG_LENGTH_SIZE;
import static org.apache.hadoop.hbase.util.Bytes.len;

import java.io.DataInput;
Expand All @@ -33,8 +32,6 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.hadoop.hbase.util.ByteBufferUtils;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.ClassSize;
Expand Down Expand Up @@ -1165,11 +1162,11 @@ public Map<String, Object> toStringMap() {
Bytes.toStringBinary(getQualifierArray(), getQualifierOffset(), getQualifierLength()));
stringMap.put("timestamp", getTimestamp());
stringMap.put("vlen", getValueLength());
List<Tag> tags = getTags();
Iterator<Tag> tags = getTags();
if (tags != null) {
List<String> tagsString = new ArrayList<>(tags.size());
for (Tag t : tags) {
tagsString.add(t.toString());
List<String> tagsString = new ArrayList<String>();
while (tags.hasNext()) {
tagsString.add(tags.next().toString());
}
stringMap.put("tag", tagsString);
}
Expand Down Expand Up @@ -2555,30 +2552,4 @@ public ExtendedCell deepClone() {
kv.setSequenceId(this.getSequenceId());
return kv;
}

@Override
public Optional<Tag> getTag(byte type) {
int length = getTagsLength();
int offset = getTagsOffset();
int pos = offset;
while (pos < offset + length) {
int tagLen = Bytes.readAsInt(getTagsArray(), pos, TAG_LENGTH_SIZE);
if (getTagsArray()[pos + TAG_LENGTH_SIZE] == type) {
return Optional
.ofNullable(new ArrayBackedTag(getTagsArray(), pos, tagLen + TAG_LENGTH_SIZE));
}
pos += TAG_LENGTH_SIZE + tagLen;
}
return Optional.ofNullable(null);
}

@Override
public List<Tag> getTags() {
List<Tag> tags = new ArrayList<>();
Iterator<Tag> tagsItr = PrivateCellUtil.tagsIterator(this);
while (tagsItr.hasNext()) {
tags.add(tagsItr.next());
}
return tags;
}
}

0 comments on commit 467a466

Please sign in to comment.