Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit13c6abf

Browse files
author
Artur Zakirov
committed
Remove deprecated @@@ and duplicated code in rum_ts_utils.c
1 parent77e00a7 commit13c6abf

File tree

2 files changed

+6
-333
lines changed

2 files changed

+6
-333
lines changed

‎rum--1.0.sql

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ CREATE OPERATOR CLASS rum_tsvector_ops
4848
FOR TYPE tsvector USING rum
4949
AS
5050
OPERATOR1 @@ (tsvector, tsquery),
51-
OPERATOR2 @@@ (tsvector, tsquery),
52-
OPERATOR3>< (tsvector, tsquery) FORORDER BYpg_catalog.float_ops,
51+
OPERATOR2>< (tsvector, tsquery) FORORDER BYpg_catalog.float_ops,
5352
FUNCTION1 gin_cmp_tslexeme(text,text),
5453
FUNCTION2 rum_extract_tsvector(tsvector,internal,internal,internal,internal),
5554
FUNCTION3 rum_extract_tsquery(tsvector,internal,smallint,internal,internal,internal,internal),

‎rum_ts_utils.c

Lines changed: 5 additions & 331 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include"postgres.h"
1313

1414
#include"catalog/pg_type.h"
15+
#include"tsearch/ts_type.h"
1516
#include"tsearch/ts_utils.h"
1617
#include"utils/array.h"
1718
#include"utils/builtins.h"
@@ -27,23 +28,11 @@ PG_FUNCTION_INFO_V1(rum_tsquery_pre_consistent);
2728
PG_FUNCTION_INFO_V1(rum_tsquery_distance);
2829
PG_FUNCTION_INFO_V1(rum_ts_distance);
2930

30-
#defineRANK_NO_NORM0x00
31-
#defineRANK_NORM_LOGLENGTH0x01
32-
#defineRANK_NORM_LENGTH0x02
33-
#defineRANK_NORM_EXTDIST0x04
34-
#defineRANK_NORM_UNIQ0x08
35-
#defineRANK_NORM_LOGUNIQ0x10
36-
#defineRANK_NORM_RDIVRPLUS10x20
37-
#defineDEF_NORM_METHODRANK_NO_NORM
38-
3931
staticfloatcalc_rank_pos_and(float*w,Datum*addInfo,bool*addInfoIsNull,
4032
intsize);
4133
staticfloatcalc_rank_pos_or(float*w,Datum*addInfo,bool*addInfoIsNull,
4234
intsize);
4335

44-
staticfloatcalc_rank_or(constfloat*w,TSVectort,TSQueryq);
45-
staticfloatcalc_rank_and(constfloat*w,TSVectort,TSQueryq);
46-
4736
typedefstruct
4837
{
4938
QueryItem*first_item;
@@ -127,82 +116,10 @@ word_distance(int32 w)
127116
return1.0 / (1.005+0.05*exp(((float4)w) /1.5-2));
128117
}
129118

130-
staticint
131-
cnt_length(TSVectort)
132-
{
133-
WordEntry*ptr=ARRPTR(t),
134-
*end= (WordEntry*)STRPTR(t);
135-
intlen=0;
136-
137-
while (ptr<end)
138-
{
139-
intclen=POSDATALEN(t,ptr);
140-
141-
if (clen==0)
142-
len+=1;
143-
else
144-
len+=clen;
145-
146-
ptr++;
147-
}
148-
149-
returnlen;
150-
}
151-
152119
#defineWordECompareQueryItem(e,q,p,i,m) \
153120
tsCompareString((q) + (i)->distance, (i)->length,\
154121
(e) + (p)->pos, (p)->len, (m))
155122

156-
/*
157-
* Returns a pointer to a WordEntry's array corresponding to 'item' from
158-
* tsvector 't'. 'q' is the TSQuery containing 'item'.
159-
* Returns NULL if not found.
160-
*/
161-
staticWordEntry*
162-
find_wordentry(TSVectort,TSQueryq,QueryOperand*item,int32*nitem)
163-
{
164-
WordEntry*StopLow=ARRPTR(t);
165-
WordEntry*StopHigh= (WordEntry*)STRPTR(t);
166-
WordEntry*StopMiddle=StopHigh;
167-
intdifference;
168-
169-
*nitem=0;
170-
171-
/* Loop invariant: StopLow <= item < StopHigh */
172-
while (StopLow<StopHigh)
173-
{
174-
StopMiddle=StopLow+ (StopHigh-StopLow) /2;
175-
difference=WordECompareQueryItem(STRPTR(t),GETOPERAND(q),StopMiddle,item, false);
176-
if (difference==0)
177-
{
178-
StopHigh=StopMiddle;
179-
*nitem=1;
180-
break;
181-
}
182-
elseif (difference>0)
183-
StopLow=StopMiddle+1;
184-
else
185-
StopHigh=StopMiddle;
186-
}
187-
188-
if (item->prefix)
189-
{
190-
if (StopLow >=StopHigh)
191-
StopMiddle=StopHigh;
192-
193-
*nitem=0;
194-
195-
while (StopMiddle< (WordEntry*)STRPTR(t)&&
196-
WordECompareQueryItem(STRPTR(t),GETOPERAND(q),StopMiddle,item, true)==0)
197-
{
198-
(*nitem)++;
199-
StopMiddle++;
200-
}
201-
}
202-
203-
return (*nitem>0) ?StopHigh :NULL;
204-
}
205-
206123
staticint
207124
compress_pos(char*target,uint16*pos,intnpos)
208125
{
@@ -487,206 +404,6 @@ SortAndUniqItems(TSQuery q, int *size)
487404
returnres;
488405
}
489406

490-
staticfloat
491-
calc_rank_and(constfloat*w,TSVectort,TSQueryq)
492-
{
493-
WordEntryPosVector**pos;
494-
WordEntryPosVector1posnull;
495-
WordEntryPosVector*POSNULL;
496-
inti,
497-
k,
498-
l,
499-
p;
500-
WordEntry*entry,
501-
*firstentry;
502-
WordEntryPos*post,
503-
*ct;
504-
int32dimt,
505-
lenct,
506-
dist,
507-
nitem;
508-
floatres=-1.0;
509-
QueryOperand**item;
510-
intsize=q->size;
511-
512-
item=SortAndUniqItems(q,&size);
513-
if (size<2)
514-
{
515-
pfree(item);
516-
returncalc_rank_or(w,t,q);
517-
}
518-
pos= (WordEntryPosVector**)palloc0(sizeof(WordEntryPosVector*)*q->size);
519-
520-
/* A dummy WordEntryPos array to use when haspos is false */
521-
posnull.npos=1;
522-
posnull.pos[0]=0;
523-
WEP_SETPOS(posnull.pos[0],MAXENTRYPOS-1);
524-
POSNULL= (WordEntryPosVector*)&posnull;
525-
526-
for (i=0;i<size;i++)
527-
{
528-
firstentry=entry=find_wordentry(t,q,item[i],&nitem);
529-
if (!entry)
530-
continue;
531-
532-
while (entry-firstentry<nitem)
533-
{
534-
if (entry->haspos)
535-
pos[i]=_POSVECPTR(t,entry);
536-
else
537-
pos[i]=POSNULL;
538-
539-
dimt=pos[i]->npos;
540-
post=pos[i]->pos;
541-
for (k=0;k<i;k++)
542-
{
543-
if (!pos[k])
544-
continue;
545-
lenct=pos[k]->npos;
546-
ct=pos[k]->pos;
547-
for (l=0;l<dimt;l++)
548-
{
549-
for (p=0;p<lenct;p++)
550-
{
551-
dist=Abs((int)WEP_GETPOS(post[l])- (int)WEP_GETPOS(ct[p]));
552-
if (dist|| (dist==0&& (pos[i]==POSNULL||pos[k]==POSNULL)))
553-
{
554-
floatcurw;
555-
556-
if (!dist)
557-
dist=MAXENTRYPOS;
558-
curw=sqrt(wpos(post[l])*wpos(ct[p])*word_distance(dist));
559-
res= (res<0) ?curw :1.0- (1.0-res)* (1.0-curw);
560-
}
561-
}
562-
}
563-
}
564-
565-
entry++;
566-
}
567-
}
568-
pfree(pos);
569-
pfree(item);
570-
returnres;
571-
}
572-
573-
staticfloat
574-
calc_rank_or(constfloat*w,TSVectort,TSQueryq)
575-
{
576-
WordEntry*entry,
577-
*firstentry;
578-
WordEntryPosVector1posnull;
579-
WordEntryPos*post;
580-
int32dimt,
581-
j,
582-
i,
583-
nitem;
584-
floatres=0.0;
585-
QueryOperand**item;
586-
intsize=q->size;
587-
588-
/* A dummy WordEntryPos array to use when haspos is false */
589-
posnull.npos=1;
590-
posnull.pos[0]=0;
591-
592-
item=SortAndUniqItems(q,&size);
593-
594-
for (i=0;i<size;i++)
595-
{
596-
floatresj,
597-
wjm;
598-
int32jm;
599-
600-
firstentry=entry=find_wordentry(t,q,item[i],&nitem);
601-
if (!entry)
602-
continue;
603-
604-
while (entry-firstentry<nitem)
605-
{
606-
if (entry->haspos)
607-
{
608-
dimt=POSDATALEN(t,entry);
609-
post=POSDATAPTR(t,entry);
610-
}
611-
else
612-
{
613-
dimt=posnull.npos;
614-
post=posnull.pos;
615-
}
616-
617-
resj=0.0;
618-
wjm=-1.0;
619-
jm=0;
620-
for (j=0;j<dimt;j++)
621-
{
622-
resj=resj+wpos(post[j]) / ((j+1)* (j+1));
623-
if (wpos(post[j])>wjm)
624-
{
625-
wjm=wpos(post[j]);
626-
jm=j;
627-
}
628-
}
629-
/*
630-
limit (sum(i/i^2),i->inf) = pi^2/6
631-
resj = sum(wi/i^2),i=1,noccurence,
632-
wi - should be sorted desc,
633-
don't sort for now, just choose maximum weight. This should be corrected
634-
Oleg Bartunov
635-
*/
636-
res=res+ (wjm+resj-wjm / ((jm+1)* (jm+1))) /1.64493406685;
637-
638-
entry++;
639-
}
640-
}
641-
if (size>0)
642-
res=res /size;
643-
pfree(item);
644-
returnres;
645-
}
646-
647-
staticfloat
648-
calc_rank(constfloat*w,TSVectort,TSQueryq,int32method)
649-
{
650-
QueryItem*item=GETQUERY(q);
651-
floatres=0.0;
652-
intlen;
653-
654-
if (!t->size|| !q->size)
655-
return0.0;
656-
657-
/* XXX: What about NOT? */
658-
res= (item->type==QI_OPR&& (item->qoperator.oper==OP_AND||
659-
item->qoperator.oper==OP_PHRASE)) ?
660-
calc_rank_and(w,t,q) :
661-
calc_rank_or(w,t,q);
662-
663-
if (res<0)
664-
res=1e-20f;
665-
666-
if ((method&RANK_NORM_LOGLENGTH)&&t->size>0)
667-
res /=log((double) (cnt_length(t)+1)) /log(2.0);
668-
669-
if (method&RANK_NORM_LENGTH)
670-
{
671-
len=cnt_length(t);
672-
if (len>0)
673-
res /= (float)len;
674-
}
675-
676-
/* RANK_NORM_EXTDIST not applicable */
677-
678-
if ((method&RANK_NORM_UNIQ)&&t->size>0)
679-
res /= (float) (t->size);
680-
681-
if ((method&RANK_NORM_LOGUNIQ)&&t->size>0)
682-
res /=log((double) (t->size+1)) /log(2.0);
683-
684-
if (method&RANK_NORM_RDIVRPLUS1)
685-
res /= (res+1);
686-
687-
returnres;
688-
}
689-
690407
Datum
691408
rum_extract_tsvector(PG_FUNCTION_ARGS)
692409
{
@@ -740,44 +457,6 @@ rum_extract_tsvector(PG_FUNCTION_ARGS)
740457
PG_RETURN_POINTER(entries);
741458
}
742459

743-
staticconstfloat*
744-
getWeights(ArrayType*win)
745-
{
746-
staticfloatws[lengthof(weights)];
747-
inti;
748-
float4*arrdata;
749-
750-
if (win==NULL)
751-
returnweights;
752-
753-
if (ARR_NDIM(win)!=1)
754-
ereport(ERROR,
755-
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
756-
errmsg("array of weight must be one-dimensional")));
757-
758-
if (ArrayGetNItems(ARR_NDIM(win),ARR_DIMS(win))<lengthof(weights))
759-
ereport(ERROR,
760-
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
761-
errmsg("array of weight is too short")));
762-
763-
if (array_contains_nulls(win))
764-
ereport(ERROR,
765-
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
766-
errmsg("array of weight must not contain nulls")));
767-
768-
arrdata= (float4*)ARR_DATA_PTR(win);
769-
for (i=0;i<lengthof(weights);i++)
770-
{
771-
ws[i]= (arrdata[i] >=0) ?arrdata[i] :weights[i];
772-
if (ws[i]>1.0)
773-
ereport(ERROR,
774-
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
775-
errmsg("weight out of range")));
776-
}
777-
778-
returnws;
779-
}
780-
781460
Datum
782461
rum_extract_tsquery(PG_FUNCTION_ARGS)
783462
{
@@ -893,15 +572,10 @@ rum_tsquery_distance(PG_FUNCTION_ARGS)
893572
Datum
894573
rum_ts_distance(PG_FUNCTION_ARGS)
895574
{
896-
TSVectortxt=PG_GETARG_TSVECTOR(0);
897-
TSQueryquery=PG_GETARG_TSQUERY(1);
898-
floatres;
899-
900-
res=1.0 /calc_rank(getWeights(NULL),txt,query,DEF_NORM_METHOD);
901-
902-
PG_FREE_IF_COPY(txt,0);
903-
PG_FREE_IF_COPY(query,1);
904-
PG_RETURN_FLOAT4(res);
575+
returnDirectFunctionCall2Coll(ts_rank_tt,
576+
PG_GET_COLLATION(),
577+
PG_GETARG_DATUM(0),
578+
PG_GETARG_DATUM(1));
905579
}
906580

907581
Datum

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp