Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 1474CB5DC5A for ; Sat, 24 Sep 2011 09:33:11 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 04327-07 for ; Sat, 24 Sep 2011 12:33:04 +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 08001B5DC60 for ; Sat, 24 Sep 2011 09:33:03 -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 p8OCWxR0014343 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Sat, 24 Sep 2011 15:32:59 +0300 Message-ID: <4E7DCDFD.70606@nitorcreations.com> Date: Sat, 24 Sep 2011 15:33:01 +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 point and box geometric types Content-Type: multipart/mixed; boundary="------------090201070803080505090001" 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/106 X-Sequence-Number: 22224 This is a multi-part message in MIME format. --------------090201070803080505090001 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Hi, This patch adds generic support for PGobject subclasses to use binary transfer mode and modifies PGpoint and PGbox objects to use it. A PGobject must implement a new PGBinaryObject interface to support binary transfers. I made it a separate interface so that existing custom PGobject instances need not be modified. The registration of OIDs is still hard coded in the AbstractJdbc2Connection so no custom types can yet use the binary transfer feature. Adding other geometric types is very easy. -Mikko --------------090201070803080505090001 Content-Type: text/plain; name="binary-geometry.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="binary-geometry.patch" diff -uNr '--exclude=CVS' pgjdbc.bin_array/org/postgresql/core/BaseConnection.java pgjdbc.bin_geom/org/postgresql/core/BaseConnection.java --- pgjdbc.bin_array/org/postgresql/core/BaseConnection.java 2011-09-23 19:16:53.391788100 +0300 +++ pgjdbc.bin_geom/org/postgresql/core/BaseConnection.java 2011-09-24 15:05:22.234567711 +0300 @@ -65,10 +65,11 @@ * * @param type the backend typename * @param value the type-specific string representation of the value + * @param byteValue the type-specific binary representation of the value * @return an appropriate object; never null. * @throws SQLException if something goes wrong */ - public Object getObject(String type, String value) throws SQLException; + public Object getObject(String type, String value, byte[] byteValue) throws SQLException; public Encoding getEncoding() throws SQLException; diff -uNr '--exclude=CVS' pgjdbc.bin_array/org/postgresql/core/Oid.java pgjdbc.bin_geom/org/postgresql/core/Oid.java --- pgjdbc.bin_array/org/postgresql/core/Oid.java 2011-09-23 19:16:53.399788054 +0300 +++ pgjdbc.bin_geom/org/postgresql/core/Oid.java 2011-09-24 15:05:22.237567693 +0300 @@ -68,6 +68,8 @@ public static final int UUID_ARRAY = 2951; public static final int XML = 142; public static final int XML_ARRAY = 143; + public static final int POINT = 600; + public static final int BOX = 603; /** * Returns the name of the oid as string. diff -uNr '--exclude=CVS' pgjdbc.bin_array/org/postgresql/geometric/PGbox.java pgjdbc.bin_geom/org/postgresql/geometric/PGbox.java --- pgjdbc.bin_array/org/postgresql/geometric/PGbox.java 2011-09-21 19:53:08.186973178 +0300 +++ pgjdbc.bin_geom/org/postgresql/geometric/PGbox.java 2011-09-24 15:05:22.239567683 +0300 @@ -10,6 +10,7 @@ package org.postgresql.geometric; import org.postgresql.util.GT; +import org.postgresql.util.PGBinaryObject; import org.postgresql.util.PGobject; import org.postgresql.util.PGtokenizer; import org.postgresql.util.PSQLException; @@ -21,7 +22,7 @@ /** * This represents the box datatype within org.postgresql. */ -public class PGbox extends PGobject implements Serializable, Cloneable +public class PGbox extends PGobject implements PGBinaryObject, Serializable, Cloneable { /** * These are the two points. @@ -86,6 +87,16 @@ point[0] = new PGpoint(t.getToken(0)); point[1] = new PGpoint(t.getToken(1)); } + + /** + * @param b Definition of this point in PostgreSQL's binary syntax + */ + public void setByteValue(byte[] b, int offset) { + point[0] = new PGpoint(); + point[0].setByteValue(b, offset); + point[1] = new PGpoint(); + point[1].setByteValue(b, offset + point[0].lengthInBytes()); + } /** * @param obj Object to compare with @@ -150,4 +161,13 @@ { return point[0].toString() + "," + point[1].toString(); } + + public int lengthInBytes() { + return point[0].lengthInBytes() + point[1].lengthInBytes(); + } + + public void toBytes(byte[] bytes, int offset) { + point[0].toBytes(bytes, offset); + point[1].toBytes(bytes, offset + point[0].lengthInBytes()); + } } diff -uNr '--exclude=CVS' pgjdbc.bin_array/org/postgresql/geometric/PGpoint.java pgjdbc.bin_geom/org/postgresql/geometric/PGpoint.java --- pgjdbc.bin_array/org/postgresql/geometric/PGpoint.java 2011-09-21 19:53:08.190973153 +0300 +++ pgjdbc.bin_geom/org/postgresql/geometric/PGpoint.java 2011-09-24 15:05:22.243567660 +0300 @@ -9,7 +9,9 @@ */ package org.postgresql.geometric; +import org.postgresql.util.ByteConverter; import org.postgresql.util.GT; +import org.postgresql.util.PGBinaryObject; import org.postgresql.util.PGobject; import org.postgresql.util.PGtokenizer; import org.postgresql.util.PSQLException; @@ -25,7 +27,7 @@ * This implements a version of java.awt.Point, except it uses double * to represent the coordinates. */ -public class PGpoint extends PGobject implements Serializable, Cloneable +public class PGpoint extends PGobject implements PGBinaryObject, Serializable, Cloneable { /** * The X coordinate of the point @@ -85,6 +87,14 @@ throw new PSQLException(GT.tr("Conversion to type {0} failed: {1}.", new Object[]{type,s}), PSQLState.DATA_TYPE_MISMATCH, e); } } + + /** + * @param b Definition of this point in PostgreSQL's binary syntax + */ + public void setByteValue(byte[] b, int offset) { + x = ByteConverter.float8(b, offset); + y = ByteConverter.float8(b, offset + 8); + } /** * @param obj Object to compare with @@ -115,6 +125,18 @@ return "(" + x + "," + y + ")"; } + public int lengthInBytes() { + return 16; + } + + /** + * @return the PGpoint in the binary syntax expected by org.postgresql + */ + public void toBytes(byte[] b, int offset) { + ByteConverter.float8(b, offset, x); + ByteConverter.float8(b, offset + 8, y); + } + /** * Translate the point by the supplied amount. * @param x integer amount to add on the x axis diff -uNr '--exclude=CVS' pgjdbc.bin_array/org/postgresql/jdbc2/AbstractJdbc2Connection.java pgjdbc.bin_geom/org/postgresql/jdbc2/AbstractJdbc2Connection.java --- pgjdbc.bin_array/org/postgresql/jdbc2/AbstractJdbc2Connection.java 2011-09-24 15:04:06.093982785 +0300 +++ pgjdbc.bin_geom/org/postgresql/jdbc2/AbstractJdbc2Connection.java 2011-09-24 15:05:22.250567622 +0300 @@ -18,6 +18,7 @@ import org.postgresql.PGNotification; import org.postgresql.fastpath.Fastpath; import org.postgresql.largeobject.LargeObjectManager; +import org.postgresql.util.PGBinaryObject; import org.postgresql.util.PSQLState; import org.postgresql.util.PGobject; import org.postgresql.util.PSQLException; @@ -161,6 +162,8 @@ binaryOids.set(Oid.FLOAT8_ARRAY); binaryOids.set(Oid.VARCHAR_ARRAY); binaryOids.set(Oid.TEXT_ARRAY); + binaryOids.set(Oid.POINT); + binaryOids.set(Oid.BOX); } // the pre 8.0 servers do not disclose their internal encoding for // time fields so do not try to use them. @@ -495,7 +498,7 @@ * @return PGobject for this type, and set to value * @exception SQLException if value is not correct for this type */ - public Object getObject(String type, String value) throws SQLException + public Object getObject(String type, String value, byte[] byteValue) throws SQLException { if (typemap != null) { @@ -526,7 +529,12 @@ { obj = (PGobject) (klass.newInstance()); obj.setType(type); - obj.setValue(value); + if (byteValue != null && obj instanceof PGBinaryObject) { + PGBinaryObject binObj = (PGBinaryObject) obj; + binObj.setByteValue(byteValue, 0); + } else { + obj.setValue(value); + } } else { diff -uNr '--exclude=CVS' pgjdbc.bin_array/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java pgjdbc.bin_geom/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java --- pgjdbc.bin_array/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java 2011-09-24 15:04:06.094982779 +0300 +++ pgjdbc.bin_geom/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java 2011-09-24 15:05:22.251567616 +0300 @@ -2657,7 +2657,10 @@ if (result != null) return result; - return connection.getObject(getPGType(columnIndex), getString(columnIndex)); + if (isBinary(columnIndex)) { + return connection.getObject(getPGType(columnIndex), null, this_row[columnIndex - 1]); + } + return connection.getObject(getPGType(columnIndex), getString(columnIndex), null); } public Object getObject(String columnName) throws SQLException diff -uNr '--exclude=CVS' pgjdbc.bin_array/org/postgresql/jdbc2/AbstractJdbc2Statement.java pgjdbc.bin_geom/org/postgresql/jdbc2/AbstractJdbc2Statement.java --- pgjdbc.bin_array/org/postgresql/jdbc2/AbstractJdbc2Statement.java 2011-09-24 15:04:06.095982774 +0300 +++ pgjdbc.bin_geom/org/postgresql/jdbc2/AbstractJdbc2Statement.java 2011-09-24 15:05:22.253567604 +0300 @@ -26,6 +26,7 @@ import org.postgresql.core.*; import org.postgresql.core.types.*; import org.postgresql.util.ByteConverter; +import org.postgresql.util.PGBinaryObject; import org.postgresql.util.PSQLException; import org.postgresql.util.PSQLState; import org.postgresql.util.PGobject; @@ -1689,7 +1690,14 @@ if (oid == Oid.UNSPECIFIED) throw new PSQLException(GT.tr("Unknown type {0}.", typename), PSQLState.INVALID_PARAMETER_TYPE); - setString(parameterIndex, x.getValue(), oid); + if ((x instanceof PGBinaryObject) && connection.binaryTransferSend(oid)) { + PGBinaryObject binObj = (PGBinaryObject) x; + byte[] data = new byte[binObj.lengthInBytes()]; + binObj.toBytes(data, 0); + bindBytes(parameterIndex, data, oid); + } else { + setString(parameterIndex, x.getValue(), oid); + } } /* diff -uNr '--exclude=CVS' pgjdbc.bin_array/org/postgresql/util/PGBinaryObject.java pgjdbc.bin_geom/org/postgresql/util/PGBinaryObject.java --- pgjdbc.bin_array/org/postgresql/util/PGBinaryObject.java 1970-01-01 02:00:00.000000000 +0200 +++ pgjdbc.bin_geom/org/postgresql/util/PGBinaryObject.java 2011-09-24 15:05:22.266567531 +0300 @@ -0,0 +1,44 @@ +/*------------------------------------------------------------------------- +* +* Copyright (c) 2011, PostgreSQL Global Development Group +* +* IDENTIFICATION +* $PostgreSQL: pgjdbc/org/postgresql/util/PGBinaryObject.java,v 1.0 2011/09/30 13:50:29 davecramer Exp $ +* +*------------------------------------------------------------------------- +*/package org.postgresql.util; + +import java.sql.SQLException; + +/** + * PGBinaryObject is a inteface that classes extending {@link PGobject} can use to + * take advantage of more optimal binary encoding of the data type. + */ +public interface PGBinaryObject { + /** + * This method is called to set the value of this object. + * + * @param value data containing the binary representation of the value of the object + * @param offset the offset in the byte array where object data starts + * @exception SQLException thrown if value is invalid for this type + */ + public void setByteValue(byte[] value, int offset) throws SQLException; + + /** + * This method is called to return the number of bytes needed to store + * this object in the binary form required by org.postgresql. + * + * @return the number of bytes needed to store this object + */ + public int lengthInBytes(); + + /** + * This method is called the to store the value of the object, in the + * binary form required by org.postgresql. + * + * @param bytes the array to store the value, it is guaranteed to be at + * lest {@link #lengthInBytes} in size. + * @param offset the offset in the byte array where object must be stored + */ + public void toBytes(byte[] bytes, int offset); +} --------------090201070803080505090001--