7
7
*
8
8
*
9
9
* IDENTIFICATION
10
- * $PostgreSQL: pgsql/src/backend/utils/adt/tsrank.c,v 1.5 2007/09/11 08:46:29 teodor Exp $
10
+ * $PostgreSQL: pgsql/src/backend/utils/adt/tsrank.c,v 1.6 2007/09/11 16:01:40 teodor Exp $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
@@ -476,25 +476,20 @@ compareDocR(const void *va, const void *vb)
476
476
return (a -> pos > b -> pos ) ? 1 : -1 ;
477
477
}
478
478
479
- static bool
480
- checkcondition_QueryOperand (void * checkval , QueryOperand * val )
479
+ typedef struct
481
480
{
482
- return (bool ) (val -> istrue );
483
- }
481
+ TSQuery query ;
482
+ bool * operandexist ;
483
+ } QueryRepresentation ;
484
484
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
490
487
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 );
498
493
}
499
494
500
495
typedef struct
@@ -508,7 +503,7 @@ typedef struct
508
503
509
504
510
505
static bool
511
- Cover (DocRepresentation * doc , int len , TSQuery query , Extention * ext )
506
+ Cover (DocRepresentation * doc , int len , QueryRepresentation * qr , Extention * ext )
512
507
{
513
508
DocRepresentation * ptr ;
514
509
int lastpos = ext -> pos ;
@@ -519,7 +514,7 @@ Cover(DocRepresentation *doc, int len, TSQuery query, Extention *ext)
519
514
* (though any decent compiler will optimize away the tail-recursion. */
520
515
check_stack_depth ();
521
516
522
- reset_istrue_flag ( query );
517
+ memset ( qr -> operandexist , 0 , sizeof ( bool ) * qr -> query -> size );
523
518
524
519
ext -> p = 0x7fffffff ;
525
520
ext -> q = 0 ;
@@ -531,9 +526,9 @@ Cover(DocRepresentation *doc, int len, TSQuery query, Extention *ext)
531
526
for (i = 0 ; i < ptr -> nitem ; i ++ )
532
527
{
533
528
if (ptr -> item [i ]-> type == QI_VAL )
534
- ptr -> item [i ]-> operand . istrue = 1 ;
529
+ QR_SET_OPERAND_EXISTS ( qr , ptr -> item [i ]) ;
535
530
}
536
- if (TS_execute (GETQUERY (query ), NULL , false, checkcondition_QueryOperand ))
531
+ if (TS_execute (GETQUERY (qr -> query ), ( void * ) qr , false, checkcondition_QueryOperand ))
537
532
{
538
533
if (ptr -> pos > ext -> q )
539
534
{
@@ -550,7 +545,7 @@ Cover(DocRepresentation *doc, int len, TSQuery query, Extention *ext)
550
545
if (!found )
551
546
return false;
552
547
553
- reset_istrue_flag ( query );
548
+ memset ( qr -> operandexist , 0 , sizeof ( bool ) * qr -> query -> size );
554
549
555
550
ptr = doc + lastpos ;
556
551
@@ -559,8 +554,8 @@ Cover(DocRepresentation *doc, int len, TSQuery query, Extention *ext)
559
554
{
560
555
for (i = 0 ; i < ptr -> nitem ; i ++ )
561
556
if (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 ))
564
559
{
565
560
if (ptr -> pos < ext -> p )
566
561
{
@@ -583,28 +578,27 @@ Cover(DocRepresentation *doc, int len, TSQuery query, Extention *ext)
583
578
}
584
579
585
580
ext -> pos ++ ;
586
- return Cover (doc , len , query , ext );
581
+ return Cover (doc , len , qr , ext );
587
582
}
588
583
589
584
static DocRepresentation *
590
- get_docrep (TSVector txt , TSQuery query , int * doclen )
585
+ get_docrep (TSVector txt , QueryRepresentation * qr , int * doclen )
591
586
{
592
- QueryItem * item = GETQUERY (query );
587
+ QueryItem * item = GETQUERY (qr -> query );
593
588
WordEntry * entry ;
594
589
WordEntryPos * post ;
595
590
int4 dimt ,
596
591
j ,
597
592
i ;
598
- int len = query -> size * 4 ,
593
+ int len = qr -> query -> size * 4 ,
599
594
cur = 0 ;
600
595
DocRepresentation * doc ;
601
596
char * operand ;
602
597
603
598
doc = (DocRepresentation * ) palloc (sizeof (DocRepresentation ) * len );
604
- operand = GETOPERAND (query );
605
- reset_istrue_flag (query );
599
+ operand = GETOPERAND (qr -> query );
606
600
607
- for (i = 0 ; i < query -> size ; i ++ )
601
+ for (i = 0 ; i < qr -> query -> size ; i ++ )
608
602
{
609
603
QueryOperand * curoperand ;
610
604
@@ -613,10 +607,10 @@ get_docrep(TSVector txt, TSQuery query, int *doclen)
613
607
614
608
curoperand = & item [i ].operand ;
615
609
616
- if (item [i ]. operand . istrue )
610
+ if (QR_GET_OPERAND_EXISTS ( qr , & item [i ]) )
617
611
continue ;
618
612
619
- entry = find_wordentry (txt , query , curoperand );
613
+ entry = find_wordentry (txt , qr -> query , curoperand );
620
614
if (!entry )
621
615
continue ;
622
616
@@ -644,9 +638,9 @@ get_docrep(TSVector txt, TSQuery query, int *doclen)
644
638
int k ;
645
639
646
640
doc [cur ].nitem = 0 ;
647
- doc [cur ].item = (QueryItem * * ) palloc (sizeof (QueryItem * ) * query -> size );
641
+ doc [cur ].item = (QueryItem * * ) palloc (sizeof (QueryItem * ) * qr -> query -> size );
648
642
649
- for (k = 0 ; k < query -> size ; k ++ )
643
+ for (k = 0 ; k < qr -> query -> size ; k ++ )
650
644
{
651
645
QueryOperand * kptr = & item [k ].operand ;
652
646
QueryOperand * iptr = & item [i ].operand ;
@@ -658,7 +652,7 @@ get_docrep(TSVector txt, TSQuery query, int *doclen)
658
652
/* if k == i, we've already checked above that it's type == Q_VAL */
659
653
doc [cur ].item [doc [cur ].nitem ] = item + k ;
660
654
doc [cur ].nitem ++ ;
661
- item [ k ]. operand . istrue = 1 ;
655
+ QR_SET_OPERAND_EXISTS ( qr , item + k ) ;
662
656
}
663
657
}
664
658
}
@@ -699,6 +693,8 @@ calc_rank_cd(float4 *arrdata, TSVector txt, TSQuery query, int method)
699
693
PrevExtPos = 0.0 ,
700
694
CurExtPos = 0.0 ;
701
695
int NExtent = 0 ;
696
+ QueryRepresentation qr ;
697
+
702
698
703
699
for (i = 0 ; i < lengthof (weights ); i ++ )
704
700
{
@@ -710,12 +706,18 @@ calc_rank_cd(float4 *arrdata, TSVector txt, TSQuery query, int method)
710
706
invws [i ] = 1.0 / invws [i ];
711
707
}
712
708
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 );
714
713
if (!doc )
714
+ {
715
+ pfree ( qr .operandexist );
715
716
return 0.0 ;
717
+ }
716
718
717
719
MemSet (& ext , 0 , sizeof (Extention ));
718
- while (Cover (doc , doclen , query , & ext ))
720
+ while (Cover (doc , doclen , & qr , & ext ))
719
721
{
720
722
double Cpos = 0.0 ;
721
723
double InvSum = 0.0 ;
@@ -771,6 +773,8 @@ calc_rank_cd(float4 *arrdata, TSVector txt, TSQuery query, int method)
771
773
772
774
pfree (doc );
773
775
776
+ pfree ( qr .operandexist );
777
+
774
778
return (float4 ) Wdoc ;
775
779
}
776
780
@@ -779,7 +783,7 @@ ts_rankcd_wttf(PG_FUNCTION_ARGS)
779
783
{
780
784
ArrayType * win = (ArrayType * ) PG_DETOAST_DATUM (PG_GETARG_DATUM (0 ));
781
785
TSVector 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 );
783
787
int method = PG_GETARG_INT32 (3 );
784
788
float res ;
785
789
@@ -796,7 +800,7 @@ ts_rankcd_wtt(PG_FUNCTION_ARGS)
796
800
{
797
801
ArrayType * win = (ArrayType * ) PG_DETOAST_DATUM (PG_GETARG_DATUM (0 ));
798
802
TSVector 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 );
800
804
float res ;
801
805
802
806
res = calc_rank_cd (getWeights (win ), txt , query , DEF_NORM_METHOD );
@@ -811,7 +815,7 @@ Datum
811
815
ts_rankcd_ttf (PG_FUNCTION_ARGS )
812
816
{
813
817
TSVector 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 );
815
819
int method = PG_GETARG_INT32 (2 );
816
820
float res ;
817
821
@@ -826,7 +830,7 @@ Datum
826
830
ts_rankcd_tt (PG_FUNCTION_ARGS )
827
831
{
828
832
TSVector 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 );
830
834
float res ;
831
835
832
836
res = calc_rank_cd (getWeights (NULL ), txt , query , DEF_NORM_METHOD );
0 commit comments