pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Herouth Maoz <herouth@oumail.openu.ac.il>
To: sreilly@cnri.Reston.Va.US
To: pgsql-interfaces@postgresql.org
Subject: Re: [INTERFACES] binary insert with JDBC
Date: Sun, 17 May 1998 14:44:19 +0300
Message-ID: <l03110704b18474ea4d98@[147.233.159.109]> (raw)
In-Reply-To: <355A19DA.CB496923@cnri.reston.va.us>
References: <714F8949628ED1119E0F0060082C52F5095246@vesta.maidstone.gov.uk>

At 1:08 +0300 on 14/5/98, Sean Reilly wrote:


> I'm using version 6.3.2.  I just tried using blobs and it seems to
> take an unusually long time to retrieve the blobs (after the record
> has been retrieved).  Since I know my data will be under 512 bytes,
> is there any way I can store it directly in a field in the table so
> I can avoid the extra lookup?  If not, is there any way that I can
> speed up the retrieval of the blob (insertion speed doesn't matter)?
> I've already tried creating an index for the oid, but it didn't help
> much.

The simplest way would be to encode the binary data into text and save it
in a "Text" attribute - assuming you don't want to rely on conversion of
binary data into Unicode characters, which is another way of doing the same
thing without taking much space. It may be unreliable when it comes to
8-bit data, though, so keep that in mind.

The simplest way - which keeps you well in the 7bit realm - is to encode
using straightforward hex, which would mean your attribute will take
exactly twice as much space as it is originally. Let's suppose your binary
data is stored in a byte array named binData.

byte [] binData = new byte [512];

... // Data filled in the array

StringBuffer conversionBuffer = new StringBuffer( binData.length * 2 );

for (int i = 0; i < binData.length; i++ ) {

    conversionBuffer.append(
        Integer.toHexString( 0x100 | binData[i] ).substring(1) );

}

... // Make a prepared statement for the insert.

stmt.setString( 1, conversionBuffer.toString() );


The thing there with the 0x100 | binData[i] is meant to make sure you have
two digits even if the number was smaller than 0x10. It gives you a hex
string between 100 and 1ff, and then you substring the last two digits.
This is appended to the conversion buffer, and eventually, you put it in a
field in pgSQL. The field is to be defined as Text, of course.

To decode this, you can use:

.. // Make a statement and select the string into the variable hexData

byte [] binData = new byte [ hexData.length() / 2];

for (int i=0; i< hexData.length(); i += 2 ) {

    binData[i/2] = Byte.parseByte( hexData.substring( i, i+2 ), 16);

}

Disclaimer: I haven't actually tested the code above. Try it on small
amounts of data, displaying them instead of putting to the database. Once
you're satisfied that my indexes into the strings and all that are correct
(that is, an array of {128, 8} gives you really 8008 and vice versa).

Herouth





view thread (5+ messages)

Message-ID: <l03110704b18474ea4d98@[147.233.159.109]>
Permalink:  ../l03110704b18474ea4d98@%5B147.233.159.109%5D/
Also on:    postgresql.org/message-id/l03110704b18474ea4d98@[147.233.159.109]

 · 

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-interfaces@postgresql.org
  Cc: herouth@oumail.openu.ac.il, sreilly@cnri.Reston.Va.US
  Subject: Re: [INTERFACES] binary insert with JDBC
  In-Reply-To: <l03110704b18474ea4d98@[147.233.159.109]>
  References: <714F8949628ED1119E0F0060082C52F5095246@vesta.maidstone.gov.uk> <l03110704b18474ea4d98@[147.233.159.109]>

* 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