2001-10-13 07:19:09 +08:00
|
|
|
/*
|
|
|
|
* In/Out definitions for txtidx type
|
|
|
|
* Internal structure:
|
|
|
|
* string of values, array of position lexem in string and it's length
|
|
|
|
* Teodor Sigaev <teodor@stack.net>
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/gist.h"
|
|
|
|
#include "access/itup.h"
|
|
|
|
#include "utils/elog.h"
|
|
|
|
#include "utils/palloc.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
#include "storage/bufpage.h"
|
|
|
|
#include "executor/spi.h"
|
|
|
|
#include "commands/trigger.h"
|
|
|
|
|
|
|
|
#include "utils/pg_locale.h"
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
#include <ctype.h> /* tolower */
|
|
|
|
#include "txtidx.h"
|
|
|
|
#include "query.h"
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
#include "deflex.h"
|
|
|
|
#include "parser.h"
|
|
|
|
|
|
|
|
#include "morph.h"
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(txtidx_in);
|
2001-10-25 13:50:21 +08:00
|
|
|
Datum txtidx_in(PG_FUNCTION_ARGS);
|
|
|
|
|
2001-10-13 07:19:09 +08:00
|
|
|
PG_FUNCTION_INFO_V1(txtidx_out);
|
2001-10-25 13:50:21 +08:00
|
|
|
Datum txtidx_out(PG_FUNCTION_ARGS);
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(txt2txtidx);
|
2001-10-25 13:50:21 +08:00
|
|
|
Datum txt2txtidx(PG_FUNCTION_ARGS);
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(tsearch);
|
2001-10-25 13:50:21 +08:00
|
|
|
Datum tsearch(PG_FUNCTION_ARGS);
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(txtidxsize);
|
2001-10-25 13:50:21 +08:00
|
|
|
Datum txtidxsize(PG_FUNCTION_ARGS);
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* in/out text index type
|
|
|
|
*/
|
|
|
|
static char *BufferStr;
|
|
|
|
static int
|
2001-10-25 13:50:21 +08:00
|
|
|
compareentry(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
if (((WordEntry *) a)->len == ((WordEntry *) b)->len)
|
|
|
|
{
|
|
|
|
return strncmp(
|
|
|
|
&BufferStr[((WordEntry *) a)->pos],
|
|
|
|
&BufferStr[((WordEntry *) b)->pos],
|
|
|
|
((WordEntry *) b)->len);
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
2001-10-25 13:50:21 +08:00
|
|
|
return (((WordEntry *) a)->len > ((WordEntry *) b)->len) ? 1 : -1;
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-10-25 13:50:21 +08:00
|
|
|
uniqueentry(WordEntry * a, int4 l, char *buf, int4 *outbuflen)
|
|
|
|
{
|
|
|
|
WordEntry *ptr,
|
|
|
|
*res;
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
res = a;
|
|
|
|
*outbuflen = res->len;
|
2001-10-25 13:50:21 +08:00
|
|
|
if (l == 1)
|
2001-10-13 07:19:09 +08:00
|
|
|
return l;
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
ptr = a + 1;
|
2001-10-13 07:19:09 +08:00
|
|
|
BufferStr = buf;
|
2001-10-25 13:50:21 +08:00
|
|
|
qsort((void *) a, l, sizeof(int4), compareentry);
|
2001-10-13 07:19:09 +08:00
|
|
|
*outbuflen = res->len;
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
while (ptr - a < l)
|
|
|
|
{
|
|
|
|
if (!(ptr->len == res->len &&
|
|
|
|
strncmp(&buf[ptr->pos], &buf[res->pos], res->len) == 0))
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
res++;
|
|
|
|
res->len = ptr->len;
|
|
|
|
res->pos = ptr->pos;
|
|
|
|
*outbuflen += res->len;
|
|
|
|
|
|
|
|
}
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
return res + 1 - a;
|
2001-10-25 13:50:21 +08:00
|
|
|
}
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
#define WAITWORD 1
|
2001-10-25 13:50:21 +08:00
|
|
|
#define WAITENDWORD 2
|
2001-10-13 07:19:09 +08:00
|
|
|
#define WAITNEXTCHAR 3
|
|
|
|
#define WAITENDCMPLX 4
|
|
|
|
|
2001-10-25 09:29:37 +08:00
|
|
|
#define RESIZEPRSBUF \
|
|
|
|
do { \
|
|
|
|
if ( state->curpos - state->word == state->len ) \
|
|
|
|
{ \
|
2001-10-25 13:50:21 +08:00
|
|
|
int4 clen = state->curpos - state->word; \
|
|
|
|
state->len *= 2; \
|
|
|
|
state->word = (char*)repalloc( (void*)state->word, state->len ); \
|
|
|
|
state->curpos = state->word + clen; \
|
|
|
|
} \
|
2001-10-25 09:29:37 +08:00
|
|
|
} while (0)
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
int4
|
2001-10-25 13:50:21 +08:00
|
|
|
gettoken_txtidx(TI_IN_STATE * state)
|
|
|
|
{
|
|
|
|
int4 oldstate = 0;
|
|
|
|
|
2001-10-13 07:19:09 +08:00
|
|
|
state->curpos = state->word;
|
|
|
|
state->state = WAITWORD;
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (state->state == WAITWORD)
|
|
|
|
{
|
|
|
|
if (*(state->prsbuf) == '\0')
|
2001-10-13 07:19:09 +08:00
|
|
|
return 0;
|
2001-10-25 13:50:21 +08:00
|
|
|
else if (*(state->prsbuf) == '\'')
|
|
|
|
state->state = WAITENDCMPLX;
|
|
|
|
else if (*(state->prsbuf) == '\\')
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
state->state = WAITNEXTCHAR;
|
|
|
|
oldstate = WAITENDWORD;
|
2001-10-25 13:50:21 +08:00
|
|
|
}
|
|
|
|
else if (state->oprisdelim && ISOPERATOR(*(state->prsbuf)))
|
2001-10-13 07:19:09 +08:00
|
|
|
elog(ERROR, "Syntax error");
|
2001-10-25 13:50:21 +08:00
|
|
|
else if (*(state->prsbuf) != ' ')
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
*(state->curpos) = *(state->prsbuf);
|
|
|
|
state->curpos++;
|
|
|
|
state->state = WAITENDWORD;
|
|
|
|
}
|
2001-10-25 13:50:21 +08:00
|
|
|
}
|
|
|
|
else if (state->state == WAITNEXTCHAR)
|
|
|
|
{
|
|
|
|
if (*(state->prsbuf) == '\0')
|
|
|
|
elog(ERROR, "There is no escaped character");
|
|
|
|
else
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
RESIZEPRSBUF;
|
|
|
|
*(state->curpos) = *(state->prsbuf);
|
|
|
|
state->curpos++;
|
|
|
|
state->state = oldstate;
|
|
|
|
}
|
2001-10-25 13:50:21 +08:00
|
|
|
}
|
|
|
|
else if (state->state == WAITENDWORD)
|
|
|
|
{
|
|
|
|
if (*(state->prsbuf) == '\\')
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
state->state = WAITNEXTCHAR;
|
|
|
|
oldstate = WAITENDWORD;
|
2001-10-25 13:50:21 +08:00
|
|
|
}
|
|
|
|
else if (*(state->prsbuf) == ' ' || *(state->prsbuf) == '\0' ||
|
|
|
|
(state->oprisdelim && ISOPERATOR(*(state->prsbuf))))
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
RESIZEPRSBUF;
|
2001-10-25 13:50:21 +08:00
|
|
|
if (state->curpos == state->word)
|
2001-10-13 07:19:09 +08:00
|
|
|
elog(ERROR, "Syntax error");
|
|
|
|
*(state->curpos) = '\0';
|
|
|
|
return 1;
|
2001-10-25 13:50:21 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
RESIZEPRSBUF;
|
|
|
|
*(state->curpos) = *(state->prsbuf);
|
|
|
|
state->curpos++;
|
|
|
|
}
|
2001-10-25 13:50:21 +08:00
|
|
|
}
|
|
|
|
else if (state->state == WAITENDCMPLX)
|
|
|
|
{
|
|
|
|
if (*(state->prsbuf) == '\'')
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
RESIZEPRSBUF;
|
|
|
|
*(state->curpos) = '\0';
|
2001-10-25 13:50:21 +08:00
|
|
|
if (state->curpos == state->word)
|
2001-10-13 07:19:09 +08:00
|
|
|
elog(ERROR, "Syntax error");
|
|
|
|
state->prsbuf++;
|
|
|
|
return 1;
|
2001-10-25 13:50:21 +08:00
|
|
|
}
|
|
|
|
else if (*(state->prsbuf) == '\\')
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
state->state = WAITNEXTCHAR;
|
|
|
|
oldstate = WAITENDCMPLX;
|
2001-10-25 13:50:21 +08:00
|
|
|
}
|
|
|
|
else if (*(state->prsbuf) == '\0')
|
|
|
|
elog(ERROR, "Syntax error");
|
|
|
|
else
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
RESIZEPRSBUF;
|
|
|
|
*(state->curpos) = *(state->prsbuf);
|
|
|
|
state->curpos++;
|
|
|
|
}
|
2001-10-25 13:50:21 +08:00
|
|
|
}
|
|
|
|
else
|
2001-10-13 07:19:09 +08:00
|
|
|
elog(ERROR, "Inner bug :(");
|
|
|
|
state->prsbuf++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2001-10-25 13:50:21 +08:00
|
|
|
txtidx_in(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2002-11-15 04:10:00 +08:00
|
|
|
char *buf = PG_GETARG_CSTRING(0);
|
2001-10-13 07:19:09 +08:00
|
|
|
TI_IN_STATE state;
|
2001-10-25 13:50:21 +08:00
|
|
|
WordEntry *arr;
|
|
|
|
int4 len = 0,
|
|
|
|
totallen = 64;
|
|
|
|
txtidx *in;
|
|
|
|
char *tmpbuf,
|
|
|
|
*cur;
|
|
|
|
int4 i,
|
|
|
|
buflen = 256;
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
state.prsbuf = buf;
|
2001-10-25 13:50:21 +08:00
|
|
|
state.len = 32;
|
|
|
|
state.word = (char *) palloc(state.len);
|
2001-10-13 07:19:09 +08:00
|
|
|
state.oprisdelim = false;
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
arr = (WordEntry *) palloc(sizeof(WordEntry) * totallen);
|
|
|
|
cur = tmpbuf = (char *) palloc(buflen);
|
|
|
|
while (gettoken_txtidx(&state))
|
|
|
|
{
|
|
|
|
if (len == totallen)
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
totallen *= 2;
|
2001-10-25 13:50:21 +08:00
|
|
|
arr = (WordEntry *) repalloc((void *) arr, sizeof(int4) * totallen);
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
2001-10-25 13:50:21 +08:00
|
|
|
while (cur - tmpbuf + state.curpos - state.word >= buflen)
|
|
|
|
{
|
|
|
|
int4 dist = cur - tmpbuf;
|
|
|
|
|
2001-10-13 07:19:09 +08:00
|
|
|
buflen *= 2;
|
2001-10-25 13:50:21 +08:00
|
|
|
tmpbuf = (char *) repalloc((void *) tmpbuf, buflen);
|
|
|
|
cur = tmpbuf + dist;
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
2001-10-25 13:50:21 +08:00
|
|
|
if (state.curpos - state.word > 0xffff)
|
|
|
|
elog(ERROR, "Word is too long");
|
2001-10-13 07:19:09 +08:00
|
|
|
arr[len].len = state.curpos - state.word;
|
2001-10-25 13:50:21 +08:00
|
|
|
if (cur - tmpbuf > 0xffff)
|
|
|
|
elog(ERROR, "Too long value");
|
2001-10-13 07:19:09 +08:00
|
|
|
arr[len].pos = cur - tmpbuf;
|
2001-10-25 13:50:21 +08:00
|
|
|
memcpy((void *) cur, (void *) state.word, arr[len].len);
|
2001-10-13 07:19:09 +08:00
|
|
|
cur += arr[len].len;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
pfree(state.word);
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
if (!len)
|
|
|
|
elog(ERROR, "Void value");
|
2001-10-13 07:19:09 +08:00
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
len = uniqueentry(arr, len, tmpbuf, &buflen);
|
|
|
|
totallen = CALCDATASIZE(len, buflen);
|
|
|
|
in = (txtidx *) palloc(totallen);
|
2001-10-13 07:19:09 +08:00
|
|
|
in->len = totallen;
|
|
|
|
in->size = len;
|
|
|
|
cur = STRPTR(in);
|
2001-10-25 13:50:21 +08:00
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
memcpy((void *) cur, (void *) &tmpbuf[arr[i].pos], arr[i].len);
|
2001-10-13 07:19:09 +08:00
|
|
|
arr[i].pos = cur - STRPTR(in);
|
|
|
|
cur += arr[i].len;
|
|
|
|
}
|
|
|
|
pfree(tmpbuf);
|
2001-10-25 13:50:21 +08:00
|
|
|
memcpy((void *) ARRPTR(in), (void *) arr, sizeof(int4) * len);
|
|
|
|
pfree(arr);
|
|
|
|
PG_RETURN_POINTER(in);
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2001-10-25 13:50:21 +08:00
|
|
|
txtidxsize(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2002-11-15 04:10:00 +08:00
|
|
|
txtidx *in = (txtidx *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
|
2001-10-25 13:50:21 +08:00
|
|
|
int4 ret = in->size;
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(in, 0);
|
|
|
|
PG_RETURN_INT32(ret);
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2001-10-25 13:50:21 +08:00
|
|
|
txtidx_out(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2002-11-15 04:10:00 +08:00
|
|
|
txtidx *out = (txtidx *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
|
2001-10-25 13:50:21 +08:00
|
|
|
char *outbuf;
|
|
|
|
int4 i,
|
|
|
|
j,
|
|
|
|
lenbuf = STRSIZE(out) + 1 /* \0 */ + out->size * 2 /* '' */ + out->size - 1 /* space */ ;
|
|
|
|
WordEntry *ptr = ARRPTR(out);
|
|
|
|
char *curin,
|
|
|
|
*curout;
|
|
|
|
|
|
|
|
curout = outbuf = (char *) palloc(lenbuf);
|
|
|
|
for (i = 0; i < out->size; i++)
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
curin = STRPTR(out) + ptr->pos;
|
2001-10-25 13:50:21 +08:00
|
|
|
if (i != 0)
|
2001-10-13 07:19:09 +08:00
|
|
|
*curout++ = ' ';
|
|
|
|
*curout++ = '\'';
|
|
|
|
j = ptr->len;
|
2001-10-25 13:50:21 +08:00
|
|
|
while (j--)
|
|
|
|
{
|
|
|
|
if (*curin == '\'')
|
|
|
|
{
|
|
|
|
int4 pos = curout - outbuf;
|
|
|
|
|
|
|
|
outbuf = (char *) repalloc((void *) outbuf, ++lenbuf);
|
2001-10-13 07:19:09 +08:00
|
|
|
curout = outbuf + pos;
|
|
|
|
*curout++ = '\\';
|
|
|
|
}
|
|
|
|
*curout++ = *curin++;
|
|
|
|
}
|
|
|
|
*curout++ = '\'';
|
|
|
|
ptr++;
|
|
|
|
}
|
2001-10-25 13:50:21 +08:00
|
|
|
outbuf[lenbuf - 1] = '\0';
|
|
|
|
PG_FREE_IF_COPY(out, 0);
|
|
|
|
PG_RETURN_POINTER(outbuf);
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint16 len;
|
|
|
|
char *word;
|
2002-09-05 04:31:48 +08:00
|
|
|
} WORD;
|
2001-10-13 07:19:09 +08:00
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
WORD *words;
|
|
|
|
int4 lenwords;
|
|
|
|
int4 curwords;
|
2001-11-06 01:46:40 +08:00
|
|
|
} PRSTEXT;
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse text to lexems
|
|
|
|
*/
|
|
|
|
static void
|
2001-10-25 13:50:21 +08:00
|
|
|
parsetext(PRSTEXT * prs, char *buf, int4 buflen)
|
|
|
|
{
|
|
|
|
int type,
|
|
|
|
lenlemm;
|
|
|
|
char *ptr,
|
|
|
|
*ptrw;
|
|
|
|
char *lemm;
|
|
|
|
|
|
|
|
start_parse_str(buf, buflen);
|
|
|
|
while ((type = tsearch_yylex()) != 0)
|
|
|
|
{
|
|
|
|
if (prs->curwords == prs->lenwords)
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
prs->lenwords *= 2;
|
2001-10-25 13:50:21 +08:00
|
|
|
prs->words = (WORD *) repalloc((void *) prs->words, prs->lenwords * sizeof(WORD));
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
2001-10-25 13:50:21 +08:00
|
|
|
if (tokenlen > 0xffff)
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
end_parse();
|
|
|
|
elog(ERROR, "Word is too long");
|
|
|
|
}
|
|
|
|
|
|
|
|
lenlemm = tokenlen;
|
2001-10-25 13:50:21 +08:00
|
|
|
lemm = lemmatize(token, &lenlemm, type);
|
|
|
|
|
|
|
|
if (!lemm)
|
2001-10-13 07:19:09 +08:00
|
|
|
continue;
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
if (lemm != token)
|
|
|
|
{
|
|
|
|
prs->words[prs->curwords].len = lenlemm;
|
|
|
|
prs->words[prs->curwords].word = lemm;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prs->words[prs->curwords].len = lenlemm;
|
|
|
|
ptrw = prs->words[prs->curwords].word = (char *) palloc(lenlemm);
|
2001-10-13 07:19:09 +08:00
|
|
|
ptr = token;
|
2001-10-25 13:50:21 +08:00
|
|
|
while (ptr - token < lenlemm)
|
|
|
|
{
|
|
|
|
*ptrw = tolower((unsigned char) *ptr);
|
|
|
|
ptr++;
|
|
|
|
ptrw++;
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
}
|
2001-10-25 13:50:21 +08:00
|
|
|
prs->curwords++;
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
end_parse();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-10-25 13:50:21 +08:00
|
|
|
compareWORD(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
if (((WORD *) a)->len == ((WORD *) b)->len)
|
|
|
|
return strncmp(
|
|
|
|
((WORD *) a)->word,
|
|
|
|
((WORD *) b)->word,
|
|
|
|
((WORD *) b)->len);
|
|
|
|
return (((WORD *) a)->len > ((WORD *) b)->len) ? 1 : -1;
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-09-05 04:31:48 +08:00
|
|
|
uniqueWORD(WORD * a, int4 l)
|
2001-10-25 13:50:21 +08:00
|
|
|
{
|
|
|
|
WORD *ptr,
|
|
|
|
*res;
|
2001-10-13 07:19:09 +08:00
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
if (l == 1)
|
2001-10-13 07:19:09 +08:00
|
|
|
return l;
|
|
|
|
|
|
|
|
res = a;
|
|
|
|
ptr = a + 1;
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
qsort((void *) a, l, sizeof(WORD), compareWORD);
|
2001-10-13 07:19:09 +08:00
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
while (ptr - a < l)
|
|
|
|
{
|
|
|
|
if (!(ptr->len == res->len &&
|
|
|
|
strncmp(ptr->word, res->word, res->len) == 0))
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
res++;
|
2001-10-25 13:50:21 +08:00
|
|
|
res->len = ptr->len;
|
2001-10-13 07:19:09 +08:00
|
|
|
res->word = ptr->word;
|
|
|
|
}
|
2001-10-25 13:50:21 +08:00
|
|
|
else
|
|
|
|
pfree(ptr->word);
|
2001-10-13 07:19:09 +08:00
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res + 1 - a;
|
2001-10-25 13:50:21 +08:00
|
|
|
}
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* make value of txtidx
|
|
|
|
*/
|
|
|
|
static txtidx *
|
2001-10-25 13:50:21 +08:00
|
|
|
makevalue(PRSTEXT * prs)
|
|
|
|
{
|
|
|
|
int4 i,
|
|
|
|
lenstr = 0,
|
|
|
|
totallen;
|
|
|
|
txtidx *in;
|
|
|
|
WordEntry *ptr;
|
|
|
|
char *str,
|
|
|
|
*cur;
|
|
|
|
|
|
|
|
prs->curwords = uniqueWORD(prs->words, prs->curwords);
|
|
|
|
for (i = 0; i < prs->curwords; i++)
|
2001-10-13 07:19:09 +08:00
|
|
|
lenstr += prs->words[i].len;
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
totallen = CALCDATASIZE(prs->curwords, lenstr);
|
|
|
|
in = (txtidx *) palloc(totallen);
|
2001-10-13 07:19:09 +08:00
|
|
|
in->len = totallen;
|
|
|
|
in->size = prs->curwords;
|
|
|
|
|
|
|
|
ptr = ARRPTR(in);
|
|
|
|
cur = str = STRPTR(in);
|
2001-10-25 13:50:21 +08:00
|
|
|
for (i = 0; i < prs->curwords; i++)
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
ptr->len = prs->words[i].len;
|
2001-10-25 13:50:21 +08:00
|
|
|
if (cur - str > 0xffff)
|
|
|
|
elog(ERROR, "Value is too big");
|
|
|
|
ptr->pos = cur - str;
|
2001-10-13 07:19:09 +08:00
|
|
|
ptr++;
|
2001-10-25 13:50:21 +08:00
|
|
|
memcpy((void *) cur, (void *) prs->words[i].word, prs->words[i].len);
|
2001-10-13 07:19:09 +08:00
|
|
|
pfree(prs->words[i].word);
|
2001-10-25 13:50:21 +08:00
|
|
|
cur += prs->words[i].len;
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
pfree(prs->words);
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2001-10-25 13:50:21 +08:00
|
|
|
txt2txtidx(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2002-11-15 04:10:00 +08:00
|
|
|
text *in = PG_GETARG_TEXT_P(0);
|
2001-10-25 13:50:21 +08:00
|
|
|
PRSTEXT prs;
|
|
|
|
txtidx *out = NULL;
|
|
|
|
|
|
|
|
prs.lenwords = 32;
|
|
|
|
prs.curwords = 0;
|
|
|
|
prs.words = (WORD *) palloc(sizeof(WORD) * prs.lenwords);
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
initmorph();
|
2001-10-25 13:50:21 +08:00
|
|
|
parsetext(&prs, VARDATA(in), VARSIZE(in) - VARHDRSZ);
|
|
|
|
PG_FREE_IF_COPY(in, 0);
|
|
|
|
|
|
|
|
if (prs.curwords)
|
|
|
|
{
|
|
|
|
out = makevalue(&prs);
|
|
|
|
PG_RETURN_POINTER(out);
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
pfree(prs.words);
|
|
|
|
PG_RETURN_NULL();
|
2001-10-25 13:50:21 +08:00
|
|
|
}
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Trigger
|
|
|
|
*/
|
|
|
|
Datum
|
2001-10-25 13:50:21 +08:00
|
|
|
tsearch(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2001-10-13 07:19:09 +08:00
|
|
|
TriggerData *trigdata;
|
|
|
|
Trigger *trigger;
|
2001-10-25 13:50:21 +08:00
|
|
|
Relation rel;
|
|
|
|
HeapTuple rettuple = NULL;
|
|
|
|
int numidxattr,
|
|
|
|
i;
|
|
|
|
PRSTEXT prs;
|
|
|
|
Datum datum = (Datum) 0;
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
if (!CALLED_AS_TRIGGER(fcinfo))
|
|
|
|
elog(ERROR, "TSearch: Not fired by trigger manager");
|
|
|
|
|
|
|
|
trigdata = (TriggerData *) fcinfo->context;
|
|
|
|
if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
|
|
|
|
elog(ERROR, "TSearch: Can't process STATEMENT events");
|
|
|
|
if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
|
|
|
|
elog(ERROR, "TSearch: Must be fired BEFORE event");
|
|
|
|
|
|
|
|
if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
|
|
|
|
rettuple = trigdata->tg_trigtuple;
|
|
|
|
else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
|
|
|
|
rettuple = trigdata->tg_newtuple;
|
2001-10-25 13:50:21 +08:00
|
|
|
else
|
2001-10-13 07:19:09 +08:00
|
|
|
elog(ERROR, "TSearch: Unknown event");
|
|
|
|
|
|
|
|
trigger = trigdata->tg_trigger;
|
|
|
|
rel = trigdata->tg_relation;
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
if (trigger->tgnargs < 2)
|
|
|
|
elog(ERROR, "TSearch: format tsearch(txtidx_field, text_field1,...)");
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
numidxattr = SPI_fnumber(rel->rd_att, trigger->tgargs[0]);
|
2002-11-15 04:10:00 +08:00
|
|
|
if (numidxattr == SPI_ERROR_NOATTRIBUTE)
|
2001-10-25 13:50:21 +08:00
|
|
|
elog(ERROR, "TSearch: Can not find txtidx_field");
|
2001-10-13 07:19:09 +08:00
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
prs.lenwords = 32;
|
|
|
|
prs.curwords = 0;
|
|
|
|
prs.words = (WORD *) palloc(sizeof(WORD) * prs.lenwords);
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
initmorph();
|
2001-10-25 13:50:21 +08:00
|
|
|
/* find all words in indexable column */
|
|
|
|
for (i = 1; i < trigger->tgnargs; i++)
|
|
|
|
{
|
2002-11-15 04:10:00 +08:00
|
|
|
int numattr;
|
2001-10-25 13:50:21 +08:00
|
|
|
Oid oidtype;
|
2002-11-15 04:10:00 +08:00
|
|
|
Datum txt_datum;
|
|
|
|
bool isnull;
|
|
|
|
text *txt;
|
2001-10-13 07:19:09 +08:00
|
|
|
|
|
|
|
numattr = SPI_fnumber(rel->rd_att, trigger->tgargs[i]);
|
2002-11-15 04:10:00 +08:00
|
|
|
if (numattr == SPI_ERROR_NOATTRIBUTE)
|
|
|
|
{
|
|
|
|
elog(WARNING, "TSearch: can not find field '%s'",
|
|
|
|
trigger->tgargs[i]);
|
|
|
|
continue;
|
|
|
|
}
|
2001-10-13 07:19:09 +08:00
|
|
|
oidtype = SPI_gettypeid(rel->rd_att, numattr);
|
2002-11-15 04:10:00 +08:00
|
|
|
/* We assume char() and varchar() are binary-equivalent to text */
|
|
|
|
if (!(oidtype == TEXTOID ||
|
|
|
|
oidtype == VARCHAROID ||
|
|
|
|
oidtype == BPCHAROID))
|
2001-10-25 13:50:21 +08:00
|
|
|
{
|
2002-11-15 04:10:00 +08:00
|
|
|
elog(WARNING, "TSearch: '%s' is not of character type",
|
|
|
|
trigger->tgargs[i]);
|
2001-10-13 07:19:09 +08:00
|
|
|
continue;
|
|
|
|
}
|
2002-11-15 04:10:00 +08:00
|
|
|
txt_datum = SPI_getbinval(rettuple, rel->rd_att, numattr, &isnull);
|
2001-10-25 13:50:21 +08:00
|
|
|
if (isnull)
|
2001-10-13 07:19:09 +08:00
|
|
|
continue;
|
2002-11-15 04:10:00 +08:00
|
|
|
txt = DatumGetTextP(txt_datum);
|
2001-10-13 07:19:09 +08:00
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
parsetext(&prs, VARDATA(txt), VARSIZE(txt) - VARHDRSZ);
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* make txtidx value */
|
2001-10-25 13:50:21 +08:00
|
|
|
if (prs.curwords)
|
|
|
|
{
|
|
|
|
datum = PointerGetDatum(makevalue(&prs));
|
|
|
|
rettuple = SPI_modifytuple(rel, rettuple, 1, &numidxattr,
|
|
|
|
&datum, NULL);
|
2001-10-13 07:19:09 +08:00
|
|
|
pfree(DatumGetPointer(datum));
|
|
|
|
}
|
2001-10-25 13:50:21 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
char nulls = 'n';
|
|
|
|
|
|
|
|
pfree(prs.words);
|
|
|
|
rettuple = SPI_modifytuple(rel, rettuple, 1, &numidxattr,
|
|
|
|
&datum, &nulls);
|
|
|
|
}
|
|
|
|
|
2001-10-13 07:19:09 +08:00
|
|
|
if (rettuple == NULL)
|
|
|
|
elog(ERROR, "TSearch: %d returned by SPI_modifytuple", SPI_result);
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
return PointerGetDatum(rettuple);
|
2001-10-13 07:19:09 +08:00
|
|
|
}
|