Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1265a9c

Browse files
committed
Add binary I/O capability for cube datatype.
We can adjust the not-yet-released cube--1.4--1.5.sql upgrade rather than making a whole new version. KaiGai Kohei Discussion: https://postgr.es/m/CAOP8fzZO4y60QPTK=RGDXeVeVHV9tLHKOsh7voUOoUouVCPV8A@mail.gmail.com
1 parent e045565 commit 1265a9c

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

contrib/cube/cube--1.4--1.5.sql

+13
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,16 @@
66
-- Remove @ and ~
77
DROP OPERATOR @ (cube, cube);
88
DROP OPERATOR ~ (cube, cube);
9+
10+
-- Add binary input/output handlers
11+
CREATE FUNCTION cube_recv(internal)
12+
RETURNS cube
13+
AS 'MODULE_PATHNAME'
14+
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
15+
16+
CREATE FUNCTION cube_send(cube)
17+
RETURNS bytea
18+
AS 'MODULE_PATHNAME'
19+
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
20+
21+
ALTER TYPE cube SET ( RECEIVE = cube_recv, SEND = cube_send );

contrib/cube/cube.c

+56
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "access/gist.h"
1414
#include "access/stratnum.h"
1515
#include "cubedata.h"
16+
#include "libpq/pqformat.h"
1617
#include "utils/array.h"
1718
#include "utils/float.h"
1819

@@ -31,6 +32,8 @@ PG_FUNCTION_INFO_V1(cube_in);
3132
PG_FUNCTION_INFO_V1(cube_a_f8_f8);
3233
PG_FUNCTION_INFO_V1(cube_a_f8);
3334
PG_FUNCTION_INFO_V1(cube_out);
35+
PG_FUNCTION_INFO_V1(cube_send);
36+
PG_FUNCTION_INFO_V1(cube_recv);
3437
PG_FUNCTION_INFO_V1(cube_f8);
3538
PG_FUNCTION_INFO_V1(cube_f8_f8);
3639
PG_FUNCTION_INFO_V1(cube_c_f8);
@@ -319,6 +322,59 @@ cube_out(PG_FUNCTION_ARGS)
319322
PG_RETURN_CSTRING(buf.data);
320323
}
321324

325+
/*
326+
* cube_send - a binary output handler for cube type
327+
*/
328+
Datum
329+
cube_send(PG_FUNCTION_ARGS)
330+
{
331+
NDBOX *cube = PG_GETARG_NDBOX_P(0);
332+
StringInfoData buf;
333+
int32 i,
334+
nitems = DIM(cube);
335+
336+
pq_begintypsend(&buf);
337+
pq_sendint32(&buf, cube->header);
338+
if (!IS_POINT(cube))
339+
nitems += nitems;
340+
/* for symmetry with cube_recv, we don't use LL_COORD/UR_COORD here */
341+
for (i = 0; i < nitems; i++)
342+
pq_sendfloat8(&buf, cube->x[i]);
343+
344+
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
345+
}
346+
347+
/*
348+
* cube_recv - a binary input handler for cube type
349+
*/
350+
Datum
351+
cube_recv(PG_FUNCTION_ARGS)
352+
{
353+
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
354+
int32 header;
355+
int32 i,
356+
nitems;
357+
NDBOX *cube;
358+
359+
header = pq_getmsgint(buf, sizeof(int32));
360+
nitems = (header & DIM_MASK);
361+
if (nitems > CUBE_MAX_DIM)
362+
ereport(ERROR,
363+
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
364+
errmsg("cube dimension is too large"),
365+
errdetail("A cube cannot have more than %d dimensions.",
366+
CUBE_MAX_DIM)));
367+
if ((header & POINT_BIT) == 0)
368+
nitems += nitems;
369+
cube = palloc(offsetof(NDBOX, x) + sizeof(double) * nitems);
370+
SET_VARSIZE(cube, offsetof(NDBOX, x) + sizeof(double) * nitems);
371+
cube->header = header;
372+
for (i = 0; i < nitems; i++)
373+
cube->x[i] = pq_getmsgfloat8(buf);
374+
375+
PG_RETURN_NDBOX_P(cube);
376+
}
377+
322378

323379
/*****************************************************************************
324380
* GiST functions

0 commit comments

Comments
 (0)