PostgreSQL Source Code  git master
dict_thesaurus.c File Reference
#include "postgres.h"
#include "catalog/namespace.h"
#include "commands/defrem.h"
#include "tsearch/ts_cache.h"
#include "tsearch/ts_locale.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
#include "utils/regproc.h"
Include dependency graph for dict_thesaurus.c:

Go to the source code of this file.

Data Structures

struct  LexemeInfo
 
struct  TheLexeme
 
struct  TheSubstitute
 
struct  DictThesaurus
 

Macros

#define DT_USEASIS   0x1000
 
#define TR_WAITLEX   1
 
#define TR_INLEX   2
 
#define TR_WAITSUBS   3
 
#define TR_INSUBS   4
 

Typedefs

typedef struct LexemeInfo LexemeInfo
 

Functions

static void newLexeme (DictThesaurus *d, char *b, char *e, uint32 idsubst, uint16 posinsubst)
 
static void addWrd (DictThesaurus *d, char *b, char *e, uint32 idsubst, uint16 nwrd, uint16 posinsubst, bool useasis)
 
static void thesaurusRead (const char *filename, DictThesaurus *d)
 
static TheLexemeaddCompiledLexeme (TheLexeme *newwrds, int *nnw, int *tnm, TSLexeme *lexeme, LexemeInfo *src, uint16 tnvariant)
 
static int cmpLexemeInfo (LexemeInfo *a, LexemeInfo *b)
 
static int cmpLexeme (const TheLexeme *a, const TheLexeme *b)
 
static int cmpLexemeQ (const void *a, const void *b)
 
static int cmpTheLexeme (const void *a, const void *b)
 
static void compileTheLexeme (DictThesaurus *d)
 
static void compileTheSubstitute (DictThesaurus *d)
 
Datum thesaurus_init (PG_FUNCTION_ARGS)
 
static LexemeInfofindTheLexeme (DictThesaurus *d, char *lexeme)
 
static bool matchIdSubst (LexemeInfo *stored, uint32 idsubst)
 
static LexemeInfofindVariant (LexemeInfo *in, LexemeInfo *stored, uint16 curpos, LexemeInfo **newin, int newn)
 
static TSLexemecopyTSLexeme (TheSubstitute *ts)
 
static TSLexemecheckMatch (DictThesaurus *d, LexemeInfo *info, uint16 curpos, bool *moreres)
 
Datum thesaurus_lexize (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ DT_USEASIS

#define DT_USEASIS   0x1000

Definition at line 28 of file dict_thesaurus.c.

Referenced by addWrd(), and compileTheSubstitute().

◆ TR_INLEX

#define TR_INLEX   2

Definition at line 163 of file dict_thesaurus.c.

Referenced by thesaurusRead().

◆ TR_INSUBS

#define TR_INSUBS   4

Definition at line 165 of file dict_thesaurus.c.

Referenced by thesaurusRead().

◆ TR_WAITLEX

#define TR_WAITLEX   1

Definition at line 162 of file dict_thesaurus.c.

Referenced by thesaurusRead().

◆ TR_WAITSUBS

#define TR_WAITSUBS   3

Definition at line 164 of file dict_thesaurus.c.

Referenced by thesaurusRead().

Typedef Documentation

◆ LexemeInfo

typedef struct LexemeInfo LexemeInfo

Function Documentation

◆ addCompiledLexeme()

static TheLexeme* addCompiledLexeme ( TheLexeme newwrds,
int *  nnw,
int *  tnm,
TSLexeme lexeme,
LexemeInfo src,
uint16  tnvariant 
)
static

Definition at line 303 of file dict_thesaurus.c.

References TheLexeme::entries, LexemeInfo::idsubst, TheLexeme::lexeme, TSLexeme::lexeme, LexemeInfo::nextentry, palloc(), LexemeInfo::posinsubst, pstrdup(), repalloc(), and LexemeInfo::tnvariant.

Referenced by compileTheLexeme().

304 {
305  if (*nnw >= *tnm)
306  {
307  *tnm *= 2;
308  newwrds = (TheLexeme *) repalloc(newwrds, sizeof(TheLexeme) * *tnm);
309  }
310 
311  newwrds[*nnw].entries = (LexemeInfo *) palloc(sizeof(LexemeInfo));
312 
313  if (lexeme && lexeme->lexeme)
314  {
315  newwrds[*nnw].lexeme = pstrdup(lexeme->lexeme);
316  newwrds[*nnw].entries->tnvariant = tnvariant;
317  }
318  else
319  {
320  newwrds[*nnw].lexeme = NULL;
321  newwrds[*nnw].entries->tnvariant = 1;
322  }
323 
324  newwrds[*nnw].entries->idsubst = src->idsubst;
325  newwrds[*nnw].entries->posinsubst = src->posinsubst;
326 
327  newwrds[*nnw].entries->nextentry = NULL;
328 
329  (*nnw)++;
330  return newwrds;
331 }
uint32 idsubst
struct LexemeInfo * nextentry
char * pstrdup(const char *in)
Definition: mcxt.c:1187
LexemeInfo * entries
char * lexeme
char * lexeme
Definition: ts_public.h:111
uint16 posinsubst
uint16 tnvariant
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1070
void * palloc(Size size)
Definition: mcxt.c:950

◆ addWrd()

static void addWrd ( DictThesaurus d,
char *  b,
char *  e,
uint32  idsubst,
uint16  nwrd,
uint16  posinsubst,
bool  useasis 
)
static

Definition at line 106 of file dict_thesaurus.c.

References DT_USEASIS, TSLexeme::flags, LexemeInfo::idsubst, TheSubstitute::lastlexeme, TSLexeme::lexeme, DictThesaurus::nsubst, TSLexeme::nvariant, palloc(), repalloc(), TheSubstitute::res, and DictThesaurus::subst.

Referenced by thesaurusRead().

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 }
TheSubstitute * subst
char * lexeme
Definition: ts_public.h:111
uint16 nvariant
Definition: ts_public.h:107
uint16 flags
Definition: ts_public.h:109
#define DT_USEASIS
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1070
e
Definition: preproc-init.c:82
void * palloc(Size size)
Definition: mcxt.c:950
TSLexeme * res

◆ checkMatch()

static TSLexeme* checkMatch ( DictThesaurus d,
LexemeInfo info,
uint16  curpos,
bool moreres 
)
static

Definition at line 769 of file dict_thesaurus.c.

References Assert, copyTSLexeme(), LexemeInfo::idsubst, TheSubstitute::lastlexeme, LexemeInfo::nextvariant, DictThesaurus::nsubst, and DictThesaurus::subst.

Referenced by thesaurus_lexize().

770 {
771  *moreres = false;
772  while (info)
773  {
774  Assert(info->idsubst < d->nsubst);
775  if (info->nextvariant)
776  *moreres = true;
777  if (d->subst[info->idsubst].lastlexeme == curpos)
778  return copyTSLexeme(d->subst + info->idsubst);
779  info = info->nextvariant;
780  }
781 
782  return NULL;
783 }
uint32 idsubst
TheSubstitute * subst
static TSLexeme * copyTSLexeme(TheSubstitute *ts)
struct LexemeInfo * nextvariant
#define Assert(condition)
Definition: c.h:800

◆ cmpLexeme()

static int cmpLexeme ( const TheLexeme a,
const TheLexeme b 
)
static

Definition at line 356 of file dict_thesaurus.c.

References TheLexeme::lexeme.

Referenced by cmpLexemeQ(), cmpTheLexeme(), and compileTheLexeme().

357 {
358  if (a->lexeme == NULL)
359  {
360  if (b->lexeme == NULL)
361  return 0;
362  else
363  return 1;
364  }
365  else if (b->lexeme == NULL)
366  return -1;
367 
368  return strcmp(a->lexeme, b->lexeme);
369 }
char * lexeme

◆ cmpLexemeInfo()

static int cmpLexemeInfo ( LexemeInfo a,
LexemeInfo b 
)
static

Definition at line 334 of file dict_thesaurus.c.

References LexemeInfo::idsubst, LexemeInfo::posinsubst, and LexemeInfo::tnvariant.

Referenced by cmpTheLexeme(), and compileTheLexeme().

335 {
336  if (a == NULL || b == NULL)
337  return 0;
338 
339  if (a->idsubst == b->idsubst)
340  {
341  if (a->posinsubst == b->posinsubst)
342  {
343  if (a->tnvariant == b->tnvariant)
344  return 0;
345 
346  return (a->tnvariant > b->tnvariant) ? 1 : -1;
347  }
348 
349  return (a->posinsubst > b->posinsubst) ? 1 : -1;
350  }
351 
352  return (a->idsubst > b->idsubst) ? 1 : -1;
353 }
uint32 idsubst
uint16 posinsubst
uint16 tnvariant

◆ cmpLexemeQ()

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

Definition at line 372 of file dict_thesaurus.c.

References cmpLexeme().

Referenced by findTheLexeme().

373 {
374  return cmpLexeme((const TheLexeme *) a, (const TheLexeme *) b);
375 }
static int cmpLexeme(const TheLexeme *a, const TheLexeme *b)

◆ cmpTheLexeme()

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

Definition at line 378 of file dict_thesaurus.c.

References cmpLexeme(), cmpLexemeInfo(), and TheLexeme::entries.

Referenced by compileTheLexeme().

379 {
380  const TheLexeme *la = (const TheLexeme *) a;
381  const TheLexeme *lb = (const TheLexeme *) b;
382  int res;
383 
384  if ((res = cmpLexeme(la, lb)) != 0)
385  return res;
386 
387  return -cmpLexemeInfo(la->entries, lb->entries);
388 }
LexemeInfo * entries
static int cmpLexemeInfo(LexemeInfo *a, LexemeInfo *b)
static int cmpLexeme(const TheLexeme *a, const TheLexeme *b)

◆ compileTheLexeme()

static void compileTheLexeme ( DictThesaurus d)
static

Definition at line 391 of file dict_thesaurus.c.

References addCompiledLexeme(), cmpLexeme(), cmpLexemeInfo(), cmpTheLexeme(), DatumGetPointer, TSDictionaryCacheEntry::dictData, TheLexeme::entries, ereport, errcode(), errhint(), errmsg(), ERROR, FunctionCall4, i, LexemeInfo::idsubst, Int32GetDatum, TheLexeme::lexeme, TSLexeme::lexeme, TSDictionaryCacheEntry::lexize, LexemeInfo::nextentry, DictThesaurus::ntwrds, TSLexeme::nvariant, DictThesaurus::nwrds, palloc(), pfree(), PointerGetDatum, qsort, repalloc(), DictThesaurus::subdict, and DictThesaurus::wrds.

Referenced by thesaurus_init().

392 {
393  int i,
394  nnw = 0,
395  tnm = 16;
396  TheLexeme *newwrds = (TheLexeme *) palloc(sizeof(TheLexeme) * tnm),
397  *ptrwrds;
398 
399  for (i = 0; i < d->nwrds; i++)
400  {
401  TSLexeme *ptr;
402 
403  if (strcmp(d->wrds[i].lexeme, "?") == 0) /* Is stop word marker? */
404  newwrds = addCompiledLexeme(newwrds, &nnw, &tnm, NULL, d->wrds[i].entries, 0);
405  else
406  {
409  PointerGetDatum(d->wrds[i].lexeme),
410  Int32GetDatum(strlen(d->wrds[i].lexeme)),
411  PointerGetDatum(NULL)));
412 
413  if (!ptr)
414  ereport(ERROR,
415  (errcode(ERRCODE_CONFIG_FILE_ERROR),
416  errmsg("thesaurus sample word \"%s\" isn't recognized by subdictionary (rule %d)",
417  d->wrds[i].lexeme,
418  d->wrds[i].entries->idsubst + 1)));
419  else if (!(ptr->lexeme))
420  ereport(ERROR,
421  (errcode(ERRCODE_CONFIG_FILE_ERROR),
422  errmsg("thesaurus sample word \"%s\" is a stop word (rule %d)",
423  d->wrds[i].lexeme,
424  d->wrds[i].entries->idsubst + 1),
425  errhint("Use \"?\" to represent a stop word within a sample phrase.")));
426  else
427  {
428  while (ptr->lexeme)
429  {
430  TSLexeme *remptr = ptr + 1;
431  int tnvar = 1;
432  int curvar = ptr->nvariant;
433 
434  /* compute n words in one variant */
435  while (remptr->lexeme)
436  {
437  if (remptr->nvariant != (remptr - 1)->nvariant)
438  break;
439  tnvar++;
440  remptr++;
441  }
442 
443  remptr = ptr;
444  while (remptr->lexeme && remptr->nvariant == curvar)
445  {
446  newwrds = addCompiledLexeme(newwrds, &nnw, &tnm, remptr, d->wrds[i].entries, tnvar);
447  remptr++;
448  }
449 
450  ptr = remptr;
451  }
452  }
453  }
454 
455  pfree(d->wrds[i].lexeme);
456  pfree(d->wrds[i].entries);
457  }
458 
459  if (d->wrds)
460  pfree(d->wrds);
461  d->wrds = newwrds;
462  d->nwrds = nnw;
463  d->ntwrds = tnm;
464 
465  if (d->nwrds > 1)
466  {
467  qsort(d->wrds, d->nwrds, sizeof(TheLexeme), cmpTheLexeme);
468 
469  /* uniq */
470  newwrds = d->wrds;
471  ptrwrds = d->wrds + 1;
472  while (ptrwrds - d->wrds < d->nwrds)
473  {
474  if (cmpLexeme(ptrwrds, newwrds) == 0)
475  {
476  if (cmpLexemeInfo(ptrwrds->entries, newwrds->entries))
477  {
478  ptrwrds->entries->nextentry = newwrds->entries;
479  newwrds->entries = ptrwrds->entries;
480  }
481  else
482  pfree(ptrwrds->entries);
483 
484  if (ptrwrds->lexeme)
485  pfree(ptrwrds->lexeme);
486  }
487  else
488  {
489  newwrds++;
490  *newwrds = *ptrwrds;
491  }
492 
493  ptrwrds++;
494  }
495 
496  d->nwrds = newwrds - d->wrds + 1;
497  d->wrds = (TheLexeme *) repalloc(d->wrds, sizeof(TheLexeme) * d->nwrds);
498  }
499 }
int errhint(const char *fmt,...)
Definition: elog.c:1149
uint32 idsubst
#define PointerGetDatum(X)
Definition: postgres.h:556
struct LexemeInfo * nextentry
static TheLexeme * addCompiledLexeme(TheLexeme *newwrds, int *nnw, int *tnm, TSLexeme *lexeme, LexemeInfo *src, uint16 tnvariant)
int errcode(int sqlerrcode)
Definition: elog.c:691
LexemeInfo * entries
char * lexeme
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ERROR
Definition: elog.h:43
char * lexeme
Definition: ts_public.h:111
#define FunctionCall4(flinfo, arg1, arg2, arg3, arg4)
Definition: fmgr.h:650
uint16 nvariant
Definition: ts_public.h:107
static int cmpLexemeInfo(LexemeInfo *a, LexemeInfo *b)
TSDictionaryCacheEntry * subdict
TheLexeme * wrds
#define ereport(elevel,...)
Definition: elog.h:155
static int cmpLexeme(const TheLexeme *a, const TheLexeme *b)
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1070
#define DatumGetPointer(X)
Definition: postgres.h:549
static int cmpTheLexeme(const void *a, const void *b)
#define Int32GetDatum(X)
Definition: postgres.h:479
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:902
int i
#define qsort(a, b, c, d)
Definition: port.h:497

◆ compileTheSubstitute()

static void compileTheSubstitute ( DictThesaurus d)
static

Definition at line 502 of file dict_thesaurus.c.

References DatumGetPointer, TSDictionaryCacheEntry::dictData, DT_USEASIS, ereport, errcode(), errmsg(), ERROR, TSLexeme::flags, FunctionCall4, i, Int32GetDatum, TSLexeme::lexeme, TSDictionaryCacheEntry::lexize, DictThesaurus::nsubst, palloc(), pfree(), PointerGetDatum, pstrdup(), repalloc(), TheSubstitute::res, TheSubstitute::reslen, DictThesaurus::subdict, DictThesaurus::subst, and TSL_ADDPOS.

Referenced by thesaurus_init().

503 {
504  int i;
505 
506  for (i = 0; i < d->nsubst; i++)
507  {
508  TSLexeme *rem = d->subst[i].res,
509  *outptr,
510  *inptr;
511  int n = 2;
512 
513  outptr = d->subst[i].res = (TSLexeme *) palloc(sizeof(TSLexeme) * n);
514  outptr->lexeme = NULL;
515  inptr = rem;
516 
517  while (inptr && inptr->lexeme)
518  {
519  TSLexeme *lexized,
520  tmplex[2];
521 
522  if (inptr->flags & DT_USEASIS)
523  { /* do not lexize */
524  tmplex[0] = *inptr;
525  tmplex[0].flags = 0;
526  tmplex[1].lexeme = NULL;
527  lexized = tmplex;
528  }
529  else
530  {
531  lexized = (TSLexeme *) DatumGetPointer(FunctionCall4(&(d->subdict->lexize),
533  PointerGetDatum(inptr->lexeme),
534  Int32GetDatum(strlen(inptr->lexeme)),
535  PointerGetDatum(NULL)));
536  }
537 
538  if (lexized && lexized->lexeme)
539  {
540  int toset = (lexized->lexeme && outptr != d->subst[i].res) ? (outptr - d->subst[i].res) : -1;
541 
542  while (lexized->lexeme)
543  {
544  if (outptr - d->subst[i].res + 1 >= n)
545  {
546  int diff = outptr - d->subst[i].res;
547 
548  n *= 2;
549  d->subst[i].res = (TSLexeme *) repalloc(d->subst[i].res, sizeof(TSLexeme) * n);
550  outptr = d->subst[i].res + diff;
551  }
552 
553  *outptr = *lexized;
554  outptr->lexeme = pstrdup(lexized->lexeme);
555 
556  outptr++;
557  lexized++;
558  }
559 
560  if (toset > 0)
561  d->subst[i].res[toset].flags |= TSL_ADDPOS;
562  }
563  else if (lexized)
564  {
565  ereport(ERROR,
566  (errcode(ERRCODE_CONFIG_FILE_ERROR),
567  errmsg("thesaurus substitute word \"%s\" is a stop word (rule %d)",
568  inptr->lexeme, i + 1)));
569  }
570  else
571  {
572  ereport(ERROR,
573  (errcode(ERRCODE_CONFIG_FILE_ERROR),
574  errmsg("thesaurus substitute word \"%s\" isn't recognized by subdictionary (rule %d)",
575  inptr->lexeme, i + 1)));
576  }
577 
578  if (inptr->lexeme)
579  pfree(inptr->lexeme);
580  inptr++;
581  }
582 
583  if (outptr == d->subst[i].res)
584  ereport(ERROR,
585  (errcode(ERRCODE_CONFIG_FILE_ERROR),
586  errmsg("thesaurus substitute phrase is empty (rule %d)",
587  i + 1)));
588 
589  d->subst[i].reslen = outptr - d->subst[i].res;
590 
591  pfree(rem);
592  }
593 }
#define TSL_ADDPOS
Definition: ts_public.h:115
TheSubstitute * subst
#define PointerGetDatum(X)
Definition: postgres.h:556
char * pstrdup(const char *in)
Definition: mcxt.c:1187
int errcode(int sqlerrcode)
Definition: elog.c:691
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ERROR
Definition: elog.h:43
char * lexeme
Definition: ts_public.h:111
#define FunctionCall4(flinfo, arg1, arg2, arg3, arg4)
Definition: fmgr.h:650
uint16 flags
Definition: ts_public.h:109
TSDictionaryCacheEntry * subdict
#define ereport(elevel,...)
Definition: elog.h:155
#define DT_USEASIS
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1070
#define DatumGetPointer(X)
Definition: postgres.h:549
#define Int32GetDatum(X)
Definition: postgres.h:479
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:902
TSLexeme * res
int i

◆ copyTSLexeme()

static TSLexeme* copyTSLexeme ( TheSubstitute ts)
static

Definition at line 751 of file dict_thesaurus.c.

References i, TSLexeme::lexeme, palloc(), pstrdup(), TheSubstitute::res, and TheSubstitute::reslen.

Referenced by checkMatch().

752 {
753  TSLexeme *res;
754  uint16 i;
755 
756  res = (TSLexeme *) palloc(sizeof(TSLexeme) * (ts->reslen + 1));
757  for (i = 0; i < ts->reslen; i++)
758  {
759  res[i] = ts->res[i];
760  res[i].lexeme = pstrdup(ts->res[i].lexeme);
761  }
762 
763  res[ts->reslen].lexeme = NULL;
764 
765  return res;
766 }
char * pstrdup(const char *in)
Definition: mcxt.c:1187
unsigned short uint16
Definition: c.h:428
char * lexeme
Definition: ts_public.h:111
void * palloc(Size size)
Definition: mcxt.c:950
TSLexeme * res
int i

◆ findTheLexeme()

static LexemeInfo* findTheLexeme ( DictThesaurus d,
char *  lexeme 
)
static

Definition at line 655 of file dict_thesaurus.c.

References cmpLexemeQ(), TheLexeme::entries, sort-test::key, TheLexeme::lexeme, DictThesaurus::nwrds, and DictThesaurus::wrds.

Referenced by thesaurus_lexize().

656 {
657  TheLexeme key,
658  *res;
659 
660  if (d->nwrds == 0)
661  return NULL;
662 
663  key.lexeme = lexeme;
664  key.entries = NULL;
665 
666  res = bsearch(&key, d->wrds, d->nwrds, sizeof(TheLexeme), cmpLexemeQ);
667 
668  if (res == NULL)
669  return NULL;
670  return res->entries;
671 }
LexemeInfo * entries
char * lexeme
static int cmpLexemeQ(const void *a, const void *b)
TheLexeme * wrds

◆ findVariant()

static LexemeInfo* findVariant ( LexemeInfo in,
LexemeInfo stored,
uint16  curpos,
LexemeInfo **  newin,
int  newn 
)
static

Definition at line 694 of file dict_thesaurus.c.

References i, LexemeInfo::idsubst, matchIdSubst(), LexemeInfo::nextentry, LexemeInfo::nextvariant, LexemeInfo::posinsubst, and LexemeInfo::tnvariant.

Referenced by thesaurus_lexize().

695 {
696  for (;;)
697  {
698  int i;
699  LexemeInfo *ptr = newin[0];
700 
701  for (i = 0; i < newn; i++)
702  {
703  while (newin[i] && newin[i]->idsubst < ptr->idsubst)
704  newin[i] = newin[i]->nextentry;
705 
706  if (newin[i] == NULL)
707  return in;
708 
709  if (newin[i]->idsubst > ptr->idsubst)
710  {
711  ptr = newin[i];
712  i = -1;
713  continue;
714  }
715 
716  while (newin[i]->idsubst == ptr->idsubst)
717  {
718  if (newin[i]->posinsubst == curpos && newin[i]->tnvariant == newn)
719  {
720  ptr = newin[i];
721  break;
722  }
723 
724  newin[i] = newin[i]->nextentry;
725  if (newin[i] == NULL)
726  return in;
727  }
728 
729  if (newin[i]->idsubst != ptr->idsubst)
730  {
731  ptr = newin[i];
732  i = -1;
733  continue;
734  }
735  }
736 
737  if (i == newn && matchIdSubst(stored, ptr->idsubst) && (in == NULL || !matchIdSubst(in, ptr->idsubst)))
738  { /* found */
739 
740  ptr->nextvariant = in;
741  in = ptr;
742  }
743 
744  /* step forward */
745  for (i = 0; i < newn; i++)
746  newin[i] = newin[i]->nextentry;
747  }
748 }
uint32 idsubst
struct LexemeInfo * nextentry
static bool matchIdSubst(LexemeInfo *stored, uint32 idsubst)
struct LexemeInfo * nextvariant
int i

◆ matchIdSubst()

static bool matchIdSubst ( LexemeInfo stored,
uint32  idsubst 
)
static

Definition at line 674 of file dict_thesaurus.c.

References LexemeInfo::idsubst, and LexemeInfo::nextvariant.

Referenced by findVariant().

675 {
676  bool res = true;
677 
678  if (stored)
679  {
680  res = false;
681 
682  for (; stored; stored = stored->nextvariant)
683  if (stored->idsubst == idsubst)
684  {
685  res = true;
686  break;
687  }
688  }
689 
690  return res;
691 }
uint32 idsubst
struct LexemeInfo * nextvariant

◆ newLexeme()

static void newLexeme ( DictThesaurus d,
char *  b,
char *  e,
uint32  idsubst,
uint16  posinsubst 
)
static

Definition at line 72 of file dict_thesaurus.c.

References TheLexeme::entries, LexemeInfo::idsubst, TheLexeme::lexeme, LexemeInfo::nextentry, DictThesaurus::ntwrds, DictThesaurus::nwrds, palloc(), LexemeInfo::posinsubst, repalloc(), and DictThesaurus::wrds.

Referenced by thesaurusRead().

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 }
uint32 idsubst
struct LexemeInfo * nextentry
LexemeInfo * entries
char * lexeme
uint16 posinsubst
TheLexeme * wrds
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1070
e
Definition: preproc-init.c:82
void * palloc(Size size)
Definition: mcxt.c:950

◆ thesaurus_init()

Datum thesaurus_init ( PG_FUNCTION_ARGS  )

Definition at line 596 of file dict_thesaurus.c.

References compileTheLexeme(), compileTheSubstitute(), defGetString(), DefElem::defname, ereport, errcode(), errmsg(), ERROR, get_ts_dict_oid(), lfirst, lookup_ts_dictionary_cache(), palloc0(), PG_GETARG_POINTER, PG_RETURN_POINTER, pstrdup(), stringToQualifiedNameList(), DictThesaurus::subdict, DictThesaurus::subdictOid, and thesaurusRead().

597 {
598  List *dictoptions = (List *) PG_GETARG_POINTER(0);
599  DictThesaurus *d;
600  char *subdictname = NULL;
601  bool fileloaded = false;
602  ListCell *l;
603 
604  d = (DictThesaurus *) palloc0(sizeof(DictThesaurus));
605 
606  foreach(l, dictoptions)
607  {
608  DefElem *defel = (DefElem *) lfirst(l);
609 
610  if (strcmp(defel->defname, "dictfile") == 0)
611  {
612  if (fileloaded)
613  ereport(ERROR,
614  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
615  errmsg("multiple DictFile parameters")));
616  thesaurusRead(defGetString(defel), d);
617  fileloaded = true;
618  }
619  else if (strcmp(defel->defname, "dictionary") == 0)
620  {
621  if (subdictname)
622  ereport(ERROR,
623  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
624  errmsg("multiple Dictionary parameters")));
625  subdictname = pstrdup(defGetString(defel));
626  }
627  else
628  {
629  ereport(ERROR,
630  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
631  errmsg("unrecognized Thesaurus parameter: \"%s\"",
632  defel->defname)));
633  }
634  }
635 
636  if (!fileloaded)
637  ereport(ERROR,
638  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
639  errmsg("missing DictFile parameter")));
640  if (!subdictname)
641  ereport(ERROR,
642  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
643  errmsg("missing Dictionary parameter")));
644 
645  d->subdictOid = get_ts_dict_oid(stringToQualifiedNameList(subdictname), false);
647 
648  compileTheLexeme(d);
650 
652 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
char * pstrdup(const char *in)
Definition: mcxt.c:1187
static void thesaurusRead(const char *filename, DictThesaurus *d)
int errcode(int sqlerrcode)
Definition: elog.c:691
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
static void compileTheLexeme(DictThesaurus *d)
TSDictionaryCacheEntry * lookup_ts_dictionary_cache(Oid dictId)
Definition: ts_cache.c:209
Oid get_ts_dict_oid(List *names, bool missing_ok)
Definition: namespace.c:2425
#define ERROR
Definition: elog.h:43
char * defGetString(DefElem *def)
Definition: define.c:49
void * palloc0(Size size)
Definition: mcxt.c:981
TSDictionaryCacheEntry * subdict
#define ereport(elevel,...)
Definition: elog.h:155
#define lfirst(lc)
Definition: pg_list.h:169
static void compileTheSubstitute(DictThesaurus *d)
List * stringToQualifiedNameList(const char *string)
Definition: regproc.c:1876
int errmsg(const char *fmt,...)
Definition: elog.c:902
char * defname
Definition: parsenodes.h:733
Definition: pg_list.h:50

◆ thesaurus_lexize()

Datum thesaurus_lexize ( PG_FUNCTION_ARGS  )

Definition at line 786 of file dict_thesaurus.c.

References checkMatch(), DatumGetPointer, TSDictionaryCacheEntry::dictData, elog, ERROR, findTheLexeme(), findVariant(), FunctionCall4, DictSubState::getnext, i, DictSubState::isend, TSDictionaryCacheEntry::isvalid, TSLexeme::lexeme, TSDictionaryCacheEntry::lexize, lookup_ts_dictionary_cache(), TSLexeme::nvariant, palloc(), pfree(), PG_GETARG_DATUM, PG_GETARG_POINTER, PG_NARGS, PG_RETURN_POINTER, PointerGetDatum, LexemeInfo::posinsubst, DictSubState::private_state, DictThesaurus::subdict, and DictThesaurus::subdictOid.

787 {
790  TSLexeme *res = NULL;
791  LexemeInfo *stored,
792  *info = NULL;
793  uint16 curpos = 0;
794  bool moreres = false;
795 
796  if (PG_NARGS() != 4 || dstate == NULL)
797  elog(ERROR, "forbidden call of thesaurus or nested call");
798 
799  if (dstate->isend)
800  PG_RETURN_POINTER(NULL);
801  stored = (LexemeInfo *) dstate->private_state;
802 
803  if (stored)
804  curpos = stored->posinsubst + 1;
805 
806  if (!d->subdict->isvalid)
808 
811  PG_GETARG_DATUM(1),
812  PG_GETARG_DATUM(2),
813  PointerGetDatum(NULL)));
814 
815  if (res && res->lexeme)
816  {
817  TSLexeme *ptr = res,
818  *basevar;
819 
820  while (ptr->lexeme)
821  {
822  uint16 nv = ptr->nvariant;
823  uint16 i,
824  nlex = 0;
825  LexemeInfo **infos;
826 
827  basevar = ptr;
828  while (ptr->lexeme && nv == ptr->nvariant)
829  {
830  nlex++;
831  ptr++;
832  }
833 
834  infos = (LexemeInfo **) palloc(sizeof(LexemeInfo *) * nlex);
835  for (i = 0; i < nlex; i++)
836  if ((infos[i] = findTheLexeme(d, basevar[i].lexeme)) == NULL)
837  break;
838 
839  if (i < nlex)
840  {
841  /* no chance to find */
842  pfree(infos);
843  continue;
844  }
845 
846  info = findVariant(info, stored, curpos, infos, nlex);
847  }
848  }
849  else if (res)
850  { /* stop-word */
851  LexemeInfo *infos = findTheLexeme(d, NULL);
852 
853  info = findVariant(NULL, stored, curpos, &infos, 1);
854  }
855  else
856  {
857  info = NULL; /* word isn't recognized */
858  }
859 
860  dstate->private_state = (void *) info;
861 
862  if (!info)
863  {
864  dstate->getnext = false;
865  PG_RETURN_POINTER(NULL);
866  }
867 
868  if ((res = checkMatch(d, info, curpos, &moreres)) != NULL)
869  {
870  dstate->getnext = moreres;
871  PG_RETURN_POINTER(res);
872  }
873 
874  dstate->getnext = true;
875 
876  PG_RETURN_POINTER(NULL);
877 }
static LexemeInfo * findTheLexeme(DictThesaurus *d, char *lexeme)
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PointerGetDatum(X)
Definition: postgres.h:556
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
bool getnext
Definition: ts_public.h:127
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
static TSLexeme * checkMatch(DictThesaurus *d, LexemeInfo *info, uint16 curpos, bool *moreres)
TSDictionaryCacheEntry * lookup_ts_dictionary_cache(Oid dictId)
Definition: ts_cache.c:209
unsigned short uint16
Definition: c.h:428
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ERROR
Definition: elog.h:43
char * lexeme
Definition: ts_public.h:111
uint16 posinsubst
#define FunctionCall4(flinfo, arg1, arg2, arg3, arg4)
Definition: fmgr.h:650
uint16 nvariant
Definition: ts_public.h:107
TSDictionaryCacheEntry * subdict
#define PG_NARGS()
Definition: fmgr.h:203
#define DatumGetPointer(X)
Definition: postgres.h:549
void * private_state
Definition: ts_public.h:128
void * palloc(Size size)
Definition: mcxt.c:950
#define elog(elevel,...)
Definition: elog.h:228
int i
static LexemeInfo * findVariant(LexemeInfo *in, LexemeInfo *stored, uint16 curpos, LexemeInfo **newin, int newn)

◆ thesaurusRead()

static void thesaurusRead ( const char *  filename,
DictThesaurus d 
)
static

Definition at line 168 of file dict_thesaurus.c.

References addWrd(), elog, ereport, errcode(), errmsg(), ERROR, get_tsearch_config_filename(), LexemeInfo::idsubst, newLexeme(), DictThesaurus::nsubst, pfree(), pg_mblen(), LexemeInfo::posinsubst, t_iseq, t_isspace(), TR_INLEX, TR_INSUBS, TR_WAITLEX, TR_WAITSUBS, tsearch_readline(), tsearch_readline_begin(), and tsearch_readline_end().

Referenced by thesaurus_init().

169 {
171  uint32 idsubst = 0;
172  bool useasis = false;
173  char *line;
174 
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  if (nwrd != (uint16) nwrd || posinsubst != (uint16) posinsubst)
290  ereport(ERROR,
291  (errcode(ERRCODE_CONFIG_FILE_ERROR),
292  errmsg("too many lexemes in thesaurus entry")));
293 
294  pfree(line);
295  }
296 
297  d->nsubst = idsubst;
298 
299  tsearch_readline_end(&trst);
300 }
int errcode(int sqlerrcode)
Definition: elog.c:691
unsigned short uint16
Definition: c.h:428
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
char * get_tsearch_config_filename(const char *basename, const char *extension)
Definition: ts_utils.c:33
int t_isspace(const char *ptr)
Definition: ts_locale.c:53
#define t_iseq(x, c)
Definition: ts_locale.h:46
unsigned int uint32
Definition: c.h:429
#define ereport(elevel,...)
Definition: elog.h:155
#define TR_INLEX
Definition: regguts.h:298
int pg_mblen(const char *mbstr)
Definition: mbutils.c:907
void tsearch_readline_end(tsearch_readline_state *stp)
Definition: ts_locale.c:193
char * tsearch_readline(tsearch_readline_state *stp)
Definition: ts_locale.c:148
bool tsearch_readline_begin(tsearch_readline_state *stp, const char *filename)
Definition: ts_locale.c:125
static char * filename
Definition: pg_dumpall.c:91
#define TR_WAITLEX
int errmsg(const char *fmt,...)
Definition: elog.c:902
#define TR_WAITSUBS
#define elog(elevel,...)
Definition: elog.h:228
#define TR_INSUBS
static void newLexeme(DictThesaurus *d, char *b, char *e, uint32 idsubst, uint16 posinsubst)