HBASE-1622 Make KeyValue implement the Comparable interface to make it work with Cascading

git-svn-id: https://svn.apache.org/repos/asf/hadoop/hbase/trunk@792025 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Stack 2009-07-08 04:06:37 +00:00
parent b00a7f271f
commit 6a5b635e6f
3 changed files with 37 additions and 4 deletions

View File

@ -450,6 +450,8 @@ Release 0.20.0 - Unreleased
HBASE-1620 Need to use special StoreScanner constructor for major compactions
(passed sf, no caching, etc) (Jon Gray via Stack)
HBASE-1624 Don't sort Puts if only one in list in HCM#processBatchOfRows
HBASE-1622 Make KeyValue implement the Comparable interface to make it work
with Cascading (Erik Holstad via Stack)
OPTIMIZATIONS
HBASE-1412 Change values for delete column and column family in KeyValue

View File

@ -30,7 +30,7 @@ import org.apache.hadoop.hbase.io.hfile.HFile;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.ClassSize;
import org.apache.hadoop.io.RawComparator;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;
/**
* An HBase Key/Value. Instances of this class are immutable. They are not
@ -53,7 +53,7 @@ import org.apache.hadoop.io.Writable;
* <p>TODO: Group Key-only comparators and operations into a Key class, just
* for neatness sake, if can figure what to call it.
*/
public class KeyValue implements Writable, HeapSize {
public class KeyValue implements WritableComparable<KeyValue>, HeapSize {
static final Log LOG = LogFactory.getLog(KeyValue.class);
/**
@ -61,7 +61,8 @@ public class KeyValue implements Writable, HeapSize {
*/
public static final char COLUMN_FAMILY_DELIMITER = ':';
public static final byte[] COLUMN_FAMILY_DELIM_ARRAY = new byte[]{COLUMN_FAMILY_DELIMITER};
public static final byte[] COLUMN_FAMILY_DELIM_ARRAY =
new byte[]{COLUMN_FAMILY_DELIMITER};
/**
* Comparator for plain key/values; i.e. non-catalog table key/values.
@ -1794,6 +1795,7 @@ public class KeyValue implements Writable, HeapSize {
(2 * Bytes.SIZEOF_INT));
}
// WritableComparable
// Writable
public void readFields(final DataInput in) throws IOException {
this.length = in.readInt();
@ -1806,4 +1808,10 @@ public class KeyValue implements Writable, HeapSize {
out.writeInt(this.length);
out.write(this.bytes, this.offset, this.length);
}
// Comparable
public int compareTo(KeyValue that) {
return KEY_COMPARATOR.compare(this.bytes, this.offset, this.length,
that.getBuffer(), that.getOffset(), that.getLength());
}
}

View File

@ -276,4 +276,27 @@ public class TestKeyValue extends TestCase {
// TODO actually write this test!
}
public void testComparableInterface() {
byte [] row1 = Bytes.toBytes("row1");
byte [] row2 = Bytes.toBytes("row2");
byte [] fam1 = Bytes.toBytes("fam1");
byte [] fam2 = Bytes.toBytes("fam2");
byte [] qf1 = Bytes.toBytes("qf1");
byte [] qf2 = Bytes.toBytes("qf2");
byte [] val1 = Bytes.toBytes("val1");
KeyValue kv1 = new KeyValue(row1, fam1, qf1, val1);
KeyValue kv2 = new KeyValue(row2, fam2, qf2, val1);
int res = 0;
res = kv1.compareTo(kv2);
assertTrue(res < 0);
res = kv2.compareTo(kv1);
assertTrue(res > 0);
res = kv1.compareTo(kv1);
assertTrue(res == 0);
}
}