Datum reset_tsearch(PG_FUNCTION_ARGS) { SET_FUNCOID(); ts_error(NOTICE, "TSearch cache cleaned"); PG_RETURN_VOID(); }
void init_dict(Oid id, DictInfo * dict) { Oid arg[1]; bool isnull; Datum pars[1]; int stat; void *plan; char buf[1024]; char *nsp = get_namespace(TSNSP_FunctionOid); arg[0] = OIDOID; pars[0] = ObjectIdGetDatum(id); memset(dict, 0, sizeof(DictInfo)); SPI_connect(); sprintf(buf, "select dict_init, dict_initoption, dict_lexize from %s.pg_ts_dict where oid = $1", nsp); pfree(nsp); plan = SPI_prepare(buf, 1, arg); if (!plan) ts_error(ERROR, "SPI_prepare() failed"); stat = SPI_execp(plan, pars, " ", 1); if (stat < 0) ts_error(ERROR, "SPI_execp return %d", stat); if (SPI_processed > 0) { Datum opt; Oid oid = InvalidOid; oid = DatumGetObjectId(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull)); if (!(isnull || oid == InvalidOid)) { opt = SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 2, &isnull); dict->dictionary = (void *) DatumGetPointer(OidFunctionCall1(oid, opt)); } oid = DatumGetObjectId(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 3, &isnull)); if (isnull || oid == InvalidOid) ts_error(ERROR, "Null dict_lexize for dictonary %d", id); fmgr_info_cxt(oid, &(dict->lexize_info), TopMemoryContext); dict->dict_id = id; } else ts_error(ERROR, "No dictionary with id %d", id); SPI_freeplan(plan); SPI_finish(); }
void exit_start(t_pm *pm, t_exit *e) { pm->sfd = -1; e->pm = pm; e->es = 0; if ((e->pret = pthread_create(&e->th1, NULL, exit_monitor, e)) != 0) ts_error("exit_start", pm, e->pret); }
void init_prs(Oid id, WParserInfo * prs) { Oid arg[1]; bool isnull; Datum pars[1]; int stat; void *plan; char buf[1024], *nsp; arg[0] = OIDOID; pars[0] = ObjectIdGetDatum(id); memset(prs, 0, sizeof(WParserInfo)); SPI_connect(); nsp = get_namespace(TSNSP_FunctionOid); sprintf(buf, "select prs_start, prs_nexttoken, prs_end, prs_lextype, prs_headline from %s.pg_ts_parser where oid = $1", nsp); pfree(nsp); plan = SPI_prepare(buf, 1, arg); if (!plan) ts_error(ERROR, "SPI_prepare() failed"); stat = SPI_execp(plan, pars, " ", 1); if (stat < 0) ts_error(ERROR, "SPI_execp return %d", stat); if (SPI_processed > 0) { Oid oid = InvalidOid; oid = DatumGetObjectId(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull)); fmgr_info_cxt(oid, &(prs->start_info), TopMemoryContext); oid = DatumGetObjectId(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 2, &isnull)); fmgr_info_cxt(oid, &(prs->getlexeme_info), TopMemoryContext); oid = DatumGetObjectId(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 3, &isnull)); fmgr_info_cxt(oid, &(prs->end_info), TopMemoryContext); prs->lextype = DatumGetObjectId(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 4, &isnull)); oid = DatumGetObjectId(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 5, &isnull)); fmgr_info_cxt(oid, &(prs->headline_info), TopMemoryContext); prs->prs_id = id; } else ts_error(ERROR, "No parser with id %d", id); SPI_freeplan(plan); SPI_finish(); }
text * mtextdup(text *in) { text *out = (text *) malloc(VARSIZE(in)); if (!out) ts_error(ERROR, "No memory"); memcpy(out, in, VARSIZE(in)); return out; }
static RegisNode * newRegisNode(RegisNode * prev, int len) { RegisNode *ptr; ptr = (RegisNode *) malloc(RNHDRSZ + len + 1); if (!ptr) ts_error(ERROR, "No memory"); memset(ptr, 0, RNHDRSZ + len + 1); if (prev) prev->next = ptr; return ptr; }
Oid name2id_prs(text *name) { Oid arg[1]; bool isnull; Datum pars[1]; int stat; Oid id = findSNMap_t(&(PList.name2id_map), name); char buf[1024], *nsp; void *plan; arg[0] = TEXTOID; pars[0] = PointerGetDatum(name); if (id) return id; SPI_connect(); nsp = get_namespace(TSNSP_FunctionOid); sprintf(buf, "select oid from %s.pg_ts_parser where prs_name = $1", nsp); pfree(nsp); plan = SPI_prepare(buf, 1, arg); if (!plan) ts_error(ERROR, "SPI_prepare() failed"); stat = SPI_execp(plan, pars, " ", 1); if (stat < 0) ts_error(ERROR, "SPI_execp return %d", stat); if (SPI_processed > 0) id = DatumGetObjectId(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull)); else ts_error(ERROR, "No parser '%s'", text2char(name)); SPI_freeplan(plan); SPI_finish(); addSNMap_t(&(PList.name2id_map), name, id); return id; }
int RS_execute(Regis * r, const char *str, int len) { RegisNode *ptr = r->node; unsigned char *c; if (len < 0) len = strlen(str); if (len < r->nchar) return 0; if (r->issuffix) c = ((unsigned char *) str) + len - r->nchar; else c = (unsigned char *) str; while (ptr) { switch (ptr->type) { case RSF_ONEOF: if (ptr->len == 0) { if (*c != *(ptr->data)) return 0; } else if (strchr((char *) ptr->data, *c) == NULL) return 0; break; case RSF_NONEOF: if (ptr->len == 0) { if (*c == *(ptr->data)) return 0; } else if (strchr((char *) ptr->data, *c) != NULL) return 0; break; default: ts_error(ERROR, "RS_execute: Unknown type node: %d\n", ptr->type); } ptr = ptr->next; c++; } return 1; }
DictInfo * finddict(Oid id) { /* last used dict */ if (DList.last_dict && DList.last_dict->dict_id == id) return DList.last_dict; /* already used dict */ if (DList.len != 0) { DictInfo key; key.dict_id = id; DList.last_dict = bsearch(&key, DList.list, DList.len, sizeof(DictInfo), comparedict); if (DList.last_dict != NULL) return DList.last_dict; } /* last chance */ if (DList.len == DList.reallen) { DictInfo *tmp; int reallen = (DList.reallen) ? 2 * DList.reallen : 16; tmp = (DictInfo *) realloc(DList.list, sizeof(DictInfo) * reallen); if (!tmp) ts_error(ERROR, "No memory"); DList.reallen = reallen; DList.list = tmp; } DList.last_dict = &(DList.list[DList.len]); init_dict(id, DList.last_dict); DList.len++; qsort(DList.list, DList.len, sizeof(DictInfo), comparedict); return finddict(id); /* qsort changed order!! */ ; }
WParserInfo * findprs(Oid id) { /* last used prs */ if (PList.last_prs && PList.last_prs->prs_id == id) return PList.last_prs; /* already used prs */ if (PList.len != 0) { WParserInfo key; key.prs_id = id; PList.last_prs = bsearch(&key, PList.list, PList.len, sizeof(WParserInfo), compareprs); if (PList.last_prs != NULL) return PList.last_prs; } /* last chance */ if (PList.len == PList.reallen) { WParserInfo *tmp; int reallen = (PList.reallen) ? 2 * PList.reallen : 16; tmp = (WParserInfo *) realloc(PList.list, sizeof(WParserInfo) * reallen); if (!tmp) ts_error(ERROR, "No memory"); PList.reallen = reallen; PList.list = tmp; } PList.last_prs = &(PList.list[PList.len]); init_prs(id, PList.last_prs); PList.len++; qsort(PList.list, PList.len, sizeof(WParserInfo), compareprs); return findprs(id); /* qsort changed order!! */ ; }
TSCfgInfo * findcfg(Oid id) { /* last used cfg */ if (CList.last_cfg && CList.last_cfg->id == id) return CList.last_cfg; /* already used cfg */ if (CList.len != 0) { TSCfgInfo key; key.id = id; CList.last_cfg = bsearch(&key, CList.list, CList.len, sizeof(TSCfgInfo), comparecfg); if (CList.last_cfg != NULL) return CList.last_cfg; } /* last chance */ if (CList.len == CList.reallen) { TSCfgInfo *tmp; int reallen = (CList.reallen) ? 2 * CList.reallen : 16; tmp = (TSCfgInfo *) realloc(CList.list, sizeof(TSCfgInfo) * reallen); if (!tmp) ts_error(ERROR, "No memory"); CList.reallen = reallen; CList.list = tmp; } init_cfg(id, &(CList.list[CList.len]) ); CList.last_cfg = &(CList.list[CList.len]); CList.len++; qsort(CList.list, CList.len, sizeof(TSCfgInfo), comparecfg); return findcfg(id); /* qsort changed order!! */ ; }
int RS_compile(Regis * r, int issuffix, const char *str) { int i, len = strlen(str); int state = RS_IN_WAIT; RegisNode *ptr = NULL; memset(r, 0, sizeof(Regis)); r->issuffix = (issuffix) ? 1 : 0; for (i = 0; i < len; i++) { unsigned char c = *(((unsigned char *) str) + i); if (state == RS_IN_WAIT) { if (isalpha(c)) { if (ptr) ptr = newRegisNode(ptr, len); else ptr = r->node = newRegisNode(NULL, len); ptr->data[0] = c; ptr->type = RSF_ONEOF; ptr->len = 1; } else if (c == '[') { if (ptr) ptr = newRegisNode(ptr, len); else ptr = r->node = newRegisNode(NULL, len); ptr->type = RSF_ONEOF; state = RS_IN_ONEOF; } else ts_error(ERROR, "Error in regis: %s at pos %d\n", str, i + 1); } else if (state == RS_IN_ONEOF) { if (c == '^') { ptr->type = RSF_NONEOF; state = RS_IN_NONEOF; } else if (isalpha(c)) { ptr->data[0] = c; ptr->len = 1; state = RS_IN_ONEOF_IN; } else ts_error(ERROR, "Error in regis: %s at pos %d\n", str, i + 1); } else if (state == RS_IN_ONEOF_IN || state == RS_IN_NONEOF) { if (isalpha(c)) { ptr->data[ptr->len] = c; ptr->len++; } else if (c == ']') state = RS_IN_WAIT; else ts_error(ERROR, "Error in regis: %s at pos %d\n", str, i + 1); } else ts_error(ERROR, "Internal error in RS_compile: %d\n", state); } ptr = r->node; while (ptr) { r->nchar++; ptr = ptr->next; } return 0; }
int ts_config(struct tsdev *ts) { char buf[80], *p; FILE *f; int line = 0, ret = 0; char *conffile; if( (conffile = getenv("TSLIB_CONFFILE")) != NULL) { f = fopen(conffile,"r"); } else { f = fopen(TS_CONF, "r"); } if (!f) return -1; while ((p = fgets(buf, sizeof(buf), f)) != NULL && ret == 0) { struct opt *opt; char *e, *tok; line++; /* * Did we read a whole line? */ e = strchr(p, '\n'); if (!e) { ts_error("%d: line too long", line); break; } /* * Chomp. */ *e = '\0'; tok = strsep(&p, " \t"); /* * Ignore comments or blank lines. */ if (!tok || *tok == '#') continue; /* * Search for the option. */ for (opt = options; opt < options + NR_OPTS; opt++) if (strcasecmp(tok, opt->str) == 0) { ret = opt->fn(ts, p); break; } if (opt == options + NR_OPTS) { ts_error("%d: option `%s' not recognised", line, tok); ret = -1; } } fclose(f); return ret; }
void init_cfg(Oid id, TSCfgInfo * cfg) { Oid arg[2]; bool isnull; Datum pars[2]; int stat, i, j; text *ptr; text *prsname = NULL; char *nsp = get_namespace(TSNSP_FunctionOid); char buf[1024]; MemoryContext oldcontext; void *plan; arg[0] = OIDOID; arg[1] = OIDOID; pars[0] = ObjectIdGetDatum(id); pars[1] = ObjectIdGetDatum(id); memset(cfg, 0, sizeof(TSCfgInfo)); SPI_connect(); sprintf(buf, "select prs_name from %s.pg_ts_cfg where oid = $1", nsp); plan = SPI_prepare(buf, 1, arg); if (!plan) ts_error(ERROR, "SPI_prepare() failed"); stat = SPI_execp(plan, pars, " ", 1); if (stat < 0) ts_error(ERROR, "SPI_execp return %d", stat); if (SPI_processed > 0) { prsname = (text *) DatumGetPointer( SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull) ); oldcontext = MemoryContextSwitchTo(TopMemoryContext); prsname = ptextdup(prsname); MemoryContextSwitchTo(oldcontext); cfg->id = id; } else ts_error(ERROR, "No tsearch cfg with id %d", id); SPI_freeplan(plan); arg[0] = TEXTOID; sprintf(buf, "select lt.tokid, map.dict_name from %s.pg_ts_cfgmap as map, %s.pg_ts_cfg as cfg, %s.token_type( $1 ) as lt where lt.alias = map.tok_alias and map.ts_name = cfg.ts_name and cfg.oid= $2 order by lt.tokid desc;", nsp, nsp, nsp); plan = SPI_prepare(buf, 2, arg); if (!plan) ts_error(ERROR, "SPI_prepare() failed"); pars[0] = PointerGetDatum(prsname); stat = SPI_execp(plan, pars, " ", 0); if (stat < 0) ts_error(ERROR, "SPI_execp return %d", stat); if (SPI_processed <= 0) ts_error(ERROR, "No parser with id %d", id); for (i = 0; i < SPI_processed; i++) { int lexid = DatumGetInt32(SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 1, &isnull)); ArrayType *toasted_a = (ArrayType *) PointerGetDatum(SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 2, &isnull)); ArrayType *a; if (!cfg->map) { cfg->len = lexid + 1; cfg->map = (ListDictionary *) malloc(sizeof(ListDictionary) * cfg->len); if (!cfg->map) ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); memset(cfg->map, 0, sizeof(ListDictionary) * cfg->len); } if (isnull) continue; a = (ArrayType *) PointerGetDatum(PG_DETOAST_DATUM(DatumGetPointer(toasted_a))); if (ARR_NDIM(a) != 1) ts_error(ERROR, "Wrong dimension"); if (ARRNELEMS(a) < 1) continue; if (ARR_HASNULL(a)) ts_error(ERROR, "Array must not contain nulls"); cfg->map[lexid].len = ARRNELEMS(a); cfg->map[lexid].dict_id = (Datum *) malloc(sizeof(Datum) * cfg->map[lexid].len); if (!cfg->map[lexid].dict_id) ts_error(ERROR, "No memory"); memset(cfg->map[lexid].dict_id, 0, sizeof(Datum) * cfg->map[lexid].len); ptr = (text *) ARR_DATA_PTR(a); oldcontext = MemoryContextSwitchTo(TopMemoryContext); for (j = 0; j < cfg->map[lexid].len; j++) { cfg->map[lexid].dict_id[j] = PointerGetDatum(ptextdup(ptr)); ptr = NEXTVAL(ptr); } MemoryContextSwitchTo(oldcontext); if (a != toasted_a) pfree(a); } SPI_freeplan(plan); SPI_finish(); cfg->prs_id = name2id_prs(prsname); pfree(prsname); pfree(nsp); for (i = 0; i < cfg->len; i++) { for (j = 0; j < cfg->map[i].len; j++) { ptr = (text *) DatumGetPointer(cfg->map[i].dict_id[j]); cfg->map[i].dict_id[j] = ObjectIdGetDatum(name2id_dict(ptr)); pfree(ptr); } } }