2020
2121#include "rum.h"
2222
23+ /*
24+ * A "wrapper" over tsquery item. More suitable representation for pocessing.
25+ */
2326typedef struct QueryItemWrap
2427{
2528QueryItemType type ;
@@ -33,8 +36,11 @@ typedef struct QueryItemWrap
3336int num ;
3437}QueryItemWrap ;
3538
39+ /*
40+ * Add child to tsquery item wrap.
41+ */
3642static QueryItemWrap *
37- add_child (QueryItemWrap * parent )
43+ add_child (QueryItemWrap * parent )
3844{
3945QueryItemWrap * result ;
4046
@@ -48,8 +54,11 @@ add_child(QueryItemWrap * parent)
4854return result ;
4955}
5056
57+ /*
58+ * Make wrapper over tsquery item. Flattern tree if needed.
59+ */
5160static QueryItemWrap *
52- make_query_item_wrap (QueryItem * item ,QueryItemWrap * parent ,bool not )
61+ make_query_item_wrap (QueryItem * item ,QueryItemWrap * parent ,bool not )
5362{
5463if (item -> type == QI_VAL )
5564{
@@ -107,8 +116,11 @@ make_query_item_wrap(QueryItem *item, QueryItemWrap * parent, bool not)
107116return NULL ;
108117}
109118
119+ /*
120+ * Recursively calculate "sum" for tsquery item wraps.
121+ */
110122static int
111- calc_wraps (QueryItemWrap * wrap ,int * num )
123+ calc_wraps (QueryItemWrap * wrap ,int * num )
112124{
113125int notCount = 0 ,
114126result ;
@@ -145,6 +157,10 @@ calc_wraps(QueryItemWrap * wrap, int *num)
145157return result ;
146158}
147159
160+ /*
161+ * Check if tsquery doesn't need any positive lexeme occurence for satisfaction.
162+ * That is this funciton returns true when tsquery maches empty tsvector.
163+ */
148164static bool
149165check_allnegative (QueryItemWrap * wrap )
150166{
@@ -186,6 +202,7 @@ check_allnegative(QueryItemWrap * wrap)
186202
187203}
188204
205+ /* Max length of variable-length encoded 32-bit integer */
189206#define MAX_ENCODED_LEN 5
190207
191208/*
@@ -253,16 +270,19 @@ typedef struct
253270char * operand ;
254271}ExtractContext ;
255272
273+ /*
274+ * Recursively extract entries from tsquery wraps. Encode paths into addInfos.
275+ */
256276static void
257- extract_wraps (QueryItemWrap * wrap ,ExtractContext * context ,int level )
277+ extract_wraps (QueryItemWrap * wrap ,ExtractContext * context ,int level )
258278{
259279if (wrap -> type == QI_VAL )
260280{
261281bytea * addinfo ;
262282unsignedchar * ptr ;
263283int index ;
264284
265-
285+ /* Check if given lexeme was already extracted */
266286for (index = 0 ;index < context -> index ;index ++ )
267287{
268288text * entry ;
@@ -273,6 +293,7 @@ extract_wraps(QueryItemWrap * wrap, ExtractContext * context, int level)
273293break ;
274294}
275295
296+ /* Either allocate new addInfo or extend existing addInfo */
276297if (index >=context -> index )
277298{
278299index = context -> index ;
@@ -292,6 +313,7 @@ extract_wraps(QueryItemWrap * wrap, ExtractContext * context, int level)
292313ptr = (unsignedchar * )VARDATA (addinfo )+ VARSIZE_ANY_EXHDR (addinfo );
293314}
294315
316+ /* Encode path into addInfo */
295317while (wrap -> parent )
296318{
297319QueryItemWrap * parent = wrap -> parent ;
@@ -471,6 +493,7 @@ ruminv_tsvector_consistent(PG_FUNCTION_ARGS)
471493if (addInfoIsNull [i ])
472494elog (ERROR ,"Unexpected addInfoIsNull" );
473495
496+ /* Iterate path making corresponding calculation */
474497ptr = (unsignedchar * )VARDATA_ANY (DatumGetPointer (addInfo [i ]));
475498size = VARSIZE_ANY_EXHDR (DatumGetPointer (addInfo [i ]));
476499
@@ -528,6 +551,7 @@ ruminv_tsvector_consistent(PG_FUNCTION_ARGS)
528551}
529552}
530553
554+ /* Iterate over nodes */
531555if (allFalse && check [nkeys - 1 ])
532556{
533557res = true;