PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
spgist_name_ops.c File Reference
#include "postgres.h"
#include "access/spgist.h"
#include "catalog/pg_type.h"
#include "utils/datum.h"
#include "varatt.h"
Include dependency graph for spgist_name_ops.c:

Go to the source code of this file.

Functions

 PG_FUNCTION_INFO_V1 (spgist_name_config)
 
Datum spgist_name_config (PG_FUNCTION_ARGS)
 
static Datum formTextDatum (const char *data, int datalen)
 
static int commonPrefix (const char *a, const char *b, int lena, int lenb)
 
static bool searchChar (Datum *nodeLabels, int nNodes, int16 c, int *i)
 
 PG_FUNCTION_INFO_V1 (spgist_name_choose)
 
Datum spgist_name_choose (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (spgist_name_inner_consistent)
 
Datum spgist_name_inner_consistent (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (spgist_name_leaf_consistent)
 
Datum spgist_name_leaf_consistent (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (spgist_name_compress)
 
Datum spgist_name_compress (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 

Function Documentation

◆ commonPrefix()

static int commonPrefix ( const char *  a,
const char *  b,
int  lena,
int  lenb 
)
static

Definition at line 77 of file spgist_name_ops.c.

78{
79 int i = 0;
80
81 while (i < lena && i < lenb && *a == *b)
82 {
83 a++;
84 b++;
85 i++;
86 }
87
88 return i;
89}
int b
Definition: isn.c:69
int a
Definition: isn.c:68
int i
Definition: isn.c:72

References a, b, and i.

Referenced by spgist_name_choose().

◆ formTextDatum()

static Datum formTextDatum ( const char *  data,
int  datalen 
)
static

Definition at line 52 of file spgist_name_ops.c.

53{
54 char *p;
55
56 p = (char *) palloc(datalen + VARHDRSZ);
57
58 if (datalen + VARHDRSZ_SHORT <= VARATT_SHORT_MAX)
59 {
61 if (datalen)
62 memcpy(p + VARHDRSZ_SHORT, data, datalen);
63 }
64 else
65 {
66 SET_VARSIZE(p, datalen + VARHDRSZ);
67 memcpy(p + VARHDRSZ, data, datalen);
68 }
69
70 return PointerGetDatum(p);
71}
#define VARHDRSZ
Definition: c.h:649
void * palloc(Size size)
Definition: mcxt.c:1317
const void * data
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
#define VARHDRSZ_SHORT
Definition: varatt.h:255
#define SET_VARSIZE_SHORT(PTR, len)
Definition: varatt.h:306
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305
#define VARATT_SHORT_MAX
Definition: varatt.h:257

References data, palloc(), PointerGetDatum(), SET_VARSIZE, SET_VARSIZE_SHORT, VARATT_SHORT_MAX, VARHDRSZ, and VARHDRSZ_SHORT.

Referenced by spgist_name_choose(), and spgist_name_compress().

◆ PG_FUNCTION_INFO_V1() [1/5]

PG_FUNCTION_INFO_V1 ( spgist_name_choose  )

◆ PG_FUNCTION_INFO_V1() [2/5]

PG_FUNCTION_INFO_V1 ( spgist_name_compress  )

◆ PG_FUNCTION_INFO_V1() [3/5]

PG_FUNCTION_INFO_V1 ( spgist_name_config  )

◆ PG_FUNCTION_INFO_V1() [4/5]

PG_FUNCTION_INFO_V1 ( spgist_name_inner_consistent  )

◆ PG_FUNCTION_INFO_V1() [5/5]

PG_FUNCTION_INFO_V1 ( spgist_name_leaf_consistent  )

◆ searchChar()

static bool searchChar ( Datum nodeLabels,
int  nNodes,
int16  c,
int *  i 
)
static

Definition at line 97 of file spgist_name_ops.c.

98{
99 int StopLow = 0,
100 StopHigh = nNodes;
101
102 while (StopLow < StopHigh)
103 {
104 int StopMiddle = (StopLow + StopHigh) >> 1;
105 int16 middle = DatumGetInt16(nodeLabels[StopMiddle]);
106
107 if (c < middle)
108 StopHigh = StopMiddle;
109 else if (c > middle)
110 StopLow = StopMiddle + 1;
111 else
112 {
113 *i = StopMiddle;
114 return true;
115 }
116 }
117
118 *i = StopHigh;
119 return false;
120}
int16_t int16
Definition: c.h:483
static int16 DatumGetInt16(Datum X)
Definition: postgres.h:167
char * c

References DatumGetInt16(), and i.

Referenced by spgist_name_choose().

◆ spgist_name_choose()

Datum spgist_name_choose ( PG_FUNCTION_ARGS  )

Definition at line 124 of file spgist_name_ops.c.

125{
128 Name inName = DatumGetName(in->datum);
129 char *inStr = NameStr(*inName);
130 int inSize = strlen(inStr);
131 char *prefixStr = NULL;
132 int prefixSize = 0;
133 int commonLen = 0;
134 int16 nodeChar = 0;
135 int i = 0;
136
137 /* Check for prefix match, set nodeChar to first byte after prefix */
138 if (in->hasPrefix)
139 {
140 text *prefixText = DatumGetTextPP(in->prefixDatum);
141
142 prefixStr = VARDATA_ANY(prefixText);
143 prefixSize = VARSIZE_ANY_EXHDR(prefixText);
144
145 commonLen = commonPrefix(inStr + in->level,
146 prefixStr,
147 inSize - in->level,
148 prefixSize);
149
150 if (commonLen == prefixSize)
151 {
152 if (inSize - in->level > commonLen)
153 nodeChar = *(unsigned char *) (inStr + in->level + commonLen);
154 else
155 nodeChar = -1;
156 }
157 else
158 {
159 /* Must split tuple because incoming value doesn't match prefix */
161
162 if (commonLen == 0)
163 {
164 out->result.splitTuple.prefixHasPrefix = false;
165 }
166 else
167 {
170 formTextDatum(prefixStr, commonLen);
171 }
174 (Datum *) palloc(sizeof(Datum));
176 Int16GetDatum(*(unsigned char *) (prefixStr + commonLen));
177
179
180 if (prefixSize - commonLen == 1)
181 {
183 }
184 else
185 {
188 formTextDatum(prefixStr + commonLen + 1,
189 prefixSize - commonLen - 1);
190 }
191
193 }
194 }
195 else if (inSize > in->level)
196 {
197 nodeChar = *(unsigned char *) (inStr + in->level);
198 }
199 else
200 {
201 nodeChar = -1;
202 }
203
204 /* Look up nodeChar in the node label array */
205 if (searchChar(in->nodeLabels, in->nNodes, nodeChar, &i))
206 {
207 /*
208 * Descend to existing node. (If in->allTheSame, the core code will
209 * ignore our nodeN specification here, but that's OK. We still have
210 * to provide the correct levelAdd and restDatum values, and those are
211 * the same regardless of which node gets chosen by core.)
212 */
213 int levelAdd;
214
216 out->result.matchNode.nodeN = i;
217 levelAdd = commonLen;
218 if (nodeChar >= 0)
219 levelAdd++;
220 out->result.matchNode.levelAdd = levelAdd;
221 if (inSize - in->level - levelAdd > 0)
223 formTextDatum(inStr + in->level + levelAdd,
224 inSize - in->level - levelAdd);
225 else
227 formTextDatum(NULL, 0);
228 }
229 else if (in->allTheSame)
230 {
231 /*
232 * Can't use AddNode action, so split the tuple. The upper tuple has
233 * the same prefix as before and uses a dummy node label -2 for the
234 * lower tuple. The lower tuple has no prefix and the same node
235 * labels as the original tuple.
236 *
237 * Note: it might seem tempting to shorten the upper tuple's prefix,
238 * if it has one, then use its last byte as label for the lower tuple.
239 * But that doesn't win since we know the incoming value matches the
240 * whole prefix: we'd just end up splitting the lower tuple again.
241 */
250 }
251 else
252 {
253 /* Add a node for the not-previously-seen nodeChar value */
254 out->resultType = spgAddNode;
255 out->result.addNode.nodeLabel = Int16GetDatum(nodeChar);
256 out->result.addNode.nodeN = i;
257 }
258
260}
#define NameStr(name)
Definition: c.h:703
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define DatumGetTextPP(X)
Definition: fmgr.h:292
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
static Name DatumGetName(Datum X)
Definition: postgres.h:365
uintptr_t Datum
Definition: postgres.h:69
static Datum Int16GetDatum(int16 X)
Definition: postgres.h:177
@ spgMatchNode
Definition: spgist.h:69
@ spgAddNode
Definition: spgist.h:70
@ spgSplitTuple
Definition: spgist.h:71
static int commonPrefix(const char *a, const char *b, int lena, int lenb)
static Datum formTextDatum(const char *data, int datalen)
static bool searchChar(Datum *nodeLabels, int nNodes, int16 c, int *i)
Definition: c.h:698
Datum * nodeLabels
Definition: spgist.h:64
bool hasPrefix
Definition: spgist.h:61
Datum prefixDatum
Definition: spgist.h:62
int nNodes
Definition: spgist.h:63
Datum datum
Definition: spgist.h:55
int level
Definition: spgist.h:57
bool allTheSame
Definition: spgist.h:60
bool postfixHasPrefix
Definition: spgist.h:101
int childNodeN
Definition: spgist.h:98
spgChooseResultType resultType
Definition: spgist.h:76
int levelAdd
Definition: spgist.h:82
struct spgChooseOut::@51::@54 splitTuple
Datum nodeLabel
Definition: spgist.h:87
Datum * prefixNodeLabels
Definition: spgist.h:96
Datum postfixPrefixDatum
Definition: spgist.h:102
Datum restDatum
Definition: spgist.h:83
int prefixNNodes
Definition: spgist.h:95
int nodeN
Definition: spgist.h:81
Datum prefixPrefixDatum
Definition: spgist.h:94
struct spgChooseOut::@51::@53 addNode
bool prefixHasPrefix
Definition: spgist.h:93
struct spgChooseOut::@51::@52 matchNode
union spgChooseOut::@51 result
Definition: c.h:644
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317

References spgChooseOut::addNode, spgChooseIn::allTheSame, spgChooseOut::childNodeN, commonPrefix(), spgChooseIn::datum, DatumGetName(), DatumGetTextPP, formTextDatum(), spgChooseIn::hasPrefix, i, Int16GetDatum(), spgChooseIn::level, spgChooseOut::levelAdd, spgChooseOut::matchNode, NameStr, spgChooseIn::nNodes, spgChooseOut::nodeLabel, spgChooseIn::nodeLabels, spgChooseOut::nodeN, palloc(), PG_GETARG_POINTER, PG_RETURN_VOID, spgChooseOut::postfixHasPrefix, spgChooseOut::postfixPrefixDatum, spgChooseIn::prefixDatum, spgChooseOut::prefixHasPrefix, spgChooseOut::prefixNNodes, spgChooseOut::prefixNodeLabels, spgChooseOut::prefixPrefixDatum, spgChooseOut::restDatum, spgChooseOut::result, spgChooseOut::resultType, searchChar(), spgAddNode, spgMatchNode, spgSplitTuple, spgChooseOut::splitTuple, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

◆ spgist_name_compress()

Datum spgist_name_compress ( PG_FUNCTION_ARGS  )

Definition at line 496 of file spgist_name_ops.c.

497{
498 Name inName = PG_GETARG_NAME(0);
499 char *inStr = NameStr(*inName);
500
501 PG_RETURN_DATUM(formTextDatum(inStr, strlen(inStr)));
502}
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353

References formTextDatum(), NameStr, PG_GETARG_NAME, and PG_RETURN_DATUM.

◆ spgist_name_config()

Datum spgist_name_config ( PG_FUNCTION_ARGS  )

Definition at line 34 of file spgist_name_ops.c.

35{
36 /* spgConfigIn *cfgin = (spgConfigIn *) PG_GETARG_POINTER(0); */
38
39 cfg->prefixType = TEXTOID;
40 cfg->labelType = INT2OID;
41 cfg->leafType = TEXTOID;
42 cfg->canReturnData = true;
43 cfg->longValuesOK = true; /* suffixing will shorten long values */
45}
Oid leafType
Definition: spgist.h:45
bool longValuesOK
Definition: spgist.h:47
bool canReturnData
Definition: spgist.h:46
Oid labelType
Definition: spgist.h:44
Oid prefixType
Definition: spgist.h:43

References spgConfigOut::canReturnData, spgConfigOut::labelType, spgConfigOut::leafType, spgConfigOut::longValuesOK, PG_GETARG_POINTER, PG_RETURN_VOID, and spgConfigOut::prefixType.

◆ spgist_name_inner_consistent()

Datum spgist_name_inner_consistent ( PG_FUNCTION_ARGS  )

Definition at line 266 of file spgist_name_ops.c.

267{
270 text *reconstructedValue;
271 text *reconstrText;
272 int maxReconstrLen;
273 text *prefixText = NULL;
274 int prefixSize = 0;
275 int i;
276
277 /*
278 * Reconstruct values represented at this tuple, including parent data,
279 * prefix of this tuple if any, and the node label if it's non-dummy.
280 * in->level should be the length of the previously reconstructed value,
281 * and the number of bytes added here is prefixSize or prefixSize + 1.
282 *
283 * Recall that reconstructedValues are assumed to be the same type as leaf
284 * datums, so we must use "text" not "name" for them.
285 *
286 * Note: we assume that in->reconstructedValue isn't toasted and doesn't
287 * have a short varlena header. This is okay because it must have been
288 * created by a previous invocation of this routine, and we always emit
289 * long-format reconstructed values.
290 */
291 reconstructedValue = (text *) DatumGetPointer(in->reconstructedValue);
292 Assert(reconstructedValue == NULL ? in->level == 0 :
293 VARSIZE_ANY_EXHDR(reconstructedValue) == in->level);
294
295 maxReconstrLen = in->level + 1;
296 if (in->hasPrefix)
297 {
298 prefixText = DatumGetTextPP(in->prefixDatum);
299 prefixSize = VARSIZE_ANY_EXHDR(prefixText);
300 maxReconstrLen += prefixSize;
301 }
302
303 reconstrText = palloc(VARHDRSZ + maxReconstrLen);
304 SET_VARSIZE(reconstrText, VARHDRSZ + maxReconstrLen);
305
306 if (in->level)
307 memcpy(VARDATA(reconstrText),
308 VARDATA(reconstructedValue),
309 in->level);
310 if (prefixSize)
311 memcpy(((char *) VARDATA(reconstrText)) + in->level,
312 VARDATA_ANY(prefixText),
313 prefixSize);
314 /* last byte of reconstrText will be filled in below */
315
316 /*
317 * Scan the child nodes. For each one, complete the reconstructed value
318 * and see if it's consistent with the query. If so, emit an entry into
319 * the output arrays.
320 */
321 out->nodeNumbers = (int *) palloc(sizeof(int) * in->nNodes);
322 out->levelAdds = (int *) palloc(sizeof(int) * in->nNodes);
323 out->reconstructedValues = (Datum *) palloc(sizeof(Datum) * in->nNodes);
324 out->nNodes = 0;
325
326 for (i = 0; i < in->nNodes; i++)
327 {
328 int16 nodeChar = DatumGetInt16(in->nodeLabels[i]);
329 int thisLen;
330 bool res = true;
331 int j;
332
333 /* If nodeChar is a dummy value, don't include it in data */
334 if (nodeChar <= 0)
335 thisLen = maxReconstrLen - 1;
336 else
337 {
338 ((unsigned char *) VARDATA(reconstrText))[maxReconstrLen - 1] = nodeChar;
339 thisLen = maxReconstrLen;
340 }
341
342 for (j = 0; j < in->nkeys; j++)
343 {
344 StrategyNumber strategy = in->scankeys[j].sk_strategy;
345 Name inName;
346 char *inStr;
347 int inSize;
348 int r;
349
350 inName = DatumGetName(in->scankeys[j].sk_argument);
351 inStr = NameStr(*inName);
352 inSize = strlen(inStr);
353
354 r = memcmp(VARDATA(reconstrText), inStr,
355 Min(inSize, thisLen));
356
357 switch (strategy)
358 {
361 if (r > 0)
362 res = false;
363 break;
365 if (r != 0 || inSize < thisLen)
366 res = false;
367 break;
370 if (r < 0)
371 res = false;
372 break;
373 default:
374 elog(ERROR, "unrecognized strategy number: %d",
375 in->scankeys[j].sk_strategy);
376 break;
377 }
378
379 if (!res)
380 break; /* no need to consider remaining conditions */
381 }
382
383 if (res)
384 {
385 out->nodeNumbers[out->nNodes] = i;
386 out->levelAdds[out->nNodes] = thisLen - in->level;
387 SET_VARSIZE(reconstrText, VARHDRSZ + thisLen);
388 out->reconstructedValues[out->nNodes] =
389 datumCopy(PointerGetDatum(reconstrText), false, -1);
390 out->nNodes++;
391 }
392 }
393
395}
#define Min(x, y)
Definition: c.h:961
#define Assert(condition)
Definition: c.h:815
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:132
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
int j
Definition: isn.c:73
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:317
uint16 StrategyNumber
Definition: stratnum.h:22
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define BTLessStrategyNumber
Definition: stratnum.h:29
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
Datum sk_argument
Definition: skey.h:72
StrategyNumber sk_strategy
Definition: skey.h:68
Datum reconstructedValue
Definition: spgist.h:140
ScanKey scankeys
Definition: spgist.h:134
Datum * nodeLabels
Definition: spgist.h:151
Datum * reconstructedValues
Definition: spgist.h:159
#define VARDATA(PTR)
Definition: varatt.h:278

References Assert, BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, datumCopy(), DatumGetInt16(), DatumGetName(), DatumGetPointer(), DatumGetTextPP, elog, ERROR, spgInnerConsistentIn::hasPrefix, i, j, spgInnerConsistentIn::level, spgInnerConsistentOut::levelAdds, Min, NameStr, spgInnerConsistentIn::nkeys, spgInnerConsistentIn::nNodes, spgInnerConsistentOut::nNodes, spgInnerConsistentIn::nodeLabels, spgInnerConsistentOut::nodeNumbers, palloc(), PG_GETARG_POINTER, PG_RETURN_VOID, PointerGetDatum(), spgInnerConsistentIn::prefixDatum, spgInnerConsistentIn::reconstructedValue, spgInnerConsistentOut::reconstructedValues, res, spgInnerConsistentIn::scankeys, SET_VARSIZE, ScanKeyData::sk_argument, ScanKeyData::sk_strategy, VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

◆ spgist_name_leaf_consistent()

Datum spgist_name_leaf_consistent ( PG_FUNCTION_ARGS  )

Definition at line 399 of file spgist_name_ops.c.

400{
403 int level = in->level;
404 text *leafValue,
405 *reconstrValue = NULL;
406 char *fullValue;
407 int fullLen;
408 bool res;
409 int j;
410
411 /* all tests are exact */
412 out->recheck = false;
413
414 leafValue = DatumGetTextPP(in->leafDatum);
415
416 /* As above, in->reconstructedValue isn't toasted or short. */
418 reconstrValue = (text *) DatumGetPointer(in->reconstructedValue);
419
420 Assert(reconstrValue == NULL ? level == 0 :
421 VARSIZE_ANY_EXHDR(reconstrValue) == level);
422
423 /* Reconstruct the Name represented by this leaf tuple */
424 fullValue = palloc0(NAMEDATALEN);
425 fullLen = level + VARSIZE_ANY_EXHDR(leafValue);
426 Assert(fullLen < NAMEDATALEN);
427 if (VARSIZE_ANY_EXHDR(leafValue) == 0 && level > 0)
428 {
429 memcpy(fullValue, VARDATA(reconstrValue),
430 VARSIZE_ANY_EXHDR(reconstrValue));
431 }
432 else
433 {
434 if (level)
435 memcpy(fullValue, VARDATA(reconstrValue), level);
436 if (VARSIZE_ANY_EXHDR(leafValue) > 0)
437 memcpy(fullValue + level, VARDATA_ANY(leafValue),
438 VARSIZE_ANY_EXHDR(leafValue));
439 }
440 out->leafValue = PointerGetDatum(fullValue);
441
442 /* Perform the required comparison(s) */
443 res = true;
444 for (j = 0; j < in->nkeys; j++)
445 {
446 StrategyNumber strategy = in->scankeys[j].sk_strategy;
447 Name queryName = DatumGetName(in->scankeys[j].sk_argument);
448 char *queryStr = NameStr(*queryName);
449 int queryLen = strlen(queryStr);
450 int r;
451
452 /* Non-collation-aware comparison */
453 r = memcmp(fullValue, queryStr, Min(queryLen, fullLen));
454
455 if (r == 0)
456 {
457 if (queryLen > fullLen)
458 r = -1;
459 else if (queryLen < fullLen)
460 r = 1;
461 }
462
463 switch (strategy)
464 {
466 res = (r < 0);
467 break;
469 res = (r <= 0);
470 break;
472 res = (r == 0);
473 break;
475 res = (r >= 0);
476 break;
478 res = (r > 0);
479 break;
480 default:
481 elog(ERROR, "unrecognized strategy number: %d",
482 in->scankeys[j].sk_strategy);
483 res = false;
484 break;
485 }
486
487 if (!res)
488 break; /* no need to consider remaining conditions */
489 }
490
492}
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
void * palloc0(Size size)
Definition: mcxt.c:1347
#define NAMEDATALEN
ScanKey scankeys
Definition: spgist.h:169
Datum reconstructedValue
Definition: spgist.h:175

References Assert, BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, DatumGetName(), DatumGetPointer(), DatumGetTextPP, elog, ERROR, j, spgLeafConsistentIn::leafDatum, spgLeafConsistentOut::leafValue, spgLeafConsistentIn::level, Min, NAMEDATALEN, NameStr, spgLeafConsistentIn::nkeys, palloc0(), PG_GETARG_POINTER, PG_RETURN_BOOL, PointerGetDatum(), spgLeafConsistentOut::recheck, spgLeafConsistentIn::reconstructedValue, res, spgLeafConsistentIn::scankeys, ScanKeyData::sk_argument, ScanKeyData::sk_strategy, VARDATA, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 29 of file spgist_name_ops.c.