virajjasani commented on code in PR #1736:
URL: https://github.com/apache/phoenix/pull/1736#discussion_r1458076655


##########
phoenix-core-client/src/main/java/org/apache/phoenix/util/ByteUtil.java:
##########
@@ -469,6 +474,105 @@ public static byte[] previousKey(byte[] key) {
         return previousKey;
     }
 
+    public static byte[] getLargestPossibleRowKeyInRange(byte[] startKey, 
byte[] endKey) {
+        if (startKey.length == 0 && endKey.length == 0) {
+            return HConstants.EMPTY_END_ROW;
+        }
+        byte[] rowKey;
+        try {
+            if (startKey.length > 0 && endKey.length > 0) {
+                int commonBytesIdx = 0;
+                while (commonBytesIdx < startKey.length && commonBytesIdx < 
endKey.length) {
+                    if (startKey[commonBytesIdx] == endKey[commonBytesIdx]) {
+                        commonBytesIdx++;
+                    } else {
+                        break;
+                    }
+                }
+                if (commonBytesIdx == 0) {
+                    rowKey = 
ByteUtil.previousKeyWithLength(ByteUtil.concat(endKey,
+                                    new byte[startKey.length + 1]),
+                            Math.max(endKey.length, startKey.length) + 1);
+                } else {
+                    byte[] newStartKey;
+                    byte[] newEndKey;
+                    if (commonBytesIdx < startKey.length) {
+                        newStartKey = new byte[startKey.length - 
commonBytesIdx];
+                        System.arraycopy(startKey, commonBytesIdx, 
newStartKey, 0,
+                                newStartKey.length);
+                    } else {
+                        newStartKey = startKey;
+                    }
+                    if (commonBytesIdx < endKey.length) {
+                        newEndKey = new byte[endKey.length - commonBytesIdx];
+                        System.arraycopy(endKey, commonBytesIdx, newEndKey, 0, 
newEndKey.length);
+                    } else {
+                        newEndKey = endKey;
+                    }
+                    byte[] commonBytes = new byte[commonBytesIdx];
+                    System.arraycopy(startKey, 0, commonBytes, 0, 
commonBytesIdx);
+                    byte[] tmpRowKey = 
ByteUtil.previousKeyWithLength(ByteUtil.concat(newEndKey,
+                                    new byte[newStartKey.length + 1]),
+                            Math.max(newEndKey.length, newStartKey.length) + 
1);
+                    // tmpRowKey can be null if newEndKey has only \x00 bytes
+                    if (tmpRowKey == null) {
+                        tmpRowKey = new byte[newEndKey.length - 1];
+                        System.arraycopy(newEndKey, 0, tmpRowKey, 0, 
tmpRowKey.length);
+                        rowKey = ByteUtil.concat(commonBytes, tmpRowKey);
+                    } else {
+                        rowKey = ByteUtil.concat(commonBytes, tmpRowKey);
+                    }
+                }
+            } else if (endKey.length > 0) {
+                rowKey = ByteUtil.previousKeyWithLength(ByteUtil.concat(endKey,
+                        new byte[1]), endKey.length + 1);
+            } else {
+                rowKey = ByteUtil.nextKeyWithLength(ByteUtil.concat(startKey,
+                        new byte[1]), startKey.length + 1);
+            }
+            if (rowKey == null) {

Review Comment:
   This is not required because callers are making enough attempt to ride over 
null value returned here. e.g. if this returns null, either start or end key is 
returned as rowkey (depending on which one is inclusive in the scan range).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to