2001-06-01 02:27:18 +08:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* rtree_gist.c
|
2001-10-25 13:50:21 +08:00
|
|
|
* pg_amproc entries for GiSTs over 2-D boxes.
|
2001-06-01 02:27:18 +08:00
|
|
|
* This gives R-tree behavior, with Guttman's poly-time split algorithm.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2003-07-28 01:10:07 +08:00
|
|
|
* $Header: /cvsroot/pgsql/contrib/rtree_gist/Attic/rtree_gist.c,v 1.7 2003/07/27 17:10:06 tgl Exp $
|
2001-06-01 02:27:18 +08:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/gist.h"
|
|
|
|
#include "access/itup.h"
|
|
|
|
#include "access/rtree.h"
|
|
|
|
#include "utils/geo_decls.h"
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
typedef Datum (*RDF) (PG_FUNCTION_ARGS);
|
|
|
|
typedef Datum (*BINARY_UNION) (Datum, Datum, int *);
|
|
|
|
typedef float (*SIZE_BOX) (Datum);
|
2001-06-01 02:27:18 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
** box ops
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(gbox_compress);
|
|
|
|
PG_FUNCTION_INFO_V1(gbox_union);
|
|
|
|
PG_FUNCTION_INFO_V1(gbox_picksplit);
|
|
|
|
PG_FUNCTION_INFO_V1(gbox_consistent);
|
|
|
|
PG_FUNCTION_INFO_V1(gbox_penalty);
|
|
|
|
PG_FUNCTION_INFO_V1(gbox_same);
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
Datum gbox_compress(PG_FUNCTION_ARGS);
|
|
|
|
Datum gbox_union(PG_FUNCTION_ARGS);
|
|
|
|
Datum gbox_picksplit(PG_FUNCTION_ARGS);
|
|
|
|
Datum gbox_consistent(PG_FUNCTION_ARGS);
|
|
|
|
Datum gbox_penalty(PG_FUNCTION_ARGS);
|
|
|
|
Datum gbox_same(PG_FUNCTION_ARGS);
|
2001-06-01 02:27:18 +08:00
|
|
|
|
|
|
|
static bool gbox_leaf_consistent(BOX *key, BOX *query, StrategyNumber strategy);
|
2001-10-25 13:50:21 +08:00
|
|
|
static float size_box(Datum box);
|
2001-06-01 02:27:18 +08:00
|
|
|
|
|
|
|
/*
|
2001-10-25 13:50:21 +08:00
|
|
|
** Polygon ops
|
2001-06-01 02:27:18 +08:00
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(gpoly_compress);
|
|
|
|
PG_FUNCTION_INFO_V1(gpoly_consistent);
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
Datum gpoly_compress(PG_FUNCTION_ARGS);
|
|
|
|
Datum gpoly_consistent(PG_FUNCTION_ARGS);
|
2001-06-01 02:27:18 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Common rtree-function (for all ops)
|
|
|
|
*/
|
|
|
|
static bool rtree_internal_consistent(BOX *key, BOX *query, StrategyNumber strategy);
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(rtree_decompress);
|
2001-08-23 02:24:26 +08:00
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
Datum rtree_decompress(PG_FUNCTION_ARGS);
|
2001-06-01 02:27:18 +08:00
|
|
|
|
|
|
|
/**************************************************
|
|
|
|
* Box ops
|
|
|
|
**************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The GiST Consistent method for boxes
|
|
|
|
** Should return false if for all data items x below entry,
|
|
|
|
** the predicate x op query == FALSE, where op is the oper
|
|
|
|
** corresponding to strategy in the pg_amop table.
|
|
|
|
*/
|
2001-08-23 02:24:26 +08:00
|
|
|
Datum
|
2001-06-01 02:27:18 +08:00
|
|
|
gbox_consistent(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 13:50:21 +08:00
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
|
|
|
BOX *query = (BOX *) PG_GETARG_POINTER(1);
|
|
|
|
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* * if entry is not leaf, use gbox_internal_consistent, * else use
|
|
|
|
* gbox_leaf_consistent
|
|
|
|
*/
|
|
|
|
if (!(DatumGetPointer(entry->key) != NULL && query))
|
2001-08-23 02:24:26 +08:00
|
|
|
PG_RETURN_BOOL(FALSE);
|
2001-06-01 02:27:18 +08:00
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
if (GIST_LEAF(entry))
|
|
|
|
PG_RETURN_BOOL(gbox_leaf_consistent((BOX *) DatumGetPointer(entry->key), query, strategy));
|
|
|
|
else
|
|
|
|
PG_RETURN_BOOL(rtree_internal_consistent((BOX *) DatumGetPointer(entry->key), query, strategy));
|
2001-06-01 02:27:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The GiST Union method for boxes
|
|
|
|
** returns the minimal bounding box that encloses all the entries in entryvec
|
|
|
|
*/
|
2001-08-23 02:24:26 +08:00
|
|
|
Datum
|
2001-06-01 02:27:18 +08:00
|
|
|
gbox_union(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 13:50:21 +08:00
|
|
|
bytea *entryvec = (bytea *) PG_GETARG_POINTER(0);
|
|
|
|
int *sizep = (int *) PG_GETARG_POINTER(1);
|
|
|
|
int numranges,
|
|
|
|
i;
|
|
|
|
BOX *cur,
|
|
|
|
*pageunion;
|
|
|
|
|
|
|
|
numranges = (VARSIZE(entryvec) - VARHDRSZ) / sizeof(GISTENTRY);
|
|
|
|
pageunion = (BOX *) palloc(sizeof(BOX));
|
|
|
|
cur = DatumGetBoxP(((GISTENTRY *) VARDATA(entryvec))[0].key);
|
|
|
|
memcpy((void *) pageunion, (void *) cur, sizeof(BOX));
|
|
|
|
|
|
|
|
for (i = 1; i < numranges; i++)
|
|
|
|
{
|
|
|
|
cur = DatumGetBoxP(((GISTENTRY *) VARDATA(entryvec))[i].key);
|
|
|
|
if (pageunion->high.x < cur->high.x)
|
|
|
|
pageunion->high.x = cur->high.x;
|
|
|
|
if (pageunion->low.x > cur->low.x)
|
|
|
|
pageunion->low.x = cur->low.x;
|
|
|
|
if (pageunion->high.y < cur->high.y)
|
|
|
|
pageunion->high.y = cur->high.y;
|
|
|
|
if (pageunion->low.y > cur->low.y)
|
|
|
|
pageunion->low.y = cur->low.y;
|
|
|
|
}
|
|
|
|
*sizep = sizeof(BOX);
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(pageunion);
|
2001-06-01 02:27:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** GiST Compress methods for boxes
|
|
|
|
** do not do anything.
|
|
|
|
*/
|
2001-08-23 02:24:26 +08:00
|
|
|
Datum
|
2001-06-01 02:27:18 +08:00
|
|
|
gbox_compress(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-08-23 02:24:26 +08:00
|
|
|
PG_RETURN_POINTER(PG_GETARG_POINTER(0));
|
2001-06-01 02:27:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The GiST Penalty method for boxes
|
|
|
|
** As in the R-tree paper, we use change in area as our penalty metric
|
|
|
|
*/
|
2001-08-23 02:24:26 +08:00
|
|
|
Datum
|
2001-06-01 02:27:18 +08:00
|
|
|
gbox_penalty(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 13:50:21 +08:00
|
|
|
GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
|
|
|
GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
|
|
|
|
float *result = (float *) PG_GETARG_POINTER(2);
|
|
|
|
Datum ud;
|
|
|
|
float tmp1;
|
|
|
|
|
|
|
|
ud = DirectFunctionCall2(rt_box_union, origentry->key, newentry->key);
|
|
|
|
tmp1 = size_box(ud);
|
|
|
|
if (DatumGetPointer(ud) != NULL)
|
|
|
|
pfree(DatumGetPointer(ud));
|
|
|
|
|
|
|
|
*result = tmp1 - size_box(origentry->key);
|
|
|
|
PG_RETURN_POINTER(result);
|
2001-08-23 02:24:26 +08:00
|
|
|
}
|
2001-06-01 02:27:18 +08:00
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
BOX *key;
|
|
|
|
int pos;
|
|
|
|
} KBsort;
|
2002-05-28 23:24:53 +08:00
|
|
|
|
|
|
|
static int
|
2002-09-05 04:31:48 +08:00
|
|
|
compare_KB(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
BOX *abox = ((KBsort *) a)->key;
|
|
|
|
BOX *bbox = ((KBsort *) b)->key;
|
|
|
|
float sa = (abox->high.x - abox->low.x) * (abox->high.y - abox->low.y);
|
|
|
|
float sb = (bbox->high.x - bbox->low.x) * (bbox->high.y - bbox->low.y);
|
|
|
|
|
|
|
|
if (sa == sb)
|
|
|
|
return 0;
|
|
|
|
return (sa > sb) ? 1 : -1;
|
2002-05-28 23:24:53 +08:00
|
|
|
}
|
|
|
|
|
2001-06-01 02:27:18 +08:00
|
|
|
/*
|
2001-08-23 02:24:26 +08:00
|
|
|
** The GiST PickSplit method
|
|
|
|
** New linear algorithm, see 'New Linear Node Splitting Algorithm for R-tree',
|
|
|
|
** C.H.Ang and T.C.Tan
|
2001-06-01 02:27:18 +08:00
|
|
|
*/
|
2001-08-23 02:24:26 +08:00
|
|
|
Datum
|
2001-06-01 02:27:18 +08:00
|
|
|
gbox_picksplit(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 13:50:21 +08:00
|
|
|
bytea *entryvec = (bytea *) PG_GETARG_POINTER(0);
|
|
|
|
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
|
|
|
|
OffsetNumber i;
|
|
|
|
OffsetNumber *listL,
|
|
|
|
*listR,
|
|
|
|
*listB,
|
|
|
|
*listT;
|
|
|
|
BOX *unionL,
|
|
|
|
*unionR,
|
|
|
|
*unionB,
|
|
|
|
*unionT;
|
|
|
|
int posL,
|
|
|
|
posR,
|
|
|
|
posB,
|
|
|
|
posT;
|
|
|
|
BOX pageunion;
|
|
|
|
BOX *cur;
|
|
|
|
char direction = ' ';
|
|
|
|
bool allisequal = true;
|
|
|
|
OffsetNumber maxoff;
|
|
|
|
int nbytes;
|
|
|
|
|
|
|
|
posL = posR = posB = posT = 0;
|
|
|
|
maxoff = ((VARSIZE(entryvec) - VARHDRSZ) / sizeof(GISTENTRY)) - 1;
|
|
|
|
|
|
|
|
cur = DatumGetBoxP(((GISTENTRY *) VARDATA(entryvec))[FirstOffsetNumber].key);
|
|
|
|
memcpy((void *) &pageunion, (void *) cur, sizeof(BOX));
|
|
|
|
|
|
|
|
/* find MBR */
|
|
|
|
for (i = OffsetNumberNext(FirstOffsetNumber); i <= maxoff; i = OffsetNumberNext(i))
|
|
|
|
{
|
|
|
|
cur = DatumGetBoxP(((GISTENTRY *) VARDATA(entryvec))[i].key);
|
2002-09-05 04:31:48 +08:00
|
|
|
if (allisequal == true && (
|
|
|
|
pageunion.high.x != cur->high.x ||
|
|
|
|
pageunion.high.y != cur->high.y ||
|
|
|
|
pageunion.low.x != cur->low.x ||
|
|
|
|
pageunion.low.y != cur->low.y
|
|
|
|
))
|
2001-10-25 13:50:21 +08:00
|
|
|
allisequal = false;
|
2002-09-05 04:31:48 +08:00
|
|
|
|
2002-05-28 23:24:53 +08:00
|
|
|
if (pageunion.high.x < cur->high.x)
|
2001-10-25 13:50:21 +08:00
|
|
|
pageunion.high.x = cur->high.x;
|
|
|
|
if (pageunion.low.x > cur->low.x)
|
|
|
|
pageunion.low.x = cur->low.x;
|
|
|
|
if (pageunion.high.y < cur->high.y)
|
|
|
|
pageunion.high.y = cur->high.y;
|
|
|
|
if (pageunion.low.y > cur->low.y)
|
|
|
|
pageunion.low.y = cur->low.y;
|
|
|
|
}
|
2001-08-23 02:24:26 +08:00
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
nbytes = (maxoff + 2) * sizeof(OffsetNumber);
|
|
|
|
listL = (OffsetNumber *) palloc(nbytes);
|
|
|
|
listR = (OffsetNumber *) palloc(nbytes);
|
|
|
|
unionL = (BOX *) palloc(sizeof(BOX));
|
|
|
|
unionR = (BOX *) palloc(sizeof(BOX));
|
|
|
|
if (allisequal)
|
|
|
|
{
|
|
|
|
cur = DatumGetBoxP(((GISTENTRY *) VARDATA(entryvec))[OffsetNumberNext(FirstOffsetNumber)].key);
|
|
|
|
if (memcmp((void *) cur, (void *) &pageunion, sizeof(BOX)) == 0)
|
|
|
|
{
|
|
|
|
v->spl_left = listL;
|
|
|
|
v->spl_right = listR;
|
|
|
|
v->spl_nleft = v->spl_nright = 0;
|
|
|
|
memcpy((void *) unionL, (void *) &pageunion, sizeof(BOX));
|
|
|
|
memcpy((void *) unionR, (void *) &pageunion, sizeof(BOX));
|
|
|
|
|
|
|
|
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
|
|
|
|
{
|
|
|
|
if (i <= (maxoff - FirstOffsetNumber + 1) / 2)
|
|
|
|
{
|
|
|
|
v->spl_left[v->spl_nleft] = i;
|
|
|
|
v->spl_nleft++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
v->spl_right[v->spl_nright] = i;
|
|
|
|
v->spl_nright++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v->spl_ldatum = BoxPGetDatum(unionL);
|
|
|
|
v->spl_rdatum = BoxPGetDatum(unionR);
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(v);
|
|
|
|
}
|
2001-08-23 02:24:26 +08:00
|
|
|
}
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
listB = (OffsetNumber *) palloc(nbytes);
|
|
|
|
listT = (OffsetNumber *) palloc(nbytes);
|
|
|
|
unionB = (BOX *) palloc(sizeof(BOX));
|
|
|
|
unionT = (BOX *) palloc(sizeof(BOX));
|
2001-08-23 02:24:26 +08:00
|
|
|
|
2002-05-28 23:24:53 +08:00
|
|
|
#define ADDLIST( list, unionD, pos, num ) do { \
|
2001-08-23 02:24:26 +08:00
|
|
|
if ( pos ) { \
|
2001-10-25 13:50:21 +08:00
|
|
|
if ( unionD->high.x < cur->high.x ) unionD->high.x = cur->high.x; \
|
|
|
|
if ( unionD->low.x > cur->low.x ) unionD->low.x = cur->low.x; \
|
|
|
|
if ( unionD->high.y < cur->high.y ) unionD->high.y = cur->high.y; \
|
|
|
|
if ( unionD->low.y > cur->low.y ) unionD->low.y = cur->low.y; \
|
2001-08-23 02:24:26 +08:00
|
|
|
} else { \
|
2001-10-25 13:50:21 +08:00
|
|
|
memcpy( (void*)unionD, (void*) cur, sizeof( BOX ) ); \
|
2001-08-23 02:24:26 +08:00
|
|
|
} \
|
2002-05-28 23:24:53 +08:00
|
|
|
list[pos] = num; \
|
2001-08-23 02:24:26 +08:00
|
|
|
(pos)++; \
|
|
|
|
} while(0)
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
|
|
|
|
{
|
|
|
|
cur = DatumGetBoxP(((GISTENTRY *) VARDATA(entryvec))[i].key);
|
|
|
|
if (cur->low.x - pageunion.low.x < pageunion.high.x - cur->high.x)
|
2002-09-05 04:31:48 +08:00
|
|
|
ADDLIST(listL, unionL, posL, i);
|
2001-10-25 13:50:21 +08:00
|
|
|
else
|
2002-09-05 04:31:48 +08:00
|
|
|
ADDLIST(listR, unionR, posR, i);
|
2001-10-25 13:50:21 +08:00
|
|
|
if (cur->low.y - pageunion.low.y < pageunion.high.y - cur->high.y)
|
2002-09-05 04:31:48 +08:00
|
|
|
ADDLIST(listB, unionB, posB, i);
|
2001-10-25 13:50:21 +08:00
|
|
|
else
|
2002-09-05 04:31:48 +08:00
|
|
|
ADDLIST(listT, unionT, posT, i);
|
2001-10-25 13:50:21 +08:00
|
|
|
}
|
|
|
|
|
2002-05-28 23:24:53 +08:00
|
|
|
/* bad disposition, sort by ascending and resplit */
|
2002-09-05 04:31:48 +08:00
|
|
|
if ((posR == 0 || posL == 0) && (posT == 0 || posB == 0))
|
|
|
|
{
|
|
|
|
KBsort *arr = (KBsort *) palloc(sizeof(KBsort) * maxoff);
|
|
|
|
|
2002-05-28 23:24:53 +08:00
|
|
|
posL = posR = posB = posT = 0;
|
2002-09-05 04:31:48 +08:00
|
|
|
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
|
|
|
|
{
|
|
|
|
arr[i - 1].key = DatumGetBoxP(((GISTENTRY *) VARDATA(entryvec))[i].key);
|
|
|
|
arr[i - 1].pos = i;
|
2002-05-28 23:24:53 +08:00
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
qsort(arr, maxoff, sizeof(KBsort), compare_KB);
|
|
|
|
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
|
|
|
|
{
|
|
|
|
cur = arr[i - 1].key;
|
2002-05-28 23:24:53 +08:00
|
|
|
if (cur->low.x - pageunion.low.x < pageunion.high.x - cur->high.x)
|
2002-09-05 04:31:48 +08:00
|
|
|
ADDLIST(listL, unionL, posL, arr[i - 1].pos);
|
|
|
|
else if (cur->low.x - pageunion.low.x == pageunion.high.x - cur->high.x)
|
|
|
|
{
|
|
|
|
if (posL > posR)
|
|
|
|
ADDLIST(listR, unionR, posR, arr[i - 1].pos);
|
2002-05-28 23:24:53 +08:00
|
|
|
else
|
2002-09-05 04:31:48 +08:00
|
|
|
ADDLIST(listL, unionL, posL, arr[i - 1].pos);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ADDLIST(listR, unionR, posR, arr[i - 1].pos);
|
2002-05-28 23:24:53 +08:00
|
|
|
|
|
|
|
if (cur->low.y - pageunion.low.y < pageunion.high.y - cur->high.y)
|
2002-09-05 04:31:48 +08:00
|
|
|
ADDLIST(listB, unionB, posB, arr[i - 1].pos);
|
|
|
|
else if (cur->low.y - pageunion.low.y == pageunion.high.y - cur->high.y)
|
|
|
|
{
|
|
|
|
if (posB > posT)
|
|
|
|
ADDLIST(listT, unionT, posT, arr[i - 1].pos);
|
2002-05-28 23:24:53 +08:00
|
|
|
else
|
2002-09-05 04:31:48 +08:00
|
|
|
ADDLIST(listB, unionB, posB, arr[i - 1].pos);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ADDLIST(listT, unionT, posT, arr[i - 1].pos);
|
2002-05-28 23:24:53 +08:00
|
|
|
}
|
|
|
|
pfree(arr);
|
|
|
|
}
|
2001-10-25 13:50:21 +08:00
|
|
|
|
2002-05-28 23:24:53 +08:00
|
|
|
/* which split more optimal? */
|
2001-10-25 13:50:21 +08:00
|
|
|
if (Max(posL, posR) < Max(posB, posT))
|
2001-08-23 02:24:26 +08:00
|
|
|
direction = 'x';
|
2001-10-25 13:50:21 +08:00
|
|
|
else if (Max(posL, posR) > Max(posB, posT))
|
2001-08-23 02:24:26 +08:00
|
|
|
direction = 'y';
|
2001-10-25 13:50:21 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Datum interLR = DirectFunctionCall2(rt_box_inter,
|
|
|
|
BoxPGetDatum(unionL),
|
|
|
|
BoxPGetDatum(unionR));
|
|
|
|
Datum interBT = DirectFunctionCall2(rt_box_inter,
|
|
|
|
BoxPGetDatum(unionB),
|
|
|
|
BoxPGetDatum(unionT));
|
|
|
|
float sizeLR,
|
|
|
|
sizeBT;
|
|
|
|
|
|
|
|
sizeLR = size_box(interLR);
|
|
|
|
sizeBT = size_box(interBT);
|
|
|
|
|
|
|
|
if (sizeLR < sizeBT)
|
|
|
|
direction = 'x';
|
|
|
|
else
|
|
|
|
direction = 'y';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (direction == 'x')
|
|
|
|
{
|
|
|
|
pfree(unionB);
|
|
|
|
pfree(listB);
|
|
|
|
pfree(unionT);
|
|
|
|
pfree(listT);
|
|
|
|
|
|
|
|
v->spl_left = listL;
|
|
|
|
v->spl_right = listR;
|
|
|
|
v->spl_nleft = posL;
|
|
|
|
v->spl_nright = posR;
|
|
|
|
v->spl_ldatum = BoxPGetDatum(unionL);
|
|
|
|
v->spl_rdatum = BoxPGetDatum(unionR);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pfree(unionR);
|
|
|
|
pfree(listR);
|
|
|
|
pfree(unionL);
|
|
|
|
pfree(listL);
|
|
|
|
|
|
|
|
v->spl_left = listB;
|
|
|
|
v->spl_right = listT;
|
|
|
|
v->spl_nleft = posB;
|
|
|
|
v->spl_nright = posT;
|
|
|
|
v->spl_ldatum = BoxPGetDatum(unionB);
|
|
|
|
v->spl_rdatum = BoxPGetDatum(unionT);
|
2001-10-02 01:53:12 +08:00
|
|
|
}
|
2001-08-23 02:24:26 +08:00
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
PG_RETURN_POINTER(v);
|
2001-06-01 02:27:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Equality method
|
|
|
|
*/
|
2001-08-23 02:24:26 +08:00
|
|
|
Datum
|
2001-06-01 02:27:18 +08:00
|
|
|
gbox_same(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 13:50:21 +08:00
|
|
|
BOX *b1 = (BOX *) PG_GETARG_POINTER(0);
|
|
|
|
BOX *b2 = (BOX *) PG_GETARG_POINTER(1);
|
|
|
|
bool *result = (bool *) PG_GETARG_POINTER(2);
|
|
|
|
|
|
|
|
if (b1 && b2)
|
|
|
|
*result = DatumGetBool(DirectFunctionCall2(box_same, PointerGetDatum(b1), PointerGetDatum(b2)));
|
|
|
|
else
|
|
|
|
*result = (b1 == NULL && b2 == NULL) ? TRUE : FALSE;
|
|
|
|
PG_RETURN_POINTER(result);
|
2001-06-01 02:27:18 +08:00
|
|
|
}
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
/*
|
2001-06-01 02:27:18 +08:00
|
|
|
** SUPPORT ROUTINES for boxes
|
|
|
|
*/
|
2001-10-25 13:50:21 +08:00
|
|
|
static bool
|
2001-06-01 02:27:18 +08:00
|
|
|
gbox_leaf_consistent(BOX *key,
|
2001-10-25 13:50:21 +08:00
|
|
|
BOX *query,
|
|
|
|
StrategyNumber strategy)
|
2001-06-01 02:27:18 +08:00
|
|
|
{
|
2001-10-25 13:50:21 +08:00
|
|
|
bool retval;
|
|
|
|
|
|
|
|
switch (strategy)
|
|
|
|
{
|
|
|
|
case RTLeftStrategyNumber:
|
|
|
|
retval = DatumGetBool(DirectFunctionCall2(box_left, PointerGetDatum(key), PointerGetDatum(query)));
|
|
|
|
break;
|
|
|
|
case RTOverLeftStrategyNumber:
|
|
|
|
retval = DatumGetBool(DirectFunctionCall2(box_overleft, PointerGetDatum(key), PointerGetDatum(query)));
|
|
|
|
break;
|
|
|
|
case RTOverlapStrategyNumber:
|
|
|
|
retval = DatumGetBool(DirectFunctionCall2(box_overlap, PointerGetDatum(key), PointerGetDatum(query)));
|
|
|
|
break;
|
|
|
|
case RTOverRightStrategyNumber:
|
|
|
|
retval = DatumGetBool(DirectFunctionCall2(box_overright, PointerGetDatum(key), PointerGetDatum(query)));
|
|
|
|
break;
|
|
|
|
case RTRightStrategyNumber:
|
|
|
|
retval = DatumGetBool(DirectFunctionCall2(box_right, PointerGetDatum(key), PointerGetDatum(query)));
|
|
|
|
break;
|
|
|
|
case RTSameStrategyNumber:
|
|
|
|
retval = DatumGetBool(DirectFunctionCall2(box_same, PointerGetDatum(key), PointerGetDatum(query)));
|
|
|
|
break;
|
|
|
|
case RTContainsStrategyNumber:
|
|
|
|
retval = DatumGetBool(DirectFunctionCall2(box_contain, PointerGetDatum(key), PointerGetDatum(query)));
|
|
|
|
break;
|
|
|
|
case RTContainedByStrategyNumber:
|
|
|
|
retval = DatumGetBool(DirectFunctionCall2(box_contained, PointerGetDatum(key), PointerGetDatum(query)));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
retval = FALSE;
|
|
|
|
}
|
|
|
|
return (retval);
|
2001-06-01 02:27:18 +08:00
|
|
|
}
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
static float
|
|
|
|
size_box(Datum box)
|
|
|
|
{
|
|
|
|
if (DatumGetPointer(box) != NULL)
|
|
|
|
{
|
|
|
|
float size;
|
2001-06-01 02:27:18 +08:00
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
DirectFunctionCall2(rt_box_size,
|
|
|
|
box, PointerGetDatum(&size));
|
2001-06-01 02:27:18 +08:00
|
|
|
return size;
|
2001-10-25 13:50:21 +08:00
|
|
|
}
|
|
|
|
else
|
2001-06-01 02:27:18 +08:00
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**************************************************
|
|
|
|
* Polygon ops
|
|
|
|
**************************************************/
|
|
|
|
|
2001-08-23 02:24:26 +08:00
|
|
|
Datum
|
2001-06-01 02:27:18 +08:00
|
|
|
gpoly_compress(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 13:50:21 +08:00
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
|
|
|
GISTENTRY *retval;
|
2001-06-01 02:27:18 +08:00
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
if (entry->leafkey)
|
|
|
|
{
|
2001-06-01 02:27:18 +08:00
|
|
|
retval = palloc(sizeof(GISTENTRY));
|
2001-10-25 13:50:21 +08:00
|
|
|
if (DatumGetPointer(entry->key) != NULL)
|
|
|
|
{
|
|
|
|
POLYGON *in;
|
|
|
|
BOX *r;
|
|
|
|
|
2001-06-01 02:27:18 +08:00
|
|
|
in = (POLYGON *) PG_DETOAST_DATUM(entry->key);
|
2001-10-25 13:50:21 +08:00
|
|
|
r = (BOX *) palloc(sizeof(BOX));
|
|
|
|
memcpy((void *) r, (void *) &(in->boundbox), sizeof(BOX));
|
|
|
|
if (in != (POLYGON *) DatumGetPointer(entry->key))
|
|
|
|
pfree(in);
|
2001-06-01 02:27:18 +08:00
|
|
|
|
|
|
|
gistentryinit(*retval, PointerGetDatum(r),
|
|
|
|
entry->rel, entry->page,
|
2001-08-23 02:24:26 +08:00
|
|
|
entry->offset, sizeof(BOX), FALSE);
|
2001-06-01 02:27:18 +08:00
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-06-01 02:27:18 +08:00
|
|
|
gistentryinit(*retval, (Datum) 0,
|
|
|
|
entry->rel, entry->page,
|
2001-10-25 13:50:21 +08:00
|
|
|
entry->offset, 0, FALSE);
|
|
|
|
}
|
2001-06-01 02:27:18 +08:00
|
|
|
}
|
2001-10-25 13:50:21 +08:00
|
|
|
else
|
|
|
|
retval = entry;
|
|
|
|
PG_RETURN_POINTER(retval);
|
2001-06-01 02:27:18 +08:00
|
|
|
}
|
|
|
|
|
2001-08-23 02:24:26 +08:00
|
|
|
Datum
|
2001-06-01 02:27:18 +08:00
|
|
|
gpoly_consistent(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-25 13:50:21 +08:00
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
|
|
|
POLYGON *query = (POLYGON *) PG_DETOAST_DATUM(PG_GETARG_POINTER(1));
|
|
|
|
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
|
|
|
|
bool result;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* * if entry is not leaf, use gbox_internal_consistent, * else use
|
|
|
|
* gbox_leaf_consistent
|
|
|
|
*/
|
|
|
|
if (!(DatumGetPointer(entry->key) != NULL && query))
|
2001-08-23 02:24:26 +08:00
|
|
|
PG_RETURN_BOOL(FALSE);
|
2001-06-01 02:27:18 +08:00
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
result = rtree_internal_consistent((BOX *) DatumGetPointer(entry->key),
|
|
|
|
&(query->boundbox), strategy);
|
2001-06-01 02:27:18 +08:00
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
PG_FREE_IF_COPY(query, 1);
|
|
|
|
PG_RETURN_BOOL(result);
|
2001-06-01 02:27:18 +08:00
|
|
|
}
|
|
|
|
|
2001-08-23 02:24:26 +08:00
|
|
|
/*****************************************
|
|
|
|
* Common rtree-function (for all ops)
|
|
|
|
*****************************************/
|
2001-06-01 02:27:18 +08:00
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
static bool
|
2001-06-01 02:27:18 +08:00
|
|
|
rtree_internal_consistent(BOX *key,
|
2001-10-25 13:50:21 +08:00
|
|
|
BOX *query,
|
|
|
|
StrategyNumber strategy)
|
2001-06-01 02:27:18 +08:00
|
|
|
{
|
2001-10-25 13:50:21 +08:00
|
|
|
bool retval;
|
|
|
|
|
|
|
|
switch (strategy)
|
|
|
|
{
|
|
|
|
case RTLeftStrategyNumber:
|
|
|
|
case RTOverLeftStrategyNumber:
|
|
|
|
retval = DatumGetBool(DirectFunctionCall2(box_overleft, PointerGetDatum(key), PointerGetDatum(query)));
|
|
|
|
break;
|
|
|
|
case RTOverlapStrategyNumber:
|
|
|
|
retval = DatumGetBool(DirectFunctionCall2(box_overlap, PointerGetDatum(key), PointerGetDatum(query)));
|
|
|
|
break;
|
|
|
|
case RTOverRightStrategyNumber:
|
|
|
|
case RTRightStrategyNumber:
|
|
|
|
retval = DatumGetBool(DirectFunctionCall2(box_right, PointerGetDatum(key), PointerGetDatum(query)));
|
|
|
|
break;
|
|
|
|
case RTSameStrategyNumber:
|
|
|
|
case RTContainsStrategyNumber:
|
|
|
|
retval = DatumGetBool(DirectFunctionCall2(box_contain, PointerGetDatum(key), PointerGetDatum(query)));
|
|
|
|
break;
|
|
|
|
case RTContainedByStrategyNumber:
|
|
|
|
retval = DatumGetBool(DirectFunctionCall2(box_overlap, PointerGetDatum(key), PointerGetDatum(query)));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
retval = FALSE;
|
|
|
|
}
|
|
|
|
return (retval);
|
2001-06-01 02:27:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** GiST DeCompress methods
|
|
|
|
** do not do anything.
|
|
|
|
*/
|
2001-08-23 02:24:26 +08:00
|
|
|
Datum
|
2001-06-01 02:27:18 +08:00
|
|
|
rtree_decompress(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-08-23 02:24:26 +08:00
|
|
|
PG_RETURN_POINTER(PG_GETARG_POINTER(0));
|
2001-06-01 02:27:18 +08:00
|
|
|
}
|