PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
to_tsany.c File Reference
#include "postgres.h"
#include "tsearch/ts_cache.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
Include dependency graph for to_tsany.c:

Go to the source code of this file.

Data Structures

struct  MorphOpaque
 

Typedefs

typedef struct MorphOpaque MorphOpaque
 

Functions

Datum get_current_ts_config (PG_FUNCTION_ARGS)
 
static int compareWORD (const void *a, const void *b)
 
static int uniqueWORD (ParsedWord *a, int32 l)
 
TSVector make_tsvector (ParsedText *prs)
 
Datum to_tsvector_byid (PG_FUNCTION_ARGS)
 
Datum to_tsvector (PG_FUNCTION_ARGS)
 
static void pushval_morph (Datum opaque, TSQueryParserState state, char *strval, int lenval, int16 weight, bool prefix)
 
Datum to_tsquery_byid (PG_FUNCTION_ARGS)
 
Datum to_tsquery (PG_FUNCTION_ARGS)
 
Datum plainto_tsquery_byid (PG_FUNCTION_ARGS)
 
Datum plainto_tsquery (PG_FUNCTION_ARGS)
 
Datum phraseto_tsquery_byid (PG_FUNCTION_ARGS)
 
Datum phraseto_tsquery (PG_FUNCTION_ARGS)
 

Typedef Documentation

Function Documentation

static int compareWORD ( const void *  a,
const void *  b 
)
static

Definition at line 38 of file to_tsany.c.

References tsCompareString(), and word().

Referenced by uniqueWORD().

39 {
40  int res;
41 
42  res = tsCompareString(
43  ((const ParsedWord *) a)->word, ((const ParsedWord *) a)->len,
44  ((const ParsedWord *) b)->word, ((const ParsedWord *) b)->len,
45  false);
46 
47  if (res == 0)
48  {
49  if (((const ParsedWord *) a)->pos.pos == ((const ParsedWord *) b)->pos.pos)
50  return 0;
51 
52  res = (((const ParsedWord *) a)->pos.pos > ((const ParsedWord *) b)->pos.pos) ? 1 : -1;
53  }
54 
55  return res;
56 }
int32 tsCompareString(char *a, int lena, char *b, int lenb, bool prefix)
Definition: tsvector_op.c:1160
static void word(struct vars *, int, struct state *, struct state *)
Definition: regcomp.c:1245
Datum get_current_ts_config ( PG_FUNCTION_ARGS  )

Definition at line 29 of file to_tsany.c.

References getTSCurrentConfig(), and PG_RETURN_OID.

30 {
32 }
Oid getTSCurrentConfig(bool emitError)
Definition: ts_cache.c:556
#define PG_RETURN_OID(x)
Definition: fmgr.h:304
TSVector make_tsvector ( ParsedText prs)

Definition at line 145 of file to_tsany.c.

References ParsedWord::alen, ARRPTR, CALCDATASIZE, ParsedText::curwords, elog, ereport, errcode(), errmsg(), ERROR, WordEntry::haspos, i, WordEntry::len, ParsedWord::len, MAXSTRPOS, palloc0(), pfree(), WordEntry::pos, ParsedWord::pos, POSDATAPTR, SET_VARSIZE, SHORTALIGN, TSVectorData::size, STRPTR, uniqueWORD(), WEP_SETPOS, WEP_SETWEIGHT, ParsedWord::word, and ParsedText::words.

Referenced by to_tsvector_byid(), and tsvector_update_trigger().

146 {
147  int i,
148  j,
149  lenstr = 0,
150  totallen;
151  TSVector in;
152  WordEntry *ptr;
153  char *str;
154  int stroff;
155 
156  prs->curwords = uniqueWORD(prs->words, prs->curwords);
157  for (i = 0; i < prs->curwords; i++)
158  {
159  lenstr += prs->words[i].len;
160  if (prs->words[i].alen)
161  {
162  lenstr = SHORTALIGN(lenstr);
163  lenstr += sizeof(uint16) + prs->words[i].pos.apos[0] * sizeof(WordEntryPos);
164  }
165  }
166 
167  if (lenstr > MAXSTRPOS)
168  ereport(ERROR,
169  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
170  errmsg("string is too long for tsvector (%d bytes, max %d bytes)", lenstr, MAXSTRPOS)));
171 
172  totallen = CALCDATASIZE(prs->curwords, lenstr);
173  in = (TSVector) palloc0(totallen);
174  SET_VARSIZE(in, totallen);
175  in->size = prs->curwords;
176 
177  ptr = ARRPTR(in);
178  str = STRPTR(in);
179  stroff = 0;
180  for (i = 0; i < prs->curwords; i++)
181  {
182  ptr->len = prs->words[i].len;
183  ptr->pos = stroff;
184  memcpy(str + stroff, prs->words[i].word, prs->words[i].len);
185  stroff += prs->words[i].len;
186  pfree(prs->words[i].word);
187  if (prs->words[i].alen)
188  {
189  int k = prs->words[i].pos.apos[0];
190  WordEntryPos *wptr;
191 
192  if (k > 0xFFFF)
193  elog(ERROR, "positions array too long");
194 
195  ptr->haspos = 1;
196  stroff = SHORTALIGN(stroff);
197  *(uint16 *) (str + stroff) = (uint16) k;
198  wptr = POSDATAPTR(in, ptr);
199  for (j = 0; j < k; j++)
200  {
201  WEP_SETWEIGHT(wptr[j], 0);
202  WEP_SETPOS(wptr[j], prs->words[i].pos.apos[j + 1]);
203  }
204  stroff += sizeof(uint16) + k * sizeof(WordEntryPos);
205  pfree(prs->words[i].pos.apos);
206  }
207  else
208  ptr->haspos = 0;
209  ptr++;
210  }
211  pfree(prs->words);
212  return in;
213 }
uint16 WordEntryPos
Definition: ts_type.h:63
uint32 len
Definition: ts_type.h:44
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MAXSTRPOS
Definition: ts_type.h:50
#define WEP_SETPOS(x, v)
Definition: ts_type.h:83
int32 curwords
Definition: ts_utils.h:87
#define POSDATAPTR(x, e)
Definition: ts_type.h:111
unsigned short uint16
Definition: c.h:264
void pfree(void *pointer)
Definition: mcxt.c:992
#define ERROR
Definition: elog.h:43
#define WEP_SETWEIGHT(x, v)
Definition: ts_type.h:82
int32 size
Definition: ts_type.h:93
ParsedWord * words
Definition: ts_utils.h:85
uint16 pos
Definition: ts_utils.h:69
uint32 haspos
Definition: ts_type.h:44
#define ereport(elevel, rest)
Definition: elog.h:122
#define CALCDATASIZE(x, lenstr)
Definition: hstore.h:72
void * palloc0(Size size)
Definition: mcxt.c:920
TSVectorData * TSVector
Definition: ts_type.h:98
uint16 len
Definition: ts_utils.h:65
uint32 pos
Definition: ts_type.h:44
uint32 alen
Definition: ts_utils.h:80
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define STRPTR(x)
Definition: hstore.h:76
int i
static int uniqueWORD(ParsedWord *a, int32 l)
Definition: to_tsany.c:59
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:330
#define ARRPTR(x)
Definition: cube.c:26
#define elog
Definition: elog.h:219
#define SHORTALIGN(LEN)
Definition: c.h:579
char * word
Definition: ts_utils.h:79
Datum phraseto_tsquery ( PG_FUNCTION_ARGS  )

Definition at line 442 of file to_tsany.c.

References DirectFunctionCall2, getTSCurrentConfig(), ObjectIdGetDatum, PG_GETARG_TEXT_P, PG_RETURN_DATUM, phraseto_tsquery_byid(), and PointerGetDatum.

443 {
444  text *in = PG_GETARG_TEXT_P(0);
445  Oid cfgId;
446 
447  cfgId = getTSCurrentConfig(true);
449  ObjectIdGetDatum(cfgId),
450  PointerGetDatum(in)));
451 }
Oid getTSCurrentConfig(bool emitError)
Definition: ts_cache.c:556
#define PointerGetDatum(X)
Definition: postgres.h:564
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:297
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:269
Definition: c.h:434
Datum phraseto_tsquery_byid(PG_FUNCTION_ARGS)
Definition: to_tsany.c:424
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:557
Datum phraseto_tsquery_byid ( PG_FUNCTION_ARGS  )

Definition at line 424 of file to_tsany.c.

References MorphOpaque::cfg_id, OP_PHRASE, parse_tsquery(), PG_GETARG_OID, PG_GETARG_TEXT_P, PG_RETURN_TSQUERY, PointerGetDatum, pushval_morph(), MorphOpaque::qoperator, and text_to_cstring().

Referenced by phraseto_tsquery().

425 {
426  text *in = PG_GETARG_TEXT_P(1);
427  TSQuery query;
428  MorphOpaque data;
429 
430  data.cfg_id = PG_GETARG_OID(0);
431  data.qoperator = OP_PHRASE;
432 
433  query = parse_tsquery(text_to_cstring(in),
435  PointerGetDatum(&data),
436  true);
437 
438  PG_RETURN_TSQUERY(query);
439 }
#define PointerGetDatum(X)
Definition: postgres.h:564
#define PG_RETURN_TSQUERY(x)
Definition: ts_type.h:240
int qoperator
Definition: to_tsany.c:24
TSQuery parse_tsquery(char *buf, PushFunction pushval, Datum opaque, bool isplain)
Definition: tsquery.c:605
#define PG_GETARG_OID(n)
Definition: fmgr.h:231
#define OP_PHRASE
Definition: ts_type.h:169
Oid cfg_id
Definition: to_tsany.c:23
char * text_to_cstring(const text *t)
Definition: varlena.c:184
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:269
Definition: c.h:434
static void pushval_morph(Datum opaque, TSQueryParserState state, char *strval, int lenval, int16 weight, bool prefix)
Definition: to_tsany.c:275
Datum plainto_tsquery ( PG_FUNCTION_ARGS  )

Definition at line 411 of file to_tsany.c.

References DirectFunctionCall2, getTSCurrentConfig(), ObjectIdGetDatum, PG_GETARG_TEXT_P, PG_RETURN_DATUM, plainto_tsquery_byid(), and PointerGetDatum.

Referenced by ts_match_tt().

412 {
413  text *in = PG_GETARG_TEXT_P(0);
414  Oid cfgId;
415 
416  cfgId = getTSCurrentConfig(true);
418  ObjectIdGetDatum(cfgId),
419  PointerGetDatum(in)));
420 }
Datum plainto_tsquery_byid(PG_FUNCTION_ARGS)
Definition: to_tsany.c:393
Oid getTSCurrentConfig(bool emitError)
Definition: ts_cache.c:556
#define PointerGetDatum(X)
Definition: postgres.h:564
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:297
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:269
Definition: c.h:434
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:557
Datum plainto_tsquery_byid ( PG_FUNCTION_ARGS  )

Definition at line 393 of file to_tsany.c.

References MorphOpaque::cfg_id, OP_AND, parse_tsquery(), PG_GETARG_OID, PG_GETARG_TEXT_P, PG_RETURN_POINTER, PointerGetDatum, pushval_morph(), MorphOpaque::qoperator, and text_to_cstring().

Referenced by plainto_tsquery().

394 {
395  text *in = PG_GETARG_TEXT_P(1);
396  TSQuery query;
397  MorphOpaque data;
398 
399  data.cfg_id = PG_GETARG_OID(0);
400  data.qoperator = OP_AND;
401 
402  query = parse_tsquery(text_to_cstring(in),
404  PointerGetDatum(&data),
405  true);
406 
407  PG_RETURN_POINTER(query);
408 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
#define PointerGetDatum(X)
Definition: postgres.h:564
#define OP_AND
Definition: ts_type.h:167
int qoperator
Definition: to_tsany.c:24
TSQuery parse_tsquery(char *buf, PushFunction pushval, Datum opaque, bool isplain)
Definition: tsquery.c:605
#define PG_GETARG_OID(n)
Definition: fmgr.h:231
Oid cfg_id
Definition: to_tsany.c:23
char * text_to_cstring(const text *t)
Definition: varlena.c:184
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:269
Definition: c.h:434
static void pushval_morph(Datum opaque, TSQueryParserState state, char *strval, int lenval, int16 weight, bool prefix)
Definition: to_tsany.c:275
static void pushval_morph ( Datum  opaque,
TSQueryParserState  state,
char *  strval,
int  lenval,
int16  weight,
bool  prefix 
)
static

Definition at line 275 of file to_tsany.c.

References MorphOpaque::cfg_id, ParsedText::curwords, DatumGetPointer, ParsedWord::flags, ParsedWord::len, ParsedText::lenwords, ParsedWord::nvariant, OP_AND, OP_OR, palloc(), parsetext(), pfree(), ParsedWord::pos, ParsedText::pos, pushOperator(), pushStop(), pushValue(), MorphOpaque::qoperator, TSL_PREFIX, ParsedWord::word, and ParsedText::words.

Referenced by phraseto_tsquery_byid(), plainto_tsquery_byid(), and to_tsquery_byid().

276 {
277  int32 count = 0;
278  ParsedText prs;
279  uint32 variant,
280  pos = 0,
281  cntvar = 0,
282  cntpos = 0,
283  cnt = 0;
284  MorphOpaque *data = (MorphOpaque *) DatumGetPointer(opaque);
285 
286  prs.lenwords = 4;
287  prs.curwords = 0;
288  prs.pos = 0;
289  prs.words = (ParsedWord *) palloc(sizeof(ParsedWord) * prs.lenwords);
290 
291  parsetext(data->cfg_id, &prs, strval, lenval);
292 
293  if (prs.curwords > 0)
294  {
295  while (count < prs.curwords)
296  {
297  /*
298  * Were any stop words removed? If so, fill empty positions with
299  * placeholders linked by an appropriate operator.
300  */
301  if (pos > 0 && pos + 1 < prs.words[count].pos.pos)
302  {
303  while (pos + 1 < prs.words[count].pos.pos)
304  {
305  /* put placeholders for each missing stop word */
306  pushStop(state);
307  if (cntpos)
308  pushOperator(state, data->qoperator, 1);
309  cntpos++;
310  pos++;
311  }
312  }
313 
314  /* save current word's position */
315  pos = prs.words[count].pos.pos;
316 
317  /* Go through all variants obtained from this token */
318  cntvar = 0;
319  while (count < prs.curwords && pos == prs.words[count].pos.pos)
320  {
321  variant = prs.words[count].nvariant;
322 
323  /* Push all words belonging to the same variant */
324  cnt = 0;
325  while (count < prs.curwords &&
326  pos == prs.words[count].pos.pos &&
327  variant == prs.words[count].nvariant)
328  {
329  pushValue(state,
330  prs.words[count].word,
331  prs.words[count].len,
332  weight,
333  ((prs.words[count].flags & TSL_PREFIX) || prefix));
334  pfree(prs.words[count].word);
335  if (cnt)
336  pushOperator(state, OP_AND, 0);
337  cnt++;
338  count++;
339  }
340 
341  if (cntvar)
342  pushOperator(state, OP_OR, 0);
343  cntvar++;
344  }
345 
346  if (cntpos)
347  {
348  /* distance may be useful */
349  pushOperator(state, data->qoperator, 1);
350  }
351 
352  cntpos++;
353  }
354 
355  pfree(prs.words);
356 
357  }
358  else
359  pushStop(state);
360 }
#define TSL_PREFIX
Definition: ts_public.h:116
uint16 nvariant
Definition: ts_utils.h:66
int32 lenwords
Definition: ts_utils.h:86
#define OP_OR
Definition: ts_type.h:168
int32 curwords
Definition: ts_utils.h:87
signed int int32
Definition: c.h:253
#define OP_AND
Definition: ts_type.h:167
void pfree(void *pointer)
Definition: mcxt.c:992
void parsetext(Oid cfgId, ParsedText *prs, char *buf, int buflen)
Definition: ts_parse.c:358
int qoperator
Definition: to_tsany.c:24
void pushStop(TSQueryParserState state)
Definition: tsquery.c:420
void pushValue(TSQueryParserState state, char *strval, int lenval, int16 weight, bool prefix)
Definition: tsquery.c:384
unsigned int uint32
Definition: c.h:265
ParsedWord * words
Definition: ts_utils.h:85
uint16 pos
Definition: ts_utils.h:69
void pushOperator(TSQueryParserState state, int8 oper, int16 distance)
Definition: tsquery.c:335
uint16 len
Definition: ts_utils.h:65
int32 pos
Definition: ts_utils.h:88
Oid cfg_id
Definition: to_tsany.c:23
#define DatumGetPointer(X)
Definition: postgres.h:557
uint16 flags
Definition: ts_utils.h:78
void * palloc(Size size)
Definition: mcxt.c:891
char * word
Definition: ts_utils.h:79
Datum to_tsquery ( PG_FUNCTION_ARGS  )

Definition at line 381 of file to_tsany.c.

References DirectFunctionCall2, getTSCurrentConfig(), ObjectIdGetDatum, PG_GETARG_TEXT_P, PG_RETURN_DATUM, PointerGetDatum, and to_tsquery_byid().

382 {
383  text *in = PG_GETARG_TEXT_P(0);
384  Oid cfgId;
385 
386  cfgId = getTSCurrentConfig(true);
388  ObjectIdGetDatum(cfgId),
389  PointerGetDatum(in)));
390 }
Oid getTSCurrentConfig(bool emitError)
Definition: ts_cache.c:556
#define PointerGetDatum(X)
Definition: postgres.h:564
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:297
Datum to_tsquery_byid(PG_FUNCTION_ARGS)
Definition: to_tsany.c:363
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:269
Definition: c.h:434
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:557
Datum to_tsquery_byid ( PG_FUNCTION_ARGS  )

Definition at line 363 of file to_tsany.c.

References MorphOpaque::cfg_id, OP_AND, parse_tsquery(), PG_GETARG_OID, PG_GETARG_TEXT_P, PG_RETURN_TSQUERY, PointerGetDatum, pushval_morph(), MorphOpaque::qoperator, and text_to_cstring().

Referenced by to_tsquery().

364 {
365  text *in = PG_GETARG_TEXT_P(1);
366  TSQuery query;
367  MorphOpaque data;
368 
369  data.cfg_id = PG_GETARG_OID(0);
370  data.qoperator = OP_AND;
371 
372  query = parse_tsquery(text_to_cstring(in),
374  PointerGetDatum(&data),
375  false);
376 
377  PG_RETURN_TSQUERY(query);
378 }
#define PointerGetDatum(X)
Definition: postgres.h:564
#define PG_RETURN_TSQUERY(x)
Definition: ts_type.h:240
#define OP_AND
Definition: ts_type.h:167
int qoperator
Definition: to_tsany.c:24
TSQuery parse_tsquery(char *buf, PushFunction pushval, Datum opaque, bool isplain)
Definition: tsquery.c:605
#define PG_GETARG_OID(n)
Definition: fmgr.h:231
Oid cfg_id
Definition: to_tsany.c:23
char * text_to_cstring(const text *t)
Definition: varlena.c:184
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:269
Definition: c.h:434
static void pushval_morph(Datum opaque, TSQueryParserState state, char *strval, int lenval, int16 weight, bool prefix)
Definition: to_tsany.c:275
Datum to_tsvector ( PG_FUNCTION_ARGS  )

Definition at line 248 of file to_tsany.c.

References DirectFunctionCall2, getTSCurrentConfig(), ObjectIdGetDatum, PG_GETARG_TEXT_P, PG_RETURN_DATUM, PointerGetDatum, and to_tsvector_byid().

Referenced by ts_match_tq(), and ts_match_tt().

249 {
250  text *in = PG_GETARG_TEXT_P(0);
251  Oid cfgId;
252 
253  cfgId = getTSCurrentConfig(true);
255  ObjectIdGetDatum(cfgId),
256  PointerGetDatum(in)));
257 }
Oid getTSCurrentConfig(bool emitError)
Definition: ts_cache.c:556
#define PointerGetDatum(X)
Definition: postgres.h:564
unsigned int Oid
Definition: postgres_ext.h:31
Datum to_tsvector_byid(PG_FUNCTION_ARGS)
Definition: to_tsany.c:216
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:297
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:269
Definition: c.h:434
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:557
Datum to_tsvector_byid ( PG_FUNCTION_ARGS  )

Definition at line 216 of file to_tsany.c.

References CALCDATASIZE, ParsedText::curwords, ParsedText::lenwords, make_tsvector(), palloc(), parsetext(), pfree(), PG_FREE_IF_COPY, PG_GETARG_OID, PG_GETARG_TEXT_P, PG_RETURN_POINTER, ParsedText::pos, SET_VARSIZE, TSVectorData::size, VARDATA, VARHDRSZ, VARSIZE, and ParsedText::words.

Referenced by to_tsvector().

217 {
218  Oid cfgId = PG_GETARG_OID(0);
219  text *in = PG_GETARG_TEXT_P(1);
220  ParsedText prs;
221  TSVector out;
222 
223  prs.lenwords = (VARSIZE(in) - VARHDRSZ) / 6; /* just estimation of
224  * word's number */
225  if (prs.lenwords == 0)
226  prs.lenwords = 2;
227  prs.curwords = 0;
228  prs.pos = 0;
229  prs.words = (ParsedWord *) palloc(sizeof(ParsedWord) * prs.lenwords);
230 
231  parsetext(cfgId, &prs, VARDATA(in), VARSIZE(in) - VARHDRSZ);
232  PG_FREE_IF_COPY(in, 1);
233 
234  if (prs.curwords)
235  out = make_tsvector(&prs);
236  else
237  {
238  pfree(prs.words);
239  out = palloc(CALCDATASIZE(0, 0));
240  SET_VARSIZE(out, CALCDATASIZE(0, 0));
241  out->size = 0;
242  }
243 
244  PG_RETURN_POINTER(out);
245 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:305
TSVector make_tsvector(ParsedText *prs)
Definition: to_tsany.c:145
#define VARDATA(PTR)
Definition: postgres.h:305
#define VARSIZE(PTR)
Definition: postgres.h:306
#define VARHDRSZ
Definition: c.h:440
int32 lenwords
Definition: ts_utils.h:86
unsigned int Oid
Definition: postgres_ext.h:31
int32 curwords
Definition: ts_utils.h:87
void pfree(void *pointer)
Definition: mcxt.c:992
void parsetext(Oid cfgId, ParsedText *prs, char *buf, int buflen)
Definition: ts_parse.c:358
#define PG_GETARG_OID(n)
Definition: fmgr.h:231
int32 size
Definition: ts_type.h:93
ParsedWord * words
Definition: ts_utils.h:85
#define CALCDATASIZE(x, lenstr)
Definition: hstore.h:72
int32 pos
Definition: ts_utils.h:88
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:216
void * palloc(Size size)
Definition: mcxt.c:891
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:269
Definition: c.h:434
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:330
static int uniqueWORD ( ParsedWord a,
int32  l 
)
static

Definition at line 59 of file to_tsany.c.

References ParsedWord::alen, compareWORD(), ParsedWord::len, LIMITPOS, MAXENTRYPOS, MAXNUMPOS, palloc(), pfree(), ParsedWord::pos, qsort, repalloc(), and ParsedWord::word.

Referenced by make_tsvector().

60 {
61  ParsedWord *ptr,
62  *res;
63  int tmppos;
64 
65  if (l == 1)
66  {
67  tmppos = LIMITPOS(a->pos.pos);
68  a->alen = 2;
69  a->pos.apos = (uint16 *) palloc(sizeof(uint16) * a->alen);
70  a->pos.apos[0] = 1;
71  a->pos.apos[1] = tmppos;
72  return l;
73  }
74 
75  res = a;
76  ptr = a + 1;
77 
78  /*
79  * Sort words with its positions
80  */
81  qsort((void *) a, l, sizeof(ParsedWord), compareWORD);
82 
83  /*
84  * Initialize first word and its first position
85  */
86  tmppos = LIMITPOS(a->pos.pos);
87  a->alen = 2;
88  a->pos.apos = (uint16 *) palloc(sizeof(uint16) * a->alen);
89  a->pos.apos[0] = 1;
90  a->pos.apos[1] = tmppos;
91 
92  /*
93  * Summarize position information for each word
94  */
95  while (ptr - a < l)
96  {
97  if (!(ptr->len == res->len &&
98  strncmp(ptr->word, res->word, res->len) == 0))
99  {
100  /*
101  * Got a new word, so put it in result
102  */
103  res++;
104  res->len = ptr->len;
105  res->word = ptr->word;
106  tmppos = LIMITPOS(ptr->pos.pos);
107  res->alen = 2;
108  res->pos.apos = (uint16 *) palloc(sizeof(uint16) * res->alen);
109  res->pos.apos[0] = 1;
110  res->pos.apos[1] = tmppos;
111  }
112  else
113  {
114  /*
115  * The word already exists, so adjust position information. But
116  * before we should check size of position's array, max allowed
117  * value for position and uniqueness of position
118  */
119  pfree(ptr->word);
120  if (res->pos.apos[0] < MAXNUMPOS - 1 && res->pos.apos[res->pos.apos[0]] != MAXENTRYPOS - 1 &&
121  res->pos.apos[res->pos.apos[0]] != LIMITPOS(ptr->pos.pos))
122  {
123  if (res->pos.apos[0] + 1 >= res->alen)
124  {
125  res->alen *= 2;
126  res->pos.apos = (uint16 *) repalloc(res->pos.apos, sizeof(uint16) * res->alen);
127  }
128  if (res->pos.apos[0] == 0 || res->pos.apos[res->pos.apos[0]] != LIMITPOS(ptr->pos.pos))
129  {
130  res->pos.apos[res->pos.apos[0] + 1] = LIMITPOS(ptr->pos.pos);
131  res->pos.apos[0]++;
132  }
133  }
134  }
135  ptr++;
136  }
137 
138  return res + 1 - a;
139 }
unsigned short uint16
Definition: c.h:264
void pfree(void *pointer)
Definition: mcxt.c:992
#define MAXNUMPOS
Definition: ts_type.h:86
uint16 pos
Definition: ts_utils.h:69
static int compareWORD(const void *a, const void *b)
Definition: to_tsany.c:38
uint16 len
Definition: ts_utils.h:65
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1021
uint32 alen
Definition: ts_utils.h:80
void * palloc(Size size)
Definition: mcxt.c:891
#define LIMITPOS(x)
Definition: ts_type.h:87
#define qsort(a, b, c, d)
Definition: port.h:440
#define MAXENTRYPOS
Definition: ts_type.h:85
char * word
Definition: ts_utils.h:79