2 * interface functions to dictionary
3 * Teodor Sigaev <teodor@sigaev.ru>
12 #include "utils/array.h"
13 #include "catalog/pg_type.h"
14 #include "executor/spi.h"
20 /*********top interface**********/
23 init_dict(Oid id, DictInfo * dict)
31 char *nsp = get_namespace(TSNSP_FunctionOid);
34 pars[0] = ObjectIdGetDatum(id);
36 memset(dict, 0, sizeof(DictInfo));
38 sprintf(buf, "select dict_init, dict_initoption, dict_lexize from %s.pg_ts_dict where oid = $1", nsp);
40 plan = SPI_prepare(buf, 1, arg);
42 ts_error(ERROR, "SPI_prepare() failed");
44 stat = SPI_execp(plan, pars, " ", 1);
46 ts_error(ERROR, "SPI_execp return %d", stat);
47 if (SPI_processed > 0)
52 oid = DatumGetObjectId(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull));
53 if (!(isnull || oid == InvalidOid))
55 opt = SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 2, &isnull);
56 dict->dictionary = (void *) DatumGetPointer(OidFunctionCall1(oid, opt));
58 oid = DatumGetObjectId(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 3, &isnull));
59 if (isnull || oid == InvalidOid)
60 ts_error(ERROR, "Null dict_lexize for dictonary %d", id);
61 fmgr_info_cxt(oid, &(dict->lexize_info), TopMemoryContext);
65 ts_error(ERROR, "No dictionary with id %d", id);
79 static DictList DList = {NULL, 0, 0, NULL, {0, 0, NULL}};
84 freeSNMap(&(DList.name2id_map));
85 /* XXX need to free DList.list[*].dictionary */
88 memset(&DList, 0, sizeof(DictList));
93 comparedict(const void *a, const void *b)
95 if ( ((DictInfo *) a)->dict_id == ((DictInfo *) b)->dict_id )
97 return ( ((DictInfo *) a)->dict_id < ((DictInfo *) b)->dict_id ) ? -1 : 1;
104 if (DList.last_dict && DList.last_dict->dict_id == id)
105 return DList.last_dict;
108 /* already used dict */
114 DList.last_dict = bsearch(&key, DList.list, DList.len, sizeof(DictInfo), comparedict);
115 if (DList.last_dict != NULL)
116 return DList.last_dict;
120 if (DList.len == DList.reallen)
123 int reallen = (DList.reallen) ? 2 * DList.reallen : 16;
125 tmp = (DictInfo *) realloc(DList.list, sizeof(DictInfo) * reallen);
127 ts_error(ERROR, "No memory");
128 DList.reallen = reallen;
131 DList.last_dict = &(DList.list[DList.len]);
132 init_dict(id, DList.last_dict);
135 qsort(DList.list, DList.len, sizeof(DictInfo), comparedict);
136 return finddict(id); /* qsort changed order!! */ ;
140 name2id_dict(text *name)
146 Oid id = findSNMap_t(&(DList.name2id_map), name);
152 pars[0] = PointerGetDatum(name);
157 nsp = get_namespace(TSNSP_FunctionOid);
159 sprintf(buf, "select oid from %s.pg_ts_dict where dict_name = $1", nsp);
161 plan = SPI_prepare(buf, 1, arg);
163 ts_error(ERROR, "SPI_prepare() failed");
165 stat = SPI_execp(plan, pars, " ", 1);
167 ts_error(ERROR, "SPI_execp return %d", stat);
168 if (SPI_processed > 0)
169 id = DatumGetObjectId(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull));
171 ts_error(ERROR, "No dictionary with name '%s'", text2char(name));
174 addSNMap_t(&(DList.name2id_map), name, id);
179 /******sql-level interface******/
180 PG_FUNCTION_INFO_V1(lexize);
181 Datum lexize(PG_FUNCTION_ARGS);
184 lexize(PG_FUNCTION_ARGS)
186 text *in = PG_GETARG_TEXT_P(1);
194 dict = finddict(PG_GETARG_OID(0));
196 ptr = res = (TSLexeme *) DatumGetPointer(
197 FunctionCall3(&(dict->lexize_info),
198 PointerGetDatum(dict->dictionary),
199 PointerGetDatum(VARDATA(in)),
200 Int32GetDatum(VARSIZE(in) - VARHDRSZ)
203 PG_FREE_IF_COPY(in, 1);
207 PG_RETURN_POINTER(NULL);
214 da = (Datum *) palloc(sizeof(Datum) * (ptr - res + 1));
218 da[ptr - res] = PointerGetDatum(char2text(ptr->lexeme));
234 pfree(DatumGetPointer(da[ptr - res]));
241 PG_RETURN_POINTER(a);
244 PG_FUNCTION_INFO_V1(lexize_byname);
245 Datum lexize_byname(PG_FUNCTION_ARGS);
247 lexize_byname(PG_FUNCTION_ARGS)
249 text *dictname = PG_GETARG_TEXT_P(0);
254 res = DirectFunctionCall3(
256 ObjectIdGetDatum(name2id_dict(dictname)),
260 PG_FREE_IF_COPY(dictname, 0);
262 PG_RETURN_DATUM(res);
267 static Oid currect_dictionary_id = 0;
269 PG_FUNCTION_INFO_V1(set_curdict);
270 Datum set_curdict(PG_FUNCTION_ARGS);
272 set_curdict(PG_FUNCTION_ARGS)
275 finddict(PG_GETARG_OID(0));
276 currect_dictionary_id = PG_GETARG_OID(0);
280 PG_FUNCTION_INFO_V1(set_curdict_byname);
281 Datum set_curdict_byname(PG_FUNCTION_ARGS);
283 set_curdict_byname(PG_FUNCTION_ARGS)
285 text *dictname = PG_GETARG_TEXT_P(0);
290 ObjectIdGetDatum(name2id_dict(dictname))
292 PG_FREE_IF_COPY(dictname, 0);
296 PG_FUNCTION_INFO_V1(lexize_bycurrent);
297 Datum lexize_bycurrent(PG_FUNCTION_ARGS);
299 lexize_bycurrent(PG_FUNCTION_ARGS)
304 if (currect_dictionary_id == 0)
306 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
307 errmsg("no currect dictionary"),
308 errhint("Execute select set_curdict().")));
310 res = DirectFunctionCall3(
312 ObjectIdGetDatum(currect_dictionary_id),
317 PG_RETURN_DATUM(res);