You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by je...@apache.org on 2014/03/10 05:21:54 UTC

[23/36] git commit: merge changes from master

merge changes from master


Project: http://git-wip-us.apache.org/repos/asf/incubator-phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-phoenix/commit/dc94419d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-phoenix/tree/dc94419d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-phoenix/diff/dc94419d

Branch: refs/heads/4.0
Commit: dc94419d2f29f908ce0d52ab5392144376af5d10
Parents: 15db7de
Author: Jeffrey Zhong <jz...@JZhongs-MacBook-Pro.local>
Authored: Sat Mar 8 18:51:15 2014 -0800
Committer: Jeffrey Zhong <jz...@JZhongs-MacBook-Pro.local>
Committed: Sat Mar 8 18:51:56 2014 -0800

----------------------------------------------------------------------
 .../coprocessor/MetaDataEndpointImpl.java       |   1 +
 .../phoenix/coprocessor/MetaDataProtocol.java   |   1 +
 .../UngroupedAggregateRegionObserver.java       |   3 +-
 .../org/apache/phoenix/hbase/index/Indexer.java |   1 +
 .../hbase/index/util/ClientKeyValueBuilder.java | 234 +++++--------------
 .../index/util/GenericKeyValueBuilder.java      |  17 +-
 .../hbase/index/util/KeyValueBuilder.java       |   9 +-
 .../apache/phoenix/index/IndexMaintainer.java   |   1 +
 .../iterate/MappedByteBufferSortedQueue.java    |   4 +-
 .../query/ConnectionlessQueryServicesImpl.java  |   6 -
 .../org/apache/phoenix/schema/PTableImpl.java   |   3 +
 .../schema/tuple/MultiKeyValueTuple.java        |   1 +
 .../phoenix/schema/tuple/ResultTuple.java       |  15 +-
 .../java/org/apache/phoenix/util/IndexUtil.java |   1 +
 .../org/apache/phoenix/util/KeyValueUtil.java   |  64 +++--
 .../org/apache/phoenix/util/ResultUtil.java     | 188 +++++++++++++++
 .../phoenix/end2end/index/IndexTestUtil.java    |  11 +-
 17 files changed, 312 insertions(+), 248 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/dc94419d/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java b/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java
index 15e55bd..e89e903 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java
@@ -97,6 +97,7 @@ import org.apache.phoenix.coprocessor.generated.MetaDataProtos.GetVersionRequest
 import org.apache.phoenix.coprocessor.generated.MetaDataProtos.GetVersionResponse;
 import org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataResponse;
 import org.apache.phoenix.coprocessor.generated.MetaDataProtos.UpdateIndexStateRequest;
+import org.apache.phoenix.hbase.index.util.GenericKeyValueBuilder;
 import org.apache.phoenix.hbase.index.util.ImmutableBytesPtr;
 import org.apache.phoenix.hbase.index.util.IndexManagementUtil;
 import org.apache.phoenix.jdbc.PhoenixDatabaseMetaData;

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/dc94419d/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/MetaDataProtocol.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/MetaDataProtocol.java b/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/MetaDataProtocol.java
index 5247ed3..de6cf5b 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/MetaDataProtocol.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/MetaDataProtocol.java
@@ -23,6 +23,7 @@ import java.util.List;
 import org.apache.phoenix.coprocessor.generated.MetaDataProtos;
 import org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataResponse;
 import org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataService;
+import org.apache.phoenix.hbase.index.util.VersionUtil;
 import org.apache.phoenix.schema.PColumn;
 import org.apache.phoenix.schema.PTable;
 import org.apache.phoenix.schema.PTableImpl;

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/dc94419d/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/UngroupedAggregateRegionObserver.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/UngroupedAggregateRegionObserver.java b/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/UngroupedAggregateRegionObserver.java
index c48d60c..71a9251 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/UngroupedAggregateRegionObserver.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/UngroupedAggregateRegionObserver.java
@@ -56,6 +56,8 @@ import org.apache.phoenix.expression.ExpressionType;
 import org.apache.phoenix.expression.aggregator.Aggregator;
 import org.apache.phoenix.expression.aggregator.Aggregators;
 import org.apache.phoenix.expression.aggregator.ServerAggregators;
+import org.apache.phoenix.hbase.index.util.GenericKeyValueBuilder;
+import org.apache.phoenix.hbase.index.util.KeyValueBuilder;
 import org.apache.phoenix.index.PhoenixIndexCodec;
 import org.apache.phoenix.join.HashJoinInfo;
 import org.apache.phoenix.join.ScanProjector;
@@ -197,7 +199,6 @@ public class UngroupedAggregateRegionObserver extends BaseScannerRegionObserver
                     result.setKeyValues(results);
                     try {
                         if (isDelete) {
-                            @SuppressWarnings("deprecation") // FIXME: Remove when unintentionally deprecated method is fixed (HBASE-7870).
                             // FIXME: the version of the Delete constructor without the lock args was introduced
                             // in 0.94.4, thus if we try to use it here we can no longer use the 0.94.2 version
                             // of the client.

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/dc94419d/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/Indexer.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/Indexer.java b/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/Indexer.java
index d9382a5..c461883 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/Indexer.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/Indexer.java
@@ -61,6 +61,7 @@ import org.apache.phoenix.hbase.index.builder.IndexBuildingFailureException;
 import org.apache.phoenix.hbase.index.table.HTableInterfaceReference;
 import org.apache.phoenix.hbase.index.util.ImmutableBytesPtr;
 import org.apache.phoenix.hbase.index.util.IndexManagementUtil;
+import org.apache.phoenix.hbase.index.util.VersionUtil;
 import org.apache.phoenix.hbase.index.wal.IndexedKeyValue;
 import org.apache.phoenix.hbase.index.write.IndexFailurePolicy;
 import org.apache.phoenix.hbase.index.write.IndexWriter;

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/dc94419d/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/util/ClientKeyValueBuilder.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/util/ClientKeyValueBuilder.java b/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/util/ClientKeyValueBuilder.java
index c7a0346..193f724 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/util/ClientKeyValueBuilder.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/util/ClientKeyValueBuilder.java
@@ -20,6 +20,7 @@ package org.apache.phoenix.hbase.index.util;
 import java.util.List;
 import java.util.Map;
 
+import org.apache.hadoop.hbase.Cell;
 import org.apache.hadoop.hbase.KeyValue;
 import org.apache.hadoop.hbase.KeyValue.KVComparator;
 import org.apache.hadoop.hbase.KeyValue.Type;
@@ -70,13 +71,13 @@ public class ClientKeyValueBuilder extends KeyValueBuilder {
     }
 
     @Override
-    public int compareQualifier(KeyValue kv, byte[] key, int offset, int length) {
-        byte[] qual = kv.getQualifier();
-        return Bytes.compareTo(qual, 0, qual.length, key, offset, length);
+    public int compareQualifier(Cell kv, byte[] key, int offset, int length) {
+        return Bytes.compareTo(kv.getQualifierArray(), kv.getQualifierOffset(), 
+          kv.getQualifierLength(), key, offset, length);
     }
 
     @Override
-    public void getValueAsPtr(KeyValue kv, ImmutableBytesWritable ptr) {
+    public void getValueAsPtr(Cell kv, ImmutableBytesWritable ptr) {
         ClientKeyValue ckv = (ClientKeyValue)kv;
         ImmutableBytesWritable value = ckv.getRawValue();
         ptr.set(value.get(), value.getOffset(), value.getLength());
@@ -92,15 +93,24 @@ public class ClientKeyValueBuilder extends KeyValueBuilder {
         private ClientKeyValueComparator() { // Singleton
         }
         
-        public int compareTypes(final KeyValue left, final KeyValue right) {
-            return left.getType() - right.getType();
+        public int compareTypes(final Cell left, final Cell right) {
+            return left.getTypeByte() - right.getTypeByte();
         }
 
-        public int compareMemstoreTimestamps(final KeyValue left, final KeyValue right) {
+        public int compareMemstoreTimestamps(final Cell left, final Cell right) {
             // Descending order
-            return Longs.compare(right.getMemstoreTS(), left.getMemstoreTS());
+            return Longs.compare(right.getMvccVersion(), left.getMvccVersion());
         }
 
+        public int compareTimestamps(final long ltimestamp, final long rtimestamp) {
+          if (ltimestamp < rtimestamp) {
+            return 1;
+          } else if (ltimestamp > rtimestamp) {
+            return -1;
+          }
+          return 0;
+        }
+        
         @Override
         public int compareTimestamps(final KeyValue left, final KeyValue right) {
             // Descending order
@@ -109,13 +119,17 @@ public class ClientKeyValueBuilder extends KeyValueBuilder {
 
 
         @Override
-        public int compare(final KeyValue left, final KeyValue right) {
-            int c = compareRows(left, right);
+        public int compare(final Cell left, final Cell right) {
+            int c = compareRows(left.getRowArray(), left.getRowOffset(), left.getRowLength(),
+              right.getRowArray(), right.getRowOffset(), right.getRowLength());
             if (c != 0) return c;
-            c = Bytes.compareTo(left.getFamily(), right.getFamily());
+            c = Bytes.compareTo(left.getFamilyArray(), left.getFamilyOffset(), left.getFamilyLength(),
+              right.getFamilyArray(), right.getFamilyOffset(), right.getFamilyLength());
             if (c != 0) return c;
-            c = Bytes.compareTo(left.getQualifier(), right.getQualifier());
-            c = compareTimestamps(left, right);
+            c = Bytes.compareTo(left.getQualifierArray(), left.getQualifierOffset(),
+              left.getQualifierLength(), right.getQualifierArray(), right.getQualifierOffset(),
+              right.getQualifierLength());
+            c = compareTimestamps(left.getTimestamp(), right.getTimestamp());
             if (c != 0) return c;
             c = compareTypes(left, right);
             if (c != 0) return c;            
@@ -130,7 +144,8 @@ public class ClientKeyValueBuilder extends KeyValueBuilder {
          */
         @Override
         public int compareRows(final KeyValue left, final KeyValue right) {
-            return Bytes.compareTo(left.getRow(), right.getRow());
+            return Bytes.compareTo(left.getRowArray(), left.getRowOffset(), left.getRowLength(),
+              right.getRowArray(), right.getRowOffset(), right.getRowLength());
         }
 
         /**
@@ -141,36 +156,10 @@ public class ClientKeyValueBuilder extends KeyValueBuilder {
          * @return Result comparing rows.
          */
         @Override
-        public int compareRows(final KeyValue left, final short lrowlength,
-            final KeyValue right, final short rrowlength) {
-            return Bytes.compareTo(left.getRow(), 0, lrowlength, right.getRow(), 0, rrowlength);
-        }
-
-        /**
-         * @param left
-         * @param row - row key (arbitrary byte array)
-         * @return RawComparator
-         */
-        @Override
-        public int compareRows(final KeyValue left, final byte [] row) {
-            return Bytes.compareTo(left.getRow(), row);
-        }
-
-        @Override
-        public int compareColumns(final KeyValue left, 
-                final byte [] right, final int roffset, final int rlength, final int rfamilyLength) {
-          // Compare family portion first.
-          byte[] lcf = left.getFamily();
-          int diff = Bytes.compareTo(lcf, 0, lcf.length,
-            right, roffset, rfamilyLength);
-          if (diff != 0) {
-            return diff;
-          }
-          // Compare qualifier portion
-          byte[] lcq = left.getQualifier();
-          return Bytes.compareTo(lcq, 0, lcq.length,
-            right, roffset + rfamilyLength, rlength - rfamilyLength);
-
+        public int compareRows(byte [] left, int loffset, int llength,
+            byte [] right, int roffset, int rlength) {
+            return Bytes.compareTo(left, loffset, llength, 
+              right, roffset, rlength);
         }
 
         /**
@@ -184,26 +173,20 @@ public class ClientKeyValueBuilder extends KeyValueBuilder {
             if (compareRows(left, right) != 0) {
                 return false;
             }
-            if (Bytes.compareTo(left.getFamily(), right.getFamily()) != 0) {
+            if (Bytes.compareTo(left.getFamilyArray(), left.getFamilyOffset(), 
+              left.getFamilyLength(), right.getFamilyArray(), right.getFamilyOffset(), 
+              right.getFamilyLength()) != 0) {
                 return false;
             }
-            if (Bytes.compareTo(left.getQualifier(), right.getQualifier()) != 0) {
+            if (Bytes.compareTo(left.getQualifierArray(), left.getQualifierOffset(),
+              left.getQualifierLength(), right.getQualifierArray(), right.getQualifierOffset(), 
+              right.getQualifierLength()) != 0) {
                 return false;
             }
             return true;
         }
 
         /**
-         * @param left
-         * @param right
-         * @return True if rows match.
-         */
-        @Override
-        public boolean matchingRows(final KeyValue left, final byte [] right) {
-          return Bytes.equals(left.getRow(), right);
-        }
-
-        /**
          * Compares the row of two keyvalues for equality
          * @param left
          * @param right
@@ -214,116 +197,13 @@ public class ClientKeyValueBuilder extends KeyValueBuilder {
             return compareRows(left, right) == 0;
         }
 
-        /**
-         * @param left
-         * @param lrowlength
-         * @param right
-         * @param rrowlength
-         * @return True if rows match.
-         */
-        @Override
-        public boolean matchingRows(final KeyValue left, final short lrowlength,
-            final KeyValue right, final short rrowlength) {
-            return compareRows(left, lrowlength, right, rrowlength) == 0;
-        }
-
         @Override
         protected Object clone() throws CloneNotSupportedException {
           return this; // Makes no sense to clone this
         }
-
-        /**
-         * @return Comparator that ignores timestamps; useful counting versions.
-         */
-        @Override
-        public KVComparator getComparatorIgnoringTimestamps() {
-            return IGNORE_TIMESTAMP_COMPARATOR;
-        }
-
-        /**
-         * @return Comparator that ignores key type; useful checking deletes
-         */
-        @Override
-        public KVComparator getComparatorIgnoringType() {
-            return IGNORE_TYPE_COMPARATOR;
-        }
-    }
-    
-    /**
-     * 
-     * Singleton ClientKeyValue version of KVComparator that ignores differences in the Type
-     *
-     */
-    private static class IgnoreTypeClientKeyValueComparator extends ClientKeyValueComparator {
-        private IgnoreTypeClientKeyValueComparator() { // Singleton
-        }
-        
-        @Override
-        public int compareTypes(final KeyValue left, final KeyValue right) {
-            return 0;
-        }
-        
-        @Override
-        public KVComparator getComparatorIgnoringTimestamps() {
-            return IGNORE_TIMESTAMP_AND_TYPE_COMPARATOR;
-        }
-    }
-
-
-    /**
-     * 
-     * Singleton ClientKeyValue version of KVComparator that ignores differences in the Timestamp
-     *
-     */
-    private static class IgnoreTimestampClientKeyValueComparator extends ClientKeyValueComparator {
-        private IgnoreTimestampClientKeyValueComparator() { // Singleton
-        }
-        
-        @Override
-        public int compareMemstoreTimestamps(final KeyValue left, final KeyValue right) {
-            return 0;
-        }
-
-        @Override
-        public int compareTimestamps(final KeyValue left, final KeyValue right) {
-            return 0;
-        }
-        
-        @Override
-        public KVComparator getComparatorIgnoringType() {
-            return IGNORE_TYPE_COMPARATOR;
-        }
-    }
-    
-    /**
-     * 
-     * Singleton ClientKeyValue version of KVComparator that ignores differences in the Timestamp and Type
-     *
-     */
-    private static final class IgnoreTimestampAndTypeClientKeyValueComparator extends IgnoreTimestampClientKeyValueComparator {
-        private IgnoreTimestampAndTypeClientKeyValueComparator() { // Singleton
-        }
-        
-        @Override
-        public int compareTypes(final KeyValue left, final KeyValue right) {
-            return 0;
-        }
-
-        @Override
-        public KVComparator getComparatorIgnoringTimestamps() {
-            return this;
-        }
-
-        @Override
-        public KVComparator getComparatorIgnoringType() {
-            return this;
-        }
     }
 
     private static final KVComparator COMPARATOR = new ClientKeyValueComparator();
-    private static final KVComparator IGNORE_TYPE_COMPARATOR = new IgnoreTypeClientKeyValueComparator();
-    private static final KVComparator IGNORE_TIMESTAMP_COMPARATOR = new IgnoreTimestampClientKeyValueComparator();
-    private static final KVComparator IGNORE_TIMESTAMP_AND_TYPE_COMPARATOR = new IgnoreTimestampAndTypeClientKeyValueComparator();
     
     @Override
     public KVComparator getKeyValueComparator() {
@@ -331,15 +211,15 @@ public class ClientKeyValueBuilder extends KeyValueBuilder {
     }
 
     @Override
-    public int compareRow(KeyValue kv, byte[] rrow, int roffset, int rlength) {
-        byte[] lrow = kv.getRow();
-        return Bytes.compareTo(lrow, 0, lrow.length, rrow, roffset, rlength);
+    public int compareRow(Cell kv, byte[] rrow, int roffset, int rlength) {
+        return Bytes.compareTo(kv.getRowArray(), kv.getRowOffset(), kv.getRowLength(), 
+          rrow, roffset, rlength);
     }
 
     @Override
-    public int compareFamily(KeyValue kv, byte[] rbuf, int roffset, int rlength) {
-        byte[] lcf = kv.getFamily();
-        return Bytes.compareTo(lcf, 0, lcf.length, rbuf, roffset, rlength);
+    public int compareFamily(Cell kv, byte[] rbuf, int roffset, int rlength) {
+        return Bytes.compareTo(kv.getFamilyArray(), kv.getFamilyOffset(), 
+          kv.getFamilyLength(), rbuf, roffset, rlength);
     }
 
     /**
@@ -362,19 +242,17 @@ public class ClientKeyValueBuilder extends KeyValueBuilder {
         for (Mutation mutation : mutations) {
             if (mutation instanceof Put) {
                 Put put = (Put)mutation;
-                @SuppressWarnings("deprecation")
-                Put newPut = new Put(put.getRow(),put.getTimeStamp(),put.getRowLock());
-                Map<byte[],List<KeyValue>> newFamilyMap = newPut.getFamilyMap();
-                for (Map.Entry<byte[], List<KeyValue>> entry : put.getFamilyMap().entrySet()) {
-                    List<KeyValue> values = entry.getValue();
-                    List<KeyValue> newValues = Lists.newArrayListWithExpectedSize(values.size());
-                    for (KeyValue value : values) {
-                        newValues.add(new KeyValue(value.getRow(),
-                                value.getFamily(),
-                                value.getQualifier(),
-                                value.getTimestamp(),
-                                Type.codeToType(value.getType()),
-                                value.getValue()));
+                Put newPut = new Put(put.getRow(),put.getTimeStamp());
+                Map<byte[],List<Cell>> newFamilyMap = newPut.getFamilyCellMap();
+                for (Map.Entry<byte[], List<Cell>> entry : put.getFamilyCellMap().entrySet()) {
+                    List<Cell> values = entry.getValue();
+                    List<Cell> newValues = Lists.newArrayListWithExpectedSize(values.size());
+                    for (Cell value : values) {
+                        newValues.add(new KeyValue(value.getRowArray(), value.getRowOffset(),
+                          value.getRowLength(), value.getFamilyArray(), value.getFamilyOffset(),
+                          value.getFamilyLength(), value.getQualifierArray(), value.getQualifierOffset(),
+                          value.getQualifierLength(),value.getTimestamp(), Type.codeToType(value.getTypeByte()),
+                          value.getValueArray(), value.getValueOffset(), value.getValueLength()));
                     }
                     newFamilyMap.put(entry.getKey(), newValues);
                 }

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/dc94419d/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/util/GenericKeyValueBuilder.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/util/GenericKeyValueBuilder.java b/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/util/GenericKeyValueBuilder.java
index be5dc73..ebffde1 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/util/GenericKeyValueBuilder.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/util/GenericKeyValueBuilder.java
@@ -21,6 +21,7 @@ import static org.apache.phoenix.hbase.index.util.ImmutableBytesPtr.copyBytesIfN
 
 import java.util.List;
 
+import org.apache.hadoop.hbase.Cell;
 import org.apache.hadoop.hbase.KeyValue;
 import org.apache.hadoop.hbase.KeyValue.KVComparator;
 import org.apache.hadoop.hbase.KeyValue.Type;
@@ -73,18 +74,18 @@ public class GenericKeyValueBuilder extends KeyValueBuilder {
     }
 
     @Override
-    public int compareQualifier(KeyValue kv, byte[] key, int offset, int length) {
-        return Bytes.compareTo(kv.getBuffer(), kv.getQualifierOffset(), kv.getQualifierLength(), key, offset, length);
+    public int compareQualifier(Cell kv, byte[] key, int offset, int length) {
+        return Bytes.compareTo(kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierLength(), key, offset, length);
     }
 
     @Override
-    public int compareFamily(KeyValue kv, byte[] key, int offset, int length) {
-        return Bytes.compareTo(kv.getBuffer(), kv.getFamilyOffset(), kv.getFamilyLength(), key, offset, length);
+    public int compareFamily(Cell kv, byte[] key, int offset, int length) {
+        return Bytes.compareTo(kv.getFamilyArray(), kv.getFamilyOffset(), kv.getFamilyLength(), key, offset, length);
     }
 
     @Override
-    public void getValueAsPtr(KeyValue kv, ImmutableBytesWritable writable) {
-        writable.set(kv.getBuffer(), kv.getValueOffset(), kv.getValueLength());
+    public void getValueAsPtr(Cell kv, ImmutableBytesWritable writable) {
+        writable.set(kv.getValueArray(), kv.getValueOffset(), kv.getValueLength());
     }
 
     @Override
@@ -93,8 +94,8 @@ public class GenericKeyValueBuilder extends KeyValueBuilder {
     }
 
     @Override
-    public int compareRow(KeyValue kv, byte[] rrow, int roffset, int rlength) {
-        return Bytes.compareTo(kv.getBuffer(), kv.getRowOffset(), kv.getRowLength(), rrow, roffset, rlength);
+    public int compareRow(Cell kv, byte[] rrow, int roffset, int rlength) {
+        return Bytes.compareTo(kv.getRowArray(), kv.getRowOffset(), kv.getRowLength(), rrow, roffset, rlength);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/dc94419d/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/util/KeyValueBuilder.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/util/KeyValueBuilder.java b/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/util/KeyValueBuilder.java
index 0d27512..4c3434b 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/util/KeyValueBuilder.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/hbase/index/util/KeyValueBuilder.java
@@ -20,6 +20,7 @@ package org.apache.phoenix.hbase.index.util;
 import java.io.IOException;
 import java.util.List;
 
+import org.apache.hadoop.hbase.Cell;
 import org.apache.hadoop.hbase.HConstants;
 import org.apache.hadoop.hbase.KeyValue;
 import org.apache.hadoop.hbase.KeyValue.KVComparator;
@@ -114,15 +115,15 @@ public abstract class KeyValueBuilder {
    * @param length length of the key from the offset to check
    * @return the byte difference between the passed keyvalue's qualifier and the passed key
    */
-  public abstract int compareQualifier(KeyValue kv, byte[] key, int offset, int length);
+  public abstract int compareQualifier(Cell kv, byte[] key, int offset, int length);
 
-  public abstract int compareFamily(KeyValue kv, byte[] key, int offset, int length);
-  public abstract int compareRow(KeyValue kv, byte[] row, int offset, int length);
+  public abstract int compareFamily(Cell kv, byte[] key, int offset, int length);
+  public abstract int compareRow(Cell kv, byte[] row, int offset, int length);
   /**
    * @param kv to read
    * @param ptr set with the value from the {@link KeyValue}
    */
-  public abstract void getValueAsPtr(KeyValue kv, ImmutableBytesWritable ptr);
+  public abstract void getValueAsPtr(Cell kv, ImmutableBytesWritable ptr);
   
   public abstract KVComparator getKeyValueComparator();
   

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/dc94419d/phoenix-core/src/main/java/org/apache/phoenix/index/IndexMaintainer.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/index/IndexMaintainer.java b/phoenix-core/src/main/java/org/apache/phoenix/index/IndexMaintainer.java
index 4d4367a..cbd440e 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/index/IndexMaintainer.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/index/IndexMaintainer.java
@@ -44,6 +44,7 @@ import org.apache.hadoop.io.WritableUtils;
 import org.apache.phoenix.hbase.index.ValueGetter;
 import org.apache.phoenix.hbase.index.covered.update.ColumnReference;
 import org.apache.phoenix.hbase.index.util.ImmutableBytesPtr;
+import org.apache.phoenix.hbase.index.util.KeyValueBuilder;
 import org.apache.phoenix.query.QueryConstants;
 import org.apache.phoenix.schema.PColumn;
 import org.apache.phoenix.schema.PColumnFamily;

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/dc94419d/phoenix-core/src/main/java/org/apache/phoenix/iterate/MappedByteBufferSortedQueue.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/iterate/MappedByteBufferSortedQueue.java b/phoenix-core/src/main/java/org/apache/phoenix/iterate/MappedByteBufferSortedQueue.java
index 4ee4957..83426c1 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/iterate/MappedByteBufferSortedQueue.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/iterate/MappedByteBufferSortedQueue.java
@@ -34,13 +34,13 @@ import org.apache.hadoop.hbase.KeyValue;
 import org.apache.hadoop.hbase.client.Result;
 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
 import org.apache.hadoop.hbase.util.Bytes;
-
-import com.google.common.collect.MinMaxPriorityQueue;
 import org.apache.phoenix.iterate.OrderedResultIterator.ResultEntry;
 import org.apache.phoenix.schema.tuple.ResultTuple;
 import org.apache.phoenix.schema.tuple.Tuple;
 import org.apache.phoenix.util.ResultUtil;
 
+import com.google.common.collect.MinMaxPriorityQueue;
+
 public class MappedByteBufferSortedQueue extends AbstractQueue<ResultEntry> {
     private Comparator<ResultEntry> comparator;
     private final int limit;

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/dc94419d/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionlessQueryServicesImpl.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionlessQueryServicesImpl.java b/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionlessQueryServicesImpl.java
index 3100cf4..0fecc24 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionlessQueryServicesImpl.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionlessQueryServicesImpl.java
@@ -25,7 +25,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 
-import org.apache.hadoop.hbase.Cell;
 import org.apache.hadoop.hbase.HConstants;
 import org.apache.hadoop.hbase.HRegionInfo;
 import org.apache.hadoop.hbase.HRegionLocation;
@@ -260,17 +259,12 @@ public class ConnectionlessQueryServicesImpl extends DelegateQueryServices imple
     public MetaDataMutationResult updateIndexState(List<Mutation> tableMetadata, String parentTableName) throws SQLException {
         byte[][] rowKeyMetadata = new byte[3][];
         SchemaUtil.getVarChars(tableMetadata.get(0).getRow(), rowKeyMetadata);
-<<<<<<< HEAD
-        Cell newKV = tableMetadata.get(0).getFamilyCellMap().get(TABLE_FAMILY_BYTES).get(0);
-        PIndexState newState =  PIndexState.fromSerializedValue(newKV.getValueArray()[newKV.getValueOffset()]);
-=======
         Mutation m = MetaDataUtil.getTableHeaderRow(tableMetadata);
         ImmutableBytesWritable ptr = new ImmutableBytesWritable();
         if (!MetaDataUtil.getMutationValue(m, INDEX_STATE_BYTES, kvBuilder, ptr)) {
             throw new IllegalStateException();
         }
         PIndexState newState =  PIndexState.fromSerializedValue(ptr.get()[ptr.getOffset()]);
->>>>>>> origin/master
         byte[] tenantIdBytes = rowKeyMetadata[PhoenixDatabaseMetaData.TENANT_ID_INDEX];
         String schemaName = Bytes.toString(rowKeyMetadata[PhoenixDatabaseMetaData.SCHEMA_NAME_INDEX]);
         String indexName = Bytes.toString(rowKeyMetadata[PhoenixDatabaseMetaData.TABLE_NAME_INDEX]);

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/dc94419d/phoenix-core/src/main/java/org/apache/phoenix/schema/PTableImpl.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/PTableImpl.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/PTableImpl.java
index 2dae10d..b63b294 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/PTableImpl.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/PTableImpl.java
@@ -17,6 +17,8 @@
  */
 package org.apache.phoenix.schema;
 
+import static org.apache.phoenix.hbase.index.util.KeyValueBuilder.addQuietly;
+import static org.apache.phoenix.hbase.index.util.KeyValueBuilder.deleteQuietly;
 import static org.apache.phoenix.query.QueryConstants.SEPARATOR_BYTE;
 import static org.apache.phoenix.schema.SaltingUtil.SALTING_COLUMN;
 
@@ -40,6 +42,7 @@ import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
 import org.apache.hadoop.hbase.util.Bytes;
 import org.apache.phoenix.coprocessor.generated.PTableProtos;
 import org.apache.phoenix.hbase.index.util.ImmutableBytesPtr;
+import org.apache.phoenix.hbase.index.util.KeyValueBuilder;
 import org.apache.phoenix.index.IndexMaintainer;
 import org.apache.phoenix.protobuf.ProtobufUtil;
 import org.apache.phoenix.query.QueryConstants;

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/dc94419d/phoenix-core/src/main/java/org/apache/phoenix/schema/tuple/MultiKeyValueTuple.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/tuple/MultiKeyValueTuple.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/tuple/MultiKeyValueTuple.java
index b423f33..255c54e 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/tuple/MultiKeyValueTuple.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/tuple/MultiKeyValueTuple.java
@@ -21,6 +21,7 @@ import java.util.List;
 
 import org.apache.hadoop.hbase.Cell;
 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
+import org.apache.phoenix.hbase.index.util.GenericKeyValueBuilder;
 import org.apache.phoenix.util.KeyValueUtil;
 
 import com.google.common.collect.ImmutableList;

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/dc94419d/phoenix-core/src/main/java/org/apache/phoenix/schema/tuple/ResultTuple.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/tuple/ResultTuple.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/tuple/ResultTuple.java
index 02aebf7..a7f411c 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/tuple/ResultTuple.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/tuple/ResultTuple.java
@@ -17,6 +17,7 @@
  */
 package org.apache.phoenix.schema.tuple;
 
+import org.apache.hadoop.hbase.Cell;
 import org.apache.hadoop.hbase.KeyValue;
 import org.apache.hadoop.hbase.client.Result;
 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
@@ -55,7 +56,9 @@ public class ResultTuple extends BaseTuple {
 
     @Override
     public KeyValue getValue(byte[] family, byte[] qualifier) {
-        return KeyValueUtil.getColumnLatest(GenericKeyValueBuilder.INSTANCE, result.raw(), family, qualifier);
+        Cell cell = KeyValueUtil.getColumnLatest(GenericKeyValueBuilder.INSTANCE, 
+          result.rawCells(), family, qualifier);
+        return org.apache.hadoop.hbase.KeyValueUtil.ensureKeyValue(cell);
     }
 
     @Override
@@ -68,13 +71,14 @@ public class ResultTuple extends BaseTuple {
       }
       sb.append("{");
       boolean moreThanOne = false;
-      for(KeyValue kv : this.result.list()) {
+      for(Cell kv : this.result.listCells()) {
         if(moreThanOne) {
           sb.append(", \n");
         } else {
           moreThanOne = true;
         }
-        sb.append(kv.toString()+"/value="+Bytes.toString(kv.getValue()));
+        sb.append(kv.toString()+"/value="+Bytes.toString(kv.getValueArray(), 
+          kv.getValueOffset(), kv.getValueLength()));
       }
       sb.append("}\n");
       return sb.toString();
@@ -87,7 +91,8 @@ public class ResultTuple extends BaseTuple {
 
     @Override
     public KeyValue getValue(int index) {
-        return result.raw()[index];
+        return  org.apache.hadoop.hbase.KeyValueUtil.ensureKeyValue(
+          result.rawCells()[index]);
     }
 
     @Override
@@ -96,7 +101,7 @@ public class ResultTuple extends BaseTuple {
         KeyValue kv = getValue(family, qualifier);
         if (kv == null)
             return false;
-        ptr.set(kv.getBuffer(), kv.getValueOffset(), kv.getValueLength());
+        ptr.set(kv.getValueArray(), kv.getValueOffset(), kv.getValueLength());
         return true;
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/dc94419d/phoenix-core/src/main/java/org/apache/phoenix/util/IndexUtil.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/IndexUtil.java b/phoenix-core/src/main/java/org/apache/phoenix/util/IndexUtil.java
index 84d9b55..ae196c3 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/util/IndexUtil.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/util/IndexUtil.java
@@ -32,6 +32,7 @@ import org.apache.phoenix.exception.SQLExceptionInfo;
 import org.apache.phoenix.hbase.index.ValueGetter;
 import org.apache.phoenix.hbase.index.covered.update.ColumnReference;
 import org.apache.phoenix.hbase.index.util.ImmutableBytesPtr;
+import org.apache.phoenix.hbase.index.util.KeyValueBuilder;
 import org.apache.phoenix.index.IndexMaintainer;
 import org.apache.phoenix.query.QueryConstants;
 import org.apache.phoenix.schema.ColumnFamilyNotFoundException;

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/dc94419d/phoenix-core/src/main/java/org/apache/phoenix/util/KeyValueUtil.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/KeyValueUtil.java b/phoenix-core/src/main/java/org/apache/phoenix/util/KeyValueUtil.java
index e8e5df7..580192e 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/util/KeyValueUtil.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/util/KeyValueUtil.java
@@ -88,21 +88,13 @@ public class KeyValueUtil {
      * @param family
      * @param qualifier
      */
-<<<<<<< HEAD
-    public static Cell getColumnLatest(List<Cell>kvs, byte[] family, byte[] qualifier) {
-        if (kvs.size() == 0) {
-        	return null;
-        }
-        Cell row = kvs.get(0);
-        Comparator<Cell> comp = new SearchComparator(row.getRowArray(), row.getRowOffset(), row.getRowLength(), family, qualifier);
-=======
     public static Cell getColumnLatest(KeyValueBuilder kvBuilder, List<Cell>kvs, byte[] family, byte[] qualifier) {
         if (kvs.size() == 0) {
         	return null;
         }
-        KeyValue kvForRow = kvs.get(0);
-        Comparator<KeyValue> comp = new SearchComparator(kvBuilder, kvForRow, family, qualifier);
->>>>>>> origin/master
+        Cell row = kvs.get(0);
+        Comparator<Cell> comp = new SearchComparator(kvBuilder, 
+          row.getRowArray(), row.getRowOffset(), row.getRowLength(), family, qualifier);
         // pos === ( -(insertion point) - 1)
         int pos = Collections.binarySearch(kvs, null, comp);
         // never will exact match
@@ -134,12 +126,13 @@ public class KeyValueUtil {
      * @param family
      * @param qualifier
      */
-    public static KeyValue getColumnLatest(KeyValueBuilder kvBuilder, KeyValue[] kvs, byte[] family, byte[] qualifier) {
+    public static Cell getColumnLatest(KeyValueBuilder kvBuilder, Cell[] kvs, byte[] family, byte[] qualifier) {
         if (kvs.length == 0) {
             return null;
         }
-        KeyValue kvForRow = kvs[0];
-        Comparator<KeyValue> comp = new SearchComparator(kvBuilder, kvForRow, family, qualifier);
+        Cell kvForRow = kvs[0];
+        Comparator<Cell> comp = new SearchComparator(kvBuilder, kvForRow.getRowArray(), 
+          kvForRow.getRowOffset(), kvForRow.getRowLength(), family, qualifier);
         // pos === ( -(insertion point) - 1)
         int pos = Arrays.binarySearch(kvs, null, comp);
         // never will exact match
@@ -151,12 +144,12 @@ public class KeyValueUtil {
           return null; // doesn't exist
         }
     
-        KeyValue kv = kvs[pos];
-        if (Bytes.compareTo(kv.getBuffer(), kv.getFamilyOffset(), kv.getFamilyLength(),
+        Cell kv = kvs[pos];
+        if (Bytes.compareTo(kv.getFamilyArray(), kv.getFamilyOffset(), kv.getFamilyLength(),
                 family, 0, family.length) != 0) {
             return null;
         }
-        if (Bytes.compareTo(kv.getBuffer(), kv.getQualifierOffset(), kv.getQualifierLength(),
+        if (Bytes.compareTo(kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierLength(),
                 qualifier, 0, qualifier.length) != 0) {
             return null;
         }
@@ -171,41 +164,38 @@ public class KeyValueUtil {
      */
 	private static class SearchComparator implements Comparator<Cell> {
 	  private final KeyValueBuilder kvBuilder;
-		private final Cell r;
-		private final byte[] family;
-		private final byte[] qualifier;
+    private final byte[] row;
+    private final byte[] family;
+    private final byte[] qualifier;
+    private final int rowOff;
+    private final int rowLen;
 
-		public SearchComparator(KeyValueBuilder kvBuilder, Cell r, byte[] f, byte[] q) {
-		  this.kvBuilder = kvBuilder;
-			this.r = r;
-			family = f;
-			qualifier = q;
-		}
+    public SearchComparator(KeyValueBuilder kvBuilder, byte[] r, int rOff, int rLen, byte[] f, byte[] q) {
+      this.kvBuilder = kvBuilder;
+      row = r;
+      family = f;
+      qualifier = q;
+      rowOff = rOff;
+      rowLen = rLen;
+    }
 
 		@Override
     public int compare(final Cell l, final Cell ignored) {
 			assert ignored == null;
-			final byte[] buf = l.getRowArray();
-			final int rOff = l.getRowOffset();
-			final short rLen = l.getRowLength();
       KVComparator comparator = kvBuilder.getKeyValueComparator();
 			// row
-			int val = comparator.compareRows(l.getRowArray(), l.getRowOffset(), l.getRowLength(),
-			  r.getRowArray(), r.getRowOffset(), r.getRowLength());
+			int val = comparator.compareRows(l.getRowArray(), l.getRowOffset(), 
+			  l.getRowLength(), row, rowOff, rowLen);
 			if (val != 0) {
 				return val;
 			}
 			// family
-			final int fOff = l.getFamilyOffset();
-			final byte fLen = l.getFamilyLength();
-      // family
-			val = kvBuilder.compareFamily(l.getFamilyArray(), fOff, fLen, family, 0, family.length);
+			val = kvBuilder.compareFamily(l, family, 0, family.length);
 			if (val != 0) {
 				return val;
 			}
 			// qualifier
-      val = kvBuilder.compareQualifier(l.getQualifierArray(), l.getQualifierOffset(),
-              l.getQualifierLength(), qualifier, 0, qualifier.length);
+      val = kvBuilder.compareQualifier(l, qualifier, 0, qualifier.length);
 			if (val != 0) {
 				return val;
 			}

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/dc94419d/phoenix-core/src/main/java/org/apache/phoenix/util/ResultUtil.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/ResultUtil.java b/phoenix-core/src/main/java/org/apache/phoenix/util/ResultUtil.java
new file mode 100644
index 0000000..2112e44
--- /dev/null
+++ b/phoenix-core/src/main/java/org/apache/phoenix/util/ResultUtil.java
@@ -0,0 +1,188 @@
+/*
+ * 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.phoenix.util;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+
+import org.apache.hadoop.hbase.Cell;
+import org.apache.hadoop.hbase.KeyValue;
+import org.apache.hadoop.hbase.client.Result;
+import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
+import org.apache.hadoop.hbase.util.Bytes;
+
+/**
+ * Static class for various methods that would be nice to have added to {@link org.apache.hadoop.hbase.client.Result}.
+ * These methods work off of the raw bytes preventing the explosion of Result into object form.
+ * 
+ * 
+ * @since 0.1
+ */
+public class ResultUtil {
+    public static final Result EMPTY_RESULT = new Result() {
+        @Override
+        public final boolean isEmpty() { return true; }
+    };
+    
+    private ResultUtil() {
+    }
+    
+    public static Result toResult(ImmutableBytesWritable bytes) {
+        byte [] buf = bytes.get();
+        int offset = bytes.getOffset();
+        int finalOffset = bytes.getSize() + offset;
+        List<Cell> kvs = new ArrayList<Cell>();
+        while(offset < finalOffset) {
+          int keyLength = Bytes.toInt(buf, offset);
+          offset += Bytes.SIZEOF_INT;
+          kvs.add(new KeyValue(buf, offset, keyLength));
+          offset += keyLength;
+        }
+        return Result.create(kvs);
+    }
+    
+    /**
+     * Return a pointer into a potentially much bigger byte buffer that points to the key of a Result.
+     * @param r
+     */
+    public static ImmutableBytesWritable getKey(Result r) {
+        return getKey(r, 0);
+    }
+    
+    public static void getKey(Result r, ImmutableBytesWritable key) {
+        key.set(r.getRow());
+        //key.set(getRawBytes(r), getKeyOffset(r), getKeyLength(r));
+    }
+    
+    public static void getKey(KeyValue value, ImmutableBytesWritable key) {
+        key.set(value.getBuffer(), value.getRowOffset(), value.getRowLength());
+    }
+    
+    /**
+     * Return a pointer into a potentially much bigger byte buffer that points to the key of a Result.
+     * Use offset to return a subset of the key bytes, for example to skip the organization ID embedded
+     * in all of our keys.
+     * @param r
+     * @param offset offset added to start of key and subtracted from key length (to select subset of key bytes)
+     */
+    public static ImmutableBytesWritable getKey(Result r, int offset) {
+        return new ImmutableBytesWritable(getRawBytes(r), getKeyOffset(r) + offset, getKeyLength(r) - offset);
+    }
+
+    public static void getKey(Result r, int offset, int length, ImmutableBytesWritable key) {
+        key.set(getRawBytes(r), getKeyOffset(r) + offset, length);
+    }
+
+    /**
+     * Comparator for comparing the keys from two Results in-place, without allocating new byte arrays
+     */
+    public static final Comparator<Result> KEY_COMPARATOR = new Comparator<Result>() {
+
+        @Override
+        public int compare(Result r1, Result r2) {
+            byte[] r1Bytes = getRawBytes(r1);
+            byte[] r2Bytes = getRawBytes(r2);
+            return Bytes.compareTo(r1Bytes, getKeyOffset(r1), getKeyLength(r1), r2Bytes, getKeyOffset(r2), getKeyLength(r2));
+        }
+        
+    };
+    
+    /**
+     * Get the offset into the Result byte array to the key.
+     * @param r
+     */
+    static int getKeyOffset(Result r) {
+        KeyValue firstKV = org.apache.hadoop.hbase.KeyValueUtil.ensureKeyValue(r.rawCells()[0]);
+        return firstKV.getOffset();
+    }
+    
+    static int getKeyLength(Result r) {
+        // Key length stored right before key as a short
+        return Bytes.toShort(getRawBytes(r), getKeyOffset(r) - Bytes.SIZEOF_SHORT);
+    }
+    
+    static byte[] getRawBytes(Result r) {
+        KeyValue firstKV = org.apache.hadoop.hbase.KeyValueUtil.ensureKeyValue(r.rawCells()[0]);
+        return firstKV.getBuffer();
+    }
+
+    public static int compareKeys(Result r1, Result r2) {
+        return Bytes.compareTo(getRawBytes(r1), getKeyOffset(r1), getKeyLength(r1), getRawBytes(r2), getKeyOffset(r2), getKeyLength(r2));
+    }
+
+    /**
+     * Binary search for latest column value without allocating memory in the process
+     */
+    public static KeyValue getColumnLatest(Result r, byte[] family, byte[] qualifier) {
+        byte[] rbytes = getRawBytes(r);
+        int roffset = getKeyOffset(r);
+        int rlength = getKeyLength(r);
+        return getColumnLatest(r, rbytes, roffset, rlength, family, 0, family.length, qualifier, 0, qualifier.length);
+    }
+
+    public static KeyValue getSearchTerm(Result r, byte[] family, byte[] qualifier) {
+        byte[] rbytes = getRawBytes(r);
+        int roffset = getKeyOffset(r);
+        int rlength = getKeyLength(r);
+        return KeyValue.createFirstOnRow(rbytes, roffset, rlength, family, 0, family.length, qualifier, 0, qualifier.length);
+    }
+    /**
+     * Binary search for latest column value without allocating memory in the process
+     */
+    public static KeyValue getColumnLatest(Result r, byte[] row, int roffset, int rlength, byte[] family, int foffset, int flength, byte[] qualifier, int qoffset, int qlength) {
+        KeyValue searchTerm = KeyValue.createFirstOnRow(row, roffset, rlength, family, foffset, flength, qualifier, qoffset, qlength);
+        return getColumnLatest(r,searchTerm);
+        
+    }
+
+     /**
+     * Binary search for latest column value without allocating memory in the process
+     * @param r
+     * @param searchTerm
+     */
+    public static KeyValue getColumnLatest(Result r, KeyValue searchTerm) {
+        KeyValue [] kvs = r.raw(); // side effect possibly.
+        if (kvs == null || kvs.length == 0) {
+          return null;
+        }
+        
+        // pos === ( -(insertion point) - 1)
+        int pos = Arrays.binarySearch(kvs, searchTerm, KeyValue.COMPARATOR);
+        // never will exact match
+        if (pos < 0) {
+          pos = (pos+1) * -1;
+          // pos is now insertion point
+        }
+        if (pos == kvs.length) {
+          return null; // doesn't exist
+        }
+
+        KeyValue kv = kvs[pos];
+        if (Bytes.compareTo(kv.getBuffer(), kv.getFamilyOffset(), kv.getFamilyLength(),
+                searchTerm.getBuffer(), searchTerm.getFamilyOffset(), searchTerm.getFamilyLength()) != 0) {
+            return null;
+        }
+        if (Bytes.compareTo(kv.getBuffer(), kv.getQualifierOffset(), kv.getQualifierLength(),
+                searchTerm.getBuffer(), searchTerm.getQualifierOffset(), searchTerm.getQualifierLength()) != 0) {
+            return null;
+        }
+        return kv;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/dc94419d/phoenix-core/src/test/java/org/apache/phoenix/end2end/index/IndexTestUtil.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/end2end/index/IndexTestUtil.java b/phoenix-core/src/test/java/org/apache/phoenix/end2end/index/IndexTestUtil.java
index 8943c99..bee67b4 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/end2end/index/IndexTestUtil.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/end2end/index/IndexTestUtil.java
@@ -17,13 +17,13 @@
  */
 package org.apache.phoenix.end2end.index;
 
-import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.COLUMN_NAME;
 import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.COLUMN_FAMILY;
+import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.COLUMN_NAME;
+import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.SYSTEM_CATALOG_SCHEMA;
+import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.SYSTEM_CATALOG_TABLE;
 import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.TABLE_NAME;
 import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.TABLE_SCHEM;
 import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.TENANT_ID;
-import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.SYSTEM_CATALOG_SCHEMA;
-import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.SYSTEM_CATALOG_TABLE;
 
 import java.sql.Connection;
 import java.sql.PreparedStatement;
@@ -38,12 +38,8 @@ import org.apache.hadoop.hbase.client.Delete;
 import org.apache.hadoop.hbase.client.Mutation;
 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
 import org.apache.hadoop.hbase.util.Bytes;
-import org.apache.phoenix.client.KeyValueBuilder;
-
-import com.google.common.collect.Lists;
 import org.apache.phoenix.hbase.index.util.KeyValueBuilder;
 import org.apache.phoenix.query.QueryConstants;
-import org.apache.phoenix.schema.SortOrder;
 import org.apache.phoenix.schema.ColumnNotFoundException;
 import org.apache.phoenix.schema.PColumn;
 import org.apache.phoenix.schema.PColumnFamily;
@@ -51,6 +47,7 @@ import org.apache.phoenix.schema.PDataType;
 import org.apache.phoenix.schema.PRow;
 import org.apache.phoenix.schema.PTable;
 import org.apache.phoenix.schema.RowKeySchema;
+import org.apache.phoenix.schema.SortOrder;
 import org.apache.phoenix.util.IndexUtil;
 import org.apache.phoenix.util.MetaDataUtil;
 import org.apache.phoenix.util.SchemaUtil;