HBASE-4626 Filters unnecessarily copy byte arrays

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1187175 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
larsh 2011-10-21 03:15:28 +00:00
parent 25385fe1e2
commit c00dbf8139
10 changed files with 41 additions and 30 deletions

View File

@ -21,6 +21,7 @@ Release 0.93.0 - Unreleased
HBASE-4469 Avoid top row seek by looking up bloomfilter (liyin via jgray)
HBASE-4418 Show all the hbase configuration in the web ui
HBASE-4489 Better key splitting in RegionSplitter
HBASE-4626 Filters unnecessarily copy byte arrays (Lars H)
BUG FIXES
HBASE-4488 Store could miss rows during flush (Lars H via jgray)

View File

@ -20,6 +20,8 @@
package org.apache.hadoop.hbase.filter;
import org.apache.hadoop.hbase.util.Bytes;
/**
* A binary comparator which lexicographically compares against the specified
* byte array using {@link org.apache.hadoop.hbase.util.Bytes#compareTo(byte[], byte[])}.
@ -37,4 +39,8 @@ public class BinaryComparator extends WritableByteArrayComparable {
super(value);
}
@Override
public int compareTo(byte [] value, int offset, int length) {
return Bytes.compareTo(this.value, 0, this.value.length, value, offset, length);
}
}

View File

@ -41,13 +41,8 @@ public class BinaryPrefixComparator extends WritableByteArrayComparable {
}
@Override
public int compareTo(byte [] value) {
if (this.value.length <= value.length) {
return Bytes.compareTo(this.value, 0, this.value.length, value, 0,
this.value.length);
} else {
return Bytes.compareTo(this.value, value);
}
public int compareTo(byte [] value, int offset, int length) {
return Bytes.compareTo(this.value, 0, this.value.length, value, offset,
this.value.length <= length ? this.value.length : length);
}
}

View File

@ -74,8 +74,8 @@ public class BitComparator extends WritableByteArrayComparable {
}
@Override
public int compareTo(byte[] value) {
if (value.length != this.value.length) {
public int compareTo(byte[] value, int offset, int length) {
if (length != this.value.length) {
return 1;
}
int b = 0;
@ -83,13 +83,13 @@ public class BitComparator extends WritableByteArrayComparable {
for (int i = value.length - 1; i >= 0 && b == 0; i--) {
switch (bitOperator) {
case AND:
b = (this.value[i] & value[i]) & 0xff;
b = (this.value[i] & value[i+offset]) & 0xff;
break;
case OR:
b = (this.value[i] | value[i]) & 0xff;
b = (this.value[i] | value[i+offset]) & 0xff;
break;
case XOR:
b = (this.value[i] ^ value[i]) & 0xff;
b = (this.value[i] ^ value[i+offset]) & 0xff;
break;
}
}

View File

@ -20,13 +20,11 @@
package org.apache.hadoop.hbase.filter;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.io.HbaseObjectWritable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Arrays;
import java.util.ArrayList;
import com.google.common.base.Preconditions;
@ -102,12 +100,10 @@ public abstract class CompareFilter extends FilterBase {
protected boolean doCompare(final CompareOp compareOp,
final WritableByteArrayComparable comparator, final byte [] data,
final int offset, final int length) {
if (compareOp == CompareOp.NO_OP) {
return true;
}
int compareResult =
comparator.compareTo(Arrays.copyOfRange(data, offset,
offset + length));
if (compareOp == CompareOp.NO_OP) {
return true;
}
int compareResult = comparator.compareTo(data, offset, length);
switch (compareOp) {
case LESS:
return compareResult <= 0;

View File

@ -36,4 +36,8 @@ public class NullComparator extends WritableByteArrayComparable {
return value != null ? 1 : 0;
}
@Override
public int compareTo(byte[] value, int offset, int length) {
throw new UnsupportedOperationException();
}
}

View File

@ -90,10 +90,11 @@ public class RegexStringComparator extends WritableByteArrayComparable {
}
@Override
public int compareTo(byte[] value) {
public int compareTo(byte[] value, int offset, int length) {
// Use find() for subsequence match instead of matches() (full sequence
// match) to adhere to the principle of least surprise.
return pattern.matcher(new String(value, charset)).find() ? 0 : 1;
return pattern.matcher(new String(value, offset, length, charset)).find() ? 0
: 1;
}
@Override

View File

@ -174,10 +174,7 @@ public class SingleColumnValueFilter extends FilterBase {
private boolean filterColumnValue(final byte [] data, final int offset,
final int length) {
// TODO: Can this filter take a rawcomparator so don't have to make this
// byte array copy?
int compareResult =
this.comparator.compareTo(Arrays.copyOfRange(data, offset, offset + length));
int compareResult = this.comparator.compareTo(data, offset, length);
switch (this.compareOp) {
case LESS:
return compareResult <= 0;

View File

@ -64,8 +64,9 @@ public class SubstringComparator extends WritableByteArrayComparable {
}
@Override
public int compareTo(byte[] value) {
return Bytes.toString(value).toLowerCase().contains(substr) ? 0 : 1;
public int compareTo(byte[] value, int offset, int length) {
return Bytes.toString(value, offset, length).toLowerCase().contains(substr) ? 0
: 1;
}
@Override

View File

@ -60,7 +60,17 @@ public abstract class WritableByteArrayComparable implements Writable, Comparabl
@Override
public int compareTo(byte [] value) {
return Bytes.compareTo(this.value, value);
return compareTo(value, 0, value.length);
}
/**
* Special compareTo method for subclasses, to avoid
* copying byte[] unnecessarily.
* @param value byte[] to compare
* @param offset offset into value
* @param length number of bytes to compare
* @return a negative integer, zero, or a positive integer as this object
* is less than, equal to, or greater than the specified object.
*/
public abstract int compareTo(byte [] value, int offset, int length);
}