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);
3132PG_FUNCTION_INFO_V1 (cube_a_f8_f8 );
3233PG_FUNCTION_INFO_V1 (cube_a_f8 );
3334PG_FUNCTION_INFO_V1 (cube_out );
35+ PG_FUNCTION_INFO_V1 (cube_send );
36+ PG_FUNCTION_INFO_V1 (cube_recv );
3437PG_FUNCTION_INFO_V1 (cube_f8 );
3538PG_FUNCTION_INFO_V1 (cube_f8_f8 );
3639PG_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