Skip to content

Commit fe30edb

Browse files
committed
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.
1 parent 4405e74 commit fe30edb

File tree

6 files changed

+57
-44
lines changed

6 files changed

+57
-44
lines changed

contrib/tsearch2/query.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ checkclass_str(CHKVAL * chkval, WordEntry * val, ITEM * item)
396396

397397
while (len--)
398398
{
399-
if (item->weight & (1 << ptr->weight))
399+
if (item->weight & (1 << WEP_GETWEIGHT(*ptr)))
400400
return true;
401401
ptr++;
402402
}

contrib/tsearch2/rank.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Datum get_covers(PG_FUNCTION_ARGS);
3939

4040
static float weights[] = {0.1, 0.2, 0.4, 1.0};
4141

42-
#define wpos(wep) ( w[ ((WordEntryPos*)(wep))->weight ] )
42+
#define wpos(wep) ( w[ WEP_GETWEIGHT(wep) ] )
4343

4444
#define DEF_NORM_METHOD 0
4545

@@ -113,8 +113,8 @@ find_wordentry(tsvector * t, QUERYTYPE * q, ITEM * item)
113113
}
114114

115115
static WordEntryPos POSNULL[] = {
116-
{0, 0},
117-
{0, MAXENTRYPOS - 1}
116+
0,
117+
0
118118
};
119119

120120
static float
@@ -136,6 +136,7 @@ calc_rank_and(float *w, tsvector * t, QUERYTYPE * q)
136136

137137
memset(pos, 0, sizeof(uint16 **) * q->size);
138138
*(uint16 *) POSNULL = lengthof(POSNULL) - 1;
139+
WEP_SETPOS(POSNULL[1], MAXENTRYPOS-1);
139140

140141
for (i = 0; i < q->size; i++)
141142
{
@@ -165,14 +166,14 @@ calc_rank_and(float *w, tsvector * t, QUERYTYPE * q)
165166
{
166167
for (p = 0; p < lenct; p++)
167168
{
168-
dist = Abs(post[l].pos - ct[p].pos);
169+
dist = Abs((int)WEP_GETPOS(post[l]) - (int)WEP_GETPOS(ct[p]));
169170
if (dist || (dist == 0 && (pos[i] == (uint16 *) POSNULL || pos[k] == (uint16 *) POSNULL)))
170171
{
171172
float curw;
172173

173174
if (!dist)
174175
dist = MAXENTRYPOS;
175-
curw = sqrt(wpos(&(post[l])) * wpos(&(ct[p])) * word_distance(dist));
176+
curw = sqrt(wpos(post[l]) * wpos(ct[p]) * word_distance(dist));
176177
res = (res < 0) ? curw : 1.0 - (1.0 - res) * (1.0 - curw);
177178
}
178179
}
@@ -219,9 +220,9 @@ calc_rank_or(float *w, tsvector * t, QUERYTYPE * q)
219220
for (j = 0; j < dimt; j++)
220221
{
221222
if (res < 0)
222-
res = wpos(&(post[j]));
223+
res = wpos(post[j]);
223224
else
224-
res = 1.0 - (1.0 - res) * (1.0 - wpos(&(post[j])));
225+
res = 1.0 - (1.0 - res) * (1.0 - wpos(post[j]));
225226
}
226227
}
227228
return res;
@@ -497,7 +498,7 @@ get_docrep(tsvector * txt, QUERYTYPE * query, int *doclen)
497498
for (j = 0; j < dimt; j++)
498499
{
499500
doc[cur].item = &(item[i]);
500-
doc[cur].pos = post[j].pos;
501+
doc[cur].pos = WEP_GETPOS(post[j]);
501502
cur++;
502503
}
503504
}
@@ -656,7 +657,7 @@ get_covers(PG_FUNCTION_ARGS)
656657
{
657658
dw[cur].w = STRPTR(txt) + pptr[i].pos;
658659
dw[cur].len = pptr[i].len;
659-
dw[cur].pos = posdata[j].pos;
660+
dw[cur].pos = WEP_GETPOS(posdata[j]);
660661
cur++;
661662
}
662663
len += (pptr[i].len + 1) * (int) POSDATALEN(txt, &(pptr[i]));

contrib/tsearch2/ts_stat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ check_weight(tsvector * txt, WordEntry * wptr, int8 weight)
4242

4343
while (len--)
4444
{
45-
if (weight & (1 << ptr->weight))
45+
if (weight & (1 << WEP_GETWEIGHT(*ptr)))
4646
num++;
4747
ptr++;
4848
}

contrib/tsearch2/tsvector.c

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ Datum tsvector_length(PG_FUNCTION_ARGS);
5050
static int
5151
comparePos(const void *a, const void *b)
5252
{
53-
if (((WordEntryPos *) a)->pos == ((WordEntryPos *) b)->pos)
53+
if (WEP_GETPOS(*(WordEntryPos *) a) == WEP_GETPOS(*(WordEntryPos *) b))
5454
return 1;
55-
return (((WordEntryPos *) a)->pos > ((WordEntryPos *) b)->pos) ? 1 : -1;
55+
return (WEP_GETPOS(*(WordEntryPos *) a)> WEP_GETPOS(*(WordEntryPos *) b)) ? 1 : -1;
5656
}
5757

5858
static int
@@ -70,16 +70,15 @@ uniquePos(WordEntryPos * a, int4 l)
7070
ptr = a + 1;
7171
while (ptr - a < l)
7272
{
73-
if (ptr->pos != res->pos)
73+
if (WEP_GETPOS(*ptr) != WEP_GETPOS(*res))
7474
{
7575
res++;
76-
res->pos = ptr->pos;
77-
res->weight = ptr->weight;
78-
if (res - a >= MAXNUMPOS - 1 || res->pos == MAXENTRYPOS - 1)
76+
*res = *ptr;
77+
if (res - a >= MAXNUMPOS - 1 || WEP_GETPOS(*res) == MAXENTRYPOS - 1)
7978
break;
8079
}
81-
else if (ptr->weight > res->weight)
82-
res->weight = ptr->weight;
80+
else if (WEP_GETWEIGHT(*ptr) > WEP_GETWEIGHT(*res))
81+
WEP_SETWEIGHT(*res, WEP_GETWEIGHT(*ptr));
8382
ptr++;
8483
}
8584
return res + 1 - a;
@@ -324,12 +323,12 @@ gettoken_tsvector(TI_IN_STATE * state)
324323
state->pos = (WordEntryPos *) repalloc(state->pos, sizeof(WordEntryPos) * state->alen);
325324
}
326325
(*(uint16 *) (state->pos))++;
327-
state->pos[*(uint16 *) (state->pos)].pos = LIMITPOS(atoi(state->prsbuf));
328-
if (state->pos[*(uint16 *) (state->pos)].pos == 0)
326+
WEP_SETPOS(state->pos[*(uint16 *) (state->pos)], LIMITPOS(atoi(state->prsbuf)));
327+
if (WEP_GETPOS(state->pos[*(uint16 *) (state->pos)]) == 0)
329328
ereport(ERROR,
330329
(errcode(ERRCODE_SYNTAX_ERROR),
331330
errmsg("wrong position info")));
332-
state->pos[*(uint16 *) (state->pos)].weight = 0;
331+
WEP_SETWEIGHT( state->pos[*(uint16 *) (state->pos)], 0 );
333332
state->state = WAITPOSDELIM;
334333
}
335334
else
@@ -343,35 +342,35 @@ gettoken_tsvector(TI_IN_STATE * state)
343342
state->state = INPOSINFO;
344343
else if (tolower(*(state->prsbuf)) == 'a' || *(state->prsbuf) == '*')
345344
{
346-
if (state->pos[*(uint16 *) (state->pos)].weight)
345+
if ( WEP_GETWEIGHT(state->pos[*(uint16 *) (state->pos)]) )
347346
ereport(ERROR,
348347
(errcode(ERRCODE_SYNTAX_ERROR),
349348
errmsg("syntax error")));
350-
state->pos[*(uint16 *) (state->pos)].weight = 3;
349+
WEP_SETWEIGHT( state->pos[*(uint16 *) (state->pos)], 3 );
351350
}
352351
else if (tolower(*(state->prsbuf)) == 'b')
353352
{
354-
if (state->pos[*(uint16 *) (state->pos)].weight)
353+
if ( WEP_GETWEIGHT(state->pos[*(uint16 *) (state->pos)]) )
355354
ereport(ERROR,
356355
(errcode(ERRCODE_SYNTAX_ERROR),
357356
errmsg("syntax error")));
358-
state->pos[*(uint16 *) (state->pos)].weight = 2;
357+
WEP_SETWEIGHT( state->pos[*(uint16 *) (state->pos)], 2 );
359358
}
360359
else if (tolower(*(state->prsbuf)) == 'c')
361360
{
362-
if (state->pos[*(uint16 *) (state->pos)].weight)
361+
if ( WEP_GETWEIGHT(state->pos[*(uint16 *) (state->pos)]) )
363362
ereport(ERROR,
364363
(errcode(ERRCODE_SYNTAX_ERROR),
365364
errmsg("syntax error")));
366-
state->pos[*(uint16 *) (state->pos)].weight = 1;
365+
WEP_SETWEIGHT( state->pos[*(uint16 *) (state->pos)], 1 );
367366
}
368367
else if (tolower(*(state->prsbuf)) == 'd')
369368
{
370-
if (state->pos[*(uint16 *) (state->pos)].weight)
369+
if ( WEP_GETWEIGHT(state->pos[*(uint16 *) (state->pos)]) )
371370
ereport(ERROR,
372371
(errcode(ERRCODE_SYNTAX_ERROR),
373372
errmsg("syntax error")));
374-
state->pos[*(uint16 *) (state->pos)].weight = 0;
373+
WEP_SETWEIGHT( state->pos[*(uint16 *) (state->pos)], 0 );
375374
}
376375
else if (isspace((unsigned char) *(state->prsbuf)) ||
377376
*(state->prsbuf) == '\0')
@@ -540,9 +539,9 @@ tsvector_out(PG_FUNCTION_ARGS)
540539
wptr = POSDATAPTR(out, ptr);
541540
while (pp)
542541
{
543-
sprintf(curout, "%d", wptr->pos);
542+
sprintf(curout, "%d", WEP_GETPOS(*wptr));
544543
curout = strchr(curout, '\0');
545-
switch (wptr->weight)
544+
switch (WEP_GETWEIGHT(*wptr))
546545
{
547546
case 3:
548547
*curout++ = 'A';
@@ -704,8 +703,8 @@ makevalue(PRSTEXT * prs)
704703
wptr = POSDATAPTR(in, ptr);
705704
for (j = 0; j < *(uint16 *) cur; j++)
706705
{
707-
wptr[j].weight = 0;
708-
wptr[j].pos = prs->words[i].pos.apos[j + 1];
706+
WEP_SETWEIGHT(wptr[j], 0);
707+
WEP_SETPOS(wptr[j], prs->words[i].pos.apos[j + 1]);
709708
}
710709
cur += sizeof(uint16) + prs->words[i].pos.apos[0] * sizeof(WordEntryPos);
711710
pfree(prs->words[i].pos.apos);

contrib/tsearch2/tsvector.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,25 @@ typedef struct
2323
#define MAXSTRLEN ( 1<<11 )
2424
#define MAXSTRPOS ( 1<<20 )
2525

26+
/*
27+
Equivalent to
2628
typedef struct
2729
{
28-
uint16
29-
weight:2,
30-
pos:14;
31-
} WordEntryPos;
30+
uint16
31+
weight:2,
32+
pos:14;
33+
} WordEntryPos;
34+
35+
*/
36+
37+
typedef uint16 WordEntryPos;
38+
39+
#define WEP_GETWEIGHT(x) ( (x) >> 14 )
40+
#define WEP_GETPOS(x) ( (x) & 0x3fff )
41+
42+
#define WEP_SETWEIGHT(x,v) (x) = ( (v) << 14 ) | ( (x) & 0x3fff )
43+
#define WEP_SETPOS(x,v) (x) = ( (x) & 0xc000 ) | ( (v) & 0x3fff )
44+
3245

3346
#define MAXENTRYPOS (1<<14)
3447
#define MAXNUMPOS 256

contrib/tsearch2/tsvector_op.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ setweight(PG_FUNCTION_ARGS)
106106
p = POSDATAPTR(out, entry);
107107
while (j--)
108108
{
109-
p->weight = w;
109+
WEP_SETWEIGHT(*p,w);
110110
p++;
111111
}
112112
}
@@ -144,10 +144,10 @@ add_pos(tsvector * src, WordEntry * srcptr, tsvector * dest, WordEntry * destptr
144144
*clen = 0;
145145

146146
startlen = *clen;
147-
for (i = 0; i < slen && *clen < MAXNUMPOS && (*clen == 0 || dpos[*clen - 1].pos != MAXENTRYPOS - 1); i++)
147+
for (i = 0; i < slen && *clen < MAXNUMPOS && (*clen == 0 || WEP_GETPOS(dpos[*clen - 1]) != MAXENTRYPOS - 1); i++)
148148
{
149-
dpos[*clen].weight = spos[i].weight;
150-
dpos[*clen].pos = LIMITPOS(spos[i].pos + maxpos);
149+
WEP_SETWEIGHT(dpos[*clen], WEP_GETWEIGHT(spos[i]));
150+
WEP_SETPOS(dpos[*clen], LIMITPOS(WEP_GETPOS(spos[i]) + maxpos));
151151
(*clen)++;
152152
}
153153

@@ -186,8 +186,8 @@ concat(PG_FUNCTION_ARGS)
186186
p = POSDATAPTR(in1, ptr);
187187
while (j--)
188188
{
189-
if (p->pos > maxpos)
190-
maxpos = p->pos;
189+
if (WEP_GETPOS(*p) > maxpos)
190+
maxpos = WEP_GETPOS(*p);
191191
p++;
192192
}
193193
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy