PostgreSQL Source Code git master
Loading...
Searching...
No Matches
hstore_gin.c
Go to the documentation of this file.
1/*
2 * contrib/hstore/hstore_gin.c
3 */
4#include "postgres.h"
5
6#include "access/gin.h"
7#include "access/stratnum.h"
8#include "catalog/pg_type.h"
9
10#include "hstore.h"
11
12
13/*
14 * When using a GIN index for hstore, we choose to index both keys and values.
15 * The storage format is "text" values, with K, V, or N prepended to the string
16 * to indicate key, value, or null values. (As of 9.1 it might be better to
17 * store null values as nulls, but we'll keep it this way for on-disk
18 * compatibility.)
19 */
20#define KEYFLAG 'K'
21#define VALFLAG 'V'
22#define NULLFLAG 'N'
23
25
26/* Build an indexable text value */
27static text *
28makeitem(char *str, int len, char flag)
29{
30 text *item;
31
32 item = (text *) palloc(VARHDRSZ + len + 1);
33 SET_VARSIZE(item, VARHDRSZ + len + 1);
34
35 *VARDATA(item) = flag;
36
37 if (str && len > 0)
38 memcpy(VARDATA(item) + 1, str, len);
39
40 return item;
41}
42
45{
47 int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
48 Datum *entries = NULL;
50 char *ptr = STRPTR(hs);
51 int count = HS_COUNT(hs);
52 int i;
53
54 *nentries = 2 * count;
55 if (count)
56 entries = (Datum *) palloc(sizeof(Datum) * 2 * count);
57
58 for (i = 0; i < count; ++i)
59 {
60 text *item;
61
62 item = makeitem(HSTORE_KEY(hsent, ptr, i),
64 KEYFLAG);
65 entries[2 * i] = PointerGetDatum(item);
66
68 item = makeitem(NULL, 0, NULLFLAG);
69 else
70 item = makeitem(HSTORE_VAL(hsent, ptr, i),
72 VALFLAG);
73 entries[2 * i + 1] = PointerGetDatum(item);
74 }
75
76 PG_RETURN_POINTER(entries);
77}
78
80
83{
84 int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
85 StrategyNumber strategy = PG_GETARG_UINT16(2);
86 int32 *searchMode = (int32 *) PG_GETARG_POINTER(6);
87 Datum *entries;
88
89 if (strategy == HStoreContainsStrategyNumber)
90 {
91 /* Query is an hstore, so just apply gin_extract_hstore... */
92 entries = (Datum *)
95 PointerGetDatum(nentries)));
96 /* ... except that "contains {}" requires a full index scan */
97 if (entries == NULL)
98 *searchMode = GIN_SEARCH_MODE_ALL;
99 }
100 else if (strategy == HStoreExistsStrategyNumber)
101 {
102 text *query = PG_GETARG_TEXT_PP(0);
103 text *item;
104
105 *nentries = 1;
106 entries = (Datum *) palloc(sizeof(Datum));
107 item = makeitem(VARDATA_ANY(query), VARSIZE_ANY_EXHDR(query), KEYFLAG);
108 entries[0] = PointerGetDatum(item);
109 }
110 else if (strategy == HStoreExistsAnyStrategyNumber ||
112 {
115 bool *key_nulls;
116 int key_count;
117 int i,
118 j;
119 text *item;
120
122
123 entries = (Datum *) palloc(sizeof(Datum) * key_count);
124
125 for (i = 0, j = 0; i < key_count; ++i)
126 {
127 /* Nulls in the array are ignored, cf hstoreArrayToPairs */
128 if (key_nulls[i])
129 continue;
131 entries[j++] = PointerGetDatum(item);
132 }
133
134 *nentries = j;
135 /* ExistsAll with no keys should match everything */
136 if (j == 0 && strategy == HStoreExistsAllStrategyNumber)
137 *searchMode = GIN_SEARCH_MODE_ALL;
138 }
139 else
140 {
141 elog(ERROR, "unrecognized strategy number: %d", strategy);
142 entries = NULL; /* keep compiler quiet */
143 }
144
145 PG_RETURN_POINTER(entries);
146}
147
149
150Datum
152{
153 bool *check = (bool *) PG_GETARG_POINTER(0);
154 StrategyNumber strategy = PG_GETARG_UINT16(1);
155#ifdef NOT_USED
156 HStore *query = PG_GETARG_HSTORE_P(2);
157#endif
158 int32 nkeys = PG_GETARG_INT32(3);
159#ifdef NOT_USED
160 Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4);
161#endif
162 bool *recheck = (bool *) PG_GETARG_POINTER(5);
163 bool res = true;
164 int32 i;
165
166 if (strategy == HStoreContainsStrategyNumber)
167 {
168 /*
169 * Index doesn't have information about correspondence of keys and
170 * values, so we need recheck. However, if not all the keys are
171 * present, we can fail at once.
172 */
173 *recheck = true;
174 for (i = 0; i < nkeys; i++)
175 {
176 if (!check[i])
177 {
178 res = false;
179 break;
180 }
181 }
182 }
183 else if (strategy == HStoreExistsStrategyNumber)
184 {
185 /* Existence of key is guaranteed in default search mode */
186 *recheck = false;
187 res = true;
188 }
189 else if (strategy == HStoreExistsAnyStrategyNumber)
190 {
191 /* Existence of key is guaranteed in default search mode */
192 *recheck = false;
193 res = true;
194 }
195 else if (strategy == HStoreExistsAllStrategyNumber)
196 {
197 /* Testing for all the keys being present gives an exact result */
198 *recheck = false;
199 for (i = 0; i < nkeys; i++)
200 {
201 if (!check[i])
202 {
203 res = false;
204 break;
205 }
206 }
207 }
208 else
209 elog(ERROR, "unrecognized strategy number: %d", strategy);
210
211 PG_RETURN_BOOL(res);
212}
#define PG_GETARG_ARRAYTYPE_P(n)
Definition array.h:263
void deconstruct_array_builtin(const ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
#define VARHDRSZ
Definition c.h:711
int32_t int32
Definition c.h:542
void * Pointer
Definition c.h:537
#define ARRPTR(x)
Definition cube.c:28
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
#define DirectFunctionCall2(func, arg1, arg2)
Definition fmgr.h:686
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
#define PG_GETARG_DATUM(n)
Definition fmgr.h:268
#define PG_FUNCTION_INFO_V1(funcname)
Definition fmgr.h:417
#define PG_GETARG_UINT16(n)
Definition fmgr.h:272
#define PG_GETARG_INT32(n)
Definition fmgr.h:269
#define PG_RETURN_POINTER(x)
Definition fmgr.h:363
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
#define GIN_SEARCH_MODE_ALL
Definition gin.h:38
const char * str
#define HStoreExistsAllStrategyNumber
Definition hstore.h:183
#define HStoreExistsStrategyNumber
Definition hstore.h:181
#define HS_COUNT(hsp_)
Definition hstore.h:61
#define HSTORE_KEY(arr_, str_, i_)
Definition hstore.h:79
#define PG_GETARG_HSTORE_P(x)
Definition hstore.h:154
#define HStoreExistsAnyStrategyNumber
Definition hstore.h:182
#define HStoreContainsStrategyNumber
Definition hstore.h:180
#define HSTORE_VALISNULL(arr_, i_)
Definition hstore.h:83
#define HSTORE_VALLEN(arr_, i_)
Definition hstore.h:82
#define HSTORE_KEYLEN(arr_, i_)
Definition hstore.h:81
#define HSTORE_VAL(arr_, str_, i_)
Definition hstore.h:80
#define STRPTR(x)
Definition hstore.h:76
static text * makeitem(char *str, int len, char flag)
Definition hstore_gin.c:28
#define VALFLAG
Definition hstore_gin.c:21
Datum gin_consistent_hstore(PG_FUNCTION_ARGS)
Definition hstore_gin.c:151
Datum gin_extract_hstore_query(PG_FUNCTION_ARGS)
Definition hstore_gin.c:82
#define NULLFLAG
Definition hstore_gin.c:22
Datum gin_extract_hstore(PG_FUNCTION_ARGS)
Definition hstore_gin.c:44
#define KEYFLAG
Definition hstore_gin.c:20
int j
Definition isn.c:78
int i
Definition isn.c:77
void * palloc(Size size)
Definition mcxt.c:1387
const void size_t len
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:342
static int fb(int x)
uint16 StrategyNumber
Definition stratnum.h:22
Definition c.h:706
char * flag(int b)
Definition test-ctype.c:33
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition varatt.h:472
static Size VARSIZE(const void *PTR)
Definition varatt.h:298
static char * VARDATA(const void *PTR)
Definition varatt.h:305
static char * VARDATA_ANY(const void *PTR)
Definition varatt.h:486
static void SET_VARSIZE(void *PTR, Size len)
Definition varatt.h:432