2004-05-31 21:18:12 +04:00
|
|
|
#include "trgm.h"
|
|
|
|
|
|
|
|
#include "access/gist.h"
|
|
|
|
#include "access/itup.h"
|
2005-11-07 20:36:47 +03:00
|
|
|
#include "access/tuptoaster.h"
|
|
|
|
#include "storage/bufpage.h"
|
2004-05-31 21:18:12 +04:00
|
|
|
#include "utils/array.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(gtrgm_in);
|
|
|
|
Datum gtrgm_in(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(gtrgm_out);
|
|
|
|
Datum gtrgm_out(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(gtrgm_compress);
|
|
|
|
Datum gtrgm_compress(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(gtrgm_decompress);
|
|
|
|
Datum gtrgm_decompress(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(gtrgm_consistent);
|
|
|
|
Datum gtrgm_consistent(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(gtrgm_union);
|
|
|
|
Datum gtrgm_union(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(gtrgm_same);
|
|
|
|
Datum gtrgm_same(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(gtrgm_penalty);
|
|
|
|
Datum gtrgm_penalty(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(gtrgm_picksplit);
|
|
|
|
Datum gtrgm_picksplit(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
#define GETENTRY(vec,pos) ((TRGM *) DatumGetPointer((vec)->vector[(pos)].key))
|
|
|
|
|
|
|
|
#define SUMBIT(val) ( \
|
|
|
|
GETBITBYTE(val,0) + \
|
|
|
|
GETBITBYTE(val,1) + \
|
|
|
|
GETBITBYTE(val,2) + \
|
|
|
|
GETBITBYTE(val,3) + \
|
|
|
|
GETBITBYTE(val,4) + \
|
|
|
|
GETBITBYTE(val,5) + \
|
|
|
|
GETBITBYTE(val,6) + \
|
|
|
|
GETBITBYTE(val,7) \
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gtrgm_in(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
elog(ERROR, "Not implemented");
|
|
|
|
PG_RETURN_DATUM(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gtrgm_out(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
elog(ERROR, "Not implemented");
|
|
|
|
PG_RETURN_DATUM(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
makesign(BITVECP sign, TRGM * a)
|
|
|
|
{
|
|
|
|
int4 k,
|
|
|
|
len = ARRNELEM(a);
|
|
|
|
trgm *ptr = GETARR(a);
|
2004-08-29 09:07:03 +04:00
|
|
|
int4 tmp = 0;
|
2004-05-31 21:18:12 +04:00
|
|
|
|
|
|
|
MemSet((void *) sign, 0, sizeof(BITVEC));
|
2004-08-29 09:07:03 +04:00
|
|
|
SETBIT(sign, SIGLENBIT); /* set last unused bit */
|
|
|
|
for (k = 0; k < len; k++)
|
|
|
|
{
|
|
|
|
CPTRGM(((char *) &tmp), ptr + k);
|
2004-05-31 21:18:12 +04:00
|
|
|
HASH(sign, tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gtrgm_compress(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
|
|
|
GISTENTRY *retval = entry;
|
|
|
|
|
|
|
|
if (entry->leafkey)
|
|
|
|
{ /* trgm */
|
2004-08-29 09:07:03 +04:00
|
|
|
TRGM *res;
|
2004-05-31 21:18:12 +04:00
|
|
|
text *val = (text *) DatumGetPointer(PG_DETOAST_DATUM(entry->key));
|
|
|
|
|
|
|
|
res = generate_trgm(VARDATA(val), VARSIZE(val) - VARHDRSZ);
|
|
|
|
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
|
|
|
|
gistentryinit(*retval, PointerGetDatum(res),
|
|
|
|
entry->rel, entry->page,
|
|
|
|
entry->offset, res->len, FALSE);
|
|
|
|
}
|
|
|
|
else if (ISSIGNKEY(DatumGetPointer(entry->key)) &&
|
|
|
|
!ISALLTRUE(DatumGetPointer(entry->key)))
|
|
|
|
{
|
|
|
|
int4 i,
|
|
|
|
len;
|
2004-08-29 09:07:03 +04:00
|
|
|
TRGM *res;
|
2004-05-31 21:18:12 +04:00
|
|
|
BITVECP sign = GETSIGN(DatumGetPointer(entry->key));
|
|
|
|
|
|
|
|
LOOPBYTE(
|
|
|
|
if ((sign[i] & 0xff) != 0xff)
|
|
|
|
PG_RETURN_POINTER(retval);
|
|
|
|
);
|
|
|
|
|
|
|
|
len = CALCGTSIZE(SIGNKEY | ALLISTRUE, 0);
|
|
|
|
res = (TRGM *) palloc(len);
|
|
|
|
res->len = len;
|
|
|
|
res->flag = SIGNKEY | ALLISTRUE;
|
|
|
|
|
|
|
|
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
|
|
|
|
gistentryinit(*retval, PointerGetDatum(res),
|
|
|
|
entry->rel, entry->page,
|
|
|
|
entry->offset, res->len, FALSE);
|
|
|
|
}
|
|
|
|
PG_RETURN_POINTER(retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gtrgm_decompress(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
PG_RETURN_DATUM(PG_GETARG_DATUM(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gtrgm_consistent(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-08-29 09:07:03 +04:00
|
|
|
text *query = (text *) PG_GETARG_TEXT_P(1);
|
|
|
|
TRGM *key = (TRGM *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
|
|
|
|
TRGM *qtrg = generate_trgm(VARDATA(query), VARSIZE(query) - VARHDRSZ);
|
|
|
|
int res = false;
|
|
|
|
|
|
|
|
if (GIST_LEAF((GISTENTRY *) PG_GETARG_POINTER(0)))
|
|
|
|
{ /* all leafs contains orig trgm */
|
|
|
|
float4 tmpsml = cnt_sml(key, qtrg);
|
|
|
|
|
|
|
|
/* strange bug at freebsd 5.2.1 and gcc 3.3.3 */
|
|
|
|
res = (*(int *) &tmpsml == *(int *) &trgm_limit || tmpsml > trgm_limit) ? true : false;
|
|
|
|
}
|
|
|
|
else if (ISALLTRUE(key))
|
|
|
|
{ /* non-leaf contains signature */
|
2004-05-31 21:18:12 +04:00
|
|
|
res = true;
|
2004-08-29 09:07:03 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* non-leaf contains signature */
|
|
|
|
int4 count = 0;
|
|
|
|
int4 k,
|
|
|
|
len = ARRNELEM(qtrg);
|
2004-05-31 21:18:12 +04:00
|
|
|
trgm *ptr = GETARR(qtrg);
|
2004-08-29 09:07:03 +04:00
|
|
|
BITVECP sign = GETSIGN(key);
|
|
|
|
int4 tmp = 0;
|
2004-05-31 21:18:12 +04:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
for (k = 0; k < len; k++)
|
|
|
|
{
|
|
|
|
CPTRGM(((char *) &tmp), ptr + k);
|
2004-05-31 21:18:12 +04:00
|
|
|
count += GETBIT(sign, HASHVAL(tmp));
|
|
|
|
}
|
|
|
|
#ifdef DIVUNION
|
2004-08-29 09:07:03 +04:00
|
|
|
res = (len == count) ? true : ((((((float4) count) / ((float4) (len - count)))) >= trgm_limit) ? true : false);
|
2004-05-31 21:18:12 +04:00
|
|
|
#else
|
2004-08-29 09:07:03 +04:00
|
|
|
res = (len == 0) ? false : ((((((float4) count) / ((float4) len))) >= trgm_limit) ? true : false);
|
2004-05-31 21:18:12 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int4
|
|
|
|
unionkey(BITVECP sbase, TRGM * add)
|
|
|
|
{
|
|
|
|
int4 i;
|
|
|
|
|
|
|
|
if (ISSIGNKEY(add))
|
|
|
|
{
|
|
|
|
BITVECP sadd = GETSIGN(add);
|
|
|
|
|
|
|
|
if (ISALLTRUE(add))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
LOOPBYTE(
|
|
|
|
sbase[i] |= sadd[i];
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
trgm *ptr = GETARR(add);
|
2004-08-29 09:07:03 +04:00
|
|
|
int4 tmp = 0;
|
2004-05-31 21:18:12 +04:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
for (i = 0; i < ARRNELEM(add); i++)
|
|
|
|
{
|
|
|
|
CPTRGM(((char *) &tmp), ptr + i);
|
2004-05-31 21:18:12 +04:00
|
|
|
HASH(sbase, tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gtrgm_union(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-08-29 09:07:03 +04:00
|
|
|
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
|
|
|
|
int4 len = entryvec->n;
|
2004-05-31 21:18:12 +04:00
|
|
|
int *size = (int *) PG_GETARG_POINTER(1);
|
|
|
|
BITVEC base;
|
|
|
|
int4 i;
|
|
|
|
int4 flag = 0;
|
2004-08-29 09:07:03 +04:00
|
|
|
TRGM *result;
|
2004-05-31 21:18:12 +04:00
|
|
|
|
|
|
|
MemSet((void *) base, 0, sizeof(BITVEC));
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
if (unionkey(base, GETENTRY(entryvec, i)))
|
|
|
|
{
|
|
|
|
flag = ALLISTRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
flag |= SIGNKEY;
|
|
|
|
len = CALCGTSIZE(flag, 0);
|
|
|
|
result = (TRGM *) palloc(len);
|
|
|
|
*size = result->len = len;
|
|
|
|
result->flag = flag;
|
|
|
|
if (!ISALLTRUE(result))
|
|
|
|
memcpy((void *) GETSIGN(result), (void *) base, sizeof(BITVEC));
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gtrgm_same(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-08-29 09:07:03 +04:00
|
|
|
TRGM *a = (TRGM *) PG_GETARG_POINTER(0);
|
|
|
|
TRGM *b = (TRGM *) PG_GETARG_POINTER(1);
|
2004-05-31 21:18:12 +04:00
|
|
|
bool *result = (bool *) PG_GETARG_POINTER(2);
|
|
|
|
|
|
|
|
if (ISSIGNKEY(a))
|
|
|
|
{ /* then b also ISSIGNKEY */
|
|
|
|
if (ISALLTRUE(a) && ISALLTRUE(b))
|
|
|
|
*result = true;
|
|
|
|
else if (ISALLTRUE(a))
|
|
|
|
*result = false;
|
|
|
|
else if (ISALLTRUE(b))
|
|
|
|
*result = false;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int4 i;
|
|
|
|
BITVECP sa = GETSIGN(a),
|
|
|
|
sb = GETSIGN(b);
|
|
|
|
|
|
|
|
*result = true;
|
|
|
|
LOOPBYTE(
|
|
|
|
if (sa[i] != sb[i])
|
|
|
|
{
|
|
|
|
*result = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* a and b ISARRKEY */
|
|
|
|
int4 lena = ARRNELEM(a),
|
|
|
|
lenb = ARRNELEM(b);
|
|
|
|
|
|
|
|
if (lena != lenb)
|
|
|
|
*result = false;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
trgm *ptra = GETARR(a),
|
|
|
|
*ptrb = GETARR(b);
|
|
|
|
int4 i;
|
|
|
|
|
|
|
|
*result = true;
|
|
|
|
for (i = 0; i < lena; i++)
|
2004-08-29 09:07:03 +04:00
|
|
|
if (CMPTRGM(ptra + i, ptrb + i))
|
2004-05-31 21:18:12 +04:00
|
|
|
{
|
|
|
|
*result = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int4
|
|
|
|
sizebitvec(BITVECP sign)
|
|
|
|
{
|
|
|
|
int4 size = 0,
|
|
|
|
i;
|
|
|
|
|
|
|
|
LOOPBYTE(
|
2004-08-29 09:07:03 +04:00
|
|
|
size += SUMBIT(*(char *) sign);
|
|
|
|
sign = (BITVECP) (((char *) sign) + 1);
|
2004-05-31 21:18:12 +04:00
|
|
|
);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-08-29 09:07:03 +04:00
|
|
|
hemdistsign(BITVECP a, BITVECP b)
|
|
|
|
{
|
|
|
|
int i,
|
|
|
|
dist = 0;
|
2004-05-31 21:18:12 +04:00
|
|
|
|
|
|
|
LOOPBIT(
|
2004-08-29 09:07:03 +04:00
|
|
|
if (GETBIT(a, i) != GETBIT(b, i))
|
2004-05-31 21:18:12 +04:00
|
|
|
dist++;
|
|
|
|
);
|
|
|
|
return dist;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-08-29 09:07:03 +04:00
|
|
|
hemdist(TRGM * a, TRGM * b)
|
|
|
|
{
|
|
|
|
if (ISALLTRUE(a))
|
|
|
|
{
|
2004-05-31 21:18:12 +04:00
|
|
|
if (ISALLTRUE(b))
|
|
|
|
return 0;
|
|
|
|
else
|
2004-08-29 09:07:03 +04:00
|
|
|
return SIGLENBIT - sizebitvec(GETSIGN(b));
|
|
|
|
}
|
|
|
|
else if (ISALLTRUE(b))
|
|
|
|
return SIGLENBIT - sizebitvec(GETSIGN(a));
|
2004-05-31 21:18:12 +04:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
return hemdistsign(GETSIGN(a), GETSIGN(b));
|
2004-05-31 21:18:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gtrgm_penalty(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0); /* always ISSIGNKEY */
|
|
|
|
GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
|
|
|
|
float *penalty = (float *) PG_GETARG_POINTER(2);
|
2004-08-29 09:07:03 +04:00
|
|
|
TRGM *origval = (TRGM *) DatumGetPointer(origentry->key);
|
|
|
|
TRGM *newval = (TRGM *) DatumGetPointer(newentry->key);
|
2004-05-31 21:18:12 +04:00
|
|
|
BITVECP orig = GETSIGN(origval);
|
|
|
|
|
|
|
|
*penalty = 0.0;
|
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
if (ISARRKEY(newval))
|
|
|
|
{
|
|
|
|
BITVEC sign;
|
|
|
|
|
2004-05-31 21:18:12 +04:00
|
|
|
makesign(sign, newval);
|
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
if (ISALLTRUE(origval))
|
|
|
|
*penalty = ((float) (SIGLENBIT - sizebitvec(sign))) / (float) (SIGLENBIT + 1);
|
|
|
|
else
|
|
|
|
*penalty = hemdistsign(sign, orig);
|
2004-05-31 21:18:12 +04:00
|
|
|
}
|
2004-08-29 09:07:03 +04:00
|
|
|
else
|
|
|
|
*penalty = hemdist(origval, newval);
|
2004-05-31 21:18:12 +04:00
|
|
|
PG_RETURN_POINTER(penalty);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
bool allistrue;
|
|
|
|
BITVEC sign;
|
|
|
|
} CACHESIGN;
|
|
|
|
|
|
|
|
static void
|
|
|
|
fillcache(CACHESIGN * item, TRGM * key)
|
|
|
|
{
|
|
|
|
item->allistrue = false;
|
|
|
|
if (ISARRKEY(key))
|
|
|
|
makesign(item->sign, key);
|
|
|
|
else if (ISALLTRUE(key))
|
|
|
|
item->allistrue = true;
|
|
|
|
else
|
|
|
|
memcpy((void *) item->sign, (void *) GETSIGN(key), sizeof(BITVEC));
|
|
|
|
}
|
|
|
|
|
|
|
|
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
OffsetNumber pos;
|
|
|
|
int4 cost;
|
|
|
|
} SPLITCOST;
|
|
|
|
|
|
|
|
static int
|
|
|
|
comparecost(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
if (((SPLITCOST *) a)->cost == ((SPLITCOST *) b)->cost)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return (((SPLITCOST *) a)->cost > ((SPLITCOST *) b)->cost) ? 1 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2004-08-29 09:07:03 +04:00
|
|
|
hemdistcache(CACHESIGN * a, CACHESIGN * b)
|
|
|
|
{
|
|
|
|
if (a->allistrue)
|
|
|
|
{
|
2004-05-31 21:18:12 +04:00
|
|
|
if (b->allistrue)
|
|
|
|
return 0;
|
|
|
|
else
|
2004-08-29 09:07:03 +04:00
|
|
|
return SIGLENBIT - sizebitvec(b->sign);
|
|
|
|
}
|
|
|
|
else if (b->allistrue)
|
|
|
|
return SIGLENBIT - sizebitvec(a->sign);
|
2004-05-31 21:18:12 +04:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
return hemdistsign(a->sign, b->sign);
|
2004-05-31 21:18:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gtrgm_picksplit(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-08-29 09:07:03 +04:00
|
|
|
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
|
|
|
|
OffsetNumber maxoff = entryvec->n - 2;
|
2004-05-31 21:18:12 +04:00
|
|
|
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
|
|
|
|
OffsetNumber k,
|
|
|
|
j;
|
2004-08-29 09:07:03 +04:00
|
|
|
TRGM *datum_l,
|
2004-05-31 21:18:12 +04:00
|
|
|
*datum_r;
|
|
|
|
BITVECP union_l,
|
|
|
|
union_r;
|
|
|
|
int4 size_alpha,
|
|
|
|
size_beta;
|
|
|
|
int4 size_waste,
|
|
|
|
waste = -1;
|
|
|
|
int4 nbytes;
|
|
|
|
OffsetNumber seed_1 = 0,
|
|
|
|
seed_2 = 0;
|
|
|
|
OffsetNumber *left,
|
|
|
|
*right;
|
|
|
|
BITVECP ptr;
|
|
|
|
int i;
|
|
|
|
CACHESIGN *cache;
|
|
|
|
SPLITCOST *costvector;
|
|
|
|
|
|
|
|
nbytes = (maxoff + 2) * sizeof(OffsetNumber);
|
|
|
|
v->spl_left = (OffsetNumber *) palloc(nbytes);
|
|
|
|
v->spl_right = (OffsetNumber *) palloc(nbytes);
|
|
|
|
|
|
|
|
cache = (CACHESIGN *) palloc(sizeof(CACHESIGN) * (maxoff + 2));
|
|
|
|
fillcache(&cache[FirstOffsetNumber], GETENTRY(entryvec, FirstOffsetNumber));
|
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
|
|
|
|
{
|
|
|
|
for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
|
|
|
|
{
|
2004-05-31 21:18:12 +04:00
|
|
|
if (k == FirstOffsetNumber)
|
|
|
|
fillcache(&cache[j], GETENTRY(entryvec, j));
|
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
size_waste = hemdistcache(&(cache[j]), &(cache[k]));
|
|
|
|
if (size_waste > waste)
|
|
|
|
{
|
2004-05-31 21:18:12 +04: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;
|
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
if (seed_1 == 0 || seed_2 == 0)
|
|
|
|
{
|
2004-05-31 21:18:12 +04:00
|
|
|
seed_1 = 1;
|
|
|
|
seed_2 = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* form initial .. */
|
2004-08-29 09:07:03 +04:00
|
|
|
if (cache[seed_1].allistrue)
|
|
|
|
{
|
2004-05-31 21:18:12 +04:00
|
|
|
datum_l = (TRGM *) palloc(CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
|
|
|
|
datum_l->len = CALCGTSIZE(SIGNKEY | ALLISTRUE, 0);
|
|
|
|
datum_l->flag = SIGNKEY | ALLISTRUE;
|
2004-08-29 09:07:03 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-05-31 21:18:12 +04:00
|
|
|
datum_l = (TRGM *) palloc(CALCGTSIZE(SIGNKEY, 0));
|
|
|
|
datum_l->len = CALCGTSIZE(SIGNKEY, 0);
|
|
|
|
datum_l->flag = SIGNKEY;
|
|
|
|
memcpy((void *) GETSIGN(datum_l), (void *) cache[seed_1].sign, sizeof(BITVEC));
|
|
|
|
}
|
2004-08-29 09:07:03 +04:00
|
|
|
if (cache[seed_2].allistrue)
|
|
|
|
{
|
2004-05-31 21:18:12 +04:00
|
|
|
datum_r = (TRGM *) palloc(CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
|
|
|
|
datum_r->len = CALCGTSIZE(SIGNKEY | ALLISTRUE, 0);
|
|
|
|
datum_r->flag = SIGNKEY | ALLISTRUE;
|
2004-08-29 09:07:03 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-05-31 21:18:12 +04:00
|
|
|
datum_r = (TRGM *) palloc(CALCGTSIZE(SIGNKEY, 0));
|
|
|
|
datum_r->len = CALCGTSIZE(SIGNKEY, 0);
|
|
|
|
datum_r->flag = SIGNKEY;
|
|
|
|
memcpy((void *) GETSIGN(datum_r), (void *) cache[seed_2].sign, sizeof(BITVEC));
|
|
|
|
}
|
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
union_l = GETSIGN(datum_l);
|
|
|
|
union_r = GETSIGN(datum_r);
|
2004-05-31 21:18:12 +04:00
|
|
|
maxoff = OffsetNumberNext(maxoff);
|
|
|
|
fillcache(&cache[maxoff], GETENTRY(entryvec, maxoff));
|
|
|
|
/* sort before ... */
|
|
|
|
costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
|
2004-08-29 09:07:03 +04:00
|
|
|
for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
|
|
|
|
{
|
2004-05-31 21:18:12 +04:00
|
|
|
costvector[j - 1].pos = j;
|
|
|
|
size_alpha = hemdistcache(&(cache[seed_1]), &(cache[j]));
|
2004-08-29 09:07:03 +04:00
|
|
|
size_beta = hemdistcache(&(cache[seed_2]), &(cache[j]));
|
2004-05-31 21:18:12 +04:00
|
|
|
costvector[j - 1].cost = abs(size_alpha - size_beta);
|
|
|
|
}
|
|
|
|
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
|
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
for (k = 0; k < maxoff; k++)
|
|
|
|
{
|
2004-05-31 21:18:12 +04:00
|
|
|
j = costvector[k].pos;
|
2004-08-29 09:07:03 +04:00
|
|
|
if (j == seed_1)
|
|
|
|
{
|
2004-05-31 21:18:12 +04:00
|
|
|
*left++ = j;
|
|
|
|
v->spl_nleft++;
|
|
|
|
continue;
|
2004-08-29 09:07:03 +04:00
|
|
|
}
|
|
|
|
else if (j == seed_2)
|
|
|
|
{
|
2004-05-31 21:18:12 +04:00
|
|
|
*right++ = j;
|
|
|
|
v->spl_nright++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
if (ISALLTRUE(datum_l) || cache[j].allistrue)
|
|
|
|
{
|
|
|
|
if (ISALLTRUE(datum_l) && cache[j].allistrue)
|
|
|
|
size_alpha = 0;
|
2004-05-31 21:18:12 +04:00
|
|
|
else
|
2004-08-29 09:07:03 +04:00
|
|
|
size_alpha = SIGLENBIT - sizebitvec(
|
|
|
|
(cache[j].allistrue) ? GETSIGN(datum_l) : GETSIGN(cache[j].sign)
|
|
|
|
);
|
2004-05-31 21:18:12 +04:00
|
|
|
}
|
2004-08-29 09:07:03 +04:00
|
|
|
else
|
|
|
|
size_alpha = hemdistsign(cache[j].sign, GETSIGN(datum_l));
|
2004-05-31 21:18:12 +04:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
if (ISALLTRUE(datum_r) || cache[j].allistrue)
|
|
|
|
{
|
|
|
|
if (ISALLTRUE(datum_r) && cache[j].allistrue)
|
|
|
|
size_beta = 0;
|
2004-05-31 21:18:12 +04:00
|
|
|
else
|
2004-08-29 09:07:03 +04:00
|
|
|
size_beta = SIGLENBIT - sizebitvec(
|
|
|
|
(cache[j].allistrue) ? GETSIGN(datum_r) : GETSIGN(cache[j].sign)
|
|
|
|
);
|
2004-05-31 21:18:12 +04:00
|
|
|
}
|
2004-08-29 09:07:03 +04:00
|
|
|
else
|
|
|
|
size_beta = hemdistsign(cache[j].sign, GETSIGN(datum_r));
|
2004-05-31 21:18:12 +04:00
|
|
|
|
2004-08-29 09:07:03 +04:00
|
|
|
if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.1))
|
|
|
|
{
|
|
|
|
if (ISALLTRUE(datum_l) || cache[j].allistrue)
|
|
|
|
{
|
|
|
|
if (!ISALLTRUE(datum_l))
|
2004-05-31 21:18:12 +04:00
|
|
|
MemSet((void *) GETSIGN(datum_l), 0xff, sizeof(BITVEC));
|
2004-08-29 09:07:03 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ptr = cache[j].sign;
|
2004-05-31 21:18:12 +04:00
|
|
|
LOOPBYTE(
|
2004-08-29 09:07:03 +04:00
|
|
|
union_l[i] |= ptr[i];
|
2004-05-31 21:18:12 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
*left++ = j;
|
|
|
|
v->spl_nleft++;
|
2004-08-29 09:07:03 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ISALLTRUE(datum_r) || cache[j].allistrue)
|
|
|
|
{
|
|
|
|
if (!ISALLTRUE(datum_r))
|
2004-05-31 21:18:12 +04:00
|
|
|
MemSet((void *) GETSIGN(datum_r), 0xff, sizeof(BITVEC));
|
2004-08-29 09:07:03 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ptr = cache[j].sign;
|
2004-05-31 21:18:12 +04:00
|
|
|
LOOPBYTE(
|
2004-08-29 09:07:03 +04:00
|
|
|
union_r[i] |= ptr[i];
|
2004-05-31 21:18:12 +04: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);
|
|
|
|
}
|