From 5ecd4e3f3035d831a8c7581401651911234c2d92 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Fri, 9 May 2003 15:44:42 +0000 Subject: Binary send/receive routines for a few basic datatypes --- enough for testing purposes. --- src/backend/utils/adt/int.c | 92 +++++++++++++++++++++++++++++++- src/backend/utils/adt/int8.c | 44 +++++++++------ src/backend/utils/adt/oid.c | 69 ++++++++++++++++++++++-- src/backend/utils/adt/varlena.c | 115 ++++++++++++++++++++++++++++++++++++---- 4 files changed, 288 insertions(+), 32 deletions(-) (limited to 'src/backend/utils/adt') diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index 73687d55ec..141312d667 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -8,14 +8,16 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/adt/int.c,v 1.53 2003/03/11 21:01:33 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/int.c,v 1.54 2003/05/09 15:44:40 tgl Exp $ * *------------------------------------------------------------------------- */ /* * OLD COMMENTS * I/O routines: - * int2in, int2out, int2vectorin, int2vectorout, int4in, int4out + * int2in, int2out, int2recv, int2send + * int4in, int4out, int4recv, int4send + * int2vectorin, int2vectorout, int2vectorrecv, int2vectorsend * Conversion routines: * itoi, int2_text, int4_text * Boolean operators: @@ -32,6 +34,7 @@ #include #include +#include "libpq/pqformat.h" #include "utils/builtins.h" #ifndef SHRT_MAX @@ -69,6 +72,31 @@ int2out(PG_FUNCTION_ARGS) PG_RETURN_CSTRING(result); } +/* + * int2recv - converts external binary format to int2 + */ +Datum +int2recv(PG_FUNCTION_ARGS) +{ + StringInfo buf = (StringInfo) PG_GETARG_POINTER(0); + + PG_RETURN_INT16((int16) pq_getmsgint(buf, sizeof(int16))); +} + +/* + * int2send - converts int2 to binary format + */ +Datum +int2send(PG_FUNCTION_ARGS) +{ + int16 arg1 = PG_GETARG_INT16(0); + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint(&buf, arg1, sizeof(int16)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); +} + /* * int2vectorin - converts "num num ..." to internal form * @@ -131,6 +159,41 @@ int2vectorout(PG_FUNCTION_ARGS) PG_RETURN_CSTRING(result); } +/* + * int2vectorrecv - converts external binary format to int2vector + */ +Datum +int2vectorrecv(PG_FUNCTION_ARGS) +{ + StringInfo buf = (StringInfo) PG_GETARG_POINTER(0); + int16 *result = (int16 *) palloc(sizeof(int16[INDEX_MAX_KEYS])); + int slot; + + for (slot = 0; slot < INDEX_MAX_KEYS; slot++) + { + result[slot] = (int16) pq_getmsgint(buf, sizeof(int16)); + } + PG_RETURN_POINTER(result); +} + +/* + * int2vectorsend - converts int2vector to binary format + */ +Datum +int2vectorsend(PG_FUNCTION_ARGS) +{ + int16 *int2Array = (int16 *) PG_GETARG_POINTER(0); + StringInfoData buf; + int slot; + + pq_begintypsend(&buf); + for (slot = 0; slot < INDEX_MAX_KEYS; slot++) + { + pq_sendint(&buf, int2Array[slot], sizeof(int16)); + } + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); +} + /* * We don't have a complete set of int2vector support routines, * but we need int2vectoreq for catcache indexing. @@ -173,6 +236,31 @@ int4out(PG_FUNCTION_ARGS) PG_RETURN_CSTRING(result); } +/* + * int4recv - converts external binary format to int4 + */ +Datum +int4recv(PG_FUNCTION_ARGS) +{ + StringInfo buf = (StringInfo) PG_GETARG_POINTER(0); + + PG_RETURN_INT32((int32) pq_getmsgint(buf, sizeof(int32))); +} + +/* + * int4send - converts int4 to binary format + */ +Datum +int4send(PG_FUNCTION_ARGS) +{ + int32 arg1 = PG_GETARG_INT32(0); + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint(&buf, arg1, sizeof(int32)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); +} + /* * =================== diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index cf16456105..30ec836cd6 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -7,36 +7,21 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/adt/int8.c,v 1.43 2003/03/11 21:01:33 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/int8.c,v 1.44 2003/05/09 15:44:40 tgl Exp $ * *------------------------------------------------------------------------- */ #include "postgres.h" #include -#include #include -#include -#include +#include "libpq/pqformat.h" #include "utils/int8.h" #define MAXINT8LEN 25 -#ifndef INT_MAX -#define INT_MAX (0x7FFFFFFFL) -#endif -#ifndef INT_MIN -#define INT_MIN (-INT_MAX-1) -#endif -#ifndef SHRT_MAX -#define SHRT_MAX (0x7FFF) -#endif -#ifndef SHRT_MIN -#define SHRT_MIN (-SHRT_MAX-1) -#endif - /*********************************************************************** ** @@ -160,6 +145,31 @@ int8out(PG_FUNCTION_ARGS) PG_RETURN_CSTRING(result); } +/* + * int8recv - converts external binary format to int8 + */ +Datum +int8recv(PG_FUNCTION_ARGS) +{ + StringInfo buf = (StringInfo) PG_GETARG_POINTER(0); + + PG_RETURN_INT64(pq_getmsgint64(buf)); +} + +/* + * int8send - converts int8 to binary format + */ +Datum +int8send(PG_FUNCTION_ARGS) +{ + int64 arg1 = PG_GETARG_INT64(0); + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint64(&buf, arg1); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); +} + /*---------------------------------------------------------- * Relational operators for int8s, including cross-data-type comparisons. diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c index 7404fc5b7b..1a0a89605b 100644 --- a/src/backend/utils/adt/oid.c +++ b/src/backend/utils/adt/oid.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/adt/oid.c,v 1.47 2002/06/20 20:29:38 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/oid.c,v 1.48 2003/05/09 15:44:40 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -18,8 +18,10 @@ #include #include +#include "libpq/pqformat.h" #include "utils/builtins.h" + /***************************************************************************** * USER I/O ROUTINES * *****************************************************************************/ @@ -108,6 +110,31 @@ oidout(PG_FUNCTION_ARGS) PG_RETURN_CSTRING(result); } +/* + * oidrecv - converts external binary format to oid + */ +Datum +oidrecv(PG_FUNCTION_ARGS) +{ + StringInfo buf = (StringInfo) PG_GETARG_POINTER(0); + + PG_RETURN_OID((Oid) pq_getmsgint(buf, sizeof(Oid))); +} + +/* + * oidsend - converts oid to binary format + */ +Datum +oidsend(PG_FUNCTION_ARGS) +{ + Oid arg1 = PG_GETARG_OID(0); + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendint(&buf, arg1, sizeof(Oid)); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); +} + /* * oidvectorin - converts "num num ..." to internal form @@ -119,11 +146,9 @@ Datum oidvectorin(PG_FUNCTION_ARGS) { char *oidString = PG_GETARG_CSTRING(0); - Oid *result; + Oid *result = (Oid *) palloc(sizeof(Oid[INDEX_MAX_KEYS])); int slot; - result = (Oid *) palloc(sizeof(Oid[INDEX_MAX_KEYS])); - for (slot = 0; slot < INDEX_MAX_KEYS; slot++) { while (*oidString && isspace((unsigned char) *oidString)) @@ -173,6 +198,42 @@ oidvectorout(PG_FUNCTION_ARGS) PG_RETURN_CSTRING(result); } +/* + * oidvectorrecv - converts external binary format to oidvector + */ +Datum +oidvectorrecv(PG_FUNCTION_ARGS) +{ + StringInfo buf = (StringInfo) PG_GETARG_POINTER(0); + Oid *result = (Oid *) palloc(sizeof(Oid[INDEX_MAX_KEYS])); + int slot; + + for (slot = 0; slot < INDEX_MAX_KEYS; slot++) + { + result[slot] = (Oid) pq_getmsgint(buf, sizeof(Oid)); + } + PG_RETURN_POINTER(result); +} + +/* + * oidvectorsend - converts oidvector to binary format + */ +Datum +oidvectorsend(PG_FUNCTION_ARGS) +{ + Oid *oidArray = (Oid *) PG_GETARG_POINTER(0); + StringInfoData buf; + int slot; + + pq_begintypsend(&buf); + for (slot = 0; slot < INDEX_MAX_KEYS; slot++) + { + pq_sendint(&buf, oidArray[slot], sizeof(Oid)); + } + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); +} + + /***************************************************************************** * PUBLIC ROUTINES * *****************************************************************************/ diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 7f928ec8ac..2a5f97ff02 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.96 2003/04/24 21:16:43 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.97 2003/05/09 15:44:40 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -20,16 +20,20 @@ #include "miscadmin.h" #include "access/tuptoaster.h" #include "lib/stringinfo.h" +#include "libpq/crypt.h" +#include "libpq/pqformat.h" #include "utils/builtins.h" #include "utils/pg_locale.h" -extern bool md5_hash(const void *buff, size_t len, char *hexsum); typedef struct varlena unknown; #define DatumGetUnknownP(X) ((unknown *) PG_DETOAST_DATUM(X)) +#define DatumGetUnknownPCopy(X) ((unknown *) PG_DETOAST_DATUM_COPY(X)) #define PG_GETARG_UNKNOWN_P(n) DatumGetUnknownP(PG_GETARG_DATUM(n)) +#define PG_GETARG_UNKNOWN_P_COPY(n) DatumGetUnknownPCopy(PG_GETARG_DATUM(n)) #define PG_RETURN_UNKNOWN_P(x) PG_RETURN_POINTER(x) + #define PG_TEXTARG_GET_STR(arg_) \ DatumGetCString(DirectFunctionCall1(textout, PG_GETARG_DATUM(arg_))) #define PG_TEXT_GET_STR(textp_) \ @@ -111,10 +115,10 @@ byteain(PG_FUNCTION_ARGS) byte += VARHDRSZ; result = (bytea *) palloc(byte); - result->vl_len = byte; /* set varlena length */ + VARATT_SIZEP(result) = byte; /* set varlena length */ tp = inputText; - rp = result->vl_dat; + rp = VARDATA(result); while (*tp != '\0') { if (tp[0] != '\\') @@ -170,8 +174,8 @@ byteaout(PG_FUNCTION_ARGS) int len; len = 1; /* empty string has 1 char */ - vp = vlena->vl_dat; - for (i = vlena->vl_len - VARHDRSZ; i != 0; i--, vp++) + vp = VARDATA(vlena); + for (i = VARSIZE(vlena) - VARHDRSZ; i != 0; i--, vp++) { if (*vp == '\\') len += 2; @@ -181,8 +185,8 @@ byteaout(PG_FUNCTION_ARGS) len += 4; } rp = result = (char *) palloc(len); - vp = vlena->vl_dat; - for (i = vlena->vl_len - VARHDRSZ; i != 0; i--, vp++) + vp = VARDATA(vlena); + for (i = VARSIZE(vlena) - VARHDRSZ; i != 0; i--, vp++) { if (*vp == '\\') { @@ -207,6 +211,36 @@ byteaout(PG_FUNCTION_ARGS) PG_RETURN_CSTRING(result); } +/* + * bytearecv - converts external binary format to bytea + */ +Datum +bytearecv(PG_FUNCTION_ARGS) +{ + StringInfo buf = (StringInfo) PG_GETARG_POINTER(0); + bytea *result; + int nbytes; + + nbytes = buf->len - buf->cursor; + result = (bytea *) palloc(nbytes + VARHDRSZ); + VARATT_SIZEP(result) = nbytes + VARHDRSZ; + pq_copymsgbytes(buf, VARDATA(result), nbytes); + PG_RETURN_BYTEA_P(result); +} + +/* + * byteasend - converts bytea to binary format + * + * This is a special case: just copy the input... + */ +Datum +byteasend(PG_FUNCTION_ARGS) +{ + bytea *vlena = PG_GETARG_BYTEA_P_COPY(0); + + PG_RETURN_BYTEA_P(vlena); +} + /* * textin - converts "..." to internal representation @@ -259,6 +293,39 @@ textout(PG_FUNCTION_ARGS) PG_RETURN_CSTRING(result); } +/* + * textrecv - converts external binary format to text + */ +Datum +textrecv(PG_FUNCTION_ARGS) +{ + StringInfo buf = (StringInfo) PG_GETARG_POINTER(0); + text *result; + char *str; + int nbytes; + + str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes); + result = (text *) palloc(nbytes + VARHDRSZ); + VARATT_SIZEP(result) = nbytes + VARHDRSZ; + memcpy(VARDATA(result), str, nbytes); + pfree(str); + PG_RETURN_TEXT_P(result); +} + +/* + * textsend - converts text to binary format + */ +Datum +textsend(PG_FUNCTION_ARGS) +{ + text *t = PG_GETARG_TEXT_P(0); + StringInfoData buf; + + pq_begintypsend(&buf); + pq_sendtext(&buf, VARDATA(t), VARSIZE(t) - VARHDRSZ); + PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); +} + /* * unknownin - converts "..." to internal representation @@ -280,7 +347,6 @@ unknownin(PG_FUNCTION_ARGS) PG_RETURN_UNKNOWN_P(result); } - /* * unknownout - converts internal representation to "..." */ @@ -299,6 +365,37 @@ unknownout(PG_FUNCTION_ARGS) PG_RETURN_CSTRING(result); } +/* + * unknownrecv - converts external binary format to unknown + */ +Datum +unknownrecv(PG_FUNCTION_ARGS) +{ + StringInfo buf = (StringInfo) PG_GETARG_POINTER(0); + unknown *result; + int nbytes; + + nbytes = buf->len - buf->cursor; + result = (unknown *) palloc(nbytes + VARHDRSZ); + VARATT_SIZEP(result) = nbytes + VARHDRSZ; + pq_copymsgbytes(buf, VARDATA(result), nbytes); + PG_RETURN_UNKNOWN_P(result); +} + +/* + * unknownsend - converts unknown to binary format + * + * This is a special case: just copy the input, since it's + * effectively the same format as bytea + */ +Datum +unknownsend(PG_FUNCTION_ARGS) +{ + unknown *vlena = PG_GETARG_UNKNOWN_P_COPY(0); + + PG_RETURN_UNKNOWN_P(vlena); +} + /* ========== PUBLIC ROUTINES ========== */ -- cgit v1.2.1