Return-Path: owner-postman
Received: from eden.CS.Berkeley.EDU (eden.CS.Berkeley.EDU [128.32.149.4]) by nobozo.CS.Berkeley.EDU (8.6.4/8.6.3) with ESMTP id MAA03096 for <post_questions@postgres.Berkeley.EDU>; Tue, 29 Mar 1994 12:46:56 -0800
Received: from localhost (andrew@localhost) by eden.CS.Berkeley.EDU (8.6.4/8.1B) id MAA12664; Tue, 29 Mar 1994 12:46:42 -0800
Date: Tue, 29 Mar 1994 12:46:42 -0800
From: "Andrew K. Yu" <andrew@postgres.Berkeley.EDU>
Message-Id: <199403292046.MAA12664@eden.CS.Berkeley.EDU>
To: ejonge@tpd.tno.nl
Subject: Re:  LARGE OBJECT <=> IMAGE
Cc: andrew@postgres.Berkeley.EDU, post_questions@postgres.Berkeley.EDU


> I've a problem.

promise not to bug me, I'll share with you one of the solutions I once
wrote. (As evident in the terseness, this is a quick and dirty work-- not to
be taken religiously.)
	
> data=??? - this is a large object, BUT HOW CAN I REPRESENT THIS?
> 

look at the image class below.

> I've read:
> "the design and implementation of the inversion file system" by Olson
> 
> The only way is to create a unique filename?!, so i can store the large object!?
> 

the dirty (read undocumented) way of creating a unique name in the large
object namespace is use newoid() as shown below.

As for the interface below, you have to have to input from a file, though but
they don't have to have unque names. They can all be /tmp/image.gif.

-andrew

--------------------------------------------------------------------------XO--
#!/bin/sh
# This is a shell archive (produced by shar 3.50)
# To extract the files from this archive, save it to a file, remove
# everything above the "!/bin/sh" line above, and type "sh file_name".
#
# made 03/29/1994 20:30 UTC by andrew@eden.CS.Berkeley.EDU
# Source directory /home2/andrew/tmp/invlo
#
# existing files will NOT be overwritten unless -c is specified
#
# This shar contains:
# length  mode       name
# ------ ---------- ------------------------------------------
#    374 -rw-r--r-- Makefile
#    228 -rw-r--r-- README
#     65 -rw-r--r-- image.pq
#   1637 -rw-r--r-- invio.c
#    402 -rw-r--r-- setup.pq
#
# ============= Makefile ==============
if test -f 'Makefile' -a X"$1" != X"-c"; then
	echo 'x - skipping Makefile (File already exists)'
else
echo 'x - extracting Makefile (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'Makefile' &&
CFLAGS= -g -I/private/src/postgres/src/backend \
X	-I/private/src/postgres/src/backend/obj \
X	-I/private/src/postgres/src/backend/tmp \
X	-I/private/src/postgres/src/backend/utils \
X	-I/private/src/postgres/src/backend/catalog \
X	-I/private/src/postgres/src/backend/port/ultrix4
X
LIB= /private/src/postgres/src/libpq/obj/libpq.a
X
invio.o:
X	$(CC) $(CFLAGS) -G 0 -c invio.c 
X
X
X
SHAR_EOF
chmod 0644 Makefile ||
echo 'restore of Makefile failed'
Wc_c="`wc -c < 'Makefile'`"
test 374 -eq "$Wc_c" ||
	echo 'Makefile: original size 374, current size' "$Wc_c"
fi
# ============= README ==============
if test -f 'README' -a X"$1" != X"-c"; then
	echo 'x - skipping README (File already exists)'
else
echo 'x - extracting README (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'README' &&
X
Sample Usage of Inversion Objects
=================================
(andrew@postgres.berkeley.edu)
X
Makefile	
setup.pq	the setup (inversion objects + the class image)
image.pq	sample append
invio.c		the user-defined functions
X
SHAR_EOF
chmod 0644 README ||
echo 'restore of README failed'
Wc_c="`wc -c < 'README'`"
test 228 -eq "$Wc_c" ||
	echo 'README: original size 228, current size' "$Wc_c"
fi
# ============= image.pq ==============
if test -f 'image.pq' -a X"$1" != X"-c"; then
	echo 'x - skipping image.pq (File already exists)'
else
echo 'x - extracting image.pq (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'image.pq' &&
X
append image (name="testimage", raster="/private/smalldata")
\g
SHAR_EOF
chmod 0644 image.pq ||
echo 'restore of image.pq failed'
Wc_c="`wc -c < 'image.pq'`"
test 65 -eq "$Wc_c" ||
	echo 'image.pq: original size 65, current size' "$Wc_c"
fi
# ============= invio.c ==============
if test -f 'invio.c' -a X"$1" != X"-c"; then
	echo 'x - skipping invio.c (File already exists)'
else
echo 'x - extracting invio.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'invio.c' &&
#include "tmp/c.h"
#include "utils/log.h"
#include "tmp/libpq-fe.h"
#include "tmp/libpq-fs.h"
#include "catalog/pg_lobj.h"
X
#define BUFSIZE        1024
X
/*
X * inv_filein--
X * 	copies a Unix file into the Inversion System
X */
char *inv_filein( filename )
X     char *filename;
{
X    int inv_fd;
X    int fd;
X    int nbytes, tmp;
X    char buf[BUFSIZE];
X    oid invObjOid;	
X    char inv_file[256];
X    char *retval;
X
X    /*
X     * open the file to be read in
X     */
X    fd = open(filename, O_RDONLY, 0666);
X    if (fd < 0)  {   /* error */
X	elog(WARN, "can't open unix file\"%s\"\n", filename);
X    }
X
X    /*
X     * our new object
X     */
X    invObjOid= newoid();
X    sprintf(inv_file, "/INVOBJ%x", invObjOid);
X
X    /*
X     * create an inversion "object"
X     */
X    inv_fd = LOcreat(inv_file, INV_READ|INV_WRITE, Inversion);
X    if (inv_fd < 0) {
X	elog(WARN, "can't create inv object \"%s\" from \"%s\"\n",
X	     inv_file, filename);
X	fprintf(stderr, "can't creat inv file\n");
X    }
X
X    /*
X     * read in from the Unix file and write to the inversion file
X     */
X    while ((nbytes = read(fd, buf, BUFSIZE)) > 0) {
X	tmp = LO_write(buf, nbytes, inv_fd);
X        if (tmp < nbytes) {
X	    elog(WARN, "error while reading \"%s\"\n", filename);
X	}
X    }
X
X    (void) close(fd);
X    (void) LOclose(inv_fd);
X
X    retval= (char *)palloc(4);
X    *(oid *)retval= invObjOid;
X    return retval;
}
X
/*
X * inv_fileout--
X * 	returns a textual representation (of the oid)
X */
char *inv_fileout( invObjId )
X     char *invObjId;
{
X    char buf[20];
X
X    sprintf(buf, "0x%x", *(oid *)invObjId);
X    return ((char *)strcpy(palloc(strlen(buf)+1), buf));
}
X
X
X
SHAR_EOF
chmod 0644 invio.c ||
echo 'restore of invio.c failed'
Wc_c="`wc -c < 'invio.c'`"
test 1637 -eq "$Wc_c" ||
	echo 'invio.c: original size 1637, current size' "$Wc_c"
fi
# ============= setup.pq ==============
if test -f 'setup.pq' -a X"$1" != X"-c"; then
	echo 'x - skipping setup.pq (File already exists)'
else
echo 'x - extracting setup.pq (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'setup.pq' &&
define function inv_filein ( language = "c",
X	returntype = InvObj )
X	arg is (any)
X	as "/private/src/postgres/test/invlo/invio.o"
\g
X
define function inv_fileout ( language = "c",
X	returntype = any )
X	arg is (any)
X	as "/private/src/postgres/test/invlo/invio.o"
\g
X
define type InvObj (internallength = 4,
X	input = inv_filein,
X	output = inv_fileout)
\g
X
create image(
X	name = text,
X	raster = InvObj
)
\g
SHAR_EOF
chmod 0644 setup.pq ||
echo 'restore of setup.pq failed'
Wc_c="`wc -c < 'setup.pq'`"
test 402 -eq "$Wc_c" ||
	echo 'setup.pq: original size 402, current size' "$Wc_c"
fi
exit 0
