Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id F23B9B5DC1A for ; Sat, 24 Sep 2011 19:47:06 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 01280-02 for ; Sat, 24 Sep 2011 22:47:00 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from auth-smtp.nebula.fi (auth-smtp.nebula.fi [217.30.180.105]) by mail.postgresql.org (Postfix) with ESMTP id 27F47B5DC09 for ; Sat, 24 Sep 2011 19:46:59 -0300 (ADT) Received: from quadvadis.localdomain (gw.nitor.fi [83.145.228.24]) (authenticated bits=0) by auth-smtp.nebula.fi (8.13.4/8.13.4) with ESMTP id p8OMku6u013930 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Sun, 25 Sep 2011 01:46:58 +0300 Message-ID: <4E7E5DE1.9030709@nitorcreations.com> Date: Sun, 25 Sep 2011 01:46:57 +0300 From: Mikko Tiihonen User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:6.0.2) Gecko/20110906 Thunderbird/6.0.2 MIME-Version: 1.0 To: pgsql-jdbc@postgresql.org Subject: Patch for binary transfer of UUID type Content-Type: multipart/mixed; boundary="------------000105000403020303060201" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201109/110 X-Sequence-Number: 22228 This is a multi-part message in MIME format. --------------000105000403020303060201 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Nothing much to say. It is much faster than the txt encoding (both in transferred bytes and in cpu cycles). --------------000105000403020303060201 Content-Type: text/plain; name="binary-uuid.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="binary-uuid.patch" diff -urN pgjdbc.bin_geom/org/postgresql/jdbc2/AbstractJdbc2Connection.java pgjdbc.bin_uuid/org/postgresql/jdbc2/AbstractJdbc2Connection.java --- pgjdbc.bin_geom/org/postgresql/jdbc2/AbstractJdbc2Connection.java 2011-09-25 00:48:19.801259402 +0300 +++ pgjdbc.bin_uuid/org/postgresql/jdbc2/AbstractJdbc2Connection.java 2011-09-25 00:56:22.916554156 +0300 @@ -164,6 +164,7 @@ binaryOids.set(Oid.TEXT_ARRAY); binaryOids.set(Oid.POINT); binaryOids.set(Oid.BOX); + binaryOids.set(Oid.UUID); } // the pre 8.0 servers do not disclose their internal encoding for // time fields so do not try to use them. diff -urN pgjdbc.bin_geom/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java pgjdbc.bin_uuid/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java --- pgjdbc.bin_geom/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java 2011-09-25 00:48:19.802259397 +0300 +++ pgjdbc.bin_uuid/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java 2011-09-25 00:53:57.253373855 +0300 @@ -166,8 +166,12 @@ if (type.equals("unknown")) return getString(columnIndex); - if (type.equals("uuid")) + if (type.equals("uuid")) { + if (isBinary(columnIndex)) { + return getUUID(this_row[columnIndex - 1]); + } return getUUID(getString(columnIndex)); + } // Specialized support for ref cursors is neater. if (type.equals("refcursor")) @@ -3163,6 +3167,15 @@ { return data; } + + /** + * Newer JVMs will return a java.util.UUID object, but it isn't + * available in older versions. + */ + protected Object getUUID(byte[] data) throws SQLException + { + return data; + } private class PrimaryKey { diff -urN pgjdbc.bin_geom/org/postgresql/jdbc3g/AbstractJdbc3gResultSet.java pgjdbc.bin_uuid/org/postgresql/jdbc3g/AbstractJdbc3gResultSet.java --- pgjdbc.bin_geom/org/postgresql/jdbc3g/AbstractJdbc3gResultSet.java 2011-09-21 19:53:08.246972795 +0300 +++ pgjdbc.bin_uuid/org/postgresql/jdbc3g/AbstractJdbc3gResultSet.java 2011-09-25 01:00:40.115100120 +0300 @@ -15,6 +15,7 @@ import java.util.Vector; import org.postgresql.core.*; +import org.postgresql.util.ByteConverter; import org.postgresql.util.GT; import org.postgresql.util.PSQLState; import org.postgresql.util.PSQLException; @@ -40,5 +41,10 @@ return uuid; } + + protected Object getUUID(byte[] data) throws SQLException + { + return new UUID(ByteConverter.int8(data, 0), ByteConverter.int8(data, 8)); + } } diff -urN pgjdbc.bin_geom/org/postgresql/jdbc3g/AbstractJdbc3gStatement.java pgjdbc.bin_uuid/org/postgresql/jdbc3g/AbstractJdbc3gStatement.java --- pgjdbc.bin_geom/org/postgresql/jdbc3g/AbstractJdbc3gStatement.java 2011-09-21 19:53:08.247972788 +0300 +++ pgjdbc.bin_uuid/org/postgresql/jdbc3g/AbstractJdbc3gStatement.java 2011-09-25 00:58:35.156807525 +0300 @@ -15,6 +15,7 @@ import org.postgresql.core.Oid; import org.postgresql.jdbc3.AbstractJdbc3Connection; +import org.postgresql.util.ByteConverter; public abstract class AbstractJdbc3gStatement extends org.postgresql.jdbc3.AbstractJdbc3Statement { @@ -32,7 +33,7 @@ { if (x instanceof UUID && connection.haveMinimumServerVersion("8.3")) { - setString(parameterIndex, x.toString(), Oid.UUID); + setUuid(parameterIndex, (UUID)x); } else { super.setObject(parameterIndex, x); } @@ -42,10 +43,20 @@ { if (targetSqlType == Types.OTHER && x instanceof UUID && connection.haveMinimumServerVersion("8.3")) { - setString(parameterIndex, x.toString(), Oid.UUID); + setUuid(parameterIndex, (UUID) x); } else { super.setObject(parameterIndex, x, targetSqlType, scale); } } -} + private void setUuid(int parameterIndex, UUID uuid) throws SQLException { + if (connection.binaryTransferSend(Oid.UUID)) { + byte[] val = new byte[16]; + ByteConverter.int8(val, 0, uuid.getMostSignificantBits()); + ByteConverter.int8(val, 8, uuid.getLeastSignificantBits()); + bindBytes(parameterIndex, val, Oid.UUID); + } else { + bindLiteral(parameterIndex, uuid.toString(), Oid.UUID); + } + } +} \ No newline at end of file --------------000105000403020303060201--