PostgreSQL Source Code  git master
dict_thesaurus.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * dict_thesaurus.c
4  * Thesaurus dictionary: phrase to phrase substitution
5  *
6  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
7  *
8  *
9  * IDENTIFICATION
10  * src/backend/tsearch/dict_thesaurus.c
11  *
12  *-------------------------------------------------------------------------
13  */
14 #include "postgres.h"
15 
16 #include "catalog/namespace.h"
17 #include "commands/defrem.h"
18 #include "tsearch/ts_cache.h"
19 #include "tsearch/ts_locale.h"
20 #include "tsearch/ts_utils.h"
21 #include "utils/builtins.h"
22 #include "utils/regproc.h"
23 
24 
25 /*
26  * Temporary we use TSLexeme.flags for inner use...
27  */
28 #define DT_USEASIS 0x1000
29 
30 typedef struct LexemeInfo
31 {
32  uint32 idsubst; /* entry's number in DictThesaurus->subst */
33  uint16 posinsubst; /* pos info in entry */
34  uint16 tnvariant; /* total num lexemes in one variant */
37 } LexemeInfo;
38 
39 typedef struct
40 {
41  char *lexeme;
43 } TheLexeme;
44 
45 typedef struct
46 {
47  uint16 lastlexeme; /* number lexemes to substitute */
49  TSLexeme *res; /* prepared substituted result */
51 
52 typedef struct
53 {
54  /* subdictionary to normalize lexemes */
57 
58  /* Array to search lexeme by exact match */
60  int nwrds; /* current number of words */
61  int ntwrds; /* allocated array length */
62 
63  /*
64  * Storage of substituted result, n-th element is for n-th expression
65  */
67  int nsubst;
69 
70 
71 static void
73 {
74  TheLexeme *ptr;
75 
76  if (d->nwrds >= d->ntwrds)
77  {
78  if (d->ntwrds == 0)
79  {
80  d->ntwrds = 16;
81  d->wrds = (TheLexeme *) palloc(sizeof(TheLexeme) * d->ntwrds);
82  }
83  else
84  {
85  d->ntwrds *= 2;
86  d->wrds = (TheLexeme *) repalloc(d->wrds, sizeof(TheLexeme) * d->ntwrds);
87  }
88  }
89 
90  ptr = d->wrds + d->nwrds;
91  d->nwrds++;
92 
93  ptr->lexeme = palloc(e - b + 1);
94 
95  memcpy(ptr->lexeme, b, e - b);
96  ptr->lexeme[e - b] = '\0';
97 
98  ptr->entries = (LexemeInfo *) palloc(sizeof(LexemeInfo));
99 
100  ptr->entries->nextentry = NULL;
101  ptr->entries->idsubst = idsubst;
102  ptr->entries->posinsubst = posinsubst;
103 }
104 
105 static void
106 addWrd(DictThesaurus *d, char *b, char *e, uint32 idsubst, uint16 nwrd, uint16 posinsubst, bool useasis)
107 {
108  static int nres = 0;
109  static int ntres = 0;
110  TheSubstitute *ptr;
111 
112  if (nwrd == 0)
113  {
114  nres = ntres = 0;
115 
116  if (idsubst >= d->nsubst)
117  {
118  if (d->nsubst == 0)
119  {
120  d->nsubst = 16;
121  d->subst = (TheSubstitute *) palloc(sizeof(TheSubstitute) * d->nsubst);
122  }
123  else
124  {
125  d->nsubst *= 2;
126  d->subst = (TheSubstitute *) repalloc(d->subst, sizeof(TheSubstitute) * d->nsubst);
127  }
128  }
129  }
130 
131  ptr = d->subst + idsubst;
132 
133  ptr->lastlexeme = posinsubst - 1;
134 
135  if (nres + 1 >= ntres)
136  {
137  if (ntres == 0)
138  {
139  ntres = 2;
140  ptr->res = (TSLexeme *) palloc(sizeof(TSLexeme) * ntres);
141  }
142  else
143  {
144  ntres *= 2;
145  ptr->res = (TSLexeme *) repalloc(ptr->res, sizeof(TSLexeme) * ntres);
146  }
147  }
148 
149  ptr->res[nres].lexeme = palloc(e - b + 1);
150  memcpy(ptr->res[nres].lexeme, b, e - b);
151  ptr->res[nres].lexeme[e - b] = '\0';
152 
153  ptr->res[nres].nvariant = nwrd;
154  if (useasis)
155  ptr->res[nres].flags = DT_USEASIS;
156  else
157  ptr->res[nres].flags = 0;
158 
159  ptr->res[++nres].lexeme = NULL;
160 }
161 
162 #define TR_WAITLEX 1
163 #define TR_INLEX 2
164 #define TR_WAITSUBS 3
165 #define TR_INSUBS 4
166 
167 static void
169 {
171  uint32 idsubst = 0;
172  bool useasis = false;
173  char *line;
174 
175  filename = get_tsearch_config_filename(filename, "ths");
176  if (!tsearch_readline_begin(&trst, filename))
177  ereport(ERROR,
178  (errcode(ERRCODE_CONFIG_FILE_ERROR),
179  errmsg("could not open thesaurus file \"%s\": %m",
180  filename)));
181 
182  while ((line = tsearch_readline(&trst)) != NULL)
183  {
184  char *ptr;
185  int state = TR_WAITLEX;
186  char *beginwrd = NULL;
187  uint32 posinsubst = 0;
188  uint32 nwrd = 0;
189 
190  ptr = line;
191 
192  /* is it a comment? */
193  while (*ptr && t_isspace(ptr))
194  ptr += pg_mblen(ptr);
195 
196  if (t_iseq(ptr, '#') || *ptr == '\0' ||
197  t_iseq(ptr, '\n') || t_iseq(ptr, '\r'))
198  {
199  pfree(line);
200  continue;
201  }
202 
203  while (*ptr)
204  {
205  if (state == TR_WAITLEX)
206  {
207  if (t_iseq(ptr, ':'))
208  {
209  if (posinsubst == 0)
210  ereport(ERROR,
211  (errcode(ERRCODE_CONFIG_FILE_ERROR),
212  errmsg("unexpected delimiter")));
213  state = TR_WAITSUBS;
214  }
215  else if (!t_isspace(ptr))
216  {
217  beginwrd = ptr;
218  state = TR_INLEX;
219  }
220  }
221  else if (state == TR_INLEX)
222  {
223  if (t_iseq(ptr, ':'))
224  {
225  newLexeme(d, beginwrd, ptr, idsubst, posinsubst++);
226  state = TR_WAITSUBS;
227  }
228  else if (t_isspace(ptr))
229  {
230  newLexeme(d, beginwrd, ptr, idsubst, posinsubst++);
231  state = TR_WAITLEX;
232  }
233  }
234  else if (state == TR_WAITSUBS)
235  {
236  if (t_iseq(ptr, '*'))
237  {
238  useasis = true;
239  state = TR_INSUBS;
240  beginwrd = ptr + pg_mblen(ptr);
241  }
242  else if (t_iseq(ptr, '\\'))
243  {
244  useasis = false;
245  state = TR_INSUBS;
246  beginwrd = ptr + pg_mblen(ptr);
247  }
248  else if (!t_isspace(ptr))
249  {
250  useasis = false;
251  beginwrd = ptr;
252  state = TR_INSUBS;
253  }
254  }
255  else if (state == TR_INSUBS)
256  {
257  if (t_isspace(ptr))
258  {
259  if (ptr == beginwrd)
260  ereport(ERROR,
261  (errcode(ERRCODE_CONFIG_FILE_ERROR),
262  errmsg("unexpected end of line or lexeme")));
263  addWrd(d, beginwrd, ptr, idsubst, nwrd++, posinsubst, useasis);
264  state = TR_WAITSUBS;
265  }
266  }
267  else
268  elog(ERROR, "unrecognized thesaurus state: %d", state);
269 
270  ptr += pg_mblen(ptr);
271  }
272 
273  if (state == TR_INSUBS)
274  {
275  if (ptr == beginwrd)
276  ereport(ERROR,
277  (errcode(ERRCODE_CONFIG_FILE_ERROR),
278  errmsg("unexpected end of line or lexeme")));
279  addWrd(d, beginwrd, ptr, idsubst, nwrd++, posinsubst, useasis);
280  }
281 
282  idsubst++;
283 
284  if (!(nwrd && posinsubst))
285  ereport(ERROR,
286  (errcode(ERRCODE_CONFIG_FILE_ERROR),
287  errmsg("unexpected end of line")));
288 
289  /*
290  * Note: currently, tsearch_readline can't return lines exceeding 4KB,
291  * so overflow of the word counts is impossible. But that may not
292  * always be true, so let's check.
293  */
294  if (nwrd != (uint16) nwrd || posinsubst != (uint16) posinsubst)
295  ereport(ERROR,
296  (errcode(ERRCODE_CONFIG_FILE_ERROR),
297  errmsg("too many lexemes in thesaurus entry")));
298 
299  pfree(line);
300  }
301 
302  d->nsubst = idsubst;
303 
304  tsearch_readline_end(&trst);
305 }
306 
307 static TheLexeme *
308 addCompiledLexeme(TheLexeme *newwrds, int *nnw, int *tnm, TSLexeme *lexeme, LexemeInfo *src, uint16 tnvariant)
309 {
310  if (*nnw >= *tnm)
311  {
312  *tnm *= 2;
313  newwrds = (TheLexeme *) repalloc(newwrds, sizeof(TheLexeme) * *tnm);
314  }
315 
316  newwrds[*nnw].entries = (LexemeInfo *) palloc(sizeof(LexemeInfo));
317 
318  if (lexeme && lexeme->lexeme)
319  {
320  newwrds[*nnw].lexeme = pstrdup(lexeme->lexeme);
321  newwrds[*nnw].entries->tnvariant = tnvariant;
322  }
323  else
324  {
325  newwrds[*nnw].lexeme = NULL;
326  newwrds[*nnw].entries->tnvariant = 1;
327  }
328 
329  newwrds[*nnw].entries->idsubst = src->idsubst;
330  newwrds[*nnw].entries->posinsubst = src->posinsubst;
331 
332  newwrds[*nnw].entries->nextentry = NULL;
333 
334  (*nnw)++;
335  return newwrds;
336 }
337 
338 static int
340 {
341  if (a == NULL || b == NULL)
342  return 0;
343 
344  if (a->idsubst == b->idsubst)
345  {
346  if (a->posinsubst == b->posinsubst)
347  {
348  if (a->tnvariant == b->tnvariant)
349  return 0;
350 
351  return (a->tnvariant > b->tnvariant) ? 1 : -1;
352  }
353 
354  return (a->posinsubst > b->posinsubst) ? 1 : -1;
355  }
356 
357  return (a->idsubst > b->idsubst) ? 1 : -1;
358 }
359 
360 static int
361 cmpLexeme(const TheLexeme *a, const TheLexeme *b)
362 {
363  if (a->lexeme == NULL)
364  {
365  if (b->lexeme == NULL)
366  return 0;
367  else
368  return 1;
369  }
370  else if (b->lexeme == NULL)
371  return -1;
372 
373  return strcmp(a->lexeme, b->lexeme);
374 }
375 
376 static int
377 cmpLexemeQ(const void *a, const void *b)
378 {
379  return cmpLexeme((const TheLexeme *) a, (const TheLexeme *) b);
380 }
381 
382 static int
383 cmpTheLexeme(const void *a, const void *b)
384 {
385  const TheLexeme *la = (const TheLexeme *) a;
386  const TheLexeme *lb = (const TheLexeme *) b;
387  int res;
388 
389  if ((res = cmpLexeme(la, lb)) != 0)
390  return res;
391 
392  return -cmpLexemeInfo(la->entries, lb->entries);
393 }
394 
395 static void
397 {
398  int i,
399  nnw = 0,
400  tnm = 16;
401  TheLexeme *newwrds = (TheLexeme *) palloc(sizeof(TheLexeme) * tnm),
402  *ptrwrds;
403 
404  for (i = 0; i < d->nwrds; i++)
405  {
406  TSLexeme *ptr;
407 
408  if (strcmp(d->wrds[i].lexeme, "?") == 0) /* Is stop word marker? */
409  newwrds = addCompiledLexeme(newwrds, &nnw, &tnm, NULL, d->wrds[i].entries, 0);
410  else
411  {
414  PointerGetDatum(d->wrds[i].lexeme),
415  Int32GetDatum(strlen(d->wrds[i].lexeme)),
416  PointerGetDatum(NULL)));
417 
418  if (!ptr)
419  ereport(ERROR,
420  (errcode(ERRCODE_CONFIG_FILE_ERROR),
421  errmsg("thesaurus sample word \"%s\" isn't recognized by subdictionary (rule %d)",
422  d->wrds[i].lexeme,
423  d->wrds[i].entries->idsubst + 1)));
424  else if (!(ptr->lexeme))
425  ereport(ERROR,
426  (errcode(ERRCODE_CONFIG_FILE_ERROR),
427  errmsg("thesaurus sample word \"%s\" is a stop word (rule %d)",
428  d->wrds[i].lexeme,
429  d->wrds[i].entries->idsubst + 1),
430  errhint("Use \"?\" to represent a stop word within a sample phrase.")));
431  else
432  {
433  while (ptr->lexeme)
434  {
435  TSLexeme *remptr = ptr + 1;
436  int tnvar = 1;
437  int curvar = ptr->nvariant;
438 
439  /* compute n words in one variant */
440  while (remptr->lexeme)
441  {
442  if (remptr->nvariant != (remptr - 1)->nvariant)
443  break;
444  tnvar++;
445  remptr++;
446  }
447 
448  remptr = ptr;
449  while (remptr->lexeme && remptr->nvariant == curvar)
450  {
451  newwrds = addCompiledLexeme(newwrds, &nnw, &tnm, remptr, d->wrds[i].entries, tnvar);
452  remptr++;
453  }
454 
455  ptr = remptr;
456  }
457  }
458  }
459 
460  pfree(d->wrds[i].lexeme);
461  pfree(d->wrds[i].entries);
462  }
463 
464  if (d->wrds)
465  pfree(d->wrds);
466  d->wrds = newwrds;
467  d->nwrds = nnw;
468  d->ntwrds = tnm;
469 
470  if (d->nwrds > 1)
471  {
472  qsort(d->wrds, d->nwrds, sizeof(TheLexeme), cmpTheLexeme);
473 
474  /* uniq */
475  newwrds = d->wrds;
476  ptrwrds = d->wrds + 1;
477  while (ptrwrds - d->wrds < d->nwrds)
478  {
479  if (cmpLexeme(ptrwrds, newwrds) == 0)
480  {
481  if (cmpLexemeInfo(ptrwrds->entries, newwrds->entries))
482  {
483  ptrwrds->entries->nextentry = newwrds->entries;
484  newwrds->entries = ptrwrds->entries;
485  }
486  else
487  pfree(ptrwrds->entries);
488 
489  if (ptrwrds->lexeme)
490  pfree(ptrwrds->lexeme);
491  }
492  else
493  {
494  newwrds++;
495  *newwrds = *ptrwrds;
496  }
497 
498  ptrwrds++;
499  }
500 
501  d->nwrds = newwrds - d->wrds + 1;
502  d->wrds = (TheLexeme *) repalloc(d->wrds, sizeof(TheLexeme) * d->nwrds);
503  }
504 }
505 
506 static void
508 {
509  int i;
510 
511  for (i = 0; i < d->nsubst; i++)
512  {
513  TSLexeme *rem = d->subst[i].res,
514  *outptr,
515  *inptr;
516  int n = 2;
517 
518  outptr = d->subst[i].res = (TSLexeme *) palloc(sizeof(TSLexeme) * n);
519  outptr->lexeme = NULL;
520  inptr = rem;
521 
522  while (inptr && inptr->lexeme)
523  {
524  TSLexeme *lexized,
525  tmplex[2];
526 
527  if (inptr->flags & DT_USEASIS)
528  { /* do not lexize */
529  tmplex[0] = *inptr;
530  tmplex[0].flags = 0;
531  tmplex[1].lexeme = NULL;
532  lexized = tmplex;
533  }
534  else
535  {
536  lexized = (TSLexeme *) DatumGetPointer(FunctionCall4(&(d->subdict->lexize),
538  PointerGetDatum(inptr->lexeme),
539  Int32GetDatum(strlen(inptr->lexeme)),
540  PointerGetDatum(NULL)));
541  }
542 
543  if (lexized && lexized->lexeme)
544  {
545  int toset = (lexized->lexeme && outptr != d->subst[i].res) ? (outptr - d->subst[i].res) : -1;
546 
547  while (lexized->lexeme)
548  {
549  if (outptr - d->subst[i].res + 1 >= n)
550  {
551  int diff = outptr - d->subst[i].res;
552 
553  n *= 2;
554  d->subst[i].res = (TSLexeme *) repalloc(d->subst[i].res, sizeof(TSLexeme) * n);
555  outptr = d->subst[i].res + diff;
556  }
557 
558  *outptr = *lexized;
559  outptr->lexeme = pstrdup(lexized->lexeme);
560 
561  outptr++;
562  lexized++;
563  }
564 
565  if (toset > 0)
566  d->subst[i].res[toset].flags |= TSL_ADDPOS;
567  }
568  else if (lexized)
569  {
570  ereport(ERROR,
571  (errcode(ERRCODE_CONFIG_FILE_ERROR),
572  errmsg("thesaurus substitute word \"%s\" is a stop word (rule %d)",
573  inptr->lexeme, i + 1)));
574  }
575  else
576  {
577  ereport(ERROR,
578  (errcode(ERRCODE_CONFIG_FILE_ERROR),
579  errmsg("thesaurus substitute word \"%s\" isn't recognized by subdictionary (rule %d)",
580  inptr->lexeme, i + 1)));
581  }
582 
583  if (inptr->lexeme)
584  pfree(inptr->lexeme);
585  inptr++;
586  }
587 
588  if (outptr == d->subst[i].res)
589  ereport(ERROR,
590  (errcode(ERRCODE_CONFIG_FILE_ERROR),
591  errmsg("thesaurus substitute phrase is empty (rule %d)",
592  i + 1)));
593 
594  d->subst[i].reslen = outptr - d->subst[i].res;
595 
596  pfree(rem);
597  }
598 }
599 
600 Datum
602 {
603  List *dictoptions = (List *) PG_GETARG_POINTER(0);
604  DictThesaurus *d;
605  char *subdictname = NULL;
606  bool fileloaded = false;
607  ListCell *l;
608 
609  d = (DictThesaurus *) palloc0(sizeof(DictThesaurus));
610 
611  foreach(l, dictoptions)
612  {
613  DefElem *defel = (DefElem *) lfirst(l);
614 
615  if (strcmp(defel->defname, "dictfile") == 0)
616  {
617  if (fileloaded)
618  ereport(ERROR,
619  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
620  errmsg("multiple DictFile parameters")));
621  thesaurusRead(defGetString(defel), d);
622  fileloaded = true;
623  }
624  else if (strcmp(defel->defname, "dictionary") == 0)
625  {
626  if (subdictname)
627  ereport(ERROR,
628  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
629  errmsg("multiple Dictionary parameters")));
630  subdictname = pstrdup(defGetString(defel));
631  }
632  else
633  {
634  ereport(ERROR,
635  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
636  errmsg("unrecognized Thesaurus parameter: \"%s\"",
637  defel->defname)));
638  }
639  }
640 
641  if (!fileloaded)
642  ereport(ERROR,
643  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
644  errmsg("missing DictFile parameter")));
645  if (!subdictname)
646  ereport(ERROR,
647  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
648  errmsg("missing Dictionary parameter")));
649 
650  d->subdictOid = get_ts_dict_oid(stringToQualifiedNameList(subdictname), false);
652 
653  compileTheLexeme(d);
655 
657 }
658 
659 static LexemeInfo *
660 findTheLexeme(DictThesaurus *d, char *lexeme)
661 {
662  TheLexeme key,
663  *res;
664 
665  if (d->nwrds == 0)
666  return NULL;
667 
668  key.lexeme = lexeme;
669  key.entries = NULL;
670 
671  res = bsearch(&key, d->wrds, d->nwrds, sizeof(TheLexeme), cmpLexemeQ);
672 
673  if (res == NULL)
674  return NULL;
675  return res->entries;
676 }
677 
678 static bool
680 {
681  bool res = true;
682 
683  if (stored)
684  {
685  res = false;
686 
687  for (; stored; stored = stored->nextvariant)
688  if (stored->idsubst == idsubst)
689  {
690  res = true;
691  break;
692  }
693  }
694 
695  return res;
696 }
697 
698 static LexemeInfo *
699 findVariant(LexemeInfo *in, LexemeInfo *stored, uint16 curpos, LexemeInfo **newin, int newn)
700 {
701  for (;;)
702  {
703  int i;
704  LexemeInfo *ptr = newin[0];
705 
706  for (i = 0; i < newn; i++)
707  {
708  while (newin[i] && newin[i]->idsubst < ptr->idsubst)
709  newin[i] = newin[i]->nextentry;
710 
711  if (newin[i] == NULL)
712  return in;
713 
714  if (newin[i]->idsubst > ptr->idsubst)
715  {
716  ptr = newin[i];
717  i = -1;
718  continue;
719  }
720 
721  while (newin[i]->idsubst == ptr->idsubst)
722  {
723  if (newin[i]->posinsubst == curpos && newin[i]->tnvariant == newn)
724  {
725  ptr = newin[i];
726  break;
727  }
728 
729  newin[i] = newin[i]->nextentry;
730  if (newin[i] == NULL)
731  return in;
732  }
733 
734  if (newin[i]->idsubst != ptr->idsubst)
735  {
736  ptr = newin[i];
737  i = -1;
738  continue;
739  }
740  }
741 
742  if (i == newn && matchIdSubst(stored, ptr->idsubst) && (in == NULL || !matchIdSubst(in, ptr->idsubst)))
743  { /* found */
744 
745  ptr->nextvariant = in;
746  in = ptr;
747  }
748 
749  /* step forward */
750  for (i = 0; i < newn; i++)
751  newin[i] = newin[i]->nextentry;
752  }
753 }
754 
755 static TSLexeme *
757 {
758  TSLexeme *res;
759  uint16 i;
760 
761  res = (TSLexeme *) palloc(sizeof(TSLexeme) * (ts->reslen + 1));
762  for (i = 0; i < ts->reslen; i++)
763  {
764  res[i] = ts->res[i];
765  res[i].lexeme = pstrdup(ts->res[i].lexeme);
766  }
767 
768  res[ts->reslen].lexeme = NULL;
769 
770  return res;
771 }
772 
773 static TSLexeme *
774 checkMatch(DictThesaurus *d, LexemeInfo *info, uint16 curpos, bool *moreres)
775 {
776  *moreres = false;
777  while (info)
778  {
779  Assert(info->idsubst < d->nsubst);
780  if (info->nextvariant)
781  *moreres = true;
782  if (d->subst[info->idsubst].lastlexeme == curpos)
783  return copyTSLexeme(d->subst + info->idsubst);
784  info = info->nextvariant;
785  }
786 
787  return NULL;
788 }
789 
790 Datum
792 {
795  TSLexeme *res = NULL;
796  LexemeInfo *stored,
797  *info = NULL;
798  uint16 curpos = 0;
799  bool moreres = false;
800 
801  if (PG_NARGS() != 4 || dstate == NULL)
802  elog(ERROR, "forbidden call of thesaurus or nested call");
803 
804  if (dstate->isend)
805  PG_RETURN_POINTER(NULL);
806  stored = (LexemeInfo *) dstate->private_state;
807 
808  if (stored)
809  curpos = stored->posinsubst + 1;
810 
811  if (!d->subdict->isvalid)
813 
816  PG_GETARG_DATUM(1),
817  PG_GETARG_DATUM(2),
818  PointerGetDatum(NULL)));
819 
820  if (res && res->lexeme)
821  {
822  TSLexeme *ptr = res,
823  *basevar;
824 
825  while (ptr->lexeme)
826  {
827  uint16 nv = ptr->nvariant;
828  uint16 i,
829  nlex = 0;
830  LexemeInfo **infos;
831 
832  basevar = ptr;
833  while (ptr->lexeme && nv == ptr->nvariant)
834  {
835  nlex++;
836  ptr++;
837  }
838 
839  infos = (LexemeInfo **) palloc(sizeof(LexemeInfo *) * nlex);
840  for (i = 0; i < nlex; i++)
841  if ((infos[i] = findTheLexeme(d, basevar[i].lexeme)) == NULL)
842  break;
843 
844  if (i < nlex)
845  {
846  /* no chance to find */
847  pfree(infos);
848  continue;
849  }
850 
851  info = findVariant(info, stored, curpos, infos, nlex);
852  }
853  }
854  else if (res)
855  { /* stop-word */
856  LexemeInfo *infos = findTheLexeme(d, NULL);
857 
858  info = findVariant(NULL, stored, curpos, &infos, 1);
859  }
860  else
861  {
862  info = NULL; /* word isn't recognized */
863  }
864 
865  dstate->private_state = (void *) info;
866 
867  if (!info)
868  {
869  dstate->getnext = false;
870  PG_RETURN_POINTER(NULL);
871  }
872 
873  if ((res = checkMatch(d, info, curpos, &moreres)) != NULL)
874  {
875  dstate->getnext = moreres;
876  PG_RETURN_POINTER(res);
877  }
878 
879  dstate->getnext = true;
880 
881  PG_RETURN_POINTER(NULL);
882 }
static LexemeInfo * findTheLexeme(DictThesaurus *d, char *lexeme)
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:360
struct LexemeInfo LexemeInfo
int errhint(const char *fmt,...)
Definition: elog.c:1071
uint32 idsubst
#define TSL_ADDPOS
Definition: ts_public.h:115
TheSubstitute * subst
#define PointerGetDatum(X)
Definition: postgres.h:556
struct LexemeInfo * nextentry
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
char * pstrdup(const char *in)
Definition: mcxt.c:1187
static TheLexeme * addCompiledLexeme(TheLexeme *newwrds, int *nnw, int *tnm, TSLexeme *lexeme, LexemeInfo *src, uint16 tnvariant)
static TSLexeme * copyTSLexeme(TheSubstitute *ts)
bool getnext
Definition: ts_public.h:127
static void thesaurusRead(const char *filename, DictThesaurus *d)
int errcode(int sqlerrcode)
Definition: elog.c:610
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
unsigned int Oid
Definition: postgres_ext.h:31
LexemeInfo * entries
char * lexeme
static TSLexeme * checkMatch(DictThesaurus *d, LexemeInfo *info, uint16 curpos, bool *moreres)
static void compileTheLexeme(DictThesaurus *d)
TSDictionaryCacheEntry * lookup_ts_dictionary_cache(Oid dictId)
Definition: ts_cache.c:210
Oid get_ts_dict_oid(List *names, bool missing_ok)
Definition: namespace.c:2425
unsigned short uint16
Definition: c.h:373
void pfree(void *pointer)
Definition: mcxt.c:1057
static void addWrd(DictThesaurus *d, char *b, char *e, uint32 idsubst, uint16 nwrd, uint16 posinsubst, bool useasis)
#define ERROR
Definition: elog.h:43
static bool matchIdSubst(LexemeInfo *stored, uint32 idsubst)
char * get_tsearch_config_filename(const char *basename, const char *extension)
Definition: ts_utils.c:33
char * defGetString(DefElem *def)
Definition: define.c:49
char * lexeme
Definition: ts_public.h:111
struct LexemeInfo * nextvariant
int t_isspace(const char *ptr)
Definition: ts_locale.c:52
uint16 posinsubst
#define t_iseq(x, c)
Definition: ts_locale.h:43
unsigned int uint32
Definition: c.h:374
#define FunctionCall4(flinfo, arg1, arg2, arg3, arg4)
Definition: fmgr.h:648
uint16 nvariant
Definition: ts_public.h:107
uint16 flags
Definition: ts_public.h:109
uint16 tnvariant
static int cmpLexemeQ(const void *a, const void *b)
static int cmpLexemeInfo(LexemeInfo *a, LexemeInfo *b)
void * palloc0(Size size)
Definition: mcxt.c:981
uintptr_t Datum
Definition: postgres.h:367
TSDictionaryCacheEntry * subdict
Datum thesaurus_lexize(PG_FUNCTION_ARGS)
TheLexeme * wrds
#define ereport(elevel,...)
Definition: elog.h:144
#define TR_INLEX
static int cmpLexeme(const TheLexeme *a, const TheLexeme *b)
#define Assert(condition)
Definition: c.h:745
#define lfirst(lc)
Definition: pg_list.h:190
Definition: regguts.h:298
#define DT_USEASIS
#define PG_NARGS()
Definition: fmgr.h:203
int pg_mblen(const char *mbstr)
Definition: mbutils.c:907
void tsearch_readline_end(tsearch_readline_state *stp)
Definition: ts_locale.c:179
char * tsearch_readline(tsearch_readline_state *stp)
Definition: ts_locale.c:146
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1070
#define DatumGetPointer(X)
Definition: postgres.h:549
bool tsearch_readline_begin(tsearch_readline_state *stp, const char *filename)
Definition: ts_locale.c:124
static int cmpTheLexeme(const void *a, const void *b)
static void compileTheSubstitute(DictThesaurus *d)
#define Int32GetDatum(X)
Definition: postgres.h:479
List * stringToQualifiedNameList(const char *string)
Definition: regproc.c:1876
void * private_state
Definition: ts_public.h:128
static char * filename
Definition: pg_dumpall.c:91
e
Definition: preproc-init.c:82
#define TR_WAITLEX
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define TR_WAITSUBS
TSLexeme * res
#define elog(elevel,...)
Definition: elog.h:214
int i
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
char * defname
Definition: parsenodes.h:733
#define TR_INSUBS
static LexemeInfo * findVariant(LexemeInfo *in, LexemeInfo *stored, uint16 curpos, LexemeInfo **newin, int newn)
#define qsort(a, b, c, d)
Definition: port.h:475
Datum thesaurus_init(PG_FUNCTION_ARGS)
Definition: pg_list.h:50
static void newLexeme(DictThesaurus *d, char *b, char *e, uint32 idsubst, uint16 posinsubst)