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
- * 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
* 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