From: Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
To: pgsql-jdbc@postgresql.org
Subject: Patch for binary transfer of point and box geometric types
Date: Sat, 24 Sep 2011 15:33:01 +0300
Message-ID: <4E7DCDFD.70606@nitorcreations.com> (raw)
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
Attachments:binary-geometry.patch (text/plain, 11.1K) download
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);+}
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 point and box geometric types
In-Reply-To: <4E7DCDFD.70606@nitorcreations.com>
References: <4E7DCDFD.70606@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