@@ -256,18 +256,47 @@ extract_wraps(QueryItemWrap *wrap, ExtractContext *context, int level)
256
256
{
257
257
if (wrap -> type == QI_VAL )
258
258
{
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 ;
261
261
int index = context -> index ;
262
262
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])));*/
265
294
266
295
while (wrap -> parent )
267
296
{
268
297
QueryItemWrap * parent = wrap -> parent ;
269
298
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);*/
271
300
encode_varbyte ((uint32 ) parent -> num , & ptr );
272
301
sum = (uint32 )abs (parent -> sum );
273
302
sum <<= 2 ;
@@ -283,11 +312,9 @@ extract_wraps(QueryItemWrap *wrap, ExtractContext *context, int level)
283
312
encode_varbyte (1 , & ptr );
284
313
encode_varbyte (4 | 1 , & ptr );
285
314
}
315
+ /*Assert(ptr <= ptrEnd);*/
286
316
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))));*/
291
318
}
292
319
else if (wrap -> type == QI_OPR )
293
320
{
@@ -325,27 +352,19 @@ ruminv_extract_tsquery(PG_FUNCTION_ARGS)
325
352
QueryItem * item = GETQUERY (query );
326
353
QueryItemWrap * wrap ;
327
354
ExtractContext context ;
328
- int num = 1 ;
355
+ int num = 1 ,
356
+ count ;
329
357
bool extractNull ;
330
358
331
359
wrap = make_query_item_wrap (item , NULL , false);
332
- * nentries = calc_wraps (wrap , & num );
360
+ count = calc_wraps (wrap , & num );
333
361
extractNull = check_allnegative (wrap );
334
362
if (extractNull )
335
- ( * nentries ) ++ ;
363
+ count ++ ;
336
364
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 );
349
368
350
369
context .addInfo = * addInfo ;
351
370
context .addInfoIsNull = * addInfoIsNull ;
@@ -355,6 +374,20 @@ ruminv_extract_tsquery(PG_FUNCTION_ARGS)
355
374
356
375
extract_wraps (wrap , & context , 0 );
357
376
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
+
358
391
/* elog(NOTICE, "%d", *nentries);
359
392
for (i = 0; i < *nentries; i++)
360
393
{
@@ -451,7 +484,7 @@ ruminv_tsvector_consistent(PG_FUNCTION_ARGS)
451
484
ptr = (unsigned char * )VARDATA_ANY (DatumGetPointer (addInfo [i ]));
452
485
size = VARSIZE_ANY_EXHDR (DatumGetPointer (addInfo [i ]));
453
486
454
- /* elog(NOTICE, "%d %s", i, DatumGetPointer(DirectFunctionCall1(byteaout, addInfo[i])));*/
487
+ /* elog(NOTICE, "%d %s", i, DatumGetPointer(DirectFunctionCall1(byteaout, addInfo[i])));*/
455
488
456
489
if (size == 0 )
457
490
{
@@ -473,7 +506,7 @@ ruminv_tsvector_consistent(PG_FUNCTION_ARGS)
473
506
474
507
index = num - 1 ;
475
508
476
- /* elog(NOTICE, "a %d %d %d %d", i, index, sum, not);*/
509
+ /* elog(NOTICE, "a %d %d %d %d", i, index, sum, not);*/
477
510
478
511
if (child )
479
512
{
@@ -501,7 +534,10 @@ ruminv_tsvector_consistent(PG_FUNCTION_ARGS)
501
534
nodes [index ].sum ++ ;
502
535
}
503
536
504
- child = & nodes [index ];
537
+ if (index == 0 )
538
+ child = NULL ;
539
+ else
540
+ child = & nodes [index ];
505
541
}
506
542
}
507
543
@@ -511,7 +547,7 @@ ruminv_tsvector_consistent(PG_FUNCTION_ARGS)
511
547
}
512
548
else
513
549
{
514
- /* for (i = 0; i < lastIndex; i++)
550
+ /* for (i = 0; i < lastIndex; i++)
515
551
{
516
552
elog(NOTICE, "s %d %d %d %d", i, nodes[i].sum, nodes[i].parent, nodes[i].not);
517
553
}*/
0 commit comments