@@ -47,18 +47,33 @@ typedef struct
47
47
48
48
typedef struct
49
49
{
50
- QueryItem * * item ;
51
- int16 nitem ;
52
- QueryItem * item_first ;
53
- int32 keyn ;
50
+ union
51
+ {
52
+ /* Used in rum_ts_distance() */
53
+ struct
54
+ {
55
+ QueryItem * * item ;
56
+ int16 nitem ;
57
+ } item ;
58
+ /* Used in rum_tsquery_distance() */
59
+ struct
60
+ {
61
+ QueryItem * item_first ;
62
+ int32 keyn ;
63
+ } key ;
64
+ } data ;
54
65
uint8 wclass ;
55
66
int32 pos ;
56
67
} DocRepresentation ;
57
68
58
69
typedef struct
59
70
{
60
71
TSQuery query ;
72
+ /* Used in rum_tsquery_distance() */
73
+ int * map_item_operand ;
74
+
61
75
bool * operandexist ;
76
+ int lenght ;
62
77
} QueryRepresentation ;
63
78
64
79
typedef struct
@@ -605,15 +620,21 @@ checkcondition_QueryOperand(void *checkval, QueryOperand *val,
605
620
{
606
621
QueryRepresentation * qr = (QueryRepresentation * ) checkval ;
607
622
623
+ /* Check for rum_tsquery_distance() */
624
+ if (qr -> map_item_operand != NULL )
625
+ {
626
+ int i = (QueryItem * ) val - GETQUERY (qr -> query );
627
+ return qr -> operandexist [qr -> map_item_operand [i ]];
628
+ }
629
+
608
630
return QR_GET_OPERAND_EXISTS (qr , val );
609
631
}
610
632
611
633
static bool
612
634
Cover (DocRepresentation * doc , uint32 len , QueryRepresentation * qr ,
613
- int * map_item_operand , Extention * ext )
635
+ Extention * ext )
614
636
{
615
637
DocRepresentation * ptr ;
616
- QueryItem * item = GETQUERY (qr -> query );
617
638
int lastpos = ext -> pos ;
618
639
int i ;
619
640
bool found = false;
@@ -624,7 +645,7 @@ Cover(DocRepresentation *doc, uint32 len, QueryRepresentation *qr,
624
645
*/
625
646
check_stack_depth ();
626
647
627
- memset (qr -> operandexist , 0 , sizeof (bool ) * qr -> query -> size );
648
+ memset (qr -> operandexist , 0 , sizeof (bool ) * qr -> lenght );
628
649
629
650
ext -> p = 0x7fffffff ;
630
651
ext -> q = 0 ;
@@ -633,16 +654,14 @@ Cover(DocRepresentation *doc, uint32 len, QueryRepresentation *qr,
633
654
/* find upper bound of cover from current position, move up */
634
655
while (ptr - doc < len )
635
656
{
636
- if (ptr -> item != NULL )
657
+ if (qr -> map_item_operand != NULL )
637
658
{
638
- for (i = 0 ; i < ptr -> nitem ; i ++ )
639
- QR_SET_OPERAND_EXISTS (qr , ptr -> item [i ]);
659
+ qr -> operandexist [ptr -> data .key .keyn ] = true;
640
660
}
641
661
else
642
662
{
643
- for (i = 0 ; i < qr -> query -> size ; i ++ )
644
- if (ptr -> keyn == map_item_operand [i ])
645
- QR_SET_OPERAND_EXISTS (qr , item + i );
663
+ for (i = 0 ; i < ptr -> data .item .nitem ; i ++ )
664
+ QR_SET_OPERAND_EXISTS (qr , ptr -> data .item .item [i ]);
646
665
}
647
666
if (TS_execute (GETQUERY (qr -> query ), (void * ) qr , false,
648
667
checkcondition_QueryOperand ))
@@ -662,23 +681,21 @@ Cover(DocRepresentation *doc, uint32 len, QueryRepresentation *qr,
662
681
if (!found )
663
682
return false;
664
683
665
- memset (qr -> operandexist , 0 , sizeof (bool ) * qr -> query -> size );
684
+ memset (qr -> operandexist , 0 , sizeof (bool ) * qr -> lenght );
666
685
667
686
ptr = doc + lastpos ;
668
687
669
688
/* find lower bound of cover from found upper bound, move down */
670
689
while (ptr >= doc + ext -> pos )
671
690
{
672
- if (ptr -> item != NULL )
691
+ if (qr -> map_item_operand != NULL )
673
692
{
674
- for (i = 0 ; i < ptr -> nitem ; i ++ )
675
- QR_SET_OPERAND_EXISTS (qr , ptr -> item [i ]);
693
+ qr -> operandexist [ptr -> data .key .keyn ] = true;
676
694
}
677
695
else
678
696
{
679
- for (i = 0 ; i < qr -> query -> size ; i ++ )
680
- if (ptr -> keyn == map_item_operand [i ])
681
- QR_SET_OPERAND_EXISTS (qr , item + i );
697
+ for (i = 0 ; i < ptr -> data .item .nitem ; i ++ )
698
+ QR_SET_OPERAND_EXISTS (qr , ptr -> data .item .item [i ]);
682
699
}
683
700
if (TS_execute (GETQUERY (qr -> query ), (void * ) qr , true,
684
701
checkcondition_QueryOperand ))
@@ -704,11 +721,11 @@ Cover(DocRepresentation *doc, uint32 len, QueryRepresentation *qr,
704
721
}
705
722
706
723
ext -> pos ++ ;
707
- return Cover (doc , len , qr , map_item_operand , ext );
724
+ return Cover (doc , len , qr , ext );
708
725
}
709
726
710
727
static DocRepresentation *
711
- get_docrep_addinfo (bool * check , QueryRepresentation * qr , int * map_item_operand ,
728
+ get_docrep_addinfo (bool * check , QueryRepresentation * qr ,
712
729
Datum * addInfo , bool * addInfoIsNull , uint32 * doclen )
713
730
{
714
731
QueryItem * item = GETQUERY (qr -> query );
@@ -730,7 +747,7 @@ get_docrep_addinfo(bool *check, QueryRepresentation *qr, int *map_item_operand,
730
747
if (item [i ].type != QI_VAL )
731
748
continue ;
732
749
733
- keyN = map_item_operand [i ];
750
+ keyN = qr -> map_item_operand [i ];
734
751
if (!check [keyN ])
735
752
continue ;
736
753
@@ -759,9 +776,8 @@ get_docrep_addinfo(bool *check, QueryRepresentation *qr, int *map_item_operand,
759
776
{
760
777
ptrt = decompress_pos (ptrt , & post );
761
778
762
- doc [cur ].item = NULL ;
763
- doc [cur ].item_first = item + i ;
764
- doc [cur ].keyn = keyN ;
779
+ doc [cur ].data .key .item_first = item + i ;
780
+ doc [cur ].data .key .keyn = keyN ;
765
781
doc [cur ].pos = WEP_GETPOS (post );
766
782
doc [cur ].wclass = WEP_GETWEIGHT (post );
767
783
cur ++ ;
@@ -894,8 +910,9 @@ get_docrep(TSVector txt, QueryRepresentation *qr, uint32 *doclen)
894
910
{
895
911
int k ;
896
912
897
- doc [cur ].nitem = 0 ;
898
- doc [cur ].item = (QueryItem * * ) palloc (sizeof (QueryItem * ) * qr -> query -> size );
913
+ doc [cur ].data .item .nitem = 0 ;
914
+ doc [cur ].data .item .item = (QueryItem * * ) palloc (
915
+ sizeof (QueryItem * ) * qr -> query -> size );
899
916
900
917
for (k = 0 ; k < qr -> query -> size ; k ++ )
901
918
{
@@ -910,18 +927,18 @@ get_docrep(TSVector txt, QueryRepresentation *qr, uint32 *doclen)
910
927
* if k == i, we've already checked above that
911
928
* it's type == Q_VAL
912
929
*/
913
- doc [cur ].item [doc [cur ].nitem ] = item + k ;
914
- doc [cur ].nitem ++ ;
930
+ doc [cur ].data .item .item [doc [cur ].data .item .nitem ] =
931
+ item + k ;
932
+ doc [cur ].data .item .nitem ++ ;
915
933
QR_SET_OPERAND_EXISTS (qr , item + k );
916
934
}
917
935
}
918
936
}
919
937
else
920
938
{
921
- doc [cur ].nitem = doc [cur - 1 ].nitem ;
922
- doc [cur ].item = doc [cur - 1 ].item ;
939
+ doc [cur ].data . item . nitem = doc [cur - 1 ]. data . item .nitem ;
940
+ doc [cur ].data . item . item = doc [cur - 1 ]. data . item .item ;
923
941
}
924
- doc [cur ].keyn = -1 ;
925
942
doc [cur ].pos = WEP_GETPOS (post [j ]);
926
943
doc [cur ].wclass = WEP_GETWEIGHT (post [j ]);
927
944
cur ++ ;
@@ -945,7 +962,7 @@ get_docrep(TSVector txt, QueryRepresentation *qr, uint32 *doclen)
945
962
946
963
static double
947
964
calc_score_docr (float4 * arrdata , DocRepresentation * doc , uint32 doclen ,
948
- QueryRepresentation * qr , int * map_item_operand , int method )
965
+ QueryRepresentation * qr , int method )
949
966
{
950
967
int32 i ;
951
968
Extention ext ;
@@ -962,7 +979,7 @@ calc_score_docr(float4 *arrdata, DocRepresentation *doc, uint32 doclen,
962
979
int ncovers = 0 ;
963
980
964
981
MemSet (& ext , 0 , sizeof (Extention ));
965
- while (Cover (doc , doclen , qr , map_item_operand , & ext ))
982
+ while (Cover (doc , doclen , qr , & ext ))
966
983
{
967
984
double Cpos = 0.0 ;
968
985
double InvSum = 0.0 ;
@@ -971,18 +988,19 @@ calc_score_docr(float4 *arrdata, DocRepresentation *doc, uint32 doclen,
971
988
/* Added by SK */
972
989
int new_cover_idx = 0 ;
973
990
int new_cover_key = 0 ;
974
- QueryItem * item = GETQUERY (qr -> query );
975
991
int nitems = 0 ;
976
992
977
993
while (ptr <= ext .end )
978
994
{
979
995
InvSum += arrdata [ptr -> wclass ];
980
996
/* SK: Quick and dirty hash key. Hope collisions will be not too frequent. */
981
997
new_cover_key = new_cover_key << 1 ;
982
- if (ptr -> item != NULL )
983
- new_cover_key += (int )(uintptr_t )ptr -> item ;
998
+ /* For rum_ts_distance() */
999
+ if (qr -> map_item_operand == NULL )
1000
+ new_cover_key += (int )(uintptr_t )ptr -> data .item .item ;
1001
+ /* For rum_tsquery_distance() */
984
1002
else
985
- new_cover_key += (int )(uintptr_t )ptr -> item_first ;
1003
+ new_cover_key += (int )(uintptr_t )ptr -> data . key . item_first ;
986
1004
ptr ++ ;
987
1005
}
988
1006
@@ -1013,10 +1031,9 @@ calc_score_docr(float4 *arrdata, DocRepresentation *doc, uint32 doclen,
1013
1031
cover_keys [new_cover_idx ] = new_cover_key ;
1014
1032
1015
1033
/* Compute the number of query terms in the cover */
1016
- for (i = 0 ; i < qr -> query -> size ; i ++ )
1017
- if (item [i ].type == QI_VAL &&
1018
- QR_GET_OPERAND_EXISTS (qr , & item [i ]))
1019
- nitems ++ ;
1034
+ for (i = 0 ; i < qr -> lenght ; i ++ )
1035
+ if (qr -> operandexist [i ])
1036
+ nitems ++ ;
1020
1037
1021
1038
Cpos = ((double ) (ext .end - ext .begin + 1 )) / InvSum ;
1022
1039
@@ -1066,26 +1083,27 @@ calc_score_docr(float4 *arrdata, DocRepresentation *doc, uint32 doclen,
1066
1083
1067
1084
static float4
1068
1085
calc_score_addinfo (float4 * arrdata , bool * check , TSQuery query ,
1069
- int * map_item_operand , Datum * addInfo , bool * addInfoIsNull )
1086
+ int * map_item_operand , Datum * addInfo , bool * addInfoIsNull ,
1087
+ int nkeys )
1070
1088
{
1071
1089
DocRepresentation * doc ;
1072
1090
uint32 doclen = 0 ;
1073
1091
double Wdoc = 0.0 ;
1074
1092
QueryRepresentation qr ;
1075
1093
1076
1094
qr .query = query ;
1077
- qr .operandexist = (bool * ) palloc0 (sizeof (bool ) * query -> size );
1095
+ qr .map_item_operand = map_item_operand ;
1096
+ qr .operandexist = (bool * ) palloc0 (sizeof (bool ) * nkeys );
1097
+ qr .lenght = nkeys ;
1078
1098
1079
- doc = get_docrep_addinfo (check , & qr , map_item_operand ,
1080
- addInfo , addInfoIsNull , & doclen );
1099
+ doc = get_docrep_addinfo (check , & qr , addInfo , addInfoIsNull , & doclen );
1081
1100
if (!doc )
1082
1101
{
1083
1102
pfree (qr .operandexist );
1084
1103
return 0.0 ;
1085
1104
}
1086
1105
1087
- Wdoc = calc_score_docr (arrdata , doc , doclen , & qr , map_item_operand ,
1088
- DEF_NORM_METHOD );
1106
+ Wdoc = calc_score_docr (arrdata , doc , doclen , & qr , DEF_NORM_METHOD );
1089
1107
1090
1108
pfree (doc );
1091
1109
pfree (qr .operandexist );
@@ -1103,7 +1121,9 @@ calc_score(float4 *arrdata, TSVector txt, TSQuery query, int method)
1103
1121
QueryRepresentation qr ;
1104
1122
1105
1123
qr .query = query ;
1124
+ qr .map_item_operand = NULL ;
1106
1125
qr .operandexist = (bool * ) palloc0 (sizeof (bool ) * query -> size );
1126
+ qr .lenght = query -> size ;
1107
1127
1108
1128
doc = get_docrep (txt , & qr , & doclen );
1109
1129
if (!doc )
@@ -1112,7 +1132,7 @@ calc_score(float4 *arrdata, TSVector txt, TSQuery query, int method)
1112
1132
return 0.0 ;
1113
1133
}
1114
1134
1115
- Wdoc = calc_score_docr (arrdata , doc , doclen , & qr , NULL , method );
1135
+ Wdoc = calc_score_docr (arrdata , doc , doclen , & qr , method );
1116
1136
1117
1137
if ((method & RANK_NORM_LOGLENGTH ) && txt -> size > 0 )
1118
1138
Wdoc /= log ((double ) (count_length (txt ) + 1 ));
@@ -1142,14 +1162,15 @@ rum_tsquery_distance(PG_FUNCTION_ARGS)
1142
1162
bool * check = (bool * ) PG_GETARG_POINTER (0 );
1143
1163
1144
1164
TSQuery query = PG_GETARG_TSQUERY (2 );
1165
+ int nkeys = PG_GETARG_INT32 (3 );
1145
1166
Pointer * extra_data = (Pointer * ) PG_GETARG_POINTER (4 );
1146
1167
Datum * addInfo = (Datum * ) PG_GETARG_POINTER (8 );
1147
1168
bool * addInfoIsNull = (bool * ) PG_GETARG_POINTER (9 );
1148
1169
float8 res ;
1149
1170
int * map_item_operand = (int * ) (extra_data [0 ]);
1150
1171
1151
1172
res = calc_score_addinfo (weights , check , query , map_item_operand ,
1152
- addInfo , addInfoIsNull );
1173
+ addInfo , addInfoIsNull , nkeys );
1153
1174
1154
1175
PG_FREE_IF_COPY (query , 2 );
1155
1176
if (res == 0 )
0 commit comments