6 #include "access/gin.h"
7 #include "access/itup.h"
8 #include "access/tuptoaster.h"
9 #include "storage/bufpage.h"
10 #include "utils/array.h"
11 #include "utils/builtins.h"
13 PG_FUNCTION_INFO_V1(gin_extract_trgm
);
14 Datum
gin_extract_trgm(PG_FUNCTION_ARGS
);
16 PG_FUNCTION_INFO_V1(gin_trgm_consistent
);
17 Datum
gin_trgm_consistent(PG_FUNCTION_ARGS
);
20 gin_extract_trgm(PG_FUNCTION_ARGS
)
22 text
*val
= (text
*) PG_GETARG_TEXT_P(0);
23 int32
*nentries
= (int32
*) PG_GETARG_POINTER(1);
24 Datum
*entries
= NULL
;
30 trg
= generate_trgm(VARDATA(val
), VARSIZE(val
) - VARHDRSZ
);
31 trglen
= ARRNELEM(trg
);
39 *nentries
= (int32
) trglen
;
40 entries
= (Datum
*) palloc(sizeof(Datum
) * trglen
);
43 while (ptr
- GETARR(trg
) < ARRNELEM(trg
))
46 entries
[i
++] = Int32GetDatum(item
);
52 PG_RETURN_POINTER(entries
);
56 * Per call strage for consistent functions to
57 * cache computed value from query
59 typedef struct PerCallConsistentStorage
{
61 text data
[1]; /* query */
62 } PerCallConsistentStorage
;
63 #define PCCSHDR_SZ offsetof(PerCallConsistentStorage, data)
66 gin_trgm_consistent(PG_FUNCTION_ARGS
)
68 bool *check
= (bool *) PG_GETARG_POINTER(0);
69 /* StrategyNumber strategy = PG_GETARG_UINT16(1); */
70 text
*query
= PG_GETARG_TEXT_P(2);
71 bool *recheck
= (bool *) PG_GETARG_POINTER(3);
76 PerCallConsistentStorage
*pccs
= (PerCallConsistentStorage
*) fcinfo
->flinfo
->fn_extra
;
78 /* All cases served by this function are inexact */
81 if ( pccs
== NULL
|| VARSIZE(pccs
->data
) != VARSIZE(query
) || memcmp( pccs
->data
, query
, VARSIZE(query
) ) !=0 )
83 TRGM
*trg
= generate_trgm(VARDATA(query
), VARSIZE(query
) - VARHDRSZ
);
88 fcinfo
->flinfo
->fn_extra
= MemoryContextAlloc(fcinfo
->flinfo
->fn_mcxt
,
89 VARSIZE(query
) + PCCSHDR_SZ
);
90 pccs
= (PerCallConsistentStorage
*) fcinfo
->flinfo
->fn_extra
;
92 pccs
->trglen
= ARRNELEM(trg
);
93 memcpy( pccs
->data
, query
, VARSIZE(query
) );
96 trglen
= pccs
->trglen
;
98 for (i
= 0; i
< trglen
; i
++)
103 res
= (trglen
== ntrue
) ? true : ((((((float4
) ntrue
) / ((float4
) (trglen
- ntrue
)))) >= trgm_limit
) ? true : false);
105 res
= (trglen
== 0) ? false : ((((((float4
) ntrue
) / ((float4
) trglen
))) >= trgm_limit
) ? true : false);