From fe30edbab8b01123c503760d2804f6764cd27a51 Mon Sep 17 00:00:00 2001 From: Teodor Sigaev Date: Tue, 25 Jan 2005 12:36:25 +0000 Subject: [PATCH] Change typedef struct {} WordEntryPos; to typedef uint16 WordEntryPos according to http://www.pgsql.ru/db/mw/msg.html?mid=2035188 Require re-fill all tsvector fields and reindex tsvector indexes. --- contrib/tsearch2/query.c | 2 +- contrib/tsearch2/rank.c | 19 +++++++------- contrib/tsearch2/ts_stat.c | 2 +- contrib/tsearch2/tsvector.c | 45 +++++++++++++++++----------------- contrib/tsearch2/tsvector.h | 21 +++++++++++++--- contrib/tsearch2/tsvector_op.c | 12 ++++----- 6 files changed, 57 insertions(+), 44 deletions(-) diff --git a/contrib/tsearch2/query.c b/contrib/tsearch2/query.c index 28d1b1dc56d..6787b63ae86 100644 --- a/contrib/tsearch2/query.c +++ b/contrib/tsearch2/query.c @@ -396,7 +396,7 @@ checkclass_str(CHKVAL * chkval, WordEntry * val, ITEM * item) while (len--) { - if (item->weight & (1 << ptr->weight)) + if (item->weight & (1 << WEP_GETWEIGHT(*ptr))) return true; ptr++; } diff --git a/contrib/tsearch2/rank.c b/contrib/tsearch2/rank.c index 3845ddf4921..fa1dbc8bb23 100644 --- a/contrib/tsearch2/rank.c +++ b/contrib/tsearch2/rank.c @@ -39,7 +39,7 @@ Datum get_covers(PG_FUNCTION_ARGS); static float weights[] = {0.1, 0.2, 0.4, 1.0}; -#define wpos(wep) ( w[ ((WordEntryPos*)(wep))->weight ] ) +#define wpos(wep) ( w[ WEP_GETWEIGHT(wep) ] ) #define DEF_NORM_METHOD 0 @@ -113,8 +113,8 @@ find_wordentry(tsvector * t, QUERYTYPE * q, ITEM * item) } static WordEntryPos POSNULL[] = { - {0, 0}, - {0, MAXENTRYPOS - 1} + 0, + 0 }; static float @@ -136,6 +136,7 @@ calc_rank_and(float *w, tsvector * t, QUERYTYPE * q) memset(pos, 0, sizeof(uint16 **) * q->size); *(uint16 *) POSNULL = lengthof(POSNULL) - 1; + WEP_SETPOS(POSNULL[1], MAXENTRYPOS-1); for (i = 0; i < q->size; i++) { @@ -165,14 +166,14 @@ calc_rank_and(float *w, tsvector * t, QUERYTYPE * q) { for (p = 0; p < lenct; p++) { - dist = Abs(post[l].pos - ct[p].pos); + dist = Abs((int)WEP_GETPOS(post[l]) - (int)WEP_GETPOS(ct[p])); if (dist || (dist == 0 && (pos[i] == (uint16 *) POSNULL || pos[k] == (uint16 *) POSNULL))) { float curw; if (!dist) dist = MAXENTRYPOS; - curw = sqrt(wpos(&(post[l])) * wpos(&(ct[p])) * word_distance(dist)); + curw = sqrt(wpos(post[l]) * wpos(ct[p]) * word_distance(dist)); res = (res < 0) ? curw : 1.0 - (1.0 - res) * (1.0 - curw); } } @@ -219,9 +220,9 @@ calc_rank_or(float *w, tsvector * t, QUERYTYPE * q) for (j = 0; j < dimt; j++) { if (res < 0) - res = wpos(&(post[j])); + res = wpos(post[j]); else - res = 1.0 - (1.0 - res) * (1.0 - wpos(&(post[j]))); + res = 1.0 - (1.0 - res) * (1.0 - wpos(post[j])); } } return res; @@ -497,7 +498,7 @@ get_docrep(tsvector * txt, QUERYTYPE * query, int *doclen) for (j = 0; j < dimt; j++) { doc[cur].item = &(item[i]); - doc[cur].pos = post[j].pos; + doc[cur].pos = WEP_GETPOS(post[j]); cur++; } } @@ -656,7 +657,7 @@ get_covers(PG_FUNCTION_ARGS) { dw[cur].w = STRPTR(txt) + pptr[i].pos; dw[cur].len = pptr[i].len; - dw[cur].pos = posdata[j].pos; + dw[cur].pos = WEP_GETPOS(posdata[j]); cur++; } len += (pptr[i].len + 1) * (int) POSDATALEN(txt, &(pptr[i])); diff --git a/contrib/tsearch2/ts_stat.c b/contrib/tsearch2/ts_stat.c index badf1d2b01e..bb54c37a700 100644 --- a/contrib/tsearch2/ts_stat.c +++ b/contrib/tsearch2/ts_stat.c @@ -42,7 +42,7 @@ check_weight(tsvector * txt, WordEntry * wptr, int8 weight) while (len--) { - if (weight & (1 << ptr->weight)) + if (weight & (1 << WEP_GETWEIGHT(*ptr))) num++; ptr++; } diff --git a/contrib/tsearch2/tsvector.c b/contrib/tsearch2/tsvector.c index a8e1dbfe738..2856a16aba8 100644 --- a/contrib/tsearch2/tsvector.c +++ b/contrib/tsearch2/tsvector.c @@ -50,9 +50,9 @@ Datum tsvector_length(PG_FUNCTION_ARGS); static int comparePos(const void *a, const void *b) { - if (((WordEntryPos *) a)->pos == ((WordEntryPos *) b)->pos) + if (WEP_GETPOS(*(WordEntryPos *) a) == WEP_GETPOS(*(WordEntryPos *) b)) return 1; - return (((WordEntryPos *) a)->pos > ((WordEntryPos *) b)->pos) ? 1 : -1; + return (WEP_GETPOS(*(WordEntryPos *) a)> WEP_GETPOS(*(WordEntryPos *) b)) ? 1 : -1; } static int @@ -70,16 +70,15 @@ uniquePos(WordEntryPos * a, int4 l) ptr = a + 1; while (ptr - a < l) { - if (ptr->pos != res->pos) + if (WEP_GETPOS(*ptr) != WEP_GETPOS(*res)) { res++; - res->pos = ptr->pos; - res->weight = ptr->weight; - if (res - a >= MAXNUMPOS - 1 || res->pos == MAXENTRYPOS - 1) + *res = *ptr; + if (res - a >= MAXNUMPOS - 1 || WEP_GETPOS(*res) == MAXENTRYPOS - 1) break; } - else if (ptr->weight > res->weight) - res->weight = ptr->weight; + else if (WEP_GETWEIGHT(*ptr) > WEP_GETWEIGHT(*res)) + WEP_SETWEIGHT(*res, WEP_GETWEIGHT(*ptr)); ptr++; } return res + 1 - a; @@ -324,12 +323,12 @@ gettoken_tsvector(TI_IN_STATE * state) state->pos = (WordEntryPos *) repalloc(state->pos, sizeof(WordEntryPos) * state->alen); } (*(uint16 *) (state->pos))++; - state->pos[*(uint16 *) (state->pos)].pos = LIMITPOS(atoi(state->prsbuf)); - if (state->pos[*(uint16 *) (state->pos)].pos == 0) + WEP_SETPOS(state->pos[*(uint16 *) (state->pos)], LIMITPOS(atoi(state->prsbuf))); + if (WEP_GETPOS(state->pos[*(uint16 *) (state->pos)]) == 0) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("wrong position info"))); - state->pos[*(uint16 *) (state->pos)].weight = 0; + WEP_SETWEIGHT( state->pos[*(uint16 *) (state->pos)], 0 ); state->state = WAITPOSDELIM; } else @@ -343,35 +342,35 @@ gettoken_tsvector(TI_IN_STATE * state) state->state = INPOSINFO; else if (tolower(*(state->prsbuf)) == 'a' || *(state->prsbuf) == '*') { - if (state->pos[*(uint16 *) (state->pos)].weight) + if ( WEP_GETWEIGHT(state->pos[*(uint16 *) (state->pos)]) ) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("syntax error"))); - state->pos[*(uint16 *) (state->pos)].weight = 3; + WEP_SETWEIGHT( state->pos[*(uint16 *) (state->pos)], 3 ); } else if (tolower(*(state->prsbuf)) == 'b') { - if (state->pos[*(uint16 *) (state->pos)].weight) + if ( WEP_GETWEIGHT(state->pos[*(uint16 *) (state->pos)]) ) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("syntax error"))); - state->pos[*(uint16 *) (state->pos)].weight = 2; + WEP_SETWEIGHT( state->pos[*(uint16 *) (state->pos)], 2 ); } else if (tolower(*(state->prsbuf)) == 'c') { - if (state->pos[*(uint16 *) (state->pos)].weight) + if ( WEP_GETWEIGHT(state->pos[*(uint16 *) (state->pos)]) ) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("syntax error"))); - state->pos[*(uint16 *) (state->pos)].weight = 1; + WEP_SETWEIGHT( state->pos[*(uint16 *) (state->pos)], 1 ); } else if (tolower(*(state->prsbuf)) == 'd') { - if (state->pos[*(uint16 *) (state->pos)].weight) + if ( WEP_GETWEIGHT(state->pos[*(uint16 *) (state->pos)]) ) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("syntax error"))); - state->pos[*(uint16 *) (state->pos)].weight = 0; + WEP_SETWEIGHT( state->pos[*(uint16 *) (state->pos)], 0 ); } else if (isspace((unsigned char) *(state->prsbuf)) || *(state->prsbuf) == '\0') @@ -540,9 +539,9 @@ tsvector_out(PG_FUNCTION_ARGS) wptr = POSDATAPTR(out, ptr); while (pp) { - sprintf(curout, "%d", wptr->pos); + sprintf(curout, "%d", WEP_GETPOS(*wptr)); curout = strchr(curout, '\0'); - switch (wptr->weight) + switch (WEP_GETWEIGHT(*wptr)) { case 3: *curout++ = 'A'; @@ -704,8 +703,8 @@ makevalue(PRSTEXT * prs) wptr = POSDATAPTR(in, ptr); for (j = 0; j < *(uint16 *) cur; j++) { - wptr[j].weight = 0; - wptr[j].pos = prs->words[i].pos.apos[j + 1]; + WEP_SETWEIGHT(wptr[j], 0); + WEP_SETPOS(wptr[j], prs->words[i].pos.apos[j + 1]); } cur += sizeof(uint16) + prs->words[i].pos.apos[0] * sizeof(WordEntryPos); pfree(prs->words[i].pos.apos); diff --git a/contrib/tsearch2/tsvector.h b/contrib/tsearch2/tsvector.h index bb695247463..358ec570b69 100644 --- a/contrib/tsearch2/tsvector.h +++ b/contrib/tsearch2/tsvector.h @@ -23,12 +23,25 @@ typedef struct #define MAXSTRLEN ( 1<<11 ) #define MAXSTRPOS ( 1<<20 ) +/* +Equivalent to typedef struct { - uint16 - weight:2, - pos:14; -} WordEntryPos; + uint16 + weight:2, + pos:14; +} WordEntryPos; + +*/ + +typedef uint16 WordEntryPos; + +#define WEP_GETWEIGHT(x) ( (x) >> 14 ) +#define WEP_GETPOS(x) ( (x) & 0x3fff ) + +#define WEP_SETWEIGHT(x,v) (x) = ( (v) << 14 ) | ( (x) & 0x3fff ) +#define WEP_SETPOS(x,v) (x) = ( (x) & 0xc000 ) | ( (v) & 0x3fff ) + #define MAXENTRYPOS (1<<14) #define MAXNUMPOS 256 diff --git a/contrib/tsearch2/tsvector_op.c b/contrib/tsearch2/tsvector_op.c index 2ffd4ca5848..7f1a33c9431 100644 --- a/contrib/tsearch2/tsvector_op.c +++ b/contrib/tsearch2/tsvector_op.c @@ -106,7 +106,7 @@ setweight(PG_FUNCTION_ARGS) p = POSDATAPTR(out, entry); while (j--) { - p->weight = w; + WEP_SETWEIGHT(*p,w); p++; } } @@ -144,10 +144,10 @@ add_pos(tsvector * src, WordEntry * srcptr, tsvector * dest, WordEntry * destptr *clen = 0; startlen = *clen; - for (i = 0; i < slen && *clen < MAXNUMPOS && (*clen == 0 || dpos[*clen - 1].pos != MAXENTRYPOS - 1); i++) + for (i = 0; i < slen && *clen < MAXNUMPOS && (*clen == 0 || WEP_GETPOS(dpos[*clen - 1]) != MAXENTRYPOS - 1); i++) { - dpos[*clen].weight = spos[i].weight; - dpos[*clen].pos = LIMITPOS(spos[i].pos + maxpos); + WEP_SETWEIGHT(dpos[*clen], WEP_GETWEIGHT(spos[i])); + WEP_SETPOS(dpos[*clen], LIMITPOS(WEP_GETPOS(spos[i]) + maxpos)); (*clen)++; } @@ -186,8 +186,8 @@ concat(PG_FUNCTION_ARGS) p = POSDATAPTR(in1, ptr); while (j--) { - if (p->pos > maxpos) - maxpos = p->pos; + if (WEP_GETPOS(*p) > maxpos) + maxpos = WEP_GETPOS(*p); p++; } }