2008-05-17 09:28:26 +08:00
|
|
|
/*
|
2008-07-11 19:56:48 +08:00
|
|
|
* $PostgreSQL: pgsql/contrib/pg_trgm/trgm_gin.c,v 1.5 2008/07/11 11:56:48 teodor Exp $
|
2008-05-17 09:28:26 +08:00
|
|
|
*/
|
2007-03-14 22:15:40 +08:00
|
|
|
#include "trgm.h"
|
|
|
|
|
|
|
|
#include "access/gin.h"
|
|
|
|
#include "access/itup.h"
|
|
|
|
#include "access/tuptoaster.h"
|
|
|
|
#include "storage/bufpage.h"
|
|
|
|
#include "utils/array.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(gin_extract_trgm);
|
|
|
|
Datum gin_extract_trgm(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(gin_trgm_consistent);
|
|
|
|
Datum gin_trgm_consistent(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gin_extract_trgm(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
text *val = (text *) PG_GETARG_TEXT_P(0);
|
|
|
|
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
|
|
|
|
Datum *entries = NULL;
|
|
|
|
TRGM *trg;
|
2007-03-14 22:15:40 +08:00
|
|
|
int4 trglen;
|
2007-11-16 05:14:46 +08:00
|
|
|
|
2007-03-14 22:15:40 +08:00
|
|
|
*nentries = 0;
|
2007-11-16 05:14:46 +08:00
|
|
|
|
2007-03-14 22:15:40 +08:00
|
|
|
trg = generate_trgm(VARDATA(val), VARSIZE(val) - VARHDRSZ);
|
|
|
|
trglen = ARRNELEM(trg);
|
2007-11-16 05:14:46 +08:00
|
|
|
|
2007-03-14 22:15:40 +08:00
|
|
|
if (trglen > 0)
|
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
trgm *ptr;
|
|
|
|
int4 i = 0,
|
|
|
|
item;
|
|
|
|
|
2007-03-14 22:15:40 +08:00
|
|
|
*nentries = (int32) trglen;
|
|
|
|
entries = (Datum *) palloc(sizeof(Datum) * trglen);
|
|
|
|
|
|
|
|
ptr = GETARR(trg);
|
|
|
|
while (ptr - GETARR(trg) < ARRNELEM(trg))
|
|
|
|
{
|
|
|
|
item = TRGMINT(ptr);
|
|
|
|
entries[i++] = Int32GetDatum(item);
|
2007-11-16 05:14:46 +08:00
|
|
|
|
2007-03-14 22:15:40 +08:00
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(entries);
|
|
|
|
}
|
|
|
|
|
2008-07-11 19:56:48 +08:00
|
|
|
/*
|
|
|
|
* Per call strage for consistent functions to
|
|
|
|
* cache computed value from query
|
|
|
|
*/
|
|
|
|
typedef struct PerCallConsistentStorage {
|
|
|
|
int trglen;
|
|
|
|
text data[1]; /* query */
|
|
|
|
} PerCallConsistentStorage;
|
|
|
|
#define PCCSHDR_SZ offsetof(PerCallConsistentStorage, data)
|
|
|
|
|
2007-03-14 22:15:40 +08:00
|
|
|
Datum
|
|
|
|
gin_trgm_consistent(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-11-16 05:14:46 +08:00
|
|
|
bool *check = (bool *) PG_GETARG_POINTER(0);
|
2008-04-15 01:05:34 +08:00
|
|
|
/* StrategyNumber strategy = PG_GETARG_UINT16(1); */
|
|
|
|
text *query = PG_GETARG_TEXT_P(2);
|
|
|
|
bool *recheck = (bool *) PG_GETARG_POINTER(3);
|
2007-03-14 22:15:40 +08:00
|
|
|
bool res = FALSE;
|
|
|
|
int4 i,
|
|
|
|
trglen,
|
|
|
|
ntrue = 0;
|
2008-07-11 19:56:48 +08:00
|
|
|
PerCallConsistentStorage *pccs = (PerCallConsistentStorage*) fcinfo->flinfo->fn_extra;
|
2007-11-16 05:14:46 +08:00
|
|
|
|
2008-04-15 01:05:34 +08:00
|
|
|
/* All cases served by this function are inexact */
|
|
|
|
*recheck = true;
|
|
|
|
|
2008-07-11 19:56:48 +08:00
|
|
|
if ( pccs == NULL || VARSIZE(pccs->data) != VARSIZE(query) || memcmp( pccs->data, query, VARSIZE(query) ) !=0 )
|
|
|
|
{
|
|
|
|
TRGM *trg = generate_trgm(VARDATA(query), VARSIZE(query) - VARHDRSZ);
|
|
|
|
|
|
|
|
if ( pccs )
|
|
|
|
pfree(pccs);
|
|
|
|
|
|
|
|
fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
|
|
|
|
VARSIZE(query) + PCCSHDR_SZ);
|
|
|
|
pccs = (PerCallConsistentStorage*) fcinfo->flinfo->fn_extra;
|
|
|
|
|
|
|
|
pccs->trglen = ARRNELEM(trg);
|
|
|
|
memcpy( pccs->data, query, VARSIZE(query) );
|
|
|
|
}
|
|
|
|
|
|
|
|
trglen = pccs->trglen;
|
2007-11-16 05:14:46 +08:00
|
|
|
|
2007-03-14 22:15:40 +08:00
|
|
|
for (i = 0; i < trglen; i++)
|
|
|
|
if (check[i])
|
2007-11-16 05:14:46 +08:00
|
|
|
ntrue++;
|
2007-03-14 22:15:40 +08:00
|
|
|
|
|
|
|
#ifdef DIVUNION
|
|
|
|
res = (trglen == ntrue) ? true : ((((((float4) ntrue) / ((float4) (trglen - ntrue)))) >= trgm_limit) ? true : false);
|
|
|
|
#else
|
|
|
|
res = (trglen == 0) ? false : ((((((float4) ntrue) / ((float4) trglen))) >= trgm_limit) ? true : false);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(res);
|
|
|
|
}
|