Work on large objects continue.

This commit is contained in:
Federico Di Gregorio 2006-09-01 17:15:27 +00:00
parent 0b9d13455d
commit 36785f753b
3 changed files with 150 additions and 8 deletions

View File

@ -61,6 +61,14 @@ STATUS_ASYNC = 4
# This is a usefull mnemonic to check if the connection is in a transaction # This is a usefull mnemonic to check if the connection is in a transaction
STATUS_IN_TRANSACTION = STATUS_BEGIN STATUS_IN_TRANSACTION = STATUS_BEGIN
"""Large object flags"""
LOBJECT_INV_READ = 0
LOBJECT_INV_WRITE = 1
LOBJECT_SEEK_SET = 0
LOBJECT_SEEK_CUR = 1
LOBJECT_SEEK_END = 2
def register_adapter(typ, callable): def register_adapter(typ, callable):
"""Register 'callable' as an ISQLQuote adapter for type 'typ'.""" """Register 'callable' as an ISQLQuote adapter for type 'typ'."""

View File

@ -31,6 +31,8 @@
#ifdef PSYCOPG_EXTENSIONS #ifdef PSYCOPG_EXTENSIONS
/* lobject_open - create a new/open an existing lo */
int int
lobject_open(lobjectObject *self, connectionObject *conn, lobject_open(lobjectObject *self, connectionObject *conn,
Oid oid, int mode, Oid new_oid, char *new_file) Oid oid, int mode, Oid new_oid, char *new_file)
@ -80,5 +82,143 @@ lobject_open(lobjectObject *self, connectionObject *conn,
} }
} }
/* lobject_unlink - remove an lo from database */
int
lobject_unlink(lobjectObject *self)
{
int res;
/* first we make sure the lobject is closed and then we unlink */
lobject_close(self);
Py_BEGIN_ALLOW_THREADS;
pthread_mutex_lock(&(self->conn->lock));
pq_begin(self->conn);
res = lo_unlink(self->conn->pgconn, self->oid);
pthread_mutex_unlock(&(self->conn->lock));
Py_END_ALLOW_THREADS;
if (res == -1)
pq_raise(self->conn, NULL, NULL, NULL);
return res;
}
/* lobject_close - close an existing lo */
void
lobject_close(lobjectObject *self)
{
if (self->conn->isolation_level > 0
&& self->conn->mark == self->mark) {
if (self->fd != -1)
lo_close(self->conn->pgconn, self->fd);
}
}
/* lobject_write - write bytes to a lo */
size_t
lobject_write(lobjectObject *self, char *buf, size_t len)
{
size_t written;
Py_BEGIN_ALLOW_THREADS;
pthread_mutex_lock(&(self->conn->lock));
written = lo_write(self->conn->pgconn, self->fd, buf, len);
pthread_mutex_unlock(&(self->conn->lock));
Py_END_ALLOW_THREADS;
if (written < 0)
pq_raise(self->conn, NULL, NULL, NULL);
return written;
}
/* lobject_read - read bytes from a lo */
size_t
lobject_read(lobjectObject *self, char *buf, size_t len)
{
size_t readed;
Py_BEGIN_ALLOW_THREADS;
pthread_mutex_lock(&(self->conn->lock));
readed = lo_read(self->conn->pgconn, self->fd, buf, len);
pthread_mutex_unlock(&(self->conn->lock));
Py_END_ALLOW_THREADS;
if (readed < 0)
pq_raise(self->conn, NULL, NULL, NULL);
return readed;
}
/* lobject_seek - move the current position in the lo */
int
lobject_seek(lobjectObject *self, int pos, int whence)
{
int where;
Py_BEGIN_ALLOW_THREADS;
pthread_mutex_lock(&(self->conn->lock));
where = lo_lseek(self->conn->pgconn, self->fd, pos, whence);
pthread_mutex_unlock(&(self->conn->lock));
Py_END_ALLOW_THREADS;
if (where < 0)
pq_raise(self->conn, NULL, NULL, NULL);
return where;
}
/* lobject_tell - tell the current position in the lo */
int
lobject_tell(lobjectObject *self)
{
int where;
Py_BEGIN_ALLOW_THREADS;
pthread_mutex_lock(&(self->conn->lock));
where = lo_tell(self->conn->pgconn, self->fd);
pthread_mutex_unlock(&(self->conn->lock));
Py_END_ALLOW_THREADS;
if (where < 0)
pq_raise(self->conn, NULL, NULL, NULL);
return where;
}
/* lobject_export - export to a local file */
int
lobject_export(lobjectObject *self, char *filename)
{
int res;
Py_BEGIN_ALLOW_THREADS;
pthread_mutex_lock(&(self->conn->lock));
res = lo_export(self->conn->pgconn, self->oid, filename);
pthread_mutex_unlock(&(self->conn->lock));
Py_END_ALLOW_THREADS;
if (res < 0)
pq_raise(self->conn, NULL, NULL, NULL);
return res;
}
#endif #endif

View File

@ -54,9 +54,7 @@ psyco_lobj_close(lobjectObject *self, PyObject *args)
EXC_IF_LOBJ_UNMARKED(self); EXC_IF_LOBJ_UNMARKED(self);
self->closed = 1; self->closed = 1;
if (self->fd != -1) lobject_close(self);
lo_close(self->conn->pgconn, self->fd);
self->fd = -1;
Dprintf("psyco_lobj_close: lobject at %p closed", self); Dprintf("psyco_lobj_close: lobject at %p closed", self);
@ -122,11 +120,7 @@ lobject_dealloc(PyObject* obj)
{ {
lobjectObject *self = (lobjectObject *)obj; lobjectObject *self = (lobjectObject *)obj;
if (self->conn->isolation_level > 0 lobject_close(self);
&& self->conn->mark == self->mark) {
if (self->fd != -1)
lo_close(self->conn->pgconn, self->fd);
}
Py_XDECREF((PyObject*)self->conn); Py_XDECREF((PyObject*)self->conn);
Dprintf("lobject_dealloc: deleted lobject object at %p, refcnt = %d", Dprintf("lobject_dealloc: deleted lobject object at %p, refcnt = %d",