Avoid updating inactive_since for invalid replication slots.
[pgsql.git] / src / backend / tsearch / dict_ispell.c
blob63bd193a78a894b3bff1080e9147882236a31066
1 /*-------------------------------------------------------------------------
3 * dict_ispell.c
4 * Ispell dictionary interface
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
9 * IDENTIFICATION
10 * src/backend/tsearch/dict_ispell.c
12 *-------------------------------------------------------------------------
14 #include "postgres.h"
16 #include "catalog/pg_collation_d.h"
17 #include "commands/defrem.h"
18 #include "tsearch/dicts/spell.h"
19 #include "tsearch/ts_public.h"
20 #include "utils/fmgrprotos.h"
21 #include "utils/formatting.h"
24 typedef struct
26 StopList stoplist;
27 IspellDict obj;
28 } DictISpell;
30 Datum
31 dispell_init(PG_FUNCTION_ARGS)
33 List *dictoptions = (List *) PG_GETARG_POINTER(0);
34 DictISpell *d;
35 bool affloaded = false,
36 dictloaded = false,
37 stoploaded = false;
38 ListCell *l;
40 d = (DictISpell *) palloc0(sizeof(DictISpell));
42 NIStartBuild(&(d->obj));
44 foreach(l, dictoptions)
46 DefElem *defel = (DefElem *) lfirst(l);
48 if (strcmp(defel->defname, "dictfile") == 0)
50 if (dictloaded)
51 ereport(ERROR,
52 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
53 errmsg("multiple DictFile parameters")));
54 NIImportDictionary(&(d->obj),
55 get_tsearch_config_filename(defGetString(defel),
56 "dict"));
57 dictloaded = true;
59 else if (strcmp(defel->defname, "afffile") == 0)
61 if (affloaded)
62 ereport(ERROR,
63 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
64 errmsg("multiple AffFile parameters")));
65 NIImportAffixes(&(d->obj),
66 get_tsearch_config_filename(defGetString(defel),
67 "affix"));
68 affloaded = true;
70 else if (strcmp(defel->defname, "stopwords") == 0)
72 if (stoploaded)
73 ereport(ERROR,
74 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
75 errmsg("multiple StopWords parameters")));
76 readstoplist(defGetString(defel), &(d->stoplist), str_tolower);
77 stoploaded = true;
79 else
81 ereport(ERROR,
82 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
83 errmsg("unrecognized Ispell parameter: \"%s\"",
84 defel->defname)));
88 if (affloaded && dictloaded)
90 NISortDictionary(&(d->obj));
91 NISortAffixes(&(d->obj));
93 else if (!affloaded)
95 ereport(ERROR,
96 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
97 errmsg("missing AffFile parameter")));
99 else
101 ereport(ERROR,
102 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
103 errmsg("missing DictFile parameter")));
106 NIFinishBuild(&(d->obj));
108 PG_RETURN_POINTER(d);
111 Datum
112 dispell_lexize(PG_FUNCTION_ARGS)
114 DictISpell *d = (DictISpell *) PG_GETARG_POINTER(0);
115 char *in = (char *) PG_GETARG_POINTER(1);
116 int32 len = PG_GETARG_INT32(2);
117 char *txt;
118 TSLexeme *res;
119 TSLexeme *ptr,
120 *cptr;
122 if (len <= 0)
123 PG_RETURN_POINTER(NULL);
125 txt = str_tolower(in, len, DEFAULT_COLLATION_OID);
126 res = NINormalizeWord(&(d->obj), txt);
128 if (res == NULL)
129 PG_RETURN_POINTER(NULL);
131 cptr = res;
132 for (ptr = cptr; ptr->lexeme; ptr++)
134 if (searchstoplist(&(d->stoplist), ptr->lexeme))
136 pfree(ptr->lexeme);
137 ptr->lexeme = NULL;
139 else
141 if (cptr != ptr)
142 memcpy(cptr, ptr, sizeof(TSLexeme));
143 cptr++;
146 cptr->lexeme = NULL;
148 PG_RETURN_POINTER(res);