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

[1/2] git commit: PHOENIX-1369 Add back encode/decode methods as deprecated

Repository: phoenix
Updated Branches:
  refs/heads/master ea6b05386 -> dc7e8c0e6


PHOENIX-1369 Add back encode/decode methods as deprecated


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

Branch: refs/heads/master
Commit: eee98b673d03b65dfa472d29246f8d3d0e612abe
Parents: ea6b053
Author: James Taylor <ja...@apache.org>
Authored: Mon Oct 20 12:57:37 2014 -0700
Committer: James Taylor <ja...@apache.org>
Committed: Mon Oct 20 12:59:14 2014 -0700

----------------------------------------------------------------------
 .../org/apache/phoenix/util/PhoenixRuntime.java | 79 ++++++++++++++++++++
 1 file changed, 79 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/eee98b67/phoenix-core/src/main/java/org/apache/phoenix/util/PhoenixRuntime.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/PhoenixRuntime.java b/phoenix-core/src/main/java/org/apache/phoenix/util/PhoenixRuntime.java
index 8755fcd..cf102db 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/util/PhoenixRuntime.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/util/PhoenixRuntime.java
@@ -588,6 +588,85 @@ public class PhoenixRuntime {
     }
     
     /**
+     * Encode the primary key values from the table as a byte array. The values must
+     * be in the same order as the primary key constraint. If the connection and
+     * table are both tenant-specific, the tenant ID column must not be present in
+     * the values.
+     * @param conn an open connection
+     * @param fullTableName the full table name
+     * @param values the values of the primary key columns ordered in the same order
+     *  as the primary key constraint
+     * @return the encoded byte array
+     * @throws SQLException if the table cannot be found or the incorrect number of
+     *  of values are provided
+     * @see #decodePK(Connection, String, byte[]) to decode the byte[] back to the
+     *  values
+     */
+    @Deprecated
+    public static byte[] encodePK(Connection conn, String fullTableName, Object[] values) throws SQLException {
+        PTable table = getTable(conn, fullTableName);
+        PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
+        int offset = (table.getBucketNum() == null ? 0 : 1) + (table.isMultiTenant() && pconn.getTenantId() != null ? 1 : 0);
+        List<PColumn> pkColumns = table.getPKColumns();
+        if (pkColumns.size() - offset != values.length) {
+            throw new SQLException("Expected " + (pkColumns.size() - offset) + " but got " + values.length);
+        }
+        PDataType type = null;
+        TrustedByteArrayOutputStream output = new TrustedByteArrayOutputStream(table.getRowKeySchema().getEstimatedValueLength());
+        try {
+            for (int i = offset; i < pkColumns.size(); i++) {
+                if (type != null && !type.isFixedWidth()) {
+                    output.write(QueryConstants.SEPARATOR_BYTE);
+                }
+                type = pkColumns.get(i).getDataType();
+
+                //for fixed width data types like CHAR and BINARY, we need to pad values to be of max length.
+                Object paddedObj = type.pad(values[i - offset], pkColumns.get(i).getMaxLength());
+                byte[] value = type.toBytes(paddedObj);
+                output.write(value);
+            }
+            return output.toByteArray();
+        } finally {
+            try {
+                output.close();
+            } catch (IOException e) {
+                throw new RuntimeException(e); // Impossible
+            }
+        }
+    }
+
+    /**
+     * Decode a byte array value back into the Object values of the
+     * primary key constraint. If the connection and table are both
+     * tenant-specific, the tenant ID column is not expected to have
+     * been encoded and will not appear in the returned values.
+     * @param conn an open connection
+     * @param name the full table name
+     * @param encodedValue the value that was encoded with {@link #encodePK(Connection, String, Object[])}
+     * @return the Object values encoded in the byte array value
+     * @throws SQLException
+     */
+    @Deprecated
+    public static Object[] decodePK(Connection conn, String name, byte[] value) throws SQLException {
+        PTable table = getTable(conn, name);
+        PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
+        int offset = (table.getBucketNum() == null ? 0 : 1) + (table.isMultiTenant() && pconn.getTenantId() != null ? 1 : 0);
+        int nValues = table.getPKColumns().size() - offset;
+        RowKeySchema schema = table.getRowKeySchema();
+        Object[] values = new Object[nValues];
+        ImmutableBytesWritable ptr = new ImmutableBytesWritable();
+        schema.iterator(value, ptr);
+        int i = 0;
+        int fieldIdx = offset;
+        while (i < nValues && schema.next(ptr, fieldIdx, value.length) != null) {
+            values[i] = schema.getField(fieldIdx).getDataType().toObject(ptr);
+            i++;
+            fieldIdx++;
+        }
+        return values;
+    }
+
+    /**
      * Returns the opitmized query plan used by phoenix for executing the sql.
      * @param stmt to return the plan for
      * @throws SQLException


[2/2] git commit: PHOENIX-1369 Add back encode/decode methods as deprecated

Posted by ja...@apache.org.
PHOENIX-1369 Add back encode/decode methods as deprecated


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

Branch: refs/heads/master
Commit: dc7e8c0e6f5f1e9724bc875d0a892f8e6393dacd
Parents: eee98b6
Author: James Taylor <ja...@apache.org>
Authored: Mon Oct 20 13:06:18 2014 -0700
Committer: James Taylor <ja...@apache.org>
Committed: Mon Oct 20 13:06:18 2014 -0700

----------------------------------------------------------------------
 .../src/main/java/org/apache/phoenix/util/PhoenixRuntime.java       | 1 +
 1 file changed, 1 insertion(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/dc7e8c0e/phoenix-core/src/main/java/org/apache/phoenix/util/PhoenixRuntime.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/PhoenixRuntime.java b/phoenix-core/src/main/java/org/apache/phoenix/util/PhoenixRuntime.java
index cf102db..8becd98 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/util/PhoenixRuntime.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/util/PhoenixRuntime.java
@@ -70,6 +70,7 @@ import org.apache.phoenix.schema.PDataType;
 import org.apache.phoenix.schema.PTable;
 import org.apache.phoenix.schema.PTableKey;
 import org.apache.phoenix.schema.PTableType;
+import org.apache.phoenix.schema.RowKeySchema;
 import org.apache.phoenix.schema.TableNotFoundException;
 import org.apache.phoenix.schema.ValueBitSet;