Commit 339cd6b9 authored by Bruce Momjian's avatar Bruce Momjian

Run pgindent on _int.c, for Tom Lane. I had skipped this file because

of a complex macro that pgindent complained about.  Hand-adjusted macro
for pgindent run.
parent 69a2c2ba
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
/* /*
#define GIST_DEBUG #define GIST_DEBUG
#define GIST_QUERY_DEBUG #define GIST_QUERY_DEBUG
*/ */
#include "postgres.h" #include "postgres.h"
...@@ -23,11 +23,11 @@ ...@@ -23,11 +23,11 @@
#include "storage/bufpage.h" #include "storage/bufpage.h"
/* number ranges for compression */ /* number ranges for compression */
#define MAXNUMRANGE 100 #define MAXNUMRANGE 100
#define max(a,b) ((a) > (b) ? (a) : (b)) #define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) <= (b) ? (a) : (b)) #define min(a,b) ((a) <= (b) ? (a) : (b))
#define abs(a) ((a) < (0) ? -(a) : (a)) #define abs(a) ((a) < (0) ? -(a) : (a))
/* dimension of array */ /* dimension of array */
#define NDIM 1 #define NDIM 1
...@@ -61,24 +61,25 @@ ...@@ -61,24 +61,25 @@
/* bigint defines */ /* bigint defines */
#define BITBYTE 8 #define BITBYTE 8
#define SIGLENINT 64 /* >122 => key will toast, so very slow!!! */ #define SIGLENINT 64 /* >122 => key will toast, so very slow!!! */
#define SIGLEN ( sizeof(int)*SIGLENINT ) #define SIGLEN ( sizeof(int)*SIGLENINT )
#define SIGLENBIT (SIGLEN*BITBYTE) #define SIGLENBIT (SIGLEN*BITBYTE)
typedef char BITVEC[SIGLEN]; typedef char BITVEC[SIGLEN];
typedef char* BITVECP; typedef char *BITVECP;
#define SIGPTR(x) ( (BITVECP) ARR_DATA_PTR(x) ) #define SIGPTR(x) ( (BITVECP) ARR_DATA_PTR(x) )
#define LOOPBYTE(a) \ #define LOOPBYTE(a) \
for(i=0;i<SIGLEN;i++) {\ for(i=0;i<SIGLEN;i++) {\
a;\ a;\
} }
#define LOOPBIT(a) \ #define LOOPBIT(a) \
for(i=0;i<SIGLENBIT;i++) {\ for(i=0;i<SIGLENBIT;i++) {\
a;\ a;\
} }
/* beware of multiple evaluation of arguments to these macros! */ /* beware of multiple evaluation of arguments to these macros! */
#define GETBYTEBIT(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITBYTE ) ) ) #define GETBYTEBIT(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITBYTE ) ) )
...@@ -88,111 +89,120 @@ typedef char* BITVECP; ...@@ -88,111 +89,120 @@ typedef char* BITVECP;
#ifdef GIST_DEBUG #ifdef GIST_DEBUG
static void printarr ( ArrayType * a, int num ) { static void
char bbb[16384]; printarr(ArrayType *a, int num)
char *cur; {
int l; char bbb[16384];
int *d; char *cur;
d = ARRPTR( a ); int l;
int *d;
d = ARRPTR(a);
*bbb = '\0'; *bbb = '\0';
cur = bbb; cur = bbb;
for(l=0; l<min( num, ARRNELEMS( a ));l++) { for (l = 0; l < min(num, ARRNELEMS(a)); l++)
sprintf(cur,"%d ", d[l] ); {
cur = strchr( cur, '\0' ) ; sprintf(cur, "%d ", d[l]);
cur = strchr(cur, '\0');
} }
elog(NOTICE, "\t\t%s", bbb); elog(NOTICE, "\t\t%s", bbb);
} }
static void printbitvec( BITVEC bv ) { static void
int i; printbitvec(BITVEC bv)
char str[ SIGLENBIT+1 ]; {
str[ SIGLENBIT ] ='\0'; int i;
LOOPBIT( str[i] = ( GETBIT(bv,i) ) ? '1' : '0' ); char str[SIGLENBIT + 1];
elog(NOTICE,"BV: %s", str); str[SIGLENBIT] = '\0';
LOOPBIT(str[i] = (GETBIT(bv, i)) ? '1' : '0');
elog(NOTICE, "BV: %s", str);
} }
#endif #endif
/* /*
** types for functions ** types for functions
*/ */
typedef ArrayType * (*formarray) (ArrayType*, ArrayType*); typedef ArrayType *(*formarray) (ArrayType *, ArrayType *);
typedef void (*formfloat) (ArrayType*, float*); typedef void (*formfloat) (ArrayType *, float *);
/* /*
** usefull function ** usefull function
*/ */
static bool isort( int *a, const int len ); static bool isort(int *a, const int len);
static ArrayType * new_intArrayType( int num ); static ArrayType *new_intArrayType(int num);
static ArrayType * copy_intArrayType( ArrayType * a ); static ArrayType *copy_intArrayType(ArrayType *a);
static ArrayType * resize_intArrayType( ArrayType * a, int num ); static ArrayType *resize_intArrayType(ArrayType *a, int num);
static int internal_size( int *a, int len ); static int internal_size(int *a, int len);
static ArrayType * _int_unique( ArrayType * a ); static ArrayType *_int_unique(ArrayType *a);
/* common GiST function*/ /* common GiST function*/
static GIST_SPLITVEC * _int_common_picksplit(bytea *entryvec, static GIST_SPLITVEC *_int_common_picksplit(bytea *entryvec,
GIST_SPLITVEC *v, GIST_SPLITVEC *v,
formarray unionf, formarray unionf,
formarray interf, formarray interf,
formfloat sizef, formfloat sizef,
float coef); float coef);
static float * _int_common_penalty(GISTENTRY *origentry, static float *_int_common_penalty(GISTENTRY *origentry,
GISTENTRY *newentry, GISTENTRY *newentry,
float *result, float *result,
formarray unionf, formarray unionf,
formfloat sizef); formfloat sizef);
static ArrayType * _int_common_union(bytea *entryvec, static ArrayType *_int_common_union(bytea *entryvec,
int *sizep, int *sizep,
formarray unionf); formarray unionf);
/* /*
** GiST support methods ** GiST support methods
*/ */
bool g_int_consistent(GISTENTRY *entry, ArrayType *query, StrategyNumber strategy); bool g_int_consistent(GISTENTRY *entry, ArrayType *query, StrategyNumber strategy);
GISTENTRY * g_int_compress(GISTENTRY *entry); GISTENTRY *g_int_compress(GISTENTRY *entry);
GISTENTRY * g_int_decompress(GISTENTRY *entry); GISTENTRY *g_int_decompress(GISTENTRY *entry);
float * g_int_penalty(GISTENTRY *origentry, GISTENTRY *newentry, float *result); float *g_int_penalty(GISTENTRY *origentry, GISTENTRY *newentry, float *result);
GIST_SPLITVEC * g_int_picksplit(bytea *entryvec, GIST_SPLITVEC *v); GIST_SPLITVEC *g_int_picksplit(bytea *entryvec, GIST_SPLITVEC *v);
ArrayType * g_int_union(bytea *entryvec, int *sizep); ArrayType *g_int_union(bytea *entryvec, int *sizep);
bool * g_int_same(ArrayType *b1, ArrayType *b2, bool *result); bool *g_int_same(ArrayType *b1, ArrayType *b2, bool *result);
/* /*
** R-tree support functions ** R-tree support functions
*/ */
bool inner_int_contains(ArrayType *a, ArrayType *b); bool inner_int_contains(ArrayType *a, ArrayType *b);
bool inner_int_overlap(ArrayType *a, ArrayType *b); bool inner_int_overlap(ArrayType *a, ArrayType *b);
ArrayType * inner_int_union(ArrayType *a, ArrayType *b); ArrayType *inner_int_union(ArrayType *a, ArrayType *b);
ArrayType * inner_int_inter(ArrayType *a, ArrayType *b); ArrayType *inner_int_inter(ArrayType *a, ArrayType *b);
bool _int_different(ArrayType *a, ArrayType *b); bool _int_different(ArrayType *a, ArrayType *b);
bool _int_same(ArrayType *a, ArrayType *b); bool _int_same(ArrayType *a, ArrayType *b);
bool _int_contains(ArrayType *a, ArrayType *b); bool _int_contains(ArrayType *a, ArrayType *b);
bool _int_contained(ArrayType *a, ArrayType *b); bool _int_contained(ArrayType *a, ArrayType *b);
bool _int_overlap(ArrayType *a, ArrayType *b); bool _int_overlap(ArrayType *a, ArrayType *b);
ArrayType * _int_union(ArrayType *a, ArrayType *b); ArrayType *_int_union(ArrayType *a, ArrayType *b);
ArrayType * _int_inter(ArrayType *a, ArrayType *b); ArrayType *_int_inter(ArrayType *a, ArrayType *b);
void rt__int_size(ArrayType *a, float* sz); void rt__int_size(ArrayType *a, float *sz);
/* /*
** _intbig methods ** _intbig methods
*/ */
bool g_intbig_consistent(GISTENTRY *entry, ArrayType *query, StrategyNumber strategy); bool g_intbig_consistent(GISTENTRY *entry, ArrayType *query, StrategyNumber strategy);
GISTENTRY * g_intbig_compress(GISTENTRY *entry); GISTENTRY *g_intbig_compress(GISTENTRY *entry);
GISTENTRY * g_intbig_decompress(GISTENTRY *entry); GISTENTRY *g_intbig_decompress(GISTENTRY *entry);
float * g_intbig_penalty(GISTENTRY *origentry, GISTENTRY *newentry, float *result); float *g_intbig_penalty(GISTENTRY *origentry, GISTENTRY *newentry, float *result);
GIST_SPLITVEC * g_intbig_picksplit(bytea *entryvec, GIST_SPLITVEC *v); GIST_SPLITVEC *g_intbig_picksplit(bytea *entryvec, GIST_SPLITVEC *v);
ArrayType * g_intbig_union(bytea *entryvec, int *sizep); ArrayType *g_intbig_union(bytea *entryvec, int *sizep);
bool * g_intbig_same(ArrayType *a, ArrayType *b, bool *result); bool *g_intbig_same(ArrayType *a, ArrayType *b, bool *result);
static bool _intbig_contains(ArrayType *a, ArrayType *b); static bool _intbig_contains(ArrayType *a, ArrayType *b);
static bool _intbig_overlap(ArrayType *a, ArrayType *b); static bool _intbig_overlap(ArrayType *a, ArrayType *b);
static ArrayType * _intbig_union(ArrayType *a, ArrayType *b); static ArrayType *_intbig_union(ArrayType *a, ArrayType *b);
/*static ArrayType * _intbig_inter(ArrayType *a, ArrayType *b);*/
static void rt__intbig_size(ArrayType *a, float* sz); /*static ArrayType * _intbig_inter(ArrayType *a, ArrayType *b);*/
static void gensign(BITVEC sign, int * a, int len); static void rt__intbig_size(ArrayType *a, float *sz);
static void gensign(BITVEC sign, int *a, int len);
/***************************************************************************** /*****************************************************************************
* GiST functions * GiST functions
*****************************************************************************/ *****************************************************************************/
/* /*
...@@ -201,176 +211,194 @@ static void gensign(BITVEC sign, int * a, int len); ...@@ -201,176 +211,194 @@ static void gensign(BITVEC sign, int * a, int len);
** the predicate x op query == FALSE, where op is the oper ** the predicate x op query == FALSE, where op is the oper
** corresponding to strategy in the pg_amop table. ** corresponding to strategy in the pg_amop table.
*/ */
bool bool
g_int_consistent(GISTENTRY *entry, g_int_consistent(GISTENTRY *entry,
ArrayType *query, ArrayType *query,
StrategyNumber strategy) StrategyNumber strategy)
{ {
bool retval; bool retval;
/* sort query for fast search, key is already sorted */ /* sort query for fast search, key is already sorted */
/* XXX are we sure it's safe to scribble on the query object here? */ /* XXX are we sure it's safe to scribble on the query object here? */
/* XXX what about toasted input? */ /* XXX what about toasted input? */
if ( ARRISNULL( query ) ) return FALSE; if (ARRISNULL(query))
PREPAREARR( query ); return FALSE;
PREPAREARR(query);
switch(strategy) {
case RTOverlapStrategyNumber: switch (strategy)
retval = (bool)inner_int_overlap((ArrayType *)(entry->pred), query); {
break; case RTOverlapStrategyNumber:
case RTSameStrategyNumber: retval = (bool) inner_int_overlap((ArrayType *) (entry->pred), query);
case RTContainsStrategyNumber: break;
retval = (bool)inner_int_contains((ArrayType *)(entry->pred), query); case RTSameStrategyNumber:
break; case RTContainsStrategyNumber:
case RTContainedByStrategyNumber: retval = (bool) inner_int_contains((ArrayType *) (entry->pred), query);
retval = (bool)inner_int_overlap((ArrayType *)(entry->pred), query); break;
break; case RTContainedByStrategyNumber:
default: retval = (bool) inner_int_overlap((ArrayType *) (entry->pred), query);
retval = FALSE; break;
} default:
return(retval); retval = FALSE;
}
return (retval);
} }
ArrayType * ArrayType *
g_int_union(bytea *entryvec, int *sizep) g_int_union(bytea *entryvec, int *sizep)
{ {
return _int_common_union( entryvec, sizep, inner_int_union ); return _int_common_union(entryvec, sizep, inner_int_union);
} }
/* /*
** GiST Compress and Decompress methods ** GiST Compress and Decompress methods
*/ */
GISTENTRY * GISTENTRY *
g_int_compress(GISTENTRY *entry) g_int_compress(GISTENTRY *entry)
{ {
GISTENTRY *retval; GISTENTRY *retval;
ArrayType * r; ArrayType *r;
int len; int len;
int *dr; int *dr;
int i,min,cand; int i,
min,
cand;
retval = palloc(sizeof(GISTENTRY)); retval = palloc(sizeof(GISTENTRY));
#ifdef PGSQL71 #ifdef PGSQL71
if ( entry->pred ) if (entry->pred)
r = (ArrayType *)PG_DETOAST_DATUM_COPY( entry->pred ); r = (ArrayType *) PG_DETOAST_DATUM_COPY(entry->pred);
else else
r = NULL; r = NULL;
#else #else
r = copy_intArrayType( (ArrayType *) entry->pred ); r = copy_intArrayType((ArrayType *) entry->pred);
#endif #endif
if ( ARRISNULL( r ) ) { if (ARRISNULL(r))
{
#ifdef GIST_DEBUG #ifdef GIST_DEBUG
elog(NOTICE,"COMP IN: NULL"); elog(NOTICE, "COMP IN: NULL");
#endif #endif
if ( r ) if ( (char*)r != (char*)entry->pred ) pfree(r); if (r)
if ((char *) r != (char *) entry->pred)
pfree(r);
gistentryinit(*retval, (char *) NULL, entry->rel, entry->page, entry->offset,
0, FALSE);
return (retval);
}
gistentryinit(*retval, (char *)NULL, entry->rel, entry->page, entry->offset, if (entry->leafkey)
0, FALSE); PREPAREARR(r);
return( retval ); len = ARRNELEMS(r);
}
if ( entry->leafkey ) PREPAREARR( r );
len = ARRNELEMS( r );
#ifdef GIST_DEBUG #ifdef GIST_DEBUG
elog(NOTICE, "COMP IN: %d leaf; %d rel; %d page; %d offset; %d bytes; %d elems", entry->leafkey, (int)entry->rel, (int)entry->page, (int)entry->offset, (int)entry->bytes, len); elog(NOTICE, "COMP IN: %d leaf; %d rel; %d page; %d offset; %d bytes; %d elems", entry->leafkey, (int) entry->rel, (int) entry->page, (int) entry->offset, (int) entry->bytes, len);
#endif #endif
if ( len >= 2*MAXNUMRANGE ) { /*compress*/ if (len >= 2 * MAXNUMRANGE)
r = resize_intArrayType( r, 2*( len ) ); { /* compress */
r = resize_intArrayType(r, 2 * (len));
dr = ARRPTR( r );
dr = ARRPTR(r);
for(i=len-1; i>=0;i--)
dr[2*i] = dr[2*i+1] = dr[i]; for (i = len - 1; i >= 0; i--)
dr[2 * i] = dr[2 * i + 1] = dr[i];
len *= 2;
cand = 1; len *= 2;
while( len > MAXNUMRANGE * 2 ) { cand = 1;
min = 0x7fffffff; while (len > MAXNUMRANGE * 2)
for( i=2; i<len;i+=2 ) {
if ( min > (dr[i] - dr[i-1]) ) { min = 0x7fffffff;
min = (dr[i] - dr[i-1]); for (i = 2; i < len; i += 2)
cand = i; if (min > (dr[i] - dr[i - 1]))
} {
memmove( (void*)&dr[cand-1], (void*)&dr[cand+1], (len - cand - 1)*sizeof(int) ); min = (dr[i] - dr[i - 1]);
len -= 2; cand = i;
}
memmove((void *) &dr[cand - 1], (void *) &dr[cand + 1], (len - cand - 1) * sizeof(int));
len -= 2;
}
r = resize_intArrayType(r, len);
} }
r = resize_intArrayType(r, len );
}
gistentryinit(*retval, (char *)r, entry->rel, entry->page, entry->offset, VARSIZE( r ), FALSE); gistentryinit(*retval, (char *) r, entry->rel, entry->page, entry->offset, VARSIZE(r), FALSE);
return(retval); return (retval);
} }
GISTENTRY * GISTENTRY *
g_int_decompress(GISTENTRY *entry) g_int_decompress(GISTENTRY *entry)
{ {
GISTENTRY *retval; GISTENTRY *retval;
ArrayType * r; ArrayType *r;
int *dr, lenr; int *dr,
ArrayType * in; lenr;
int lenin; ArrayType *in;
int *din; int lenin;
int i,j; int *din;
int i,
j;
#ifdef PGSQL71 #ifdef PGSQL71
if ( entry->pred ) if (entry->pred)
in = (ArrayType *)PG_DETOAST_DATUM( entry->pred ); in = (ArrayType *) PG_DETOAST_DATUM(entry->pred);
else else
in = NULL; in = NULL;
#else #else
in = (ArrayType *) entry->pred; in = (ArrayType *) entry->pred;
#endif #endif
if ( entry->bytes < ARR_OVERHEAD( NDIM ) || ARRISNULL( in ) ) { if (entry->bytes < ARR_OVERHEAD(NDIM) || ARRISNULL(in))
retval = palloc(sizeof(GISTENTRY)); {
retval = palloc(sizeof(GISTENTRY));
#ifdef PGSQL71 #ifdef PGSQL71
if ( in ) if ( (char*)in != (char*)entry->pred ) pfree(in); if (in)
if ((char *) in != (char *) entry->pred)
pfree(in);
#endif #endif
gistentryinit(*retval, (char *)NULL, entry->rel, entry->page, entry->offset, 0, FALSE); gistentryinit(*retval, (char *) NULL, entry->rel, entry->page, entry->offset, 0, FALSE);
#ifdef GIST_DEBUG #ifdef GIST_DEBUG
elog(NOTICE,"DECOMP IN: NULL"); elog(NOTICE, "DECOMP IN: NULL");
#endif #endif
return( retval ); return (retval);
} }
lenin = ARRNELEMS(in);
din = ARRPTR(in);
if ( lenin < 2*MAXNUMRANGE ) { /*not comressed value*/ lenin = ARRNELEMS(in);
/* sometimes strange bytesize */ din = ARRPTR(in);
gistentryinit(*entry, (char *)in, entry->rel, entry->page, entry->offset, VARSIZE( in ), FALSE);
return (entry); if (lenin < 2 * MAXNUMRANGE)
} { /* not comressed value */
/* sometimes strange bytesize */
gistentryinit(*entry, (char *) in, entry->rel, entry->page, entry->offset, VARSIZE(in), FALSE);
return (entry);
}
#ifdef GIST_DEBUG #ifdef GIST_DEBUG
elog(NOTICE, "DECOMP IN: %d leaf; %d rel; %d page; %d offset; %d bytes; %d elems", entry->leafkey, (int)entry->rel, (int)entry->page, (int)entry->offset, (int)entry->bytes, lenin); elog(NOTICE, "DECOMP IN: %d leaf; %d rel; %d page; %d offset; %d bytes; %d elems", entry->leafkey, (int) entry->rel, (int) entry->page, (int) entry->offset, (int) entry->bytes, lenin);
#endif #endif
lenr = internal_size(din, lenin); lenr = internal_size(din, lenin);
r = new_intArrayType( lenr ); r = new_intArrayType(lenr);
dr = ARRPTR( r ); dr = ARRPTR(r);
for(i=0;i<lenin;i+=2) for (i = 0; i < lenin; i += 2)
for(j=din[i]; j<=din[i+1]; j++) for (j = din[i]; j <= din[i + 1]; j++)
if ( (!i) || *(dr-1) != j ) if ((!i) || *(dr - 1) != j)
*dr++ = j; *dr++ = j;
#ifdef PGSQL71 #ifdef PGSQL71
if ( (char*)in != (char*)entry->pred ) pfree(in); if ((char *) in != (char *) entry->pred)
pfree(in);
#endif #endif
retval = palloc(sizeof(GISTENTRY)); retval = palloc(sizeof(GISTENTRY));
gistentryinit(*retval, (char *)r, entry->rel, entry->page, entry->offset, VARSIZE( r ), FALSE); gistentryinit(*retval, (char *) r, entry->rel, entry->page, entry->offset, VARSIZE(r), FALSE);
return(retval); return (retval);
} }
/* /*
...@@ -379,19 +407,19 @@ g_int_decompress(GISTENTRY *entry) ...@@ -379,19 +407,19 @@ g_int_decompress(GISTENTRY *entry)
float * float *
g_int_penalty(GISTENTRY *origentry, GISTENTRY *newentry, float *result) g_int_penalty(GISTENTRY *origentry, GISTENTRY *newentry, float *result)
{ {
return _int_common_penalty( origentry, newentry, result, inner_int_union, rt__int_size); return _int_common_penalty(origentry, newentry, result, inner_int_union, rt__int_size);
} }
GIST_SPLITVEC * GIST_SPLITVEC *
g_int_picksplit(bytea *entryvec, g_int_picksplit(bytea *entryvec,
GIST_SPLITVEC *v) GIST_SPLITVEC *v)
{ {
return _int_common_picksplit( entryvec, v, return _int_common_picksplit(entryvec, v,
inner_int_union, inner_int_union,
inner_int_inter, inner_int_inter,
rt__int_size, rt__int_size,
1e-8); 1e-8);
} }
/* /*
...@@ -402,64 +430,79 @@ g_int_picksplit(bytea *entryvec, ...@@ -402,64 +430,79 @@ g_int_picksplit(bytea *entryvec,
bool * bool *
g_int_same(ArrayType *b1, ArrayType *b2, bool *result) g_int_same(ArrayType *b1, ArrayType *b2, bool *result)
{ {
if (_int_same(b1, b2)) if (_int_same(b1, b2))
*result = TRUE; *result = TRUE;
else *result = FALSE; else
*result = FALSE;
return(result); return (result);
} }
bool bool
_int_contained(ArrayType *a, ArrayType *b) _int_contained(ArrayType *a, ArrayType *b)
{ {
return ( _int_contains(b, a) ); return (_int_contains(b, a));
} }
bool bool
_int_contains ( ArrayType *a, ArrayType *b ) { _int_contains(ArrayType *a, ArrayType *b)
bool res; {
ArrayType *an, *bn; bool res;
ArrayType *an,
*bn;
if ( ARRISNULL( a ) || ARRISNULL( b ) ) return FALSE; if (ARRISNULL(a) || ARRISNULL(b))
return FALSE;
an = copy_intArrayType( a ); an = copy_intArrayType(a);
bn = copy_intArrayType( b ); bn = copy_intArrayType(b);
PREPAREARR(an); PREPAREARR(an);
PREPAREARR(bn); PREPAREARR(bn);
res = inner_int_contains( an, bn ); res = inner_int_contains(an, bn);
pfree( an ); pfree( bn ); pfree(an);
pfree(bn);
return res; return res;
} }
bool bool
inner_int_contains ( ArrayType *a, ArrayType *b ) { inner_int_contains(ArrayType *a, ArrayType *b)
int na, nb; {
int i,j, n; int na,
int *da, *db; nb;
int i,
if ( ARRISNULL( a ) || ARRISNULL( b ) ) return FALSE; j,
n;
na = ARRNELEMS( a ); int *da,
nb = ARRNELEMS( b ); *db;
da = ARRPTR( a );
db = ARRPTR( b ); if (ARRISNULL(a) || ARRISNULL(b))
return FALSE;
na = ARRNELEMS(a);
nb = ARRNELEMS(b);
da = ARRPTR(a);
db = ARRPTR(b);
#ifdef GIST_DEBUG #ifdef GIST_DEBUG
elog(NOTICE, "contains %d %d", na, nb); elog(NOTICE, "contains %d %d", na, nb);
#endif #endif
i = j = n = 0; i = j = n = 0;
while( i<na && j<nb ) while (i < na && j < nb)
if ( da[i] < db[j] ) if (da[i] < db[j])
i++; i++;
else if ( da[i] == db[j] ) { else if (da[i] == db[j])
n++; i++; j++; {
} else n++;
i++;
j++;
}
else
j++; j++;
return ( n == nb ) ? TRUE : FALSE; return (n == nb) ? TRUE : FALSE;
} }
/***************************************************************************** /*****************************************************************************
...@@ -469,268 +512,325 @@ inner_int_contains ( ArrayType *a, ArrayType *b ) { ...@@ -469,268 +512,325 @@ inner_int_contains ( ArrayType *a, ArrayType *b ) {
bool bool
_int_different(ArrayType *a, ArrayType *b) _int_different(ArrayType *a, ArrayType *b)
{ {
return ( !_int_same( a, b ) ); return (!_int_same(a, b));
} }
bool bool
_int_same ( ArrayType *a, ArrayType *b ) { _int_same(ArrayType *a, ArrayType *b)
int na , nb ; {
int n; int na,
int *da, *db; nb;
bool result; int n;
ArrayType *an, *bn; int *da,
bool anull = ARRISNULL( a ); *db;
bool bnull = ARRISNULL( b ); bool result;
ArrayType *an,
if ( anull || bnull ) *bn;
return ( anull && bnull ) ? TRUE : FALSE; bool anull = ARRISNULL(a);
bool bnull = ARRISNULL(b);
an = copy_intArrayType( a );
bn = copy_intArrayType( b ); if (anull || bnull)
return (anull && bnull) ? TRUE : FALSE;
SORT( an );
SORT( bn ); an = copy_intArrayType(a);
na = ARRNELEMS( an ); bn = copy_intArrayType(b);
nb = ARRNELEMS( bn );
da = ARRPTR( an ); SORT(an);
db = ARRPTR( bn ); SORT(bn);
na = ARRNELEMS(an);
nb = ARRNELEMS(bn);
da = ARRPTR(an);
db = ARRPTR(bn);
result = FALSE; result = FALSE;
if ( na == nb ) if (na == nb)
{ {
result = TRUE; result = TRUE;
for(n=0; n<na; n++) for (n = 0; n < na; n++)
if ( da[n] != db[n] ) if (da[n] != db[n])
{ {
result = FALSE; result = FALSE;
break; break;
} }
} }
pfree( an ); pfree( bn ); pfree(an);
pfree(bn);
return result; return result;
} }
/* _int_overlap -- does a overlap b? /* _int_overlap -- does a overlap b?
*/ */
bool bool
_int_overlap ( ArrayType *a, ArrayType *b ) { _int_overlap(ArrayType *a, ArrayType *b)
bool result; {
ArrayType *an, *bn; bool result;
ArrayType *an,
*bn;
if ( ARRISNULL( a ) || ARRISNULL( b ) ) return FALSE; if (ARRISNULL(a) || ARRISNULL(b))
return FALSE;
an = copy_intArrayType( a );
bn = copy_intArrayType( b ); an = copy_intArrayType(a);
bn = copy_intArrayType(b);
SORT(an); SORT(an);
SORT(bn); SORT(bn);
result = inner_int_overlap( an, bn ); result = inner_int_overlap(an, bn);
pfree( an ); pfree( bn ); pfree(an);
pfree(bn);
return result; return result;
} }
bool bool
inner_int_overlap ( ArrayType *a, ArrayType *b ) { inner_int_overlap(ArrayType *a, ArrayType *b)
int na , nb ; {
int i,j; int na,
int *da, *db; nb;
int i,
j;
int *da,
*db;
if ( ARRISNULL( a ) || ARRISNULL( b ) ) return FALSE; if (ARRISNULL(a) || ARRISNULL(b))
return FALSE;
na = ARRNELEMS( a );
nb = ARRNELEMS( b ); na = ARRNELEMS(a);
da = ARRPTR( a ); nb = ARRNELEMS(b);
db = ARRPTR( b ); da = ARRPTR(a);
db = ARRPTR(b);
#ifdef GIST_DEBUG #ifdef GIST_DEBUG
elog(NOTICE, "g_int_overlap"); elog(NOTICE, "g_int_overlap");
#endif #endif
i = j = 0; i = j = 0;
while( i<na && j<nb ) while (i < na && j < nb)
if ( da[i] < db[j] ) if (da[i] < db[j])
i++; i++;
else if ( da[i] == db[j] ) else if (da[i] == db[j])
return TRUE; return TRUE;
else else
j++; j++;
return FALSE; return FALSE;
} }
ArrayType * ArrayType *
_int_union ( ArrayType *a, ArrayType *b ) { _int_union(ArrayType *a, ArrayType *b)
ArrayType *result; {
ArrayType *an, *bn; ArrayType *result;
ArrayType *an,
an = copy_intArrayType( a ); *bn;
bn = copy_intArrayType( b );
an = copy_intArrayType(a);
bn = copy_intArrayType(b);
if ( ! ARRISNULL( an ) ) SORT(an); if (!ARRISNULL(an))
if ( ! ARRISNULL( bn ) ) SORT(bn); SORT(an);
if (!ARRISNULL(bn))
SORT(bn);
result = inner_int_union( an, bn ); result = inner_int_union(an, bn);
if (an) pfree( an ); if (an)
if (bn) pfree( bn ); pfree(an);
if (bn)
pfree(bn);
return result; return result;
} }
ArrayType * ArrayType *
inner_int_union ( ArrayType *a, ArrayType *b ) { inner_int_union(ArrayType *a, ArrayType *b)
ArrayType * r = NULL; {
int na , nb; ArrayType *r = NULL;
int *da, *db, *dr; int na,
int i,j; nb;
int *da,
*db,
*dr;
int i,
j;
#ifdef GIST_DEBUG #ifdef GIST_DEBUG
elog(NOTICE, "inner_union %d %d", ARRISNULL( a ) , ARRISNULL( b ) ); elog(NOTICE, "inner_union %d %d", ARRISNULL(a), ARRISNULL(b));
#endif #endif
if ( ARRISNULL( a ) && ARRISNULL( b ) ) return new_intArrayType(0); if (ARRISNULL(a) && ARRISNULL(b))
if ( ARRISNULL( a ) ) r = copy_intArrayType( b ); return new_intArrayType(0);
if ( ARRISNULL( b ) ) r = copy_intArrayType( a ); if (ARRISNULL(a))
r = copy_intArrayType(b);
if ( r ) { if (ARRISNULL(b))
dr = ARRPTR( r ); r = copy_intArrayType(a);
} else {
na = ARRNELEMS( a ); if (r)
nb = ARRNELEMS( b ); dr = ARRPTR(r);
da = ARRPTR( a ); else
db = ARRPTR( b ); {
na = ARRNELEMS(a);
nb = ARRNELEMS(b);
da = ARRPTR(a);
db = ARRPTR(b);
r = new_intArrayType( na + nb ); r = new_intArrayType(na + nb);
dr = ARRPTR( r ); dr = ARRPTR(r);
/* union */ /* union */
i = j = 0; i = j = 0;
while( i<na && j<nb ) while (i < na && j < nb)
if ( da[i] < db[j] ) if (da[i] < db[j])
*dr++ = da[i++]; *dr++ = da[i++];
else else
*dr++ = db[j++]; *dr++ = db[j++];
while( i<na ) *dr++ = da[i++];
while( j<nb ) *dr++ = db[j++];
} while (i < na)
*dr++ = da[i++];
while (j < nb)
*dr++ = db[j++];
if ( ARRNELEMS(r) > 1 ) }
r = _int_unique( r );
if (ARRNELEMS(r) > 1)
r = _int_unique(r);
return r; return r;
} }
ArrayType * ArrayType *
_int_inter ( ArrayType *a, ArrayType *b ) { _int_inter(ArrayType *a, ArrayType *b)
ArrayType *result; {
ArrayType *an, *bn; ArrayType *result;
ArrayType *an,
if ( ARRISNULL( a ) || ARRISNULL( b ) ) return new_intArrayType(0); *bn;
an = copy_intArrayType( a ); if (ARRISNULL(a) || ARRISNULL(b))
bn = copy_intArrayType( b ); return new_intArrayType(0);
an = copy_intArrayType(a);
bn = copy_intArrayType(b);
SORT(an); SORT(an);
SORT(bn); SORT(bn);
result = inner_int_inter( an, bn ); result = inner_int_inter(an, bn);
pfree( an ); pfree( bn ); pfree(an);
pfree(bn);
return result; return result;
} }
ArrayType * ArrayType *
inner_int_inter ( ArrayType *a, ArrayType *b ) { inner_int_inter(ArrayType *a, ArrayType *b)
ArrayType * r; {
int na , nb ; ArrayType *r;
int *da, *db, *dr; int na,
int i,j; nb;
int *da,
*db,
*dr;
int i,
j;
#ifdef GIST_DEBUG #ifdef GIST_DEBUG
elog(NOTICE, "inner_inter %d %d", ARRISNULL( a ), ARRISNULL( b ) ); elog(NOTICE, "inner_inter %d %d", ARRISNULL(a), ARRISNULL(b));
#endif #endif
if ( ARRISNULL( a ) || ARRISNULL( b ) ) return new_intArrayType(0); if (ARRISNULL(a) || ARRISNULL(b))
return new_intArrayType(0);
na = ARRNELEMS(a);
nb = ARRNELEMS(b);
da = ARRPTR(a);
db = ARRPTR(b);
r = new_intArrayType(min(na, nb));
dr = ARRPTR(r);
na = ARRNELEMS( a );
nb = ARRNELEMS( b );
da = ARRPTR( a );
db = ARRPTR( b );
r = new_intArrayType( min(na, nb) );
dr = ARRPTR( r );
i = j = 0; i = j = 0;
while( i<na && j<nb ) while (i < na && j < nb)
if ( da[i] < db[j] ) if (da[i] < db[j])
i++; i++;
else if ( da[i] == db[j] ) { else if (da[i] == db[j])
if ( i+j == 0 || ( i+j>0 && *(dr-1) != db[j] ) ) {
if (i + j == 0 || (i + j > 0 && *(dr - 1) != db[j]))
*dr++ = db[j]; *dr++ = db[j];
i++; j++; i++;
} else j++;
}
else
j++; j++;
if ( (dr - ARRPTR(r)) == 0 ) { if ((dr - ARRPTR(r)) == 0)
pfree( r ); {
pfree(r);
return new_intArrayType(0); return new_intArrayType(0);
} else }
return resize_intArrayType(r, dr - ARRPTR(r) ); else
return resize_intArrayType(r, dr - ARRPTR(r));
} }
void void
rt__int_size(ArrayType *a, float *size) rt__int_size(ArrayType *a, float *size)
{ {
if ( ARRISNULL( a ) ) if (ARRISNULL(a))
*size = 0.0; *size = 0.0;
else else
*size = (float)ARRNELEMS( a ); *size = (float) ARRNELEMS(a);
return; return;
} }
/***************************************************************************** /*****************************************************************************
* Miscellaneous operators and functions * Miscellaneous operators and functions
*****************************************************************************/ *****************************************************************************/
/* len >= 2 */ /* len >= 2 */
static bool static bool
isort ( int *a, int len ) { isort(int *a, int len)
int tmp, index; {
int *cur, *end; int tmp,
bool r = FALSE; index;
end = a + len; int *cur,
do { *end;
index = 0; bool r = FALSE;
cur = a + 1;
while( cur < end ) { end = a + len;
if( *(cur-1) > *cur ) { do
tmp=*(cur-1); *(cur-1) = *cur; *cur=tmp; {
index = 1; index = 0;
} else if ( ! r && *(cur-1) == *cur ) cur = a + 1;
while (cur < end)
{
if (*(cur - 1) > *cur)
{
tmp = *(cur - 1);
*(cur - 1) = *cur;
*cur = tmp;
index = 1;
}
else if (!r && *(cur - 1) == *cur)
r = TRUE; r = TRUE;
cur++; cur++;
} }
} while( index ); } while (index);
return r; return r;
} }
static ArrayType * static ArrayType *
new_intArrayType( int num ) { new_intArrayType(int num)
ArrayType * r; {
int nbytes = ARR_OVERHEAD( NDIM ) + sizeof(int)*num; ArrayType *r;
int nbytes = ARR_OVERHEAD(NDIM) + sizeof(int) * num;
r = (ArrayType *) palloc( nbytes );
r = (ArrayType *) palloc(nbytes);
MemSet(r, 0, nbytes); MemSet(r, 0, nbytes);
r->size = nbytes; r->size = nbytes;
...@@ -738,127 +838,156 @@ new_intArrayType( int num ) { ...@@ -738,127 +838,156 @@ new_intArrayType( int num ) {
#ifndef PGSQL71 #ifndef PGSQL71
SET_LO_FLAG(false, r); SET_LO_FLAG(false, r);
#endif #endif
*( (int*)ARR_DIMS(r) ) = num; *((int *) ARR_DIMS(r)) = num;
*( (int*)ARR_LBOUND(r) ) = 1; *((int *) ARR_LBOUND(r)) = 1;
return r; return r;
} }
static ArrayType *
resize_intArrayType(ArrayType *a, int num)
{
int nbytes = ARR_OVERHEAD(NDIM) + sizeof(int) * num;
static ArrayType * if (num == ARRNELEMS(a))
resize_intArrayType( ArrayType * a, int num ) { return a;
int nbytes = ARR_OVERHEAD( NDIM ) + sizeof(int)*num;
if ( num == ARRNELEMS(a) ) return a; a = (ArrayType *) repalloc(a, nbytes);
a = (ArrayType *) repalloc( a, nbytes );
a->size = nbytes; a->size = nbytes;
*( (int*)ARR_DIMS(a) ) = num; *((int *) ARR_DIMS(a)) = num;
return a; return a;
} }
static ArrayType * static ArrayType *
copy_intArrayType( ArrayType * a ) { copy_intArrayType(ArrayType *a)
ArrayType * r; {
if ( ARRISNULL(a) ) return NULL; ArrayType *r;
r = new_intArrayType( ARRNELEMS(a) );
memmove(r,a,VARSIZE(a)); if (ARRISNULL(a))
return NULL;
r = new_intArrayType(ARRNELEMS(a));
memmove(r, a, VARSIZE(a));
return r; return r;
} }
/* num for compressed key */ /* num for compressed key */
static int static int
internal_size (int *a, int len ) { internal_size(int *a, int len)
int i,size=0; {
int i,
size = 0;
for(i=0;i<len;i+=2) for (i = 0; i < len; i += 2)
if ( ! i || a[i] != a[i-1] ) /* do not count repeated range */ if (!i || a[i] != a[i - 1]) /* do not count repeated range */
size += a[i+1] - a[i] + 1; size += a[i + 1] - a[i] + 1;
return size; return size;
} }
/* r is sorted and size of r > 1 */ /* r is sorted and size of r > 1 */
static ArrayType * static ArrayType *
_int_unique( ArrayType * r ) { _int_unique(ArrayType *r)
int *tmp, *dr, *data; {
int num = ARRNELEMS(r); int *tmp,
data = tmp = dr = ARRPTR( r ); *dr,
while( tmp - data < num ) *data;
if ( *tmp != *dr ) int num = ARRNELEMS(r);
data = tmp = dr = ARRPTR(r);
while (tmp - data < num)
if (*tmp != *dr)
*(++dr) = *tmp++; *(++dr) = *tmp++;
else else
tmp++; tmp++;
return resize_intArrayType(r, dr + 1 - ARRPTR(r) ); return resize_intArrayType(r, dr + 1 - ARRPTR(r));
} }
/********************************************************************* /*********************************************************************
** intbig functions ** intbig functions
*********************************************************************/ *********************************************************************/
static void static void
gensign(BITVEC sign, int * a, int len) { gensign(BITVEC sign, int *a, int len)
int i; {
/* we assume that the sign vector is previously zeroed */ int i;
for(i=0; i<len; i++) {
SETBIT( sign, (*a)%SIGLENBIT ); /* we assume that the sign vector is previously zeroed */
a++; for (i = 0; i < len; i++)
} {
SETBIT(sign, (*a) % SIGLENBIT);
a++;
}
} }
static bool static bool
_intbig_overlap(ArrayType *a, ArrayType *b) { _intbig_overlap(ArrayType *a, ArrayType *b)
int i; {
BITVECP da, db; int i;
BITVECP da,
if ( ARRISNULL( a ) || ARRISNULL( b ) ) return FALSE; db;
if (ARRISNULL(a) || ARRISNULL(b))
return FALSE;
da = SIGPTR(a); da = SIGPTR(a);
db = SIGPTR(b); db = SIGPTR(b);
LOOPBYTE( if ( da[i] & db[i] ) return TRUE ); LOOPBYTE(if (da[i] & db[i]) return TRUE);
return FALSE; return FALSE;
} }
static bool static bool
_intbig_contains(ArrayType *a, ArrayType *b) { _intbig_contains(ArrayType *a, ArrayType *b)
int i; {
BITVECP da, db; int i;
BITVECP da,
if ( ARRISNULL( a ) || ARRISNULL( b ) ) return FALSE; db;
if (ARRISNULL(a) || ARRISNULL(b))
return FALSE;
da = SIGPTR(a); da = SIGPTR(a);
db = SIGPTR(b); db = SIGPTR(b);
LOOPBYTE( if ( db[i] & ~da[i] ) return FALSE ); LOOPBYTE(if (db[i] & ~da[i]) return FALSE);
return TRUE; return TRUE;
} }
static void static void
rt__intbig_size(ArrayType *a, float* sz) { rt__intbig_size(ArrayType *a, float *sz)
int i, len=0; {
BITVECP bv; int i,
len = 0;
BITVECP bv;
if ( ARRISNULL( a ) ) { if (ARRISNULL(a))
*sz=0.0; {
*sz = 0.0;
return; return;
} }
bv = SIGPTR(a); bv = SIGPTR(a);
LOOPBIT( len += GETBIT(bv, i) ); LOOPBIT(len += GETBIT(bv, i));
*sz = (float) len; *sz = (float) len;
return; return;
} }
static ArrayType * static ArrayType *
_intbig_union(ArrayType *a, ArrayType *b) { _intbig_union(ArrayType *a, ArrayType *b)
ArrayType * r; {
BITVECP da, db, dr; ArrayType *r;
int i; BITVECP da,
db,
if ( ARRISNULL( a ) && ARRISNULL( b ) ) return new_intArrayType(0); dr;
if ( ARRISNULL( a ) ) return copy_intArrayType( b ); int i;
if ( ARRISNULL( b ) ) return copy_intArrayType( a );
if (ARRISNULL(a) && ARRISNULL(b))
r = new_intArrayType( SIGLENINT ); return new_intArrayType(0);
if (ARRISNULL(a))
return copy_intArrayType(b);
if (ARRISNULL(b))
return copy_intArrayType(a);
r = new_intArrayType(SIGLENINT);
da = SIGPTR(a); da = SIGPTR(a);
db = SIGPTR(b); db = SIGPTR(b);
...@@ -866,18 +995,22 @@ _intbig_union(ArrayType *a, ArrayType *b) { ...@@ -866,18 +995,22 @@ _intbig_union(ArrayType *a, ArrayType *b) {
LOOPBYTE(dr[i] = da[i] | db[i]); LOOPBYTE(dr[i] = da[i] | db[i]);
return r; return r;
} }
static ArrayType * static ArrayType *
_intbig_inter(ArrayType *a, ArrayType *b) { _intbig_inter(ArrayType *a, ArrayType *b)
ArrayType * r; {
BITVECP da, db, dr; ArrayType *r;
int i; BITVECP da,
db,
dr;
int i;
if ( ARRISNULL( a ) || ARRISNULL( b ) ) return new_intArrayType(0); if (ARRISNULL(a) || ARRISNULL(b))
return new_intArrayType(0);
r = new_intArrayType( SIGLENINT ); r = new_intArrayType(SIGLENINT);
da = SIGPTR(a); da = SIGPTR(a);
db = SIGPTR(b); db = SIGPTR(b);
...@@ -885,16 +1018,19 @@ _intbig_inter(ArrayType *a, ArrayType *b) { ...@@ -885,16 +1018,19 @@ _intbig_inter(ArrayType *a, ArrayType *b) {
LOOPBYTE(dr[i] = da[i] & db[i]); LOOPBYTE(dr[i] = da[i] & db[i]);
return r; return r;
} }
bool * bool *
g_intbig_same(ArrayType *a, ArrayType *b, bool *result) { g_intbig_same(ArrayType *a, ArrayType *b, bool *result)
BITVECP da, db; {
int i; BITVECP da,
db;
int i;
if ( ARRISNULL( a ) || ARRISNULL( b ) ) { if (ARRISNULL(a) || ARRISNULL(b))
*result = ( ARRISNULL( a ) && ARRISNULL( b ) ) ? TRUE : FALSE; {
*result = (ARRISNULL(a) && ARRISNULL(b)) ? TRUE : FALSE;
return result; return result;
} }
...@@ -902,121 +1038,141 @@ g_intbig_same(ArrayType *a, ArrayType *b, bool *result) { ...@@ -902,121 +1038,141 @@ g_intbig_same(ArrayType *a, ArrayType *b, bool *result) {
db = SIGPTR(b); db = SIGPTR(b);
LOOPBYTE( LOOPBYTE(
if ( da[i] != db[i] ) { if (da[i] != db[i])
*result = FALSE; {
return result; *result = FALSE;
} return result;
}
); );
*result = TRUE; *result = TRUE;
return result; return result;
} }
GISTENTRY * GISTENTRY *
g_intbig_compress(GISTENTRY *entry) { g_intbig_compress(GISTENTRY *entry)
GISTENTRY *retval; {
ArrayType *r, *in; GISTENTRY *retval;
ArrayType *r,
*in;
#ifdef PGSQL71 #ifdef PGSQL71
if ( entry->pred ) if (entry->pred)
in = (ArrayType *)PG_DETOAST_DATUM( entry->pred ); in = (ArrayType *) PG_DETOAST_DATUM(entry->pred);
else else
in = NULL; in = NULL;
#else #else
in = (ArrayType *) entry->pred; in = (ArrayType *) entry->pred;
#endif #endif
if ( ! entry->leafkey ) return entry; if (!entry->leafkey)
return entry;
retval = palloc(sizeof(GISTENTRY)); retval = palloc(sizeof(GISTENTRY));
if ( ARRISNULL( in ) ) { if (ARRISNULL(in))
#ifdef PGSQL71 {
if ( in ) if ( (char*)in != (char*)entry->pred ) pfree(in); #ifdef PGSQL71
if (in)
if ((char *) in != (char *) entry->pred)
pfree(in);
#endif #endif
gistentryinit(*retval, (char *)NULL, entry->rel, entry->page, entry->offset,0, FALSE); gistentryinit(*retval, (char *) NULL, entry->rel, entry->page, entry->offset, 0, FALSE);
return( retval ); return (retval);
} }
r = new_intArrayType( SIGLENINT ); r = new_intArrayType(SIGLENINT);
gensign( SIGPTR( r ), gensign(SIGPTR(r),
ARRPTR ( in ), ARRPTR(in),
ARRNELEMS( in ) ); ARRNELEMS(in));
gistentryinit(*retval, (char *)r, entry->rel, entry->page, entry->offset, VARSIZE( r ), FALSE); gistentryinit(*retval, (char *) r, entry->rel, entry->page, entry->offset, VARSIZE(r), FALSE);
#ifdef PGSQL71 #ifdef PGSQL71
if ( in ) if ( (char*)in != (char*)entry->pred ) pfree(in); if (in)
if ((char *) in != (char *) entry->pred)
pfree(in);
#endif #endif
return(retval); return (retval);
} }
GISTENTRY * GISTENTRY *
g_intbig_decompress(GISTENTRY *entry) { g_intbig_decompress(GISTENTRY *entry)
{
#ifdef PGSQL71 #ifdef PGSQL71
ArrayType *key; ArrayType *key;
key = (ArrayType *)PG_DETOAST_DATUM( entry->pred );
if ( (char*)key != (char*)entry->pred ) { key = (ArrayType *) PG_DETOAST_DATUM(entry->pred);
GISTENTRY *retval; if ((char *) key != (char *) entry->pred)
{
GISTENTRY *retval;
retval = palloc(sizeof(GISTENTRY)); retval = palloc(sizeof(GISTENTRY));
gistentryinit(*retval, (char *)key, entry->rel, entry->page, entry->offset, VARSIZE( key ), FALSE); gistentryinit(*retval, (char *) key, entry->rel, entry->page, entry->offset, VARSIZE(key), FALSE);
return retval; return retval;
} }
#endif #endif
return entry; return entry;
} }
GIST_SPLITVEC * GIST_SPLITVEC *
g_intbig_picksplit(bytea *entryvec, GIST_SPLITVEC *v) { g_intbig_picksplit(bytea *entryvec, GIST_SPLITVEC *v)
return _int_common_picksplit( entryvec, v, {
_intbig_union, return _int_common_picksplit(entryvec, v,
_intbig_inter, _intbig_union,
rt__intbig_size, _intbig_inter,
1.0); rt__intbig_size,
1.0);
} }
ArrayType * ArrayType *
g_intbig_union(bytea *entryvec, int *sizep) { g_intbig_union(bytea *entryvec, int *sizep)
return _int_common_union( entryvec, sizep, _intbig_union ); {
return _int_common_union(entryvec, sizep, _intbig_union);
} }
float * float *
g_intbig_penalty(GISTENTRY *origentry, GISTENTRY *newentry, float *result){ g_intbig_penalty(GISTENTRY *origentry, GISTENTRY *newentry, float *result)
_int_common_penalty( origentry, newentry, result, _intbig_union, rt__intbig_size); {
return result; _int_common_penalty(origentry, newentry, result, _intbig_union, rt__intbig_size);
return result;
} }
bool bool
g_intbig_consistent(GISTENTRY *entry, ArrayType *query, StrategyNumber strategy) { g_intbig_consistent(GISTENTRY *entry, ArrayType *query, StrategyNumber strategy)
bool retval; {
ArrayType * q; bool retval;
ArrayType *q;
/* XXX what about toasted input? */ /* XXX what about toasted input? */
if ( ARRISNULL( query ) ) return FALSE; if (ARRISNULL(query))
return FALSE;
q = new_intArrayType( SIGLENINT );
gensign( SIGPTR( q ), q = new_intArrayType(SIGLENINT);
ARRPTR( query ), gensign(SIGPTR(q),
ARRNELEMS( query ) ); ARRPTR(query),
ARRNELEMS(query));
switch(strategy) {
case RTOverlapStrategyNumber: switch (strategy)
retval = (bool)_intbig_overlap((ArrayType *)(entry->pred), q); {
break; case RTOverlapStrategyNumber:
case RTSameStrategyNumber: retval = (bool) _intbig_overlap((ArrayType *) (entry->pred), q);
case RTContainsStrategyNumber: break;
retval = (bool)_intbig_contains((ArrayType *)(entry->pred), q); case RTSameStrategyNumber:
break; case RTContainsStrategyNumber:
case RTContainedByStrategyNumber: retval = (bool) _intbig_contains((ArrayType *) (entry->pred), q);
retval = (bool)_intbig_overlap((ArrayType *)(entry->pred), q); break;
break; case RTContainedByStrategyNumber:
default: retval = (bool) _intbig_overlap((ArrayType *) (entry->pred), q);
retval = FALSE; break;
} default:
pfree( q ); retval = FALSE;
return(retval); }
pfree(q);
return (retval);
} }
/***************************************************************** /*****************************************************************
...@@ -1027,37 +1183,43 @@ g_intbig_consistent(GISTENTRY *entry, ArrayType *query, StrategyNumber strategy) ...@@ -1027,37 +1183,43 @@ g_intbig_consistent(GISTENTRY *entry, ArrayType *query, StrategyNumber strategy)
** The GiST Union method for _intments ** The GiST Union method for _intments
** returns the minimal set that encloses all the entries in entryvec ** returns the minimal set that encloses all the entries in entryvec
*/ */
ArrayType * ArrayType *
_int_common_union(bytea *entryvec, int *sizep, formarray unionf) { _int_common_union(bytea *entryvec, int *sizep, formarray unionf)
int numranges, i; {
ArrayType *out = (ArrayType *)NULL; int numranges,
ArrayType *tmp; i;
ArrayType *out = (ArrayType *) NULL;
ArrayType *tmp;
#ifdef GIST_DEBUG #ifdef GIST_DEBUG
elog(NOTICE, "_int_common_union in"); elog(NOTICE, "_int_common_union in");
#endif #endif
numranges = (VARSIZE(entryvec) - VARHDRSZ)/sizeof(GISTENTRY); numranges = (VARSIZE(entryvec) - VARHDRSZ) / sizeof(GISTENTRY);
tmp = (ArrayType *)(((GISTENTRY *)(VARDATA(entryvec)))[0]).pred; tmp = (ArrayType *) (((GISTENTRY *) (VARDATA(entryvec)))[0]).pred;
for (i = 1; i < numranges; i++) { for (i = 1; i < numranges; i++)
out = (*unionf)(tmp, (ArrayType *) {
(((GISTENTRY *)(VARDATA(entryvec)))[i]).pred); out = (*unionf) (tmp, (ArrayType *)
if (i > 1 && tmp) pfree(tmp); (((GISTENTRY *) (VARDATA(entryvec)))[i]).pred);
tmp = out; if (i > 1 && tmp)
} pfree(tmp);
tmp = out;
*sizep = VARSIZE( out ); }
if ( *sizep == 0 ) {
pfree( out ); *sizep = VARSIZE(out);
if (*sizep == 0)
{
pfree(out);
#ifdef GIST_DEBUG #ifdef GIST_DEBUG
elog(NOTICE, "_int_common_union out1"); elog(NOTICE, "_int_common_union out1");
#endif #endif
return NULL; return NULL;
} }
#ifdef GIST_DEBUG #ifdef GIST_DEBUG
elog(NOTICE, "_int_common_union out"); elog(NOTICE, "_int_common_union out");
#endif #endif
return(out); return (out);
} }
...@@ -1067,181 +1229,210 @@ _int_common_union(bytea *entryvec, int *sizep, formarray unionf) { ...@@ -1067,181 +1229,210 @@ _int_common_union(bytea *entryvec, int *sizep, formarray unionf) {
float * float *
_int_common_penalty(GISTENTRY *origentry, GISTENTRY *newentry, float *result, _int_common_penalty(GISTENTRY *origentry, GISTENTRY *newentry, float *result,
formarray unionf, formarray unionf,
formfloat sizef) formfloat sizef)
{ {
Datum ud; Datum ud;
float tmp1, tmp2; float tmp1,
tmp2;
#ifdef GIST_DEBUG #ifdef GIST_DEBUG
elog(NOTICE, "penalty"); elog(NOTICE, "penalty");
#endif #endif
ud = (Datum)(*unionf)((ArrayType *)(origentry->pred), (ArrayType *)(newentry->pred)); ud = (Datum) (*unionf) ((ArrayType *) (origentry->pred), (ArrayType *) (newentry->pred));
(*sizef)((ArrayType *)ud, &tmp1); (*sizef) ((ArrayType *) ud, &tmp1);
(*sizef)((ArrayType *)(origentry->pred), &tmp2); (*sizef) ((ArrayType *) (origentry->pred), &tmp2);
*result = tmp1 - tmp2; *result = tmp1 - tmp2;
pfree((char *)ud); pfree((char *) ud);
#ifdef GIST_DEBUG #ifdef GIST_DEBUG
elog(NOTICE, "--penalty\t%g", *result); elog(NOTICE, "--penalty\t%g", *result);
#endif #endif
return(result); return (result);
} }
/* /*
** The GiST PickSplit method for _intments ** The GiST PickSplit method for _intments
** We use Guttman's poly time split algorithm ** We use Guttman's poly time split algorithm
*/ */
GIST_SPLITVEC * GIST_SPLITVEC *
_int_common_picksplit(bytea *entryvec, _int_common_picksplit(bytea *entryvec,
GIST_SPLITVEC *v, GIST_SPLITVEC *v,
formarray unionf, formarray unionf,
formarray interf, formarray interf,
formfloat sizef, formfloat sizef,
float coef) float coef)
{ {
OffsetNumber i, j; OffsetNumber i,
ArrayType *datum_alpha, *datum_beta; j;
ArrayType *datum_l, *datum_r; ArrayType *datum_alpha,
ArrayType *union_d, *union_dl, *union_dr; *datum_beta;
ArrayType *inter_d; ArrayType *datum_l,
bool firsttime; *datum_r;
float size_alpha, size_beta, size_union, size_inter; ArrayType *union_d,
float size_waste, waste; *union_dl,
float size_l, size_r; *union_dr;
int nbytes; ArrayType *inter_d;
OffsetNumber seed_1 = 0, seed_2 = 0; bool firsttime;
OffsetNumber *left, *right; float size_alpha,
OffsetNumber maxoff; size_beta,
size_union,
size_inter;
float size_waste,
waste;
float size_l,
size_r;
int nbytes;
OffsetNumber seed_1 = 0,
seed_2 = 0;
OffsetNumber *left,
*right;
OffsetNumber maxoff;
#ifdef GIST_DEBUG #ifdef GIST_DEBUG
elog(NOTICE, "--------picksplit %d",(VARSIZE(entryvec) - VARHDRSZ)/sizeof(GISTENTRY)); elog(NOTICE, "--------picksplit %d", (VARSIZE(entryvec) - VARHDRSZ) / sizeof(GISTENTRY));
#endif #endif
maxoff = ((VARSIZE(entryvec) - VARHDRSZ)/sizeof(GISTENTRY)) - 2; maxoff = ((VARSIZE(entryvec) - VARHDRSZ) / sizeof(GISTENTRY)) - 2;
nbytes = (maxoff + 2) * sizeof(OffsetNumber); nbytes = (maxoff + 2) * sizeof(OffsetNumber);
v->spl_left = (OffsetNumber *) palloc(nbytes); v->spl_left = (OffsetNumber *) palloc(nbytes);
v->spl_right = (OffsetNumber *) palloc(nbytes); v->spl_right = (OffsetNumber *) palloc(nbytes);
firsttime = true; firsttime = true;
waste = 0.0; waste = 0.0;
for (i = FirstOffsetNumber; i < maxoff; i = OffsetNumberNext(i)) { for (i = FirstOffsetNumber; i < maxoff; i = OffsetNumberNext(i))
datum_alpha = (ArrayType *)(((GISTENTRY *)(VARDATA(entryvec)))[i].pred); {
for (j = OffsetNumberNext(i); j <= maxoff; j = OffsetNumberNext(j)) { datum_alpha = (ArrayType *) (((GISTENTRY *) (VARDATA(entryvec)))[i].pred);
datum_beta = (ArrayType *)(((GISTENTRY *)(VARDATA(entryvec)))[j].pred); for (j = OffsetNumberNext(i); j <= maxoff; j = OffsetNumberNext(j))
{
/* compute the wasted space by unioning these guys */ datum_beta = (ArrayType *) (((GISTENTRY *) (VARDATA(entryvec)))[j].pred);
/* size_waste = size_union - size_inter; */
union_d = (*unionf)(datum_alpha, datum_beta); /* compute the wasted space by unioning these guys */
(*sizef)(union_d, &size_union); /* size_waste = size_union - size_inter; */
inter_d = (*interf)(datum_alpha, datum_beta); union_d = (*unionf) (datum_alpha, datum_beta);
(*sizef)(inter_d, &size_inter); (*sizef) (union_d, &size_union);
size_waste = size_union - size_inter; inter_d = (*interf) (datum_alpha, datum_beta);
(*sizef) (inter_d, &size_inter);
pfree(union_d); size_waste = size_union - size_inter;
if (inter_d != (ArrayType *) NULL) pfree(union_d);
pfree(inter_d);
if (inter_d != (ArrayType *) NULL)
/* pfree(inter_d);
* are these a more promising split that what we've
* already seen? /*
*/ * are these a more promising split that what we've already
* seen?
if (size_waste > waste || firsttime) { */
waste = size_waste;
seed_1 = i; if (size_waste > waste || firsttime)
seed_2 = j; {
firsttime = false; waste = size_waste;
} seed_1 = i;
seed_2 = j;
firsttime = false;
}
}
} }
}
left = v->spl_left;
left = v->spl_left; v->spl_nleft = 0;
v->spl_nleft = 0; right = v->spl_right;
right = v->spl_right; v->spl_nright = 0;
v->spl_nright = 0;
datum_alpha = (ArrayType *) (((GISTENTRY *) (VARDATA(entryvec)))[seed_1].pred);
datum_alpha = (ArrayType *)(((GISTENTRY *)(VARDATA(entryvec)))[seed_1].pred); datum_l = copy_intArrayType(datum_alpha);
datum_l = copy_intArrayType( datum_alpha ); (*sizef) ((ArrayType *) datum_l, &size_l);
(*sizef)((ArrayType *)datum_l, &size_l); datum_beta = (ArrayType *) (((GISTENTRY *) (VARDATA(entryvec)))[seed_2].pred);
datum_beta = (ArrayType *)(((GISTENTRY *)(VARDATA(entryvec)))[seed_2].pred); datum_r = copy_intArrayType(datum_beta);
datum_r = copy_intArrayType( datum_beta ); (*sizef) ((ArrayType *) datum_r, &size_r);
(*sizef)((ArrayType *)datum_r, &size_r);
/*
* Now split up the regions between the two seeds. An important
* property of this split algorithm is that the split vector v
* has the indices of items to be split in order in its left and
* right vectors. We exploit this property by doing a merge in
* the code that actually splits the page.
*
* For efficiency, we also place the new index tuple in this loop.
* This is handled at the very end, when we have placed all the
* existing tuples and i == maxoff + 1.
*/
maxoff = OffsetNumberNext(maxoff);
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i)) {
/* /*
* If we've already decided where to place this item, just * Now split up the regions between the two seeds. An important
* put it on the right list. Otherwise, we need to figure * property of this split algorithm is that the split vector v has the
* out which page needs the least enlargement in order to * indices of items to be split in order in its left and right
* store the item. * vectors. We exploit this property by doing a merge in the code
* that actually splits the page.
*
* For efficiency, we also place the new index tuple in this loop. This
* is handled at the very end, when we have placed all the existing
* tuples and i == maxoff + 1.
*/ */
if (i == seed_1) { maxoff = OffsetNumberNext(maxoff);
*left++ = i; for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
v->spl_nleft++; {
continue;
} else if (i == seed_2) {
*right++ = i; /*
v->spl_nright++; * If we've already decided where to place this item, just put it
continue; * on the right list. Otherwise, we need to figure out which page
} * needs the least enlargement in order to store the item.
*/
/* okay, which page needs least enlargement? */
datum_alpha = (ArrayType *)(((GISTENTRY *)(VARDATA(entryvec)))[i].pred); if (i == seed_1)
union_dl = (ArrayType *)(*unionf)(datum_l, datum_alpha); {
union_dr = (ArrayType *)(*unionf)(datum_r, datum_alpha); *left++ = i;
(*sizef)((ArrayType *)union_dl, &size_alpha); v->spl_nleft++;
(*sizef)((ArrayType *)union_dr, &size_beta); continue;
}
/* pick which page to add it to */ else if (i == seed_2)
if (size_alpha - size_l < size_beta - size_r + WISH_F(v->spl_nleft, v->spl_nright, coef)) { {
if ( datum_l ) pfree(datum_l); *right++ = i;
if ( union_dr ) pfree(union_dr); v->spl_nright++;
datum_l = union_dl; continue;
size_l = size_alpha; }
*left++ = i;
v->spl_nleft++; /* okay, which page needs least enlargement? */
} else { datum_alpha = (ArrayType *) (((GISTENTRY *) (VARDATA(entryvec)))[i].pred);
if ( datum_r ) pfree(datum_r); union_dl = (ArrayType *) (*unionf) (datum_l, datum_alpha);
if ( union_dl ) pfree(union_dl); union_dr = (ArrayType *) (*unionf) (datum_r, datum_alpha);
datum_r = union_dr; (*sizef) ((ArrayType *) union_dl, &size_alpha);
size_r = size_beta; (*sizef) ((ArrayType *) union_dr, &size_beta);
*right++ = i;
v->spl_nright++; /* pick which page to add it to */
if (size_alpha - size_l < size_beta - size_r + WISH_F(v->spl_nleft, v->spl_nright, coef))
{
if (datum_l)
pfree(datum_l);
if (union_dr)
pfree(union_dr);
datum_l = union_dl;
size_l = size_alpha;
*left++ = i;
v->spl_nleft++;
}
else
{
if (datum_r)
pfree(datum_r);
if (union_dl)
pfree(union_dl);
datum_r = union_dr;
size_r = size_beta;
*right++ = i;
v->spl_nright++;
}
} }
}
if ( *(left-1) > *(right-1) ) { if (*(left - 1) > *(right - 1))
*right = FirstOffsetNumber; {
*(left-1) = InvalidOffsetNumber; *right = FirstOffsetNumber;
} else { *(left - 1) = InvalidOffsetNumber;
*left = FirstOffsetNumber; }
*(right-1) = InvalidOffsetNumber; else
} {
*left = FirstOffsetNumber;
*(right - 1) = InvalidOffsetNumber;
}
v->spl_ldatum = (char *)datum_l; v->spl_ldatum = (char *) datum_l;
v->spl_rdatum = (char *)datum_r; v->spl_rdatum = (char *) datum_r;
#ifdef GIST_DEBUG #ifdef GIST_DEBUG
elog(NOTICE, "--------ENDpicksplit %d %d",v->spl_nleft, v->spl_nright); elog(NOTICE, "--------ENDpicksplit %d %d", v->spl_nleft, v->spl_nright);
#endif #endif
return v; return v;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment