PostgreSQL Source Code  git master
dict_xsyn.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * dict_xsyn.c
4  * Extended synonym dictionary
5  *
6  * Copyright (c) 2007-2024, PostgreSQL Global Development Group
7  *
8  * IDENTIFICATION
9  * contrib/dict_xsyn/dict_xsyn.c
10  *
11  *-------------------------------------------------------------------------
12  */
13 #include "postgres.h"
14 
15 #include <ctype.h>
16 
17 #include "commands/defrem.h"
18 #include "tsearch/ts_locale.h"
19 #include "tsearch/ts_utils.h"
20 
22 
23 typedef struct
24 {
25  char *key; /* Word */
26  char *value; /* Unparsed list of synonyms, including the
27  * word itself */
28 } Syn;
29 
30 typedef struct
31 {
32  int len;
33  Syn *syn;
34 
35  bool matchorig;
36  bool keeporig;
39 } DictSyn;
40 
41 
44 
45 static char *
46 find_word(char *in, char **end)
47 {
48  char *start;
49 
50  *end = NULL;
51  while (*in && t_isspace(in))
52  in += pg_mblen(in);
53 
54  if (!*in || *in == '#')
55  return NULL;
56  start = in;
57 
58  while (*in && !t_isspace(in))
59  in += pg_mblen(in);
60 
61  *end = in;
62 
63  return start;
64 }
65 
66 static int
67 compare_syn(const void *a, const void *b)
68 {
69  return strcmp(((const Syn *) a)->key, ((const Syn *) b)->key);
70 }
71 
72 static void
74 {
75  char *real_filename = get_tsearch_config_filename(filename, "rules");
77  char *line;
78  int cur = 0;
79 
80  if (!tsearch_readline_begin(&trst, real_filename))
81  ereport(ERROR,
82  (errcode(ERRCODE_CONFIG_FILE_ERROR),
83  errmsg("could not open synonym file \"%s\": %m",
84  real_filename)));
85 
86  while ((line = tsearch_readline(&trst)) != NULL)
87  {
88  char *value;
89  char *key;
90  char *pos;
91  char *end;
92 
93  if (*line == '\0')
94  continue;
95 
96  value = lowerstr(line);
97  pfree(line);
98 
99  pos = value;
100  while ((key = find_word(pos, &end)) != NULL)
101  {
102  /* Enlarge syn structure if full */
103  if (cur == d->len)
104  {
105  d->len = (d->len > 0) ? 2 * d->len : 16;
106  if (d->syn)
107  d->syn = (Syn *) repalloc(d->syn, sizeof(Syn) * d->len);
108  else
109  d->syn = (Syn *) palloc(sizeof(Syn) * d->len);
110  }
111 
112  /* Save first word only if we will match it */
113  if (pos != value || d->matchorig)
114  {
115  d->syn[cur].key = pnstrdup(key, end - key);
116  d->syn[cur].value = pstrdup(value);
117 
118  cur++;
119  }
120 
121  pos = end;
122 
123  /* Don't bother scanning synonyms if we will not match them */
124  if (!d->matchsynonyms)
125  break;
126  }
127 
128  pfree(value);
129  }
130 
131  tsearch_readline_end(&trst);
132 
133  d->len = cur;
134  if (cur > 1)
135  qsort(d->syn, d->len, sizeof(Syn), compare_syn);
136 
137  pfree(real_filename);
138 }
139 
140 Datum
142 {
143  List *dictoptions = (List *) PG_GETARG_POINTER(0);
144  DictSyn *d;
145  ListCell *l;
146  char *filename = NULL;
147 
148  d = (DictSyn *) palloc0(sizeof(DictSyn));
149  d->len = 0;
150  d->syn = NULL;
151  d->matchorig = true;
152  d->keeporig = true;
153  d->matchsynonyms = false;
154  d->keepsynonyms = true;
155 
156  foreach(l, dictoptions)
157  {
158  DefElem *defel = (DefElem *) lfirst(l);
159 
160  if (strcmp(defel->defname, "matchorig") == 0)
161  {
162  d->matchorig = defGetBoolean(defel);
163  }
164  else if (strcmp(defel->defname, "keeporig") == 0)
165  {
166  d->keeporig = defGetBoolean(defel);
167  }
168  else if (strcmp(defel->defname, "matchsynonyms") == 0)
169  {
170  d->matchsynonyms = defGetBoolean(defel);
171  }
172  else if (strcmp(defel->defname, "keepsynonyms") == 0)
173  {
174  d->keepsynonyms = defGetBoolean(defel);
175  }
176  else if (strcmp(defel->defname, "rules") == 0)
177  {
178  /* we can't read the rules before parsing all options! */
179  filename = defGetString(defel);
180  }
181  else
182  {
183  ereport(ERROR,
184  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
185  errmsg("unrecognized xsyn parameter: \"%s\"",
186  defel->defname)));
187  }
188  }
189 
190  if (filename)
192 
194 }
195 
196 Datum
198 {
199  DictSyn *d = (DictSyn *) PG_GETARG_POINTER(0);
200  char *in = (char *) PG_GETARG_POINTER(1);
201  int length = PG_GETARG_INT32(2);
202  Syn word;
203  Syn *found;
204  TSLexeme *res = NULL;
205 
206  if (!length || d->len == 0)
207  PG_RETURN_POINTER(NULL);
208 
209  /* Create search pattern */
210  {
211  char *temp = pnstrdup(in, length);
212 
213  word.key = lowerstr(temp);
214  pfree(temp);
215  word.value = NULL;
216  }
217 
218  /* Look for matching syn */
219  found = (Syn *) bsearch(&word, d->syn, d->len, sizeof(Syn), compare_syn);
220  pfree(word.key);
221 
222  if (!found)
223  PG_RETURN_POINTER(NULL);
224 
225  /* Parse string of synonyms and return array of words */
226  {
227  char *value = found->value;
228  char *syn;
229  char *pos;
230  char *end;
231  int nsyns = 0;
232 
233  res = palloc(sizeof(TSLexeme));
234 
235  pos = value;
236  while ((syn = find_word(pos, &end)) != NULL)
237  {
238  res = repalloc(res, sizeof(TSLexeme) * (nsyns + 2));
239 
240  /* The first word is output only if keeporig=true */
241  if (pos != value || d->keeporig)
242  {
243  res[nsyns].lexeme = pnstrdup(syn, end - syn);
244  res[nsyns].nvariant = 0;
245  res[nsyns].flags = 0;
246  nsyns++;
247  }
248 
249  pos = end;
250 
251  /* Stop if we are not to output the synonyms */
252  if (!d->keepsynonyms)
253  break;
254  }
255  res[nsyns].lexeme = NULL;
256  }
257 
259 }
bool defGetBoolean(DefElem *def)
Definition: define.c:107
char * defGetString(DefElem *def)
Definition: define.c:48
static char * find_word(char *in, char **end)
Definition: dict_xsyn.c:46
PG_MODULE_MAGIC
Definition: dict_xsyn.c:21
static int compare_syn(const void *a, const void *b)
Definition: dict_xsyn.c:67
Datum dxsyn_init(PG_FUNCTION_ARGS)
Definition: dict_xsyn.c:141
static void read_dictionary(DictSyn *d, const char *filename)
Definition: dict_xsyn.c:73
Datum dxsyn_lexize(PG_FUNCTION_ARGS)
Definition: dict_xsyn.c:197
PG_FUNCTION_INFO_V1(dxsyn_init)
struct cursor * cur
Definition: ecpg.c:28
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
return str start
static struct @155 value
int b
Definition: isn.c:70
int a
Definition: isn.c:69
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
int pg_mblen(const char *mbstr)
Definition: mbutils.c:1023
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1706
char * pstrdup(const char *in)
Definition: mcxt.c:1695
void pfree(void *pointer)
Definition: mcxt.c:1520
void * palloc0(Size size)
Definition: mcxt.c:1346
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1540
void * palloc(Size size)
Definition: mcxt.c:1316
static char * filename
Definition: pg_dumpall.c:119
#define lfirst(lc)
Definition: pg_list.h:172
#define qsort(a, b, c, d)
Definition: port.h:449
uintptr_t Datum
Definition: postgres.h:64
static void word(struct vars *v, int dir, struct state *lp, struct state *rp)
Definition: regcomp.c:1474
char * defname
Definition: parsenodes.h:815
bool matchsynonyms
Definition: dict_xsyn.c:37
Syn * syn
Definition: dict_xsyn.c:33
bool keeporig
Definition: dict_xsyn.c:36
int len
Definition: dict_xsyn.c:32
bool matchorig
Definition: dict_xsyn.c:35
bool keepsynonyms
Definition: dict_xsyn.c:38
Definition: pg_list.h:54
Definition: dict_xsyn.c:24
char * value
Definition: dict_xsyn.c:26
char * key
Definition: dict_xsyn.c:25
bool tsearch_readline_begin(tsearch_readline_state *stp, const char *filename)
Definition: ts_locale.c:134
char * tsearch_readline(tsearch_readline_state *stp)
Definition: ts_locale.c:157
int t_isspace(const char *ptr)
Definition: ts_locale.c:50
void tsearch_readline_end(tsearch_readline_state *stp)
Definition: ts_locale.c:202
char * lowerstr(const char *str)
Definition: ts_locale.c:253
char * get_tsearch_config_filename(const char *basename, const char *extension)
Definition: ts_utils.c:33