@@ -156,9 +156,6 @@ typedef struct JsonPathAnlStats
156
156
char * pathstr ;
157
157
double freq ;
158
158
int depth ;
159
-
160
- JsonPathEntry * * entries ;
161
- int nentries ;
162
159
} JsonPathAnlStats ;
163
160
164
161
/* various bits needed while analyzing JSON */
@@ -273,8 +270,6 @@ jsonAnalyzeAddPath(JsonAnalyzeContext *ctx, JsonPathEntry *parent,
273
270
stats -> stats = NULL ;
274
271
stats -> freq = 0.0 ;
275
272
stats -> depth = parent -> depth + 1 ;
276
- stats -> entries = NULL ;
277
- stats -> nentries = 0 ;
278
273
279
274
/* update maximal depth */
280
275
if (ctx -> maxdepth < stats -> depth )
@@ -504,18 +499,19 @@ jsonAnalyzeCollectPaths(JsonAnalyzeContext *ctx, Jsonb *jb, void *param)
504
499
*/
505
500
static void
506
501
jsonAnalyzeCollectSubpath (JsonAnalyzeContext * ctx , JsonPathAnlStats * pstats ,
507
- JsonbValue * jbv , int n )
502
+ JsonbValue * jbv , JsonPathEntry * * entries ,
503
+ int start_entry )
508
504
{
509
505
JsonbValue scalar ;
510
506
int i ;
511
507
512
- for (i = n ; i < pstats -> depth ; i ++ )
508
+ for (i = start_entry ; i < pstats -> depth ; i ++ )
513
509
{
514
- JsonPathEntry * entry = pstats -> entries [i ];
510
+ JsonPathEntry * entry = entries [i ];
515
511
JsonbContainer * jbc = jbv -> val .binary .data ;
516
512
JsonbValueType type = jbv -> type ;
517
513
518
- if (i > n )
514
+ if (i > start_entry )
519
515
pfree (jbv );
520
516
521
517
if (type != jbvBinary )
@@ -535,7 +531,7 @@ jsonAnalyzeCollectSubpath(JsonAnalyzeContext *ctx, JsonPathAnlStats *pstats,
535
531
while ((r = JsonbIteratorNext (& it , & elem , true)) != WJB_DONE )
536
532
{
537
533
if (r == WJB_ELEM )
538
- jsonAnalyzeCollectSubpath (ctx , pstats , & elem , i + 1 );
534
+ jsonAnalyzeCollectSubpath (ctx , pstats , & elem , entries , i + 1 );
539
535
}
540
536
541
537
return ;
@@ -553,14 +549,14 @@ jsonAnalyzeCollectSubpath(JsonAnalyzeContext *ctx, JsonPathAnlStats *pstats,
553
549
}
554
550
}
555
551
556
- if (i == n &&
552
+ if (i == start_entry &&
557
553
jbv -> type == jbvBinary &&
558
554
JsonbExtractScalar (jbv -> val .binary .data , & scalar ))
559
555
jbv = & scalar ;
560
556
561
557
jsonAnalyzeJsonValue (ctx , & pstats -> vstats , jbv );
562
558
563
- if (i > n )
559
+ if (i > start_entry )
564
560
pfree (jbv );
565
561
}
566
562
@@ -571,25 +567,24 @@ jsonAnalyzeCollectSubpath(JsonAnalyzeContext *ctx, JsonPathAnlStats *pstats,
571
567
static void
572
568
jsonAnalyzeCollectPath (JsonAnalyzeContext * ctx , Jsonb * jb , void * param )
573
569
{
574
- JsonPathAnlStats * pstats = (JsonPathAnlStats * ) param ;
575
- JsonbValue jbvtmp ;
576
- JsonbValue * jbv = JsonValueInitBinary (& jbvtmp , jb );
570
+ JsonPathAnlStats * pstats = (JsonPathAnlStats * ) param ;
571
+ JsonbValue jbvtmp ;
572
+ JsonbValue * jbv = JsonValueInitBinary (& jbvtmp , jb );
577
573
JsonPathEntry * path ;
574
+ JsonPathEntry * * entries ;
575
+ int i ;
578
576
579
- if (!pstats -> entries )
580
- {
581
- int i ;
577
+ entries = palloc (sizeof (* entries ) * pstats -> depth );
582
578
583
- pstats -> entries = MemoryContextAlloc (ctx -> mcxt ,
584
- sizeof (* pstats -> entries ) * pstats -> depth );
579
+ /* Build entry array in direct order */
580
+ for (path = & pstats -> path , i = pstats -> depth - 1 ;
581
+ path -> parent && i >= 0 ;
582
+ path = path -> parent , i -- )
583
+ entries [i ] = path ;
585
584
586
- for (path = & pstats -> path , i = pstats -> depth - 1 ;
587
- path -> parent && i >= 0 ;
588
- path = path -> parent , i -- )
589
- pstats -> entries [i ] = path ;
590
- }
585
+ jsonAnalyzeCollectSubpath (ctx , pstats , jbv , entries , 0 );
591
586
592
- jsonAnalyzeCollectSubpath ( ctx , pstats , jbv , 0 );
587
+ pfree ( entries );
593
588
}
594
589
595
590
static Datum
0 commit comments