@@ -19,14 +19,17 @@ Datumgin_extract_trgm(PG_FUNCTION_ARGS);
1919PG_FUNCTION_INFO_V1 (gin_trgm_consistent );
2020Datum gin_trgm_consistent (PG_FUNCTION_ARGS );
2121
22+ /*
23+ * This function is used as both extractValue and extractQuery
24+ */
2225Datum
2326gin_extract_trgm (PG_FUNCTION_ARGS )
2427{
2528text * val = (text * )PG_GETARG_TEXT_P (0 );
2629int32 * nentries = (int32 * )PG_GETARG_POINTER (1 );
2730Datum * entries = NULL ;
2831TRGM * trg ;
29- int4 trglen ;
32+ int32 trglen ;
3033
3134* nentries = 0 ;
3235
@@ -36,31 +39,19 @@ gin_extract_trgm(PG_FUNCTION_ARGS)
3639if (trglen > 0 )
3740{
3841trgm * ptr ;
39- int4 i = 0 ,
40- item ;
42+ int32 i ;
4143
42- * nentries = ( int32 ) trglen ;
44+ * nentries = trglen ;
4345entries = (Datum * )palloc (sizeof (Datum )* trglen );
4446
4547ptr = GETARR (trg );
46- while ( ptr - GETARR ( trg ) < ARRNELEM ( trg ) )
48+ for ( i = 0 ; i < trglen ; i ++ )
4749{
48- item = trgm2int (ptr );
49- entries [i ++ ]= Int32GetDatum (item );
50+ int32 item = trgm2int (ptr );
5051
52+ entries [i ]= Int32GetDatum (item );
5153ptr ++ ;
5254}
53- if (PG_NARGS ()> 4 )
54- {
55- /*
56- * Function called from query extracting
57- */
58- Pointer * * extra_data = (Pointer * * )PG_GETARG_POINTER (4 );
59-
60- * extra_data = (Pointer * )palloc0 (sizeof (Pointer )* (* nentries ));
61-
62- * (int32 * ) (* extra_data )= trglen ;
63- }
6455}
6556
6657PG_RETURN_POINTER (entries );
@@ -70,30 +61,29 @@ Datum
7061gin_trgm_consistent (PG_FUNCTION_ARGS )
7162{
7263bool * check = (bool * )PG_GETARG_POINTER (0 );
73-
7464/* StrategyNumber strategy = PG_GETARG_UINT16(1); */
7565/* text *query = PG_GETARG_TEXT_P(2); */
76- /* int32nkeys = PG_GETARG_INT32(3); */
77- Pointer * extra_data = (Pointer * )PG_GETARG_POINTER (4 );
66+ int32 nkeys = PG_GETARG_INT32 (3 );
67+ /* Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4); */
7868bool * recheck = (bool * )PG_GETARG_POINTER (5 );
7969bool res = FALSE;
80- int4 i ,
81- trglen ,
70+ int32 i ,
8271ntrue = 0 ;
8372
8473/* All cases served by this function are inexact */
8574* recheck = true;
8675
87- trglen = * ( int32 * ) extra_data ;
88-
89- for ( i = 0 ; i < trglen ; i ++ )
76+ /* Count the matches */
77+ for ( i = 0 ; i < nkeys ; i ++ )
78+ {
9079if (check [i ])
9180ntrue ++ ;
81+ }
9282
9383#ifdef DIVUNION
94- res = (trglen == ntrue ) ? true : ((((((float4 )ntrue ) / ((float4 ) (trglen - ntrue )))) >=trgm_limit ) ? true : false);
84+ res = (nkeys == ntrue ) ? true : ((((((float4 )ntrue ) / ((float4 ) (nkeys - ntrue )))) >=trgm_limit ) ? true : false);
9585#else
96- res = (trglen == 0 ) ? false : ((((((float4 )ntrue ) / ((float4 )trglen ))) >=trgm_limit ) ? true : false);
86+ res = (nkeys == 0 ) ? false : ((((((float4 )ntrue ) / ((float4 )nkeys ))) >=trgm_limit ) ? true : false);
9787#endif
9888
9989PG_RETURN_BOOL (res );