@@ -256,18 +256,47 @@ extract_wraps(QueryItemWrap *wrap, ExtractContext *context, int level)
256256{
257257 if (wrap -> type == QI_VAL )
258258 {
259- bytea * addinfo = ( bytea * ) palloc ( VARHDRSZ + Max ( level , 1 ) * MAX_ENCODED_LEN ) ;
260- unsigned char * ptr = ( unsigned char * ) VARDATA ( addinfo ) ;
259+ bytea * addinfo ;
260+ unsigned char * ptr ;
261261 int index = context -> index ;
262262
263- context -> entries [index ] = PointerGetDatum (cstring_to_text_with_len (context -> operand + wrap -> distance , wrap -> length ));
264- elog (NOTICE , "%s" , text_to_cstring (DatumGetTextP (context -> entries [index ])));
263+
264+ for (index = 0 ; index < context -> index ; index ++ )
265+ {
266+ text * entry ;
267+ entry = DatumGetByteaP (context -> entries [index ]);
268+ if (VARSIZE_ANY_EXHDR (entry ) == wrap -> length &&
269+ !memcmp (context -> operand + wrap -> distance , VARDATA_ANY (entry ), wrap -> length ))
270+ break ;
271+ }
272+
273+ if (index >= context -> index )
274+ {
275+ index = context -> index ;
276+ addinfo = (bytea * ) palloc (VARHDRSZ + 2 * Max (level , 1 ) * MAX_ENCODED_LEN );
277+ ptr = (unsigned char * ) VARDATA (addinfo );
278+ context -> entries [index ] = PointerGetDatum (cstring_to_text_with_len (context -> operand + wrap -> distance , wrap -> length ));
279+ context -> addInfo [index ] = PointerGetDatum (addinfo );
280+ context -> addInfoIsNull [index ] = false;
281+ context -> index ++ ;
282+ /*ptrEnd = (unsigned char *) VARDATA(addinfo) + VARHDRSZ + 2 * Max(level, 1) * MAX_ENCODED_LEN;*/
283+ }
284+ else
285+ {
286+ addinfo = DatumGetByteaP (context -> addInfo [index ]);
287+ addinfo = (bytea * ) repalloc (addinfo ,
288+ VARSIZE (addinfo ) + 2 * Max (level , 1 ) * MAX_ENCODED_LEN );
289+ context -> addInfo [index ] = PointerGetDatum (addinfo );
290+ ptr = (unsigned char * ) VARDATA (addinfo ) + VARSIZE_ANY_EXHDR (addinfo );
291+ /*ptrEnd = (unsigned char *) VARDATA(addinfo) + VARSIZE_ANY_EXHDR(addinfo) + 2 * Max(level, 1) * MAX_ENCODED_LEN;*/
292+ }
293+ /*elog(NOTICE, "%s", text_to_cstring(DatumGetTextP(context->entries[index])));*/
265294
266295 while (wrap -> parent )
267296 {
268297 QueryItemWrap * parent = wrap -> parent ;
269298 uint32 sum ;
270- elog (NOTICE , "%d %d %d" , parent -> num , parent -> sum , wrap -> not );
299+ /* elog(NOTICE, "%d %d %d", parent->num, parent->sum, wrap->not);*/
271300 encode_varbyte ((uint32 ) parent -> num , & ptr );
272301 sum = (uint32 )abs (parent -> sum );
273302 sum <<= 2 ;
@@ -283,11 +312,9 @@ extract_wraps(QueryItemWrap *wrap, ExtractContext *context, int level)
283312 encode_varbyte (1 , & ptr );
284313 encode_varbyte (4 | 1 , & ptr );
285314 }
315+ /*Assert(ptr <= ptrEnd);*/
286316 SET_VARSIZE (addinfo , ptr - (unsigned char * )addinfo );
287-
288- context -> addInfo [index ] = PointerGetDatum (addinfo );
289- context -> addInfoIsNull [index ] = false;
290- context -> index ++ ;
317+ /*elog(NOTICE, "%s", DatumGetPointer(DirectFunctionCall1(byteaout, PointerGetDatum(addinfo))));*/
291318 }
292319 else if (wrap -> type == QI_OPR )
293320 {
@@ -325,27 +352,19 @@ ruminv_extract_tsquery(PG_FUNCTION_ARGS)
325352 QueryItem * item = GETQUERY (query );
326353 QueryItemWrap * wrap ;
327354 ExtractContext context ;
328- int num = 1 ;
355+ int num = 1 ,
356+ count ;
329357 bool extractNull ;
330358
331359 wrap = make_query_item_wrap (item , NULL , false);
332- * nentries = calc_wraps (wrap , & num );
360+ count = calc_wraps (wrap , & num );
333361 extractNull = check_allnegative (wrap );
334362 if (extractNull )
335- ( * nentries ) ++ ;
363+ count ++ ;
336364
337- entries = (Datum * ) palloc (sizeof (Datum ) * (* nentries ));
338- * addInfo = (Datum * ) palloc (sizeof (Datum ) * (* nentries ));
339- * addInfoIsNull = (bool * ) palloc (sizeof (bool ) * (* nentries ));
340- if (extractNull )
341- {
342- int i ;
343- * nullFlags = (bool * ) palloc (sizeof (bool ) * (* nentries ));
344- for (i = 0 ; i < * nentries - 1 ; i ++ )
345- (* nullFlags )[i ] = false;
346- (* nullFlags )[* nentries - 1 ] = true;
347- (* addInfoIsNull )[* nentries - 1 ] = true;
348- }
365+ entries = (Datum * ) palloc (sizeof (Datum ) * count );
366+ * addInfo = (Datum * ) palloc (sizeof (Datum ) * count );
367+ * addInfoIsNull = (bool * ) palloc (sizeof (bool ) * count );
349368
350369 context .addInfo = * addInfo ;
351370 context .addInfoIsNull = * addInfoIsNull ;
@@ -355,6 +374,20 @@ ruminv_extract_tsquery(PG_FUNCTION_ARGS)
355374
356375 extract_wraps (wrap , & context , 0 );
357376
377+ count = context .index ;
378+ if (extractNull )
379+ {
380+ int i ;
381+
382+ count ++ ;
383+ * nullFlags = (bool * ) palloc (sizeof (bool ) * count );
384+ for (i = 0 ; i < count - 1 ; i ++ )
385+ (* nullFlags )[i ] = false;
386+ (* nullFlags )[count - 1 ] = true;
387+ (* addInfoIsNull )[count - 1 ] = true;
388+ }
389+ * nentries = count ;
390+
358391/* elog(NOTICE, "%d", *nentries);
359392 for (i = 0; i < *nentries; i++)
360393 {
@@ -451,7 +484,7 @@ ruminv_tsvector_consistent(PG_FUNCTION_ARGS)
451484 ptr = (unsigned char * )VARDATA_ANY (DatumGetPointer (addInfo [i ]));
452485 size = VARSIZE_ANY_EXHDR (DatumGetPointer (addInfo [i ]));
453486
454- /* elog(NOTICE, "%d %s", i, DatumGetPointer(DirectFunctionCall1(byteaout, addInfo[i])));*/
487+ /* elog(NOTICE, "%d %s", i, DatumGetPointer(DirectFunctionCall1(byteaout, addInfo[i])));*/
455488
456489 if (size == 0 )
457490 {
@@ -473,7 +506,7 @@ ruminv_tsvector_consistent(PG_FUNCTION_ARGS)
473506
474507 index = num - 1 ;
475508
476- /* elog(NOTICE, "a %d %d %d %d", i, index, sum, not);*/
509+ /* elog(NOTICE, "a %d %d %d %d", i, index, sum, not);*/
477510
478511 if (child )
479512 {
@@ -501,7 +534,10 @@ ruminv_tsvector_consistent(PG_FUNCTION_ARGS)
501534 nodes [index ].sum ++ ;
502535 }
503536
504- child = & nodes [index ];
537+ if (index == 0 )
538+ child = NULL ;
539+ else
540+ child = & nodes [index ];
505541 }
506542 }
507543
@@ -511,7 +547,7 @@ ruminv_tsvector_consistent(PG_FUNCTION_ARGS)
511547 }
512548 else
513549 {
514- /* for (i = 0; i < lastIndex; i++)
550+ /* for (i = 0; i < lastIndex; i++)
515551 {
516552 elog(NOTICE, "s %d %d %d %d", i, nodes[i].sum, nodes[i].parent, nodes[i].not);
517553 }*/
0 commit comments