diff --git a/src/main/java/org/apache/hadoop/hbase/client/Result.java b/src/main/java/org/apache/hadoop/hbase/client/Result.java index 7d81264..d94ce0d 100644 --- a/src/main/java/org/apache/hadoop/hbase/client/Result.java +++ b/src/main/java/org/apache/hadoop/hbase/client/Result.java @@ -21,10 +21,11 @@ package org.apache.hadoop.hbase.client; import com.google.common.collect.Ordering; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.KeyValue.SplitKeyValue; import org.apache.hadoop.hbase.io.ImmutableBytesWritable; -import org.apache.hadoop.hbase.io.WritableWithSize; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.io.Writable; @@ -42,6 +43,8 @@ import java.util.TreeMap; /** * Single row result of a {@link Get} or {@link Scan} query.

* + * This class is NOT THREAD SAFE.

+ * * Convenience methods are available that return various {@link Map} * structures and values directly.

* @@ -110,7 +115,7 @@ public class Result implements Writable, WritableWithSize { * Method for retrieving the row that this result is for * @return row */ - public synchronized byte [] getRow() { + public byte [] getRow() { if (this.row == null) { if(this.kvs == null) { readFields(); @@ -121,8 +126,8 @@ public class Result implements Writable, WritableWithSize { } /** - * Return the unsorted array of KeyValues backing this Result instance. - * @return unsorted array of KeyValues + * Return the array of KeyValues backing this Result instance. + * @return array of KeyValues */ public KeyValue[] raw() { if(this.kvs == null) { @@ -134,6 +139,8 @@ public class Result implements Writable, WritableWithSize { /** * Create a sorted list of the KeyValue's in this result. * + * Since HBase 0.20.5 this is equivalent to raw(). + * * @return The sorted list of KeyValue's. */ public List list() { @@ -146,25 +153,120 @@ public class Result implements Writable, WritableWithSize { /** * Returns a sorted array of KeyValues in this Result. *

- * Note: Sorting is done in place, so the backing array will be sorted - * after calling this method. + * Since HBase 0.20.5 this is equivalent to raw(). * @return sorted array of KeyValues */ public KeyValue[] sorted() { - if (isEmpty()) { // used for side effect! - return null; - } + raw(); // side effect of loading this.kvs if (!sorted) { assert Ordering.from(KeyValue.COMPARATOR).isOrdered(Arrays.asList(kvs)); Arrays.sort(kvs, KeyValue.COMPARATOR); sorted = true; } - return kvs; + + return raw(); } private boolean sorted = false; /** + * Return me the KeyValues for the specific column. If you requested multiple timestamps + * it will be sorted in TS order, that is result.get(0) will be the newest, .get(1) the 2nd + * newest and so on to result.size()-1 being the oldest (or the oldest available as per the + * version retention policy on the column family). + * + * @param family + * @param qualifier + * @return + */ + public List getColumn(byte [] family, byte [] qualifier) { + List result = new ArrayList(); + + KeyValue [] kvs = sorted(); + + if (kvs == null || kvs.length == 0) { + return result; + } + int pos = binarySearch(kvs, family, qualifier); + if (pos == -1) { + return result; // cant find it + } + + for (int i = pos ; i < kvs.length ; i++ ) { + KeyValue kv = kvs[i]; + if (kv.matchingColumn(family,qualifier)) { + result.add(kv); + } else { + break; // done! + } + } + + return result; + } + + protected int binarySearch(KeyValue [] kvs, byte [] family, byte [] qualifier) { + KeyValue searchTerm = + KeyValue.createFirstOnRow(kvs[0].getRow(), + family, qualifier); + + // pos === ( -(insertion point) - 1) + int pos = Arrays.binarySearch(kvs, searchTerm, KeyValue.COMPARATOR); + if (pos == kvs.length) { + return -1; // null/empty result. + } + // never will exact match + if (pos < 0) { + pos = (pos+1) * -1; + // pos is now insertion point + } + return pos; + } + + /** + * The KeyValue for the most recent for a given column. + * + * @param family + * @param qualifier + * @return + */ + public KeyValue getColumnLatest(byte [] family, byte [] qualifier) { + KeyValue [] kvs = sorted(); // side effect possibly. + if (kvs == null || kvs.length == 0) { + return null; + } + int pos = binarySearch(kvs, family, qualifier); + if (pos == -1) { + return null; + } + KeyValue kv = kvs[pos]; + if (kv.matchingColumn(family, qualifier)) { + return kv; + } + return null; + } + private boolean sorted = false; + + /** + * Get the latest value for the given column. Returns null if there is none. + * + * @param family + * @param qualifier + * @return + */ + public byte[] valueEx(byte [] family, byte [] qualifier) { + KeyValue kv = getColumnLatest(family, qualifier); + if (kv == null) { + return null; + } + return kv.getValue(); + } + + public boolean containsColumnEx(byte [] family, byte [] qualifier) { + KeyValue kv = getColumnLatest(family, qualifier); + return kv != null; + } + + /** * Map of families to all versions of its qualifiers and values. *

* Returns a three level Map of the form: diff --git a/src/test/java/org/apache/hadoop/hbase/client/TestResult.java b/src/test/java/org/apache/hadoop/hbase/client/TestResult.java new file mode 100644 index 0000000..4c7aed2 --- /dev/null +++ b/src/test/java/org/apache/hadoop/hbase/client/TestResult.java @@ -0,0 +1,208 @@ +/* + * Copyright 2010 The Apache Software Foundation + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hbase.client; + +import junit.framework.TestCase; +import org.apache.hadoop.hbase.KeyValue; +import org.apache.hadoop.hbase.util.Bytes; + +import static org.apache.hadoop.hbase.HBaseTestCase.assertByteEquals; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.NavigableMap; + +public class TestResult extends TestCase { + + static KeyValue[] genKVs(final byte[] row, final byte[] family, + final byte[] value, + final long timestamp, + final int cols) { + KeyValue [] kvs = new KeyValue[cols]; + + for (int i = 0; i < cols ; i++) { + kvs[i] = new KeyValue( + row, family, Bytes.toBytes(i), + timestamp, + Bytes.add(value, Bytes.toBytes(i))); + } + return kvs; + } + + static final byte [] row = Bytes.toBytes("row"); + static final byte [] family = Bytes.toBytes("family"); + static final byte [] value = Bytes.toBytes("value"); + + public void testBasic() throws Exception { + KeyValue [] kvs = genKVs(row, family, value, 1, 100); + + Arrays.sort(kvs, KeyValue.COMPARATOR); + + Result r = new Result(kvs); + + for (int i = 0; i < 100; ++i) { + final byte[] qf = Bytes.toBytes(i); + + List ks = r.getColumn(family, qf); + assertEquals(1, ks.size()); + assertByteEquals(qf, ks.get(0).getQualifier()); + + assertEquals(ks.get(0), r.getColumnLatest(family, qf)); + assertByteEquals(Bytes.add(value, Bytes.toBytes(i)), r.valueEx(family, qf)); + assertTrue(r.containsColumnEx(family, qf)); + } + } + public void testMultiVersion() throws Exception { + KeyValue [] kvs1 = genKVs(row, family, value, 1, 100); + KeyValue [] kvs2 = genKVs(row, family, value, 200, 100); + + KeyValue [] kvs = new KeyValue[kvs1.length+kvs2.length]; + System.arraycopy(kvs1, 0, kvs, 0, kvs1.length); + System.arraycopy(kvs2, 0, kvs, kvs1.length, kvs2.length); + + Arrays.sort(kvs, KeyValue.COMPARATOR); + + Result r = new Result(kvs); + for (int i = 0; i < 100; ++i) { + final byte[] qf = Bytes.toBytes(i); + + List ks = r.getColumn(family, qf); + assertEquals(2, ks.size()); + assertByteEquals(qf, ks.get(0).getQualifier()); + assertEquals(200, ks.get(0).getTimestamp()); + + assertEquals(ks.get(0), r.getColumnLatest(family, qf)); + assertByteEquals(Bytes.add(value, Bytes.toBytes(i)), r.valueEx(family, qf)); + assertTrue(r.containsColumnEx(family, qf)); + } + + } + + public void testSpeed() throws Exception { + KeyValue [] kvs =genKVs(row, family, value, 1, 100); +// Arrays.sort(kvs, KeyValue.COMPARATOR); + + // warm up both: + for (int i = 1000 ; i > 0; i--) { + doNew(kvs); + } + for (int i = 1000 ; i > 0; i--) { + doOld(kvs); + } + + for (int i = 1000 ; i > 0; i--) { + doNewList(kvs); + } + for (int i = 1000 ; i > 0; i--) { + doOldList(kvs); + } + + + long start = System.nanoTime(); + for (int i = 1000 ; i > 0; i--) { + doNew(kvs); + } + + long diff = System.nanoTime() - start; + System.out.println("Time for new: " + diff); + + + + start = System.nanoTime(); + + for (int i = 1000 ; i > 0; i--) { + doOld(kvs); + } + + diff = System.nanoTime() - start; + System.out.println("Time for old: " + diff); + + + start = System.nanoTime(); + for (int i = 1000 ; i > 0; i--) { + doNewList(kvs); + } + + diff = System.nanoTime() - start; + System.out.println("Time for list new: " + diff); + + + start = System.nanoTime(); + + for (int i = 1000 ; i > 0; i--) { + doOldList(kvs); + } + + diff = System.nanoTime() - start; + System.out.println("Time for list old: " + diff); + } + + private void doNewList(KeyValue [] kvs) { + Result r; + r = new Result(kvs); + + KeyValue [] myKvs = r.sorted(); + for ( KeyValue kv : myKvs ) { + byte[] cf = kv.getQualifier(); + byte[] expected = Bytes.add(value,cf); + assertByteEquals(expected, kv.getValue()); + } + } + + private void doNew(KeyValue[] kvs) { + Result r; + r = new Result(kvs); + + + + for (int i = 0 ; i < 100 ; ++i) { + final byte[] qf = Bytes.toBytes(i); + byte [] v = r.valueEx(family, qf); + assertByteEquals(Bytes.add(value, Bytes.toBytes(i)), v); + } + + } + + private void doOldList(KeyValue [] kvs) { + Result r = new Result(kvs); + + + NavigableMap mm = r.getFamilyMap(family); + for (Map.Entry e : mm.entrySet()) { + byte [] expected = Bytes.add(value, e.getKey()); + assertByteEquals(expected, e.getValue()); + } + + } + + private void doOld(KeyValue[] kvs) { + Result r = new Result(kvs); + + + for (int i = 0 ; i < 100 ; ++i) { + final byte[] qf = Bytes.toBytes(i); + byte [] v = r.getValue(family, qf); + assertByteEquals(Bytes.add(value, Bytes.toBytes(i)), v); + } + + } +}