pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
To: pgsql-jdbc@postgresql.org
Subject: Patch for binary transfer of UUID type
Date: Sun, 25 Sep 2011 01:46:57 +0300
Message-ID: <4E7E5DE1.9030709@nitorcreations.com> (raw)

Nothing much to say. It is much faster than the txt encoding (both in transferred bytes and in cpu cycles).

Attachments:

binary-uuid.patch (text/plain, 4.4K) download 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

Message-ID: <4E7E5DE1.9030709@nitorcreations.com>
Permalink:  ../4E7E5DE1.9030709@nitorcreations.com/
Also on:    postgresql.org/message-id/4E7E5DE1.9030709@nitorcreations.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: mikko.tiihonen@nitorcreations.com
  Subject: Re: Patch for binary transfer of UUID type
  In-Reply-To: <4E7E5DE1.9030709@nitorcreations.com>
  References: <4E7E5DE1.9030709@nitorcreations.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox