1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-30 11:03:19 +03:00

Pgindent run for 8.0.

This commit is contained in:
Bruce Momjian
2004-08-29 05:07:03 +00:00
parent 90cb9c3051
commit b6b71b85bc
527 changed files with 20550 additions and 18283 deletions

View File

@ -14,92 +14,99 @@ PG_FUNCTION_INFO_V1(gbt_bit_consistent);
PG_FUNCTION_INFO_V1(gbt_bit_penalty);
PG_FUNCTION_INFO_V1(gbt_bit_same);
Datum gbt_bit_compress(PG_FUNCTION_ARGS);
Datum gbt_bit_union(PG_FUNCTION_ARGS);
Datum gbt_bit_picksplit(PG_FUNCTION_ARGS);
Datum gbt_bit_consistent(PG_FUNCTION_ARGS);
Datum gbt_bit_penalty(PG_FUNCTION_ARGS);
Datum gbt_bit_same(PG_FUNCTION_ARGS);
Datum gbt_bit_compress(PG_FUNCTION_ARGS);
Datum gbt_bit_union(PG_FUNCTION_ARGS);
Datum gbt_bit_picksplit(PG_FUNCTION_ARGS);
Datum gbt_bit_consistent(PG_FUNCTION_ARGS);
Datum gbt_bit_penalty(PG_FUNCTION_ARGS);
Datum gbt_bit_same(PG_FUNCTION_ARGS);
/* define for comparison */
static bool gbt_bitgt (const void *a, const void *b)
static bool
gbt_bitgt(const void *a, const void *b)
{
return ( DatumGetBool(DirectFunctionCall2( bitgt ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
return (DatumGetBool(DirectFunctionCall2(bitgt, PointerGetDatum(a), PointerGetDatum(b))));
}
static bool gbt_bitge (const void *a, const void *b)
static bool
gbt_bitge(const void *a, const void *b)
{
return ( DatumGetBool(DirectFunctionCall2( bitge ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
return (DatumGetBool(DirectFunctionCall2(bitge, PointerGetDatum(a), PointerGetDatum(b))));
}
static bool gbt_biteq (const void *a, const void *b)
static bool
gbt_biteq(const void *a, const void *b)
{
return ( DatumGetBool(DirectFunctionCall2( biteq ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
return (DatumGetBool(DirectFunctionCall2(biteq, PointerGetDatum(a), PointerGetDatum(b))));
}
static bool gbt_bitle (const void *a, const void *b)
static bool
gbt_bitle(const void *a, const void *b)
{
return ( DatumGetBool(DirectFunctionCall2( bitle ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
return (DatumGetBool(DirectFunctionCall2(bitle, PointerGetDatum(a), PointerGetDatum(b))));
}
static bool gbt_bitlt (const void *a, const void *b)
static bool
gbt_bitlt(const void *a, const void *b)
{
return ( DatumGetBool(DirectFunctionCall2( bitlt ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
return (DatumGetBool(DirectFunctionCall2(bitlt, PointerGetDatum(a), PointerGetDatum(b))));
}
static int32 gbt_bitcmp ( const bytea * a , const bytea * b )
static int32
gbt_bitcmp(const bytea *a, const bytea *b)
{
return
( DatumGetInt32(DirectFunctionCall2(byteacmp,PointerGetDatum(a),PointerGetDatum(b) ) ) );
return
(DatumGetInt32(DirectFunctionCall2(byteacmp, PointerGetDatum(a), PointerGetDatum(b))));
}
static bytea *
gbt_bit_xfrm ( bytea * leaf )
gbt_bit_xfrm(bytea *leaf)
{
bytea * out = leaf;
int s = VARBITBYTES(leaf) + VARHDRSZ;
bytea *out = leaf;
int s = VARBITBYTES(leaf) + VARHDRSZ;
out = palloc ( s );
VARATT_SIZEP(out) = s;
memcpy ( (void*)VARDATA(out), (void*)VARBITS(leaf), VARBITBYTES(leaf) );
return out;
out = palloc(s);
VARATT_SIZEP(out) = s;
memcpy((void *) VARDATA(out), (void *) VARBITS(leaf), VARBITBYTES(leaf));
return out;
}
static GBT_VARKEY * gbt_bit_l2n ( GBT_VARKEY * leaf )
static GBT_VARKEY *
gbt_bit_l2n(GBT_VARKEY * leaf)
{
GBT_VARKEY *out = leaf ;
GBT_VARKEY_R r = gbt_var_key_readable ( leaf );
bytea *o ;
GBT_VARKEY *out = leaf;
GBT_VARKEY_R r = gbt_var_key_readable(leaf);
bytea *o;
o = gbt_bit_xfrm (r.lower);
r.upper = r.lower = o;
out = gbt_var_key_copy( &r, TRUE );
pfree(o);
o = gbt_bit_xfrm(r.lower);
r.upper = r.lower = o;
out = gbt_var_key_copy(&r, TRUE);
pfree(o);
return out;
return out;
}
static const gbtree_vinfo tinfo =
{
gbt_t_bit,
FALSE,
TRUE,
gbt_bitgt,
gbt_bitge,
gbt_biteq,
gbt_bitle,
gbt_bitlt,
gbt_bitcmp,
gbt_bit_l2n
gbt_t_bit,
FALSE,
TRUE,
gbt_bitgt,
gbt_bitge,
gbt_biteq,
gbt_bitle,
gbt_bitlt,
gbt_bitcmp,
gbt_bit_l2n
};
@ -108,40 +115,40 @@ static const gbtree_vinfo tinfo =
**************************************************/
Datum
gbt_bit_compress (PG_FUNCTION_ARGS)
gbt_bit_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
PG_RETURN_POINTER ( gbt_var_compress( entry, &tinfo ) );
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
PG_RETURN_POINTER(gbt_var_compress(entry, &tinfo));
}
Datum
gbt_bit_consistent(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GBT_VARKEY *ktst = (GBT_VARKEY *) DatumGetPointer ( entry->key ) ;
GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer ( PG_DETOAST_DATUM( entry->key ) );
void *qtst = ( void * ) DatumGetPointer( PG_GETARG_DATUM(1) );
void *query = ( void * ) DatumGetByteaP ( PG_GETARG_DATUM(1) );
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
bool retval = FALSE;
GBT_VARKEY_R r = gbt_var_key_readable ( key );
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GBT_VARKEY *ktst = (GBT_VARKEY *) DatumGetPointer(entry->key);
GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(PG_DETOAST_DATUM(entry->key));
void *qtst = (void *) DatumGetPointer(PG_GETARG_DATUM(1));
void *query = (void *) DatumGetByteaP(PG_GETARG_DATUM(1));
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
bool retval = FALSE;
GBT_VARKEY_R r = gbt_var_key_readable(key);
if ( GIST_LEAF(entry) )
{
retval = gbt_var_consistent( &r, query, &strategy, TRUE, &tinfo );
} else {
bytea * q = gbt_bit_xfrm ( ( bytea * ) query );
retval = gbt_var_consistent( &r, (void*)q, &strategy, FALSE, &tinfo );
pfree(q);
}
if (GIST_LEAF(entry))
retval = gbt_var_consistent(&r, query, &strategy, TRUE, &tinfo);
else
{
bytea *q = gbt_bit_xfrm((bytea *) query);
if ( ktst != key ){
pfree ( key );
}
if ( qtst != query ){
pfree ( query );
}
PG_RETURN_BOOL(retval);
retval = gbt_var_consistent(&r, (void *) q, &strategy, FALSE, &tinfo);
pfree(q);
}
if (ktst != key)
pfree(key);
if (qtst != query)
pfree(query);
PG_RETURN_BOOL(retval);
}
@ -149,37 +156,40 @@ gbt_bit_consistent(PG_FUNCTION_ARGS)
Datum
gbt_bit_union(PG_FUNCTION_ARGS)
{
GistEntryVector * entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
int32 * size = (int *) PG_GETARG_POINTER(1);
PG_RETURN_POINTER( gbt_var_union ( entryvec , size , &tinfo ) );
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
int32 *size = (int *) PG_GETARG_POINTER(1);
PG_RETURN_POINTER(gbt_var_union(entryvec, size, &tinfo));
}
Datum
gbt_bit_picksplit(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
gbt_var_picksplit ( entryvec, v, &tinfo );
PG_RETURN_POINTER(v);
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
gbt_var_picksplit(entryvec, v, &tinfo);
PG_RETURN_POINTER(v);
}
Datum
gbt_bit_same(PG_FUNCTION_ARGS)
{
Datum d1 = PG_GETARG_DATUM(0);
Datum d2 = PG_GETARG_DATUM(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
PG_RETURN_POINTER( gbt_var_same ( result, d1 , d2 , &tinfo ));
Datum d1 = PG_GETARG_DATUM(0);
Datum d2 = PG_GETARG_DATUM(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
PG_RETURN_POINTER(gbt_var_same(result, d1, d2, &tinfo));
}
Datum
gbt_bit_penalty(PG_FUNCTION_ARGS)
{
float *result = (float *) PG_GETARG_POINTER(2);
GISTENTRY * o = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY * n = (GISTENTRY *) PG_GETARG_POINTER(1);
PG_RETURN_POINTER( gbt_var_penalty ( result ,o , n, &tinfo ) );
}
float *result = (float *) PG_GETARG_POINTER(2);
GISTENTRY *o = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *n = (GISTENTRY *) PG_GETARG_POINTER(1);
PG_RETURN_POINTER(gbt_var_penalty(result, o, n, &tinfo));
}

View File

@ -13,61 +13,67 @@ PG_FUNCTION_INFO_V1(gbt_bytea_consistent);
PG_FUNCTION_INFO_V1(gbt_bytea_penalty);
PG_FUNCTION_INFO_V1(gbt_bytea_same);
Datum gbt_bytea_compress(PG_FUNCTION_ARGS);
Datum gbt_bytea_union(PG_FUNCTION_ARGS);
Datum gbt_bytea_picksplit(PG_FUNCTION_ARGS);
Datum gbt_bytea_consistent(PG_FUNCTION_ARGS);
Datum gbt_bytea_penalty(PG_FUNCTION_ARGS);
Datum gbt_bytea_same(PG_FUNCTION_ARGS);
Datum gbt_bytea_compress(PG_FUNCTION_ARGS);
Datum gbt_bytea_union(PG_FUNCTION_ARGS);
Datum gbt_bytea_picksplit(PG_FUNCTION_ARGS);
Datum gbt_bytea_consistent(PG_FUNCTION_ARGS);
Datum gbt_bytea_penalty(PG_FUNCTION_ARGS);
Datum gbt_bytea_same(PG_FUNCTION_ARGS);
/* define for comparison */
static bool gbt_byteagt (const void *a, const void *b)
static bool
gbt_byteagt(const void *a, const void *b)
{
return ( DatumGetBool(DirectFunctionCall2( byteagt ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
return (DatumGetBool(DirectFunctionCall2(byteagt, PointerGetDatum(a), PointerGetDatum(b))));
}
static bool gbt_byteage (const void *a, const void *b)
static bool
gbt_byteage(const void *a, const void *b)
{
return ( DatumGetBool(DirectFunctionCall2( byteage ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
return (DatumGetBool(DirectFunctionCall2(byteage, PointerGetDatum(a), PointerGetDatum(b))));
}
static bool gbt_byteaeq (const void *a, const void *b)
static bool
gbt_byteaeq(const void *a, const void *b)
{
return ( DatumGetBool(DirectFunctionCall2( byteaeq ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
return (DatumGetBool(DirectFunctionCall2(byteaeq, PointerGetDatum(a), PointerGetDatum(b))));
}
static bool gbt_byteale (const void *a, const void *b)
static bool
gbt_byteale(const void *a, const void *b)
{
return ( DatumGetBool(DirectFunctionCall2( byteale ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
return (DatumGetBool(DirectFunctionCall2(byteale, PointerGetDatum(a), PointerGetDatum(b))));
}
static bool gbt_bytealt (const void *a, const void *b)
static bool
gbt_bytealt(const void *a, const void *b)
{
return ( DatumGetBool(DirectFunctionCall2( bytealt ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
return (DatumGetBool(DirectFunctionCall2(bytealt, PointerGetDatum(a), PointerGetDatum(b))));
}
static int32 gbt_byteacmp ( const bytea * a , const bytea * b )
static int32
gbt_byteacmp(const bytea *a, const bytea *b)
{
return
( DatumGetInt32(DirectFunctionCall2(byteacmp,PointerGetDatum(a),PointerGetDatum(b) ) ) );
return
(DatumGetInt32(DirectFunctionCall2(byteacmp, PointerGetDatum(a), PointerGetDatum(b))));
}
static const gbtree_vinfo tinfo =
{
gbt_t_bytea,
FALSE,
TRUE,
gbt_byteagt,
gbt_byteage,
gbt_byteaeq,
gbt_byteale,
gbt_bytealt,
gbt_byteacmp,
NULL
gbt_t_bytea,
FALSE,
TRUE,
gbt_byteagt,
gbt_byteage,
gbt_byteaeq,
gbt_byteale,
gbt_bytealt,
gbt_byteacmp,
NULL
};
@ -77,10 +83,11 @@ static const gbtree_vinfo tinfo =
Datum
gbt_bytea_compress (PG_FUNCTION_ARGS)
gbt_bytea_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
PG_RETURN_POINTER ( gbt_var_compress( entry, &tinfo ) );
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
PG_RETURN_POINTER(gbt_var_compress(entry, &tinfo));
}
@ -88,24 +95,22 @@ gbt_bytea_compress (PG_FUNCTION_ARGS)
Datum
gbt_bytea_consistent(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GBT_VARKEY *ktst = (GBT_VARKEY *) DatumGetPointer ( entry->key ) ;
GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer ( PG_DETOAST_DATUM( entry->key ) );
void *qtst = ( void * ) DatumGetPointer( PG_GETARG_DATUM(1) );
void *query = ( void * ) DatumGetByteaP ( PG_GETARG_DATUM(1) );
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
bool retval = FALSE;
GBT_VARKEY_R r = gbt_var_key_readable ( key );
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GBT_VARKEY *ktst = (GBT_VARKEY *) DatumGetPointer(entry->key);
GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(PG_DETOAST_DATUM(entry->key));
void *qtst = (void *) DatumGetPointer(PG_GETARG_DATUM(1));
void *query = (void *) DatumGetByteaP(PG_GETARG_DATUM(1));
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
bool retval = FALSE;
GBT_VARKEY_R r = gbt_var_key_readable(key);
retval = gbt_var_consistent( &r, query, &strategy, GIST_LEAF(entry), &tinfo );
retval = gbt_var_consistent(&r, query, &strategy, GIST_LEAF(entry), &tinfo);
if ( ktst != key ){
pfree ( key );
}
if ( qtst != query ){
pfree ( query );
}
PG_RETURN_BOOL(retval);
if (ktst != key)
pfree(key);
if (qtst != query)
pfree(query);
PG_RETURN_BOOL(retval);
}
@ -113,37 +118,40 @@ gbt_bytea_consistent(PG_FUNCTION_ARGS)
Datum
gbt_bytea_union(PG_FUNCTION_ARGS)
{
GistEntryVector * entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
int32 * size = (int *) PG_GETARG_POINTER(1);
PG_RETURN_POINTER( gbt_var_union ( entryvec , size , &tinfo ) );
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
int32 *size = (int *) PG_GETARG_POINTER(1);
PG_RETURN_POINTER(gbt_var_union(entryvec, size, &tinfo));
}
Datum
gbt_bytea_picksplit(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
gbt_var_picksplit ( entryvec, v, &tinfo );
PG_RETURN_POINTER(v);
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
gbt_var_picksplit(entryvec, v, &tinfo);
PG_RETURN_POINTER(v);
}
Datum
gbt_bytea_same(PG_FUNCTION_ARGS)
{
Datum d1 = PG_GETARG_DATUM(0);
Datum d2 = PG_GETARG_DATUM(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
PG_RETURN_POINTER( gbt_var_same ( result, d1 , d2 , &tinfo ));
Datum d1 = PG_GETARG_DATUM(0);
Datum d2 = PG_GETARG_DATUM(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
PG_RETURN_POINTER(gbt_var_same(result, d1, d2, &tinfo));
}
Datum
gbt_bytea_penalty(PG_FUNCTION_ARGS)
{
float *result = (float *) PG_GETARG_POINTER(2);
GISTENTRY * o = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY * n = (GISTENTRY *) PG_GETARG_POINTER(1);
PG_RETURN_POINTER( gbt_var_penalty ( result ,o , n, &tinfo ) );
}
float *result = (float *) PG_GETARG_POINTER(2);
GISTENTRY *o = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *n = (GISTENTRY *) PG_GETARG_POINTER(1);
PG_RETURN_POINTER(gbt_var_penalty(result, o, n, &tinfo));
}

View File

@ -4,9 +4,9 @@
typedef struct
{
Cash lower;
Cash upper;
} cashKEY;
Cash lower;
Cash upper;
} cashKEY;
/*
** Cash ops
@ -18,59 +18,62 @@ PG_FUNCTION_INFO_V1(gbt_cash_consistent);
PG_FUNCTION_INFO_V1(gbt_cash_penalty);
PG_FUNCTION_INFO_V1(gbt_cash_same);
Datum gbt_cash_compress(PG_FUNCTION_ARGS);
Datum gbt_cash_union(PG_FUNCTION_ARGS);
Datum gbt_cash_picksplit(PG_FUNCTION_ARGS);
Datum gbt_cash_consistent(PG_FUNCTION_ARGS);
Datum gbt_cash_penalty(PG_FUNCTION_ARGS);
Datum gbt_cash_same(PG_FUNCTION_ARGS);
Datum gbt_cash_compress(PG_FUNCTION_ARGS);
Datum gbt_cash_union(PG_FUNCTION_ARGS);
Datum gbt_cash_picksplit(PG_FUNCTION_ARGS);
Datum gbt_cash_consistent(PG_FUNCTION_ARGS);
Datum gbt_cash_penalty(PG_FUNCTION_ARGS);
Datum gbt_cash_same(PG_FUNCTION_ARGS);
static bool gbt_cashgt (const void *a, const void *b)
static bool
gbt_cashgt(const void *a, const void *b)
{
return ( *((Cash*)a) > *((Cash*)b) );
return (*((Cash *) a) > *((Cash *) b));
}
static bool gbt_cashge (const void *a, const void *b)
static bool
gbt_cashge(const void *a, const void *b)
{
return ( *((Cash*)a) >= *((Cash*)b) );
return (*((Cash *) a) >= *((Cash *) b));
}
static bool gbt_casheq (const void *a, const void *b)
static bool
gbt_casheq(const void *a, const void *b)
{
return ( *((Cash*)a) == *((Cash*)b) );
return (*((Cash *) a) == *((Cash *) b));
}
static bool gbt_cashle (const void *a, const void *b)
static bool
gbt_cashle(const void *a, const void *b)
{
return ( *((Cash*)a) <= *((Cash*)b) );
return (*((Cash *) a) <= *((Cash *) b));
}
static bool gbt_cashlt (const void *a, const void *b)
static bool
gbt_cashlt(const void *a, const void *b)
{
return ( *((Cash*)a) < *((Cash*)b) );
return (*((Cash *) a) < *((Cash *) b));
}
static int
gbt_cashkey_cmp(const void *a, const void *b)
{
if ( *(Cash*)&(((Nsrt *) a)->t[0]) > *(Cash*)&(((Nsrt *) b)->t[0]) ){
return 1;
} else
if ( *(Cash*)&(((Nsrt *) a)->t[0]) < *(Cash*)&(((Nsrt *) b)->t[0]) ){
return -1;
}
return 0;
if (*(Cash *) &(((Nsrt *) a)->t[0]) > *(Cash *) &(((Nsrt *) b)->t[0]))
return 1;
else if (*(Cash *) &(((Nsrt *) a)->t[0]) < *(Cash *) &(((Nsrt *) b)->t[0]))
return -1;
return 0;
}
static const gbtree_ninfo tinfo =
static const gbtree_ninfo tinfo =
{
gbt_t_cash,
sizeof(Cash),
gbt_cashgt,
gbt_cashge,
gbt_casheq,
gbt_cashle,
gbt_cashlt,
gbt_cashkey_cmp
gbt_t_cash,
sizeof(Cash),
gbt_cashgt,
gbt_cashge,
gbt_casheq,
gbt_cashle,
gbt_cashlt,
gbt_cashkey_cmp
};
@ -82,81 +85,83 @@ static const gbtree_ninfo tinfo =
Datum
gbt_cash_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER( gbt_num_compress( retval , entry , &tinfo ));
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER(gbt_num_compress(retval, entry, &tinfo));
}
Datum
gbt_cash_consistent(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
Cash query = (*((Cash *) PG_GETARG_POINTER(1)));
cashKEY *kkk = (cashKEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key ;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
key.lower = (GBT_NUMKEY*) &kkk->lower ;
key.upper = (GBT_NUMKEY*) &kkk->upper ;
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
Cash query = (*((Cash *) PG_GETARG_POINTER(1)));
cashKEY *kkk = (cashKEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
PG_RETURN_BOOL(
gbt_num_consistent( &key, (void*)&query,&strategy,GIST_LEAF(entry),&tinfo)
);
key.lower = (GBT_NUMKEY *) & kkk->lower;
key.upper = (GBT_NUMKEY *) & kkk->upper;
PG_RETURN_BOOL(
gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
);
}
Datum
gbt_cash_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc(sizeof(cashKEY));
*(int *) PG_GETARG_POINTER(1) = sizeof(cashKEY);
PG_RETURN_POINTER( gbt_num_union ( (void*)out, entryvec, &tinfo ) );
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc(sizeof(cashKEY));
*(int *) PG_GETARG_POINTER(1) = sizeof(cashKEY);
PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
}
Datum
gbt_cash_penalty(PG_FUNCTION_ARGS)
{
cashKEY *origentry = (cashKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
cashKEY *newentry = (cashKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
cashKEY *origentry = (cashKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
cashKEY *newentry = (cashKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
Cash res ;
Cash res;
*result = 0.0;
penalty_range_enlarge ( origentry->lower, origentry->upper, newentry->lower, newentry->upper );
*result = 0.0;
if ( res > 0 ){
*result += FLT_MIN ;
*result += (float) ( res / ( (double) ( res + origentry->upper - origentry->lower ) ) );
*result *= ( FLT_MAX / ( ( (GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1 ) );
}
penalty_range_enlarge(origentry->lower, origentry->upper, newentry->lower, newentry->upper);
PG_RETURN_POINTER(result);
if (res > 0)
{
*result += FLT_MIN;
*result += (float) (res / ((double) (res + origentry->upper - origentry->lower)));
*result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
}
PG_RETURN_POINTER(result);
}
Datum
gbt_cash_picksplit(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}
Datum
gbt_cash_same(PG_FUNCTION_ARGS)
{
cashKEY *b1 = (cashKEY *) PG_GETARG_POINTER(0);
cashKEY *b2 = (cashKEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
cashKEY *b1 = (cashKEY *) PG_GETARG_POINTER(0);
cashKEY *b2 = (cashKEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
*result = gbt_num_same ( (void*)b1, (void*)b2, &tinfo );
PG_RETURN_POINTER(result);
*result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
PG_RETURN_POINTER(result);
}

View File

@ -4,9 +4,9 @@
typedef struct
{
DateADT lower;
DateADT upper;
} dateKEY;
DateADT lower;
DateADT upper;
} dateKEY;
/*
** date ops
@ -18,46 +18,51 @@ PG_FUNCTION_INFO_V1(gbt_date_consistent);
PG_FUNCTION_INFO_V1(gbt_date_penalty);
PG_FUNCTION_INFO_V1(gbt_date_same);
Datum gbt_date_compress(PG_FUNCTION_ARGS);
Datum gbt_date_union(PG_FUNCTION_ARGS);
Datum gbt_date_picksplit(PG_FUNCTION_ARGS);
Datum gbt_date_consistent(PG_FUNCTION_ARGS);
Datum gbt_date_penalty(PG_FUNCTION_ARGS);
Datum gbt_date_same(PG_FUNCTION_ARGS);
Datum gbt_date_compress(PG_FUNCTION_ARGS);
Datum gbt_date_union(PG_FUNCTION_ARGS);
Datum gbt_date_picksplit(PG_FUNCTION_ARGS);
Datum gbt_date_consistent(PG_FUNCTION_ARGS);
Datum gbt_date_penalty(PG_FUNCTION_ARGS);
Datum gbt_date_same(PG_FUNCTION_ARGS);
static bool gbt_dategt (const void *a, const void *b)
static bool
gbt_dategt(const void *a, const void *b)
{
return DatumGetBool(
DirectFunctionCall2(date_gt,DateADTGetDatum( *((DateADT*)a) ), DateADTGetDatum( *((DateADT*)b) ) )
);
return DatumGetBool(
DirectFunctionCall2(date_gt, DateADTGetDatum(*((DateADT *) a)), DateADTGetDatum(*((DateADT *) b)))
);
}
static bool gbt_datege (const void *a, const void *b)
static bool
gbt_datege(const void *a, const void *b)
{
return DatumGetBool(
DirectFunctionCall2(date_ge,DateADTGetDatum( *((DateADT*)a) ), DateADTGetDatum( *((DateADT*)b) ) )
);
return DatumGetBool(
DirectFunctionCall2(date_ge, DateADTGetDatum(*((DateADT *) a)), DateADTGetDatum(*((DateADT *) b)))
);
}
static bool gbt_dateeq (const void *a, const void *b)
static bool
gbt_dateeq(const void *a, const void *b)
{
return DatumGetBool(
DirectFunctionCall2(date_eq,DateADTGetDatum( *((DateADT*)a) ), DateADTGetDatum( *((DateADT*)b) ) )
);
return DatumGetBool(
DirectFunctionCall2(date_eq, DateADTGetDatum(*((DateADT *) a)), DateADTGetDatum(*((DateADT *) b)))
);
}
static bool gbt_datele (const void *a, const void *b)
static bool
gbt_datele(const void *a, const void *b)
{
return DatumGetBool(
DirectFunctionCall2(date_le,DateADTGetDatum( *((DateADT*)a) ), DateADTGetDatum( *((DateADT*)b) ) )
);
return DatumGetBool(
DirectFunctionCall2(date_le, DateADTGetDatum(*((DateADT *) a)), DateADTGetDatum(*((DateADT *) b)))
);
}
static bool gbt_datelt (const void *a, const void *b)
static bool
gbt_datelt(const void *a, const void *b)
{
return DatumGetBool(
DirectFunctionCall2(date_lt,DateADTGetDatum( *((DateADT*)a) ), DateADTGetDatum( *((DateADT*)b) ) )
);
return DatumGetBool(
DirectFunctionCall2(date_lt, DateADTGetDatum(*((DateADT *) a)), DateADTGetDatum(*((DateADT *) b)))
);
}
@ -65,26 +70,24 @@ static bool gbt_datelt (const void *a, const void *b)
static int
gbt_datekey_cmp(const void *a, const void *b)
{
if ( gbt_dategt( (void*)&(((Nsrt *) a)->t[0]) , (void*)&(((Nsrt *) b)->t[0]) ) ){
return 1;
} else
if ( gbt_datelt( (void*)&(((Nsrt *) a)->t[0]) , (void*)&(((Nsrt *) b)->t[0]) ) ){
return -1;
}
return 0;
if (gbt_dategt((void *) &(((Nsrt *) a)->t[0]), (void *) &(((Nsrt *) b)->t[0])))
return 1;
else if (gbt_datelt((void *) &(((Nsrt *) a)->t[0]), (void *) &(((Nsrt *) b)->t[0])))
return -1;
return 0;
}
static const gbtree_ninfo tinfo =
static const gbtree_ninfo tinfo =
{
gbt_t_date,
sizeof(DateADT),
gbt_dategt,
gbt_datege,
gbt_dateeq,
gbt_datele,
gbt_datelt,
gbt_datekey_cmp
gbt_t_date,
sizeof(DateADT),
gbt_dategt,
gbt_datege,
gbt_dateeq,
gbt_datele,
gbt_datelt,
gbt_datekey_cmp
};
@ -97,9 +100,10 @@ static const gbtree_ninfo tinfo =
Datum
gbt_date_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER( gbt_num_compress( retval , entry , &tinfo ));
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER(gbt_num_compress(retval, entry, &tinfo));
}
@ -107,86 +111,89 @@ gbt_date_compress(PG_FUNCTION_ARGS)
Datum
gbt_date_consistent(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
DateADT query = PG_GETARG_DATEADT( 1 );
dateKEY *kkk = (dateKEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key ;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
DateADT query = PG_GETARG_DATEADT(1);
dateKEY *kkk = (dateKEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
key.lower = (GBT_NUMKEY*) &kkk->lower ;
key.upper = (GBT_NUMKEY*) &kkk->upper ;
PG_RETURN_BOOL(
gbt_num_consistent( &key, (void*)&query,&strategy,GIST_LEAF(entry),&tinfo)
);
key.lower = (GBT_NUMKEY *) & kkk->lower;
key.upper = (GBT_NUMKEY *) & kkk->upper;
PG_RETURN_BOOL(
gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
);
}
Datum
gbt_date_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc(sizeof(dateKEY));
*(int *) PG_GETARG_POINTER(1) = sizeof(dateKEY);
PG_RETURN_POINTER( gbt_num_union ( (void*)out, entryvec, &tinfo ) );
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc(sizeof(dateKEY));
*(int *) PG_GETARG_POINTER(1) = sizeof(dateKEY);
PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
}
Datum
gbt_date_penalty(PG_FUNCTION_ARGS)
{
dateKEY *origentry = (dateKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
dateKEY *newentry = (dateKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
int32 diff, res ;
dateKEY *origentry = (dateKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
dateKEY *newentry = (dateKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
int32 diff,
res;
diff = DatumGetInt32(DirectFunctionCall2(
date_mi,
DateADTGetDatum(newentry->upper),
DateADTGetDatum(origentry->upper)));
diff = DatumGetInt32(DirectFunctionCall2(
date_mi,
DateADTGetDatum(newentry->upper),
DateADTGetDatum(origentry->upper)));
res = Max(diff, 0);
res = Max(diff, 0);
diff = DatumGetInt32(DirectFunctionCall2(
date_mi,
DateADTGetDatum(origentry->lower),
DateADTGetDatum(newentry->lower)));
diff = DatumGetInt32(DirectFunctionCall2(
date_mi,
DateADTGetDatum(origentry->lower),
DateADTGetDatum(newentry->lower)));
res += Max(diff, 0);
res += Max(diff, 0);
*result = 0.0;
*result = 0.0;
if ( res > 0 ){
diff = DatumGetInt32(DirectFunctionCall2(
date_mi,
DateADTGetDatum(origentry->upper),
DateADTGetDatum(origentry->lower)));
*result += FLT_MIN ;
*result += (float) ( res / ( (double) ( res + diff ) ) );
*result *= ( FLT_MAX / ( ( (GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1 ) );
}
if (res > 0)
{
diff = DatumGetInt32(DirectFunctionCall2(
date_mi,
DateADTGetDatum(origentry->upper),
DateADTGetDatum(origentry->lower)));
*result += FLT_MIN;
*result += (float) (res / ((double) (res + diff)));
*result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
}
PG_RETURN_POINTER(result);
PG_RETURN_POINTER(result);
}
Datum
gbt_date_picksplit(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}
Datum
gbt_date_same(PG_FUNCTION_ARGS)
{
dateKEY *b1 = (dateKEY *) PG_GETARG_POINTER(0);
dateKEY *b2 = (dateKEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
dateKEY *b1 = (dateKEY *) PG_GETARG_POINTER(0);
dateKEY *b2 = (dateKEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
*result = gbt_num_same ( (void*)b1, (void*)b2, &tinfo );
PG_RETURN_POINTER(result);
*result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
PG_RETURN_POINTER(result);
}

View File

@ -3,9 +3,9 @@
typedef struct float4key
{
float4 lower;
float4 upper;
} float4KEY;
float4 lower;
float4 upper;
} float4KEY;
/*
** float4 ops
@ -17,59 +17,62 @@ PG_FUNCTION_INFO_V1(gbt_float4_consistent);
PG_FUNCTION_INFO_V1(gbt_float4_penalty);
PG_FUNCTION_INFO_V1(gbt_float4_same);
Datum gbt_float4_compress(PG_FUNCTION_ARGS);
Datum gbt_float4_union(PG_FUNCTION_ARGS);
Datum gbt_float4_picksplit(PG_FUNCTION_ARGS);
Datum gbt_float4_consistent(PG_FUNCTION_ARGS);
Datum gbt_float4_penalty(PG_FUNCTION_ARGS);
Datum gbt_float4_same(PG_FUNCTION_ARGS);
Datum gbt_float4_compress(PG_FUNCTION_ARGS);
Datum gbt_float4_union(PG_FUNCTION_ARGS);
Datum gbt_float4_picksplit(PG_FUNCTION_ARGS);
Datum gbt_float4_consistent(PG_FUNCTION_ARGS);
Datum gbt_float4_penalty(PG_FUNCTION_ARGS);
Datum gbt_float4_same(PG_FUNCTION_ARGS);
static bool gbt_float4gt (const void *a, const void *b)
static bool
gbt_float4gt(const void *a, const void *b)
{
return ( *((float4*)a) > *((float4*)b) );
return (*((float4 *) a) > *((float4 *) b));
}
static bool gbt_float4ge (const void *a, const void *b)
static bool
gbt_float4ge(const void *a, const void *b)
{
return ( *((float4*)a) >= *((float4*)b) );
return (*((float4 *) a) >= *((float4 *) b));
}
static bool gbt_float4eq (const void *a, const void *b)
static bool
gbt_float4eq(const void *a, const void *b)
{
return ( *((float4*)a) == *((float4*)b) );
return (*((float4 *) a) == *((float4 *) b));
}
static bool gbt_float4le (const void *a, const void *b)
static bool
gbt_float4le(const void *a, const void *b)
{
return ( *((float4*)a) <= *((float4*)b) );
return (*((float4 *) a) <= *((float4 *) b));
}
static bool gbt_float4lt (const void *a, const void *b)
static bool
gbt_float4lt(const void *a, const void *b)
{
return ( *((float4*)a) < *((float4*)b) );
return (*((float4 *) a) < *((float4 *) b));
}
static int
gbt_float4key_cmp(const void *a, const void *b)
{
if ( *(float4*)&(((Nsrt *) a)->t[0]) > *(float4*)&(((Nsrt *) b)->t[0]) ){
return 1;
} else
if ( *(float4*)&(((Nsrt *) a)->t[0]) < *(float4*)&(((Nsrt *) b)->t[0]) ){
return -1;
}
return 0;
if (*(float4 *) &(((Nsrt *) a)->t[0]) > *(float4 *) &(((Nsrt *) b)->t[0]))
return 1;
else if (*(float4 *) &(((Nsrt *) a)->t[0]) < *(float4 *) &(((Nsrt *) b)->t[0]))
return -1;
return 0;
}
static const gbtree_ninfo tinfo =
{
gbt_t_float4,
sizeof(float4),
gbt_float4gt,
gbt_float4ge,
gbt_float4eq,
gbt_float4le,
gbt_float4lt,
gbt_float4key_cmp
static const gbtree_ninfo tinfo =
{
gbt_t_float4,
sizeof(float4),
gbt_float4gt,
gbt_float4ge,
gbt_float4eq,
gbt_float4le,
gbt_float4lt,
gbt_float4key_cmp
};
@ -81,80 +84,83 @@ static const gbtree_ninfo tinfo =
Datum
gbt_float4_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER( gbt_num_compress( retval , entry , &tinfo ));
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER(gbt_num_compress(retval, entry, &tinfo));
}
Datum
gbt_float4_consistent(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
float4 query = PG_GETARG_FLOAT4(1);
float4KEY *kkk = (float4KEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key ;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
key.lower = (GBT_NUMKEY*) &kkk->lower ;
key.upper = (GBT_NUMKEY*) &kkk->upper ;
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
float4 query = PG_GETARG_FLOAT4(1);
float4KEY *kkk = (float4KEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
PG_RETURN_BOOL(
gbt_num_consistent( &key, (void*)&query,&strategy,GIST_LEAF(entry),&tinfo)
);
key.lower = (GBT_NUMKEY *) & kkk->lower;
key.upper = (GBT_NUMKEY *) & kkk->upper;
PG_RETURN_BOOL(
gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
);
}
Datum
gbt_float4_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc(sizeof(float4KEY));
*(int *) PG_GETARG_POINTER(1) = sizeof(float4KEY);
PG_RETURN_POINTER( gbt_num_union ( (void*)out, entryvec, &tinfo ) );
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc(sizeof(float4KEY));
*(int *) PG_GETARG_POINTER(1) = sizeof(float4KEY);
PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
}
Datum
gbt_float4_penalty(PG_FUNCTION_ARGS)
{
float4KEY *origentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
float4KEY *newentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
float4KEY *origentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
float4KEY *newentry = (float4KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
float4 res ;
float4 res;
*result = 0.0;
penalty_range_enlarge ( origentry->lower, origentry->upper, newentry->lower, newentry->upper );
*result = 0.0;
if ( res > 0 ){
*result += FLT_MIN ;
*result += (float) ( res / ( (double) ( res + origentry->upper - origentry->lower ) ) );
*result *= ( FLT_MAX / ( ( (GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1 ) );
}
penalty_range_enlarge(origentry->lower, origentry->upper, newentry->lower, newentry->upper);
PG_RETURN_POINTER(result);
if (res > 0)
{
*result += FLT_MIN;
*result += (float) (res / ((double) (res + origentry->upper - origentry->lower)));
*result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
}
PG_RETURN_POINTER(result);
}
Datum
gbt_float4_picksplit(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}
Datum
gbt_float4_same(PG_FUNCTION_ARGS)
{
float4KEY *b1 = (float4KEY *) PG_GETARG_POINTER(0);
float4KEY *b2 = (float4KEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
float4KEY *b1 = (float4KEY *) PG_GETARG_POINTER(0);
float4KEY *b2 = (float4KEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
*result = gbt_num_same ( (void*)b1, (void*)b2, &tinfo );
PG_RETURN_POINTER(result);
*result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
PG_RETURN_POINTER(result);
}

View File

@ -3,9 +3,9 @@
typedef struct float8key
{
float8 lower;
float8 upper;
} float8KEY;
float8 lower;
float8 upper;
} float8KEY;
/*
** float8 ops
@ -17,60 +17,63 @@ PG_FUNCTION_INFO_V1(gbt_float8_consistent);
PG_FUNCTION_INFO_V1(gbt_float8_penalty);
PG_FUNCTION_INFO_V1(gbt_float8_same);
Datum gbt_float8_compress(PG_FUNCTION_ARGS);
Datum gbt_float8_union(PG_FUNCTION_ARGS);
Datum gbt_float8_picksplit(PG_FUNCTION_ARGS);
Datum gbt_float8_consistent(PG_FUNCTION_ARGS);
Datum gbt_float8_penalty(PG_FUNCTION_ARGS);
Datum gbt_float8_same(PG_FUNCTION_ARGS);
Datum gbt_float8_compress(PG_FUNCTION_ARGS);
Datum gbt_float8_union(PG_FUNCTION_ARGS);
Datum gbt_float8_picksplit(PG_FUNCTION_ARGS);
Datum gbt_float8_consistent(PG_FUNCTION_ARGS);
Datum gbt_float8_penalty(PG_FUNCTION_ARGS);
Datum gbt_float8_same(PG_FUNCTION_ARGS);
static bool gbt_float8gt (const void *a, const void *b)
static bool
gbt_float8gt(const void *a, const void *b)
{
return ( *((float8*)a) > *((float8*)b) );
return (*((float8 *) a) > *((float8 *) b));
}
static bool gbt_float8ge (const void *a, const void *b)
static bool
gbt_float8ge(const void *a, const void *b)
{
return ( *((float8*)a) >= *((float8*)b) );
return (*((float8 *) a) >= *((float8 *) b));
}
static bool gbt_float8eq (const void *a, const void *b)
static bool
gbt_float8eq(const void *a, const void *b)
{
return ( *((float8*)a) == *((float8*)b) );
return (*((float8 *) a) == *((float8 *) b));
}
static bool gbt_float8le (const void *a, const void *b)
static bool
gbt_float8le(const void *a, const void *b)
{
return ( *((float8*)a) <= *((float8*)b) );
return (*((float8 *) a) <= *((float8 *) b));
}
static bool gbt_float8lt (const void *a, const void *b)
static bool
gbt_float8lt(const void *a, const void *b)
{
return ( *((float8*)a) < *((float8*)b) );
return (*((float8 *) a) < *((float8 *) b));
}
static int
gbt_float8key_cmp(const void *a, const void *b)
{
if ( *(float8*)&(((Nsrt *) a)->t[0]) > *(float8*)&(((Nsrt *) b)->t[0]) ){
return 1;
} else
if ( *(float8*)&(((Nsrt *) a)->t[0]) < *(float8*)&(((Nsrt *) b)->t[0]) ){
return -1;
}
return 0;
if (*(float8 *) &(((Nsrt *) a)->t[0]) > *(float8 *) &(((Nsrt *) b)->t[0]))
return 1;
else if (*(float8 *) &(((Nsrt *) a)->t[0]) < *(float8 *) &(((Nsrt *) b)->t[0]))
return -1;
return 0;
}
static const gbtree_ninfo tinfo =
{
gbt_t_float8,
sizeof(float8),
gbt_float8gt,
gbt_float8ge,
gbt_float8eq,
gbt_float8le,
gbt_float8lt,
gbt_float8key_cmp
static const gbtree_ninfo tinfo =
{
gbt_t_float8,
sizeof(float8),
gbt_float8gt,
gbt_float8ge,
gbt_float8eq,
gbt_float8le,
gbt_float8lt,
gbt_float8key_cmp
};
@ -82,9 +85,10 @@ static const gbtree_ninfo tinfo =
Datum
gbt_float8_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER( gbt_num_compress( retval , entry , &tinfo ));
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER(gbt_num_compress(retval, entry, &tinfo));
}
@ -92,70 +96,73 @@ Datum
gbt_float8_consistent(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
float8 query = PG_GETARG_FLOAT8(1);
float8KEY *kkk = (float8KEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key ;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
key.lower = (GBT_NUMKEY*) &kkk->lower ;
key.upper = (GBT_NUMKEY*) &kkk->upper ;
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
float8 query = PG_GETARG_FLOAT8(1);
float8KEY *kkk = (float8KEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
PG_RETURN_BOOL(
gbt_num_consistent( &key, (void*)&query,&strategy,GIST_LEAF(entry),&tinfo)
);
key.lower = (GBT_NUMKEY *) & kkk->lower;
key.upper = (GBT_NUMKEY *) & kkk->upper;
PG_RETURN_BOOL(
gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
);
}
Datum
gbt_float8_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc(sizeof(float8KEY));
*(int *) PG_GETARG_POINTER(1) = sizeof(float8KEY);
PG_RETURN_POINTER( gbt_num_union ( (void*)out, entryvec, &tinfo ) );
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc(sizeof(float8KEY));
*(int *) PG_GETARG_POINTER(1) = sizeof(float8KEY);
PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
}
Datum
gbt_float8_penalty(PG_FUNCTION_ARGS)
{
float8KEY *origentry = (float8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
float8KEY *newentry = (float8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
float8KEY *origentry = (float8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
float8KEY *newentry = (float8KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
float8 res ;
float8 res;
*result = 0.0;
penalty_range_enlarge ( origentry->lower, origentry->upper, newentry->lower, newentry->upper );
*result = 0.0;
if ( res > 0 ){
*result += FLT_MIN ;
*result += (float) ( res / ( (double) ( res + origentry->upper - origentry->lower ) ) );
*result *= ( FLT_MAX / ( ( (GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1 ) );
}
penalty_range_enlarge(origentry->lower, origentry->upper, newentry->lower, newentry->upper);
PG_RETURN_POINTER(result);
if (res > 0)
{
*result += FLT_MIN;
*result += (float) (res / ((double) (res + origentry->upper - origentry->lower)));
*result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
}
PG_RETURN_POINTER(result);
}
Datum
gbt_float8_picksplit(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}
Datum
gbt_float8_same(PG_FUNCTION_ARGS)
{
float8KEY *b1 = (float8KEY *) PG_GETARG_POINTER(0);
float8KEY *b2 = (float8KEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
float8KEY *b1 = (float8KEY *) PG_GETARG_POINTER(0);
float8KEY *b2 = (float8KEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
*result = gbt_num_same ( (void*)b1, (void*)b2, &tinfo );
PG_RETURN_POINTER(result);
*result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
PG_RETURN_POINTER(result);
}

View File

@ -1,24 +1,24 @@
#include "btree_gist.h"
PG_FUNCTION_INFO_V1(gbt_decompress);
PG_FUNCTION_INFO_V1(gbtreekey_in);
PG_FUNCTION_INFO_V1(gbtreekey_in);
PG_FUNCTION_INFO_V1(gbtreekey_out);
Datum gbt_decompress(PG_FUNCTION_ARGS);
Datum gbt_decompress(PG_FUNCTION_ARGS);
/**************************************************
* In/Out for keys
**************************************************/
Datum
gbtreekey_in(PG_FUNCTION_ARGS)
{
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("<datatype>key_in() not implemented")));
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("<datatype>key_in() not implemented")));
PG_RETURN_POINTER(NULL);
PG_RETURN_POINTER(NULL);
}
#include "btree_utils_var.h"
@ -26,10 +26,10 @@ gbtreekey_in(PG_FUNCTION_ARGS)
Datum
gbtreekey_out(PG_FUNCTION_ARGS)
{
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("<datatype>key_out() not implemented")));
PG_RETURN_POINTER(NULL);
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("<datatype>key_out() not implemented")));
PG_RETURN_POINTER(NULL);
}
@ -40,6 +40,5 @@ gbtreekey_out(PG_FUNCTION_ARGS)
Datum
gbt_decompress(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(PG_GETARG_POINTER(0));
PG_RETURN_POINTER(PG_GETARG_POINTER(0));
}

View File

@ -7,25 +7,25 @@
enum gbtree_type
{
gbt_t_var ,
gbt_t_int2 ,
gbt_t_int4 ,
gbt_t_int8 ,
gbt_t_float4 ,
gbt_t_float8 ,
gbt_t_numeric,
gbt_t_ts,
gbt_t_cash,
gbt_t_oid,
gbt_t_time,
gbt_t_date,
gbt_t_intv,
gbt_t_macad,
gbt_t_text,
gbt_t_bpchar,
gbt_t_bytea,
gbt_t_bit,
gbt_t_inet
gbt_t_var,
gbt_t_int2,
gbt_t_int4,
gbt_t_int8,
gbt_t_float4,
gbt_t_float8,
gbt_t_numeric,
gbt_t_ts,
gbt_t_cash,
gbt_t_oid,
gbt_t_time,
gbt_t_date,
gbt_t_intv,
gbt_t_macad,
gbt_t_text,
gbt_t_bpchar,
gbt_t_bytea,
gbt_t_bit,
gbt_t_inet
};
@ -34,6 +34,6 @@ enum gbtree_type
* Generic btree functions
*/
Datum gbtreekey_in (PG_FUNCTION_ARGS);
Datum gbtreekey_in(PG_FUNCTION_ARGS);
Datum gbtreekey_out(PG_FUNCTION_ARGS);
Datum gbtreekey_out(PG_FUNCTION_ARGS);

View File

@ -6,9 +6,9 @@
typedef struct inetkey
{
double lower;
double upper;
} inetKEY;
double lower;
double upper;
} inetKEY;
/*
** inet ops
@ -22,62 +22,65 @@ PG_FUNCTION_INFO_V1(gbt_cidr_consistent);
PG_FUNCTION_INFO_V1(gbt_inet_penalty);
PG_FUNCTION_INFO_V1(gbt_inet_same);
Datum gbt_inet_compress(PG_FUNCTION_ARGS);
Datum gbt_cidr_compress(PG_FUNCTION_ARGS);
Datum gbt_inet_union(PG_FUNCTION_ARGS);
Datum gbt_inet_picksplit(PG_FUNCTION_ARGS);
Datum gbt_inet_consistent(PG_FUNCTION_ARGS);
Datum gbt_cidr_consistent(PG_FUNCTION_ARGS);
Datum gbt_inet_penalty(PG_FUNCTION_ARGS);
Datum gbt_inet_same(PG_FUNCTION_ARGS);
Datum gbt_inet_compress(PG_FUNCTION_ARGS);
Datum gbt_cidr_compress(PG_FUNCTION_ARGS);
Datum gbt_inet_union(PG_FUNCTION_ARGS);
Datum gbt_inet_picksplit(PG_FUNCTION_ARGS);
Datum gbt_inet_consistent(PG_FUNCTION_ARGS);
Datum gbt_cidr_consistent(PG_FUNCTION_ARGS);
Datum gbt_inet_penalty(PG_FUNCTION_ARGS);
Datum gbt_inet_same(PG_FUNCTION_ARGS);
static bool gbt_inetgt (const void *a, const void *b)
static bool
gbt_inetgt(const void *a, const void *b)
{
return ( *((double*)a) > *((double*)b) );
return (*((double *) a) > *((double *) b));
}
static bool gbt_inetge (const void *a, const void *b)
static bool
gbt_inetge(const void *a, const void *b)
{
return ( *((double*)a) >= *((double*)b) );
return (*((double *) a) >= *((double *) b));
}
static bool gbt_ineteq (const void *a, const void *b)
static bool
gbt_ineteq(const void *a, const void *b)
{
return ( *((double*)a) == *((double*)b) );
return (*((double *) a) == *((double *) b));
}
static bool gbt_inetle (const void *a, const void *b)
static bool
gbt_inetle(const void *a, const void *b)
{
return ( *((double*)a) <= *((double*)b) );
return (*((double *) a) <= *((double *) b));
}
static bool gbt_inetlt (const void *a, const void *b)
static bool
gbt_inetlt(const void *a, const void *b)
{
return ( *((double*)a) < *((double*)b) );
return (*((double *) a) < *((double *) b));
}
static int
gbt_inetkey_cmp(const void *a, const void *b)
{
if ( *(double*)(&((Nsrt *) a)->t[0]) > *(double*)(&((Nsrt *) b)->t[0]) ){
return 1;
} else
if ( *(double*)(&((Nsrt *) a)->t[0]) < *(double*)(&((Nsrt *) b)->t[0]) ){
return -1;
}
return 0;
if (*(double *) (&((Nsrt *) a)->t[0]) > *(double *) (&((Nsrt *) b)->t[0]))
return 1;
else if (*(double *) (&((Nsrt *) a)->t[0]) < *(double *) (&((Nsrt *) b)->t[0]))
return -1;
return 0;
}
static const gbtree_ninfo tinfo =
static const gbtree_ninfo tinfo =
{
gbt_t_inet,
sizeof(double),
gbt_inetgt,
gbt_inetge,
gbt_ineteq,
gbt_inetle,
gbt_inetlt,
gbt_inetkey_cmp
gbt_t_inet,
sizeof(double),
gbt_inetgt,
gbt_inetge,
gbt_ineteq,
gbt_inetle,
gbt_inetlt,
gbt_inetkey_cmp
};
@ -87,24 +90,25 @@ static const gbtree_ninfo tinfo =
static GISTENTRY *
gbt_inet_compress_inetrnal(GISTENTRY *retval , GISTENTRY *entry , Oid typid)
static GISTENTRY *
gbt_inet_compress_inetrnal(GISTENTRY *retval, GISTENTRY *entry, Oid typid)
{
if (entry->leafkey)
{
inetKEY *r = (inetKEY *) palloc(sizeof(inetKEY));
retval = palloc(sizeof(GISTENTRY));
r->lower = convert_network_to_scalar(entry->key, typid );
r->upper = r->lower ;
gistentryinit(*retval, PointerGetDatum(r),
entry->rel, entry->page,
entry->offset, sizeof(inetKEY), FALSE);
}
else
retval = entry;
return ( retval );
if (entry->leafkey)
{
inetKEY *r = (inetKEY *) palloc(sizeof(inetKEY));
retval = palloc(sizeof(GISTENTRY));
r->lower = convert_network_to_scalar(entry->key, typid);
r->upper = r->lower;
gistentryinit(*retval, PointerGetDatum(r),
entry->rel, entry->page,
entry->offset, sizeof(inetKEY), FALSE);
}
else
retval = entry;
return (retval);
}
@ -112,113 +116,118 @@ gbt_inet_compress_inetrnal(GISTENTRY *retval , GISTENTRY *entry , Oid typid)
Datum
gbt_inet_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER( gbt_inet_compress_inetrnal(retval ,entry ,INETOID ) );
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER(gbt_inet_compress_inetrnal(retval, entry, INETOID));
}
Datum
gbt_cidr_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER( gbt_inet_compress_inetrnal(retval ,entry ,CIDROID ) );
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER(gbt_inet_compress_inetrnal(retval, entry, CIDROID));
}
static bool
gbt_inet_consistent_internal (
const GISTENTRY * entry,
const double * query,
const StrategyNumber * strategy
){
inetKEY *kkk = (inetKEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key ;
gbt_inet_consistent_internal(
const GISTENTRY *entry,
const double *query,
const StrategyNumber *strategy
)
{
inetKEY *kkk = (inetKEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key;
key.lower = (GBT_NUMKEY*) &kkk->lower ;
key.upper = (GBT_NUMKEY*) &kkk->upper ;
return (
gbt_num_consistent( &key, (void*)query,strategy,GIST_LEAF(entry),&tinfo)
);
key.lower = (GBT_NUMKEY *) & kkk->lower;
key.upper = (GBT_NUMKEY *) & kkk->upper;
return (
gbt_num_consistent(&key, (void *) query, strategy, GIST_LEAF(entry), &tinfo)
);
}
Datum
gbt_inet_consistent(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
double query = convert_network_to_scalar( PG_GETARG_DATUM(1) ,INETOID );
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
double query = convert_network_to_scalar(PG_GETARG_DATUM(1), INETOID);
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
PG_RETURN_BOOL(
gbt_inet_consistent_internal ( entry, &query, &strategy )
);
PG_RETURN_BOOL(
gbt_inet_consistent_internal(entry, &query, &strategy)
);
}
Datum
gbt_cidr_consistent(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
double query = convert_network_to_scalar( PG_GETARG_DATUM(1) ,CIDROID );
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
double query = convert_network_to_scalar(PG_GETARG_DATUM(1), CIDROID);
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
PG_RETURN_BOOL(
gbt_inet_consistent_internal ( entry, &query, &strategy )
);
PG_RETURN_BOOL(
gbt_inet_consistent_internal(entry, &query, &strategy)
);
}
Datum
gbt_inet_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc(sizeof(inetKEY));
*(int *) PG_GETARG_POINTER(1) = sizeof(inetKEY);
PG_RETURN_POINTER( gbt_num_union ( (void*)out, entryvec, &tinfo ) );
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc(sizeof(inetKEY));
*(int *) PG_GETARG_POINTER(1) = sizeof(inetKEY);
PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
}
Datum
gbt_inet_penalty(PG_FUNCTION_ARGS)
{
inetKEY *origentry = (inetKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
inetKEY *newentry = (inetKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
inetKEY *origentry = (inetKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
inetKEY *newentry = (inetKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
double res ;
double res;
*result = 0.0;
*result = 0.0;
penalty_range_enlarge ( origentry->lower, origentry->upper, newentry->lower, newentry->upper );
penalty_range_enlarge(origentry->lower, origentry->upper, newentry->lower, newentry->upper);
if ( res > 0 ){
*result += FLT_MIN ;
*result += (float) ( res / ( (double) ( res + origentry->upper - origentry->lower ) ) );
*result *= ( FLT_MAX / ( ( (GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1 ) );
}
if (res > 0)
{
*result += FLT_MIN;
*result += (float) (res / ((double) (res + origentry->upper - origentry->lower)));
*result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
}
PG_RETURN_POINTER(result);
PG_RETURN_POINTER(result);
}
Datum
gbt_inet_picksplit(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}
Datum
gbt_inet_same(PG_FUNCTION_ARGS)
{
inetKEY *b1 = (inetKEY *) PG_GETARG_POINTER(0);
inetKEY *b2 = (inetKEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
inetKEY *b1 = (inetKEY *) PG_GETARG_POINTER(0);
inetKEY *b2 = (inetKEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
*result = gbt_num_same ( (void*)b1, (void*)b2, &tinfo );
PG_RETURN_POINTER(result);
*result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
PG_RETURN_POINTER(result);
}

View File

@ -3,9 +3,9 @@
typedef struct int16key
{
int16 lower;
int16 upper;
} int16KEY;
int16 lower;
int16 upper;
} int16KEY;
/*
** int16 ops
@ -17,59 +17,62 @@ PG_FUNCTION_INFO_V1(gbt_int2_consistent);
PG_FUNCTION_INFO_V1(gbt_int2_penalty);
PG_FUNCTION_INFO_V1(gbt_int2_same);
Datum gbt_int2_compress(PG_FUNCTION_ARGS);
Datum gbt_int2_union(PG_FUNCTION_ARGS);
Datum gbt_int2_picksplit(PG_FUNCTION_ARGS);
Datum gbt_int2_consistent(PG_FUNCTION_ARGS);
Datum gbt_int2_penalty(PG_FUNCTION_ARGS);
Datum gbt_int2_same(PG_FUNCTION_ARGS);
Datum gbt_int2_compress(PG_FUNCTION_ARGS);
Datum gbt_int2_union(PG_FUNCTION_ARGS);
Datum gbt_int2_picksplit(PG_FUNCTION_ARGS);
Datum gbt_int2_consistent(PG_FUNCTION_ARGS);
Datum gbt_int2_penalty(PG_FUNCTION_ARGS);
Datum gbt_int2_same(PG_FUNCTION_ARGS);
static bool gbt_int2gt (const void *a, const void *b)
static bool
gbt_int2gt(const void *a, const void *b)
{
return ( *((int16*)a) > *((int16*)b) );
return (*((int16 *) a) > *((int16 *) b));
}
static bool gbt_int2ge (const void *a, const void *b)
static bool
gbt_int2ge(const void *a, const void *b)
{
return ( *((int16*)a) >= *((int16*)b) );
return (*((int16 *) a) >= *((int16 *) b));
}
static bool gbt_int2eq (const void *a, const void *b)
static bool
gbt_int2eq(const void *a, const void *b)
{
return ( *((int16*)a) == *((int16*)b) );
return (*((int16 *) a) == *((int16 *) b));
}
static bool gbt_int2le (const void *a, const void *b)
static bool
gbt_int2le(const void *a, const void *b)
{
return ( *((int16*)a) <= *((int16*)b) );
return (*((int16 *) a) <= *((int16 *) b));
}
static bool gbt_int2lt (const void *a, const void *b)
static bool
gbt_int2lt(const void *a, const void *b)
{
return ( *((int16*)a) < *((int16*)b) );
return (*((int16 *) a) < *((int16 *) b));
}
static int
gbt_int2key_cmp(const void *a, const void *b)
{
if ( *(int16*)(&((Nsrt *) a)->t[0]) > *(int16*)&(((Nsrt *) b)->t[0]) ){
return 1;
} else
if ( *(int16*)&(((Nsrt *) a)->t[0]) < *(int16*)&(((Nsrt *) b)->t[0]) ){
return -1;
}
return 0;
if (*(int16 *) (&((Nsrt *) a)->t[0]) > *(int16 *) &(((Nsrt *) b)->t[0]))
return 1;
else if (*(int16 *) &(((Nsrt *) a)->t[0]) < *(int16 *) &(((Nsrt *) b)->t[0]))
return -1;
return 0;
}
static const gbtree_ninfo tinfo =
{
gbt_t_int2,
sizeof(int16),
gbt_int2gt,
gbt_int2ge,
gbt_int2eq,
gbt_int2le,
gbt_int2lt,
gbt_int2key_cmp
static const gbtree_ninfo tinfo =
{
gbt_t_int2,
sizeof(int16),
gbt_int2gt,
gbt_int2ge,
gbt_int2eq,
gbt_int2le,
gbt_int2lt,
gbt_int2key_cmp
};
@ -85,77 +88,81 @@ static const gbtree_ninfo tinfo =
Datum
gbt_int2_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER( gbt_num_compress( retval , entry , &tinfo ));
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER(gbt_num_compress(retval, entry, &tinfo));
}
Datum
gbt_int2_consistent(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
int16 query = PG_GETARG_INT16(1);
int16KEY *kkk = (int16KEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key ;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
key.lower = (GBT_NUMKEY*) &kkk->lower ;
key.upper = (GBT_NUMKEY*) &kkk->upper ;
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
int16 query = PG_GETARG_INT16(1);
int16KEY *kkk = (int16KEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
PG_RETURN_BOOL(
gbt_num_consistent( &key, (void*)&query,&strategy,GIST_LEAF(entry),&tinfo)
);
key.lower = (GBT_NUMKEY *) & kkk->lower;
key.upper = (GBT_NUMKEY *) & kkk->upper;
PG_RETURN_BOOL(
gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
);
}
Datum
gbt_int2_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc(sizeof(int16KEY));
*(int *) PG_GETARG_POINTER(1) = sizeof(int16KEY);
PG_RETURN_POINTER( gbt_num_union ( (void*)out, entryvec, &tinfo ) );
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc(sizeof(int16KEY));
*(int *) PG_GETARG_POINTER(1) = sizeof(int16KEY);
PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
}
Datum
gbt_int2_penalty(PG_FUNCTION_ARGS)
{
int16KEY *origentry = (int16KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
int16KEY *newentry = (int16KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
int2 res ;
int16KEY *origentry = (int16KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
int16KEY *newentry = (int16KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
int2 res;
*result = 0.0;
*result = 0.0;
penalty_range_enlarge ( origentry->lower, origentry->upper, newentry->lower, newentry->upper );
penalty_range_enlarge(origentry->lower, origentry->upper, newentry->lower, newentry->upper);
if ( res > 0 ){
*result += FLT_MIN ;
*result += (float) ( res / ( (double) ( res + origentry->upper - origentry->lower ) ) );
*result *= ( FLT_MAX / ( ( (GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1 ) );
}
if (res > 0)
{
*result += FLT_MIN;
*result += (float) (res / ((double) (res + origentry->upper - origentry->lower)));
*result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
}
PG_RETURN_POINTER(result);
PG_RETURN_POINTER(result);
}
Datum
gbt_int2_picksplit(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}
Datum
gbt_int2_same(PG_FUNCTION_ARGS)
{
int16KEY *b1 = (int16KEY *) PG_GETARG_POINTER(0);
int16KEY *b2 = (int16KEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
int16KEY *b1 = (int16KEY *) PG_GETARG_POINTER(0);
int16KEY *b2 = (int16KEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
*result = gbt_num_same ( (void*)b1, (void*)b2, &tinfo );
PG_RETURN_POINTER(result);
*result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
PG_RETURN_POINTER(result);
}

View File

@ -3,9 +3,9 @@
typedef struct int32key
{
int32 lower;
int32 upper;
} int32KEY;
int32 lower;
int32 upper;
} int32KEY;
/*
** int32 ops
@ -17,60 +17,63 @@ PG_FUNCTION_INFO_V1(gbt_int4_consistent);
PG_FUNCTION_INFO_V1(gbt_int4_penalty);
PG_FUNCTION_INFO_V1(gbt_int4_same);
Datum gbt_int4_compress(PG_FUNCTION_ARGS);
Datum gbt_int4_union(PG_FUNCTION_ARGS);
Datum gbt_int4_picksplit(PG_FUNCTION_ARGS);
Datum gbt_int4_consistent(PG_FUNCTION_ARGS);
Datum gbt_int4_penalty(PG_FUNCTION_ARGS);
Datum gbt_int4_same(PG_FUNCTION_ARGS);
Datum gbt_int4_compress(PG_FUNCTION_ARGS);
Datum gbt_int4_union(PG_FUNCTION_ARGS);
Datum gbt_int4_picksplit(PG_FUNCTION_ARGS);
Datum gbt_int4_consistent(PG_FUNCTION_ARGS);
Datum gbt_int4_penalty(PG_FUNCTION_ARGS);
Datum gbt_int4_same(PG_FUNCTION_ARGS);
static bool gbt_int4gt (const void *a, const void *b)
static bool
gbt_int4gt(const void *a, const void *b)
{
return ( *((int32*)a) > *((int32*)b) );
return (*((int32 *) a) > *((int32 *) b));
}
static bool gbt_int4ge (const void *a, const void *b)
static bool
gbt_int4ge(const void *a, const void *b)
{
return ( *((int32*)a) >= *((int32*)b) );
return (*((int32 *) a) >= *((int32 *) b));
}
static bool gbt_int4eq (const void *a, const void *b)
static bool
gbt_int4eq(const void *a, const void *b)
{
return ( *((int32*)a) == *((int32*)b) );
return (*((int32 *) a) == *((int32 *) b));
}
static bool gbt_int4le (const void *a, const void *b)
static bool
gbt_int4le(const void *a, const void *b)
{
return ( *((int32*)a) <= *((int32*)b) );
return (*((int32 *) a) <= *((int32 *) b));
}
static bool gbt_int4lt (const void *a, const void *b)
static bool
gbt_int4lt(const void *a, const void *b)
{
return ( *((int32*)a) < *((int32*)b) );
return (*((int32 *) a) < *((int32 *) b));
}
static int
gbt_int4key_cmp(const void *a, const void *b)
{
if ( *(int32*)&(((Nsrt *) a)->t[0]) > *(int32*)&(((Nsrt *) b)->t[0]) ){
return 1;
} else
if ( *(int32*)&(((Nsrt *) a)->t[0]) < *(int32*)&(((Nsrt *) b)->t[0]) ){
return -1;
}
return 0;
if (*(int32 *) &(((Nsrt *) a)->t[0]) > *(int32 *) &(((Nsrt *) b)->t[0]))
return 1;
else if (*(int32 *) &(((Nsrt *) a)->t[0]) < *(int32 *) &(((Nsrt *) b)->t[0]))
return -1;
return 0;
}
static const gbtree_ninfo tinfo =
{
gbt_t_int4,
sizeof(int32),
gbt_int4gt,
gbt_int4ge,
gbt_int4eq,
gbt_int4le,
gbt_int4lt,
gbt_int4key_cmp
static const gbtree_ninfo tinfo =
{
gbt_t_int4,
sizeof(int32),
gbt_int4gt,
gbt_int4ge,
gbt_int4eq,
gbt_int4le,
gbt_int4lt,
gbt_int4key_cmp
};
@ -82,9 +85,10 @@ static const gbtree_ninfo tinfo =
Datum
gbt_int4_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER( gbt_num_compress( retval , entry , &tinfo ));
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER(gbt_num_compress(retval, entry, &tinfo));
}
@ -92,68 +96,71 @@ Datum
gbt_int4_consistent(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
int32 query = PG_GETARG_INT32(1);
int32KEY *kkk = (int32KEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key ;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
key.lower = (GBT_NUMKEY*) &kkk->lower ;
key.upper = (GBT_NUMKEY*) &kkk->upper ;
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
int32 query = PG_GETARG_INT32(1);
int32KEY *kkk = (int32KEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
PG_RETURN_BOOL(
gbt_num_consistent( &key, (void*)&query,&strategy,GIST_LEAF(entry),&tinfo)
);
key.lower = (GBT_NUMKEY *) & kkk->lower;
key.upper = (GBT_NUMKEY *) & kkk->upper;
PG_RETURN_BOOL(
gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
);
}
Datum
gbt_int4_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc( sizeof(int32KEY) );
*(int *) PG_GETARG_POINTER(1) = sizeof(int32KEY);
PG_RETURN_POINTER( gbt_num_union ( (void*)out, entryvec, &tinfo ) );
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc(sizeof(int32KEY));
*(int *) PG_GETARG_POINTER(1) = sizeof(int32KEY);
PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
}
Datum
gbt_int4_penalty(PG_FUNCTION_ARGS)
{
int32KEY *origentry = (int32KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
int32KEY *newentry = (int32KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
int4 res ;
int32KEY *origentry = (int32KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
int32KEY *newentry = (int32KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
int4 res;
*result = 0.0;
*result = 0.0;
penalty_range_enlarge ( origentry->lower, origentry->upper, newentry->lower, newentry->upper );
penalty_range_enlarge(origentry->lower, origentry->upper, newentry->lower, newentry->upper);
if ( res > 0 ){
*result += FLT_MIN ;
*result += (float) ( res / ( (double) ( res + origentry->upper - origentry->lower ) ) );
*result *= ( FLT_MAX / ( ( (GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1 ) );
}
if (res > 0)
{
*result += FLT_MIN;
*result += (float) (res / ((double) (res + origentry->upper - origentry->lower)));
*result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
}
PG_RETURN_POINTER(result);
PG_RETURN_POINTER(result);
}
Datum
gbt_int4_picksplit(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}
Datum
gbt_int4_same(PG_FUNCTION_ARGS)
{
int32KEY *b1 = (int32KEY *) PG_GETARG_POINTER(0);
int32KEY *b2 = (int32KEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
int32KEY *b1 = (int32KEY *) PG_GETARG_POINTER(0);
int32KEY *b2 = (int32KEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
*result = gbt_num_same ( (void*)b1, (void*)b2, &tinfo );
PG_RETURN_POINTER(result);
*result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
PG_RETURN_POINTER(result);
}

View File

@ -3,9 +3,9 @@
typedef struct int64key
{
int64 lower;
int64 upper;
} int64KEY;
int64 lower;
int64 upper;
} int64KEY;
/*
** int64 ops
@ -17,60 +17,63 @@ PG_FUNCTION_INFO_V1(gbt_int8_consistent);
PG_FUNCTION_INFO_V1(gbt_int8_penalty);
PG_FUNCTION_INFO_V1(gbt_int8_same);
Datum gbt_int8_compress(PG_FUNCTION_ARGS);
Datum gbt_int8_union(PG_FUNCTION_ARGS);
Datum gbt_int8_picksplit(PG_FUNCTION_ARGS);
Datum gbt_int8_consistent(PG_FUNCTION_ARGS);
Datum gbt_int8_penalty(PG_FUNCTION_ARGS);
Datum gbt_int8_same(PG_FUNCTION_ARGS);
Datum gbt_int8_compress(PG_FUNCTION_ARGS);
Datum gbt_int8_union(PG_FUNCTION_ARGS);
Datum gbt_int8_picksplit(PG_FUNCTION_ARGS);
Datum gbt_int8_consistent(PG_FUNCTION_ARGS);
Datum gbt_int8_penalty(PG_FUNCTION_ARGS);
Datum gbt_int8_same(PG_FUNCTION_ARGS);
static bool gbt_int8gt (const void *a, const void *b)
static bool
gbt_int8gt(const void *a, const void *b)
{
return ( *((int64*)a) > *((int64*)b) );
return (*((int64 *) a) > *((int64 *) b));
}
static bool gbt_int8ge (const void *a, const void *b)
static bool
gbt_int8ge(const void *a, const void *b)
{
return ( *((int64*)a) >= *((int64*)b) );
return (*((int64 *) a) >= *((int64 *) b));
}
static bool gbt_int8eq (const void *a, const void *b)
static bool
gbt_int8eq(const void *a, const void *b)
{
return ( *((int64*)a) == *((int64*)b) );
return (*((int64 *) a) == *((int64 *) b));
}
static bool gbt_int8le (const void *a, const void *b)
static bool
gbt_int8le(const void *a, const void *b)
{
return ( *((int64*)a) <= *((int64*)b) );
return (*((int64 *) a) <= *((int64 *) b));
}
static bool gbt_int8lt (const void *a, const void *b)
static bool
gbt_int8lt(const void *a, const void *b)
{
return ( *((int64*)a) < *((int64*)b) );
return (*((int64 *) a) < *((int64 *) b));
}
static int
gbt_int8key_cmp(const void *a, const void *b)
{
if ( *(int64*)&(((Nsrt *) a)->t[0]) > *(int64*)&(((Nsrt *) b)->t[0]) ){
return 1;
} else
if ( *(int64*)&(((Nsrt *) a)->t[0]) < *(int64*)&(((Nsrt *) b)->t[0]) ){
return -1;
}
return 0;
if (*(int64 *) &(((Nsrt *) a)->t[0]) > *(int64 *) &(((Nsrt *) b)->t[0]))
return 1;
else if (*(int64 *) &(((Nsrt *) a)->t[0]) < *(int64 *) &(((Nsrt *) b)->t[0]))
return -1;
return 0;
}
static const gbtree_ninfo tinfo =
static const gbtree_ninfo tinfo =
{
gbt_t_int8,
sizeof(int64),
gbt_int8gt,
gbt_int8ge,
gbt_int8eq,
gbt_int8le,
gbt_int8lt,
gbt_int8key_cmp
gbt_t_int8,
sizeof(int64),
gbt_int8gt,
gbt_int8ge,
gbt_int8eq,
gbt_int8le,
gbt_int8lt,
gbt_int8key_cmp
};
@ -82,79 +85,82 @@ static const gbtree_ninfo tinfo =
Datum
gbt_int8_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER( gbt_num_compress( retval , entry , &tinfo ));
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER(gbt_num_compress(retval, entry, &tinfo));
}
Datum
gbt_int8_consistent(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
int64 query = PG_GETARG_INT64(1);
int64KEY *kkk = (int64KEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key ;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
key.lower = (GBT_NUMKEY*) &kkk->lower ;
key.upper = (GBT_NUMKEY*) &kkk->upper ;
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
int64 query = PG_GETARG_INT64(1);
int64KEY *kkk = (int64KEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
PG_RETURN_BOOL(
gbt_num_consistent( &key, (void*)&query,&strategy,GIST_LEAF(entry),&tinfo)
);
key.lower = (GBT_NUMKEY *) & kkk->lower;
key.upper = (GBT_NUMKEY *) & kkk->upper;
PG_RETURN_BOOL(
gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
);
}
Datum
gbt_int8_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc(sizeof(int64KEY));
*(int *) PG_GETARG_POINTER(1) = sizeof(int64KEY);
PG_RETURN_POINTER( gbt_num_union ( (void*)out, entryvec, &tinfo ) );
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc(sizeof(int64KEY));
*(int *) PG_GETARG_POINTER(1) = sizeof(int64KEY);
PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
}
Datum
gbt_int8_penalty(PG_FUNCTION_ARGS)
{
int64KEY *origentry = (int64KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
int64KEY *newentry = (int64KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
int64 res ;
int64KEY *origentry = (int64KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
int64KEY *newentry = (int64KEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
int64 res;
*result = 0.0;
*result = 0.0;
penalty_range_enlarge ( origentry->lower, origentry->upper, newentry->lower, newentry->upper );
penalty_range_enlarge(origentry->lower, origentry->upper, newentry->lower, newentry->upper);
if ( res > 0 ){
*result += FLT_MIN ;
*result += (float) ( res / ( (double) ( res + origentry->upper - origentry->lower ) ) );
*result *= ( FLT_MAX / ( ( (GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1 ) );
}
if (res > 0)
{
*result += FLT_MIN;
*result += (float) (res / ((double) (res + origentry->upper - origentry->lower)));
*result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
}
PG_RETURN_POINTER(result);
PG_RETURN_POINTER(result);
}
Datum
gbt_int8_picksplit(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}
Datum
gbt_int8_same(PG_FUNCTION_ARGS)
{
int64KEY *b1 = (int64KEY *) PG_GETARG_POINTER(0);
int64KEY *b2 = (int64KEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
int64KEY *b1 = (int64KEY *) PG_GETARG_POINTER(0);
int64KEY *b2 = (int64KEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
*result = gbt_num_same ( (void*)b1, (void*)b2, &tinfo );
PG_RETURN_POINTER(result);
*result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
PG_RETURN_POINTER(result);
}

View File

@ -3,8 +3,9 @@
typedef struct
{
Interval lower, upper;
} intvKEY;
Interval lower,
upper;
} intvKEY;
/*
@ -18,81 +19,88 @@ PG_FUNCTION_INFO_V1(gbt_intv_consistent);
PG_FUNCTION_INFO_V1(gbt_intv_penalty);
PG_FUNCTION_INFO_V1(gbt_intv_same);
Datum gbt_intv_compress(PG_FUNCTION_ARGS);
Datum gbt_intv_decompress(PG_FUNCTION_ARGS);
Datum gbt_intv_union(PG_FUNCTION_ARGS);
Datum gbt_intv_picksplit(PG_FUNCTION_ARGS);
Datum gbt_intv_consistent(PG_FUNCTION_ARGS);
Datum gbt_intv_penalty(PG_FUNCTION_ARGS);
Datum gbt_intv_same(PG_FUNCTION_ARGS);
Datum gbt_intv_compress(PG_FUNCTION_ARGS);
Datum gbt_intv_decompress(PG_FUNCTION_ARGS);
Datum gbt_intv_union(PG_FUNCTION_ARGS);
Datum gbt_intv_picksplit(PG_FUNCTION_ARGS);
Datum gbt_intv_consistent(PG_FUNCTION_ARGS);
Datum gbt_intv_penalty(PG_FUNCTION_ARGS);
Datum gbt_intv_same(PG_FUNCTION_ARGS);
static bool gbt_intvgt (const void *a, const void *b)
static bool
gbt_intvgt(const void *a, const void *b)
{
return DatumGetBool(DirectFunctionCall2 ( interval_gt , IntervalPGetDatum ( a ) , IntervalPGetDatum ( b ) ));
return DatumGetBool(DirectFunctionCall2(interval_gt, IntervalPGetDatum(a), IntervalPGetDatum(b)));
}
static bool gbt_intvge (const void *a, const void *b)
static bool
gbt_intvge(const void *a, const void *b)
{
return DatumGetBool(DirectFunctionCall2 ( interval_ge , IntervalPGetDatum ( a ) , IntervalPGetDatum ( b ) ));
return DatumGetBool(DirectFunctionCall2(interval_ge, IntervalPGetDatum(a), IntervalPGetDatum(b)));
}
static bool gbt_intveq (const void *a, const void *b)
static bool
gbt_intveq(const void *a, const void *b)
{
return DatumGetBool(DirectFunctionCall2 ( interval_eq , IntervalPGetDatum ( a ) , IntervalPGetDatum ( b ) ));
return DatumGetBool(DirectFunctionCall2(interval_eq, IntervalPGetDatum(a), IntervalPGetDatum(b)));
}
static bool gbt_intvle (const void *a, const void *b)
static bool
gbt_intvle(const void *a, const void *b)
{
return DatumGetBool(DirectFunctionCall2 ( interval_le , IntervalPGetDatum ( a ) , IntervalPGetDatum ( b ) ));
return DatumGetBool(DirectFunctionCall2(interval_le, IntervalPGetDatum(a), IntervalPGetDatum(b)));
}
static bool gbt_intvlt (const void *a, const void *b)
static bool
gbt_intvlt(const void *a, const void *b)
{
return DatumGetBool(DirectFunctionCall2 ( interval_lt , IntervalPGetDatum ( a ) , IntervalPGetDatum ( b ) ));
return DatumGetBool(DirectFunctionCall2(interval_lt, IntervalPGetDatum(a), IntervalPGetDatum(b)));
}
static int
gbt_intvkey_cmp(const void *a, const void *b)
{
return DatumGetInt32 (
DirectFunctionCall2 ( interval_cmp ,
IntervalPGetDatum(((Nsrt *) a)->t) ,
IntervalPGetDatum(((Nsrt *) b)->t)
)
);
return DatumGetInt32(
DirectFunctionCall2(interval_cmp,
IntervalPGetDatum(((Nsrt *) a)->t),
IntervalPGetDatum(((Nsrt *) b)->t)
)
);
}
static double intr2num ( const Interval * i )
static double
intr2num(const Interval *i)
{
double ret = 0.0;
struct pg_tm tm;
fsec_t fsec;
interval2tm( *i, &tm, &fsec);
ret += ( tm.tm_year * 360.0 * 86400.0 ) ;
ret += ( tm.tm_mon * 12.0 * 86400.0 ) ;
ret += ( tm.tm_mday * 86400.0 ) ;
ret += ( tm.tm_hour * 3600.0 ) ;
ret += ( tm.tm_min * 60.0 ) ;
ret += ( tm.tm_sec ) ;
ret += ( fsec / 1000000.0 );
double ret = 0.0;
struct pg_tm tm;
fsec_t fsec;
return ( ret );
interval2tm(*i, &tm, &fsec);
ret += (tm.tm_year * 360.0 * 86400.0);
ret += (tm.tm_mon * 12.0 * 86400.0);
ret += (tm.tm_mday * 86400.0);
ret += (tm.tm_hour * 3600.0);
ret += (tm.tm_min * 60.0);
ret += (tm.tm_sec);
ret += (fsec / 1000000.0);
return (ret);
}
#define INTERVALSIZE 12
static const gbtree_ninfo tinfo =
static const gbtree_ninfo tinfo =
{
gbt_t_intv,
sizeof(Interval),
gbt_intvgt,
gbt_intvge,
gbt_intveq,
gbt_intvle,
gbt_intvlt,
gbt_intvkey_cmp
gbt_t_intv,
sizeof(Interval),
gbt_intvgt,
gbt_intvge,
gbt_intveq,
gbt_intvle,
gbt_intvlt,
gbt_intvkey_cmp
};
@ -104,126 +112,137 @@ static const gbtree_ninfo tinfo =
Datum
gbt_intv_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = entry;
if ( entry->leafkey || INTERVALSIZE != sizeof(Interval) ) {
char *r = ( char * ) palloc(2 * INTERVALSIZE);
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = entry;
retval = palloc(sizeof(GISTENTRY));
if (entry->leafkey || INTERVALSIZE != sizeof(Interval))
{
char *r = (char *) palloc(2 * INTERVALSIZE);
if ( entry->leafkey ) {
Interval *key = DatumGetIntervalP(entry->key);
memcpy( (void*) r , (void*)key, INTERVALSIZE);
memcpy( (void*)(r + INTERVALSIZE), (void*)key, INTERVALSIZE);
} else {
intvKEY *key = ( intvKEY * ) DatumGetPointer(entry->key);
memcpy(r, &key->lower, INTERVALSIZE);
memcpy(r + INTERVALSIZE, &key->upper, INTERVALSIZE);
}
gistentryinit(*retval, PointerGetDatum(r),
entry->rel, entry->page,
entry->offset, 2 * INTERVALSIZE, FALSE);
}
PG_RETURN_POINTER(retval);
retval = palloc(sizeof(GISTENTRY));
if (entry->leafkey)
{
Interval *key = DatumGetIntervalP(entry->key);
memcpy((void *) r, (void *) key, INTERVALSIZE);
memcpy((void *) (r + INTERVALSIZE), (void *) key, INTERVALSIZE);
}
else
{
intvKEY *key = (intvKEY *) DatumGetPointer(entry->key);
memcpy(r, &key->lower, INTERVALSIZE);
memcpy(r + INTERVALSIZE, &key->upper, INTERVALSIZE);
}
gistentryinit(*retval, PointerGetDatum(r),
entry->rel, entry->page,
entry->offset, 2 * INTERVALSIZE, FALSE);
}
PG_RETURN_POINTER(retval);
}
Datum
gbt_intv_decompress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = entry;
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = entry;
if ( INTERVALSIZE != sizeof(Interval) ) {
intvKEY *r = palloc(sizeof(intvKEY));
char *key = DatumGetPointer(entry->key);
if (INTERVALSIZE != sizeof(Interval))
{
intvKEY *r = palloc(sizeof(intvKEY));
char *key = DatumGetPointer(entry->key);
retval = palloc(sizeof(GISTENTRY));
memcpy( &r->lower, key, INTERVALSIZE);
memcpy( &r->upper, key+ INTERVALSIZE, INTERVALSIZE);
retval = palloc(sizeof(GISTENTRY));
memcpy(&r->lower, key, INTERVALSIZE);
memcpy(&r->upper, key + INTERVALSIZE, INTERVALSIZE);
gistentryinit(*retval, PointerGetDatum(r),
entry->rel, entry->page,
entry->offset, sizeof(intvKEY), FALSE);
}
PG_RETURN_POINTER(retval);
gistentryinit(*retval, PointerGetDatum(r),
entry->rel, entry->page,
entry->offset, sizeof(intvKEY), FALSE);
}
PG_RETURN_POINTER(retval);
}
Datum
gbt_intv_consistent(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
Interval *query = PG_GETARG_INTERVAL_P(1);
intvKEY *kkk = (intvKEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key ;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
key.lower = (GBT_NUMKEY*) &kkk->lower ;
key.upper = (GBT_NUMKEY*) &kkk->upper ;
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
Interval *query = PG_GETARG_INTERVAL_P(1);
intvKEY *kkk = (intvKEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
PG_RETURN_BOOL(
gbt_num_consistent( &key, (void*)query ,&strategy,GIST_LEAF(entry),&tinfo)
);
key.lower = (GBT_NUMKEY *) & kkk->lower;
key.upper = (GBT_NUMKEY *) & kkk->upper;
PG_RETURN_BOOL(
gbt_num_consistent(&key, (void *) query, &strategy, GIST_LEAF(entry), &tinfo)
);
}
Datum
gbt_intv_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc(sizeof(intvKEY));
*(int *) PG_GETARG_POINTER(1) = sizeof(intvKEY);
PG_RETURN_POINTER( gbt_num_union ( (void*)out, entryvec, &tinfo ) );
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc(sizeof(intvKEY));
*(int *) PG_GETARG_POINTER(1) = sizeof(intvKEY);
PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
}
Datum
gbt_intv_penalty(PG_FUNCTION_ARGS)
{
intvKEY *origentry = (intvKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
intvKEY *newentry = (intvKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
double iorg[2], inew[2], res;
intvKEY *origentry = (intvKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
intvKEY *newentry = (intvKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
double iorg[2],
inew[2],
res;
iorg[0] = intr2num ( &origentry->lower );
iorg[1] = intr2num ( &origentry->upper );
inew[0] = intr2num ( &newentry->lower );
inew[1] = intr2num ( &newentry->upper );
iorg[0] = intr2num(&origentry->lower);
iorg[1] = intr2num(&origentry->upper);
inew[0] = intr2num(&newentry->lower);
inew[1] = intr2num(&newentry->upper);
penalty_range_enlarge ( iorg[0], iorg[1], inew[0], inew[1] );
penalty_range_enlarge(iorg[0], iorg[1], inew[0], inew[1]);
*result = 0.0;
*result = 0.0;
if ( res > 0 ){
*result += FLT_MIN ;
*result += (float) ( res / ( res + iorg[1] - iorg[0] ) );
*result *= ( FLT_MAX / ( ( (GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1 ) );
}
if (res > 0)
{
*result += FLT_MIN;
*result += (float) (res / (res + iorg[1] - iorg[0]));
*result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
}
PG_RETURN_POINTER(result);
PG_RETURN_POINTER(result);
}
Datum
gbt_intv_picksplit(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}
Datum
gbt_intv_same(PG_FUNCTION_ARGS)
{
intvKEY *b1 = (intvKEY *) PG_GETARG_POINTER(0);
intvKEY *b2 = (intvKEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
intvKEY *b1 = (intvKEY *) PG_GETARG_POINTER(0);
intvKEY *b2 = (intvKEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
*result = gbt_num_same ( (void*)b1, (void*)b2, &tinfo );
PG_RETURN_POINTER(result);
*result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
PG_RETURN_POINTER(result);
}

View File

@ -5,9 +5,9 @@
typedef struct
{
macaddr lower;
macaddr upper;
} macKEY;
macaddr lower;
macaddr upper;
} macKEY;
/*
** OID ops
@ -19,62 +19,67 @@ PG_FUNCTION_INFO_V1(gbt_macad_consistent);
PG_FUNCTION_INFO_V1(gbt_macad_penalty);
PG_FUNCTION_INFO_V1(gbt_macad_same);
Datum gbt_macad_compress(PG_FUNCTION_ARGS);
Datum gbt_macad_union(PG_FUNCTION_ARGS);
Datum gbt_macad_picksplit(PG_FUNCTION_ARGS);
Datum gbt_macad_consistent(PG_FUNCTION_ARGS);
Datum gbt_macad_penalty(PG_FUNCTION_ARGS);
Datum gbt_macad_same(PG_FUNCTION_ARGS);
Datum gbt_macad_compress(PG_FUNCTION_ARGS);
Datum gbt_macad_union(PG_FUNCTION_ARGS);
Datum gbt_macad_picksplit(PG_FUNCTION_ARGS);
Datum gbt_macad_consistent(PG_FUNCTION_ARGS);
Datum gbt_macad_penalty(PG_FUNCTION_ARGS);
Datum gbt_macad_same(PG_FUNCTION_ARGS);
static bool gbt_macadgt (const void *a, const void *b)
static bool
gbt_macadgt(const void *a, const void *b)
{
return DatumGetBool(DirectFunctionCall2(macaddr_gt,PointerGetDatum(a),PointerGetDatum(b)));
return DatumGetBool(DirectFunctionCall2(macaddr_gt, PointerGetDatum(a), PointerGetDatum(b)));
}
static bool gbt_macadge (const void *a, const void *b)
static bool
gbt_macadge(const void *a, const void *b)
{
return DatumGetBool(DirectFunctionCall2(macaddr_ge,PointerGetDatum(a),PointerGetDatum(b)));
return DatumGetBool(DirectFunctionCall2(macaddr_ge, PointerGetDatum(a), PointerGetDatum(b)));
}
static bool gbt_macadeq (const void *a, const void *b)
static bool
gbt_macadeq(const void *a, const void *b)
{
return DatumGetBool(DirectFunctionCall2(macaddr_eq,PointerGetDatum(a),PointerGetDatum(b)));
return DatumGetBool(DirectFunctionCall2(macaddr_eq, PointerGetDatum(a), PointerGetDatum(b)));
}
static bool gbt_macadle (const void *a, const void *b)
static bool
gbt_macadle(const void *a, const void *b)
{
return DatumGetBool(DirectFunctionCall2(macaddr_le,PointerGetDatum(a),PointerGetDatum(b)));
return DatumGetBool(DirectFunctionCall2(macaddr_le, PointerGetDatum(a), PointerGetDatum(b)));
}
static bool gbt_macadlt (const void *a, const void *b)
static bool
gbt_macadlt(const void *a, const void *b)
{
return DatumGetBool(DirectFunctionCall2(macaddr_lt,PointerGetDatum(a),PointerGetDatum(b)));
return DatumGetBool(DirectFunctionCall2(macaddr_lt, PointerGetDatum(a), PointerGetDatum(b)));
}
static int
gbt_macadkey_cmp(const void *a, const void *b)
{
return DatumGetInt32(
DirectFunctionCall2(
macaddr_cmp ,
PointerGetDatum (&((Nsrt *) a)->t[0]),
PointerGetDatum (&((Nsrt *) b)->t[0])
)
);
return DatumGetInt32(
DirectFunctionCall2(
macaddr_cmp,
PointerGetDatum(&((Nsrt *) a)->t[0]),
PointerGetDatum(&((Nsrt *) b)->t[0])
)
);
}
static const gbtree_ninfo tinfo =
{
gbt_t_macad,
sizeof(macaddr),
gbt_macadgt,
gbt_macadge,
gbt_macadeq,
gbt_macadle,
gbt_macadlt,
gbt_macadkey_cmp
static const gbtree_ninfo tinfo =
{
gbt_t_macad,
sizeof(macaddr),
gbt_macadgt,
gbt_macadge,
gbt_macadeq,
gbt_macadle,
gbt_macadlt,
gbt_macadkey_cmp
};
@ -84,14 +89,16 @@ static const gbtree_ninfo tinfo =
static uint64 mac_2_uint64 ( macaddr * m ){
unsigned char * mi = ( unsigned char * ) m;
uint64 res = 0;
int i;
for (i=0; i<6; i++ ){
res += ( ( (uint64) mi[i] ) << ( (uint64) ( (5-i)*8 ) ) );
}
return res;
static uint64
mac_2_uint64(macaddr *m)
{
unsigned char *mi = (unsigned char *) m;
uint64 res = 0;
int i;
for (i = 0; i < 6; i++)
res += (((uint64) mi[i]) << ((uint64) ((5 - i) * 8)));
return res;
}
@ -99,9 +106,10 @@ static uint64 mac_2_uint64 ( macaddr * m ){
Datum
gbt_macad_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER( gbt_num_compress( retval , entry , &tinfo ));
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER(gbt_num_compress(retval, entry, &tinfo));
}
@ -109,76 +117,79 @@ Datum
gbt_macad_consistent(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
macaddr *query = (macaddr *) PG_GETARG_POINTER(1);
macKEY *kkk = (macKEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key ;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
key.lower = (GBT_NUMKEY*) &kkk->lower ;
key.upper = (GBT_NUMKEY*) &kkk->upper ;
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
macaddr *query = (macaddr *) PG_GETARG_POINTER(1);
macKEY *kkk = (macKEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
PG_RETURN_BOOL(
gbt_num_consistent( &key, (void*)query,&strategy,GIST_LEAF(entry),&tinfo)
);
key.lower = (GBT_NUMKEY *) & kkk->lower;
key.upper = (GBT_NUMKEY *) & kkk->upper;
PG_RETURN_BOOL(
gbt_num_consistent(&key, (void *) query, &strategy, GIST_LEAF(entry), &tinfo)
);
}
Datum
gbt_macad_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc(sizeof(macKEY));
*(int *) PG_GETARG_POINTER(1) = sizeof(macKEY);
PG_RETURN_POINTER( gbt_num_union ( (void*)out, entryvec, &tinfo ) );
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc(sizeof(macKEY));
*(int *) PG_GETARG_POINTER(1) = sizeof(macKEY);
PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
}
Datum
gbt_macad_penalty(PG_FUNCTION_ARGS)
{
macKEY *origentry = (macKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
macKEY *newentry = (macKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
uint64 iorg[2], inew[2];
uint64 res;
macKEY *origentry = (macKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
macKEY *newentry = (macKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
uint64 iorg[2],
inew[2];
uint64 res;
iorg[0] = mac_2_uint64 ( &origentry->lower );
iorg[1] = mac_2_uint64 ( &origentry->upper );
inew[0] = mac_2_uint64 ( &newentry->lower );
inew[1] = mac_2_uint64 ( &newentry->upper );
iorg[0] = mac_2_uint64(&origentry->lower);
iorg[1] = mac_2_uint64(&origentry->upper);
inew[0] = mac_2_uint64(&newentry->lower);
inew[1] = mac_2_uint64(&newentry->upper);
penalty_range_enlarge ( iorg[0], iorg[1], inew[0], inew[1] );
penalty_range_enlarge(iorg[0], iorg[1], inew[0], inew[1]);
*result = 0.0;
*result = 0.0;
if ( res > 0 ){
*result += FLT_MIN ;
*result += (float) ( ( (double)res ) / ( (double)res + (double)iorg[1] - (double)iorg[0] ) );
*result *= ( FLT_MAX / ( ( (GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1 ) );
}
if (res > 0)
{
*result += FLT_MIN;
*result += (float) (((double) res) / ((double) res + (double) iorg[1] - (double) iorg[0]));
*result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
}
PG_RETURN_POINTER(result);
PG_RETURN_POINTER(result);
}
Datum
gbt_macad_picksplit(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}
Datum
gbt_macad_same(PG_FUNCTION_ARGS)
{
macKEY *b1 = (macKEY *) PG_GETARG_POINTER(0);
macKEY *b2 = (macKEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
macKEY *b1 = (macKEY *) PG_GETARG_POINTER(0);
macKEY *b2 = (macKEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
*result = gbt_num_same ( (void*)b1, (void*)b2, &tinfo );
PG_RETURN_POINTER(result);
*result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
PG_RETURN_POINTER(result);
}

View File

@ -13,61 +13,67 @@ PG_FUNCTION_INFO_V1(gbt_numeric_consistent);
PG_FUNCTION_INFO_V1(gbt_numeric_penalty);
PG_FUNCTION_INFO_V1(gbt_numeric_same);
Datum gbt_numeric_compress(PG_FUNCTION_ARGS);
Datum gbt_numeric_union(PG_FUNCTION_ARGS);
Datum gbt_numeric_picksplit(PG_FUNCTION_ARGS);
Datum gbt_numeric_consistent(PG_FUNCTION_ARGS);
Datum gbt_numeric_penalty(PG_FUNCTION_ARGS);
Datum gbt_numeric_same(PG_FUNCTION_ARGS);
Datum gbt_numeric_compress(PG_FUNCTION_ARGS);
Datum gbt_numeric_union(PG_FUNCTION_ARGS);
Datum gbt_numeric_picksplit(PG_FUNCTION_ARGS);
Datum gbt_numeric_consistent(PG_FUNCTION_ARGS);
Datum gbt_numeric_penalty(PG_FUNCTION_ARGS);
Datum gbt_numeric_same(PG_FUNCTION_ARGS);
/* define for comparison */
static bool gbt_numeric_gt (const void *a, const void *b)
static bool
gbt_numeric_gt(const void *a, const void *b)
{
return ( DatumGetBool(DirectFunctionCall2( numeric_gt ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
return (DatumGetBool(DirectFunctionCall2(numeric_gt, PointerGetDatum(a), PointerGetDatum(b))));
}
static bool gbt_numeric_ge (const void *a, const void *b)
static bool
gbt_numeric_ge(const void *a, const void *b)
{
return ( DatumGetBool(DirectFunctionCall2( numeric_ge ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
return (DatumGetBool(DirectFunctionCall2(numeric_ge, PointerGetDatum(a), PointerGetDatum(b))));
}
static bool gbt_numeric_eq (const void *a, const void *b)
static bool
gbt_numeric_eq(const void *a, const void *b)
{
return ( DatumGetBool(DirectFunctionCall2( numeric_eq ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
return (DatumGetBool(DirectFunctionCall2(numeric_eq, PointerGetDatum(a), PointerGetDatum(b))));
}
static bool gbt_numeric_le (const void *a, const void *b)
static bool
gbt_numeric_le(const void *a, const void *b)
{
return ( DatumGetBool(DirectFunctionCall2( numeric_le ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
return (DatumGetBool(DirectFunctionCall2(numeric_le, PointerGetDatum(a), PointerGetDatum(b))));
}
static bool gbt_numeric_lt (const void *a, const void *b)
static bool
gbt_numeric_lt(const void *a, const void *b)
{
return ( DatumGetBool(DirectFunctionCall2( numeric_lt ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
return (DatumGetBool(DirectFunctionCall2(numeric_lt, PointerGetDatum(a), PointerGetDatum(b))));
}
static int32 gbt_numeric_cmp ( const bytea * a , const bytea * b )
static int32
gbt_numeric_cmp(const bytea *a, const bytea *b)
{
return
( DatumGetInt32(DirectFunctionCall2(numeric_cmp,PointerGetDatum(a),PointerGetDatum(b) ) ) );
return
(DatumGetInt32(DirectFunctionCall2(numeric_cmp, PointerGetDatum(a), PointerGetDatum(b))));
}
static const gbtree_vinfo tinfo =
{
gbt_t_numeric,
FALSE,
FALSE,
gbt_numeric_gt,
gbt_numeric_ge,
gbt_numeric_eq,
gbt_numeric_le,
gbt_numeric_lt,
gbt_numeric_cmp,
NULL
gbt_t_numeric,
FALSE,
FALSE,
gbt_numeric_gt,
gbt_numeric_ge,
gbt_numeric_eq,
gbt_numeric_le,
gbt_numeric_lt,
gbt_numeric_cmp,
NULL
};
@ -77,10 +83,11 @@ static const gbtree_vinfo tinfo =
Datum
gbt_numeric_compress (PG_FUNCTION_ARGS)
gbt_numeric_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
PG_RETURN_POINTER ( gbt_var_compress( entry, &tinfo ) );
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
PG_RETURN_POINTER(gbt_var_compress(entry, &tinfo));
}
@ -89,24 +96,22 @@ Datum
gbt_numeric_consistent(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GBT_VARKEY *ktst = (GBT_VARKEY *) DatumGetPointer ( entry->key ) ;
GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer ( PG_DETOAST_DATUM( entry->key ) );
void *qtst = ( void * ) DatumGetPointer ( PG_GETARG_DATUM(1) );
void *query = ( void * ) DatumGetNumeric ( PG_GETARG_DATUM(1) );
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
bool retval = FALSE;
GBT_VARKEY_R r = gbt_var_key_readable ( key );
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GBT_VARKEY *ktst = (GBT_VARKEY *) DatumGetPointer(entry->key);
GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(PG_DETOAST_DATUM(entry->key));
void *qtst = (void *) DatumGetPointer(PG_GETARG_DATUM(1));
void *query = (void *) DatumGetNumeric(PG_GETARG_DATUM(1));
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
bool retval = FALSE;
GBT_VARKEY_R r = gbt_var_key_readable(key);
retval = gbt_var_consistent( &r, query, &strategy, GIST_LEAF(entry), &tinfo );
retval = gbt_var_consistent(&r, query, &strategy, GIST_LEAF(entry), &tinfo);
if ( ktst != key ){
pfree ( key );
}
if ( qtst != query ){
pfree ( query );
}
PG_RETURN_BOOL(retval);
if (ktst != key)
pfree(key);
if (qtst != query)
pfree(query);
PG_RETURN_BOOL(retval);
}
@ -114,104 +119,110 @@ gbt_numeric_consistent(PG_FUNCTION_ARGS)
Datum
gbt_numeric_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
int32 * size = (int *) PG_GETARG_POINTER(1);
PG_RETURN_POINTER( gbt_var_union ( entryvec , size , &tinfo ) );
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
int32 *size = (int *) PG_GETARG_POINTER(1);
PG_RETURN_POINTER(gbt_var_union(entryvec, size, &tinfo));
}
Datum
gbt_numeric_same(PG_FUNCTION_ARGS)
{
Datum d1 = PG_GETARG_DATUM(0);
Datum d2 = PG_GETARG_DATUM(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
PG_RETURN_POINTER( gbt_var_same ( result, d1 , d2 , &tinfo ));
Datum d1 = PG_GETARG_DATUM(0);
Datum d2 = PG_GETARG_DATUM(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
PG_RETURN_POINTER(gbt_var_same(result, d1, d2, &tinfo));
}
Datum
gbt_numeric_penalty (PG_FUNCTION_ARGS)
gbt_numeric_penalty(PG_FUNCTION_ARGS)
{
GISTENTRY * o = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY * n = (GISTENTRY *) PG_GETARG_POINTER(1);
float *result = (float *) PG_GETARG_POINTER(2);
GISTENTRY *o = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *n = (GISTENTRY *) PG_GETARG_POINTER(1);
float *result = (float *) PG_GETARG_POINTER(2);
Numeric us, os, ds ;
Numeric us,
os,
ds;
GBT_VARKEY *org = (GBT_VARKEY *) DatumGetPointer(o->key);
GBT_VARKEY *newe = (GBT_VARKEY *) DatumGetPointer(n->key);
Datum uni ;
GBT_VARKEY_R rk , ok, uk ;
GBT_VARKEY *org = (GBT_VARKEY *) DatumGetPointer(o->key);
GBT_VARKEY *newe = (GBT_VARKEY *) DatumGetPointer(n->key);
Datum uni;
GBT_VARKEY_R rk,
ok,
uk;
rk = gbt_var_key_readable ( org );
uni = PointerGetDatum( gbt_var_key_copy( &rk, TRUE ) );
gbt_var_bin_union ( &uni , newe, &tinfo );
ok = gbt_var_key_readable ( org );
uk = gbt_var_key_readable ( (GBT_VARKEY *) DatumGetPointer(uni) );
rk = gbt_var_key_readable(org);
uni = PointerGetDatum(gbt_var_key_copy(&rk, TRUE));
gbt_var_bin_union(&uni, newe, &tinfo);
ok = gbt_var_key_readable(org);
uk = gbt_var_key_readable((GBT_VARKEY *) DatumGetPointer(uni));
us = DatumGetNumeric(DirectFunctionCall2(
numeric_sub,
PointerGetDatum(uk.upper),
PointerGetDatum(uk.lower)
));
us = DatumGetNumeric(DirectFunctionCall2(
numeric_sub,
PointerGetDatum(uk.upper),
PointerGetDatum(uk.lower)
));
pfree ( DatumGetPointer(uni) );
pfree(DatumGetPointer(uni));
os = DatumGetNumeric(DirectFunctionCall2(
numeric_sub,
PointerGetDatum(ok.upper),
PointerGetDatum(ok.lower)
));
os = DatumGetNumeric(DirectFunctionCall2(
numeric_sub,
PointerGetDatum(ok.upper),
PointerGetDatum(ok.lower)
));
ds = DatumGetNumeric(DirectFunctionCall2(
numeric_sub,
NumericGetDatum(us),
NumericGetDatum(os)
));
ds = DatumGetNumeric(DirectFunctionCall2(
numeric_sub,
NumericGetDatum(us),
NumericGetDatum(os)
));
pfree ( os );
pfree(os);
if ( NUMERIC_IS_NAN( us ) )
{
if (NUMERIC_IS_NAN(us))
{
if ( NUMERIC_IS_NAN( os ) )
{
*result = 0.0;
} else {
*result = 1.0;
}
if (NUMERIC_IS_NAN(os))
*result = 0.0;
else
*result = 1.0;
} else {
}
else
{
Numeric nul = DatumGetNumeric(DirectFunctionCall1( int4_numeric , Int32GetDatum (0) ) );
Numeric nul = DatumGetNumeric(DirectFunctionCall1(int4_numeric, Int32GetDatum(0)));
*result = 0.0;
*result = 0.0;
if ( DirectFunctionCall2( numeric_gt , NumericGetDatum(ds), NumericGetDatum(nul) ) )
{
if (DirectFunctionCall2(numeric_gt, NumericGetDatum(ds), NumericGetDatum(nul)))
{
*result += FLT_MIN ;
os = DatumGetNumeric(DirectFunctionCall2(
numeric_div,
NumericGetDatum(ds),
NumericGetDatum(us)
));
*result += ( float4 ) DatumGetFloat8( DirectFunctionCall1( numeric_float8_no_overflow , NumericGetDatum(os) ) );
pfree ( os );
*result += FLT_MIN;
os = DatumGetNumeric(DirectFunctionCall2(
numeric_div,
NumericGetDatum(ds),
NumericGetDatum(us)
));
*result += (float4) DatumGetFloat8(DirectFunctionCall1(numeric_float8_no_overflow, NumericGetDatum(os)));
pfree(os);
}
}
pfree ( nul );
}
pfree(nul);
}
if ( *result > 0 )
*result *= ( FLT_MAX / ( ( (GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1 ) );
if (*result > 0)
*result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
pfree ( us );
pfree ( ds );
pfree(us);
pfree(ds);
PG_RETURN_POINTER( result );
PG_RETURN_POINTER(result);
}
@ -219,9 +230,9 @@ gbt_numeric_penalty (PG_FUNCTION_ARGS)
Datum
gbt_numeric_picksplit(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
gbt_var_picksplit ( entryvec, v, &tinfo );
PG_RETURN_POINTER(v);
}
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
gbt_var_picksplit(entryvec, v, &tinfo);
PG_RETURN_POINTER(v);
}

View File

@ -3,9 +3,9 @@
typedef struct
{
Oid lower;
Oid upper;
} oidKEY;
Oid lower;
Oid upper;
} oidKEY;
/*
** OID ops
@ -17,60 +17,63 @@ PG_FUNCTION_INFO_V1(gbt_oid_consistent);
PG_FUNCTION_INFO_V1(gbt_oid_penalty);
PG_FUNCTION_INFO_V1(gbt_oid_same);
Datum gbt_oid_compress(PG_FUNCTION_ARGS);
Datum gbt_oid_union(PG_FUNCTION_ARGS);
Datum gbt_oid_picksplit(PG_FUNCTION_ARGS);
Datum gbt_oid_consistent(PG_FUNCTION_ARGS);
Datum gbt_oid_penalty(PG_FUNCTION_ARGS);
Datum gbt_oid_same(PG_FUNCTION_ARGS);
Datum gbt_oid_compress(PG_FUNCTION_ARGS);
Datum gbt_oid_union(PG_FUNCTION_ARGS);
Datum gbt_oid_picksplit(PG_FUNCTION_ARGS);
Datum gbt_oid_consistent(PG_FUNCTION_ARGS);
Datum gbt_oid_penalty(PG_FUNCTION_ARGS);
Datum gbt_oid_same(PG_FUNCTION_ARGS);
static bool gbt_oidgt (const void *a, const void *b)
static bool
gbt_oidgt(const void *a, const void *b)
{
return ( *((Oid*)a) > *((Oid*)b) );
return (*((Oid *) a) > *((Oid *) b));
}
static bool gbt_oidge (const void *a, const void *b)
static bool
gbt_oidge(const void *a, const void *b)
{
return ( *((Oid*)a) >= *((Oid*)b) );
return (*((Oid *) a) >= *((Oid *) b));
}
static bool gbt_oideq (const void *a, const void *b)
static bool
gbt_oideq(const void *a, const void *b)
{
return ( *((Oid*)a) == *((Oid*)b) );
return (*((Oid *) a) == *((Oid *) b));
}
static bool gbt_oidle (const void *a, const void *b)
static bool
gbt_oidle(const void *a, const void *b)
{
return ( *((Oid*)a) <= *((Oid*)b) );
return (*((Oid *) a) <= *((Oid *) b));
}
static bool gbt_oidlt (const void *a, const void *b)
static bool
gbt_oidlt(const void *a, const void *b)
{
return ( *((Oid*)a) < *((Oid*)b) );
return (*((Oid *) a) < *((Oid *) b));
}
static int
gbt_oidkey_cmp(const void *a, const void *b)
{
if ( *(Oid*)&(((Nsrt *) a)->t[0]) > *(Oid*)&(((Nsrt *) b)->t[0]) ){
return 1;
} else
if ( *(Oid*)&(((Nsrt *) a)->t[0]) < *(Oid*)&(((Nsrt *) b)->t[0]) ){
return -1;
}
return 0;
if (*(Oid *) &(((Nsrt *) a)->t[0]) > *(Oid *) &(((Nsrt *) b)->t[0]))
return 1;
else if (*(Oid *) &(((Nsrt *) a)->t[0]) < *(Oid *) &(((Nsrt *) b)->t[0]))
return -1;
return 0;
}
static const gbtree_ninfo tinfo =
{
gbt_t_oid,
sizeof(Oid),
gbt_oidgt,
gbt_oidge,
gbt_oideq,
gbt_oidle,
gbt_oidlt,
gbt_oidkey_cmp
static const gbtree_ninfo tinfo =
{
gbt_t_oid,
sizeof(Oid),
gbt_oidgt,
gbt_oidge,
gbt_oideq,
gbt_oidle,
gbt_oidlt,
gbt_oidkey_cmp
};
@ -82,9 +85,10 @@ static const gbtree_ninfo tinfo =
Datum
gbt_oid_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER( gbt_num_compress( retval , entry , &tinfo ));
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER(gbt_num_compress(retval, entry, &tinfo));
}
@ -92,69 +96,72 @@ Datum
gbt_oid_consistent(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
Oid query = PG_GETARG_OID(1);
oidKEY *kkk = (oidKEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key ;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
key.lower = (GBT_NUMKEY*) &kkk->lower ;
key.upper = (GBT_NUMKEY*) &kkk->upper ;
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
Oid query = PG_GETARG_OID(1);
oidKEY *kkk = (oidKEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
PG_RETURN_BOOL(
gbt_num_consistent( &key, (void*)&query,&strategy,GIST_LEAF(entry),&tinfo)
);
key.lower = (GBT_NUMKEY *) & kkk->lower;
key.upper = (GBT_NUMKEY *) & kkk->upper;
PG_RETURN_BOOL(
gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
);
}
Datum
gbt_oid_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc(sizeof(oidKEY));
*(int *) PG_GETARG_POINTER(1) = sizeof(oidKEY);
PG_RETURN_POINTER( gbt_num_union ( (void*)out, entryvec, &tinfo ) );
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc(sizeof(oidKEY));
*(int *) PG_GETARG_POINTER(1) = sizeof(oidKEY);
PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
}
Datum
gbt_oid_penalty(PG_FUNCTION_ARGS)
{
oidKEY *origentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
oidKEY *newentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
oidKEY *origentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
oidKEY *newentry = (oidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
Oid res = 0 ;
Oid res = 0;
*result = 0.0;
penalty_range_enlarge ( origentry->lower, origentry->upper, newentry->lower , newentry->upper );
*result = 0.0;
if ( res > 0 ){
*result += FLT_MIN ;
*result += (float) ( res / ( (double) ( res + origentry->upper - origentry->lower ) ) );
*result *= ( FLT_MAX / ( ( (GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1 ) );
}
penalty_range_enlarge(origentry->lower, origentry->upper, newentry->lower, newentry->upper);
PG_RETURN_POINTER(result);
if (res > 0)
{
*result += FLT_MIN;
*result += (float) (res / ((double) (res + origentry->upper - origentry->lower)));
*result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
}
PG_RETURN_POINTER(result);
}
Datum
gbt_oid_picksplit(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}
Datum
gbt_oid_same(PG_FUNCTION_ARGS)
{
oidKEY *b1 = (oidKEY *) PG_GETARG_POINTER(0);
oidKEY *b2 = (oidKEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
oidKEY *b1 = (oidKEY *) PG_GETARG_POINTER(0);
oidKEY *b2 = (oidKEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
*result = gbt_num_same ( (void*)b1, (void*)b2, &tinfo );
PG_RETURN_POINTER(result);
*result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
PG_RETURN_POINTER(result);
}

View File

@ -14,95 +14,102 @@ PG_FUNCTION_INFO_V1(gbt_bpchar_consistent);
PG_FUNCTION_INFO_V1(gbt_text_penalty);
PG_FUNCTION_INFO_V1(gbt_text_same);
Datum gbt_text_compress(PG_FUNCTION_ARGS);
Datum gbt_bpchar_compress(PG_FUNCTION_ARGS);
Datum gbt_text_union(PG_FUNCTION_ARGS);
Datum gbt_text_picksplit(PG_FUNCTION_ARGS);
Datum gbt_text_consistent(PG_FUNCTION_ARGS);
Datum gbt_bpchar_consistent(PG_FUNCTION_ARGS);
Datum gbt_text_penalty(PG_FUNCTION_ARGS);
Datum gbt_text_same(PG_FUNCTION_ARGS);
Datum gbt_text_compress(PG_FUNCTION_ARGS);
Datum gbt_bpchar_compress(PG_FUNCTION_ARGS);
Datum gbt_text_union(PG_FUNCTION_ARGS);
Datum gbt_text_picksplit(PG_FUNCTION_ARGS);
Datum gbt_text_consistent(PG_FUNCTION_ARGS);
Datum gbt_bpchar_consistent(PG_FUNCTION_ARGS);
Datum gbt_text_penalty(PG_FUNCTION_ARGS);
Datum gbt_text_same(PG_FUNCTION_ARGS);
/* define for comparison */
static bool gbt_textgt (const void *a, const void *b)
static bool
gbt_textgt(const void *a, const void *b)
{
return ( DatumGetBool(DirectFunctionCall2( text_gt ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
return (DatumGetBool(DirectFunctionCall2(text_gt, PointerGetDatum(a), PointerGetDatum(b))));
}
static bool gbt_textge (const void *a, const void *b)
static bool
gbt_textge(const void *a, const void *b)
{
return ( DatumGetBool(DirectFunctionCall2( text_ge ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
return (DatumGetBool(DirectFunctionCall2(text_ge, PointerGetDatum(a), PointerGetDatum(b))));
}
static bool gbt_texteq (const void *a, const void *b)
static bool
gbt_texteq(const void *a, const void *b)
{
return ( DatumGetBool(DirectFunctionCall2( texteq ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
return (DatumGetBool(DirectFunctionCall2(texteq, PointerGetDatum(a), PointerGetDatum(b))));
}
static bool gbt_textle (const void *a, const void *b)
static bool
gbt_textle(const void *a, const void *b)
{
return ( DatumGetBool(DirectFunctionCall2( text_le ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
return (DatumGetBool(DirectFunctionCall2(text_le, PointerGetDatum(a), PointerGetDatum(b))));
}
static bool gbt_textlt (const void *a, const void *b)
static bool
gbt_textlt(const void *a, const void *b)
{
return ( DatumGetBool(DirectFunctionCall2( text_lt ,PointerGetDatum( a ),PointerGetDatum( b ) ) ) );
return (DatumGetBool(DirectFunctionCall2(text_lt, PointerGetDatum(a), PointerGetDatum(b))));
}
static int32 gbt_textcmp ( const bytea * a , const bytea * b )
static int32
gbt_textcmp(const bytea *a, const bytea *b)
{
return strcmp( VARDATA(a), VARDATA(b) );
return strcmp(VARDATA(a), VARDATA(b));
}
/*
* Converts data of leaf using strxfrm ( locale support )
*/
static bytea *
gbt_text_xfrm ( bytea * leaf )
gbt_text_xfrm(bytea *leaf)
{
bytea * out = leaf;
int32 ilen = VARSIZE (leaf) - VARHDRSZ;
int32 olen ;
char * sin;
char * sou;
bytea *out = leaf;
int32 ilen = VARSIZE(leaf) - VARHDRSZ;
int32 olen;
char *sin;
char *sou;
sin = palloc(ilen + 1);
memcpy (sin, (void*) VARDATA(leaf) ,ilen );
sin[ilen] = '\0';
memcpy(sin, (void *) VARDATA(leaf), ilen);
sin[ilen] = '\0';
olen = strxfrm ( NULL, &sin[0], 0 ) + 1;
sou = palloc ( olen );
olen = strxfrm ( sou , &sin[0] , olen );
olen += VARHDRSZ;
out = palloc ( olen + 1 );
out->vl_len = olen+1;
memcpy( (void*) VARDATA(out), sou, olen-VARHDRSZ );
((char*)out)[olen] = '\0';
olen = strxfrm(NULL, &sin[0], 0) + 1;
sou = palloc(olen);
olen = strxfrm(sou, &sin[0], olen);
olen += VARHDRSZ;
out = palloc(olen + 1);
out->vl_len = olen + 1;
memcpy((void *) VARDATA(out), sou, olen - VARHDRSZ);
((char *) out)[olen] = '\0';
pfree(sou);
pfree(sin);
pfree(sou);
pfree(sin);
return out;
return out;
}
static GBT_VARKEY * gbt_text_l2n ( GBT_VARKEY * leaf )
static GBT_VARKEY *
gbt_text_l2n(GBT_VARKEY * leaf)
{
GBT_VARKEY *out = leaf ;
GBT_VARKEY_R r = gbt_var_key_readable ( leaf );
bytea * o ;
o = gbt_text_xfrm ( r.lower );
r.lower = r.upper = o;
out = gbt_var_key_copy ( &r , TRUE );
pfree(o);
GBT_VARKEY *out = leaf;
GBT_VARKEY_R r = gbt_var_key_readable(leaf);
bytea *o;
return out;
o = gbt_text_xfrm(r.lower);
r.lower = r.upper = o;
out = gbt_var_key_copy(&r, TRUE);
pfree(o);
return out;
}
@ -112,16 +119,16 @@ static GBT_VARKEY * gbt_text_l2n ( GBT_VARKEY * leaf )
static const gbtree_vinfo tinfo =
{
gbt_t_text,
TRUE,
TRUE,
gbt_textgt,
gbt_textge,
gbt_texteq,
gbt_textle,
gbt_textlt,
gbt_textcmp,
gbt_text_l2n
gbt_t_text,
TRUE,
TRUE,
gbt_textgt,
gbt_textge,
gbt_texteq,
gbt_textle,
gbt_textlt,
gbt_textcmp,
gbt_text_l2n
};
@ -132,36 +139,38 @@ static const gbtree_vinfo tinfo =
Datum
gbt_text_compress (PG_FUNCTION_ARGS)
gbt_text_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
PG_RETURN_POINTER ( gbt_var_compress( entry, &tinfo ) );
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
PG_RETURN_POINTER(gbt_var_compress(entry, &tinfo));
}
Datum
gbt_bpchar_compress (PG_FUNCTION_ARGS)
gbt_bpchar_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY * retval ;
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval;
if (entry->leafkey)
{
if (entry->leafkey)
{
Datum d = DirectFunctionCall1 ( rtrim1, entry->key );
GISTENTRY * trim = palloc(sizeof(GISTENTRY));
Datum d = DirectFunctionCall1(rtrim1, entry->key);
GISTENTRY *trim = palloc(sizeof(GISTENTRY));
gistentryinit(*trim, d ,
entry->rel, entry->page,
entry->offset, VARSIZE(DatumGetPointer(d)), TRUE);
retval = gbt_var_compress( trim , &tinfo ) ;
gistentryinit(*trim, d,
entry->rel, entry->page,
entry->offset, VARSIZE(DatumGetPointer(d)), TRUE);
retval = gbt_var_compress(trim, &tinfo);
pfree ( trim );
pfree ( DatumGetPointer(d) );
} else
retval = entry;
pfree(trim);
pfree(DatumGetPointer(d));
}
else
retval = entry;
PG_RETURN_POINTER ( retval );
PG_RETURN_POINTER(retval);
}
@ -169,68 +178,66 @@ gbt_bpchar_compress (PG_FUNCTION_ARGS)
Datum
gbt_text_consistent(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GBT_VARKEY *ktst = (GBT_VARKEY *) DatumGetPointer ( entry->key ) ;
GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer ( PG_DETOAST_DATUM( entry->key ) );
void *qtst = ( void * ) DatumGetPointer( PG_GETARG_DATUM(1) );
void *query = ( void * ) DatumGetTextP ( PG_GETARG_DATUM(1) );
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
bool retval = FALSE;
GBT_VARKEY_R r = gbt_var_key_readable ( key );
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GBT_VARKEY *ktst = (GBT_VARKEY *) DatumGetPointer(entry->key);
GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(PG_DETOAST_DATUM(entry->key));
void *qtst = (void *) DatumGetPointer(PG_GETARG_DATUM(1));
void *query = (void *) DatumGetTextP(PG_GETARG_DATUM(1));
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
bool retval = FALSE;
GBT_VARKEY_R r = gbt_var_key_readable(key);
if ( GIST_LEAF(entry) )
{
retval = gbt_var_consistent( &r, query, &strategy, TRUE, &tinfo );
} else {
bytea * q = gbt_text_xfrm ( ( bytea * ) query );
retval = gbt_var_consistent( &r, (void*)q, &strategy, FALSE, &tinfo );
if ( q != query )
pfree(q);
}
if (GIST_LEAF(entry))
retval = gbt_var_consistent(&r, query, &strategy, TRUE, &tinfo);
else
{
bytea *q = gbt_text_xfrm((bytea *) query);
if ( ktst != key ){
pfree ( key );
}
if ( qtst != query ){
pfree ( query );
}
retval = gbt_var_consistent(&r, (void *) q, &strategy, FALSE, &tinfo);
if (q != query)
pfree(q);
}
PG_RETURN_BOOL(retval);
if (ktst != key)
pfree(key);
if (qtst != query)
pfree(query);
PG_RETURN_BOOL(retval);
}
Datum
gbt_bpchar_consistent(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GBT_VARKEY *ktst = (GBT_VARKEY *) DatumGetPointer ( entry->key ) ;
GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer ( PG_DETOAST_DATUM( entry->key ) );
void *qtst = ( void * ) DatumGetPointer ( PG_GETARG_DATUM(1) );
void *query = ( void * ) DatumGetPointer (PG_DETOAST_DATUM( PG_GETARG_DATUM(1) ) );
void *trim = ( void * ) DatumGetPointer ( DirectFunctionCall1 ( rtrim1, PointerGetDatum ( query ) ) ) ;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
bool retval = FALSE;
GBT_VARKEY_R r = gbt_var_key_readable ( key );
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GBT_VARKEY *ktst = (GBT_VARKEY *) DatumGetPointer(entry->key);
GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(PG_DETOAST_DATUM(entry->key));
void *qtst = (void *) DatumGetPointer(PG_GETARG_DATUM(1));
void *query = (void *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(1)));
void *trim = (void *) DatumGetPointer(DirectFunctionCall1(rtrim1, PointerGetDatum(query)));
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
bool retval = FALSE;
GBT_VARKEY_R r = gbt_var_key_readable(key);
if ( GIST_LEAF(entry) )
{
retval = gbt_var_consistent( &r, trim , &strategy, TRUE, &tinfo );
} else {
bytea * q = gbt_text_xfrm ( ( bytea * ) trim );
retval = gbt_var_consistent( &r, (void*)q, &strategy, FALSE, &tinfo );
if ( q != trim )
pfree(q);
}
if (GIST_LEAF(entry))
retval = gbt_var_consistent(&r, trim, &strategy, TRUE, &tinfo);
else
{
bytea *q = gbt_text_xfrm((bytea *) trim);
pfree(trim);
retval = gbt_var_consistent(&r, (void *) q, &strategy, FALSE, &tinfo);
if (q != trim)
pfree(q);
}
if ( ktst != key ){
pfree ( key );
}
if ( qtst != query ){
pfree ( query );
}
PG_RETURN_BOOL(retval);
pfree(trim);
if (ktst != key)
pfree(key);
if (qtst != query)
pfree(query);
PG_RETURN_BOOL(retval);
}
@ -239,37 +246,40 @@ gbt_bpchar_consistent(PG_FUNCTION_ARGS)
Datum
gbt_text_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
int32 *size = (int *) PG_GETARG_POINTER(1);
PG_RETURN_POINTER( gbt_var_union ( entryvec , size , &tinfo ) );
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
int32 *size = (int *) PG_GETARG_POINTER(1);
PG_RETURN_POINTER(gbt_var_union(entryvec, size, &tinfo));
}
Datum
gbt_text_picksplit(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
gbt_var_picksplit ( entryvec, v, &tinfo );
PG_RETURN_POINTER(v);
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
gbt_var_picksplit(entryvec, v, &tinfo);
PG_RETURN_POINTER(v);
}
Datum
gbt_text_same(PG_FUNCTION_ARGS)
{
Datum d1 = PG_GETARG_DATUM(0);
Datum d2 = PG_GETARG_DATUM(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
PG_RETURN_POINTER( gbt_var_same ( result, d1 , d2 , &tinfo ));
Datum d1 = PG_GETARG_DATUM(0);
Datum d2 = PG_GETARG_DATUM(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
PG_RETURN_POINTER(gbt_var_same(result, d1, d2, &tinfo));
}
Datum
gbt_text_penalty(PG_FUNCTION_ARGS)
{
float *result = (float *) PG_GETARG_POINTER(2);
GISTENTRY * o = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY * n = (GISTENTRY *) PG_GETARG_POINTER(1);
PG_RETURN_POINTER( gbt_var_penalty ( result ,o , n, &tinfo ) );
}
float *result = (float *) PG_GETARG_POINTER(2);
GISTENTRY *o = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *n = (GISTENTRY *) PG_GETARG_POINTER(1);
PG_RETURN_POINTER(gbt_var_penalty(result, o, n, &tinfo));
}

View File

@ -4,9 +4,9 @@
typedef struct
{
TimeADT lower;
TimeADT upper;
} timeKEY;
TimeADT lower;
TimeADT upper;
} timeKEY;
/*
** time ops
@ -20,49 +20,54 @@ PG_FUNCTION_INFO_V1(gbt_timetz_consistent);
PG_FUNCTION_INFO_V1(gbt_time_penalty);
PG_FUNCTION_INFO_V1(gbt_time_same);
Datum gbt_time_compress(PG_FUNCTION_ARGS);
Datum gbt_timetz_compress(PG_FUNCTION_ARGS);
Datum gbt_time_union(PG_FUNCTION_ARGS);
Datum gbt_time_picksplit(PG_FUNCTION_ARGS);
Datum gbt_time_consistent(PG_FUNCTION_ARGS);
Datum gbt_timetz_consistent(PG_FUNCTION_ARGS);
Datum gbt_time_penalty(PG_FUNCTION_ARGS);
Datum gbt_time_same(PG_FUNCTION_ARGS);
Datum gbt_time_compress(PG_FUNCTION_ARGS);
Datum gbt_timetz_compress(PG_FUNCTION_ARGS);
Datum gbt_time_union(PG_FUNCTION_ARGS);
Datum gbt_time_picksplit(PG_FUNCTION_ARGS);
Datum gbt_time_consistent(PG_FUNCTION_ARGS);
Datum gbt_timetz_consistent(PG_FUNCTION_ARGS);
Datum gbt_time_penalty(PG_FUNCTION_ARGS);
Datum gbt_time_same(PG_FUNCTION_ARGS);
static bool gbt_timegt (const void *a, const void *b)
static bool
gbt_timegt(const void *a, const void *b)
{
return DatumGetBool(
DirectFunctionCall2(time_gt,TimeADTGetDatum( *((TimeADT*)a) ), TimeADTGetDatum( *((TimeADT*)b) ) )
);
return DatumGetBool(
DirectFunctionCall2(time_gt, TimeADTGetDatum(*((TimeADT *) a)), TimeADTGetDatum(*((TimeADT *) b)))
);
}
static bool gbt_timege (const void *a, const void *b)
static bool
gbt_timege(const void *a, const void *b)
{
return DatumGetBool(
DirectFunctionCall2(time_ge,TimeADTGetDatum( *((TimeADT*)a) ), TimeADTGetDatum( *((TimeADT*)b) ) )
);
return DatumGetBool(
DirectFunctionCall2(time_ge, TimeADTGetDatum(*((TimeADT *) a)), TimeADTGetDatum(*((TimeADT *) b)))
);
}
static bool gbt_timeeq (const void *a, const void *b)
static bool
gbt_timeeq(const void *a, const void *b)
{
return DatumGetBool(
DirectFunctionCall2(time_eq,TimeADTGetDatum( *((TimeADT*)a) ), TimeADTGetDatum( *((TimeADT*)b) ) )
);
return DatumGetBool(
DirectFunctionCall2(time_eq, TimeADTGetDatum(*((TimeADT *) a)), TimeADTGetDatum(*((TimeADT *) b)))
);
}
static bool gbt_timele (const void *a, const void *b)
static bool
gbt_timele(const void *a, const void *b)
{
return DatumGetBool(
DirectFunctionCall2(time_le,TimeADTGetDatum( *((TimeADT*)a) ), TimeADTGetDatum( *((TimeADT*)b) ) )
);
return DatumGetBool(
DirectFunctionCall2(time_le, TimeADTGetDatum(*((TimeADT *) a)), TimeADTGetDatum(*((TimeADT *) b)))
);
}
static bool gbt_timelt (const void *a, const void *b)
static bool
gbt_timelt(const void *a, const void *b)
{
return DatumGetBool(
DirectFunctionCall2(time_lt,TimeADTGetDatum( *((TimeADT*)a) ), TimeADTGetDatum( *((TimeADT*)b) ) )
);
return DatumGetBool(
DirectFunctionCall2(time_lt, TimeADTGetDatum(*((TimeADT *) a)), TimeADTGetDatum(*((TimeADT *) b)))
);
}
@ -70,26 +75,24 @@ static bool gbt_timelt (const void *a, const void *b)
static int
gbt_timekey_cmp(const void *a, const void *b)
{
if ( gbt_timegt( (void*)&(((Nsrt *) a)->t[0]) , (void*)&(((Nsrt *) b)->t[0]) ) ){
return 1;
} else
if ( gbt_timelt( (void*)&(((Nsrt *) a)->t[0]) , (void*)&(((Nsrt *) b)->t[0]) ) ){
return -1;
}
return 0;
if (gbt_timegt((void *) &(((Nsrt *) a)->t[0]), (void *) &(((Nsrt *) b)->t[0])))
return 1;
else if (gbt_timelt((void *) &(((Nsrt *) a)->t[0]), (void *) &(((Nsrt *) b)->t[0])))
return -1;
return 0;
}
static const gbtree_ninfo tinfo =
static const gbtree_ninfo tinfo =
{
gbt_t_time,
sizeof(TimeADT),
gbt_timegt,
gbt_timege,
gbt_timeeq,
gbt_timele,
gbt_timelt,
gbt_timekey_cmp
gbt_t_time,
sizeof(TimeADT),
gbt_timegt,
gbt_timege,
gbt_timeeq,
gbt_timele,
gbt_timelt,
gbt_timekey_cmp
};
@ -102,150 +105,154 @@ static const gbtree_ninfo tinfo =
Datum
gbt_time_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER( gbt_num_compress( retval , entry , &tinfo ));
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER(gbt_num_compress(retval, entry, &tinfo));
}
Datum
gbt_timetz_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval;
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval;
if (entry->leafkey)
{
timeKEY *r = (timeKEY *) palloc(sizeof(timeKEY));
TimeTzADT *tz = DatumGetTimeTzADTP(entry->key);
if (entry->leafkey)
{
timeKEY *r = (timeKEY *) palloc(sizeof(timeKEY));
TimeTzADT *tz = DatumGetTimeTzADTP(entry->key);
retval = palloc(sizeof(GISTENTRY));
retval = palloc(sizeof(GISTENTRY));
/* We are using the time + zone only to compress */
r->lower = r->upper = ( tz->time + tz->zone ) ;
gistentryinit(*retval, PointerGetDatum(r),
entry->rel, entry->page,
entry->offset, sizeof(timeKEY), FALSE);
}
else
retval = entry;
PG_RETURN_POINTER(retval);
/* We are using the time + zone only to compress */
r->lower = r->upper = (tz->time + tz->zone);
gistentryinit(*retval, PointerGetDatum(r),
entry->rel, entry->page,
entry->offset, sizeof(timeKEY), FALSE);
}
else
retval = entry;
PG_RETURN_POINTER(retval);
}
Datum
gbt_time_consistent(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
TimeADT query = PG_GETARG_TIMEADT( 1 );
timeKEY *kkk = (timeKEY*) DatumGetPointer(entry->key);
GBT_NUMKEY_R key ;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
key.lower = (GBT_NUMKEY*) &kkk->lower ;
key.upper = (GBT_NUMKEY*) &kkk->upper ;
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
TimeADT query = PG_GETARG_TIMEADT(1);
timeKEY *kkk = (timeKEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
PG_RETURN_BOOL(
gbt_num_consistent( &key, (void*)&query,&strategy,GIST_LEAF(entry),&tinfo)
);
key.lower = (GBT_NUMKEY *) & kkk->lower;
key.upper = (GBT_NUMKEY *) & kkk->upper;
PG_RETURN_BOOL(
gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
);
}
Datum
gbt_timetz_consistent(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
TimeTzADT *query = PG_GETARG_TIMETZADT_P( 1 );
TimeADT qqq = query->time + query->zone ;
timeKEY *kkk = (timeKEY*) DatumGetPointer(entry->key);
GBT_NUMKEY_R key ;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
key.lower = (GBT_NUMKEY*) &kkk->lower ;
key.upper = (GBT_NUMKEY*) &kkk->upper ;
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
TimeTzADT *query = PG_GETARG_TIMETZADT_P(1);
TimeADT qqq = query->time + query->zone;
timeKEY *kkk = (timeKEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
PG_RETURN_BOOL(
gbt_num_consistent( &key, (void*)&qqq, &strategy,GIST_LEAF(entry),&tinfo)
);
key.lower = (GBT_NUMKEY *) & kkk->lower;
key.upper = (GBT_NUMKEY *) & kkk->upper;
PG_RETURN_BOOL(
gbt_num_consistent(&key, (void *) &qqq, &strategy, GIST_LEAF(entry), &tinfo)
);
}
Datum
gbt_time_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc(sizeof(timeKEY));
*(int *) PG_GETARG_POINTER(1) = sizeof(timeKEY);
PG_RETURN_POINTER( gbt_num_union ( (void*)out, entryvec, &tinfo ) );
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc(sizeof(timeKEY));
*(int *) PG_GETARG_POINTER(1) = sizeof(timeKEY);
PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
}
Datum
gbt_time_penalty(PG_FUNCTION_ARGS)
{
timeKEY *origentry = (timeKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
timeKEY *newentry = (timeKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
Interval *intr;
timeKEY *origentry = (timeKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
timeKEY *newentry = (timeKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
Interval *intr;
#ifdef HAVE_INT64_TIMESTAMP
int64 res;
int64 res;
#else
double res;
double res;
#endif
intr = DatumGetIntervalP(DirectFunctionCall2(
time_mi_time,
TimeADTGetDatum(newentry->upper),
TimeADTGetDatum(origentry->upper)));
intr = DatumGetIntervalP(DirectFunctionCall2(
time_mi_time,
TimeADTGetDatum(newentry->upper),
TimeADTGetDatum(origentry->upper)));
/* see interval_larger */
res = Max(intr->time + intr->month * (30 * 86400), 0);
pfree(intr);
/* see interval_larger */
res = Max(intr->time + intr->month * (30 * 86400), 0);
pfree(intr);
intr = DatumGetIntervalP(DirectFunctionCall2(
time_mi_time,
TimeADTGetDatum(origentry->lower),
TimeADTGetDatum(newentry->lower)));
/* see interval_larger */
res += Max(intr->time + intr->month * (30 * 86400), 0);
pfree(intr);
intr = DatumGetIntervalP(DirectFunctionCall2(
time_mi_time,
TimeADTGetDatum(origentry->lower),
TimeADTGetDatum(newentry->lower)));
*result = 0.0;
/* see interval_larger */
res += Max(intr->time + intr->month * (30 * 86400), 0);
pfree(intr);
if ( res > 0 ){
intr = DatumGetIntervalP(DirectFunctionCall2(
time_mi_time,
TimeADTGetDatum(origentry->upper),
TimeADTGetDatum(origentry->lower)));
*result += FLT_MIN ;
*result += (float) ( res / ( (double) ( res + intr->time + intr->month * (30 * 86400) ) ) );
*result *= ( FLT_MAX / ( ( (GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1 ) );
pfree ( intr );
}
*result = 0.0;
PG_RETURN_POINTER(result);
if (res > 0)
{
intr = DatumGetIntervalP(DirectFunctionCall2(
time_mi_time,
TimeADTGetDatum(origentry->upper),
TimeADTGetDatum(origentry->lower)));
*result += FLT_MIN;
*result += (float) (res / ((double) (res + intr->time + intr->month * (30 * 86400))));
*result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
pfree(intr);
}
PG_RETURN_POINTER(result);
}
Datum
gbt_time_picksplit(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}
Datum
gbt_time_same(PG_FUNCTION_ARGS)
{
timeKEY *b1 = (timeKEY *) PG_GETARG_POINTER(0);
timeKEY *b2 = (timeKEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
timeKEY *b1 = (timeKEY *) PG_GETARG_POINTER(0);
timeKEY *b2 = (timeKEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
*result = gbt_num_same ( (void*)b1, (void*)b2, &tinfo );
PG_RETURN_POINTER(result);
*result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
PG_RETURN_POINTER(result);
}

View File

@ -3,9 +3,9 @@
typedef struct
{
Timestamp lower;
Timestamp upper;
} tsKEY;
Timestamp lower;
Timestamp upper;
} tsKEY;
/*
** timestamp ops
@ -19,75 +19,78 @@ PG_FUNCTION_INFO_V1(gbt_tstz_consistent);
PG_FUNCTION_INFO_V1(gbt_ts_penalty);
PG_FUNCTION_INFO_V1(gbt_ts_same);
Datum gbt_ts_compress(PG_FUNCTION_ARGS);
Datum gbt_tstz_compress(PG_FUNCTION_ARGS);
Datum gbt_ts_union(PG_FUNCTION_ARGS);
Datum gbt_ts_picksplit(PG_FUNCTION_ARGS);
Datum gbt_ts_consistent(PG_FUNCTION_ARGS);
Datum gbt_tstz_consistent(PG_FUNCTION_ARGS);
Datum gbt_ts_penalty(PG_FUNCTION_ARGS);
Datum gbt_ts_same(PG_FUNCTION_ARGS);
Datum gbt_ts_compress(PG_FUNCTION_ARGS);
Datum gbt_tstz_compress(PG_FUNCTION_ARGS);
Datum gbt_ts_union(PG_FUNCTION_ARGS);
Datum gbt_ts_picksplit(PG_FUNCTION_ARGS);
Datum gbt_ts_consistent(PG_FUNCTION_ARGS);
Datum gbt_tstz_consistent(PG_FUNCTION_ARGS);
Datum gbt_ts_penalty(PG_FUNCTION_ARGS);
Datum gbt_ts_same(PG_FUNCTION_ARGS);
static bool gbt_tsgt (const void *a, const void *b)
static bool
gbt_tsgt(const void *a, const void *b)
{
return DatumGetBool(
DirectFunctionCall2(timestamp_gt,PointerGetDatum( a ), PointerGetDatum( b ) )
);
return DatumGetBool(
DirectFunctionCall2(timestamp_gt, PointerGetDatum(a), PointerGetDatum(b))
);
}
static bool gbt_tsge (const void *a, const void *b)
static bool
gbt_tsge(const void *a, const void *b)
{
return DatumGetBool(
DirectFunctionCall2(timestamp_ge,PointerGetDatum( a ), PointerGetDatum( b ) )
);
return DatumGetBool(
DirectFunctionCall2(timestamp_ge, PointerGetDatum(a), PointerGetDatum(b))
);
}
static bool gbt_tseq (const void *a, const void *b)
static bool
gbt_tseq(const void *a, const void *b)
{
return DatumGetBool(
DirectFunctionCall2(timestamp_eq,PointerGetDatum( a ), PointerGetDatum( b ) )
);
return DatumGetBool(
DirectFunctionCall2(timestamp_eq, PointerGetDatum(a), PointerGetDatum(b))
);
}
static bool gbt_tsle (const void *a, const void *b)
static bool
gbt_tsle(const void *a, const void *b)
{
return DatumGetBool(
DirectFunctionCall2(timestamp_le,PointerGetDatum( a ), PointerGetDatum( b ) )
);
return DatumGetBool(
DirectFunctionCall2(timestamp_le, PointerGetDatum(a), PointerGetDatum(b))
);
}
static bool gbt_tslt (const void *a, const void *b)
static bool
gbt_tslt(const void *a, const void *b)
{
return DatumGetBool(
DirectFunctionCall2(timestamp_lt,PointerGetDatum( a ), PointerGetDatum( b ) )
);
return DatumGetBool(
DirectFunctionCall2(timestamp_lt, PointerGetDatum(a), PointerGetDatum(b))
);
}
static int
gbt_tskey_cmp(const void *a, const void *b)
{
if ( gbt_tsgt( (void*)&(((Nsrt *) a)->t[0]) , (void*)&(((Nsrt *) b)->t[0]) ) ){
return 1;
} else
if ( gbt_tslt( (void*)&(((Nsrt *) a)->t[0]) , (void*)&(((Nsrt *) b)->t[0]) ) ){
return -1;
}
return 0;
if (gbt_tsgt((void *) &(((Nsrt *) a)->t[0]), (void *) &(((Nsrt *) b)->t[0])))
return 1;
else if (gbt_tslt((void *) &(((Nsrt *) a)->t[0]), (void *) &(((Nsrt *) b)->t[0])))
return -1;
return 0;
}
static const gbtree_ninfo tinfo =
{
gbt_t_ts,
sizeof(Timestamp),
gbt_tsgt,
gbt_tsge,
gbt_tseq,
gbt_tsle,
gbt_tslt,
gbt_tskey_cmp
static const gbtree_ninfo tinfo =
{
gbt_t_ts,
sizeof(Timestamp),
gbt_tsgt,
gbt_tsge,
gbt_tseq,
gbt_tsle,
gbt_tslt,
gbt_tskey_cmp
};
@ -97,26 +100,28 @@ static const gbtree_ninfo tinfo =
static Timestamp * tstz_to_ts_gmt ( Timestamp * gmt, TimestampTz * ts )
static Timestamp *
tstz_to_ts_gmt(Timestamp *gmt, TimestampTz *ts)
{
int val, tz ;
int val,
tz;
*gmt = *ts;
DecodeSpecial(0, "gmt", &val);
if ( ! TIMESTAMP_NOT_FINITE(*ts))
{
tz = val * 60;
*gmt = *ts;
DecodeSpecial(0, "gmt", &val);
if (!TIMESTAMP_NOT_FINITE(*ts))
{
tz = val * 60;
#ifdef HAVE_INT64_TIMESTAMP
*gmt -= (tz * INT64CONST(1000000));
*gmt -= (tz * INT64CONST(1000000));
#else
*gmt -= tz;
*gmt = JROUND(*gmt);
*gmt -= tz;
*gmt = JROUND(*gmt);
#endif
}
return gmt;
}
return gmt;
}
@ -125,84 +130,86 @@ static Timestamp * tstz_to_ts_gmt ( Timestamp * gmt, TimestampTz * ts )
Datum
gbt_ts_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER( gbt_num_compress( retval , entry , &tinfo ));
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER(gbt_num_compress(retval, entry, &tinfo));
}
Datum
gbt_tstz_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval ;
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval;
if (entry->leafkey)
{
tsKEY *r = (tsKEY *) palloc(sizeof(tsKEY));
if (entry->leafkey)
{
tsKEY *r = (tsKEY *) palloc(sizeof(tsKEY));
TimestampTz ts = *(TimestampTz *) DatumGetPointer(entry->key);
Timestamp gmt ;
TimestampTz ts = *(TimestampTz *) DatumGetPointer(entry->key);
Timestamp gmt;
tstz_to_ts_gmt ( &gmt, &ts );
tstz_to_ts_gmt(&gmt, &ts);
retval = palloc(sizeof(GISTENTRY));
r->lower = r->upper = gmt ;
gistentryinit(*retval, PointerGetDatum(r),
entry->rel, entry->page,
entry->offset, sizeof(tsKEY), FALSE);
}
else
retval = entry;
retval = palloc(sizeof(GISTENTRY));
r->lower = r->upper = gmt;
gistentryinit(*retval, PointerGetDatum(r),
entry->rel, entry->page,
entry->offset, sizeof(tsKEY), FALSE);
}
else
retval = entry;
PG_RETURN_POINTER( retval );
PG_RETURN_POINTER(retval);
}
Datum
gbt_ts_consistent(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
Timestamp *query = (Timestamp *) PG_GETARG_POINTER(1);
tsKEY *kkk = (tsKEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key ;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
Timestamp *query = (Timestamp *) PG_GETARG_POINTER(1);
tsKEY *kkk = (tsKEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
key.lower = (GBT_NUMKEY*) &kkk->lower ;
key.upper = (GBT_NUMKEY*) &kkk->upper ;
key.lower = (GBT_NUMKEY *) & kkk->lower;
key.upper = (GBT_NUMKEY *) & kkk->upper;
PG_RETURN_BOOL(
gbt_num_consistent( &key, (void*)query,&strategy,GIST_LEAF(entry),&tinfo)
);
PG_RETURN_BOOL(
gbt_num_consistent(&key, (void *) query, &strategy, GIST_LEAF(entry), &tinfo)
);
}
Datum
gbt_tstz_consistent(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
TimestampTz *query = (Timestamp *) PG_GETARG_POINTER(1);
char *kkk = (char *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key ;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
Timestamp qqq ;
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
TimestampTz *query = (Timestamp *) PG_GETARG_POINTER(1);
char *kkk = (char *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
Timestamp qqq;
key.lower = (GBT_NUMKEY*) &kkk[0];
key.upper = (GBT_NUMKEY*) &kkk[MAXALIGN(tinfo.size)];
tstz_to_ts_gmt ( &qqq, query );
key.lower = (GBT_NUMKEY *) & kkk[0];
key.upper = (GBT_NUMKEY *) & kkk[MAXALIGN(tinfo.size)];
tstz_to_ts_gmt(&qqq, query);
PG_RETURN_BOOL(
gbt_num_consistent( &key, (void*)&qqq,&strategy,GIST_LEAF(entry),&tinfo)
);
PG_RETURN_BOOL(
gbt_num_consistent(&key, (void *) &qqq, &strategy, GIST_LEAF(entry), &tinfo)
);
}
Datum
gbt_ts_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc(sizeof(tsKEY));
*(int *) PG_GETARG_POINTER(1) = sizeof(tsKEY);
PG_RETURN_POINTER( gbt_num_union ( (void*)out, entryvec, &tinfo ) );
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc(sizeof(tsKEY));
*(int *) PG_GETARG_POINTER(1) = sizeof(tsKEY);
PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
}
@ -210,52 +217,55 @@ Datum
gbt_ts_penalty(PG_FUNCTION_ARGS)
{
tsKEY *origentry = (tsKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
tsKEY *newentry = (tsKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
Interval *intr;
tsKEY *origentry = (tsKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
tsKEY *newentry = (tsKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
Interval *intr;
#ifdef HAVE_INT64_TIMESTAMP
int64 res;
int64 res;
#else
double res;
double res;
#endif
intr = DatumGetIntervalP(DirectFunctionCall2(
timestamp_mi,
TimestampGetDatum(newentry->upper),
TimestampGetDatum(origentry->upper)
));
intr = DatumGetIntervalP(DirectFunctionCall2(
timestamp_mi,
TimestampGetDatum(newentry->upper),
TimestampGetDatum(origentry->upper)
));
/* see interval_larger */
/* see interval_larger */
res = Max(intr->time + intr->month * (30 * 86400), 0);
pfree(intr);
res = Max(intr->time + intr->month * (30 * 86400), 0);
pfree(intr);
intr = DatumGetIntervalP(DirectFunctionCall2(
timestamp_mi,
TimestampGetDatum(origentry->lower),
TimestampGetDatum(newentry->lower)
));
intr = DatumGetIntervalP(DirectFunctionCall2(
timestamp_mi,
TimestampGetDatum(origentry->lower),
TimestampGetDatum(newentry->lower)
));
/* see interval_larger */
res += Max(intr->time + intr->month * (30 * 86400), 0);
pfree(intr);
/* see interval_larger */
res += Max(intr->time + intr->month * (30 * 86400), 0);
pfree(intr);
*result = 0.0;
*result = 0.0;
if ( res > 0 ){
intr = DatumGetIntervalP(DirectFunctionCall2(
timestamp_mi,
TimestampGetDatum(origentry->upper),
TimestampGetDatum(origentry->lower)
));
*result += FLT_MIN ;
*result += (float) ( res / ( (double) ( res + intr->time + intr->month * (30 * 86400) ) ) );
*result *= ( FLT_MAX / ( ( (GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1 ) );
pfree(intr);
}
if (res > 0)
{
intr = DatumGetIntervalP(DirectFunctionCall2(
timestamp_mi,
TimestampGetDatum(origentry->upper),
TimestampGetDatum(origentry->lower)
));
*result += FLT_MIN;
*result += (float) (res / ((double) (res + intr->time + intr->month * (30 * 86400))));
*result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
pfree(intr);
}
PG_RETURN_POINTER(result);
PG_RETURN_POINTER(result);
}
@ -263,21 +273,20 @@ gbt_ts_penalty(PG_FUNCTION_ARGS)
Datum
gbt_ts_picksplit(PG_FUNCTION_ARGS)
{
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}
Datum
gbt_ts_same(PG_FUNCTION_ARGS)
{
tsKEY *b1 = (tsKEY *) PG_GETARG_POINTER(0);
tsKEY *b2 = (tsKEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
tsKEY *b1 = (tsKEY *) PG_GETARG_POINTER(0);
tsKEY *b2 = (tsKEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
*result = gbt_num_same ( (void*)b1, (void*)b2, &tinfo );
PG_RETURN_POINTER(result);
*result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
PG_RETURN_POINTER(result);
}

View File

@ -3,58 +3,60 @@
#include "utils/date.h"
extern GISTENTRY *
gbt_num_compress( GISTENTRY *retval , GISTENTRY *entry , const gbtree_ninfo * tinfo )
gbt_num_compress(GISTENTRY *retval, GISTENTRY *entry, const gbtree_ninfo * tinfo)
{
if (entry->leafkey)
{
if (entry->leafkey)
{
union {
int16 i2;
int32 i4;
TimeADT ts;
DateADT dt;
} v ;
GBT_NUMKEY *r = ( GBT_NUMKEY * ) palloc(2 * tinfo->size );
void *leaf = NULL;
union
{
int16 i2;
int32 i4;
TimeADT ts;
DateADT dt;
} v;
switch ( tinfo->t )
{
case gbt_t_int2 :
v.i2 = DatumGetInt16(entry->key);
leaf = &v.i2;
break;
case gbt_t_int4 :
v.i4 = DatumGetInt32(entry->key);
leaf = &v.i4;
break;
case gbt_t_oid :
v.i4 = DatumGetObjectId(entry->key);
leaf = &v.i4;
break;
case gbt_t_time :
v.ts = DatumGetTimeADT(entry->key);
leaf = &v.ts;
break;
case gbt_t_date :
v.dt = DatumGetDateADT(entry->key);
leaf = &v.dt;
break;
default :
leaf = DatumGetPointer(entry->key);
}
GBT_NUMKEY *r = (GBT_NUMKEY *) palloc(2 * tinfo->size);
void *leaf = NULL;
memset ( (void*) &r[0] , 0 , 2*tinfo->size );
memcpy ( (void*) &r[0] , leaf, tinfo->size );
memcpy ( (void*) &r[tinfo->size] , leaf, tinfo->size );
retval = palloc(sizeof(GISTENTRY));
gistentryinit(*retval, PointerGetDatum(r), entry->rel, entry->page,
entry->offset,( 2 * tinfo->size ), FALSE);
} else
retval = entry;
switch (tinfo->t)
{
case gbt_t_int2:
v.i2 = DatumGetInt16(entry->key);
leaf = &v.i2;
break;
case gbt_t_int4:
v.i4 = DatumGetInt32(entry->key);
leaf = &v.i4;
break;
case gbt_t_oid:
v.i4 = DatumGetObjectId(entry->key);
leaf = &v.i4;
break;
case gbt_t_time:
v.ts = DatumGetTimeADT(entry->key);
leaf = &v.ts;
break;
case gbt_t_date:
v.dt = DatumGetDateADT(entry->key);
leaf = &v.dt;
break;
default:
leaf = DatumGetPointer(entry->key);
}
return retval;
memset((void *) &r[0], 0, 2 * tinfo->size);
memcpy((void *) &r[0], leaf, tinfo->size);
memcpy((void *) &r[tinfo->size], leaf, tinfo->size);
retval = palloc(sizeof(GISTENTRY));
gistentryinit(*retval, PointerGetDatum(r), entry->rel, entry->page,
entry->offset, (2 * tinfo->size), FALSE);
}
else
retval = entry;
return retval;
}
@ -65,34 +67,35 @@ gbt_num_compress( GISTENTRY *retval , GISTENTRY *entry , const gbtree_ninfo *
*/
extern void *
gbt_num_union( GBT_NUMKEY * out, const GistEntryVector * entryvec, const gbtree_ninfo * tinfo )
gbt_num_union(GBT_NUMKEY * out, const GistEntryVector *entryvec, const gbtree_ninfo * tinfo)
{
int i,
numranges;
GBT_NUMKEY * cur ;
GBT_NUMKEY_R o, c;
int i,
numranges;
GBT_NUMKEY *cur;
GBT_NUMKEY_R o,
c;
numranges = entryvec->n;
cur = (GBT_NUMKEY *) DatumGetPointer((entryvec->vector[0].key));
numranges = entryvec->n;
cur = (GBT_NUMKEY *) DatumGetPointer((entryvec->vector[0].key));
o.lower = &((GBT_NUMKEY *)out)[0];
o.upper = &((GBT_NUMKEY *)out)[tinfo->size];
o.lower = &((GBT_NUMKEY *) out)[0];
o.upper = &((GBT_NUMKEY *) out)[tinfo->size];
memcpy( (void*)out, (void*) cur, 2*tinfo->size );
memcpy((void *) out, (void *) cur, 2 * tinfo->size);
for (i = 1; i < numranges; i++)
{
cur = (GBT_NUMKEY *) DatumGetPointer((entryvec->vector[i].key));
c.lower = &cur[0];
c.upper = &cur[tinfo->size];
if ( (*tinfo->f_gt)(o.lower, c.lower) ) /* out->lower > cur->lower */
memcpy( (void* ) o.lower, (void*) c.lower, tinfo->size );
if ( (*tinfo->f_lt)(o.upper, c.upper) ) /* out->upper < cur->upper */
memcpy( (void*) o.upper, (void*) c.upper, tinfo->size );
}
for (i = 1; i < numranges; i++)
{
cur = (GBT_NUMKEY *) DatumGetPointer((entryvec->vector[i].key));
c.lower = &cur[0];
c.upper = &cur[tinfo->size];
if ((*tinfo->f_gt) (o.lower, c.lower)) /* out->lower > cur->lower */
memcpy((void *) o.lower, (void *) c.lower, tinfo->size);
if ((*tinfo->f_lt) (o.upper, c.upper)) /* out->upper < cur->upper */
memcpy((void *) o.upper, (void *) c.upper, tinfo->size);
}
return out;
return out;
}
@ -101,51 +104,54 @@ gbt_num_union( GBT_NUMKEY * out, const GistEntryVector * entryvec, const gbtree_
** The GiST same method for numerical values
*/
extern bool gbt_num_same ( const GBT_NUMKEY * a, const GBT_NUMKEY * b, const gbtree_ninfo * tinfo )
extern bool
gbt_num_same(const GBT_NUMKEY * a, const GBT_NUMKEY * b, const gbtree_ninfo * tinfo)
{
GBT_NUMKEY_R b1, b2 ;
GBT_NUMKEY_R b1,
b2;
b1.lower = &(((GBT_NUMKEY *)a)[0]);
b1.upper = &(((GBT_NUMKEY *)a)[tinfo->size]);
b2.lower = &(((GBT_NUMKEY *)b)[0]);
b2.upper = &(((GBT_NUMKEY *)b)[tinfo->size]);
b1.lower = &(((GBT_NUMKEY *) a)[0]);
b1.upper = &(((GBT_NUMKEY *) a)[tinfo->size]);
b2.lower = &(((GBT_NUMKEY *) b)[0]);
b2.upper = &(((GBT_NUMKEY *) b)[tinfo->size]);
if (
(*tinfo->f_eq) (b1.lower, b2.lower) &&
(*tinfo->f_eq) (b1.upper, b2.upper)
)
return TRUE;
return FALSE;
if (
(*tinfo->f_eq)( b1.lower, b2.lower) &&
(*tinfo->f_eq)( b1.upper, b2.upper)
)
return TRUE;
return FALSE;
}
extern void
gbt_num_bin_union(Datum * u , GBT_NUMKEY * e , const gbtree_ninfo * tinfo )
gbt_num_bin_union(Datum *u, GBT_NUMKEY * e, const gbtree_ninfo * tinfo)
{
GBT_NUMKEY_R rd;
GBT_NUMKEY_R rd;
rd.lower = &e[0];
rd.upper = &e[tinfo->size];
rd.lower = &e[0];
rd.upper = &e[tinfo->size];
if (!DatumGetPointer(*u))
{
*u = PointerGetDatum(palloc(2 * tinfo->size));
memcpy( (void* ) &( ( (GBT_NUMKEY *) DatumGetPointer(*u) )[0] ) , (void*)rd.lower , tinfo->size );
memcpy( (void* ) &( ( (GBT_NUMKEY *) DatumGetPointer(*u) )[tinfo->size]) , (void*)rd.upper , tinfo->size );
}
else
{
GBT_NUMKEY_R ur ;
ur.lower = &( ( (GBT_NUMKEY *) DatumGetPointer(*u) )[0] ) ;
ur.upper = &( ( (GBT_NUMKEY *) DatumGetPointer(*u) )[tinfo->size]) ;
if ( (*tinfo->f_gt)((void*)ur.lower, (void*)rd.lower) )
memcpy( (void*) ur.lower, (void*) rd.lower, tinfo->size );
if ( (*tinfo->f_lt)((void*)ur.upper, (void*)rd.upper) )
memcpy( (void*) ur.upper, (void*) rd.upper, tinfo->size );
}
if (!DatumGetPointer(*u))
{
*u = PointerGetDatum(palloc(2 * tinfo->size));
memcpy((void *) &(((GBT_NUMKEY *) DatumGetPointer(*u))[0]), (void *) rd.lower, tinfo->size);
memcpy((void *) &(((GBT_NUMKEY *) DatumGetPointer(*u))[tinfo->size]), (void *) rd.upper, tinfo->size);
}
else
{
GBT_NUMKEY_R ur;
ur.lower = &(((GBT_NUMKEY *) DatumGetPointer(*u))[0]);
ur.upper = &(((GBT_NUMKEY *) DatumGetPointer(*u))[tinfo->size]);
if ((*tinfo->f_gt) ((void *) ur.lower, (void *) rd.lower))
memcpy((void *) ur.lower, (void *) rd.lower, tinfo->size);
if ((*tinfo->f_lt) ((void *) ur.upper, (void *) rd.upper))
memcpy((void *) ur.upper, (void *) rd.upper, tinfo->size);
}
}
@ -154,98 +160,98 @@ gbt_num_bin_union(Datum * u , GBT_NUMKEY * e , const gbtree_ninfo * tinfo )
** The GiST consistent method
*/
extern bool
extern bool
gbt_num_consistent(
const GBT_NUMKEY_R * key,
const void * query,
const StrategyNumber * strategy,
bool is_leaf,
const gbtree_ninfo * tinfo
const GBT_NUMKEY_R * key,
const void *query,
const StrategyNumber *strategy,
bool is_leaf,
const gbtree_ninfo * tinfo
)
{
bool retval = FALSE;
bool retval = FALSE;
switch (*strategy)
{
case BTLessEqualStrategyNumber:
retval = (*tinfo->f_ge)(query, key->lower);
break;
case BTLessStrategyNumber:
if ( is_leaf )
retval = (*tinfo->f_gt)(query, key->lower);
else
retval = (*tinfo->f_ge)(query, key->lower);
break;
case BTEqualStrategyNumber:
if ( is_leaf )
retval = (*tinfo->f_eq)(query, key->lower);
else
retval = (*tinfo->f_le)(key->lower, query) && (*tinfo->f_le)(query, key->upper );
break;
case BTGreaterStrategyNumber:
if ( is_leaf )
retval = (*tinfo->f_lt)(query, key->upper);
else
retval = (*tinfo->f_le)(query, key->upper);
break;
case BTGreaterEqualStrategyNumber:
retval = (*tinfo->f_le)(query, key->upper);
break;
default:
retval = FALSE;
}
switch (*strategy)
{
case BTLessEqualStrategyNumber:
retval = (*tinfo->f_ge) (query, key->lower);
break;
case BTLessStrategyNumber:
if (is_leaf)
retval = (*tinfo->f_gt) (query, key->lower);
else
retval = (*tinfo->f_ge) (query, key->lower);
break;
case BTEqualStrategyNumber:
if (is_leaf)
retval = (*tinfo->f_eq) (query, key->lower);
else
retval = (*tinfo->f_le) (key->lower, query) && (*tinfo->f_le) (query, key->upper);
break;
case BTGreaterStrategyNumber:
if (is_leaf)
retval = (*tinfo->f_lt) (query, key->upper);
else
retval = (*tinfo->f_le) (query, key->upper);
break;
case BTGreaterEqualStrategyNumber:
retval = (*tinfo->f_le) (query, key->upper);
break;
default:
retval = FALSE;
}
return (retval);
return (retval);
}
GIST_SPLITVEC *
gbt_num_picksplit( const GistEntryVector *entryvec, GIST_SPLITVEC *v,
const gbtree_ninfo * tinfo )
gbt_num_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v,
const gbtree_ninfo * tinfo)
{
OffsetNumber i,
maxoff = entryvec->n - 1;
Nsrt *arr;
int nbytes;
OffsetNumber i,
maxoff = entryvec->n - 1;
Nsrt *arr;
int nbytes;
arr = (Nsrt *) palloc((maxoff+1) * sizeof(Nsrt));
nbytes = (maxoff + 2) * sizeof(OffsetNumber);
v->spl_left = (OffsetNumber *) palloc(nbytes);
v->spl_right = (OffsetNumber *) palloc(nbytes);
v->spl_ldatum = PointerGetDatum(0);
v->spl_rdatum = PointerGetDatum(0);
v->spl_nleft = 0;
v->spl_nright = 0;
arr = (Nsrt *) palloc((maxoff + 1) * sizeof(Nsrt));
nbytes = (maxoff + 2) * sizeof(OffsetNumber);
v->spl_left = (OffsetNumber *) palloc(nbytes);
v->spl_right = (OffsetNumber *) palloc(nbytes);
v->spl_ldatum = PointerGetDatum(0);
v->spl_rdatum = PointerGetDatum(0);
v->spl_nleft = 0;
v->spl_nright = 0;
/* Sort entries */
/* Sort entries */
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
{
arr[i].t = (GBT_NUMKEY *) DatumGetPointer((entryvec->vector[i].key));
arr[i].i = i;
}
qsort ( (void*) &arr[FirstOffsetNumber], maxoff-FirstOffsetNumber+1,sizeof(Nsrt), tinfo->f_cmp );
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
{
arr[i].t = (GBT_NUMKEY *) DatumGetPointer((entryvec->vector[i].key));
arr[i].i = i;
}
qsort((void *) &arr[FirstOffsetNumber], maxoff - FirstOffsetNumber + 1, sizeof(Nsrt), tinfo->f_cmp);
/* We do simply create two parts */
/* We do simply create two parts */
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
{
if (i <= (maxoff - FirstOffsetNumber + 1) / 2)
{
gbt_num_bin_union(&v->spl_ldatum, arr[i].t, tinfo);
v->spl_left[v->spl_nleft] = arr[i].i;
v->spl_nleft++;
}
else
{
gbt_num_bin_union(&v->spl_rdatum, arr[i].t, tinfo);
v->spl_right[v->spl_nright] = arr[i].i;
v->spl_nright++;
}
}
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
{
if (i <= (maxoff - FirstOffsetNumber + 1) / 2)
{
gbt_num_bin_union(&v->spl_ldatum, arr[i].t, tinfo);
v->spl_left[v->spl_nleft] = arr[i].i;
v->spl_nleft++;
}
else
{
gbt_num_bin_union(&v->spl_rdatum, arr[i].t, tinfo);
v->spl_right[v->spl_nright] = arr[i].i;
v->spl_nright++;
}
}
pfree(arr);
return v;
return v;
}

View File

@ -4,16 +4,17 @@ typedef char GBT_NUMKEY;
/* Better readable key */
typedef struct
{
const GBT_NUMKEY * lower, * upper;
} GBT_NUMKEY_R;
const GBT_NUMKEY *lower,
*upper;
} GBT_NUMKEY_R;
/* for sorting */
typedef struct
{
int i;
GBT_NUMKEY * t;
} Nsrt;
int i;
GBT_NUMKEY *t;
} Nsrt;
/* type description */
@ -21,53 +22,53 @@ typedef struct
typedef struct
{
/* Attribs */
/* Attribs */
enum gbtree_type t ; /* data type */
int32 size ; /* size of type , 0 means variable */
enum gbtree_type t; /* data type */
int32 size; /* size of type , 0 means variable */
/* Methods */
/* Methods */
bool (*f_gt) ( const void * , const void * ); /* greater then */
bool (*f_ge) ( const void * , const void * ); /* greater equal */
bool (*f_eq) ( const void * , const void * ); /* equal */
bool (*f_le) ( const void * , const void * ); /* less equal */
bool (*f_lt) ( const void * , const void * ); /* less then */
int (*f_cmp) ( const void * , const void * ); /* key compare function */
} gbtree_ninfo;
bool (*f_gt) (const void *, const void *); /* greater then */
bool (*f_ge) (const void *, const void *); /* greater equal */
bool (*f_eq) (const void *, const void *); /* equal */
bool (*f_le) (const void *, const void *); /* less equal */
bool (*f_lt) (const void *, const void *); /* less then */
int (*f_cmp) (const void *, const void *); /* key compare function */
} gbtree_ninfo;
/*
* Numeric btree functions
* Numeric btree functions
*/
#define penalty_range_enlarge(olower,oupper,nlower,nupper) do { \
res = 0; \
if ( (nupper) > (oupper) ) \
res += ( (nupper) - (oupper) ); \
if ( (olower) > (nlower) ) \
res += ( (olower) - (nlower) ); \
res += ( (nupper) - (oupper) ); \
if ( (olower) > (nlower) ) \
res += ( (olower) - (nlower) ); \
} while (0);
extern bool gbt_num_consistent( const GBT_NUMKEY_R * key , const void * query,
const StrategyNumber * strategy , bool is_leaf,
const gbtree_ninfo * tinfo );
extern bool gbt_num_consistent(const GBT_NUMKEY_R * key, const void *query,
const StrategyNumber *strategy, bool is_leaf,
const gbtree_ninfo * tinfo);
extern GIST_SPLITVEC *gbt_num_picksplit ( const GistEntryVector *entryvec, GIST_SPLITVEC *v,
const gbtree_ninfo * tinfo );
extern GIST_SPLITVEC *gbt_num_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v,
const gbtree_ninfo * tinfo);
extern GISTENTRY *gbt_num_compress( GISTENTRY *retval , GISTENTRY *entry ,
const gbtree_ninfo * tinfo );
extern GISTENTRY *gbt_num_compress(GISTENTRY *retval, GISTENTRY *entry,
const gbtree_ninfo * tinfo);
extern void *gbt_num_union ( GBT_NUMKEY * out, const GistEntryVector * entryvec,
const gbtree_ninfo * tinfo );
extern void *gbt_num_union(GBT_NUMKEY * out, const GistEntryVector *entryvec,
const gbtree_ninfo * tinfo);
extern bool gbt_num_same ( const GBT_NUMKEY * a, const GBT_NUMKEY * b,
const gbtree_ninfo * tinfo );
extern bool gbt_num_same(const GBT_NUMKEY * a, const GBT_NUMKEY * b,
const gbtree_ninfo * tinfo);
extern void gbt_num_bin_union(Datum * u , GBT_NUMKEY * e ,
const gbtree_ninfo * tinfo );
extern void gbt_num_bin_union(Datum *u, GBT_NUMKEY * e,
const gbtree_ninfo * tinfo);

File diff suppressed because it is too large Load Diff

View File

@ -5,15 +5,16 @@ typedef bytea GBT_VARKEY;
/* Better readable key */
typedef struct
{
bytea * lower, * upper;
} GBT_VARKEY_R;
bytea *lower,
*upper;
} GBT_VARKEY_R;
/* used for key sorting */
typedef struct
{
int i ;
GBT_VARKEY * t ;
} Vsrt ;
int i;
GBT_VARKEY *t;
} Vsrt;
/*
type description
@ -23,45 +24,45 @@ typedef struct
typedef struct
{
/* Attribs */
/* Attribs */
enum gbtree_type t ; /* data type */
bool str ; /* true, if string ( else binary ) */
bool trnc ; /* truncate (=compress) key */
enum gbtree_type t; /* data type */
bool str; /* true, if string ( else binary ) */
bool trnc; /* truncate (=compress) key */
/* Methods */
/* Methods */
bool (*f_gt) ( const void * , const void * ); /* greater then */
bool (*f_ge) ( const void * , const void * ); /* greater equal */
bool (*f_eq) ( const void * , const void * ); /* equal */
bool (*f_le) ( const void * , const void * ); /* less equal */
bool (*f_lt) ( const void * , const void * ); /* less then */
int32 (*f_cmp) ( const bytea * , const bytea * ); /* node compare */
GBT_VARKEY* (*f_l2n) ( GBT_VARKEY * ); /* convert leaf to node */
} gbtree_vinfo;
bool (*f_gt) (const void *, const void *); /* greater then */
bool (*f_ge) (const void *, const void *); /* greater equal */
bool (*f_eq) (const void *, const void *); /* equal */
bool (*f_le) (const void *, const void *); /* less equal */
bool (*f_lt) (const void *, const void *); /* less then */
int32 (*f_cmp) (const bytea *, const bytea *); /* node compare */
GBT_VARKEY *(*f_l2n) (GBT_VARKEY *); /* convert leaf to node */
} gbtree_vinfo;
extern GBT_VARKEY_R gbt_var_key_readable ( const GBT_VARKEY * k );
extern GBT_VARKEY_R gbt_var_key_readable(const GBT_VARKEY * k);
extern GBT_VARKEY *gbt_var_key_copy ( const GBT_VARKEY_R * u, bool force_node );
extern GBT_VARKEY *gbt_var_key_copy(const GBT_VARKEY_R * u, bool force_node);
extern GISTENTRY *gbt_var_compress ( GISTENTRY *entry , const gbtree_vinfo * tinfo );
extern GISTENTRY *gbt_var_compress(GISTENTRY *entry, const gbtree_vinfo * tinfo);
extern GBT_VARKEY *gbt_var_union ( const GistEntryVector * entryvec , int32 * size ,
const gbtree_vinfo * tinfo );
extern GBT_VARKEY *gbt_var_union(const GistEntryVector *entryvec, int32 *size,
const gbtree_vinfo * tinfo);
extern bool gbt_var_same ( bool * result, const Datum d1 , const Datum d2 ,
const gbtree_vinfo * tinfo );
extern bool gbt_var_same(bool *result, const Datum d1, const Datum d2,
const gbtree_vinfo * tinfo);
extern float *gbt_var_penalty ( float * res , const GISTENTRY * o , const GISTENTRY * n,
const gbtree_vinfo * tinfo );
extern float *gbt_var_penalty(float *res, const GISTENTRY *o, const GISTENTRY *n,
const gbtree_vinfo * tinfo);
extern bool gbt_var_consistent( GBT_VARKEY_R * key , const void * query,
const StrategyNumber * strategy , bool is_leaf,
const gbtree_vinfo * tinfo );
extern bool gbt_var_consistent(GBT_VARKEY_R * key, const void *query,
const StrategyNumber *strategy, bool is_leaf,
const gbtree_vinfo * tinfo);
extern GIST_SPLITVEC *gbt_var_picksplit ( const GistEntryVector *entryvec, GIST_SPLITVEC *v,
const gbtree_vinfo * tinfo );
extern void gbt_var_bin_union ( Datum * u , GBT_VARKEY * e ,
const gbtree_vinfo * tinfo );
extern GIST_SPLITVEC *gbt_var_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v,
const gbtree_vinfo * tinfo);
extern void gbt_var_bin_union(Datum *u, GBT_VARKEY * e,
const gbtree_vinfo * tinfo);

View File

@ -230,7 +230,7 @@ g_cube_union(GistEntryVector *entryvec, int *sizep)
for (i = 1; i < entryvec->n; i++)
{
out = g_cube_binary_union(tmp, (NDBOX *)
DatumGetPointer(entryvec->vector[i].key),
DatumGetPointer(entryvec->vector[i].key),
sizep);
if (i > 1)
pfree(tmp);

View File

@ -700,8 +700,8 @@ main(int argc, char **argv)
if (verbose > 1)
printf("Opening dbf-file\n");
setlocale(LC_ALL, ""); /* fix for isprint() */
setlocale(LC_ALL, ""); /* fix for isprint() */
if ((dbh = dbf_open(argv[0], O_RDONLY)) == (dbhead *) - 1)
{
fprintf(stderr, "Couldn't open xbase-file %s\n", argv[0]);

View File

@ -571,8 +571,8 @@ dblink_fetch(PG_FUNCTION_ARGS)
rsinfo->expectedDesc == NULL)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("function returning record called in context "
"that cannot accept type record")));
errmsg("function returning record called in context "
"that cannot accept type record")));
/* get the requested return tuple description */
tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
@ -777,8 +777,8 @@ dblink_record(PG_FUNCTION_ARGS)
rsinfo->expectedDesc == NULL)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("function returning record called in context "
"that cannot accept type record")));
errmsg("function returning record called in context "
"that cannot accept type record")));
/* get the requested return tuple description */
tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);

View File

@ -1,7 +1,7 @@
/****************************************************************************
* pending.c
* $Id: pending.c,v 1.18 2004/05/26 00:08:26 wieck Exp $
* $PostgreSQL: pgsql/contrib/dbmirror/pending.c,v 1.18 2004/05/26 00:08:26 wieck Exp $
* $Id: pending.c,v 1.19 2004/08/29 05:06:35 momjian Exp $
* $PostgreSQL: pgsql/contrib/dbmirror/pending.c,v 1.19 2004/08/29 05:06:35 momjian Exp $
*
* This file contains a trigger for Postgresql-7.x to record changes to tables
* to a pending table for mirroring.
@ -43,17 +43,17 @@ enum FieldUsage
};
int storePending(char *cpTableName, HeapTuple tBeforeTuple,
HeapTuple tAfterTuple,
TupleDesc tTupdesc,
Oid tableOid,
char cOp);
HeapTuple tAfterTuple,
TupleDesc tTupdesc,
Oid tableOid,
char cOp);
int storeKeyInfo(char *cpTableName, HeapTuple tTupleData, TupleDesc tTuplDesc,
Oid tableOid);
int storeData(char *cpTableName, HeapTuple tTupleData,
TupleDesc tTupleDesc,Oid tableOid,int iIncludeKeyData);
Oid tableOid);
int storeData(char *cpTableName, HeapTuple tTupleData,
TupleDesc tTupleDesc, Oid tableOid, int iIncludeKeyData);
int2vector *getPrimaryKey(Oid tblOid);
@ -74,10 +74,9 @@ PG_FUNCTION_INFO_V1(recordchange);
#define debug_msg(x) elog(NOTICE,x)
#define debug_msg3(x,y,z) elog(NOTICE,x,y,z)
#else
#define debug_msg2(x,y)
#define debug_msg2(x,y)
#define debug_msg(x)
#define debug_msg3(x,y,z)
#endif
@ -85,8 +84,8 @@ PG_FUNCTION_INFO_V1(recordchange);
extern Datum nextval(PG_FUNCTION_ARGS);
extern Datum setval(PG_FUNCTION_ARGS);
int saveSequenceUpdate(const text * sequenceName,
int nextSequenceValue);
int saveSequenceUpdate(const text *sequenceName,
int nextSequenceValue);
/*****************************************************************************
@ -107,15 +106,15 @@ recordchange(PG_FUNCTION_ARGS)
char op = 0;
char *schemaname;
char *fullyqualtblname;
char *pkxpress=NULL;
char *pkxpress = NULL;
if (fcinfo->context != NULL)
{
if (SPI_connect() < 0)
{
ereport(ERROR,(errcode(ERRCODE_CONNECTION_FAILURE),
errmsg("dbmirror:recordchange could not connect to SPI")));
ereport(ERROR, (errcode(ERRCODE_CONNECTION_FAILURE),
errmsg("dbmirror:recordchange could not connect to SPI")));
return -1;
}
trigdata = (TriggerData *) fcinfo->context;
@ -154,13 +153,13 @@ recordchange(PG_FUNCTION_ARGS)
}
else
{
ereport(ERROR,(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
errmsg("dbmirror:recordchange Unknown operation")));
ereport(ERROR, (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
errmsg("dbmirror:recordchange Unknown operation")));
}
if (storePending(fullyqualtblname, beforeTuple, afterTuple,
tupdesc, retTuple->t_tableOid, op))
if (storePending(fullyqualtblname, beforeTuple, afterTuple,
tupdesc, retTuple->t_tableOid, op))
{
/* An error occoured. Skip the operation. */
ereport(ERROR,
@ -173,8 +172,8 @@ recordchange(PG_FUNCTION_ARGS)
debug_msg("dbmirror:recordchange returning on success");
SPI_pfree(fullyqualtblname);
if(pkxpress != NULL)
SPI_pfree(pkxpress);
if (pkxpress != NULL)
SPI_pfree(pkxpress);
SPI_finish();
return PointerGetDatum(retTuple);
}
@ -196,20 +195,20 @@ int
storePending(char *cpTableName, HeapTuple tBeforeTuple,
HeapTuple tAfterTuple,
TupleDesc tTupDesc,
Oid tableOid,
Oid tableOid,
char cOp)
{
char *cpQueryBase = "INSERT INTO dbmirror_pending (TableName,Op,XID) VALUES ($1,$2,$3)";
int iResult = 0;
HeapTuple tCurTuple;
char nulls[3]=" ";
char nulls[3] = " ";
/* Points the current tuple(before or after) */
Datum saPlanData[3];
Oid taPlanArgTypes[4] = {NAMEOID,
CHAROID,
INT4OID};
Oid taPlanArgTypes[4] = {NAMEOID,
CHAROID,
INT4OID};
void *vpPlan;
tCurTuple = tBeforeTuple ? tBeforeTuple : tAfterTuple;
@ -218,8 +217,8 @@ storePending(char *cpTableName, HeapTuple tBeforeTuple,
vpPlan = SPI_prepare(cpQueryBase, 3, taPlanArgTypes);
if (vpPlan == NULL)
ereport(ERROR,(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
errmsg("dbmirror:storePending error creating plan")));
ereport(ERROR, (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
errmsg("dbmirror:storePending error creating plan")));
saPlanData[0] = PointerGetDatum(cpTableName);
@ -228,8 +227,8 @@ storePending(char *cpTableName, HeapTuple tBeforeTuple,
iResult = SPI_execp(vpPlan, saPlanData, nulls, 1);
if (iResult < 0)
elog(NOTICE, "storedPending fired (%s) returned %d",
cpQueryBase, iResult);
elog(NOTICE, "storedPending fired (%s) returned %d",
cpQueryBase, iResult);
@ -242,8 +241,8 @@ storePending(char *cpTableName, HeapTuple tBeforeTuple,
* This is a record of a delete operation.
* Just store the key data.
*/
iResult = storeKeyInfo(cpTableName,
tBeforeTuple, tTupDesc, tableOid);
iResult = storeKeyInfo(cpTableName,
tBeforeTuple, tTupDesc, tableOid);
}
else if (cOp == 'i')
{
@ -251,18 +250,18 @@ storePending(char *cpTableName, HeapTuple tBeforeTuple,
* An Insert operation.
* Store all data
*/
iResult = storeData(cpTableName, tAfterTuple,
tTupDesc, tableOid,TRUE);
iResult = storeData(cpTableName, tAfterTuple,
tTupDesc, tableOid, TRUE);
}
else
{
/* op must be an update. */
iResult = storeKeyInfo(cpTableName, tBeforeTuple,
tTupDesc, tableOid);
iResult = iResult ? iResult :
storeData(cpTableName, tAfterTuple, tTupDesc,
tableOid,TRUE);
iResult = storeKeyInfo(cpTableName, tBeforeTuple,
tTupDesc, tableOid);
iResult = iResult ? iResult :
storeData(cpTableName, tAfterTuple, tTupDesc,
tableOid, TRUE);
}
@ -292,7 +291,7 @@ storeKeyInfo(char *cpTableName, HeapTuple tTupleData,
}
/* pplan = SPI_saveplan(pplan); */
cpKeyData = packageData(tTupleData, tTupleDesc,tableOid, PRIMARY);
cpKeyData = packageData(tTupleData, tTupleDesc, tableOid, PRIMARY);
if (cpKeyData == NULL)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
@ -312,8 +311,8 @@ storeKeyInfo(char *cpTableName, HeapTuple tTupleData,
if (iRetCode != SPI_OK_INSERT)
{
ereport(ERROR,(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION)
,errmsg("error inserting row in pendingDelete")));
ereport(ERROR, (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION)
,errmsg("error inserting row in pendingDelete")));
return -1;
}
@ -360,8 +359,8 @@ getPrimaryKey(Oid tblOid)
* Stores a copy of the non-key data for the row.
*****************************************************************************/
int
storeData(char *cpTableName, HeapTuple tTupleData,
TupleDesc tTupleDesc,Oid tableOid, int iIncludeKeyData)
storeData(char *cpTableName, HeapTuple tTupleData,
TupleDesc tTupleDesc, Oid tableOid, int iIncludeKeyData)
{
Oid planArgTypes[1] = {NAMEOID};
@ -380,10 +379,10 @@ storeData(char *cpTableName, HeapTuple tTupleData,
/* pplan = SPI_saveplan(pplan); */
if (iIncludeKeyData == 0)
cpKeyData = packageData(tTupleData, tTupleDesc,
tableOid, NONPRIMARY);
cpKeyData = packageData(tTupleData, tTupleDesc,
tableOid, NONPRIMARY);
else
cpKeyData = packageData(tTupleData, tTupleDesc,tableOid, ALL);
cpKeyData = packageData(tTupleData, tTupleDesc, tableOid, ALL);
planData[0] = PointerGetDatum(cpKeyData);
iRetValue = SPI_execp(pplan, planData, NULL, 1);
@ -439,10 +438,10 @@ packageData(HeapTuple tTupleData, TupleDesc tTupleDesc, Oid tableOid,
}
if (tpPKeys != NULL)
{
debug_msg("dbmirror:packageData have primary keys");
{
debug_msg("dbmirror:packageData have primary keys");
}
}
cpDataBlock = SPI_palloc(BUFFER_SIZE);
iDataBlockSize = BUFFER_SIZE;
@ -463,18 +462,18 @@ packageData(HeapTuple tTupleData, TupleDesc tTupleDesc, Oid tableOid,
/* Determine if this is a primary key or not. */
iIsPrimaryKey = 0;
for (iPrimaryKeyIndex = 0;
(*tpPKeys)[iPrimaryKeyIndex] != 0;
(*tpPKeys)[iPrimaryKeyIndex] != 0;
iPrimaryKeyIndex++)
{
if ((*tpPKeys)[iPrimaryKeyIndex]
== iColumnCounter)
if ((*tpPKeys)[iPrimaryKeyIndex]
== iColumnCounter)
{
iIsPrimaryKey = 1;
break;
}
}
if (iIsPrimaryKey ? (eKeyUsage != PRIMARY) :
(eKeyUsage != NONPRIMARY))
if (iIsPrimaryKey ? (eKeyUsage != PRIMARY) :
(eKeyUsage != NONPRIMARY))
{
/**
* Don't use.
@ -486,34 +485,34 @@ packageData(HeapTuple tTupleData, TupleDesc tTupleDesc, Oid tableOid,
}
} /* KeyUsage!=ALL */
if(tTupleDesc->attrs[iColumnCounter-1]->attisdropped)
{
/**
* This column has been dropped.
* Do not mirror it.
*/
continue;
}
if (tTupleDesc->attrs[iColumnCounter - 1]->attisdropped)
{
/**
* This column has been dropped.
* Do not mirror it.
*/
continue;
}
cpFieldName = DatumGetPointer(NameGetDatum
(&tTupleDesc->attrs
[iColumnCounter - 1]->attname));
(&tTupleDesc->attrs
[iColumnCounter - 1]->attname));
debug_msg2("dbmirror:packageData field name: %s", cpFieldName);
while (iDataBlockSize - iUsedDataBlock <
strlen(cpFieldName) + 6)
while (iDataBlockSize - iUsedDataBlock <
strlen(cpFieldName) + 6)
{
cpDataBlock = SPI_repalloc(cpDataBlock,
iDataBlockSize +
BUFFER_SIZE);
cpDataBlock = SPI_repalloc(cpDataBlock,
iDataBlockSize +
BUFFER_SIZE);
iDataBlockSize = iDataBlockSize + BUFFER_SIZE;
}
sprintf(cpDataBlock + iUsedDataBlock, "\"%s\"=", cpFieldName);
iUsedDataBlock = iUsedDataBlock + strlen(cpFieldName) + 3;
cpFieldData = SPI_getvalue(tTupleData, tTupleDesc,
iColumnCounter);
cpFieldData = SPI_getvalue(tTupleData, tTupleDesc,
iColumnCounter);
cpUnFormatedPtr = cpFieldData;
cpFormatedPtr = cpDataBlock + iUsedDataBlock;
@ -531,17 +530,17 @@ packageData(HeapTuple tTupleData, TupleDesc tTupleDesc, Oid tableOid,
continue;
}
debug_msg2("dbmirror:packageData field data: \"%s\"",
cpFieldData);
debug_msg2("dbmirror:packageData field data: \"%s\"",
cpFieldData);
debug_msg("dbmirror:packageData starting format loop");
while (*cpUnFormatedPtr != 0)
{
while (iDataBlockSize - iUsedDataBlock < 2)
{
cpDataBlock = SPI_repalloc(cpDataBlock,
iDataBlockSize
+ BUFFER_SIZE);
cpDataBlock = SPI_repalloc(cpDataBlock,
iDataBlockSize
+ BUFFER_SIZE);
iDataBlockSize = iDataBlockSize + BUFFER_SIZE;
cpFormatedPtr = cpDataBlock + iUsedDataBlock;
}
@ -561,25 +560,25 @@ packageData(HeapTuple tTupleData, TupleDesc tTupleDesc, Oid tableOid,
while (iDataBlockSize - iUsedDataBlock < 3)
{
cpDataBlock = SPI_repalloc(cpDataBlock,
iDataBlockSize +
BUFFER_SIZE);
cpDataBlock = SPI_repalloc(cpDataBlock,
iDataBlockSize +
BUFFER_SIZE);
iDataBlockSize = iDataBlockSize + BUFFER_SIZE;
cpFormatedPtr = cpDataBlock + iUsedDataBlock;
}
sprintf(cpFormatedPtr, "' ");
iUsedDataBlock = iUsedDataBlock + 2;
debug_msg2("dbmirror:packageData data block: \"%s\"",
cpDataBlock);
debug_msg2("dbmirror:packageData data block: \"%s\"",
cpDataBlock);
} /* for iColumnCounter */
if (tpPKeys != NULL)
SPI_pfree(tpPKeys);
debug_msg3("dbmirror:packageData returning DataBlockSize:%d iUsedDataBlock:%d",
iDataBlockSize,
iUsedDataBlock);
debug_msg3("dbmirror:packageData returning DataBlockSize:%d iUsedDataBlock:%d",
iDataBlockSize,
iUsedDataBlock);
memset(cpDataBlock + iUsedDataBlock, 0, iDataBlockSize - iUsedDataBlock);
@ -590,54 +589,55 @@ packageData(HeapTuple tTupleData, TupleDesc tTupleDesc, Oid tableOid,
PG_FUNCTION_INFO_V1(setval);
Datum setval(PG_FUNCTION_ARGS)
Datum
setval(PG_FUNCTION_ARGS)
{
text * sequenceName;
text *sequenceName;
Oid setvalArgTypes[2] = {TEXTOID,INT4OID};
int nextValue;
void * setvalPlan=NULL;
Datum setvalData[2];
const char * setvalQuery = "SELECT setval_pg($1,$2)";
int ret;
sequenceName = PG_GETARG_TEXT_P(0);
nextValue = PG_GETARG_INT32(1);
Oid setvalArgTypes[2] = {TEXTOID, INT4OID};
int nextValue;
void *setvalPlan = NULL;
Datum setvalData[2];
const char *setvalQuery = "SELECT setval_pg($1,$2)";
int ret;
setvalData[0] = PointerGetDatum(sequenceName);
setvalData[1] = Int32GetDatum(nextValue);
sequenceName = PG_GETARG_TEXT_P(0);
nextValue = PG_GETARG_INT32(1);
if (SPI_connect() < 0)
{
ereport(ERROR,(errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
errmsg("dbmirror:setval could not connect to SPI")));
return -1;
}
setvalData[0] = PointerGetDatum(sequenceName);
setvalData[1] = Int32GetDatum(nextValue);
setvalPlan = SPI_prepare(setvalQuery,2,setvalArgTypes);
if(setvalPlan == NULL)
{
ereport(ERROR,(errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
errmsg("dbmirror:setval could not prepare plan")));
return -1;
}
if (SPI_connect() < 0)
{
ereport(ERROR, (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
errmsg("dbmirror:setval could not connect to SPI")));
return -1;
}
ret = SPI_execp(setvalPlan,setvalData,NULL,1);
if(ret != SPI_OK_SELECT || SPI_processed != 1)
return -1;
setvalPlan = SPI_prepare(setvalQuery, 2, setvalArgTypes);
if (setvalPlan == NULL)
{
ereport(ERROR, (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
errmsg("dbmirror:setval could not prepare plan")));
return -1;
}
debug_msg2("dbmirror:setval: setval_pg returned ok:%d",nextValue);
ret = saveSequenceUpdate(sequenceName,nextValue);
SPI_pfree(setvalPlan);
ret = SPI_execp(setvalPlan, setvalData, NULL, 1);
SPI_finish();
debug_msg("dbmirror:setval about to return");
return Int64GetDatum(nextValue);
if (ret != SPI_OK_SELECT || SPI_processed != 1)
return -1;
debug_msg2("dbmirror:setval: setval_pg returned ok:%d", nextValue);
ret = saveSequenceUpdate(sequenceName, nextValue);
SPI_pfree(setvalPlan);
SPI_finish();
debug_msg("dbmirror:setval about to return");
return Int64GetDatum(nextValue);
}
@ -645,134 +645,131 @@ Datum setval(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(nextval);
Datum
Datum
nextval(PG_FUNCTION_ARGS)
{
text * sequenceName;
const char * nextvalQuery = "SELECT nextval_pg($1)";
Oid nextvalArgTypes[1] = {TEXTOID};
void * nextvalPlan=NULL;
Datum nextvalData[1];
int ret;
HeapTuple resTuple;
char isNull;
int nextSequenceValue;
text *sequenceName;
const char *nextvalQuery = "SELECT nextval_pg($1)";
Oid nextvalArgTypes[1] = {TEXTOID};
void *nextvalPlan = NULL;
Datum nextvalData[1];
debug_msg("dbmirror:nextval Starting pending.so:nextval");
int ret;
HeapTuple resTuple;
char isNull;
int nextSequenceValue;
sequenceName = PG_GETARG_TEXT_P(0);
if (SPI_connect() < 0)
{
ereport(ERROR,(errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
errmsg("dbmirror:nextval could not connect to SPI")));
return -1;
}
nextvalPlan = SPI_prepare(nextvalQuery,1,nextvalArgTypes);
debug_msg("prepared plan to call nextval_pg");
debug_msg("dbmirror:nextval Starting pending.so:nextval");
if(nextvalPlan==NULL)
{
ereport(ERROR,(errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
errmsg("dbmirror:nextval error creating plan")));
return -1;
}
nextvalData[0] = PointerGetDatum(sequenceName);
sequenceName = PG_GETARG_TEXT_P(0);
ret = SPI_execp(nextvalPlan,nextvalData,NULL,1);
if (SPI_connect() < 0)
{
ereport(ERROR, (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
errmsg("dbmirror:nextval could not connect to SPI")));
return -1;
}
debug_msg("dbmirror:Executed call to nextval_pg");
nextvalPlan = SPI_prepare(nextvalQuery, 1, nextvalArgTypes);
if(ret != SPI_OK_SELECT || SPI_processed != 1)
return -1;
debug_msg("prepared plan to call nextval_pg");
resTuple = SPI_tuptable->vals[0];
debug_msg("dbmirror:nextval Set resTuple");
if (nextvalPlan == NULL)
{
ereport(ERROR, (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
errmsg("dbmirror:nextval error creating plan")));
return -1;
}
nextvalData[0] = PointerGetDatum(sequenceName);
nextSequenceValue =* (unsigned int *)(DatumGetPointer(SPI_getbinval(resTuple,
SPI_tuptable->tupdesc,
1,&isNull)));
ret = SPI_execp(nextvalPlan, nextvalData, NULL, 1);
debug_msg("dbmirror:Executed call to nextval_pg");
debug_msg2("dbmirror:nextval Set SPI_getbinval:%d",nextSequenceValue);
saveSequenceUpdate(sequenceName,nextSequenceValue);
SPI_pfree(resTuple);
SPI_pfree(nextvalPlan);
if (ret != SPI_OK_SELECT || SPI_processed != 1)
return -1;
SPI_finish();
resTuple = SPI_tuptable->vals[0];
return Int64GetDatum(nextSequenceValue);
debug_msg("dbmirror:nextval Set resTuple");
nextSequenceValue = *(unsigned int *) (DatumGetPointer(SPI_getbinval(resTuple,
SPI_tuptable->tupdesc,
1, &isNull)));
debug_msg2("dbmirror:nextval Set SPI_getbinval:%d", nextSequenceValue);
saveSequenceUpdate(sequenceName, nextSequenceValue);
SPI_pfree(resTuple);
SPI_pfree(nextvalPlan);
SPI_finish();
return Int64GetDatum(nextSequenceValue);
}
int
saveSequenceUpdate(const text * sequenceName,
int nextSequenceVal)
saveSequenceUpdate(const text *sequenceName,
int nextSequenceVal)
{
Oid insertArgTypes[2] = {TEXTOID,INT4OID};
Oid insertDataArgTypes[1] = {NAMEOID};
void * insertPlan=NULL;
void * insertDataPlan=NULL;
Datum insertDatum[2];
Datum insertDataDatum[1];
char nextSequenceText[32];
Oid insertArgTypes[2] = {TEXTOID, INT4OID};
Oid insertDataArgTypes[1] = {NAMEOID};
void *insertPlan = NULL;
void *insertDataPlan = NULL;
Datum insertDatum[2];
Datum insertDataDatum[1];
char nextSequenceText[32];
const char * insertQuery =
"INSERT INTO dbmirror_Pending (TableName,Op,XID) VALUES" \
"($1,'s',$2)";
const char * insertDataQuery =
"INSERT INTO dbmirror_PendingData(SeqId,IsKey,Data) VALUES " \
"(currval('dbmirror_pending_seqid_seq'),'t',$1)";
int ret;
const char *insertQuery =
"INSERT INTO dbmirror_Pending (TableName,Op,XID) VALUES" \
"($1,'s',$2)";
const char *insertDataQuery =
"INSERT INTO dbmirror_PendingData(SeqId,IsKey,Data) VALUES " \
"(currval('dbmirror_pending_seqid_seq'),'t',$1)";
int ret;
insertPlan = SPI_prepare(insertQuery,2,insertArgTypes);
insertDataPlan = SPI_prepare(insertDataQuery,1,insertDataArgTypes);
insertPlan = SPI_prepare(insertQuery, 2, insertArgTypes);
insertDataPlan = SPI_prepare(insertDataQuery, 1, insertDataArgTypes);
debug_msg("Prepared insert query");
debug_msg("Prepared insert query");
if(insertPlan == NULL || insertDataPlan == NULL)
{
ereport(ERROR,(errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),errmsg("dbmirror:nextval error creating plan")));
}
if (insertPlan == NULL || insertDataPlan == NULL)
ereport(ERROR, (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION), errmsg("dbmirror:nextval error creating plan")));
insertDatum[1] = Int32GetDatum(GetCurrentTransactionId());
insertDatum[0] = PointerGetDatum(sequenceName);
insertDatum[1] = Int32GetDatum(GetCurrentTransactionId());
insertDatum[0] = PointerGetDatum(sequenceName);
sprintf(nextSequenceText,"%d",nextSequenceVal);
insertDataDatum[0] = PointerGetDatum(nextSequenceText);
debug_msg2("dbmirror:savesequenceupdate: Setting value %s",
nextSequenceText);
sprintf(nextSequenceText, "%d", nextSequenceVal);
insertDataDatum[0] = PointerGetDatum(nextSequenceText);
debug_msg2("dbmirror:savesequenceupdate: Setting value %s",
nextSequenceText);
debug_msg("dbmirror:About to execute insert query");
debug_msg("dbmirror:About to execute insert query");
ret = SPI_execp(insertPlan,insertDatum,NULL,1);
ret = SPI_execp(insertDataPlan,insertDataDatum,NULL,1);
ret = SPI_execp(insertPlan, insertDatum, NULL, 1);
debug_msg("dbmirror:Insert query finished");
SPI_pfree(insertPlan);
SPI_pfree(insertDataPlan);
return ret;
ret = SPI_execp(insertDataPlan, insertDataDatum, NULL, 1);
debug_msg("dbmirror:Insert query finished");
SPI_pfree(insertPlan);
SPI_pfree(insertDataPlan);
return ret;
}

View File

@ -16,7 +16,7 @@
static int64
get_tablespace_size(Oid dbid, Oid spcid, bool baddirOK);
get_tablespace_size(Oid dbid, Oid spcid, bool baddirOK);
static char *
psnprintf(size_t len, const char *fmt,...)
@ -50,10 +50,11 @@ database_size(PG_FUNCTION_ARGS)
Oid dbid;
int64 totalsize;
#ifdef SYMLINK
Relation dbrel;
HeapScanDesc scan;
HeapTuple tuple;
Relation dbrel;
HeapScanDesc scan;
HeapTuple tuple;
#endif
dbid = get_database_oid(NameStr(*dbname));
@ -62,17 +63,18 @@ database_size(PG_FUNCTION_ARGS)
(errcode(ERRCODE_UNDEFINED_DATABASE),
errmsg("database \"%s\" does not exist", NameStr(*dbname))));
#ifdef SYMLINK
#ifdef SYMLINK
dbrel = heap_openr(TableSpaceRelationName, AccessShareLock);
dbrel = heap_openr(TableSpaceRelationName, AccessShareLock);
scan = heap_beginscan(dbrel, SnapshotNow, 0, (ScanKey) NULL);
totalsize = 0;
while((tuple = heap_getnext(scan, ForwardScanDirection)))
while ((tuple = heap_getnext(scan, ForwardScanDirection)))
{
Oid spcid = HeapTupleGetOid(tuple);
if(spcid != GLOBALTABLESPACE_OID)
Oid spcid = HeapTupleGetOid(tuple);
if (spcid != GLOBALTABLESPACE_OID)
totalsize += get_tablespace_size(dbid, spcid, true);
}
heap_endscan(scan);
@ -94,8 +96,8 @@ database_size(PG_FUNCTION_ARGS)
static int64
get_tablespace_size(Oid dbid, Oid spcid, bool baddirOK)
{
char *dbpath;
DIR *dirdesc;
char *dbpath;
DIR *dirdesc;
struct dirent *direntry;
int64 totalsize;
@ -104,11 +106,11 @@ get_tablespace_size(Oid dbid, Oid spcid, bool baddirOK)
dirdesc = AllocateDir(dbpath);
if (!dirdesc)
{
if(baddirOK)
if (baddirOK)
return 0;
else
ereport(ERROR,
(errcode_for_file_access(),
(errcode_for_file_access(),
errmsg("could not open directory \"%s\": %m", dbpath)));
}
totalsize = 0;

View File

@ -1,36 +1,36 @@
/*
* This is a port of the Double Metaphone algorithm for use in PostgreSQL.
*
*
* Double Metaphone computes 2 "sounds like" strings - a primary and an
* alternate. In most cases they are the same, but for foreign names
* especially they can be a bit different, depending on pronunciation.
*
* Information on using Double Metaphone can be found at
* http://www.codeproject.com/useritems/dmetaphone1.asp
* http://www.codeproject.com/useritems/dmetaphone1.asp
* and the original article describing it can be found at
* http://www.cuj.com/documents/s=8038/cuj0006philips/
* http://www.cuj.com/documents/s=8038/cuj0006philips/
*
* For PostgrSQL we provide 2 functions - one for the primary and one for
* the alternate. That way the functions are pure text->text mappings that
* are useful in functional indexes. These are 'dmetaphone' for the
* primary and 'dmetaphone_alt' for the alternate.
*
* Assuming that dmetaphone.so is in $libdir, the SQL to set up the
* Assuming that dmetaphone.so is in $libdir, the SQL to set up the
* functions looks like this:
*
* CREATE FUNCTION dmetaphone (text) RETURNS text
* LANGUAGE C IMMUTABLE STRICT
* AS '$libdir/dmetaphone', 'dmetaphone';
* CREATE FUNCTION dmetaphone (text) RETURNS text
* LANGUAGE C IMMUTABLE STRICT
* AS '$libdir/dmetaphone', 'dmetaphone';
*
* CREATE FUNCTION dmetaphone_alt (text) RETURNS text
* LANGUAGE C IMMUTABLE STRICT
* AS '$libdir/dmetaphone', 'dmetaphone_alt';
* CREATE FUNCTION dmetaphone_alt (text) RETURNS text
* LANGUAGE C IMMUTABLE STRICT
* AS '$libdir/dmetaphone', 'dmetaphone_alt';
*
* Note that you have to declare the functions IMMUTABLE if you want to
* use them in functional indexes, and you have to declare them as STRICT
* as they do not check for NULL input, and will segfault if given NULL input.
* (See below for alternative ) Declaring them as STRICT means PostgreSQL
* will never call them with NULL, but instead assume the result is NULL,
* as they do not check for NULL input, and will segfault if given NULL input.
* (See below for alternative ) Declaring them as STRICT means PostgreSQL
* will never call them with NULL, but instead assume the result is NULL,
* which is what we (I) want.
*
* Alternatively, compile with -DDMETAPHONE_NOSTRICT and the functions
@ -42,25 +42,25 @@
* need. That's the way the perl module was written, because perl can handle
* a list return more easily than we can in PostgreSQL. The result has been
* fast enough for my needs, but it could maybe be optimized a bit to remove
* that behaviour.
* that behaviour.
*
*/
/*
* $Revision: 1.2 $
* $Id: dmetaphone.c,v 1.2 2004/08/20 19:48:14 momjian Exp $
* $Revision: 1.3 $
* $Id: dmetaphone.c,v 1.3 2004/08/29 05:06:35 momjian Exp $
*/
/***************************** COPYRIGHT NOTICES ***********************
Most of this code is directly from the Text::DoubleMetaphone perl module
version 0.05 available from http://www.cpan.org.
version 0.05 available from http://www.cpan.org.
It bears this copyright notice:
Copyright 2000, Maurice Aubrey <maurice@hevanet.com>.
Copyright 2000, Maurice Aubrey <maurice@hevanet.com>.
All rights reserved.
This code is based heavily on the C++ implementation by
@ -73,7 +73,7 @@ It bears this copyright notice:
The remaining code is authored by Andrew Dunstan <amdunstan@ncshp.org> and
<andrew@dunslane.net> and is covered this copyright:
Copyright 2003, North Carolina State Highway Patrol.
Copyright 2003, North Carolina State Highway Patrol.
All rights reserved.
Permission to use, copy, modify, and distribute this software and its
@ -81,14 +81,14 @@ The remaining code is authored by Andrew Dunstan <amdunstan@ncshp.org> and
is hereby granted, provided that the above copyright notice and this
paragraph and the following two paragraphs appear in all copies.
IN NO EVENT SHALL THE NORTH CAROLINA STATE HIGHWAY PATROL BE LIABLE TO ANY
PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
IN NO EVENT SHALL THE NORTH CAROLINA STATE HIGHWAY PATROL BE LIABLE TO ANY
PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
DOCUMENTATION, EVEN IF THE NORTH CAROLINA STATE HIGHWAY PATROL HAS BEEN
DOCUMENTATION, EVEN IF THE NORTH CAROLINA STATE HIGHWAY PATROL HAS BEEN
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THE NORTH CAROLINA STATE HIGHWAY PATROL SPECIFICALLY DISCLAIMS ANY
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
THE NORTH CAROLINA STATE HIGHWAY PATROL SPECIFICALLY DISCLAIMS ANY
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED
HEREUNDER IS ON AN "AS IS" BASIS, AND THE NORTH CAROLINA STATE HIGHWAY PATROL
HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
@ -109,7 +109,6 @@ The remaining code is authored by Andrew Dunstan <amdunstan@ncshp.org> and
/* turn off assertions for embedded function */
#define NDEBUG
#endif
#include <stdio.h>
@ -124,7 +123,7 @@ extern Datum dmetaphone_alt(PG_FUNCTION_ARGS);
/* prototype for the main function we got from the perl module */
static void
DoubleMetaphone(char *, char **);
DoubleMetaphone(char *, char **);
#ifndef DMETAPHONE_MAIN
@ -138,37 +137,41 @@ PG_FUNCTION_INFO_V1(dmetaphone);
Datum
dmetaphone(PG_FUNCTION_ARGS)
{
text * arg, * result;
int alen, rsize;
char * aptr, *codes[2], * code, * rptr;
text *arg,
*result;
int alen,
rsize;
char *aptr,
*codes[2],
*code,
*rptr;
#ifdef DMETAPHONE_NOSTRICT
if (PG_ARGISNULL(0))
PG_RETURNNULL();
#endif
arg = PG_GETARG_TEXT_P(0);
alen = VARSIZE(arg)-VARHDRSZ;
alen = VARSIZE(arg) - VARHDRSZ;
/*
* Postgres' string values might not have trailing nuls.
* The VARSIZE will not include the nul in any case
* so we copy things out and add a trailing nul.
* When we copy back we ignore the nul
* (and we don't make space for it).
/*
* Postgres' string values might not have trailing nuls. The VARSIZE
* will not include the nul in any case so we copy things out and add
* a trailing nul. When we copy back we ignore the nul (and we don't
* make space for it).
*/
aptr = palloc(alen+1);
memcpy(aptr,VARDATA(arg),alen);
aptr[alen]=0;
DoubleMetaphone(aptr,codes);
aptr = palloc(alen + 1);
memcpy(aptr, VARDATA(arg), alen);
aptr[alen] = 0;
DoubleMetaphone(aptr, codes);
code = codes[0];
if (!code)
code = "";
rsize = VARHDRSZ + strlen(code) ;
rsize = VARHDRSZ + strlen(code);
result = (text *) palloc(rsize);
memset(result,0,rsize);
memset(result, 0, rsize);
rptr = VARDATA(result);
memcpy(rptr,code,strlen(code));
memcpy(rptr, code, strlen(code));
VARATT_SIZEP(result) = rsize;
PG_RETURN_TEXT_P(result);
}
@ -183,28 +186,33 @@ PG_FUNCTION_INFO_V1(dmetaphone_alt);
Datum
dmetaphone_alt(PG_FUNCTION_ARGS)
{
text * arg, * result;
int alen, rsize;
char * aptr, * codes[2], * code, * rptr;
text *arg,
*result;
int alen,
rsize;
char *aptr,
*codes[2],
*code,
*rptr;
#ifdef DMETAPHONE_NOSTRICT
if (PG_ARGISNULL(0))
PG_RETURNNULL();
#endif
arg = PG_GETARG_TEXT_P(0);
alen = VARSIZE(arg)-VARHDRSZ;
aptr = palloc(alen+1);
memcpy(aptr,VARDATA(arg),alen);
aptr[alen]=0;
DoubleMetaphone(aptr,codes);
alen = VARSIZE(arg) - VARHDRSZ;
aptr = palloc(alen + 1);
memcpy(aptr, VARDATA(arg), alen);
aptr[alen] = 0;
DoubleMetaphone(aptr, codes);
code = codes[1];
if (!code)
code = "";
rsize = VARHDRSZ + strlen(code) ;
rsize = VARHDRSZ + strlen(code);
result = (text *) palloc(rsize);
memset(result,0,rsize);
memset(result, 0, rsize);
rptr = VARDATA(result);
memcpy(rptr,code,strlen(code));
memcpy(rptr, code, strlen(code));
VARATT_SIZEP(result) = rsize;
PG_RETURN_TEXT_P(result);
}
@ -215,141 +223,139 @@ dmetaphone_alt(PG_FUNCTION_ARGS)
/* all memory handling is done with these macros */
#define META_MALLOC(v,n,t) \
(v = (t*)palloc(((n)*sizeof(t))))
(v = (t*)palloc(((n)*sizeof(t))))
#define META_REALLOC(v,n,t) \
(v = (t*)repalloc((v),((n)*sizeof(t))))
(v = (t*)repalloc((v),((n)*sizeof(t))))
/*
/*
* Don't do pfree - it seems to cause a segv sometimes - which might have just
* been caused by reloading the module in development.
* So we rely on context cleanup - Tom Lane says pfree shouldn't be necessary
* in a case like this.
*/
#define META_FREE(x) /* pfree((x)) */
#define META_FREE(x) /* pfree((x)) */
#else /* not defined DMETAPHONE_MAIN */
#else /* not defined DMETAPHONE_MAIN */
/* use the standard malloc library when not running in PostgreSQL */
#define META_MALLOC(v,n,t) \
(v = (t*)malloc(((n)*sizeof(t))))
(v = (t*)malloc(((n)*sizeof(t))))
#define META_REALLOC(v,n,t) \
(v = (t*)realloc((v),((n)*sizeof(t))))
(v = (t*)realloc((v),((n)*sizeof(t))))
#define META_FREE(x) free((x))
#endif /* defined DMETAPHONE_MAIN */
#endif /* defined DMETAPHONE_MAIN */
/* this typedef was orignally in the perl module's .h file */
/* this typedef was orignally in the perl module's .h file */
typedef struct
{
char *str;
int length;
int bufsize;
int free_string_on_destroy;
char *str;
int length;
int bufsize;
int free_string_on_destroy;
}
metastring;
/*
metastring;
/*
* remaining perl module funcs unchanged except for declaring them static
* and reformatting to PostgreSQL indentation and to fit in 80 cols.
*
*/
*/
static metastring *
NewMetaString(char *init_str)
{
metastring *s;
char empty_string[] = "";
metastring *s;
char empty_string[] = "";
META_MALLOC(s, 1, metastring);
assert( s != NULL );
META_MALLOC(s, 1, metastring);
assert(s != NULL);
if (init_str == NULL)
if (init_str == NULL)
init_str = empty_string;
s->length = strlen(init_str);
/* preallocate a bit more for potential growth */
s->bufsize = s->length + 7;
s->length = strlen(init_str);
/* preallocate a bit more for potential growth */
s->bufsize = s->length + 7;
META_MALLOC(s->str, s->bufsize, char);
assert( s->str != NULL );
strncpy(s->str, init_str, s->length + 1);
s->free_string_on_destroy = 1;
META_MALLOC(s->str, s->bufsize, char);
assert(s->str != NULL);
return s;
strncpy(s->str, init_str, s->length + 1);
s->free_string_on_destroy = 1;
return s;
}
static void
DestroyMetaString(metastring * s)
{
if (s == NULL)
if (s == NULL)
return;
if (s->free_string_on_destroy && (s->str != NULL))
if (s->free_string_on_destroy && (s->str != NULL))
META_FREE(s->str);
META_FREE(s);
META_FREE(s);
}
static void
IncreaseBuffer(metastring * s, int chars_needed)
{
META_REALLOC(s->str, (s->bufsize + chars_needed + 10), char);
assert( s->str != NULL );
s->bufsize = s->bufsize + chars_needed + 10;
META_REALLOC(s->str, (s->bufsize + chars_needed + 10), char);
assert(s->str != NULL);
s->bufsize = s->bufsize + chars_needed + 10;
}
static void
MakeUpper(metastring * s)
{
char *i;
char *i;
for (i = s->str; *i; i++)
{
for (i = s->str; *i; i++)
*i = toupper(*i);
}
}
static int
IsVowel(metastring * s, int pos)
{
char c;
char c;
if ((pos < 0) || (pos >= s->length))
if ((pos < 0) || (pos >= s->length))
return 0;
c = *(s->str + pos);
if ((c == 'A') || (c == 'E') || (c == 'I') || (c =='O') ||
(c =='U') || (c == 'Y'))
c = *(s->str + pos);
if ((c == 'A') || (c == 'E') || (c == 'I') || (c == 'O') ||
(c == 'U') || (c == 'Y'))
return 1;
return 0;
return 0;
}
static int
SlavoGermanic(metastring * s)
{
if ((char *) strstr(s->str, "W"))
if ((char *) strstr(s->str, "W"))
return 1;
else if ((char *) strstr(s->str, "K"))
else if ((char *) strstr(s->str, "K"))
return 1;
else if ((char *) strstr(s->str, "CZ"))
else if ((char *) strstr(s->str, "CZ"))
return 1;
else if ((char *) strstr(s->str, "WITZ"))
else if ((char *) strstr(s->str, "WITZ"))
return 1;
else
else
return 0;
}
@ -357,117 +363,115 @@ SlavoGermanic(metastring * s)
static char
GetAt(metastring * s, int pos)
{
if ((pos < 0) || (pos >= s->length))
if ((pos < 0) || (pos >= s->length))
return '\0';
return ((char) *(s->str + pos));
return ((char) *(s->str + pos));
}
static void
SetAt(metastring * s, int pos, char c)
{
if ((pos < 0) || (pos >= s->length))
if ((pos < 0) || (pos >= s->length))
return;
*(s->str + pos) = c;
*(s->str + pos) = c;
}
/*
/*
Caveats: the START value is 0 based
*/
static int
StringAt(metastring * s, int start, int length, ...)
StringAt(metastring * s, int start, int length,...)
{
char *test;
char *pos;
va_list ap;
char *test;
char *pos;
va_list ap;
if ((start < 0) || (start >= s->length))
return 0;
if ((start < 0) || (start >= s->length))
return 0;
pos = (s->str + start);
va_start(ap, length);
pos = (s->str + start);
va_start(ap, length);
do
do
{
test = va_arg(ap, char *);
if (*test && (strncmp(pos, test, length) == 0))
return 1;
}
while (strcmp(test, ""));
while (strcmp(test, ""));
va_end(ap);
va_end(ap);
return 0;
return 0;
}
static void
MetaphAdd(metastring * s, char *new_str)
{
int add_length;
int add_length;
if (new_str == NULL)
if (new_str == NULL)
return;
add_length = strlen(new_str);
if ((s->length + add_length) > (s->bufsize - 1))
{
add_length = strlen(new_str);
if ((s->length + add_length) > (s->bufsize - 1))
IncreaseBuffer(s, add_length);
}
strcat(s->str, new_str);
s->length += add_length;
strcat(s->str, new_str);
s->length += add_length;
}
static void
DoubleMetaphone(char *str, char **codes)
{
int length;
metastring *original;
metastring *primary;
metastring *secondary;
int current;
int last;
int length;
metastring *original;
metastring *primary;
metastring *secondary;
int current;
int last;
current = 0;
/* we need the real length and last prior to padding */
length = strlen(str);
last = length - 1;
original = NewMetaString(str);
/* Pad original so we can index beyond end */
MetaphAdd(original, " ");
current = 0;
/* we need the real length and last prior to padding */
length = strlen(str);
last = length - 1;
original = NewMetaString(str);
/* Pad original so we can index beyond end */
MetaphAdd(original, " ");
primary = NewMetaString("");
secondary = NewMetaString("");
primary->free_string_on_destroy = 0;
secondary->free_string_on_destroy = 0;
primary = NewMetaString("");
secondary = NewMetaString("");
primary->free_string_on_destroy = 0;
secondary->free_string_on_destroy = 0;
MakeUpper(original);
MakeUpper(original);
/* skip these when at start of word */
if (StringAt(original, 0, 2, "GN", "KN", "PN", "WR", "PS", ""))
/* skip these when at start of word */
if (StringAt(original, 0, 2, "GN", "KN", "PN", "WR", "PS", ""))
current += 1;
/* Initial 'X' is pronounced 'Z' e.g. 'Xavier' */
if (GetAt(original, 0) == 'X')
/* Initial 'X' is pronounced 'Z' e.g. 'Xavier' */
if (GetAt(original, 0) == 'X')
{
MetaphAdd(primary, "S"); /* 'Z' maps to 'S' */
MetaphAdd(secondary, "S");
current += 1;
}
/* main loop */
while ((primary->length < 4) || (secondary->length < 4))
/* main loop */
while ((primary->length < 4) || (secondary->length < 4))
{
if (current >= length)
break;
switch (GetAt(original, current))
{
{
case 'A':
case 'E':
case 'I':
@ -508,8 +512,8 @@ DoubleMetaphone(char *str, char **codes)
&& StringAt(original, (current - 1), 3, "ACH", "")
&& ((GetAt(original, current + 2) != 'I')
&& ((GetAt(original, current + 2) != 'E')
|| StringAt(original, (current - 2), 6, "BACHER",
"MACHER", ""))))
|| StringAt(original, (current - 2), 6, "BACHER",
"MACHER", ""))))
{
MetaphAdd(primary, "K");
MetaphAdd(secondary, "K");
@ -550,7 +554,7 @@ DoubleMetaphone(char *str, char **codes)
/* greek roots e.g. 'chemistry', 'chorus' */
if ((current == 0)
&& (StringAt(original, (current + 1), 5,
&& (StringAt(original, (current + 1), 5,
"HARAC", "HARIS", "")
|| StringAt(original, (current + 1), 3, "HOR",
"HYM", "HIA", "HEM", ""))
@ -566,19 +570,21 @@ DoubleMetaphone(char *str, char **codes)
if (
(StringAt(original, 0, 4, "VAN ", "VON ", "")
|| StringAt(original, 0, 3, "SCH", ""))
/* 'architect but not 'arch', 'orchestra', 'orchid' */
/* 'architect but not 'arch', 'orchestra', 'orchid' */
|| StringAt(original, (current - 2), 6, "ORCHES",
"ARCHIT", "ORCHID", "")
|| StringAt(original, (current + 2), 1, "T", "S",
"")
|| ((StringAt(original, (current - 1), 1,
"A", "O", "U", "E", "")
|| ((StringAt(original, (current - 1), 1,
"A", "O", "U", "E", "")
|| (current == 0))
/* e.g., 'wachtler', 'wechsler',
but not 'tichner' */
&& StringAt(original, (current + 2), 1, "L", "R",
"N", "M", "B", "H", "F", "V", "W",
" ", "")))
/*
* e.g., 'wachtler', 'wechsler', but not 'tichner'
*/
&& StringAt(original, (current + 2), 1, "L", "R",
"N", "M", "B", "H", "F", "V", "W",
" ", "")))
{
MetaphAdd(primary, "K");
MetaphAdd(secondary, "K");
@ -633,14 +639,14 @@ DoubleMetaphone(char *str, char **codes)
{
/* 'bellocchio' but not 'bacchus' */
if (StringAt(original, (current + 2), 1, "I", "E", "H", "")
&& !StringAt(original, (current + 2), 2, "HU", ""))
&& !StringAt(original, (current + 2), 2, "HU", ""))
{
/* 'accident', 'accede' 'succeed' */
if (
((current == 1)
&& (GetAt(original, current - 1) == 'A'))
|| StringAt(original, (current - 1), 5, "UCCEE",
"UCCES", ""))
|| StringAt(original, (current - 1), 5, "UCCEE",
"UCCES", ""))
{
MetaphAdd(primary, "KS");
MetaphAdd(secondary, "KS");
@ -655,7 +661,7 @@ DoubleMetaphone(char *str, char **codes)
break;
}
else
{ /* Pierce's rule */
{ /* Pierce's rule */
MetaphAdd(primary, "K");
MetaphAdd(secondary, "K");
current += 2;
@ -696,19 +702,18 @@ DoubleMetaphone(char *str, char **codes)
/* name sent in 'mac caffrey', 'mac gregor */
if (StringAt(original, (current + 1), 2, " C", " Q", " G", ""))
current += 3;
else if (StringAt(original, (current + 1), 1, "C", "K", "Q", "")
&& !StringAt(original, (current + 1), 2,
"CE", "CI", ""))
current += 2;
else
if (StringAt(original, (current + 1), 1, "C", "K", "Q", "")
&& !StringAt(original, (current + 1), 2,
"CE", "CI", ""))
current += 2;
else
current += 1;
current += 1;
break;
case 'D':
if (StringAt(original, current, 2, "DG", ""))
{
if (StringAt(original, (current + 2), 1,
if (StringAt(original, (current + 2), 1,
"I", "E", "Y", ""))
{
/* e.g. 'edge' */
@ -780,19 +785,22 @@ DoubleMetaphone(char *str, char **codes)
break;
}
}
/* Parker's rule (with some further refinements) -
e.g., 'hugh' */
/*
* Parker's rule (with some further refinements) -
* e.g., 'hugh'
*/
if (
((current > 1)
&& StringAt(original, (current - 2), 1,
&& StringAt(original, (current - 2), 1,
"B", "H", "D", ""))
/* e.g., 'bough' */
/* e.g., 'bough' */
|| ((current > 2)
&& StringAt(original, (current - 3), 1,
&& StringAt(original, (current - 3), 1,
"B", "H", "D", ""))
/* e.g., 'broughton' */
/* e.g., 'broughton' */
|| ((current > 3)
&& StringAt(original, (current - 4), 1,
&& StringAt(original, (current - 4), 1,
"B", "H", "")))
{
current += 2;
@ -800,8 +808,10 @@ DoubleMetaphone(char *str, char **codes)
}
else
{
/* e.g., 'laugh', 'McLaughlin', 'cough',
'gough', 'rough', 'tough' */
/*
* e.g., 'laugh', 'McLaughlin', 'cough', 'gough',
* 'rough', 'tough'
*/
if ((current > 2)
&& (GetAt(original, current - 1) == 'U')
&& StringAt(original, (current - 3), 1, "C",
@ -837,15 +847,15 @@ DoubleMetaphone(char *str, char **codes)
if (!StringAt(original, (current + 2), 2, "EY", "")
&& (GetAt(original, current + 1) != 'Y')
&& !SlavoGermanic(original))
{
MetaphAdd(primary, "N");
MetaphAdd(secondary, "KN");
}
else
{
MetaphAdd(primary, "KN");
MetaphAdd(secondary, "KN");
}
{
MetaphAdd(primary, "N");
MetaphAdd(secondary, "KN");
}
else
{
MetaphAdd(primary, "KN");
MetaphAdd(secondary, "KN");
}
current += 2;
break;
}
@ -863,9 +873,9 @@ DoubleMetaphone(char *str, char **codes)
/* -ges-,-gep-,-gel-, -gie- at beginning */
if ((current == 0)
&& ((GetAt(original, current + 1) == 'Y')
|| StringAt(original, (current + 1), 2, "ES", "EP",
"EB", "EL", "EY", "IB", "IL", "IN", "IE",
"EI", "ER", "")))
|| StringAt(original, (current + 1), 2, "ES", "EP",
"EB", "EL", "EY", "IB", "IL", "IN", "IE",
"EI", "ER", "")))
{
MetaphAdd(primary, "K");
MetaphAdd(secondary, "J");
@ -873,11 +883,11 @@ DoubleMetaphone(char *str, char **codes)
break;
}
/* -ger-, -gy- */
/* -ger-, -gy- */
if (
(StringAt(original, (current + 1), 2, "ER", "")
|| (GetAt(original, current + 1) == 'Y'))
&& !StringAt(original, 0, 6,
&& !StringAt(original, 0, 6,
"DANGER", "RANGER", "MANGER", "")
&& !StringAt(original, (current - 1), 1, "E", "I", "")
&& !StringAt(original, (current - 1), 3, "RGY", "OGY",
@ -889,9 +899,9 @@ DoubleMetaphone(char *str, char **codes)
break;
}
/* italian e.g, 'biaggi' */
/* italian e.g, 'biaggi' */
if (StringAt(original, (current + 1), 1, "E", "I", "Y", "")
|| StringAt(original, (current - 1), 4,
|| StringAt(original, (current - 1), 4,
"AGGI", "OGGI", ""))
{
/* obvious germanic */
@ -939,7 +949,8 @@ DoubleMetaphone(char *str, char **codes)
MetaphAdd(secondary, "H");
current += 2;
}
else /* also takes care of 'HH' */
else
/* also takes care of 'HH' */
current += 1;
break;
@ -991,9 +1002,9 @@ DoubleMetaphone(char *str, char **codes)
else
{
if (!StringAt(original, (current + 1), 1, "L", "T",
"K", "S", "N", "M", "B", "Z", "")
"K", "S", "N", "M", "B", "Z", "")
&& !StringAt(original, (current - 1), 1,
"S", "K", "L", ""))
"S", "K", "L", ""))
{
MetaphAdd(primary, "J");
MetaphAdd(secondary, "J");
@ -1002,7 +1013,7 @@ DoubleMetaphone(char *str, char **codes)
}
}
if (GetAt(original, current + 1) == 'J') /* it could happen! */
if (GetAt(original, current + 1) == 'J') /* it could happen! */
current += 2;
else
current += 1;
@ -1024,10 +1035,10 @@ DoubleMetaphone(char *str, char **codes)
if (((current == (length - 3))
&& StringAt(original, (current - 1), 4, "ILLO",
"ILLA", "ALLE", ""))
|| ((StringAt(original, (last - 1), 2, "AS", "OS", "")
|| StringAt(original, last, 1, "A", "O", ""))
&& StringAt(original, (current - 1), 4,
"ALLE", "")))
|| ((StringAt(original, (last - 1), 2, "AS", "OS", "")
|| StringAt(original, last, 1, "A", "O", ""))
&& StringAt(original, (current - 1), 4,
"ALLE", "")))
{
MetaphAdd(primary, "L");
MetaphAdd(secondary, "");
@ -1045,8 +1056,8 @@ DoubleMetaphone(char *str, char **codes)
case 'M':
if ((StringAt(original, (current - 1), 3, "UMB", "")
&& (((current + 1) == last)
|| StringAt(original, (current + 2), 2, "ER", "")))
/* 'dumb','thumb' */
|| StringAt(original, (current + 2), 2, "ER", "")))
/* 'dumb','thumb' */
|| (GetAt(original, current + 1) == 'M'))
current += 2;
else
@ -1102,7 +1113,7 @@ DoubleMetaphone(char *str, char **codes)
if ((current == last)
&& !SlavoGermanic(original)
&& StringAt(original, (current - 2), 2, "IE", "")
&& !StringAt(original, (current - 4), 2, "ME", "MA", ""))
&& !StringAt(original, (current - 4), 2, "ME", "MA", ""))
{
MetaphAdd(primary, "");
MetaphAdd(secondary, "R");
@ -1141,8 +1152,8 @@ DoubleMetaphone(char *str, char **codes)
{
/* germanic */
if (StringAt
(original, (current + 1), 4, "HEIM", "HOEK", "HOLM",
"HOLZ", ""))
(original, (current + 1), 4, "HEIM", "HOEK", "HOLM",
"HOLZ", ""))
{
MetaphAdd(primary, "S");
MetaphAdd(secondary, "S");
@ -1174,12 +1185,13 @@ DoubleMetaphone(char *str, char **codes)
break;
}
/* german & anglicisations, e.g. 'smith' match 'schmidt',
'snider' match 'schneider'
also, -sz- in slavic language altho in hungarian it is
pronounced 's' */
/*
* german & anglicisations, e.g. 'smith' match 'schmidt',
* 'snider' match 'schneider' also, -sz- in slavic
* language altho in hungarian it is pronounced 's'
*/
if (((current == 0)
&& StringAt(original, (current + 1), 1,
&& StringAt(original, (current + 1), 1,
"M", "N", "L", "W", ""))
|| StringAt(original, (current + 1), 1, "Z", ""))
{
@ -1198,12 +1210,12 @@ DoubleMetaphone(char *str, char **codes)
if (GetAt(original, current + 2) == 'H')
{
/* dutch origin, e.g. 'school', 'schooner' */
if (StringAt(original, (current + 3), 2,
if (StringAt(original, (current + 3), 2,
"OO", "ER", "EN",
"UY", "ED", "EM", ""))
{
/* 'schermerhorn', 'schenker' */
if (StringAt(original, (current + 3), 2,
if (StringAt(original, (current + 3), 2,
"ER", "EN", ""))
{
MetaphAdd(primary, "X");
@ -1235,7 +1247,7 @@ DoubleMetaphone(char *str, char **codes)
}
}
if (StringAt(original, (current + 2), 1,
if (StringAt(original, (current + 2), 1,
"I", "E", "Y", ""))
{
MetaphAdd(primary, "S");
@ -1252,7 +1264,7 @@ DoubleMetaphone(char *str, char **codes)
/* french e.g. 'resnais', 'artois' */
if ((current == last)
&& StringAt(original, (current - 2), 2, "AI", "OI", ""))
&& StringAt(original, (current - 2), 2, "AI", "OI", ""))
{
MetaphAdd(primary, "");
MetaphAdd(secondary, "S");
@ -1353,8 +1365,8 @@ DoubleMetaphone(char *str, char **codes)
/* Arnow should match Arnoff */
if (((current == last) && IsVowel(original, current - 1))
|| StringAt(original, (current - 1), 5, "EWSKI", "EWSKY",
"OWSKI", "OWSKY", "")
|| StringAt(original, (current - 1), 5, "EWSKI", "EWSKY",
"OWSKI", "OWSKY", "")
|| StringAt(original, 0, 3, "SCH", ""))
{
MetaphAdd(primary, "");
@ -1379,15 +1391,15 @@ DoubleMetaphone(char *str, char **codes)
case 'X':
/* french e.g. breaux */
if (!((current == last)
&& (StringAt(original, (current - 3), 3,
&& (StringAt(original, (current - 3), 3,
"IAU", "EAU", "")
|| StringAt(original, (current - 2), 2,
|| StringAt(original, (current - 2), 2,
"AU", "OU", ""))))
{
MetaphAdd(primary, "KS");
MetaphAdd(secondary, "KS");
}
if (StringAt(original, (current + 1), 1, "C", "X", ""))
current += 2;
@ -1404,7 +1416,7 @@ DoubleMetaphone(char *str, char **codes)
current += 2;
break;
}
else if (StringAt(original, (current + 1), 2,
else if (StringAt(original, (current + 1), 2,
"ZO", "ZI", "ZA", "")
|| (SlavoGermanic(original)
&& ((current > 0)
@ -1427,38 +1439,42 @@ DoubleMetaphone(char *str, char **codes)
default:
current += 1;
}
/* printf("PRIMARY: %s\n", primary->str);
printf("SECONDARY: %s\n", secondary->str); */
}
/*
* printf("PRIMARY: %s\n", primary->str); printf("SECONDARY:
* %s\n", secondary->str);
*/
}
if (primary->length > 4)
if (primary->length > 4)
SetAt(primary, 4, '\0');
if (secondary->length > 4)
if (secondary->length > 4)
SetAt(secondary, 4, '\0');
*codes = primary->str;
*++codes = secondary->str;
*codes = primary->str;
*++codes = secondary->str;
DestroyMetaString(original);
DestroyMetaString(primary);
DestroyMetaString(secondary);
DestroyMetaString(original);
DestroyMetaString(primary);
DestroyMetaString(secondary);
}
#ifdef DMETAPHONE_MAIN
/* just for testing - not part of the perl code */
main(int argc, char ** argv)
main(int argc, char **argv)
{
char * codes[2];
char *codes[2];
if (argc > 1)
{
DoubleMetaphone(argv[1],codes);
printf("%s|%s\n",codes[0],codes[1]);
}
DoubleMetaphone(argv[1], codes);
printf("%s|%s\n", codes[0], codes[1]);
}
}
#endif

View File

@ -87,7 +87,7 @@ g_int_consistent(PG_FUNCTION_ARGS)
Datum
g_int_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
int *size = (int *) PG_GETARG_POINTER(1);
int4 i;
ArrayType *res;
@ -317,7 +317,7 @@ comparecost(const void *a, const void *b)
Datum
g_int_picksplit(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
OffsetNumber i,
j;
@ -359,10 +359,10 @@ g_int_picksplit(PG_FUNCTION_ARGS)
waste = 0.0;
for (i = FirstOffsetNumber; i < maxoff; i = OffsetNumberNext(i))
{
datum_alpha = GETENTRY(entryvec,i);
datum_alpha = GETENTRY(entryvec, i);
for (j = OffsetNumberNext(i); j <= maxoff; j = OffsetNumberNext(j))
{
datum_beta = GETENTRY(entryvec,j);
datum_beta = GETENTRY(entryvec, j);
/* compute the wasted space by unioning these guys */
/* size_waste = size_union - size_inter; */
@ -402,10 +402,10 @@ g_int_picksplit(PG_FUNCTION_ARGS)
seed_2 = 2;
}
datum_alpha = GETENTRY(entryvec,seed_1);
datum_alpha = GETENTRY(entryvec, seed_1);
datum_l = copy_intArrayType(datum_alpha);
rt__int_size(datum_l, &size_l);
datum_beta = GETENTRY(entryvec,seed_2);
datum_beta = GETENTRY(entryvec, seed_2);
datum_r = copy_intArrayType(datum_beta);
rt__int_size(datum_r, &size_r);
@ -418,7 +418,7 @@ g_int_picksplit(PG_FUNCTION_ARGS)
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
{
costvector[i - 1].pos = i;
datum_alpha = GETENTRY(entryvec,i);
datum_alpha = GETENTRY(entryvec, i);
union_d = inner_int_union(datum_l, datum_alpha);
rt__int_size(union_d, &size_alpha);
pfree(union_d);
@ -466,7 +466,7 @@ g_int_picksplit(PG_FUNCTION_ARGS)
}
/* okay, which page needs least enlargement? */
datum_alpha = GETENTRY(entryvec,i);
datum_alpha = GETENTRY(entryvec, i);
union_dl = inner_int_union(datum_l, datum_alpha);
union_dr = inner_int_union(datum_r, datum_alpha);
rt__int_size(union_dl, &size_alpha);

View File

@ -20,52 +20,55 @@ Datum g_intbig_picksplit(PG_FUNCTION_ARGS);
Datum g_intbig_union(PG_FUNCTION_ARGS);
Datum g_intbig_same(PG_FUNCTION_ARGS);
#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) \
#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) \
)
PG_FUNCTION_INFO_V1(_intbig_in);
Datum _intbig_in(PG_FUNCTION_ARGS);
Datum _intbig_in(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(_intbig_out);
Datum _intbig_out(PG_FUNCTION_ARGS);
Datum _intbig_out(PG_FUNCTION_ARGS);
Datum
_intbig_in(PG_FUNCTION_ARGS) {
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("_intbig_in() not implemented")));
PG_RETURN_DATUM(0);
_intbig_in(PG_FUNCTION_ARGS)
{
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("_intbig_in() not implemented")));
PG_RETURN_DATUM(0);
}
Datum
_intbig_out(PG_FUNCTION_ARGS) {
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("_intbig_out() not implemented")));
PG_RETURN_DATUM(0);
}
_intbig_out(PG_FUNCTION_ARGS)
{
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("_intbig_out() not implemented")));
PG_RETURN_DATUM(0);
}
/*********************************************************************
** intbig functions
*********************************************************************/
static bool
_intbig_overlap(GISTTYPE *a, ArrayType *b)
_intbig_overlap(GISTTYPE * a, ArrayType *b)
{
int num=ARRNELEMS(b);
int4 *ptr=ARRPTR(b);
int num = ARRNELEMS(b);
int4 *ptr = ARRPTR(b);
while(num--) {
if (GETBIT(GETSIGN(a),HASHVAL(*ptr)))
while (num--)
{
if (GETBIT(GETSIGN(a), HASHVAL(*ptr)))
return true;
ptr++;
}
@ -74,13 +77,14 @@ _intbig_overlap(GISTTYPE *a, ArrayType *b)
}
static bool
_intbig_contains(GISTTYPE *a, ArrayType *b)
_intbig_contains(GISTTYPE * a, ArrayType *b)
{
int num=ARRNELEMS(b);
int4 *ptr=ARRPTR(b);
int num = ARRNELEMS(b);
int4 *ptr = ARRPTR(b);
while(num--) {
if (!GETBIT(GETSIGN(a),HASHVAL(*ptr)))
while (num--)
{
if (!GETBIT(GETSIGN(a), HASHVAL(*ptr)))
return false;
ptr++;
}
@ -89,10 +93,11 @@ _intbig_contains(GISTTYPE *a, ArrayType *b)
}
Datum
g_intbig_same(PG_FUNCTION_ARGS) {
g_intbig_same(PG_FUNCTION_ARGS)
{
GISTTYPE *a = (GISTTYPE *) PG_GETARG_POINTER(0);
GISTTYPE *b = (GISTTYPE *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
bool *result = (bool *) PG_GETARG_POINTER(2);
if (ISALLTRUE(a) && ISALLTRUE(b))
*result = true;
@ -100,16 +105,19 @@ g_intbig_same(PG_FUNCTION_ARGS) {
*result = false;
else if (ISALLTRUE(b))
*result = false;
else {
int4 i;
BITVECP sa = GETSIGN(a),
sb = GETSIGN(b);
else
{
int4 i;
BITVECP sa = GETSIGN(a),
sb = GETSIGN(b);
*result = true;
LOOPBYTE(
if (sa[i] != sb[i]) {
*result = false;
break;
}
if (sa[i] != sb[i])
{
*result = false;
break;
}
);
}
PG_RETURN_POINTER(result);
@ -120,93 +128,105 @@ g_intbig_compress(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
if (entry->leafkey) {
if (entry->leafkey)
{
GISTENTRY *retval;
ArrayType *in = (ArrayType *) PG_DETOAST_DATUM(entry->key);
int4 *ptr;
int num;
GISTTYPE *res=(GISTTYPE*)palloc(CALCGTSIZE(0));
ArrayType *in = (ArrayType *) PG_DETOAST_DATUM(entry->key);
int4 *ptr;
int num;
GISTTYPE *res = (GISTTYPE *) palloc(CALCGTSIZE(0));
ARRISVOID(in);
ptr=ARRPTR(in);
num=ARRNELEMS(in);
memset(res,0,CALCGTSIZE(0));
res->len=CALCGTSIZE(0);
ptr = ARRPTR(in);
num = ARRNELEMS(in);
memset(res, 0, CALCGTSIZE(0));
res->len = CALCGTSIZE(0);
while(num--) {
HASH(GETSIGN(res),*ptr);
while (num--)
{
HASH(GETSIGN(res), *ptr);
ptr++;
}
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
gistentryinit(*retval, PointerGetDatum(res),
entry->rel, entry->page,
entry->offset, res->len, FALSE);
if ( in!=(ArrayType *) PG_DETOAST_DATUM(entry->key) )
entry->rel, entry->page,
entry->offset, res->len, FALSE);
if (in != (ArrayType *) PG_DETOAST_DATUM(entry->key))
pfree(in);
PG_RETURN_POINTER(retval);
} else if ( !ISALLTRUE(DatumGetPointer(entry->key)) ) {
}
else if (!ISALLTRUE(DatumGetPointer(entry->key)))
{
GISTENTRY *retval;
int i;
BITVECP sign = GETSIGN(DatumGetPointer(entry->key));
int i;
BITVECP sign = GETSIGN(DatumGetPointer(entry->key));
GISTTYPE *res;
LOOPBYTE(
if ((sign[i] & 0xff) != 0xff)
PG_RETURN_POINTER(entry);
if ((sign[i] & 0xff) != 0xff)
PG_RETURN_POINTER(entry);
);
res=(GISTTYPE*)palloc(CALCGTSIZE(ALLISTRUE));
res->len=CALCGTSIZE(ALLISTRUE);
res = (GISTTYPE *) palloc(CALCGTSIZE(ALLISTRUE));
res->len = CALCGTSIZE(ALLISTRUE);
res->flag = ALLISTRUE;
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
gistentryinit(*retval, PointerGetDatum(res),
entry->rel, entry->page,
entry->offset, res->len, FALSE);
entry->rel, entry->page,
entry->offset, res->len, FALSE);
PG_RETURN_POINTER(retval);
}
PG_RETURN_POINTER(entry);
}
static int4
sizebitvec(BITVECP sign) {
int4 size = 0, i;
sizebitvec(BITVECP sign)
{
int4 size = 0,
i;
LOOPBYTE(
size += SUMBIT(sign);
sign = (BITVECP) (((char *) sign) + 1);
size += SUMBIT(sign);
sign = (BITVECP) (((char *) sign) + 1);
);
return size;
return size;
}
static int
hemdistsign(BITVECP a, BITVECP b) {
int i,dist=0;
LOOPBIT(
if ( GETBIT(a,i) != GETBIT(b,i) )
dist++;
);
return dist;
hemdistsign(BITVECP a, BITVECP b)
{
int i,
dist = 0;
LOOPBIT(
if (GETBIT(a, i) != GETBIT(b, i))
dist++;
);
return dist;
}
static int
hemdist(GISTTYPE *a, GISTTYPE *b) {
if ( ISALLTRUE(a) ) {
if (ISALLTRUE(b))
return 0;
else
return SIGLENBIT-sizebitvec(GETSIGN(b));
} else if (ISALLTRUE(b))
return SIGLENBIT-sizebitvec(GETSIGN(a));
return hemdistsign( GETSIGN(a), GETSIGN(b) );
hemdist(GISTTYPE * a, GISTTYPE * b)
{
if (ISALLTRUE(a))
{
if (ISALLTRUE(b))
return 0;
else
return SIGLENBIT - sizebitvec(GETSIGN(b));
}
else if (ISALLTRUE(b))
return SIGLENBIT - sizebitvec(GETSIGN(a));
return hemdistsign(GETSIGN(a), GETSIGN(b));
}
Datum
@ -218,29 +238,33 @@ g_intbig_decompress(PG_FUNCTION_ARGS)
static int4
unionkey(BITVECP sbase, GISTTYPE * add)
{
int4 i;
BITVECP sadd = GETSIGN(add);
int4 i;
BITVECP sadd = GETSIGN(add);
if (ISALLTRUE(add))
return 1;
LOOPBYTE(
sbase[i] |= sadd[i];
sbase[i] |= sadd[i];
);
return 0;
}
Datum
g_intbig_union(PG_FUNCTION_ARGS) {
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
int *size = (int *) PG_GETARG_POINTER(1);
BITVEC base;
int4 i, len;
int4 flag = 0;
g_intbig_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
int *size = (int *) PG_GETARG_POINTER(1);
BITVEC base;
int4 i,
len;
int4 flag = 0;
GISTTYPE *result;
MemSet((void *) base, 0, sizeof(BITVEC));
for (i = 0; i < entryvec->n; i++) {
if (unionkey(base, GETENTRY(entryvec, i))) {
for (i = 0; i < entryvec->n; i++)
{
if (unionkey(base, GETENTRY(entryvec, i)))
{
flag = ALLISTRUE;
break;
}
@ -252,187 +276,201 @@ g_intbig_union(PG_FUNCTION_ARGS) {
result->flag = flag;
if (!ISALLTRUE(result))
memcpy((void *) GETSIGN(result), (void *) base, sizeof(BITVEC));
PG_RETURN_POINTER(result);
}
Datum
g_intbig_penalty(PG_FUNCTION_ARGS) {
g_intbig_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);
float *penalty = (float *) PG_GETARG_POINTER(2);
GISTTYPE *origval = (GISTTYPE *) DatumGetPointer(origentry->key);
GISTTYPE *newval = (GISTTYPE *) DatumGetPointer(newentry->key);
*penalty=hemdist(origval,newval);
*penalty = hemdist(origval, newval);
PG_RETURN_POINTER(penalty);
}
typedef struct {
typedef struct
{
OffsetNumber pos;
int4 cost;
int4 cost;
} SPLITCOST;
static int
comparecost(const void *a, const void *b) {
comparecost(const void *a, const void *b)
{
return ((SPLITCOST *) a)->cost - ((SPLITCOST *) b)->cost;
}
Datum
g_intbig_picksplit(PG_FUNCTION_ARGS) {
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
OffsetNumber k,
j;
GISTTYPE *datum_l,
*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;
OffsetNumber maxoff;
BITVECP ptr;
int i;
SPLITCOST *costvector;
GISTTYPE *_k,
*_j;
g_intbig_picksplit(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
OffsetNumber k,
j;
GISTTYPE *datum_l,
*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;
OffsetNumber maxoff;
BITVECP ptr;
int i;
SPLITCOST *costvector;
GISTTYPE *_k,
*_j;
maxoff = entryvec->n - 2;
nbytes = (maxoff + 2) * sizeof(OffsetNumber);
v->spl_left = (OffsetNumber *) palloc(nbytes);
v->spl_right = (OffsetNumber *) palloc(nbytes);
maxoff = entryvec->n - 2;
nbytes = (maxoff + 2) * sizeof(OffsetNumber);
v->spl_left = (OffsetNumber *) palloc(nbytes);
v->spl_right = (OffsetNumber *) palloc(nbytes);
for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k)) {
_k = GETENTRY(entryvec, k);
for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j)) {
size_waste=hemdist(_k, GETENTRY(entryvec, j));
if (size_waste > waste ) {
waste = size_waste;
seed_1 = k;
seed_2 = j;
}
}
}
for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
{
_k = GETENTRY(entryvec, k);
for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
{
size_waste = hemdist(_k, GETENTRY(entryvec, j));
if (size_waste > waste)
{
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;
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;
}
if (seed_1 == 0 || seed_2 == 0)
{
seed_1 = 1;
seed_2 = 2;
}
/* form initial .. */
if (ISALLTRUE(GETENTRY(entryvec, seed_1)))
{
datum_l = (GISTTYPE *) palloc(GTHDRSIZE);
datum_l->len = GTHDRSIZE;
datum_l->flag = ALLISTRUE;
}
else
{
datum_l = (GISTTYPE *) palloc(GTHDRSIZE + SIGLEN);
datum_l->len = GTHDRSIZE + SIGLEN;
datum_l->flag = 0;
memcpy((void *) GETSIGN(datum_l), (void *) GETSIGN(GETENTRY(entryvec, seed_1)), sizeof(BITVEC));
}
if (ISALLTRUE(GETENTRY(entryvec, seed_2)))
{
datum_r = (GISTTYPE *) palloc(GTHDRSIZE);
datum_r->len = GTHDRSIZE;
datum_r->flag = ALLISTRUE;
}
else
{
datum_r = (GISTTYPE *) palloc(GTHDRSIZE + SIGLEN);
datum_r->len = GTHDRSIZE + SIGLEN;
datum_r->flag = 0;
memcpy((void *) GETSIGN(datum_r), (void *) GETSIGN(GETENTRY(entryvec, seed_2)), sizeof(BITVEC));
}
/* form initial .. */
if (ISALLTRUE(GETENTRY(entryvec, seed_1)))
{
datum_l = (GISTTYPE *) palloc(GTHDRSIZE);
datum_l->len = GTHDRSIZE;
datum_l->flag = ALLISTRUE;
}
else
{
datum_l = (GISTTYPE *) palloc(GTHDRSIZE + SIGLEN);
datum_l->len = GTHDRSIZE + SIGLEN;
datum_l->flag = 0;
memcpy((void *) GETSIGN(datum_l), (void *) GETSIGN(GETENTRY(entryvec, seed_1)), sizeof(BITVEC));
}
if (ISALLTRUE(GETENTRY(entryvec, seed_2)))
{
datum_r = (GISTTYPE *) palloc(GTHDRSIZE);
datum_r->len = GTHDRSIZE;
datum_r->flag = ALLISTRUE;
}
else
{
datum_r = (GISTTYPE *) palloc(GTHDRSIZE + SIGLEN);
datum_r->len = GTHDRSIZE + SIGLEN;
datum_r->flag = 0;
memcpy((void *) GETSIGN(datum_r), (void *) GETSIGN(GETENTRY(entryvec, seed_2)), sizeof(BITVEC));
}
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);
size_alpha = hemdist(datum_l,_j);
size_beta = hemdist(datum_r,_j);
costvector[j - 1].cost = abs(size_alpha - size_beta);
}
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
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);
size_alpha = hemdist(datum_l, _j);
size_beta = hemdist(datum_r, _j);
costvector[j - 1].cost = abs(size_alpha - size_beta);
}
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
union_l=GETSIGN(datum_l);
union_r=GETSIGN(datum_r);
union_l = GETSIGN(datum_l);
union_r = GETSIGN(datum_r);
for (k = 0; k < maxoff; k++)
{
j = costvector[k].pos;
if (j == seed_1)
{
*left++ = j;
v->spl_nleft++;
continue;
}
else if (j == seed_2)
{
*right++ = j;
v->spl_nright++;
continue;
}
_j = GETENTRY(entryvec, j);
size_alpha = hemdist(datum_l,_j);
size_beta = hemdist(datum_r,_j);
for (k = 0; k < maxoff; k++)
{
j = costvector[k].pos;
if (j == seed_1)
{
*left++ = j;
v->spl_nleft++;
continue;
}
else if (j == seed_2)
{
*right++ = j;
v->spl_nright++;
continue;
}
_j = GETENTRY(entryvec, j);
size_alpha = hemdist(datum_l, _j);
size_beta = hemdist(datum_r, _j);
if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.00001))
{
if (ISALLTRUE(datum_l) || ISALLTRUE(_j) ) {
if (!ISALLTRUE(datum_l))
MemSet((void *) union_l, 0xff, sizeof(BITVEC));
} else {
ptr=GETSIGN(_j);
LOOPBYTE(
union_l[i] |= ptr[i];
);
}
*left++ = j;
v->spl_nleft++;
}
else
{
if (ISALLTRUE(datum_r) || ISALLTRUE(_j) ) {
if (!ISALLTRUE(datum_r))
MemSet((void *) union_r, 0xff, sizeof(BITVEC));
} else {
ptr=GETSIGN(_j);
LOOPBYTE(
union_r[i] |= ptr[i];
);
}
*right++ = j;
v->spl_nright++;
}
}
if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.00001))
{
if (ISALLTRUE(datum_l) || ISALLTRUE(_j))
{
if (!ISALLTRUE(datum_l))
MemSet((void *) union_l, 0xff, sizeof(BITVEC));
}
else
{
ptr = GETSIGN(_j);
LOOPBYTE(
union_l[i] |= ptr[i];
);
}
*left++ = j;
v->spl_nleft++;
}
else
{
if (ISALLTRUE(datum_r) || ISALLTRUE(_j))
{
if (!ISALLTRUE(datum_r))
MemSet((void *) union_r, 0xff, sizeof(BITVEC));
}
else
{
ptr = GETSIGN(_j);
LOOPBYTE(
union_r[i] |= ptr[i];
);
}
*right++ = j;
v->spl_nright++;
}
}
*right = *left = FirstOffsetNumber;
pfree(costvector);
*right = *left = FirstOffsetNumber;
pfree(costvector);
v->spl_ldatum = PointerGetDatum(datum_l);
v->spl_rdatum = PointerGetDatum(datum_r);
v->spl_ldatum = PointerGetDatum(datum_l);
v->spl_rdatum = PointerGetDatum(datum_r);
PG_RETURN_POINTER(v);
PG_RETURN_POINTER(v);
}
Datum
@ -443,12 +481,13 @@ g_intbig_consistent(PG_FUNCTION_ARGS)
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
bool retval;
if ( ISALLTRUE(DatumGetPointer(entry->key)) )
if (ISALLTRUE(DatumGetPointer(entry->key)))
PG_RETURN_BOOL(true);
if (strategy == BooleanSearchStrategy) {
if (strategy == BooleanSearchStrategy)
{
PG_RETURN_BOOL(signconsistent((QUERYTYPE *) query,
GETSIGN(DatumGetPointer(entry->key)),
GETSIGN(DatumGetPointer(entry->key)),
false));
}
@ -462,58 +501,72 @@ g_intbig_consistent(PG_FUNCTION_ARGS)
retval = _intbig_overlap((GISTTYPE *) DatumGetPointer(entry->key), query);
break;
case RTSameStrategyNumber:
if (GIST_LEAF(entry)) {
int i,num=ARRNELEMS(query);
int4 *ptr=ARRPTR(query);
BITVEC qp;
BITVECP dq, de;
memset(qp,0,sizeof(BITVEC));
if (GIST_LEAF(entry))
{
int i,
num = ARRNELEMS(query);
int4 *ptr = ARRPTR(query);
BITVEC qp;
BITVECP dq,
de;
while(num--) {
memset(qp, 0, sizeof(BITVEC));
while (num--)
{
HASH(qp, *ptr);
ptr++;
}
de=GETSIGN((GISTTYPE *) DatumGetPointer(entry->key));
dq=qp;
retval=true;
de = GETSIGN((GISTTYPE *) DatumGetPointer(entry->key));
dq = qp;
retval = true;
LOOPBYTE(
if ( de[i] != dq[i] ) {
retval=false;
break;
}
if (de[i] != dq[i])
{
retval = false;
break;
}
);
} else
}
else
retval = _intbig_contains((GISTTYPE *) DatumGetPointer(entry->key), query);
break;
case RTContainsStrategyNumber:
retval = _intbig_contains((GISTTYPE *) DatumGetPointer(entry->key), query);
break;
case RTContainedByStrategyNumber:
if (GIST_LEAF(entry)) {
int i,num=ARRNELEMS(query);
int4 *ptr=ARRPTR(query);
BITVEC qp;
BITVECP dq, de;
memset(qp,0,sizeof(BITVEC));
if (GIST_LEAF(entry))
{
int i,
num = ARRNELEMS(query);
int4 *ptr = ARRPTR(query);
BITVEC qp;
BITVECP dq,
de;
while(num--) {
memset(qp, 0, sizeof(BITVEC));
while (num--)
{
HASH(qp, *ptr);
ptr++;
}
de=GETSIGN((GISTTYPE *) DatumGetPointer(entry->key));
dq=qp;
retval=true;
de = GETSIGN((GISTTYPE *) DatumGetPointer(entry->key));
dq = qp;
retval = true;
LOOPBYTE(
if ( de[i] & ~dq[i] ) {
retval=false;
break;
}
if (de[i] & ~dq[i])
{
retval = false;
break;
}
);
} else
}
else
retval = _intbig_overlap((GISTTYPE *) DatumGetPointer(entry->key), query);
break;
default:
@ -521,5 +574,3 @@ g_intbig_consistent(PG_FUNCTION_ARGS)
}
PG_RETURN_BOOL(retval);
}

View File

@ -75,8 +75,8 @@ _ltree_compress(PG_FUNCTION_ARGS)
if (ARR_NDIM(val) != 1)
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("array must be one-dimensional")));
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("array must be one-dimensional")));
key = (ltree_gist *) palloc(len);
key->len = len;
@ -98,7 +98,7 @@ _ltree_compress(PG_FUNCTION_ARGS)
entry->rel, entry->page,
entry->offset, key->len, FALSE);
}
else if ( !LTG_ISALLTRUE(entry->key) )
else if (!LTG_ISALLTRUE(entry->key))
{
int4 i,
len;
@ -107,7 +107,7 @@ _ltree_compress(PG_FUNCTION_ARGS)
BITVECP sign = LTG_SIGN(DatumGetPointer(entry->key));
ALOOPBYTE(
if ((sign[i]&0xff) != 0xff)
if ((sign[i] & 0xff) != 0xff)
PG_RETURN_POINTER(retval);
);
len = LTG_HDRSIZE;
@ -172,10 +172,11 @@ unionkey(BITVECP sbase, ltree_gist * add)
Datum
_ltree_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
int *size = (int *) PG_GETARG_POINTER(1);
ABITVEC base;
int4 i,len;
int4 i,
len;
int4 flag = 0;
ltree_gist *result;
@ -212,28 +213,33 @@ sizebitvec(BITVECP sign)
return size;
}
static int
hemdistsign(BITVECP a, BITVECP b) {
int i,dist=0;
static int
hemdistsign(BITVECP a, BITVECP b)
{
int i,
dist = 0;
ALOOPBIT(
if ( GETBIT(a,i) != GETBIT(b,i) )
dist++;
if (GETBIT(a, i) != GETBIT(b, i))
dist++;
);
return dist;
}
static int
hemdist(ltree_gist *a, ltree_gist *b) {
if ( LTG_ISALLTRUE(a) ) {
if (LTG_ISALLTRUE(b))
return 0;
else
return ASIGLENBIT-sizebitvec(LTG_SIGN(b));
} else if (LTG_ISALLTRUE(b))
return ASIGLENBIT-sizebitvec(LTG_SIGN(a));
return hemdistsign( LTG_SIGN(a), LTG_SIGN(b) );
hemdist(ltree_gist * a, ltree_gist * b)
{
if (LTG_ISALLTRUE(a))
{
if (LTG_ISALLTRUE(b))
return 0;
else
return ASIGLENBIT - sizebitvec(LTG_SIGN(b));
}
else if (LTG_ISALLTRUE(b))
return ASIGLENBIT - sizebitvec(LTG_SIGN(a));
return hemdistsign(LTG_SIGN(a), LTG_SIGN(b));
}
@ -244,7 +250,7 @@ _ltree_penalty(PG_FUNCTION_ARGS)
ltree_gist *newval = (ltree_gist *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *penalty = (float *) PG_GETARG_POINTER(2);
*penalty=hemdist(origval,newval);
*penalty = hemdist(origval, newval);
PG_RETURN_POINTER(penalty);
}
@ -263,7 +269,7 @@ comparecost(const void *a, const void *b)
Datum
_ltree_picksplit(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
OffsetNumber k,
j;
@ -271,7 +277,8 @@ _ltree_picksplit(PG_FUNCTION_ARGS)
*datum_r;
BITVECP union_l,
union_r;
int4 size_alpha, size_beta;
int4 size_alpha,
size_beta;
int4 size_waste,
waste = -1;
int4 nbytes;
@ -291,11 +298,14 @@ _ltree_picksplit(PG_FUNCTION_ARGS)
v->spl_left = (OffsetNumber *) palloc(nbytes);
v->spl_right = (OffsetNumber *) palloc(nbytes);
for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k)) {
for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
{
_k = GETENTRY(entryvec, k);
for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j)) {
size_waste=hemdist(_k, GETENTRY(entryvec, j));
if (size_waste > waste ) {
for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
{
size_waste = hemdist(_k, GETENTRY(entryvec, j));
if (size_waste > waste)
{
waste = size_waste;
seed_1 = k;
seed_2 = j;
@ -349,15 +359,15 @@ _ltree_picksplit(PG_FUNCTION_ARGS)
{
costvector[j - 1].pos = j;
_j = GETENTRY(entryvec, j);
size_alpha = hemdist(datum_l,_j);
size_beta = hemdist(datum_r,_j);
size_alpha = hemdist(datum_l, _j);
size_beta = hemdist(datum_r, _j);
costvector[j - 1].cost = abs(size_alpha - size_beta);
}
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
union_l=LTG_SIGN(datum_l);
union_r=LTG_SIGN(datum_r);
union_l = LTG_SIGN(datum_l);
union_r = LTG_SIGN(datum_r);
for (k = 0; k < maxoff; k++)
{
j = costvector[k].pos;
@ -374,18 +384,21 @@ _ltree_picksplit(PG_FUNCTION_ARGS)
continue;
}
_j = GETENTRY(entryvec, j);
size_alpha = hemdist(datum_l,_j);
size_beta = hemdist(datum_r,_j);
size_alpha = hemdist(datum_l, _j);
size_beta = hemdist(datum_r, _j);
if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.00001))
if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.00001))
{
if (LTG_ISALLTRUE(datum_l) || LTG_ISALLTRUE(_j) ) {
if (LTG_ISALLTRUE(datum_l) || LTG_ISALLTRUE(_j))
{
if (!LTG_ISALLTRUE(datum_l))
MemSet((void *) union_l, 0xff, sizeof(ABITVEC));
} else {
ptr=LTG_SIGN(_j);
MemSet((void *) union_l, 0xff, sizeof(ABITVEC));
}
else
{
ptr = LTG_SIGN(_j);
ALOOPBYTE(
union_l[i] |= ptr[i];
union_l[i] |= ptr[i];
);
}
*left++ = j;
@ -393,13 +406,16 @@ _ltree_picksplit(PG_FUNCTION_ARGS)
}
else
{
if (LTG_ISALLTRUE(datum_r) || LTG_ISALLTRUE(_j) ) {
if (LTG_ISALLTRUE(datum_r) || LTG_ISALLTRUE(_j))
{
if (!LTG_ISALLTRUE(datum_r))
MemSet((void *) union_r, 0xff, sizeof(ABITVEC));
} else {
ptr=LTG_SIGN(_j);
MemSet((void *) union_r, 0xff, sizeof(ABITVEC));
}
else
{
ptr = LTG_SIGN(_j);
ALOOPBYTE(
union_r[i] |= ptr[i];
union_r[i] |= ptr[i];
);
}
*right++ = j;
@ -498,22 +514,24 @@ gist_qe(ltree_gist * key, lquery * query)
}
static bool
_arrq_cons(ltree_gist *key, ArrayType *_query) {
lquery *query = (lquery *) ARR_DATA_PTR(_query);
int num = ArrayGetNItems(ARR_NDIM(_query), ARR_DIMS(_query));
_arrq_cons(ltree_gist * key, ArrayType *_query)
{
lquery *query = (lquery *) ARR_DATA_PTR(_query);
int num = ArrayGetNItems(ARR_NDIM(_query), ARR_DIMS(_query));
if (ARR_NDIM(_query) != 1)
ereport(ERROR,
if (ARR_NDIM(_query) != 1)
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("array must be one-dimensional")));
while (num > 0) {
if ( gist_qe(key, query) )
return true;
num--;
query = (lquery*)NEXTVAL(query);
}
return false;
while (num > 0)
{
if (gist_qe(key, query))
return true;
num--;
query = (lquery *) NEXTVAL(query);
}
return false;
}
Datum

View File

@ -166,7 +166,7 @@ hashing(BITVECP sign, ltree * t)
Datum
ltree_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
int *size = (int *) PG_GETARG_POINTER(1);
BITVEC base;
int4 i,
@ -277,14 +277,14 @@ treekey_cmp(const void *a, const void *b)
return ltree_compare(
((RIX *) a)->r,
((RIX *) b)->r
);
);
}
Datum
ltree_picksplit(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
OffsetNumber j;
int4 i;
@ -602,21 +602,23 @@ gist_qtxt(ltree_gist * key, ltxtquery * query)
}
static bool
arrq_cons(ltree_gist *key, ArrayType *_query) {
lquery *query = (lquery *) ARR_DATA_PTR(_query);
int num = ArrayGetNItems(ARR_NDIM(_query), ARR_DIMS(_query));
arrq_cons(ltree_gist * key, ArrayType *_query)
{
lquery *query = (lquery *) ARR_DATA_PTR(_query);
int num = ArrayGetNItems(ARR_NDIM(_query), ARR_DIMS(_query));
if (ARR_NDIM(_query) != 1)
ereport(ERROR,
if (ARR_NDIM(_query) != 1)
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("array must be one-dimensional")));
while (num > 0) {
if ( gist_qe(key, query) && gist_between(key, query) )
return true;
num--;
query = NEXTVAL(query);
}
while (num > 0)
{
if (gist_qe(key, query) && gist_between(key, query))
return true;
num--;
query = NEXTVAL(query);
}
return false;
}
@ -700,7 +702,7 @@ ltree_consistent(PG_FUNCTION_ARGS)
if (GIST_LEAF(entry))
res = DatumGetBool(DirectFunctionCall2(lt_q_regex,
PointerGetDatum(LTG_NODE(key)),
PointerGetDatum((ArrayType *) query)
PointerGetDatum((ArrayType *) query)
));
else
res = arrq_cons(key, (ArrayType *) query);

View File

@ -228,20 +228,20 @@ update_table_list(db_info * dbi)
if (res != NULL)
{
t = PQntuples(res);
/*
* First: use the tbl_list as the outer loop and the result set as
* the inner loop, this will determine what tables should be
* removed
*/
* First: use the tbl_list as the outer loop and the result
* set as the inner loop, this will determine what tables
* should be removed
*/
while (tbl_elem != NULL)
{
tbl = ((tbl_info *) DLE_VAL(tbl_elem));
found_match = 0;
for (i = 0; i < t; i++)
{ /* loop through result set looking for a
* match */
{ /* loop through result set looking for a
* match */
if (tbl->relid == atooid(PQgetvalue(res, i, PQfnumber(res, "oid"))))
{
found_match = 1;
@ -249,22 +249,22 @@ update_table_list(db_info * dbi)
}
}
if (found_match == 0)
{ /* then we didn't find this tbl_elem in
* the result set */
{ /* then we didn't find this tbl_elem in
* the result set */
Dlelem *elem_to_remove = tbl_elem;
tbl_elem = DLGetSucc(tbl_elem);
remove_table_from_list(elem_to_remove);
}
else
tbl_elem = DLGetSucc(tbl_elem);
} /* Done removing dropped tables from the
* table_list */
} /* Done removing dropped tables from the
* table_list */
/*
* Then loop use result set as outer loop and tbl_list as the
* inner loop to determine what tables are new
*/
* Then loop use result set as outer loop and tbl_list as the
* inner loop to determine what tables are new
*/
for (i = 0; i < t; i++)
{
tbl_elem = DLGetHead(dbi->table_list);
@ -279,8 +279,8 @@ update_table_list(db_info * dbi)
}
tbl_elem = DLGetSucc(tbl_elem);
}
if (found_match == 0) /* then we didn't find this result
* now in the tbl_list */
if (found_match == 0) /* then we didn't find this result
* now in the tbl_list */
{
DLAddTail(dbi->table_list, DLNewElem(init_table_info(res, i, dbi)));
if (args->debug >= 1)
@ -290,7 +290,7 @@ update_table_list(db_info * dbi)
log_entry(logbuffer);
}
}
} /* end of for loop that adds tables */
} /* end of for loop that adds tables */
}
fflush(LOGOUTPUT);
PQclear(res);
@ -419,7 +419,7 @@ init_db_list()
dbs->age = atol(PQgetvalue(res, 0, PQfnumber(res, "age")));
if (res)
PQclear(res);
if (args->debug >= 2)
print_db_list(db_list, 0);
}
@ -499,49 +499,49 @@ update_db_list(Dllist *db_list)
if (res != NULL)
{
t = PQntuples(res);
/*
* First: use the db_list as the outer loop and the result set as
* the inner loop, this will determine what databases should be
* removed
*/
* First: use the db_list as the outer loop and the result set
* as the inner loop, this will determine what databases
* should be removed
*/
while (db_elem != NULL)
{
dbi = ((db_info *) DLE_VAL(db_elem));
found_match = 0;
for (i = 0; i < t; i++)
{ /* loop through result set looking for a
* match */
{ /* loop through result set looking for a
* match */
if (dbi->oid == atooid(PQgetvalue(res, i, PQfnumber(res, "oid"))))
{
found_match = 1;
/*
* update the dbi->age so that we ensure
* xid_wraparound won't happen
*/
* update the dbi->age so that we ensure
* xid_wraparound won't happen
*/
dbi->age = atol(PQgetvalue(res, i, PQfnumber(res, "age")));
break;
}
}
if (found_match == 0)
{ /* then we didn't find this db_elem in the
* result set */
{ /* then we didn't find this db_elem in the
* result set */
Dlelem *elem_to_remove = db_elem;
db_elem = DLGetSucc(db_elem);
remove_db_from_list(elem_to_remove);
}
else
db_elem = DLGetSucc(db_elem);
} /* Done removing dropped databases from
* the table_list */
} /* Done removing dropped databases from
* the table_list */
/*
* Then loop use result set as outer loop and db_list as the inner
* loop to determine what databases are new
*/
* Then loop use result set as outer loop and db_list as the
* inner loop to determine what databases are new
*/
for (i = 0; i < t; i++)
{
db_elem = DLGetHead(db_list);
@ -556,20 +556,20 @@ update_db_list(Dllist *db_list)
}
db_elem = DLGetSucc(db_elem);
}
if (found_match == 0) /* then we didn't find this result
* now in the tbl_list */
if (found_match == 0) /* then we didn't find this result
* now in the tbl_list */
{
DLAddTail(db_list, DLNewElem(init_dbinfo
(PQgetvalue(res, i, PQfnumber(res, "datname")),
atooid(PQgetvalue(res, i, PQfnumber(res, "oid"))),
atol(PQgetvalue(res, i, PQfnumber(res, "age"))))));
(PQgetvalue(res, i, PQfnumber(res, "datname")),
atooid(PQgetvalue(res, i, PQfnumber(res, "oid"))),
atol(PQgetvalue(res, i, PQfnumber(res, "age"))))));
if (args->debug >= 1)
{
sprintf(logbuffer, "added database: %s", ((db_info *) DLE_VAL(DLGetTail(db_list)))->dbname);
log_entry(logbuffer);
}
}
} /* end of for loop that adds tables */
} /* end of for loop that adds tables */
}
fflush(LOGOUTPUT);
PQclear(res);
@ -604,16 +604,14 @@ xid_wraparound_check(db_info * dbi)
* 500million xacts to work with so we should be able to spread the
* load of full database vacuums a bit
*/
if (dbi->age > 1500000000 )
if (dbi->age > 1500000000)
{
PGresult *res = NULL;
res = send_query("VACUUM", dbi);
/* FIXME: Perhaps should add a check for PQ_COMMAND_OK */
if (res != NULL)
{
PQclear(res);
}
return 1;
}
return 0;
@ -697,16 +695,16 @@ print_db_info(db_info * dbi, int print_tbl_list)
{
sprintf(logbuffer, "dbname: %s", (dbi->dbname) ? dbi->dbname : "(null)");
log_entry(logbuffer);
sprintf(logbuffer, " oid: %u", dbi->oid);
log_entry(logbuffer);
sprintf(logbuffer, " username: %s", (dbi->username) ? dbi->username : "(null)");
log_entry(logbuffer);
sprintf(logbuffer, " password: %s", (dbi->password) ? dbi->password : "(null)");
log_entry(logbuffer);
if (dbi->conn != NULL)
log_entry(" conn is valid, (connected)");
else
@ -714,10 +712,10 @@ print_db_info(db_info * dbi, int print_tbl_list)
sprintf(logbuffer, " default_analyze_threshold: %li", dbi->analyze_threshold);
log_entry(logbuffer);
sprintf(logbuffer, " default_vacuum_threshold: %li", dbi->vacuum_threshold);
log_entry(logbuffer);
fflush(LOGOUTPUT);
if (print_tbl_list > 0)
print_table_list(dbi->table_list);
@ -1036,7 +1034,7 @@ main(int argc, char *argv[])
db_list = init_db_list();
if (db_list == NULL)
return 1;
if (check_stats_enabled(((db_info *) DLE_VAL(DLGetHead(db_list)))) != 0)
{
log_entry("Error: GUC variable stats_row_level must be enabled.");
@ -1096,38 +1094,40 @@ main(int argc, char *argv[])
if (res != NULL)
{
for (j = 0; j < PQntuples(res); j++)
{ /* loop through result set */
tbl_elem = DLGetHead(dbs->table_list); /* Reset tbl_elem to top
* of dbs->table_list */
{ /* loop through result set */
tbl_elem = DLGetHead(dbs->table_list); /* Reset tbl_elem to top
* of dbs->table_list */
while (tbl_elem != NULL)
{ /* Loop through tables in list */
tbl = ((tbl_info *) DLE_VAL(tbl_elem)); /* set tbl_info =
* current_table */
{ /* Loop through tables in list */
tbl = ((tbl_info *) DLE_VAL(tbl_elem)); /* set tbl_info =
* current_table */
if (tbl->relid == atooid(PQgetvalue(res, j, PQfnumber(res, "oid"))))
{
tbl->curr_analyze_count =
(atol(PQgetvalue(res, j, PQfnumber(res, "n_tup_ins"))) +
atol(PQgetvalue(res, j, PQfnumber(res, "n_tup_upd"))) +
atol(PQgetvalue(res, j, PQfnumber(res, "n_tup_del"))));
atol(PQgetvalue(res, j, PQfnumber(res, "n_tup_upd"))) +
atol(PQgetvalue(res, j, PQfnumber(res, "n_tup_del"))));
tbl->curr_vacuum_count =
(atol(PQgetvalue(res, j, PQfnumber(res, "n_tup_del"))) +
atol(PQgetvalue(res, j, PQfnumber(res, "n_tup_upd"))));
atol(PQgetvalue(res, j, PQfnumber(res, "n_tup_upd"))));
/*
* Check numDeletes to see if we need to
* vacuum, if so: Run vacuum analyze
* (adding analyze is small so we might as
* well) Update table thresholds and
* related information if numDeletes is
* not big enough for vacuum then check
* numInserts for analyze
*/
* Check numDeletes to see if we need
* to vacuum, if so: Run vacuum
* analyze (adding analyze is small so
* we might as well) Update table
* thresholds and related information
* if numDeletes is not big enough for
* vacuum then check numInserts for
* analyze
*/
if (tbl->curr_vacuum_count - tbl->CountAtLastVacuum >= tbl->vacuum_threshold)
{
/*
* if relisshared = t and database !=
* template1 then only do an analyze
*/
* if relisshared = t and database
* != template1 then only do an
* analyze
*/
if (tbl->relisshared > 0 && strcmp("template1", dbs->dbname))
snprintf(buf, sizeof(buf), "ANALYZE %s", tbl->table_name);
else
@ -1157,19 +1157,20 @@ main(int argc, char *argv[])
if (args->debug >= 2)
print_table_info(tbl);
}
break; /* once we have found a match, no
* need to keep checking. */
break; /* once we have found a
* match, no need to keep
* checking. */
}
/*
* Advance the table pointers for the next
* loop
*/
* Advance the table pointers for the next
* loop
*/
tbl_elem = DLGetSucc(tbl_elem);
} /* end for table while loop */
} /* end for j loop (tuples in PGresult) */
} /* end for table while loop */
} /* end for j loop (tuples in PGresult) */
} /* end if (res != NULL) */
} /* close of if(xid_wraparound_check()) */
/* Done working on this db, Clean up, then advance cur_db */

View File

@ -1,7 +1,7 @@
/* -------------------------------------------------------------------------
* pg_dumplo
*
* $PostgreSQL: pgsql/contrib/pg_dumplo/utils.c,v 1.8 2003/11/29 19:51:35 pgsql Exp $
* $PostgreSQL: pgsql/contrib/pg_dumplo/utils.c,v 1.9 2004/08/29 05:06:36 momjian Exp $
*
* Karel Zak 1999-2000
* -------------------------------------------------------------------------
@ -30,7 +30,7 @@ void
index_file(LODumpMaster * pgLO)
{
char path[BUFSIZ];
int sz;
int sz;
if (pgLO->action == ACTION_SHOW)
return;
@ -51,7 +51,7 @@ index_file(LODumpMaster * pgLO)
}
sz = strlen(path);
strncat(path, "/lo_dump.index", BUFSIZ-sz);
strncat(path, "/lo_dump.index", BUFSIZ - sz);
if ((pgLO->index = fopen(path, "w")) == NULL)
{
@ -63,7 +63,7 @@ index_file(LODumpMaster * pgLO)
else if (pgLO->action != ACTION_NONE)
{
sz = strlen(path);
strncat(path, "/lo_dump.index", BUFSIZ-sz);
strncat(path, "/lo_dump.index", BUFSIZ - sz);
if ((pgLO->index = fopen(path, "r")) == NULL)
{

View File

@ -11,53 +11,54 @@
#include "storage/bufpage.h"
/* options */
#define LPADDING 2
#define RPADDING 1
#define LPADDING 2
#define RPADDING 1
#define KEEPONLYALNUM
#define IGNORECASE
#define IGNORECASE
#define DIVUNION
typedef char trgm[3];
#define CMPCHAR(a,b) ( ((a)==(b)) ? 0 : ( ((a)<(b)) ? -1 : 1 ) )
#define CMPPCHAR(a,b,i) CMPCHAR( *(((char*)(a))+i), *(((char*)(b))+i) )
#define CMPPCHAR(a,b,i) CMPCHAR( *(((char*)(a))+i), *(((char*)(b))+i) )
#define CMPTRGM(a,b) ( CMPPCHAR(a,b,0) ? CMPPCHAR(a,b,0) : ( CMPPCHAR(a,b,1) ? CMPPCHAR(a,b,1) : CMPPCHAR(a,b,2) ) )
#define CPTRGM(a,b) do { \
#define CPTRGM(a,b) do { \
*(((char*)(a))+0) = *(((char*)(b))+0); \
*(((char*)(a))+1) = *(((char*)(b))+1); \
*(((char*)(a))+2) = *(((char*)(b))+2); \
} while(0);
typedef struct {
int4 len;
uint8 flag;
char data[1];
} TRGM;
typedef struct
{
int4 len;
uint8 flag;
char data[1];
} TRGM;
#define TRGMHRDSIZE (sizeof(int4)+sizeof(uint8))
#define TRGMHRDSIZE (sizeof(int4)+sizeof(uint8))
/* gist */
#define BITBYTE 8
#define SIGLENINT 3 /* >122 => key will toast, so very slow!!! */
#define SIGLEN ( sizeof(int)*SIGLENINT )
#define SIGLENINT 3 /* >122 => key will toast, so very slow!!! */
#define SIGLEN ( sizeof(int)*SIGLENINT )
#define SIGLENBIT (SIGLEN*BITBYTE - 1) /* see makesign */
#define SIGLENBIT (SIGLEN*BITBYTE - 1) /* see makesign */
typedef char BITVEC[SIGLEN];
typedef char *BITVECP;
#define LOOPBYTE(a) \
for(i=0;i<SIGLEN;i++) {\
a;\
}
for(i=0;i<SIGLEN;i++) {\
a;\
}
#define LOOPBIT(a) \
for(i=0;i<SIGLENBIT;i++) {\
a;\
}
for(i=0;i<SIGLENBIT;i++) {\
a;\
}
#define GETBYTE(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITBYTE ) ) )
#define GETBITBYTE(x,i) ( ((char)(x)) >> i & 0x01 )
@ -68,21 +69,21 @@ typedef char *BITVECP;
#define HASHVAL(val) (((unsigned int)(val)) % SIGLENBIT)
#define HASH(sign, val) SETBIT((sign), HASHVAL(val))
#define ARRKEY 0x01
#define SIGNKEY 0x02
#define ALLISTRUE 0x04
#define ARRKEY 0x01
#define SIGNKEY 0x02
#define ALLISTRUE 0x04
#define ISARRKEY(x) ( ((TRGM*)x)->flag & ARRKEY )
#define ISSIGNKEY(x) ( ((TRGM*)x)->flag & SIGNKEY )
#define ISALLTRUE(x) ( ((TRGM*)x)->flag & ALLISTRUE )
#define ISSIGNKEY(x) ( ((TRGM*)x)->flag & SIGNKEY )
#define ISALLTRUE(x) ( ((TRGM*)x)->flag & ALLISTRUE )
#define CALCGTSIZE(flag, len) ( TRGMHRDSIZE + ( ( (flag) & ARRKEY ) ? ((len)*sizeof(trgm)) : (((flag) & ALLISTRUE) ? 0 : SIGLEN) ) )
#define GETSIGN(x) ( (BITVECP)( (char*)x+TRGMHRDSIZE ) )
#define GETARR(x) ( (trgm*)( (char*)x+TRGMHRDSIZE ) )
#define GETSIGN(x) ( (BITVECP)( (char*)x+TRGMHRDSIZE ) )
#define GETARR(x) ( (trgm*)( (char*)x+TRGMHRDSIZE ) )
#define ARRNELEM(x) ( ( ((TRGM*)x)->len - TRGMHRDSIZE )/sizeof(trgm) )
extern float4 trgm_limit;
TRGM* generate_trgm(char *str, int slen);
float4 cnt_sml(TRGM *trg1, TRGM *trg2);
TRGM *generate_trgm(char *str, int slen);
float4 cnt_sml(TRGM * trg1, TRGM * trg2);
#endif

View File

@ -71,12 +71,13 @@ makesign(BITVECP sign, TRGM * a)
int4 k,
len = ARRNELEM(a);
trgm *ptr = GETARR(a);
int4 tmp=0;
int4 tmp = 0;
MemSet((void *) sign, 0, sizeof(BITVEC));
SETBIT(sign, SIGLENBIT); /*set last unused bit*/
for (k = 0; k < len; k++) {
CPTRGM( ((char*)&tmp), ptr+k );
SETBIT(sign, SIGLENBIT); /* set last unused bit */
for (k = 0; k < len; k++)
{
CPTRGM(((char *) &tmp), ptr + k);
HASH(sign, tmp);
}
}
@ -89,7 +90,7 @@ gtrgm_compress(PG_FUNCTION_ARGS)
if (entry->leafkey)
{ /* trgm */
TRGM *res;
TRGM *res;
text *toastedval = (text *) DatumGetPointer(entry->key);
text *val = (text *) DatumGetPointer(PG_DETOAST_DATUM(entry->key));
@ -107,7 +108,7 @@ gtrgm_compress(PG_FUNCTION_ARGS)
{
int4 i,
len;
TRGM *res;
TRGM *res;
BITVECP sign = GETSIGN(DatumGetPointer(entry->key));
LOOPBYTE(
@ -137,37 +138,45 @@ gtrgm_decompress(PG_FUNCTION_ARGS)
Datum
gtrgm_consistent(PG_FUNCTION_ARGS)
{
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;
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 */
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 */
res = true;
} else { /* non-leaf contains signature */
int4 count=0;
int4 k, len = ARRNELEM(qtrg);
}
else
{ /* non-leaf contains signature */
int4 count = 0;
int4 k,
len = ARRNELEM(qtrg);
trgm *ptr = GETARR(qtrg);
BITVECP sign = GETSIGN(key);
int4 tmp=0;
BITVECP sign = GETSIGN(key);
int4 tmp = 0;
for (k = 0; k < len; k++) {
CPTRGM( ((char*)&tmp), ptr+k );
for (k = 0; k < len; k++)
{
CPTRGM(((char *) &tmp), ptr + k);
count += GETBIT(sign, HASHVAL(tmp));
}
#ifdef DIVUNION
res = ( len==count ) ? true : ( ( ( ( ((float4)count) / ((float4)(len-count)) ) ) >= trgm_limit ) ? true : false );
res = (len == count) ? true : ((((((float4) count) / ((float4) (len - count)))) >= trgm_limit) ? true : false);
#else
res = (len==0) ? false : ( ( ( ( ((float4)count) / ((float4)len) ) ) >= trgm_limit ) ? true : false );
res = (len == 0) ? false : ((((((float4) count) / ((float4) len))) >= trgm_limit) ? true : false);
#endif
}
PG_FREE_IF_COPY(query,1);
pfree(qtrg);
PG_FREE_IF_COPY(query, 1);
pfree(qtrg);
PG_RETURN_BOOL(res);
}
@ -191,10 +200,11 @@ unionkey(BITVECP sbase, TRGM * add)
else
{
trgm *ptr = GETARR(add);
int4 tmp=0;
int4 tmp = 0;
for (i = 0; i < ARRNELEM(add); i++) {
CPTRGM( ((char*)&tmp), ptr+i );
for (i = 0; i < ARRNELEM(add); i++)
{
CPTRGM(((char *) &tmp), ptr + i);
HASH(sbase, tmp);
}
}
@ -205,13 +215,13 @@ unionkey(BITVECP sbase, TRGM * add)
Datum
gtrgm_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
int4 len = entryvec->n;
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;
TRGM *result;
TRGM *result;
MemSet((void *) base, 0, sizeof(BITVEC));
for (i = 0; i < len; i++)
@ -237,8 +247,8 @@ gtrgm_union(PG_FUNCTION_ARGS)
Datum
gtrgm_same(PG_FUNCTION_ARGS)
{
TRGM *a = (TRGM *) PG_GETARG_POINTER(0);
TRGM *b = (TRGM *) PG_GETARG_POINTER(1);
TRGM *a = (TRGM *) PG_GETARG_POINTER(0);
TRGM *b = (TRGM *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
if (ISSIGNKEY(a))
@ -280,7 +290,7 @@ gtrgm_same(PG_FUNCTION_ARGS)
*result = true;
for (i = 0; i < lena; i++)
if (CMPTRGM(ptra+i, ptrb+i))
if (CMPTRGM(ptra + i, ptrb + i))
{
*result = false;
break;
@ -298,34 +308,39 @@ sizebitvec(BITVECP sign)
i;
LOOPBYTE(
size += SUMBIT(*(char *) sign);
sign = (BITVECP) (((char *) sign) + 1);
size += SUMBIT(*(char *) sign);
sign = (BITVECP) (((char *) sign) + 1);
);
return size;
}
static int
hemdistsign(BITVECP a, BITVECP b) {
int i,dist=0;
hemdistsign(BITVECP a, BITVECP b)
{
int i,
dist = 0;
LOOPBIT(
if ( GETBIT(a,i) != GETBIT(b,i) )
if (GETBIT(a, i) != GETBIT(b, i))
dist++;
);
return dist;
}
static int
hemdist(TRGM *a, TRGM *b) {
if ( ISALLTRUE(a) ) {
hemdist(TRGM * a, TRGM * b)
{
if (ISALLTRUE(a))
{
if (ISALLTRUE(b))
return 0;
else
return SIGLENBIT-sizebitvec(GETSIGN(b));
} else if (ISALLTRUE(b))
return SIGLENBIT-sizebitvec(GETSIGN(a));
return SIGLENBIT - sizebitvec(GETSIGN(b));
}
else if (ISALLTRUE(b))
return SIGLENBIT - sizebitvec(GETSIGN(a));
return hemdistsign( GETSIGN(a), GETSIGN(b) );
return hemdistsign(GETSIGN(a), GETSIGN(b));
}
Datum
@ -334,23 +349,25 @@ 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);
TRGM *origval = (TRGM *) DatumGetPointer(origentry->key);
TRGM *newval = (TRGM *) DatumGetPointer(newentry->key);
TRGM *origval = (TRGM *) DatumGetPointer(origentry->key);
TRGM *newval = (TRGM *) DatumGetPointer(newentry->key);
BITVECP orig = GETSIGN(origval);
*penalty = 0.0;
if (ISARRKEY(newval)) {
BITVEC sign;
if (ISARRKEY(newval))
{
BITVEC sign;
makesign(sign, newval);
if ( ISALLTRUE(origval) )
*penalty=((float)(SIGLENBIT-sizebitvec(sign)))/(float)(SIGLENBIT+1);
else
*penalty=hemdistsign(sign,orig);
} else {
*penalty=hemdist(origval,newval);
if (ISALLTRUE(origval))
*penalty = ((float) (SIGLENBIT - sizebitvec(sign))) / (float) (SIGLENBIT + 1);
else
*penalty = hemdistsign(sign, orig);
}
else
*penalty = hemdist(origval, newval);
PG_RETURN_POINTER(penalty);
}
@ -390,27 +407,30 @@ comparecost(const void *a, const void *b)
static int
hemdistcache(CACHESIGN *a, CACHESIGN *b) {
if ( a->allistrue ) {
hemdistcache(CACHESIGN * a, CACHESIGN * b)
{
if (a->allistrue)
{
if (b->allistrue)
return 0;
else
return SIGLENBIT-sizebitvec(b->sign);
} else if (b->allistrue)
return SIGLENBIT-sizebitvec(a->sign);
return SIGLENBIT - sizebitvec(b->sign);
}
else if (b->allistrue)
return SIGLENBIT - sizebitvec(a->sign);
return hemdistsign( a->sign, b->sign );
return hemdistsign(a->sign, b->sign);
}
Datum
gtrgm_picksplit(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
OffsetNumber maxoff = entryvec->n - 2;
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
OffsetNumber maxoff = entryvec->n - 2;
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
OffsetNumber k,
j;
TRGM *datum_l,
TRGM *datum_l,
*datum_r;
BITVECP union_l,
union_r;
@ -435,13 +455,16 @@ gtrgm_picksplit(PG_FUNCTION_ARGS)
cache = (CACHESIGN *) palloc(sizeof(CACHESIGN) * (maxoff + 2));
fillcache(&cache[FirstOffsetNumber], GETENTRY(entryvec, FirstOffsetNumber));
for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k)) {
for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j)) {
for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
{
for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
{
if (k == FirstOffsetNumber)
fillcache(&cache[j], GETENTRY(entryvec, j));
size_waste=hemdistcache(&(cache[j]),&(cache[k]));
if (size_waste > waste) {
size_waste = hemdistcache(&(cache[j]), &(cache[k]));
if (size_waste > waste)
{
waste = size_waste;
seed_1 = k;
seed_2 = j;
@ -454,101 +477,124 @@ gtrgm_picksplit(PG_FUNCTION_ARGS)
right = v->spl_right;
v->spl_nright = 0;
if (seed_1 == 0 || seed_2 == 0) {
if (seed_1 == 0 || seed_2 == 0)
{
seed_1 = 1;
seed_2 = 2;
}
/* form initial .. */
if (cache[seed_1].allistrue) {
if (cache[seed_1].allistrue)
{
datum_l = (TRGM *) palloc(CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
datum_l->len = CALCGTSIZE(SIGNKEY | ALLISTRUE, 0);
datum_l->flag = SIGNKEY | ALLISTRUE;
} else {
}
else
{
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));
}
if (cache[seed_2].allistrue) {
if (cache[seed_2].allistrue)
{
datum_r = (TRGM *) palloc(CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
datum_r->len = CALCGTSIZE(SIGNKEY | ALLISTRUE, 0);
datum_r->flag = SIGNKEY | ALLISTRUE;
} else {
}
else
{
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));
}
union_l=GETSIGN(datum_l);
union_r=GETSIGN(datum_r);
union_l = GETSIGN(datum_l);
union_r = GETSIGN(datum_r);
maxoff = OffsetNumberNext(maxoff);
fillcache(&cache[maxoff], GETENTRY(entryvec, maxoff));
/* sort before ... */
costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j)) {
for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
{
costvector[j - 1].pos = j;
size_alpha = hemdistcache(&(cache[seed_1]), &(cache[j]));
size_beta = hemdistcache(&(cache[seed_2]), &(cache[j]));
size_beta = hemdistcache(&(cache[seed_2]), &(cache[j]));
costvector[j - 1].cost = abs(size_alpha - size_beta);
}
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
for (k = 0; k < maxoff; k++) {
for (k = 0; k < maxoff; k++)
{
j = costvector[k].pos;
if (j == seed_1) {
if (j == seed_1)
{
*left++ = j;
v->spl_nleft++;
continue;
} else if (j == seed_2) {
}
else if (j == seed_2)
{
*right++ = j;
v->spl_nright++;
continue;
}
if (ISALLTRUE(datum_l) || cache[j].allistrue) {
if ( ISALLTRUE(datum_l) && cache[j].allistrue )
size_alpha=0;
if (ISALLTRUE(datum_l) || cache[j].allistrue)
{
if (ISALLTRUE(datum_l) && cache[j].allistrue)
size_alpha = 0;
else
size_alpha = SIGLENBIT-sizebitvec(
( cache[j].allistrue ) ? GETSIGN(datum_l) : GETSIGN(cache[j].sign)
);
} else {
size_alpha=hemdistsign(cache[j].sign,GETSIGN(datum_l));
size_alpha = SIGLENBIT - sizebitvec(
(cache[j].allistrue) ? GETSIGN(datum_l) : GETSIGN(cache[j].sign)
);
}
else
size_alpha = hemdistsign(cache[j].sign, GETSIGN(datum_l));
if (ISALLTRUE(datum_r) || cache[j].allistrue) {
if ( ISALLTRUE(datum_r) && cache[j].allistrue )
size_beta=0;
if (ISALLTRUE(datum_r) || cache[j].allistrue)
{
if (ISALLTRUE(datum_r) && cache[j].allistrue)
size_beta = 0;
else
size_beta = SIGLENBIT-sizebitvec(
( cache[j].allistrue ) ? GETSIGN(datum_r) : GETSIGN(cache[j].sign)
);
} else {
size_beta=hemdistsign(cache[j].sign,GETSIGN(datum_r));
size_beta = SIGLENBIT - sizebitvec(
(cache[j].allistrue) ? GETSIGN(datum_r) : GETSIGN(cache[j].sign)
);
}
else
size_beta = hemdistsign(cache[j].sign, GETSIGN(datum_r));
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) )
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))
MemSet((void *) GETSIGN(datum_l), 0xff, sizeof(BITVEC));
} else {
ptr=cache[j].sign;
}
else
{
ptr = cache[j].sign;
LOOPBYTE(
union_l[i] |= ptr[i];
union_l[i] |= ptr[i];
);
}
*left++ = j;
v->spl_nleft++;
} else {
if (ISALLTRUE(datum_r) || cache[j].allistrue) {
if (! ISALLTRUE(datum_r) )
}
else
{
if (ISALLTRUE(datum_r) || cache[j].allistrue)
{
if (!ISALLTRUE(datum_r))
MemSet((void *) GETSIGN(datum_r), 0xff, sizeof(BITVEC));
} else {
ptr=cache[j].sign;
}
else
{
ptr = cache[j].sign;
LOOPBYTE(
union_r[i] |= ptr[i];
union_r[i] |= ptr[i];
);
}
*right++ = j;

View File

@ -3,136 +3,162 @@
#include "utils/array.h"
#include "catalog/pg_type.h"
float4 trgm_limit = 0.3;
float4 trgm_limit = 0.3;
PG_FUNCTION_INFO_V1(set_limit);
Datum set_limit(PG_FUNCTION_ARGS);
Datum set_limit(PG_FUNCTION_ARGS);
Datum
set_limit(PG_FUNCTION_ARGS) {
float4 nlimit = PG_GETARG_FLOAT4(0);
if ( nlimit < 0 || nlimit > 1.0 )
elog(ERROR,"Wrong limit, should be between 0 and 1");
set_limit(PG_FUNCTION_ARGS)
{
float4 nlimit = PG_GETARG_FLOAT4(0);
if (nlimit < 0 || nlimit > 1.0)
elog(ERROR, "Wrong limit, should be between 0 and 1");
trgm_limit = nlimit;
PG_RETURN_FLOAT4(trgm_limit);
}
PG_FUNCTION_INFO_V1(show_limit);
Datum show_limit(PG_FUNCTION_ARGS);
Datum show_limit(PG_FUNCTION_ARGS);
Datum
show_limit(PG_FUNCTION_ARGS) {
show_limit(PG_FUNCTION_ARGS)
{
PG_RETURN_FLOAT4(trgm_limit);
}
#define WORDWAIT 0
#define INWORD 1
#define WORDWAIT 0
#define INWORD 1
static int
comp_trgm(const void *a, const void *b) {
return CMPTRGM(a,b);
comp_trgm(const void *a, const void *b)
{
return CMPTRGM(a, b);
}
static int
unique_array (trgm *a, int len) {
trgm *curend, *tmp;
unique_array(trgm * a, int len)
{
trgm *curend,
*tmp;
curend = tmp = a;
while (tmp - a < len)
if ( CMPTRGM(tmp, curend) ) {
if (CMPTRGM(tmp, curend))
{
curend++;
CPTRGM(curend,tmp);
CPTRGM(curend, tmp);
tmp++;
} else
}
else
tmp++;
return curend + 1 - a;
}
TRGM*
generate_trgm(char *str, int slen) {
TRGM* trg;
char *buf,*sptr,*bufptr;
trgm *tptr;
int state=WORDWAIT;
int wl,len;
TRGM *
generate_trgm(char *str, int slen)
{
TRGM *trg;
char *buf,
*sptr,
*bufptr;
trgm *tptr;
int state = WORDWAIT;
int wl,
len;
trg = (TRGM*) palloc(TRGMHRDSIZE+sizeof(trgm) * (slen/2 + 1) * 3);
trg = (TRGM *) palloc(TRGMHRDSIZE + sizeof(trgm) * (slen / 2 + 1) * 3);
trg->flag = ARRKEY;
trg->len = TRGMHRDSIZE;
if ( slen+LPADDING+RPADDING<3 || slen == 0 )
if (slen + LPADDING + RPADDING < 3 || slen == 0)
return trg;
tptr = GETARR(trg);
buf = palloc(sizeof(char) * (slen+4));
buf = palloc(sizeof(char) * (slen + 4));
sptr = str;
if ( LPADDING > 0 ) {
if (LPADDING > 0)
{
*buf = ' ';
if ( LPADDING > 1 )
*(buf+1) = ' ';
if (LPADDING > 1)
*(buf + 1) = ' ';
}
bufptr = buf+LPADDING;
while( sptr-str < slen ) {
if ( state == WORDWAIT ) {
if (
#ifdef KEEPONLYALNUM
isalnum((unsigned char)*sptr)
#else
!isspace( (unsigned char)*sptr )
#endif
) {
*bufptr = *sptr; /* start put word in buffer */
bufptr++;
state = INWORD;
if ( sptr-str == slen-1 /* last char */ )
goto gettrg;
}
} else {
bufptr = buf + LPADDING;
while (sptr - str < slen)
{
if (state == WORDWAIT)
{
if (
#ifdef KEEPONLYALNUM
!isalnum((unsigned char)*sptr)
isalnum((unsigned char) *sptr)
#else
isspace( (unsigned char)*sptr )
!isspace((unsigned char) *sptr)
#endif
) {
gettrg:
)
{
*bufptr = *sptr; /* start put word in buffer */
bufptr++;
state = INWORD;
if (sptr - str == slen - 1 /* last char */ )
goto gettrg;
}
}
else
{
if (
#ifdef KEEPONLYALNUM
!isalnum((unsigned char) *sptr)
#else
isspace((unsigned char) *sptr)
#endif
)
{
gettrg:
/* word in buffer, so count trigrams */
*bufptr = ' ';
*(bufptr+1) = ' ';
wl = bufptr - (buf+LPADDING) - 2 + LPADDING + RPADDING;
if ( wl<=0 ) {
bufptr = buf+LPADDING;
*(bufptr + 1) = ' ';
wl = bufptr - (buf + LPADDING) - 2 + LPADDING + RPADDING;
if (wl <= 0)
{
bufptr = buf + LPADDING;
state = WORDWAIT;
sptr++;
continue;
}
#ifdef IGNORECASE
do { /* lower word */
int wwl = bufptr-buf;
bufptr = buf+LPADDING;
while( bufptr-buf < wwl ) {
*bufptr = tolower( (unsigned char) *bufptr );
do
{ /* lower word */
int wwl = bufptr - buf;
bufptr = buf + LPADDING;
while (bufptr - buf < wwl)
{
*bufptr = tolower((unsigned char) *bufptr);
bufptr++;
}
} while(0);
} while (0);
#endif
bufptr = buf;
/* set trigrams */
while( bufptr-buf < wl ) {
while (bufptr - buf < wl)
{
CPTRGM(tptr, bufptr);
bufptr++;
tptr++;
}
bufptr = buf+LPADDING;
bufptr = buf + LPADDING;
state = WORDWAIT;
} else {
*bufptr = *sptr; /* put in buffer */
}
else
{
*bufptr = *sptr; /* put in buffer */
bufptr++;
if ( sptr-str == slen-1 )
if (sptr - str == slen - 1)
goto gettrg;
}
}
@ -141,13 +167,14 @@ gettrg:
pfree(buf);
if ( (len=tptr-GETARR(trg)) == 0 )
if ((len = tptr - GETARR(trg)) == 0)
return trg;
if ( len>0 ) {
qsort( (void*)GETARR(trg), len, sizeof(trgm), comp_trgm );
len = unique_array( GETARR(trg), len );
}
if (len > 0)
{
qsort((void *) GETARR(trg), len, sizeof(trgm), comp_trgm);
len = unique_array(GETARR(trg), len);
}
trg->len = CALCGTSIZE(ARRKEY, len);
@ -156,68 +183,78 @@ gettrg:
PG_FUNCTION_INFO_V1(show_trgm);
Datum show_trgm(PG_FUNCTION_ARGS);
Datum show_trgm(PG_FUNCTION_ARGS);
Datum
show_trgm(PG_FUNCTION_ARGS) {
text *in = PG_GETARG_TEXT_P(0);
TRGM *trg;
Datum *d;
ArrayType *a;
trgm *ptr;
show_trgm(PG_FUNCTION_ARGS)
{
text *in = PG_GETARG_TEXT_P(0);
TRGM *trg;
Datum *d;
ArrayType *a;
trgm *ptr;
trg = generate_trgm(VARDATA(in), VARSIZE(in) - VARHDRSZ);
d = (Datum*)palloc( sizeof(Datum)*(1+ARRNELEM(trg)) );
d = (Datum *) palloc(sizeof(Datum) * (1 + ARRNELEM(trg)));
ptr = GETARR(trg);
while( ptr-GETARR(trg) < ARRNELEM(trg) ) {
text *item=(text*)palloc(VARHDRSZ + 3);
VARATT_SIZEP(item) = VARHDRSZ+3;
while (ptr - GETARR(trg) < ARRNELEM(trg))
{
text *item = (text *) palloc(VARHDRSZ + 3);
VARATT_SIZEP(item) = VARHDRSZ + 3;
CPTRGM(VARDATA(item), ptr);
d[ ptr-GETARR(trg) ] = PointerGetDatum(item);
d[ptr - GETARR(trg)] = PointerGetDatum(item);
ptr++;
}
a = construct_array(
d,
ARRNELEM(trg),
TEXTOID,
-1,
false,
'i'
);
d,
ARRNELEM(trg),
TEXTOID,
-1,
false,
'i'
);
ptr = GETARR(trg);
while( ptr-GETARR(trg) < ARRNELEM(trg) ) {
pfree(DatumGetPointer(d[ ptr-GETARR(trg) ]));
while (ptr - GETARR(trg) < ARRNELEM(trg))
{
pfree(DatumGetPointer(d[ptr - GETARR(trg)]));
ptr++;
}
pfree(d);
pfree(trg);
PG_FREE_IF_COPY(in,0);
PG_FREE_IF_COPY(in, 0);
PG_RETURN_POINTER(a);
}
float4
cnt_sml(TRGM *trg1, TRGM *trg2) {
trgm *ptr1, *ptr2;
int count=0;
int len1, len2;
cnt_sml(TRGM * trg1, TRGM * trg2)
{
trgm *ptr1,
*ptr2;
int count = 0;
int len1,
len2;
ptr1 = GETARR(trg1);
ptr2 = GETARR(trg2);
len1 = ARRNELEM(trg1);
len2 = ARRNELEM(trg2);
while( ptr1 - GETARR(trg1) < len1 && ptr2 - GETARR(trg2) < len2 ) {
int res = CMPTRGM(ptr1,ptr2);
if ( res < 0 ) {
while (ptr1 - GETARR(trg1) < len1 && ptr2 - GETARR(trg2) < len2)
{
int res = CMPTRGM(ptr1, ptr2);
if (res < 0)
ptr1++;
} else if ( res > 0 ) {
else if (res > 0)
ptr2++;
} else {
else
{
ptr1++;
ptr2++;
count++;
@ -225,45 +262,47 @@ cnt_sml(TRGM *trg1, TRGM *trg2) {
}
#ifdef DIVUNION
return ( ( ((float4)count) / ((float4)(len1+len2-count)) ) );
return ((((float4) count) / ((float4) (len1 + len2 - count))));
#else
return ( ((float)count) / ((float)( (len1>len2) ? len1 : len2 )) );
return (((float) count) / ((float) ((len1 > len2) ? len1 : len2)));
#endif
}
PG_FUNCTION_INFO_V1(similarity);
Datum similarity(PG_FUNCTION_ARGS);
Datum similarity(PG_FUNCTION_ARGS);
Datum
similarity(PG_FUNCTION_ARGS) {
text *in1 = PG_GETARG_TEXT_P(0);
text *in2 = PG_GETARG_TEXT_P(1);
TRGM *trg1, *trg2;
float4 res;
similarity(PG_FUNCTION_ARGS)
{
text *in1 = PG_GETARG_TEXT_P(0);
text *in2 = PG_GETARG_TEXT_P(1);
TRGM *trg1,
*trg2;
float4 res;
trg1 = generate_trgm(VARDATA(in1), VARSIZE(in1) - VARHDRSZ);
trg2 = generate_trgm(VARDATA(in2), VARSIZE(in2) - VARHDRSZ);
res = cnt_sml(trg1,trg2);
res = cnt_sml(trg1, trg2);
pfree(trg1);
pfree(trg2);
PG_FREE_IF_COPY(in1,0);
PG_FREE_IF_COPY(in2,1);
PG_FREE_IF_COPY(in1, 0);
PG_FREE_IF_COPY(in2, 1);
PG_RETURN_FLOAT4(res);
}
PG_FUNCTION_INFO_V1(similarity_op);
Datum similarity_op(PG_FUNCTION_ARGS);
Datum similarity_op(PG_FUNCTION_ARGS);
Datum
similarity_op(PG_FUNCTION_ARGS) {
float4 res=DatumGetFloat4( DirectFunctionCall2(
similarity,
PG_GETARG_DATUM(0),
PG_GETARG_DATUM(1)
) );
PG_RETURN_BOOL( res >= trgm_limit );
similarity_op(PG_FUNCTION_ARGS)
{
float4 res = DatumGetFloat4(DirectFunctionCall2(
similarity,
PG_GETARG_DATUM(0),
PG_GETARG_DATUM(1)
));
PG_RETURN_BOOL(res >= trgm_limit);
}

View File

@ -1,5 +1,5 @@
/*
* $PostgreSQL: pgsql/contrib/pgbench/pgbench.c,v 1.31 2004/06/14 11:00:12 ishii Exp $
* $PostgreSQL: pgsql/contrib/pgbench/pgbench.c,v 1.32 2004/08/29 05:06:36 momjian Exp $
*
* pgbench: a simple TPC-B like benchmark program for PostgreSQL
* written by Tatsuo Ishii
@ -261,7 +261,7 @@ doOne(CState * state, int n, int debug, int ttype)
*/
if (use_log)
{
double diff;
double diff;
struct timeval now;
gettimeofday(&now, 0);
@ -492,7 +492,7 @@ init(void)
static char *DDLAFTERs[] = {
"alter table branches add primary key (bid)",
"alter table tellers add primary key (tid)",
"alter table accounts add primary key (aid)"};
"alter table accounts add primary key (aid)"};
char sql[256];

View File

@ -1,5 +1,5 @@
/*
* $PostgreSQL: pgsql/contrib/pgstattuple/pgstattuple.c,v 1.15 2004/05/08 19:09:24 tgl Exp $
* $PostgreSQL: pgsql/contrib/pgstattuple/pgstattuple.c,v 1.16 2004/08/29 05:06:37 momjian Exp $
*
* Copyright (c) 2001,2002 Tatsuo Ishii
*
@ -129,7 +129,7 @@ pgstattuple_real(Relation rel)
scan = heap_beginscan(rel, SnapshotAny, 0, NULL);
nblocks = scan->rs_nblocks; /* # blocks to be scanned */
nblocks = scan->rs_nblocks; /* # blocks to be scanned */
/* scan the relation */
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
@ -189,9 +189,9 @@ pgstattuple_real(Relation rel)
}
/*
* Prepare a values array for constructing the tuple. This should be an
* array of C strings which will be processed later by the appropriate
* "in" functions.
* Prepare a values array for constructing the tuple. This should be
* an array of C strings which will be processed later by the
* appropriate "in" functions.
*/
values = (char **) palloc(NCOLUMNS * sizeof(char *));
for (i = 0; i < NCOLUMNS; i++)

View File

@ -7,7 +7,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/contrib/rtree_gist/rtree_gist.c,v 1.9 2004/03/30 15:45:33 teodor Exp $
* $PostgreSQL: pgsql/contrib/rtree_gist/rtree_gist.c,v 1.10 2004/08/29 05:06:37 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@ -98,7 +98,7 @@ gbox_consistent(PG_FUNCTION_ARGS)
Datum
gbox_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
int *sizep = (int *) PG_GETARG_POINTER(1);
int numranges,
i;
@ -186,7 +186,7 @@ compare_KB(const void *a, const void *b)
Datum
gbox_picksplit(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
OffsetNumber i;
OffsetNumber *listL,

View File

@ -240,7 +240,7 @@ gseg_union(GistEntryVector *entryvec, int *sizep)
for (i = 1; i < numranges; i++)
{
out = gseg_binary_union(tmp, (SEG *)
DatumGetPointer(entryvec->vector[i].key),
DatumGetPointer(entryvec->vector[i].key),
sizep);
if (i > 1)
pfree(tmp);
@ -755,8 +755,8 @@ seg_cmp(SEG * a, SEG * b)
* a->lower == b->lower, so consider type of boundary.
*
* A '-' lower bound is < any other kind (this could only be relevant if
* -HUGE_VAL is used as a regular data value). A '<' lower bound is < any
* other kind except '-'. A '>' lower bound is > any other kind.
* -HUGE_VAL is used as a regular data value). A '<' lower bound is <
* any other kind except '-'. A '>' lower bound is > any other kind.
*/
if (a->l_ext != b->l_ext)
{
@ -813,8 +813,8 @@ seg_cmp(SEG * a, SEG * b)
* a->upper == b->upper, so consider type of boundary.
*
* A '-' upper bound is > any other kind (this could only be relevant if
* HUGE_VAL is used as a regular data value). A '<' upper bound is < any
* other kind. A '>' upper bound is > any other kind except '-'.
* HUGE_VAL is used as a regular data value). A '<' upper bound is <
* any other kind. A '>' upper bound is > any other kind except '-'.
*/
if (a->u_ext != b->u_ext)
{

View File

@ -309,7 +309,7 @@ timetravel(PG_FUNCTION_ARGS)
void *pplan;
Oid *ctypes;
char sql[8192];
char separ=' ';
char separ = ' ';
/* allocate ctypes for preparation */
ctypes = (Oid *) palloc(natts * sizeof(Oid));
@ -323,8 +323,8 @@ timetravel(PG_FUNCTION_ARGS)
ctypes[i - 1] = SPI_gettypeid(tupdesc, i);
if (!(tupdesc->attrs[i - 1]->attisdropped)) /* skip dropped columns */
{
snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), "%c$%d", separ,i);
separ = ',';
snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), "%c$%d", separ, i);
separ = ',';
}
}
snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), ")");

View File

@ -875,7 +875,7 @@ get_crosstab_tuplestore(char *sql,
/* no qualifying category tuples */
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("provided \"categories\" SQL must " \
errmsg("provided \"categories\" SQL must " \
"return 1 column of at least one row")));
}

View File

@ -326,10 +326,11 @@ unionkey(BITVECP sbase, GISTTYPE * add)
Datum
gtxtidx_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
int *size = (int *) PG_GETARG_POINTER(1);
BITVEC base;
int4 i,len;
int4 i,
len;
int4 flag = 0;
GISTTYPE *result;
@ -512,7 +513,7 @@ comparecost(const void *a, const void *b)
Datum
gtxtidx_picksplit(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
OffsetNumber k,
j;

View File

@ -241,16 +241,20 @@ pushval_morph(QPRS_STATE * state, int typeval, char *strval, int lenval)
lemm = lemmatize(token, &lenlemm, type);
if (lemm)
{
if ( lemm==token ) {
char *ptrs=token,*ptrd;
ptrd = lemm = palloc(lenlemm+1);
while(ptrs-token<lenlemm) {
if (lemm == token)
{
char *ptrs = token,
*ptrd;
ptrd = lemm = palloc(lenlemm + 1);
while (ptrs - token < lenlemm)
{
*ptrd = tolower((unsigned char) *ptrs);
ptrs++;
ptrd++;
}
*ptrd='\0';
}
*ptrd = '\0';
}
pushval_asis(state, VAL, lemm, lenlemm);
pfree(lemm);
}

View File

@ -21,7 +21,7 @@
#include "dict.h"
Oid TSNSP_FunctionOid = InvalidOid;
Oid TSNSP_FunctionOid = InvalidOid;
text *
@ -121,44 +121,45 @@ text_cmp(text *a, text *b)
}
char*
get_namespace(Oid funcoid) {
HeapTuple tuple;
Form_pg_proc proc;
Form_pg_namespace nsp;
Oid nspoid;
char *txt;
char *
get_namespace(Oid funcoid)
{
HeapTuple tuple;
Form_pg_proc proc;
Form_pg_namespace nsp;
Oid nspoid;
char *txt;
tuple = SearchSysCache(PROCOID, ObjectIdGetDatum(funcoid), 0, 0, 0);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "cache lookup failed for proc oid %u", funcoid);
proc=(Form_pg_proc) GETSTRUCT(tuple);
nspoid = proc->pronamespace;
ReleaseSysCache(tuple);
tuple = SearchSysCache(PROCOID, ObjectIdGetDatum(funcoid), 0, 0, 0);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "cache lookup failed for proc oid %u", funcoid);
proc = (Form_pg_proc) GETSTRUCT(tuple);
nspoid = proc->pronamespace;
ReleaseSysCache(tuple);
tuple = SearchSysCache(NAMESPACEOID, ObjectIdGetDatum(nspoid), 0, 0, 0);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "cache lookup failed for namespace oid %u", nspoid);
nsp = (Form_pg_namespace) GETSTRUCT(tuple);
txt = pstrdup( NameStr((nsp->nspname)) );
ReleaseSysCache(tuple);
tuple = SearchSysCache(NAMESPACEOID, ObjectIdGetDatum(nspoid), 0, 0, 0);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "cache lookup failed for namespace oid %u", nspoid);
nsp = (Form_pg_namespace) GETSTRUCT(tuple);
txt = pstrdup(NameStr((nsp->nspname)));
ReleaseSysCache(tuple);
return txt;
return txt;
}
Oid
get_oidnamespace(Oid funcoid) {
HeapTuple tuple;
Form_pg_proc proc;
Oid nspoid;
get_oidnamespace(Oid funcoid)
{
HeapTuple tuple;
Form_pg_proc proc;
Oid nspoid;
tuple = SearchSysCache(PROCOID, ObjectIdGetDatum(funcoid), 0, 0, 0);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "cache lookup failed for proc oid %u", funcoid);
proc=(Form_pg_proc) GETSTRUCT(tuple);
nspoid = proc->pronamespace;
ReleaseSysCache(tuple);
tuple = SearchSysCache(PROCOID, ObjectIdGetDatum(funcoid), 0, 0, 0);
if (!HeapTupleIsValid(tuple))
elog(ERROR, "cache lookup failed for proc oid %u", funcoid);
proc = (Form_pg_proc) GETSTRUCT(tuple);
nspoid = proc->pronamespace;
ReleaseSysCache(tuple);
return nspoid;
return nspoid;
}

View File

@ -21,13 +21,14 @@ int text_cmp(text *a, text *b);
void ts_error(int state, const char *format,...);
extern Oid TSNSP_FunctionOid; /* oid of called function, needed only for determ namespace, no more */
char* get_namespace(Oid funcoid);
Oid get_oidnamespace(Oid funcoid);
extern Oid TSNSP_FunctionOid; /* oid of called function, needed only for
* determ namespace, no more */
char *get_namespace(Oid funcoid);
Oid get_oidnamespace(Oid funcoid);
#define SET_FUNCOID() do { \
if ( fcinfo->flinfo && fcinfo->flinfo->fn_oid != InvalidOid ) \
TSNSP_FunctionOid = fcinfo->flinfo->fn_oid; \
#define SET_FUNCOID() do { \
if ( fcinfo->flinfo && fcinfo->flinfo->fn_oid != InvalidOid ) \
TSNSP_FunctionOid = fcinfo->flinfo->fn_oid; \
} while(0)
#endif

View File

@ -26,18 +26,18 @@ init_dict(Oid id, DictInfo * dict)
bool isnull;
Datum pars[1];
int stat;
void *plan;
char buf[1024];
char *nsp = get_namespace(TSNSP_FunctionOid);
void *plan;
char buf[1024];
char *nsp = get_namespace(TSNSP_FunctionOid);
arg[0] = OIDOID;
pars[0] = ObjectIdGetDatum(id);
memset(dict, 0, sizeof(DictInfo));
SPI_connect();
sprintf(buf,"select dict_init, dict_initoption, dict_lexize from %s.pg_ts_dict where oid = $1", nsp);
sprintf(buf, "select dict_init, dict_initoption, dict_lexize from %s.pg_ts_dict where oid = $1", nsp);
pfree(nsp);
plan= SPI_prepare(buf, 1, arg);
plan = SPI_prepare(buf, 1, arg);
if (!plan)
ts_error(ERROR, "SPI_prepare() failed");
@ -142,8 +142,9 @@ name2id_dict(text *name)
Datum pars[1];
int stat;
Oid id = findSNMap_t(&(DList.name2id_map), name);
void *plan;
char buf[1024], *nsp;
void *plan;
char buf[1024],
*nsp;
arg[0] = TEXTOID;
pars[0] = PointerGetDatum(name);
@ -153,9 +154,9 @@ name2id_dict(text *name)
nsp = get_namespace(TSNSP_FunctionOid);
SPI_connect();
sprintf(buf,"select oid from %s.pg_ts_dict where dict_name = $1", nsp);
sprintf(buf, "select oid from %s.pg_ts_dict where dict_name = $1", nsp);
pfree(nsp);
plan= SPI_prepare(buf, 1, arg);
plan = SPI_prepare(buf, 1, arg);
if (!plan)
ts_error(ERROR, "SPI_prepare() failed");
@ -245,7 +246,8 @@ lexize_byname(PG_FUNCTION_ARGS)
{
text *dictname = PG_GETARG_TEXT_P(0);
Datum res;
SET_FUNCOID();
SET_FUNCOID();
res = DirectFunctionCall3(
lexize,
@ -267,7 +269,7 @@ Datum set_curdict(PG_FUNCTION_ARGS);
Datum
set_curdict(PG_FUNCTION_ARGS)
{
SET_FUNCOID();
SET_FUNCOID();
finddict(PG_GETARG_OID(0));
currect_dictionary_id = PG_GETARG_OID(0);
PG_RETURN_VOID();
@ -279,7 +281,8 @@ Datum
set_curdict_byname(PG_FUNCTION_ARGS)
{
text *dictname = PG_GETARG_TEXT_P(0);
SET_FUNCOID();
SET_FUNCOID();
DirectFunctionCall1(
set_curdict,
ObjectIdGetDatum(name2id_dict(dictname))
@ -294,7 +297,8 @@ Datum
lexize_bycurrent(PG_FUNCTION_ARGS)
{
Datum res;
SET_FUNCOID();
SET_FUNCOID();
if (currect_dictionary_id == 0)
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),

View File

@ -123,8 +123,8 @@ gtsvector_compress(PG_FUNCTION_ARGS)
if (entry->leafkey)
{ /* tsvector */
GISTTYPE *res;
tsvector *toastedval = (tsvector *) DatumGetPointer(entry->key);
tsvector *val = (tsvector *) DatumGetPointer(PG_DETOAST_DATUM(entry->key));
tsvector *toastedval = (tsvector *) DatumGetPointer(entry->key);
tsvector *val = (tsvector *) DatumGetPointer(PG_DETOAST_DATUM(entry->key));
int4 len;
int4 *arr;
WordEntry *ptr = ARRPTR(val);
@ -277,10 +277,10 @@ gtsvector_consistent(PG_FUNCTION_ARGS)
PG_RETURN_BOOL(true);
PG_RETURN_BOOL(TS_execute(
GETQUERY(query),
(void *) GETSIGN(key), false,
checkcondition_bit
));
GETQUERY(query),
(void *) GETSIGN(key), false,
checkcondition_bit
));
}
else
{ /* only leaf pages */
@ -289,10 +289,10 @@ gtsvector_consistent(PG_FUNCTION_ARGS)
chkval.arrb = GETARR(key);
chkval.arre = chkval.arrb + ARRNELEM(key);
PG_RETURN_BOOL(TS_execute(
GETQUERY(query),
(void *) &chkval, true,
checkcondition_arr
));
GETQUERY(query),
(void *) &chkval, true,
checkcondition_arr
));
}
}
@ -326,10 +326,11 @@ unionkey(BITVECP sbase, GISTTYPE * add)
Datum
gtsvector_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
int *size = (int *) PG_GETARG_POINTER(1);
BITVEC base;
int4 i,len;
int4 i,
len;
int4 flag = 0;
GISTTYPE *result;
@ -418,34 +419,39 @@ sizebitvec(BITVECP sign)
i;
LOOPBYTE(
size += SUMBIT(*(char *) sign);
sign = (BITVECP) (((char *) sign) + 1);
size += SUMBIT(*(char *) sign);
sign = (BITVECP) (((char *) sign) + 1);
);
return size;
}
static int
hemdistsign(BITVECP a, BITVECP b) {
int i,dist=0;
hemdistsign(BITVECP a, BITVECP b)
{
int i,
dist = 0;
LOOPBIT(
if ( GETBIT(a,i) != GETBIT(b,i) )
if (GETBIT(a, i) != GETBIT(b, i))
dist++;
);
return dist;
}
static int
hemdist(GISTTYPE *a, GISTTYPE *b) {
if ( ISALLTRUE(a) ) {
hemdist(GISTTYPE * a, GISTTYPE * b)
{
if (ISALLTRUE(a))
{
if (ISALLTRUE(b))
return 0;
else
return SIGLENBIT-sizebitvec(GETSIGN(b));
} else if (ISALLTRUE(b))
return SIGLENBIT-sizebitvec(GETSIGN(a));
return SIGLENBIT - sizebitvec(GETSIGN(b));
}
else if (ISALLTRUE(b))
return SIGLENBIT - sizebitvec(GETSIGN(a));
return hemdistsign( GETSIGN(a), GETSIGN(b) );
return hemdistsign(GETSIGN(a), GETSIGN(b));
}
Datum
@ -460,17 +466,19 @@ gtsvector_penalty(PG_FUNCTION_ARGS)
*penalty = 0.0;
if (ISARRKEY(newval)) {
BITVEC sign;
if (ISARRKEY(newval))
{
BITVEC sign;
makesign(sign, newval);
if ( ISALLTRUE(origval) )
*penalty=((float)(SIGLENBIT-sizebitvec(sign)))/(float)(SIGLENBIT+1);
else
*penalty=hemdistsign(sign,orig);
} else {
*penalty=hemdist(origval,newval);
if (ISALLTRUE(origval))
*penalty = ((float) (SIGLENBIT - sizebitvec(sign))) / (float) (SIGLENBIT + 1);
else
*penalty = hemdistsign(sign, orig);
}
else
*penalty = hemdist(origval, newval);
PG_RETURN_POINTER(penalty);
}
@ -510,22 +518,25 @@ comparecost(const void *a, const void *b)
static int
hemdistcache(CACHESIGN *a, CACHESIGN *b) {
if ( a->allistrue ) {
hemdistcache(CACHESIGN * a, CACHESIGN * b)
{
if (a->allistrue)
{
if (b->allistrue)
return 0;
else
return SIGLENBIT-sizebitvec(b->sign);
} else if (b->allistrue)
return SIGLENBIT-sizebitvec(a->sign);
return SIGLENBIT - sizebitvec(b->sign);
}
else if (b->allistrue)
return SIGLENBIT - sizebitvec(a->sign);
return hemdistsign( a->sign, b->sign );
return hemdistsign(a->sign, b->sign);
}
Datum
gtsvector_picksplit(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
OffsetNumber k,
j;
@ -556,13 +567,16 @@ gtsvector_picksplit(PG_FUNCTION_ARGS)
cache = (CACHESIGN *) palloc(sizeof(CACHESIGN) * (maxoff + 2));
fillcache(&cache[FirstOffsetNumber], GETENTRY(entryvec, FirstOffsetNumber));
for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k)) {
for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j)) {
for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
{
for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
{
if (k == FirstOffsetNumber)
fillcache(&cache[j], GETENTRY(entryvec, j));
size_waste=hemdistcache(&(cache[j]),&(cache[k]));
if (size_waste > waste) {
size_waste = hemdistcache(&(cache[j]), &(cache[k]));
if (size_waste > waste)
{
waste = size_waste;
seed_1 = k;
seed_2 = j;
@ -575,101 +589,124 @@ gtsvector_picksplit(PG_FUNCTION_ARGS)
right = v->spl_right;
v->spl_nright = 0;
if (seed_1 == 0 || seed_2 == 0) {
if (seed_1 == 0 || seed_2 == 0)
{
seed_1 = 1;
seed_2 = 2;
}
/* form initial .. */
if (cache[seed_1].allistrue) {
if (cache[seed_1].allistrue)
{
datum_l = (GISTTYPE *) palloc(CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
datum_l->len = CALCGTSIZE(SIGNKEY | ALLISTRUE, 0);
datum_l->flag = SIGNKEY | ALLISTRUE;
} else {
}
else
{
datum_l = (GISTTYPE *) 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));
}
if (cache[seed_2].allistrue) {
if (cache[seed_2].allistrue)
{
datum_r = (GISTTYPE *) palloc(CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
datum_r->len = CALCGTSIZE(SIGNKEY | ALLISTRUE, 0);
datum_r->flag = SIGNKEY | ALLISTRUE;
} else {
}
else
{
datum_r = (GISTTYPE *) 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));
}
union_l=GETSIGN(datum_l);
union_r=GETSIGN(datum_r);
union_l = GETSIGN(datum_l);
union_r = GETSIGN(datum_r);
maxoff = OffsetNumberNext(maxoff);
fillcache(&cache[maxoff], GETENTRY(entryvec, maxoff));
/* sort before ... */
costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j)) {
for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
{
costvector[j - 1].pos = j;
size_alpha = hemdistcache(&(cache[seed_1]), &(cache[j]));
size_beta = hemdistcache(&(cache[seed_2]), &(cache[j]));
size_beta = hemdistcache(&(cache[seed_2]), &(cache[j]));
costvector[j - 1].cost = abs(size_alpha - size_beta);
}
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
for (k = 0; k < maxoff; k++) {
for (k = 0; k < maxoff; k++)
{
j = costvector[k].pos;
if (j == seed_1) {
if (j == seed_1)
{
*left++ = j;
v->spl_nleft++;
continue;
} else if (j == seed_2) {
}
else if (j == seed_2)
{
*right++ = j;
v->spl_nright++;
continue;
}
if (ISALLTRUE(datum_l) || cache[j].allistrue) {
if ( ISALLTRUE(datum_l) && cache[j].allistrue )
size_alpha=0;
if (ISALLTRUE(datum_l) || cache[j].allistrue)
{
if (ISALLTRUE(datum_l) && cache[j].allistrue)
size_alpha = 0;
else
size_alpha = SIGLENBIT-sizebitvec(
( cache[j].allistrue ) ? GETSIGN(datum_l) : GETSIGN(cache[j].sign)
);
} else {
size_alpha=hemdistsign(cache[j].sign,GETSIGN(datum_l));
size_alpha = SIGLENBIT - sizebitvec(
(cache[j].allistrue) ? GETSIGN(datum_l) : GETSIGN(cache[j].sign)
);
}
else
size_alpha = hemdistsign(cache[j].sign, GETSIGN(datum_l));
if (ISALLTRUE(datum_r) || cache[j].allistrue) {
if ( ISALLTRUE(datum_r) && cache[j].allistrue )
size_beta=0;
if (ISALLTRUE(datum_r) || cache[j].allistrue)
{
if (ISALLTRUE(datum_r) && cache[j].allistrue)
size_beta = 0;
else
size_beta = SIGLENBIT-sizebitvec(
( cache[j].allistrue ) ? GETSIGN(datum_r) : GETSIGN(cache[j].sign)
);
} else {
size_beta=hemdistsign(cache[j].sign,GETSIGN(datum_r));
size_beta = SIGLENBIT - sizebitvec(
(cache[j].allistrue) ? GETSIGN(datum_r) : GETSIGN(cache[j].sign)
);
}
else
size_beta = hemdistsign(cache[j].sign, GETSIGN(datum_r));
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) )
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))
MemSet((void *) GETSIGN(datum_l), 0xff, sizeof(BITVEC));
} else {
ptr=cache[j].sign;
}
else
{
ptr = cache[j].sign;
LOOPBYTE(
union_l[i] |= ptr[i];
union_l[i] |= ptr[i];
);
}
*left++ = j;
v->spl_nleft++;
} else {
if (ISALLTRUE(datum_r) || cache[j].allistrue) {
if (! ISALLTRUE(datum_r) )
}
else
{
if (ISALLTRUE(datum_r) || cache[j].allistrue)
{
if (!ISALLTRUE(datum_r))
MemSet((void *) GETSIGN(datum_r), 0xff, sizeof(BITVEC));
} else {
ptr=cache[j].sign;
}
else
{
ptr = cache[j].sign;
LOOPBYTE(
union_r[i] |= ptr[i];
union_r[i] |= ptr[i];
);
}
*right++ = j;

View File

@ -2,105 +2,132 @@
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "regis.h"
#include "common.h"
int
RS_isRegis(const char *str) {
unsigned char *ptr=(unsigned char *)str;
RS_isRegis(const char *str)
{
unsigned char *ptr = (unsigned char *) str;
while(ptr && *ptr)
if ( isalpha(*ptr) || *ptr=='[' || *ptr==']' || *ptr=='^')
while (ptr && *ptr)
if (isalpha(*ptr) || *ptr == '[' || *ptr == ']' || *ptr == '^')
ptr++;
else
return 0;
return 1;
return 1;
}
#define RS_IN_ONEOF 1
#define RS_IN_ONEOF 1
#define RS_IN_ONEOF_IN 2
#define RS_IN_NONEOF 3
#define RS_IN_WAIT 4
static RegisNode*
newRegisNode(RegisNode *prev, int len) {
RegisNode *ptr;
ptr = (RegisNode*)malloc(RNHDRSZ+len+1);
static RegisNode *
newRegisNode(RegisNode * prev, int len)
{
RegisNode *ptr;
ptr = (RegisNode *) malloc(RNHDRSZ + len + 1);
if (!ptr)
ts_error(ERROR, "No memory");
memset(ptr,0,RNHDRSZ+len+1);
ts_error(ERROR, "No memory");
memset(ptr, 0, RNHDRSZ + len + 1);
if (prev)
prev->next=ptr;
prev->next = ptr;
return ptr;
}
int
RS_compile(Regis *r, int issuffix, const char *str) {
int i,len = strlen(str);
int state = RS_IN_WAIT;
RegisNode *ptr=NULL;
RS_compile(Regis * r, int issuffix, const char *str)
{
int i,
len = strlen(str);
int state = RS_IN_WAIT;
RegisNode *ptr = NULL;
memset(r,0,sizeof(Regis));
memset(r, 0, sizeof(Regis));
r->issuffix = (issuffix) ? 1 : 0;
for(i=0;i<len;i++) {
unsigned char c = *( ( (unsigned char*)str ) + i );
if ( state == RS_IN_WAIT ) {
if ( isalpha(c) ) {
if ( ptr )
ptr = newRegisNode(ptr,len);
for (i = 0; i < len; i++)
{
unsigned char c = *(((unsigned char *) str) + i);
if (state == RS_IN_WAIT)
{
if (isalpha(c))
{
if (ptr)
ptr = newRegisNode(ptr, len);
else
ptr = r->node = newRegisNode(NULL,len);
ptr->data[ 0 ] = c;
ptr = r->node = newRegisNode(NULL, len);
ptr->data[0] = c;
ptr->type = RSF_ONEOF;
ptr->len=1;
} else if ( c=='[' ) {
if ( ptr )
ptr = newRegisNode(ptr,len);
ptr->len = 1;
}
else if (c == '[')
{
if (ptr)
ptr = newRegisNode(ptr, len);
else
ptr = r->node = newRegisNode(NULL,len);
ptr = r->node = newRegisNode(NULL, len);
ptr->type = RSF_ONEOF;
state=RS_IN_ONEOF;
} else
ts_error(ERROR,"Error in regis: %s at pos %d\n", str, i+1);
} else if ( state == RS_IN_ONEOF ) {
if ( c=='^' ) {
state = RS_IN_ONEOF;
}
else
ts_error(ERROR, "Error in regis: %s at pos %d\n", str, i + 1);
}
else if (state == RS_IN_ONEOF)
{
if (c == '^')
{
ptr->type = RSF_NONEOF;
state=RS_IN_NONEOF;
} else if ( isalpha(c) ) {
ptr->data[ 0 ] = c;
ptr->len=1;
state=RS_IN_ONEOF_IN;
} else
ts_error(ERROR,"Error in regis: %s at pos %d\n", str, i+1);
} else if ( state == RS_IN_ONEOF_IN || state == RS_IN_NONEOF ) {
if ( isalpha(c) ) {
ptr->data[ ptr->len ] = c;
state = RS_IN_NONEOF;
}
else if (isalpha(c))
{
ptr->data[0] = c;
ptr->len = 1;
state = RS_IN_ONEOF_IN;
}
else
ts_error(ERROR, "Error in regis: %s at pos %d\n", str, i + 1);
}
else if (state == RS_IN_ONEOF_IN || state == RS_IN_NONEOF)
{
if (isalpha(c))
{
ptr->data[ptr->len] = c;
ptr->len++;
} else if ( c==']' ) {
state=RS_IN_WAIT;
} else
ts_error(ERROR,"Error in regis: %s at pos %d\n", str, i+1);
} else
ts_error(ERROR,"Internal error in RS_compile: %d\n", state);
}
else if (c == ']')
state = RS_IN_WAIT;
else
ts_error(ERROR, "Error in regis: %s at pos %d\n", str, i + 1);
}
else
ts_error(ERROR, "Internal error in RS_compile: %d\n", state);
}
ptr = r->node;
while(ptr) {
while (ptr)
{
r->nchar++;
ptr=ptr->next;
ptr = ptr->next;
}
return 0;
}
void
RS_free(Regis *r) {
RegisNode *ptr=r->node,*tmp;
void
RS_free(Regis * r)
{
RegisNode *ptr = r->node,
*tmp;
while(ptr) {
tmp=ptr->next;
while (ptr)
{
tmp = ptr->next;
free(ptr);
ptr = tmp;
}
@ -108,42 +135,49 @@ RS_free(Regis *r) {
r->node = NULL;
}
int
RS_execute(Regis *r, const char *str, int len) {
RegisNode *ptr=r->node;
int
RS_execute(Regis * r, const char *str, int len)
{
RegisNode *ptr = r->node;
unsigned char *c;
if (len<0)
len=strlen(str);
if (len < 0)
len = strlen(str);
if (len<r->nchar)
if (len < r->nchar)
return 0;
if ( r->issuffix )
c = ((unsigned char*)str) + len - r->nchar;
if (r->issuffix)
c = ((unsigned char *) str) + len - r->nchar;
else
c = (unsigned char*)str;
c = (unsigned char *) str;
while(ptr) {
switch(ptr->type) {
while (ptr)
{
switch (ptr->type)
{
case RSF_ONEOF:
if ( ptr->len==0 ) {
if ( *c != *(ptr->data) )
if (ptr->len == 0)
{
if (*c != *(ptr->data))
return 0;
} else if ( strchr((char*)ptr->data, *c) == NULL )
}
else if (strchr((char *) ptr->data, *c) == NULL)
return 0;
break;
case RSF_NONEOF:
if ( ptr->len==0 ) {
if ( *c == *(ptr->data) )
if (ptr->len == 0)
{
if (*c == *(ptr->data))
return 0;
} else if ( strchr((char*)ptr->data, *c) != NULL )
}
else if (strchr((char *) ptr->data, *c) != NULL)
return 0;
break;
default:
ts_error(ERROR,"RS_execute: Unknown type node: %d\n", ptr->type);
ts_error(ERROR, "RS_execute: Unknown type node: %d\n", ptr->type);
}
ptr=ptr->next;
ptr = ptr->next;
c++;
}

View File

@ -1,34 +1,38 @@
#ifndef __REGIS_H__
#define __REGIS_H__
#include "postgres.h"
#include "postgres.h"
typedef struct RegisNode {
uint32
type:2,
len:16,
unused:14;
typedef struct RegisNode
{
uint32
type:2,
len:16,
unused:14;
struct RegisNode *next;
unsigned char data[1];
} RegisNode;
unsigned char data[1];
} RegisNode;
#define RNHDRSZ (sizeof(uint32)+sizeof(void*))
#define RNHDRSZ (sizeof(uint32)+sizeof(void*))
#define RSF_ONEOF 1
#define RSF_NONEOF 2
#define RSF_ONEOF 1
#define RSF_NONEOF 2
typedef struct Regis {
RegisNode *node;
uint32
issuffix:1,
nchar:16,
unused:15;
} Regis;
typedef struct Regis
{
RegisNode *node;
uint32
issuffix:1,
nchar:16,
unused:15;
} Regis;
int RS_isRegis(const char *str);
int RS_isRegis(const char *str);
int RS_compile(Regis * r, int issuffix, const char *str);
void RS_free(Regis * r);
int RS_compile(Regis *r, int issuffix, const char *str);
void RS_free(Regis *r);
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1 <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
int RS_execute(Regis *r, const char *str, int len);
int RS_execute(Regis * r, const char *str, int len);
#endif

File diff suppressed because it is too large Load Diff

View File

@ -10,19 +10,21 @@
struct SPNode;
typedef struct {
uint32
val:8,
isword:1,
compoundallow:1,
affix:22;
struct SPNode *node;
} SPNodeData;
typedef struct
{
uint32
val:8,
isword:1,
compoundallow:1,
affix:22;
struct SPNode *node;
} SPNodeData;
typedef struct SPNode {
uint32 length;
SPNodeData data[1];
} SPNode;
typedef struct SPNode
{
uint32 length;
SPNodeData data[1];
} SPNode;
#define SPNHRDSZ (sizeof(uint32))
@ -30,81 +32,87 @@ typedef struct SPNode {
typedef struct spell_struct
{
char *word;
union {
union
{
char flag[16];
struct {
int affix;
int len;
} d;
} p;
struct
{
int affix;
int len;
} d;
} p;
} SPELL;
typedef struct aff_struct
{
uint32
flag:8,
type:2,
compile:1,
flagflags:3,
issimple:1,
isregis:1,
unused:1,
replen:16;
char mask[32];
char find[16];
char repl[16];
union {
regex_t regex;
Regis regis;
} reg;
uint32
flag:8,
type:2,
compile:1,
flagflags:3,
issimple:1,
isregis:1,
unused:1,
replen:16;
char mask[32];
char find[16];
char repl[16];
union
{
regex_t regex;
Regis regis;
} reg;
} AFFIX;
#define FF_CROSSPRODUCT 0x01
#define FF_COMPOUNDWORD 0x02
#define FF_COMPOUNDONLYAFX 0x04
#define FF_SUFFIX 2
#define FF_PREFIX 1
#define FF_CROSSPRODUCT 0x01
#define FF_COMPOUNDWORD 0x02
#define FF_COMPOUNDONLYAFX 0x04
#define FF_SUFFIX 2
#define FF_PREFIX 1
struct AffixNode;
typedef struct {
typedef struct
{
uint32
val:8,
naff:24;
AFFIX **aff;
val:8,
naff:24;
AFFIX **aff;
struct AffixNode *node;
} AffixNodeData;
} AffixNodeData;
typedef struct AffixNode {
uint32 isvoid:1,
length:31;
AffixNodeData data[1];
} AffixNode;
typedef struct AffixNode
{
uint32 isvoid:1,
length:31;
AffixNodeData data[1];
} AffixNode;
#define ANHRDSZ (sizeof(uint32))
#define ANHRDSZ (sizeof(uint32))
typedef struct {
char *affix;
int len;
} CMPDAffix;
typedef struct
{
char *affix;
int len;
} CMPDAffix;
typedef struct
{
int maffixes;
int naffixes;
AFFIX *Affix;
char compoundcontrol;
char compoundcontrol;
int nspell;
int mspell;
SPELL *Spell;
AffixNode *Suffix;
AffixNode *Prefix;
AffixNode *Suffix;
AffixNode *Prefix;
SPNode *Dictionary;
char **AffixData;
CMPDAffix *CompoundAffix;
SPNode *Dictionary;
char **AffixData;
CMPDAffix *CompoundAffix;
} IspellDict;

View File

@ -469,7 +469,7 @@ TS_execute(ITEM * curitem, void *checkval, bool calcnot, bool (*chkcond) (void *
Datum
rexectsq(PG_FUNCTION_ARGS)
{
SET_FUNCOID();
SET_FUNCOID();
return DirectFunctionCall2(
exectsq,
PG_GETARG_DATUM(1),
@ -484,7 +484,8 @@ exectsq(PG_FUNCTION_ARGS)
QUERYTYPE *query = (QUERYTYPE *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(1)));
CHKVAL chkval;
bool result;
SET_FUNCOID();
SET_FUNCOID();
if (!val->size || !query->size)
{
PG_FREE_IF_COPY(val, 0);
@ -639,7 +640,7 @@ static QUERYTYPE *
Datum
tsquery_in(PG_FUNCTION_ARGS)
{
SET_FUNCOID();
SET_FUNCOID();
PG_RETURN_POINTER(queryin((char *) PG_GETARG_POINTER(0), pushval_asis, 0));
}
@ -865,7 +866,8 @@ to_tsquery(PG_FUNCTION_ARGS)
QUERYTYPE *query;
ITEM *res;
int4 len;
SET_FUNCOID();
SET_FUNCOID();
str = text2char(in);
PG_FREE_IF_COPY(in, 1);
@ -888,10 +890,11 @@ to_tsquery_name(PG_FUNCTION_ARGS)
{
text *name = PG_GETARG_TEXT_P(0);
Datum res;
SET_FUNCOID();
SET_FUNCOID();
res = DirectFunctionCall2(to_tsquery,
Int32GetDatum(name2id_cfg(name)),
PG_GETARG_DATUM(1));
Int32GetDatum(name2id_cfg(name)),
PG_GETARG_DATUM(1));
PG_FREE_IF_COPY(name, 0);
PG_RETURN_DATUM(res);

View File

@ -13,11 +13,11 @@
static int
compareSNMapEntry(const void *a, const void *b)
{
if ( ((SNMapEntry *) a)->nsp < ((SNMapEntry *) b)->nsp )
if (((SNMapEntry *) a)->nsp < ((SNMapEntry *) b)->nsp)
return -1;
else if ( ((SNMapEntry *) a)->nsp > ((SNMapEntry *) b)->nsp )
else if (((SNMapEntry *) a)->nsp > ((SNMapEntry *) b)->nsp)
return 1;
else
else
return strcmp(((SNMapEntry *) a)->key, ((SNMapEntry *) b)->key);
}

View File

@ -38,10 +38,10 @@ init_cfg(Oid id, TSCfgInfo * cfg)
j;
text *ptr;
text *prsname = NULL;
char *nsp=get_namespace(TSNSP_FunctionOid);
char buf[1024];
char *nsp = get_namespace(TSNSP_FunctionOid);
char buf[1024];
MemoryContext oldcontext;
void *plan;
void *plan;
arg[0] = OIDOID;
arg[1] = OIDOID;
@ -52,7 +52,7 @@ init_cfg(Oid id, TSCfgInfo * cfg)
SPI_connect();
sprintf(buf, "select prs_name from %s.pg_ts_cfg where oid = $1", nsp);
plan= SPI_prepare(buf, 1, arg);
plan = SPI_prepare(buf, 1, arg);
if (!plan)
ts_error(ERROR, "SPI_prepare() failed");
@ -77,7 +77,7 @@ init_cfg(Oid id, TSCfgInfo * cfg)
arg[0] = TEXTOID;
sprintf(buf, "select lt.tokid, map.dict_name from %s.pg_ts_cfgmap as map, %s.pg_ts_cfg as cfg, %s.token_type( $1 ) as lt where lt.alias = map.tok_alias and map.ts_name = cfg.ts_name and cfg.oid= $2 order by lt.tokid desc;", nsp, nsp, nsp);
plan= SPI_prepare(buf, 2, arg);
plan = SPI_prepare(buf, 2, arg);
if (!plan)
ts_error(ERROR, "SPI_prepare() failed");
@ -118,7 +118,7 @@ init_cfg(Oid id, TSCfgInfo * cfg)
cfg->map[lexid].len = ARRNELEMS(a);
cfg->map[lexid].dict_id = (Datum *) malloc(sizeof(Datum) * cfg->map[lexid].len);
if (!cfg->map[lexid].dict_id)
ts_error(ERROR, "No memory");
ts_error(ERROR, "No memory");
memset(cfg->map[lexid].dict_id, 0, sizeof(Datum) * cfg->map[lexid].len);
ptr = (text *) ARR_DATA_PTR(a);
@ -235,9 +235,9 @@ name2id_cfg(text *name)
Datum pars[1];
int stat;
Oid id = findSNMap_t(&(CList.name2id_map), name);
void *plan;
char *nsp;
char buf[1024];
void *plan;
char *nsp;
char buf[1024];
arg[0] = TEXTOID;
pars[0] = PointerGetDatum(name);
@ -245,10 +245,10 @@ name2id_cfg(text *name)
if (id)
return id;
nsp=get_namespace(TSNSP_FunctionOid);
nsp = get_namespace(TSNSP_FunctionOid);
SPI_connect();
sprintf(buf, "select oid from %s.pg_ts_cfg where ts_name = $1", nsp);
plan= SPI_prepare(buf, 1, arg);
sprintf(buf, "select oid from %s.pg_ts_cfg where ts_name = $1", nsp);
plan = SPI_prepare(buf, 1, arg);
if (!plan)
/* internal error */
elog(ERROR, "SPI_prepare() failed");
@ -301,13 +301,14 @@ parsetext_v2(TSCfgInfo * cfg, PRSTEXT * prs, char *buf, int4 buflen)
PointerGetDatum(&lenlemm)))) != 0)
{
if (lenlemm >= MAXSTRLEN) {
if (lenlemm >= MAXSTRLEN)
{
#ifdef IGNORE_LONGLEXEME
ereport(NOTICE,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("word is too long")));
continue;
#else
#else
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("word is too long")));
@ -435,13 +436,14 @@ hlparsetext(TSCfgInfo * cfg, HLPRSTEXT * prs, QUERYTYPE * query, char *buf, int4
PointerGetDatum(&lenlemm)))) != 0)
{
if (lenlemm >= MAXSTRLEN) {
if (lenlemm >= MAXSTRLEN)
{
#ifdef IGNORE_LONGLEXEME
ereport(NOTICE,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("word is too long")));
continue;
#else
#else
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("word is too long")));
@ -532,9 +534,8 @@ genhl(HLPRSTEXT * prs)
ptr += prs->stopsellen;
}
}
} else
if (!wrd->repeated)
}
else if (!wrd->repeated)
pfree(wrd->word);
wrd++;
@ -552,16 +553,16 @@ get_currcfg(void)
Datum pars[1];
bool isnull;
int stat;
char buf[1024];
char *nsp;
void *plan;
char buf[1024];
char *nsp;
void *plan;
if (current_cfg_id > 0)
return current_cfg_id;
nsp=get_namespace(TSNSP_FunctionOid);
nsp = get_namespace(TSNSP_FunctionOid);
SPI_connect();
sprintf(buf, "select oid from %s.pg_ts_cfg where locale = $1 ", nsp);
sprintf(buf, "select oid from %s.pg_ts_cfg where locale = $1 ", nsp);
pfree(nsp);
plan = SPI_prepare(buf, 1, arg);
if (!plan)
@ -593,7 +594,7 @@ Datum set_curcfg(PG_FUNCTION_ARGS);
Datum
set_curcfg(PG_FUNCTION_ARGS)
{
SET_FUNCOID();
SET_FUNCOID();
findcfg(PG_GETARG_OID(0));
current_cfg_id = PG_GETARG_OID(0);
PG_RETURN_VOID();
@ -605,7 +606,8 @@ Datum
set_curcfg_byname(PG_FUNCTION_ARGS)
{
text *name = PG_GETARG_TEXT_P(0);
SET_FUNCOID();
SET_FUNCOID();
DirectFunctionCall1(
set_curcfg,
ObjectIdGetDatum(name2id_cfg(name))
@ -619,7 +621,7 @@ Datum show_curcfg(PG_FUNCTION_ARGS);
Datum
show_curcfg(PG_FUNCTION_ARGS)
{
SET_FUNCOID();
SET_FUNCOID();
PG_RETURN_OID(get_currcfg());
}
@ -628,8 +630,7 @@ Datum reset_tsearch(PG_FUNCTION_ARGS);
Datum
reset_tsearch(PG_FUNCTION_ARGS)
{
SET_FUNCOID();
SET_FUNCOID();
ts_error(NOTICE, "TSearch cache cleaned");
PG_RETURN_VOID();
}

View File

@ -15,7 +15,7 @@ Datum
tsstat_in(PG_FUNCTION_ARGS)
{
tsstat *stat = palloc(STATHDRSIZE);
stat->len = STATHDRSIZE;
stat->size = 0;
stat->weight = 0;
@ -34,12 +34,14 @@ tsstat_out(PG_FUNCTION_ARGS)
}
static int
check_weight(tsvector *txt, WordEntry *wptr, int8 weight) {
int len = POSDATALEN(txt, wptr);
int num=0;
WordEntryPos *ptr = POSDATAPTR(txt, wptr);
check_weight(tsvector * txt, WordEntry * wptr, int8 weight)
{
int len = POSDATALEN(txt, wptr);
int num = 0;
WordEntryPos *ptr = POSDATAPTR(txt, wptr);
while (len--) {
while (len--)
{
if (weight & (1 << ptr->weight))
num++;
ptr++;
@ -123,9 +125,9 @@ formstat(tsstat * stat, tsvector * txt, WordEntry ** entry, uint32 len)
}
nptr = STATPTR(newstat) + (StopLow - STATPTR(stat));
memcpy(STATPTR(newstat), STATPTR(stat), sizeof(StatEntry) * (StopLow - STATPTR(stat)));
if ( (*ptr)->haspos ) {
nptr->nentry = ( stat->weight ) ? check_weight(txt, *ptr, stat->weight) : POSDATALEN(txt, *ptr);
} else
if ((*ptr)->haspos)
nptr->nentry = (stat->weight) ? check_weight(txt, *ptr, stat->weight) : POSDATALEN(txt, *ptr);
else
nptr->nentry = 1;
nptr->ndoc = 1;
nptr->len = (*ptr)->len;
@ -144,9 +146,9 @@ formstat(tsstat * stat, tsvector * txt, WordEntry ** entry, uint32 len)
}
else
{
if ( (*ptr)->haspos ) {
nptr->nentry = ( stat->weight ) ? check_weight(txt, *ptr, stat->weight) : POSDATALEN(txt, *ptr);
} else
if ((*ptr)->haspos)
nptr->nentry = (stat->weight) ? check_weight(txt, *ptr, stat->weight) : POSDATALEN(txt, *ptr);
else
nptr->nentry = 1;
nptr->ndoc = 1;
nptr->len = (*ptr)->len;
@ -162,9 +164,9 @@ formstat(tsstat * stat, tsvector * txt, WordEntry ** entry, uint32 len)
while (ptr - entry < len)
{
if ( (*ptr)->haspos ) {
nptr->nentry = ( stat->weight ) ? check_weight(txt, *ptr, stat->weight) : POSDATALEN(txt, *ptr);
} else
if ((*ptr)->haspos)
nptr->nentry = (stat->weight) ? check_weight(txt, *ptr, stat->weight) : POSDATALEN(txt, *ptr);
else
nptr->nentry = 1;
nptr->ndoc = 1;
nptr->len = (*ptr)->len;
@ -192,7 +194,7 @@ ts_accum(PG_FUNCTION_ARGS)
cur = 0;
StatEntry *sptr;
WordEntry *wptr;
int n=0;
int n = 0;
if (stat == NULL || PG_ARGISNULL(0))
{ /* Init in first */
@ -222,10 +224,13 @@ ts_accum(PG_FUNCTION_ARGS)
sptr++;
else if (cmp == 0)
{
if ( stat->weight == 0 ) {
if (stat->weight == 0)
{
sptr->ndoc++;
sptr->nentry += (wptr->haspos) ? POSDATALEN(txt, wptr) : 1;
} else if ( wptr->haspos && (n=check_weight(txt, wptr, stat->weight))!=0 ) {
}
else if (wptr->haspos && (n = check_weight(txt, wptr, stat->weight)) != 0)
{
sptr->ndoc++;
sptr->nentry += n;
}
@ -234,7 +239,8 @@ ts_accum(PG_FUNCTION_ARGS)
}
else
{
if ( stat->weight == 0 || check_weight(txt, wptr, stat->weight)!=0 ) {
if (stat->weight == 0 || check_weight(txt, wptr, stat->weight) != 0)
{
if (cur == len)
newentry = SEI_realloc(newentry, &len);
newentry[cur] = wptr;
@ -246,7 +252,8 @@ ts_accum(PG_FUNCTION_ARGS)
while (wptr - ARRPTR(txt) < txt->size)
{
if ( stat->weight == 0 || check_weight(txt, wptr, stat->weight)!=0 ) {
if (stat->weight == 0 || check_weight(txt, wptr, stat->weight) != 0)
{
if (cur == len)
newentry = SEI_realloc(newentry, &len);
newentry[cur] = wptr;
@ -269,10 +276,13 @@ ts_accum(PG_FUNCTION_ARGS)
cmp = compareStatWord(sptr, wptr, stat, txt);
if (cmp == 0)
{
if ( stat->weight == 0 ) {
if (stat->weight == 0)
{
sptr->ndoc++;
sptr->nentry += (wptr->haspos) ? POSDATALEN(txt, wptr) : 1;
} else if ( wptr->haspos && (n=check_weight(txt, wptr, stat->weight))!=0 ) {
}
else if (wptr->haspos && (n = check_weight(txt, wptr, stat->weight)) != 0)
{
sptr->ndoc++;
sptr->nentry += n;
}
@ -286,7 +296,8 @@ ts_accum(PG_FUNCTION_ARGS)
if (StopLow >= StopHigh)
{ /* not found */
if ( stat->weight == 0 || check_weight(txt, wptr, stat->weight)!=0 ) {
if (stat->weight == 0 || check_weight(txt, wptr, stat->weight) != 0)
{
if (cur == len)
newentry = SEI_realloc(newentry, &len);
newentry[cur] = wptr;
@ -454,11 +465,15 @@ ts_stat_sql(text *txt, text *ws)
stat->size = 0;
stat->weight = 0;
if ( ws ) {
char *buf;
if (ws)
{
char *buf;
buf = VARDATA(ws);
while( buf - VARDATA(ws) < VARSIZE(ws) - VARHDRSZ ) {
switch (tolower(*buf)) {
while (buf - VARDATA(ws) < VARSIZE(ws) - VARHDRSZ)
{
switch (tolower(*buf))
{
case 'a':
stat->weight |= 1 << 3;
break;
@ -521,13 +536,14 @@ ts_stat(PG_FUNCTION_ARGS)
{
tsstat *stat;
text *txt = PG_GETARG_TEXT_P(0);
text *ws = (PG_NARGS() > 1) ? PG_GETARG_TEXT_P(1) : NULL;
text *ws = (PG_NARGS() > 1) ? PG_GETARG_TEXT_P(1) : NULL;
funcctx = SRF_FIRSTCALL_INIT();
SPI_connect();
stat = ts_stat_sql(txt,ws);
stat = ts_stat_sql(txt, ws);
PG_FREE_IF_COPY(txt, 0);
if (PG_NARGS() > 1 ) PG_FREE_IF_COPY(ws, 1);
if (PG_NARGS() > 1)
PG_FREE_IF_COPY(ws, 1);
ts_setup_firstcall(funcctx, stat);
SPI_finish();
}

View File

@ -404,7 +404,8 @@ tsvector_in(PG_FUNCTION_ARGS)
*cur;
int4 i,
buflen = 256;
SET_FUNCOID();
SET_FUNCOID();
state.prsbuf = buf;
state.len = 32;
state.word = (char *) palloc(state.len);
@ -453,7 +454,7 @@ tsvector_in(PG_FUNCTION_ARGS)
if (len > 0)
len = uniqueentry(arr, len, tmpbuf, &buflen);
else
buflen=0;
buflen = 0;
totallen = CALCDATASIZE(len, buflen);
in = (tsvector *) palloc(totallen);
memset(in, 0, totallen);
@ -638,7 +639,8 @@ uniqueWORD(TSWORD * a, int4 l)
res->alen *= 2;
res->pos.apos = (uint16 *) repalloc(res->pos.apos, sizeof(uint16) * res->alen);
}
if ( res->pos.apos[0]==0 || res->pos.apos[res->pos.apos[0]] != LIMITPOS(ptr->pos.pos) ) {
if (res->pos.apos[0] == 0 || res->pos.apos[res->pos.apos[0]] != LIMITPOS(ptr->pos.pos))
{
res->pos.apos[res->pos.apos[0] + 1] = LIMITPOS(ptr->pos.pos);
res->pos.apos[0]++;
}
@ -725,7 +727,7 @@ to_tsvector(PG_FUNCTION_ARGS)
tsvector *out = NULL;
TSCfgInfo *cfg;
SET_FUNCOID();
SET_FUNCOID();
cfg = findcfg(PG_GETARG_INT32(0));
prs.lenwords = 32;
@ -753,13 +755,14 @@ to_tsvector_name(PG_FUNCTION_ARGS)
{
text *cfg = PG_GETARG_TEXT_P(0);
Datum res;
SET_FUNCOID();
SET_FUNCOID();
res = DirectFunctionCall3(
to_tsvector,
Int32GetDatum(name2id_cfg(cfg)),
PG_GETARG_DATUM(1),
(Datum) 0
);
to_tsvector,
Int32GetDatum(name2id_cfg(cfg)),
PG_GETARG_DATUM(1),
(Datum) 0
);
PG_FREE_IF_COPY(cfg, 0);
PG_RETURN_DATUM(res);
@ -769,13 +772,14 @@ Datum
to_tsvector_current(PG_FUNCTION_ARGS)
{
Datum res;
SET_FUNCOID();
SET_FUNCOID();
res = DirectFunctionCall3(
to_tsvector,
Int32GetDatum(get_currcfg()),
PG_GETARG_DATUM(0),
(Datum) 0
);
to_tsvector,
Int32GetDatum(get_currcfg()),
PG_GETARG_DATUM(0),
(Datum) 0
);
PG_RETURN_DATUM(res);
}
@ -823,7 +827,7 @@ tsearch2(PG_FUNCTION_ARGS)
Oid funcoid = InvalidOid;
TSCfgInfo *cfg;
SET_FUNCOID();
SET_FUNCOID();
cfg = findcfg(get_currcfg());
if (!CALLED_AS_TRIGGER(fcinfo))
@ -947,26 +951,30 @@ tsearch2(PG_FUNCTION_ARGS)
}
static int
silly_cmp_tsvector(const tsvector *a, const tsvector *b) {
if ( a->len < b->len )
silly_cmp_tsvector(const tsvector * a, const tsvector * b)
{
if (a->len < b->len)
return -1;
else if ( a->len > b->len )
else if (a->len > b->len)
return 1;
else if ( a->size < b->size )
else if (a->size < b->size)
return -1;
else if ( a->size > b->size )
else if (a->size > b->size)
return 1;
else {
unsigned char *aptr=(unsigned char *)(a->data) + DATAHDRSIZE;
unsigned char *bptr=(unsigned char *)(b->data) + DATAHDRSIZE;
while( aptr - ( (unsigned char *)(a->data) ) < a->len ) {
if ( *aptr != *bptr )
return ( *aptr < *bptr ) ? -1 : 1;
aptr++; bptr++;
}
else
{
unsigned char *aptr = (unsigned char *) (a->data) + DATAHDRSIZE;
unsigned char *bptr = (unsigned char *) (b->data) + DATAHDRSIZE;
while (aptr - ((unsigned char *) (a->data)) < a->len)
{
if (*aptr != *bptr)
return (*aptr < *bptr) ? -1 : 1;
aptr++;
bptr++;
}
}
return 0;
return 0;
}
PG_FUNCTION_INFO_V1(tsvector_cmp);
@ -976,60 +984,66 @@ PG_FUNCTION_INFO_V1(tsvector_eq);
PG_FUNCTION_INFO_V1(tsvector_ne);
PG_FUNCTION_INFO_V1(tsvector_ge);
PG_FUNCTION_INFO_V1(tsvector_gt);
Datum tsvector_cmp(PG_FUNCTION_ARGS);
Datum tsvector_lt(PG_FUNCTION_ARGS);
Datum tsvector_le(PG_FUNCTION_ARGS);
Datum tsvector_eq(PG_FUNCTION_ARGS);
Datum tsvector_ne(PG_FUNCTION_ARGS);
Datum tsvector_ge(PG_FUNCTION_ARGS);
Datum tsvector_gt(PG_FUNCTION_ARGS);
Datum tsvector_cmp(PG_FUNCTION_ARGS);
Datum tsvector_lt(PG_FUNCTION_ARGS);
Datum tsvector_le(PG_FUNCTION_ARGS);
Datum tsvector_eq(PG_FUNCTION_ARGS);
Datum tsvector_ne(PG_FUNCTION_ARGS);
Datum tsvector_ge(PG_FUNCTION_ARGS);
Datum tsvector_gt(PG_FUNCTION_ARGS);
#define RUNCMP \
tsvector *a = (tsvector *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0)));\
tsvector *b = (tsvector *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(1)));\
#define RUNCMP \
tsvector *a = (tsvector *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0)));\
tsvector *b = (tsvector *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(1)));\
int res = silly_cmp_tsvector(a,b); \
PG_FREE_IF_COPY(a,0); \
PG_FREE_IF_COPY(b,1); \
Datum
tsvector_cmp(PG_FUNCTION_ARGS) {
tsvector_cmp(PG_FUNCTION_ARGS)
{
RUNCMP
PG_RETURN_INT32(res);
}
Datum
tsvector_lt(PG_FUNCTION_ARGS) {
tsvector_lt(PG_FUNCTION_ARGS)
{
RUNCMP
PG_RETURN_BOOL((res < 0) ? true : false);
}
Datum
tsvector_le(PG_FUNCTION_ARGS) {
tsvector_le(PG_FUNCTION_ARGS)
{
RUNCMP
PG_RETURN_BOOL((res <= 0) ? true : false);
}
Datum
tsvector_eq(PG_FUNCTION_ARGS) {
tsvector_eq(PG_FUNCTION_ARGS)
{
RUNCMP
PG_RETURN_BOOL((res == 0) ? true : false);
}
Datum
tsvector_ge(PG_FUNCTION_ARGS) {
tsvector_ge(PG_FUNCTION_ARGS)
{
RUNCMP
PG_RETURN_BOOL((res >= 0) ? true : false);
}
Datum
tsvector_gt(PG_FUNCTION_ARGS) {
tsvector_gt(PG_FUNCTION_ARGS)
{
RUNCMP
PG_RETURN_BOOL((res > 0) ? true : false);
}
Datum
tsvector_ne(PG_FUNCTION_ARGS) {
RUNCMP
PG_RETURN_BOOL((res != 0) ? true : false);
}
Datum
tsvector_ne(PG_FUNCTION_ARGS)
{
RUNCMP
PG_RETURN_BOOL((res != 0) ? true : false);
}

View File

@ -1,8 +1,8 @@
#ifndef __PARSER_H__
#define __PARSER_H__
extern char *token;
extern int tokenlen;
extern char *token;
extern int tokenlen;
int tsearch2_yylex(void);
void tsearch2_start_parse_str(char *, int);
void tsearch2_end_parse(void);

View File

@ -30,18 +30,19 @@ init_prs(Oid id, WParserInfo * prs)
bool isnull;
Datum pars[1];
int stat;
void *plan;
char buf[1024], *nsp;
void *plan;
char buf[1024],
*nsp;
arg[0] = OIDOID;
pars[0] = ObjectIdGetDatum(id);
memset(prs, 0, sizeof(WParserInfo));
SPI_connect();
nsp=get_namespace(TSNSP_FunctionOid);
nsp = get_namespace(TSNSP_FunctionOid);
sprintf(buf, "select prs_start, prs_nexttoken, prs_end, prs_lextype, prs_headline from %s.pg_ts_parser where oid = $1", nsp);
pfree(nsp);
plan= SPI_prepare(buf, 1, arg);
plan = SPI_prepare(buf, 1, arg);
if (!plan)
ts_error(ERROR, "SPI_prepare() failed");
@ -140,8 +141,9 @@ name2id_prs(text *name)
Datum pars[1];
int stat;
Oid id = findSNMap_t(&(PList.name2id_map), name);
char buf[1024], *nsp;
void *plan;
char buf[1024],
*nsp;
void *plan;
arg[0] = TEXTOID;
pars[0] = PointerGetDatum(name);
@ -153,7 +155,7 @@ name2id_prs(text *name)
nsp = get_namespace(TSNSP_FunctionOid);
sprintf(buf, "select oid from %s.pg_ts_parser where prs_name = $1", nsp);
pfree(nsp);
plan= SPI_prepare(buf, 1, arg);
plan = SPI_prepare(buf, 1, arg);
if (!plan)
ts_error(ERROR, "SPI_prepare() failed");
@ -242,7 +244,8 @@ token_type(PG_FUNCTION_ARGS)
{
FuncCallContext *funcctx;
Datum result;
SET_FUNCOID();
SET_FUNCOID();
if (SRF_IS_FIRSTCALL())
{
funcctx = SRF_FIRSTCALL_INIT();
@ -263,7 +266,8 @@ token_type_byname(PG_FUNCTION_ARGS)
{
FuncCallContext *funcctx;
Datum result;
SET_FUNCOID();
SET_FUNCOID();
if (SRF_IS_FIRSTCALL())
{
text *name = PG_GETARG_TEXT_P(0);
@ -287,7 +291,8 @@ token_type_current(PG_FUNCTION_ARGS)
{
FuncCallContext *funcctx;
Datum result;
SET_FUNCOID();
SET_FUNCOID();
if (SRF_IS_FIRSTCALL())
{
funcctx = SRF_FIRSTCALL_INIT();
@ -309,7 +314,7 @@ Datum set_curprs(PG_FUNCTION_ARGS);
Datum
set_curprs(PG_FUNCTION_ARGS)
{
SET_FUNCOID();
SET_FUNCOID();
findprs(PG_GETARG_OID(0));
current_parser_id = PG_GETARG_OID(0);
PG_RETURN_VOID();
@ -321,7 +326,8 @@ Datum
set_curprs_byname(PG_FUNCTION_ARGS)
{
text *name = PG_GETARG_TEXT_P(0);
SET_FUNCOID();
SET_FUNCOID();
DirectFunctionCall1(
set_curprs,
ObjectIdGetDatum(name2id_prs(name))
@ -444,7 +450,8 @@ parse(PG_FUNCTION_ARGS)
{
FuncCallContext *funcctx;
Datum result;
SET_FUNCOID();
SET_FUNCOID();
if (SRF_IS_FIRSTCALL())
{
text *txt = PG_GETARG_TEXT_P(1);
@ -468,7 +475,8 @@ parse_byname(PG_FUNCTION_ARGS)
{
FuncCallContext *funcctx;
Datum result;
SET_FUNCOID();
SET_FUNCOID();
if (SRF_IS_FIRSTCALL())
{
text *name = PG_GETARG_TEXT_P(0);
@ -495,7 +503,8 @@ parse_current(PG_FUNCTION_ARGS)
{
FuncCallContext *funcctx;
Datum result;
SET_FUNCOID();
SET_FUNCOID();
if (SRF_IS_FIRSTCALL())
{
text *txt = PG_GETARG_TEXT_P(0);
@ -527,7 +536,7 @@ headline(PG_FUNCTION_ARGS)
TSCfgInfo *cfg;
WParserInfo *prsobj;
SET_FUNCOID();
SET_FUNCOID();
cfg = findcfg(PG_GETARG_OID(0));
prsobj = findprs(cfg->prs_id);
@ -566,14 +575,15 @@ headline_byname(PG_FUNCTION_ARGS)
text *cfg = PG_GETARG_TEXT_P(0);
Datum out;
SET_FUNCOID();
SET_FUNCOID();
out = DirectFunctionCall4(
headline,
ObjectIdGetDatum(name2id_cfg(cfg)),
PG_GETARG_DATUM(1),
PG_GETARG_DATUM(2),
headline,
ObjectIdGetDatum(name2id_cfg(cfg)),
PG_GETARG_DATUM(1),
PG_GETARG_DATUM(2),
(PG_NARGS() > 3) ? PG_GETARG_DATUM(3) : PointerGetDatum(NULL)
);
);
PG_FREE_IF_COPY(cfg, 0);
PG_RETURN_DATUM(out);
@ -584,7 +594,7 @@ Datum headline_current(PG_FUNCTION_ARGS);
Datum
headline_current(PG_FUNCTION_ARGS)
{
SET_FUNCOID();
SET_FUNCOID();
PG_RETURN_DATUM(DirectFunctionCall4(
headline,
ObjectIdGetDatum(get_currcfg()),

View File

@ -192,12 +192,13 @@ prsd_headline(PG_FUNCTION_ARGS)
int bestb = -1,
beste = -1;
int bestlen = -1;
int pose = 0, posb,
int pose = 0,
posb,
poslen,
curlen;
int i;
int highlight=0;
int highlight = 0;
/* config */
prs->startsel = NULL;
@ -224,13 +225,13 @@ prsd_headline(PG_FUNCTION_ARGS)
prs->stopsel = pstrdup(mptr->value);
else if (pg_strcasecmp(mptr->key, "HighlightAll") == 0)
highlight = (
pg_strcasecmp(mptr->value, "1")==0 ||
pg_strcasecmp(mptr->value, "on")==0 ||
pg_strcasecmp(mptr->value, "true")==0 ||
pg_strcasecmp(mptr->value, "t")==0 ||
pg_strcasecmp(mptr->value, "y")==0 ||
pg_strcasecmp(mptr->value, "yes")==0 ) ?
1 : 0;
pg_strcasecmp(mptr->value, "1") == 0 ||
pg_strcasecmp(mptr->value, "on") == 0 ||
pg_strcasecmp(mptr->value, "true") == 0 ||
pg_strcasecmp(mptr->value, "t") == 0 ||
pg_strcasecmp(mptr->value, "y") == 0 ||
pg_strcasecmp(mptr->value, "yes") == 0) ?
1 : 0;
pfree(mptr->key);
pfree(mptr->value);
@ -239,23 +240,25 @@ prsd_headline(PG_FUNCTION_ARGS)
}
pfree(map);
if (highlight==0) {
if (highlight == 0)
{
if (min_words >= max_words)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("MinWords should be less than MaxWords")));
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("MinWords should be less than MaxWords")));
if (min_words <= 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("MinWords should be positive")));
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("MinWords should be positive")));
if (shortword < 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("ShortWord should be >= 0")));
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("ShortWord should be >= 0")));
}
}
if (highlight==0) {
if (highlight == 0)
{
while (hlCover(prs, query, &p, &q))
{
/* find cover len in words */
@ -269,17 +272,17 @@ prsd_headline(PG_FUNCTION_ARGS)
poslen++;
pose = i;
}
if (poslen < bestlen && !(NOENDTOKEN(prs->words[beste].type) || prs->words[beste].len <= shortword))
{
/* best already finded, so try one more cover */
p++;
continue;
}
posb=p;
posb = p;
if (curlen < max_words)
{ /* find good end */
{ /* find good end */
for (i = i - 1; i < prs->curwords && curlen < max_words; i++)
{
if (i != q)
@ -295,8 +298,11 @@ prsd_headline(PG_FUNCTION_ARGS)
if (curlen >= min_words)
break;
}
if ( curlen < min_words && i>=prs->curwords ) { /* got end of text and our cover is shoter than min_words */
for(i=p; i>= 0; i--) {
if (curlen < min_words && i >= prs->curwords)
{ /* got end of text and our cover is shoter
* than min_words */
for (i = p; i >= 0; i--)
{
if (!NONWORDTOKEN(prs->words[i].type))
curlen++;
if (prs->words[i].item && !prs->words[i].repeated)
@ -306,11 +312,11 @@ prsd_headline(PG_FUNCTION_ARGS)
if (curlen >= min_words)
break;
}
posb=(i>=0) ? i : 0;
posb = (i >= 0) ? i : 0;
}
}
else
{ /* shorter cover :((( */
{ /* shorter cover :((( */
for (; curlen > min_words; i--)
{
if (!NONWORDTOKEN(prs->words[i].type))
@ -323,7 +329,7 @@ prsd_headline(PG_FUNCTION_ARGS)
break;
}
}
if (bestlen < 0 || (poslen > bestlen && !(NOENDTOKEN(prs->words[pose].type) || prs->words[pose].len <= shortword)) ||
(bestlen >= 0 && !(NOENDTOKEN(prs->words[pose].type) || prs->words[pose].len <= shortword) &&
(NOENDTOKEN(prs->words[beste].type) || prs->words[beste].len <= shortword)))
@ -332,7 +338,7 @@ prsd_headline(PG_FUNCTION_ARGS)
beste = pose;
bestlen = poslen;
}
p++;
}
@ -348,19 +354,24 @@ prsd_headline(PG_FUNCTION_ARGS)
bestb = 0;
beste = pose;
}
} else {
bestb=0;
beste=prs->curwords-1;
}
else
{
bestb = 0;
beste = prs->curwords - 1;
}
for (i = bestb; i <= beste; i++)
{
if (prs->words[i].item)
prs->words[i].selected = 1;
if ( highlight==0 ) {
if (highlight == 0)
{
if (HLIDIGNORE(prs->words[i].type))
prs->words[i].replace = 1;
} else {
}
else
{
if (HTMLHLIDIGNORE(prs->words[i].type))
prs->words[i].replace = 1;
}

File diff suppressed because it is too large Load Diff

View File

@ -32,153 +32,150 @@ extern xmlChar *pgxml_texttoxmlchar(text *textstring);
/* local defs */
static void parse_params(const char **params, text *paramstr);
Datum xslt_process(PG_FUNCTION_ARGS);
Datum xslt_process(PG_FUNCTION_ARGS);
#define MAXPARAMS 20
PG_FUNCTION_INFO_V1(xslt_process);
Datum xslt_process(PG_FUNCTION_ARGS) {
Datum
xslt_process(PG_FUNCTION_ARGS)
{
const char *params[MAXPARAMS + 1]; /* +1 for the terminator */
xsltStylesheetPtr stylesheet = NULL;
xmlDocPtr doctree;
xmlDocPtr restree;
xmlDocPtr ssdoc = NULL;
xmlChar *resstr;
int resstat;
int reslen;
const char *params[MAXPARAMS + 1]; /* +1 for the terminator */
xsltStylesheetPtr stylesheet = NULL;
xmlDocPtr doctree;
xmlDocPtr restree;
xmlDocPtr ssdoc = NULL;
xmlChar *resstr;
int resstat;
int reslen;
text *doct = PG_GETARG_TEXT_P(0);
text *ssheet = PG_GETARG_TEXT_P(1);
text *paramstr;
text *tres;
text *doct = PG_GETARG_TEXT_P(0);
text *ssheet = PG_GETARG_TEXT_P(1);
text *paramstr;
text *tres;
if (fcinfo->nargs == 3)
{
paramstr = PG_GETARG_TEXT_P(2);
parse_params(params,paramstr);
}
else /* No parameters */
{
params[0] = NULL;
}
/* Setup parser */
pgxml_parser_init();
/* Check to see if document is a file or a literal */
if (VARDATA(doct)[0] == '<')
{
doctree = xmlParseMemory((char *) VARDATA(doct), VARSIZE(doct)-VARHDRSZ);
}
else
{
doctree = xmlParseFile(GET_STR(doct));
}
if (doctree == NULL)
{
xmlCleanupParser();
elog_error(ERROR,"Error parsing XML document",0);
PG_RETURN_NULL();
}
/* Same for stylesheet */
if (VARDATA(ssheet)[0] == '<')
{
ssdoc = xmlParseMemory((char *) VARDATA(ssheet),
VARSIZE(ssheet)-VARHDRSZ);
if (ssdoc == NULL)
if (fcinfo->nargs == 3)
{
xmlFreeDoc(doctree);
xmlCleanupParser();
elog_error(ERROR,"Error parsing stylesheet as XML document",0);
PG_RETURN_NULL();
paramstr = PG_GETARG_TEXT_P(2);
parse_params(params, paramstr);
}
else
/* No parameters */
params[0] = NULL;
/* Setup parser */
pgxml_parser_init();
/* Check to see if document is a file or a literal */
if (VARDATA(doct)[0] == '<')
doctree = xmlParseMemory((char *) VARDATA(doct), VARSIZE(doct) - VARHDRSZ);
else
doctree = xmlParseFile(GET_STR(doct));
if (doctree == NULL)
{
xmlCleanupParser();
elog_error(ERROR, "Error parsing XML document", 0);
PG_RETURN_NULL();
}
stylesheet = xsltParseStylesheetDoc(ssdoc);
}
else
{
stylesheet = xsltParseStylesheetFile(GET_STR(ssheet));
}
/* Same for stylesheet */
if (VARDATA(ssheet)[0] == '<')
{
ssdoc = xmlParseMemory((char *) VARDATA(ssheet),
VARSIZE(ssheet) - VARHDRSZ);
if (ssdoc == NULL)
{
xmlFreeDoc(doctree);
xmlCleanupParser();
elog_error(ERROR, "Error parsing stylesheet as XML document", 0);
PG_RETURN_NULL();
}
stylesheet = xsltParseStylesheetDoc(ssdoc);
}
else
stylesheet = xsltParseStylesheetFile(GET_STR(ssheet));
if (stylesheet == NULL)
{
xmlFreeDoc(doctree);
xsltCleanupGlobals();
xmlCleanupParser();
elog_error(ERROR,"Failed to parse stylesheet",0);
PG_RETURN_NULL();
}
if (stylesheet == NULL)
{
xmlFreeDoc(doctree);
xsltCleanupGlobals();
xmlCleanupParser();
elog_error(ERROR, "Failed to parse stylesheet", 0);
PG_RETURN_NULL();
}
restree = xsltApplyStylesheet(stylesheet, doctree, params);
resstat = xsltSaveResultToString(&resstr, &reslen, restree, stylesheet);
restree = xsltApplyStylesheet(stylesheet, doctree, params);
resstat = xsltSaveResultToString(&resstr, &reslen, restree, stylesheet);
xsltFreeStylesheet(stylesheet);
xmlFreeDoc(restree);
xmlFreeDoc(doctree);
xsltCleanupGlobals();
xmlCleanupParser();
if (resstat < 0) {
PG_RETURN_NULL();
}
tres = palloc(reslen + VARHDRSZ);
memcpy(VARDATA(tres),resstr,reslen);
VARATT_SIZEP(tres) = reslen + VARHDRSZ;
PG_RETURN_TEXT_P(tres);
xsltFreeStylesheet(stylesheet);
xmlFreeDoc(restree);
xmlFreeDoc(doctree);
xsltCleanupGlobals();
xmlCleanupParser();
if (resstat < 0)
PG_RETURN_NULL();
tres = palloc(reslen + VARHDRSZ);
memcpy(VARDATA(tres), resstr, reslen);
VARATT_SIZEP(tres) = reslen + VARHDRSZ;
PG_RETURN_TEXT_P(tres);
}
void parse_params(const char **params, text *paramstr)
void
parse_params(const char **params, text *paramstr)
{
char *pos;
char *pstr;
char *pos;
char *pstr;
int i;
char *nvsep="=";
char *itsep=",";
int i;
char *nvsep = "=";
char *itsep = ",";
pstr = GET_STR(paramstr);
pstr = GET_STR(paramstr);
pos=pstr;
for (i=0; i < MAXPARAMS; i++)
{
params[i] = pos;
pos = strstr(pos,nvsep);
if (pos != NULL) {
*pos = '\0';
pos++;
} else {
params[i]=NULL;
break;
}
/* Value */
i++;
params[i]=pos;
pos = strstr(pos,itsep);
if (pos != NULL) {
*pos = '\0';
pos++;
} else {
break;
}
pos = pstr;
}
if (i < MAXPARAMS)
{
params[i+1]=NULL;
}
for (i = 0; i < MAXPARAMS; i++)
{
params[i] = pos;
pos = strstr(pos, nvsep);
if (pos != NULL)
{
*pos = '\0';
pos++;
}
else
{
params[i] = NULL;
break;
}
/* Value */
i++;
params[i] = pos;
pos = strstr(pos, itsep);
if (pos != NULL)
{
*pos = '\0';
pos++;
}
else
break;
}
if (i < MAXPARAMS)
params[i + 1] = NULL;
}