2008-05-12 08:00:54 +08:00
|
|
|
/*
|
2009-10-01 03:50:22 +08:00
|
|
|
* $PostgreSQL: pgsql/contrib/hstore/hstore_gist.c,v 1.11 2009/09/30 19:50:22 tgl Exp $
|
2008-05-12 08:00:54 +08:00
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
#include "access/gist.h"
|
|
|
|
#include "access/itup.h"
|
2008-05-12 08:00:54 +08:00
|
|
|
#include "access/skey.h"
|
2009-10-01 03:50:22 +08:00
|
|
|
#include "catalog/pg_type.h"
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2009-10-01 03:50:22 +08:00
|
|
|
#include "crc32.h"
|
2008-05-12 08:00:54 +08:00
|
|
|
#include "hstore.h"
|
|
|
|
|
2006-09-06 02:00:58 +08:00
|
|
|
/* bigint defines */
|
|
|
|
#define BITBYTE 8
|
2006-10-04 08:30:14 +08:00
|
|
|
#define SIGLENINT 4 /* >122 => key will toast, so very slow!!! */
|
|
|
|
#define SIGLEN ( sizeof(int)*SIGLENINT )
|
2006-09-06 02:00:58 +08:00
|
|
|
#define SIGLENBIT (SIGLEN*BITBYTE)
|
|
|
|
|
|
|
|
typedef char BITVEC[SIGLEN];
|
|
|
|
typedef char *BITVECP;
|
|
|
|
|
|
|
|
#define SIGPTR(x) ( (BITVECP) ARR_DATA_PTR(x) )
|
|
|
|
|
|
|
|
|
2007-11-16 08:13:02 +08:00
|
|
|
#define LOOPBYTE \
|
|
|
|
for(i=0;i<SIGLEN;i++)
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2007-11-16 08:13:02 +08:00
|
|
|
#define LOOPBIT \
|
|
|
|
for(i=0;i<SIGLENBIT;i++)
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
/* beware of multiple evaluation of arguments to these macros! */
|
|
|
|
#define GETBYTE(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITBYTE ) ) )
|
|
|
|
#define GETBITBYTE(x,i) ( (*((char*)(x)) >> (i)) & 0x01 )
|
|
|
|
#define CLRBIT(x,i) GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITBYTE ) )
|
|
|
|
#define SETBIT(x,i) GETBYTE(x,i) |= ( 0x01 << ( (i) % BITBYTE ) )
|
|
|
|
#define GETBIT(x,i) ( (GETBYTE(x,i) >> ( (i) % BITBYTE )) & 0x01 )
|
|
|
|
#define HASHVAL(val) (((unsigned int)(val)) % SIGLENBIT)
|
|
|
|
#define HASH(sign, val) SETBIT((sign), HASHVAL(val))
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
2007-03-01 06:44:38 +08:00
|
|
|
int32 vl_len_; /* varlena header (do not touch directly!) */
|
2006-10-04 08:30:14 +08:00
|
|
|
int4 flag;
|
|
|
|
char data[1];
|
2009-06-11 22:49:15 +08:00
|
|
|
} GISTTYPE;
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
#define ALLISTRUE 0x04
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
#define ISALLTRUE(x) ( ((GISTTYPE*)x)->flag & ALLISTRUE )
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2007-03-01 06:44:38 +08:00
|
|
|
#define GTHDRSIZE (VARHDRSZ + sizeof(int4))
|
2006-09-06 02:00:58 +08:00
|
|
|
#define CALCGTSIZE(flag) ( GTHDRSIZE+(((flag) & ALLISTRUE) ? 0 : SIGLEN) )
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
#define GETSIGN(x) ( (BITVECP)( (char*)x+GTHDRSIZE ) )
|
|
|
|
|
|
|
|
#define SUMBIT(val) ( \
|
2006-09-06 02:00:58 +08:00
|
|
|
GETBITBYTE((val),0) + \
|
|
|
|
GETBITBYTE((val),1) + \
|
|
|
|
GETBITBYTE((val),2) + \
|
|
|
|
GETBITBYTE((val),3) + \
|
|
|
|
GETBITBYTE((val),4) + \
|
|
|
|
GETBITBYTE((val),5) + \
|
|
|
|
GETBITBYTE((val),6) + \
|
|
|
|
GETBITBYTE((val),7) \
|
|
|
|
)
|
|
|
|
|
|
|
|
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
|
|
|
|
|
|
|
|
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(ghstore_in);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum ghstore_in(PG_FUNCTION_ARGS);
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(ghstore_out);
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum ghstore_out(PG_FUNCTION_ARGS);
|
|
|
|
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
ghstore_in(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2006-09-06 02:00:58 +08:00
|
|
|
elog(ERROR, "Not implemented");
|
|
|
|
PG_RETURN_DATUM(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
ghstore_out(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2006-09-06 02:00:58 +08:00
|
|
|
elog(ERROR, "Not implemented");
|
|
|
|
PG_RETURN_DATUM(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(ghstore_consistent);
|
|
|
|
PG_FUNCTION_INFO_V1(ghstore_compress);
|
|
|
|
PG_FUNCTION_INFO_V1(ghstore_decompress);
|
|
|
|
PG_FUNCTION_INFO_V1(ghstore_penalty);
|
|
|
|
PG_FUNCTION_INFO_V1(ghstore_picksplit);
|
|
|
|
PG_FUNCTION_INFO_V1(ghstore_union);
|
|
|
|
PG_FUNCTION_INFO_V1(ghstore_same);
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum ghstore_consistent(PG_FUNCTION_ARGS);
|
|
|
|
Datum ghstore_compress(PG_FUNCTION_ARGS);
|
|
|
|
Datum ghstore_decompress(PG_FUNCTION_ARGS);
|
|
|
|
Datum ghstore_penalty(PG_FUNCTION_ARGS);
|
|
|
|
Datum ghstore_picksplit(PG_FUNCTION_ARGS);
|
|
|
|
Datum ghstore_union(PG_FUNCTION_ARGS);
|
|
|
|
Datum ghstore_same(PG_FUNCTION_ARGS);
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
ghstore_compress(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2006-09-06 02:00:58 +08:00
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
|
|
|
GISTENTRY *retval = entry;
|
2006-10-04 08:30:14 +08:00
|
|
|
|
|
|
|
if (entry->leafkey)
|
|
|
|
{
|
2007-03-01 06:44:38 +08:00
|
|
|
GISTTYPE *res = (GISTTYPE *) palloc0(CALCGTSIZE(0));
|
2009-10-01 03:50:22 +08:00
|
|
|
HStore *val = DatumGetHStoreP(entry->key);
|
|
|
|
HEntry *hsent = ARRPTR(val);
|
|
|
|
char *ptr = STRPTR(val);
|
|
|
|
int count = HS_COUNT(val);
|
|
|
|
int i;
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2007-03-01 06:44:38 +08:00
|
|
|
SET_VARSIZE(res, CALCGTSIZE(0));
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2009-10-01 03:50:22 +08:00
|
|
|
for (i = 0; i < count; ++i)
|
2006-10-04 08:30:14 +08:00
|
|
|
{
|
2009-10-01 03:50:22 +08:00
|
|
|
int h;
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2009-10-01 03:50:22 +08:00
|
|
|
h = crc32_sz((char *) HS_KEY(hsent,ptr,i), HS_KEYLEN(hsent,i));
|
2006-10-04 08:30:14 +08:00
|
|
|
HASH(GETSIGN(res), h);
|
2009-10-01 03:50:22 +08:00
|
|
|
if (!HS_VALISNULL(hsent,i))
|
2006-10-04 08:30:14 +08:00
|
|
|
{
|
2009-10-01 03:50:22 +08:00
|
|
|
h = crc32_sz((char *) HS_VAL(hsent,ptr,i), HS_VALLEN(hsent,i));
|
2006-10-04 08:30:14 +08:00
|
|
|
HASH(GETSIGN(res), h);
|
2006-09-06 02:00:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
|
|
|
|
gistentryinit(*retval, PointerGetDatum(res),
|
|
|
|
entry->rel, entry->page,
|
|
|
|
entry->offset,
|
|
|
|
FALSE);
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else if (!ISALLTRUE(DatumGetPointer(entry->key)))
|
|
|
|
{
|
|
|
|
int4 i;
|
2006-09-06 02:00:58 +08:00
|
|
|
GISTTYPE *res;
|
2006-10-04 08:30:14 +08:00
|
|
|
BITVECP sign = GETSIGN(DatumGetPointer(entry->key));
|
|
|
|
|
2007-11-16 08:13:02 +08:00
|
|
|
LOOPBYTE
|
|
|
|
{
|
2007-11-16 09:12:24 +08:00
|
|
|
if ((sign[i] & 0xff) != 0xff)
|
|
|
|
PG_RETURN_POINTER(retval);
|
2007-11-16 08:13:02 +08:00
|
|
|
}
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
res = (GISTTYPE *) palloc(CALCGTSIZE(ALLISTRUE));
|
2007-03-01 06:44:38 +08:00
|
|
|
SET_VARSIZE(res, CALCGTSIZE(ALLISTRUE));
|
2006-09-06 02:00:58 +08:00
|
|
|
res->flag = ALLISTRUE;
|
|
|
|
|
|
|
|
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
|
|
|
|
gistentryinit(*retval, PointerGetDatum(res),
|
|
|
|
entry->rel, entry->page,
|
2006-10-04 08:30:14 +08:00
|
|
|
entry->offset,
|
2006-09-06 02:00:58 +08:00
|
|
|
FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
ghstore_decompress(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-04-06 12:21:44 +08:00
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
|
|
|
GISTENTRY *retval;
|
2007-11-16 09:12:24 +08:00
|
|
|
HStore *key;
|
2007-04-06 12:21:44 +08:00
|
|
|
|
2009-10-01 03:50:22 +08:00
|
|
|
key = DatumGetHStoreP(entry->key);
|
2007-04-06 12:21:44 +08:00
|
|
|
|
|
|
|
if (key != (HStore *) DatumGetPointer(entry->key))
|
|
|
|
{
|
|
|
|
/* need to pass back the decompressed item */
|
|
|
|
retval = palloc(sizeof(GISTENTRY));
|
|
|
|
gistentryinit(*retval, PointerGetDatum(key),
|
|
|
|
entry->rel, entry->page, entry->offset, entry->leafkey);
|
|
|
|
PG_RETURN_POINTER(retval);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* we can return the entry as-is */
|
|
|
|
PG_RETURN_POINTER(entry);
|
|
|
|
}
|
2006-09-06 02:00:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
ghstore_same(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2006-09-06 02:00:58 +08:00
|
|
|
GISTTYPE *a = (GISTTYPE *) PG_GETARG_POINTER(0);
|
|
|
|
GISTTYPE *b = (GISTTYPE *) PG_GETARG_POINTER(1);
|
2006-10-04 08:30:14 +08:00
|
|
|
bool *result = (bool *) PG_GETARG_POINTER(2);
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
if (ISALLTRUE(a) && ISALLTRUE(b))
|
|
|
|
*result = true;
|
|
|
|
else if (ISALLTRUE(a))
|
|
|
|
*result = false;
|
|
|
|
else if (ISALLTRUE(b))
|
|
|
|
*result = false;
|
2006-10-04 08:30:14 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
int4 i;
|
|
|
|
BITVECP sa = GETSIGN(a),
|
|
|
|
sb = GETSIGN(b);
|
|
|
|
|
2006-09-06 02:00:58 +08:00
|
|
|
*result = true;
|
2007-11-16 08:13:02 +08:00
|
|
|
LOOPBYTE
|
|
|
|
{
|
|
|
|
if (sa[i] != sb[i])
|
|
|
|
{
|
|
|
|
*result = false;
|
|
|
|
break;
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
2006-09-06 02:00:58 +08:00
|
|
|
}
|
|
|
|
PG_RETURN_POINTER(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int4
|
2006-10-04 08:30:14 +08:00
|
|
|
sizebitvec(BITVECP sign)
|
|
|
|
{
|
|
|
|
int4 size = 0,
|
|
|
|
i;
|
|
|
|
|
2007-11-16 08:13:02 +08:00
|
|
|
LOOPBYTE
|
|
|
|
{
|
|
|
|
size += SUMBIT(sign);
|
|
|
|
sign = (BITVECP) (((char *) sign) + 1);
|
|
|
|
}
|
2006-09-06 02:00:58 +08:00
|
|
|
return size;
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2006-09-06 02:00:58 +08:00
|
|
|
static int
|
2006-10-04 08:30:14 +08:00
|
|
|
hemdistsign(BITVECP a, BITVECP b)
|
|
|
|
{
|
|
|
|
int i,
|
|
|
|
dist = 0;
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2007-11-16 08:13:02 +08:00
|
|
|
LOOPBIT
|
|
|
|
{
|
|
|
|
if (GETBIT(a, i) != GETBIT(b, i))
|
2006-09-06 02:00:58 +08:00
|
|
|
dist++;
|
2007-11-16 08:13:02 +08:00
|
|
|
}
|
2006-09-06 02:00:58 +08:00
|
|
|
return dist;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-06-11 22:49:15 +08:00
|
|
|
hemdist(GISTTYPE *a, GISTTYPE *b)
|
2006-10-04 08:30:14 +08:00
|
|
|
{
|
|
|
|
if (ISALLTRUE(a))
|
|
|
|
{
|
2006-09-06 02:00:58 +08:00
|
|
|
if (ISALLTRUE(b))
|
|
|
|
return 0;
|
|
|
|
else
|
2006-10-04 08:30:14 +08:00
|
|
|
return SIGLENBIT - sizebitvec(GETSIGN(b));
|
|
|
|
}
|
|
|
|
else if (ISALLTRUE(b))
|
|
|
|
return SIGLENBIT - sizebitvec(GETSIGN(a));
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return hemdistsign(GETSIGN(a), GETSIGN(b));
|
2006-09-06 02:00:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int4
|
2009-06-11 22:49:15 +08:00
|
|
|
unionkey(BITVECP sbase, GISTTYPE *add)
|
2006-09-06 02:00:58 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
int4 i;
|
|
|
|
BITVECP sadd = GETSIGN(add);
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
if (ISALLTRUE(add))
|
|
|
|
return 1;
|
2007-11-16 08:13:02 +08:00
|
|
|
LOOPBYTE
|
2007-11-16 09:12:24 +08:00
|
|
|
sbase[i] |= sadd[i];
|
2006-09-06 02:00:58 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
ghstore_union(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
|
|
|
|
int4 len = entryvec->n;
|
|
|
|
|
|
|
|
int *size = (int *) PG_GETARG_POINTER(1);
|
|
|
|
BITVEC base;
|
|
|
|
int4 i;
|
|
|
|
int4 flag = 0;
|
2006-09-06 02:00:58 +08:00
|
|
|
GISTTYPE *result;
|
|
|
|
|
|
|
|
MemSet((void *) base, 0, sizeof(BITVEC));
|
2006-10-04 08:30:14 +08:00
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
if (unionkey(base, GETENTRY(entryvec, i)))
|
|
|
|
{
|
2006-09-06 02:00:58 +08:00
|
|
|
flag = ALLISTRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
len = CALCGTSIZE(flag);
|
|
|
|
result = (GISTTYPE *) palloc(len);
|
2007-03-01 06:44:38 +08:00
|
|
|
SET_VARSIZE(result, len);
|
2006-09-06 02:00:58 +08:00
|
|
|
result->flag = flag;
|
|
|
|
if (!ISALLTRUE(result))
|
|
|
|
memcpy((void *) GETSIGN(result), (void *) base, sizeof(BITVEC));
|
2007-03-01 06:44:38 +08:00
|
|
|
*size = len;
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
ghstore_penalty(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2006-09-06 02:00:58 +08:00
|
|
|
GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0); /* always ISSIGNKEY */
|
|
|
|
GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
|
2006-10-04 08:30:14 +08:00
|
|
|
float *penalty = (float *) PG_GETARG_POINTER(2);
|
2006-09-06 02:00:58 +08:00
|
|
|
GISTTYPE *origval = (GISTTYPE *) DatumGetPointer(origentry->key);
|
|
|
|
GISTTYPE *newval = (GISTTYPE *) DatumGetPointer(newentry->key);
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
*penalty = hemdist(origval, newval);
|
2006-09-06 02:00:58 +08:00
|
|
|
PG_RETURN_POINTER(penalty);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
2006-09-06 02:00:58 +08:00
|
|
|
OffsetNumber pos;
|
2006-10-04 08:30:14 +08:00
|
|
|
int4 cost;
|
2007-11-16 09:12:24 +08:00
|
|
|
} SPLITCOST;
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
static int
|
2006-10-04 08:30:14 +08:00
|
|
|
comparecost(const void *a, const void *b)
|
|
|
|
{
|
2006-09-06 02:00:58 +08:00
|
|
|
return ((SPLITCOST *) a)->cost - ((SPLITCOST *) b)->cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
2006-10-04 08:30:14 +08:00
|
|
|
ghstore_picksplit(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
|
2006-09-06 02:00:58 +08:00
|
|
|
OffsetNumber maxoff = entryvec->n - 2;
|
|
|
|
|
|
|
|
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
|
|
|
|
OffsetNumber k,
|
|
|
|
j;
|
2006-10-04 08:30:14 +08:00
|
|
|
GISTTYPE *datum_l,
|
2006-09-06 02:00:58 +08:00
|
|
|
*datum_r;
|
2006-10-04 08:30:14 +08:00
|
|
|
BITVECP union_l,
|
2006-09-06 02:00:58 +08:00
|
|
|
union_r;
|
2006-10-04 08:30:14 +08:00
|
|
|
int4 size_alpha,
|
|
|
|
size_beta;
|
|
|
|
int4 size_waste,
|
2006-09-06 02:00:58 +08:00
|
|
|
waste = -1;
|
2006-10-04 08:30:14 +08:00
|
|
|
int4 nbytes;
|
2006-09-06 02:00:58 +08:00
|
|
|
OffsetNumber seed_1 = 0,
|
|
|
|
seed_2 = 0;
|
|
|
|
OffsetNumber *left,
|
|
|
|
*right;
|
2006-10-04 08:30:14 +08:00
|
|
|
BITVECP ptr;
|
|
|
|
int i;
|
2006-09-06 02:00:58 +08:00
|
|
|
SPLITCOST *costvector;
|
2006-10-04 08:30:14 +08:00
|
|
|
GISTTYPE *_k,
|
2006-09-06 02:00:58 +08:00
|
|
|
*_j;
|
|
|
|
|
|
|
|
nbytes = (maxoff + 2) * sizeof(OffsetNumber);
|
|
|
|
v->spl_left = (OffsetNumber *) palloc(nbytes);
|
|
|
|
v->spl_right = (OffsetNumber *) palloc(nbytes);
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
|
|
|
|
{
|
2006-09-06 02:00:58 +08:00
|
|
|
_k = GETENTRY(entryvec, k);
|
2006-10-04 08:30:14 +08:00
|
|
|
for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
|
|
|
|
{
|
|
|
|
size_waste = hemdist(_k, GETENTRY(entryvec, j));
|
|
|
|
if (size_waste > waste)
|
|
|
|
{
|
2006-09-06 02:00:58 +08:00
|
|
|
waste = size_waste;
|
|
|
|
seed_1 = k;
|
|
|
|
seed_2 = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
left = v->spl_left;
|
|
|
|
v->spl_nleft = 0;
|
|
|
|
right = v->spl_right;
|
|
|
|
v->spl_nright = 0;
|
|
|
|
|
|
|
|
if (seed_1 == 0 || seed_2 == 0)
|
|
|
|
{
|
|
|
|
seed_1 = 1;
|
|
|
|
seed_2 = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* form initial .. */
|
2006-10-04 08:30:14 +08:00
|
|
|
if (ISALLTRUE(GETENTRY(entryvec, seed_1)))
|
|
|
|
{
|
2006-09-06 02:00:58 +08:00
|
|
|
datum_l = (GISTTYPE *) palloc(GTHDRSIZE);
|
2007-03-01 06:44:38 +08:00
|
|
|
SET_VARSIZE(datum_l, GTHDRSIZE);
|
2006-09-06 02:00:58 +08:00
|
|
|
datum_l->flag = ALLISTRUE;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-09-06 02:00:58 +08:00
|
|
|
datum_l = (GISTTYPE *) palloc(GTHDRSIZE + SIGLEN);
|
2007-03-01 06:44:38 +08:00
|
|
|
SET_VARSIZE(datum_l, GTHDRSIZE + SIGLEN);
|
2006-09-06 02:00:58 +08:00
|
|
|
datum_l->flag = 0;
|
|
|
|
memcpy((void *) GETSIGN(datum_l), (void *) GETSIGN(GETENTRY(entryvec, seed_1)), sizeof(BITVEC))
|
2006-10-04 08:30:14 +08:00
|
|
|
;
|
2006-09-06 02:00:58 +08:00
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
if (ISALLTRUE(GETENTRY(entryvec, seed_2)))
|
|
|
|
{
|
2006-09-06 02:00:58 +08:00
|
|
|
datum_r = (GISTTYPE *) palloc(GTHDRSIZE);
|
2007-03-01 06:44:38 +08:00
|
|
|
SET_VARSIZE(datum_r, GTHDRSIZE);
|
2006-09-06 02:00:58 +08:00
|
|
|
datum_r->flag = ALLISTRUE;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-09-06 02:00:58 +08:00
|
|
|
datum_r = (GISTTYPE *) palloc(GTHDRSIZE + SIGLEN);
|
2007-03-01 06:44:38 +08:00
|
|
|
SET_VARSIZE(datum_r, GTHDRSIZE + SIGLEN);
|
2006-09-06 02:00:58 +08:00
|
|
|
datum_r->flag = 0;
|
2006-10-04 08:30:14 +08:00
|
|
|
memcpy((void *) GETSIGN(datum_r), (void *) GETSIGN(GETENTRY(entryvec, seed_2)), sizeof(BITVEC));
|
2006-09-06 02:00:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
maxoff = OffsetNumberNext(maxoff);
|
|
|
|
/* sort before ... */
|
|
|
|
costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
|
|
|
|
for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
|
|
|
|
{
|
|
|
|
costvector[j - 1].pos = j;
|
|
|
|
_j = GETENTRY(entryvec, j);
|
2006-10-04 08:30:14 +08:00
|
|
|
size_alpha = hemdist(datum_l, _j);
|
|
|
|
size_beta = hemdist(datum_r, _j);
|
2006-09-06 02:00:58 +08:00
|
|
|
costvector[j - 1].cost = abs(size_alpha - size_beta);
|
|
|
|
}
|
|
|
|
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
union_l = GETSIGN(datum_l);
|
|
|
|
union_r = GETSIGN(datum_r);
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
for (k = 0; k < maxoff; k++)
|
|
|
|
{
|
2006-09-06 02:00:58 +08:00
|
|
|
j = costvector[k].pos;
|
2006-10-04 08:30:14 +08:00
|
|
|
if (j == seed_1)
|
|
|
|
{
|
2006-09-06 02:00:58 +08:00
|
|
|
*left++ = j;
|
|
|
|
v->spl_nleft++;
|
|
|
|
continue;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else if (j == seed_2)
|
|
|
|
{
|
2006-09-06 02:00:58 +08:00
|
|
|
*right++ = j;
|
|
|
|
v->spl_nright++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
_j = GETENTRY(entryvec, j);
|
2006-10-04 08:30:14 +08:00
|
|
|
size_alpha = hemdist(datum_l, _j);
|
|
|
|
size_beta = hemdist(datum_r, _j);
|
2006-09-06 02:00:58 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.0001))
|
|
|
|
{
|
|
|
|
if (ISALLTRUE(datum_l) || ISALLTRUE(_j))
|
|
|
|
{
|
2006-09-06 02:00:58 +08:00
|
|
|
if (!ISALLTRUE(datum_l))
|
|
|
|
MemSet((void *) union_l, 0xff, sizeof(BITVEC));
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ptr = GETSIGN(_j);
|
2007-11-16 08:13:02 +08:00
|
|
|
LOOPBYTE
|
2007-11-16 09:12:24 +08:00
|
|
|
union_l[i] |= ptr[i];
|
2006-09-06 02:00:58 +08:00
|
|
|
}
|
|
|
|
*left++ = j;
|
|
|
|
v->spl_nleft++;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ISALLTRUE(datum_r) || ISALLTRUE(_j))
|
|
|
|
{
|
2006-09-06 02:00:58 +08:00
|
|
|
if (!ISALLTRUE(datum_r))
|
|
|
|
MemSet((void *) union_r, 0xff, sizeof(BITVEC));
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ptr = GETSIGN(_j);
|
2007-11-16 08:13:02 +08:00
|
|
|
LOOPBYTE
|
2007-11-16 09:12:24 +08:00
|
|
|
union_r[i] |= ptr[i];
|
2006-09-06 02:00:58 +08:00
|
|
|
}
|
|
|
|
*right++ = j;
|
|
|
|
v->spl_nright++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*right = *left = FirstOffsetNumber;
|
|
|
|
|
|
|
|
v->spl_ldatum = PointerGetDatum(datum_l);
|
|
|
|
v->spl_rdatum = PointerGetDatum(datum_r);
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
Datum
|
|
|
|
ghstore_consistent(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
GISTTYPE *entry = (GISTTYPE *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
|
2007-03-14 22:21:53 +08:00
|
|
|
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
|
2008-04-15 01:05:34 +08:00
|
|
|
/* Oid subtype = PG_GETARG_OID(3); */
|
|
|
|
bool *recheck = (bool *) PG_GETARG_POINTER(4);
|
2006-10-04 08:30:14 +08:00
|
|
|
bool res = true;
|
|
|
|
BITVECP sign;
|
|
|
|
|
2008-04-15 01:05:34 +08:00
|
|
|
/* All cases served by this function are inexact */
|
|
|
|
*recheck = true;
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (ISALLTRUE(entry))
|
2006-09-06 02:00:58 +08:00
|
|
|
PG_RETURN_BOOL(true);
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
sign = GETSIGN(entry);
|
2007-03-14 22:21:53 +08:00
|
|
|
|
2009-10-01 03:50:22 +08:00
|
|
|
if (strategy == HStoreContainsStrategyNumber ||
|
|
|
|
strategy == HStoreOldContainsStrategyNumber)
|
2006-10-04 08:30:14 +08:00
|
|
|
{
|
2007-03-14 22:21:53 +08:00
|
|
|
HStore *query = PG_GETARG_HS(1);
|
|
|
|
HEntry *qe = ARRPTR(query);
|
|
|
|
char *qv = STRPTR(query);
|
2009-10-01 03:50:22 +08:00
|
|
|
int count = HS_COUNT(query);
|
|
|
|
int i;
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2009-10-01 03:50:22 +08:00
|
|
|
for (i = 0; res && i < count; ++i)
|
2006-10-04 08:30:14 +08:00
|
|
|
{
|
2009-10-01 03:50:22 +08:00
|
|
|
int crc = crc32_sz((char *) HS_KEY(qe,qv,i), HS_KEYLEN(qe,i));
|
2007-03-14 22:21:53 +08:00
|
|
|
|
|
|
|
if (GETBIT(sign, HASHVAL(crc)))
|
2006-10-04 08:30:14 +08:00
|
|
|
{
|
2009-10-01 03:50:22 +08:00
|
|
|
if (!HS_VALISNULL(qe,i))
|
2007-03-14 22:21:53 +08:00
|
|
|
{
|
2009-10-01 03:50:22 +08:00
|
|
|
crc = crc32_sz((char *) HS_VAL(qe,qv,i), HS_VALLEN(qe,i));
|
2007-03-14 22:21:53 +08:00
|
|
|
if (!GETBIT(sign, HASHVAL(crc)))
|
|
|
|
res = false;
|
|
|
|
}
|
2006-09-06 02:00:58 +08:00
|
|
|
}
|
2007-03-14 22:21:53 +08:00
|
|
|
else
|
|
|
|
res = false;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
2006-09-06 02:00:58 +08:00
|
|
|
}
|
2007-11-16 09:12:24 +08:00
|
|
|
else if (strategy == HStoreExistsStrategyNumber)
|
2007-03-14 22:21:53 +08:00
|
|
|
{
|
2009-10-01 03:50:22 +08:00
|
|
|
text *query = PG_GETARG_TEXT_PP(1);
|
|
|
|
int crc = crc32_sz(VARDATA_ANY(query), VARSIZE_ANY_EXHDR(query));
|
2007-03-14 22:21:53 +08:00
|
|
|
|
|
|
|
res = (GETBIT(sign, HASHVAL(crc))) ? true : false;
|
|
|
|
}
|
2009-10-01 03:50:22 +08:00
|
|
|
else if (strategy == HStoreExistsAllStrategyNumber)
|
|
|
|
{
|
|
|
|
ArrayType *query = PG_GETARG_ARRAYTYPE_P(1);
|
|
|
|
Datum *key_datums;
|
|
|
|
bool *key_nulls;
|
|
|
|
int key_count;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
deconstruct_array(query,
|
|
|
|
TEXTOID, -1, false, 'i',
|
|
|
|
&key_datums, &key_nulls, &key_count);
|
|
|
|
|
|
|
|
for (i = 0; res && i < key_count; ++i)
|
|
|
|
{
|
|
|
|
int crc;
|
|
|
|
if (key_nulls[i])
|
|
|
|
continue;
|
|
|
|
crc = crc32_sz(VARDATA(key_datums[i]), VARSIZE(key_datums[i]) - VARHDRSZ);
|
|
|
|
if (!(GETBIT(sign, HASHVAL(crc))))
|
|
|
|
res = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (strategy == HStoreExistsAnyStrategyNumber)
|
|
|
|
{
|
|
|
|
ArrayType *query = PG_GETARG_ARRAYTYPE_P(1);
|
|
|
|
Datum *key_datums;
|
|
|
|
bool *key_nulls;
|
|
|
|
int key_count;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
deconstruct_array(query,
|
|
|
|
TEXTOID, -1, false, 'i',
|
|
|
|
&key_datums, &key_nulls, &key_count);
|
|
|
|
|
|
|
|
res = FALSE;
|
|
|
|
|
|
|
|
for (i = 0; !res && i < key_count; ++i)
|
|
|
|
{
|
|
|
|
int crc;
|
|
|
|
if (key_nulls[i])
|
|
|
|
continue;
|
|
|
|
crc = crc32_sz(VARDATA(key_datums[i]), VARSIZE(key_datums[i]) - VARHDRSZ);
|
|
|
|
if (GETBIT(sign, HASHVAL(crc)))
|
|
|
|
res = TRUE;
|
|
|
|
}
|
|
|
|
}
|
2007-03-14 22:21:53 +08:00
|
|
|
else
|
|
|
|
elog(ERROR, "Unsupported strategy number: %d", strategy);
|
2006-09-06 02:00:58 +08:00
|
|
|
|
|
|
|
PG_RETURN_BOOL(res);
|
|
|
|
}
|