77 *
88 *
99 * IDENTIFICATION
10- * $PostgreSQL: pgsql/src/backend/utils/adt/tsrank.c,v 1.5 2007/09/1108:46:29 teodor Exp $
10+ * $PostgreSQL: pgsql/src/backend/utils/adt/tsrank.c,v 1.6 2007/09/1116:01:40 teodor Exp $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
@@ -476,25 +476,20 @@ compareDocR(const void *va, const void *vb)
476476return (a -> pos > b -> pos ) ?1 :-1 ;
477477}
478478
479- static bool
480- checkcondition_QueryOperand (void * checkval ,QueryOperand * val )
479+ typedef struct
481480{
482- return (bool ) (val -> istrue );
483- }
481+ TSQuery query ;
482+ bool * operandexist ;
483+ }QueryRepresentation ;
484484
485- static void
486- reset_istrue_flag (TSQuery query )
487- {
488- QueryItem * item = GETQUERY (query );
489- int i ;
485+ #define QR_GET_OPERAND_EXISTS (q ,v ) ( (q)->operandexist[ ((QueryItem*)(v)) - GETQUERY((q)->query) ] )
486+ #define QR_SET_OPERAND_EXISTS (q ,v ) QR_GET_OPERAND_EXISTS(q,v) = true
490487
491- /* reset istrue flag */
492- for (i = 0 ;i < query -> size ;i ++ )
493- {
494- if (item -> type == QI_VAL )
495- item -> operand .istrue = 0 ;
496- item ++ ;
497- }
488+ static bool
489+ checkcondition_QueryOperand (void * checkval ,QueryOperand * val )
490+ {
491+ QueryRepresentation * qr = (QueryRepresentation * )checkval ;
492+ return QR_GET_OPERAND_EXISTS (qr ,val );
498493}
499494
500495typedef struct
@@ -508,7 +503,7 @@ typedef struct
508503
509504
510505static bool
511- Cover (DocRepresentation * doc ,int len ,TSQuery query ,Extention * ext )
506+ Cover (DocRepresentation * doc ,int len ,QueryRepresentation * qr ,Extention * ext )
512507{
513508DocRepresentation * ptr ;
514509int lastpos = ext -> pos ;
@@ -519,7 +514,7 @@ Cover(DocRepresentation *doc, int len, TSQuery query, Extention *ext)
519514 * (though any decent compiler will optimize away the tail-recursion. */
520515check_stack_depth ();
521516
522- reset_istrue_flag ( query );
517+ memset ( qr -> operandexist , 0 , sizeof ( bool ) * qr -> query -> size );
523518
524519ext -> p = 0x7fffffff ;
525520ext -> q = 0 ;
@@ -531,9 +526,9 @@ Cover(DocRepresentation *doc, int len, TSQuery query, Extention *ext)
531526for (i = 0 ;i < ptr -> nitem ;i ++ )
532527{
533528if (ptr -> item [i ]-> type == QI_VAL )
534- ptr -> item [i ]-> operand . istrue = 1 ;
529+ QR_SET_OPERAND_EXISTS ( qr , ptr -> item [i ]) ;
535530}
536- if (TS_execute (GETQUERY (query ),NULL , false,checkcondition_QueryOperand ))
531+ if (TS_execute (GETQUERY (qr -> query ),( void * ) qr , false,checkcondition_QueryOperand ))
537532{
538533if (ptr -> pos > ext -> q )
539534{
@@ -550,7 +545,7 @@ Cover(DocRepresentation *doc, int len, TSQuery query, Extention *ext)
550545if (!found )
551546return false;
552547
553- reset_istrue_flag ( query );
548+ memset ( qr -> operandexist , 0 , sizeof ( bool ) * qr -> query -> size );
554549
555550ptr = doc + lastpos ;
556551
@@ -559,8 +554,8 @@ Cover(DocRepresentation *doc, int len, TSQuery query, Extention *ext)
559554{
560555for (i = 0 ;i < ptr -> nitem ;i ++ )
561556if (ptr -> item [i ]-> type == QI_VAL )
562- ptr -> item [i ]-> operand . istrue = 1 ;
563- if (TS_execute (GETQUERY (query ),NULL , true,checkcondition_QueryOperand ))
557+ QR_SET_OPERAND_EXISTS ( qr , ptr -> item [i ]) ;
558+ if (TS_execute (GETQUERY (qr -> query ),( void * ) qr , true,checkcondition_QueryOperand ))
564559{
565560if (ptr -> pos < ext -> p )
566561{
@@ -583,28 +578,27 @@ Cover(DocRepresentation *doc, int len, TSQuery query, Extention *ext)
583578}
584579
585580ext -> pos ++ ;
586- return Cover (doc ,len ,query ,ext );
581+ return Cover (doc ,len ,qr ,ext );
587582}
588583
589584static DocRepresentation *
590- get_docrep (TSVector txt ,TSQuery query ,int * doclen )
585+ get_docrep (TSVector txt ,QueryRepresentation * qr ,int * doclen )
591586{
592- QueryItem * item = GETQUERY (query );
587+ QueryItem * item = GETQUERY (qr -> query );
593588WordEntry * entry ;
594589WordEntryPos * post ;
595590int4 dimt ,
596591j ,
597592i ;
598- int len = query -> size * 4 ,
593+ int len = qr -> query -> size * 4 ,
599594cur = 0 ;
600595DocRepresentation * doc ;
601596char * operand ;
602597
603598doc = (DocRepresentation * )palloc (sizeof (DocRepresentation )* len );
604- operand = GETOPERAND (query );
605- reset_istrue_flag (query );
599+ operand = GETOPERAND (qr -> query );
606600
607- for (i = 0 ;i < query -> size ;i ++ )
601+ for (i = 0 ;i < qr -> query -> size ;i ++ )
608602{
609603QueryOperand * curoperand ;
610604
@@ -613,10 +607,10 @@ get_docrep(TSVector txt, TSQuery query, int *doclen)
613607
614608curoperand = & item [i ].operand ;
615609
616- if (item [i ]. operand . istrue )
610+ if (QR_GET_OPERAND_EXISTS ( qr , & item [i ]) )
617611continue ;
618612
619- entry = find_wordentry (txt ,query ,curoperand );
613+ entry = find_wordentry (txt ,qr -> query ,curoperand );
620614if (!entry )
621615continue ;
622616
@@ -644,9 +638,9 @@ get_docrep(TSVector txt, TSQuery query, int *doclen)
644638int k ;
645639
646640doc [cur ].nitem = 0 ;
647- doc [cur ].item = (QueryItem * * )palloc (sizeof (QueryItem * )* query -> size );
641+ doc [cur ].item = (QueryItem * * )palloc (sizeof (QueryItem * )* qr -> query -> size );
648642
649- for (k = 0 ;k < query -> size ;k ++ )
643+ for (k = 0 ;k < qr -> query -> size ;k ++ )
650644{
651645QueryOperand * kptr = & item [k ].operand ;
652646QueryOperand * iptr = & item [i ].operand ;
@@ -658,7 +652,7 @@ get_docrep(TSVector txt, TSQuery query, int *doclen)
658652/* if k == i, we've already checked above that it's type == Q_VAL */
659653doc [cur ].item [doc [cur ].nitem ]= item + k ;
660654doc [cur ].nitem ++ ;
661- item [ k ]. operand . istrue = 1 ;
655+ QR_SET_OPERAND_EXISTS ( qr , item + k ) ;
662656}
663657}
664658}
@@ -699,6 +693,8 @@ calc_rank_cd(float4 *arrdata, TSVector txt, TSQuery query, int method)
699693PrevExtPos = 0.0 ,
700694CurExtPos = 0.0 ;
701695int NExtent = 0 ;
696+ QueryRepresentation qr ;
697+
702698
703699for (i = 0 ;i < lengthof (weights );i ++ )
704700{
@@ -710,12 +706,18 @@ calc_rank_cd(float4 *arrdata, TSVector txt, TSQuery query, int method)
710706invws [i ]= 1.0 /invws [i ];
711707}
712708
713- doc = get_docrep (txt ,query ,& doclen );
709+ qr .query = query ;
710+ qr .operandexist = (int * )palloc0 (sizeof (bool )* query -> size );
711+
712+ doc = get_docrep (txt ,& qr ,& doclen );
714713if (!doc )
714+ {
715+ pfree (qr .operandexist );
715716return 0.0 ;
717+ }
716718
717719MemSet (& ext ,0 ,sizeof (Extention ));
718- while (Cover (doc ,doclen ,query ,& ext ))
720+ while (Cover (doc ,doclen ,& qr ,& ext ))
719721{
720722double Cpos = 0.0 ;
721723double InvSum = 0.0 ;
@@ -771,6 +773,8 @@ calc_rank_cd(float4 *arrdata, TSVector txt, TSQuery query, int method)
771773
772774pfree (doc );
773775
776+ pfree (qr .operandexist );
777+
774778return (float4 )Wdoc ;
775779}
776780
@@ -779,7 +783,7 @@ ts_rankcd_wttf(PG_FUNCTION_ARGS)
779783{
780784ArrayType * win = (ArrayType * )PG_DETOAST_DATUM (PG_GETARG_DATUM (0 ));
781785TSVector txt = PG_GETARG_TSVECTOR (1 );
782- TSQuery query = PG_GETARG_TSQUERY_COPY (2 );/* copy because we modify the istrue-flag */
786+ TSQuery query = PG_GETARG_TSQUERY (2 );
783787int method = PG_GETARG_INT32 (3 );
784788float res ;
785789
@@ -796,7 +800,7 @@ ts_rankcd_wtt(PG_FUNCTION_ARGS)
796800{
797801ArrayType * win = (ArrayType * )PG_DETOAST_DATUM (PG_GETARG_DATUM (0 ));
798802TSVector txt = PG_GETARG_TSVECTOR (1 );
799- TSQuery query = PG_GETARG_TSQUERY_COPY (2 );/* copy because we modify the istrue-flag */
803+ TSQuery query = PG_GETARG_TSQUERY (2 );
800804float res ;
801805
802806res = calc_rank_cd (getWeights (win ),txt ,query ,DEF_NORM_METHOD );
@@ -811,7 +815,7 @@ Datum
811815ts_rankcd_ttf (PG_FUNCTION_ARGS )
812816{
813817TSVector txt = PG_GETARG_TSVECTOR (0 );
814- TSQuery query = PG_GETARG_TSQUERY_COPY (1 );/* copy because we modify the istrue-flag */
818+ TSQuery query = PG_GETARG_TSQUERY (1 );
815819int method = PG_GETARG_INT32 (2 );
816820float res ;
817821
@@ -826,7 +830,7 @@ Datum
826830ts_rankcd_tt (PG_FUNCTION_ARGS )
827831{
828832TSVector txt = PG_GETARG_TSVECTOR (0 );
829- TSQuery query = PG_GETARG_TSQUERY_COPY (1 );/* copy because we modify the istrue-flag */
833+ TSQuery query = PG_GETARG_TSQUERY (1 );
830834float res ;
831835
832836res = calc_rank_cd (getWeights (NULL ),txt ,query ,DEF_NORM_METHOD );