1 /*-------------------------------------------------------------------------
3 * Portions Copyright (c) 2007-2012, PostgreSQL Global Development Group
7 * Support full text search using bigrams.
8 * Author: NTT DATA Corporation
10 *-------------------------------------------------------------------------
16 #include "access/gin.h"
17 #include "access/gin_private.h"
18 #include "access/itup.h"
19 #include "access/skey.h"
20 #include "access/tuptoaster.h"
21 #include "catalog/pg_type.h"
23 #include "mb/pg_wchar.h"
24 #include "storage/bufmgr.h"
25 #include "storage/bufpage.h"
26 #include "tsearch/ts_locale.h"
27 #include "utils/array.h"
28 #include "utils/builtins.h"
31 PG_FUNCTION_INFO_V1(gin_extract_value_bigm);
32 Datum gin_extract_value_bigm(PG_FUNCTION_ARGS);
34 PG_FUNCTION_INFO_V1(gin_extract_query_bigm);
35 Datum gin_extract_query_bigm(PG_FUNCTION_ARGS);
37 PG_FUNCTION_INFO_V1(gin_bigm_consistent);
38 Datum gin_bigm_consistent(PG_FUNCTION_ARGS);
40 PG_FUNCTION_INFO_V1(gin_bigm_compare_partial);
41 Datum gin_bigm_compare_partial(PG_FUNCTION_ARGS);
43 PG_FUNCTION_INFO_V1(pg_gin_pending_stats);
44 Datum pg_gin_pending_stats(PG_FUNCTION_ARGS);
47 gin_extract_value_bigm(PG_FUNCTION_ARGS)
49 text *val = (text *) PG_GETARG_TEXT_P(0);
50 int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
51 Datum *entries = NULL;
57 bgm = generate_bigm(VARDATA(val), VARSIZE(val) - VARHDRSZ);
58 bgmlen = ARRNELEM(bgm);
66 entries = (Datum *) palloc(sizeof(Datum) * bgmlen);
69 for (i = 0; i < bgmlen; i++)
71 text *item = cstring_to_text_with_len(ptr->str, ptr->bytelen);
72 entries[i] = PointerGetDatum(item);
77 PG_RETURN_POINTER(entries);
81 gin_extract_query_bigm(PG_FUNCTION_ARGS)
83 text *val = (text *) PG_GETARG_TEXT_P(0);
84 int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
85 StrategyNumber strategy = PG_GETARG_UINT16(2);
87 bool **pmatch = (bool **) PG_GETARG_POINTER(3);
88 Pointer **extra_data = (Pointer **) PG_GETARG_POINTER(4);
89 /* bool **nullFlags = (bool **) PG_GETARG_POINTER(5); */
90 int32 *searchMode = (int32 *) PG_GETARG_POINTER(6);
91 Datum *entries = NULL;
100 case LikeStrategyNumber:
102 char *str = VARDATA(val);
103 int slen = VARSIZE(val) - VARHDRSZ;
107 * For wildcard search we extract all the bigrams that every
108 * potentially-matching string must include.
110 bgm = generate_wildcard_bigm(str, slen, &removeDups);
111 bgmlen = ARRNELEM(bgm);
114 * Check whether the heap tuple fetched by index search needs to be
115 * rechecked against the query. If the search word consists of one
116 * or two characters and doesn't contain any space character, we can
117 * guarantee that the index test would be exact. That is, the heap
118 * tuple does match the query, so it doesn't need to be rechecked.
120 *extra_data = (Pointer *) palloc(sizeof(bool));
121 recheck = (bool *) *extra_data;
122 if (bgmlen == 1 && !removeDups)
127 for (sp = str; (sp - str) < slen;)
135 sp += IS_HIGHBIT_SET(*sp) ? pg_mblen(sp) : 1;
143 elog(ERROR, "unrecognized strategy number: %d", strategy);
144 bgm = NULL; /* keep compiler quiet */
148 *nentries = (bigm_gin_key_limit == 0) ?
149 bgmlen : Min(bigm_gin_key_limit, bgmlen);
154 entries = (Datum *) palloc(sizeof(Datum) * *nentries);
156 for (i = 0; i < *nentries; i++)
163 *pmatch = (bool *) palloc0(sizeof(bool) * *nentries);
166 item = cstring_to_text_with_len(ptr->str, ptr->bytelen);
167 entries[i] = PointerGetDatum(item);
173 * If no bigram was extracted then we have to scan all the index.
176 *searchMode = GIN_SEARCH_MODE_ALL;
178 PG_RETURN_POINTER(entries);
182 gin_bigm_consistent(PG_FUNCTION_ARGS)
184 bool *check = (bool *) PG_GETARG_POINTER(0);
185 StrategyNumber strategy = PG_GETARG_UINT16(1);
187 /* text *query = PG_GETARG_TEXT_P(2); */
188 int32 nkeys = PG_GETARG_INT32(3);
190 Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4);
191 bool *recheck = (bool *) PG_GETARG_POINTER(5);
198 * Don't recheck the heap tuple against the query if either
199 * pg_bigm.enable_recheck is disabled or the search word is
200 * the special one so that the index can return the exact
203 *recheck = bigm_enable_recheck &&
204 ((nkeys > 1) || *((bool *) extra_data));
208 case LikeStrategyNumber:
209 /* Check if all extracted bigrams are presented. */
211 for (i = 0; i < nkeys; i++)
221 elog(ERROR, "unrecognized strategy number: %d", strategy);
222 res = false; /* keep compiler quiet */
230 gin_bigm_compare_partial(PG_FUNCTION_ARGS)
232 text *arg1 = PG_GETARG_TEXT_PP(0);
233 text *arg2 = PG_GETARG_TEXT_PP(1);
240 a1p = VARDATA_ANY(arg1);
241 a2p = VARDATA_ANY(arg2);
243 mblen1 = pg_mblen(a1p);
244 mblen2 = pg_mblen(a2p);
246 if (mblen1 != mblen2)
249 res = memcmp(a1p, a2p, mblen1) ? 1 : 0;
250 PG_RETURN_INT32(res);
254 * Report both number of pages and number of heap tuples that
255 * are in the pending list.
258 pg_gin_pending_stats(PG_FUNCTION_ARGS)
260 Oid indexOid = PG_GETARG_OID(0);
264 GinMetaPageData *metadata;
271 * Obtain statistic information from the meta page
273 indexRel = index_open(indexOid, AccessShareLock);
274 metabuffer = ReadBuffer(indexRel, GIN_METAPAGE_BLKNO);
275 LockBuffer(metabuffer, GIN_SHARE);
276 metapage = BufferGetPage(metabuffer);
277 metadata = GinPageGetMeta(metapage);
278 index_close(indexRel, AccessShareLock);
281 * Construct a tuple descriptor for the result row. This must
282 * match this function's pg_bigm--x.x.sql entry.
284 tupdesc = CreateTemplateTupleDesc(2, false);
285 TupleDescInitEntry(tupdesc, (AttrNumber) 1,
286 "pages", INT4OID, -1, 0);
287 TupleDescInitEntry(tupdesc, (AttrNumber) 2,
288 "tuples", INT8OID, -1, 0);
289 tupdesc = BlessTupleDesc(tupdesc);
292 values[0] = Int32GetDatum(metadata->nPendingPages);
296 values[1] = Int64GetDatum(metadata->nPendingHeapTuples);
299 UnlockReleaseBuffer(metabuffer);
301 tuple = heap_form_tuple(tupdesc, values, isnull);
302 PG_RETURN_DATUM(HeapTupleGetDatum(tuple));