Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 633eba3

Browse files
author
Nikita Glukhov
committed
Add some comments, fix whitespace
1 parent 46a2589 commit 633eba3

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

src/backend/utils/adt/jsonb_typanalyze.c

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -955,11 +955,13 @@ jsonAnalyzeSortPaths(JsonAnalyzeContext *ctx, int *p_npaths)
955955

956956
/*
957957
* jsonAnalyzeBuildPathStatsArray
958-
* ???
958+
* Build jsonb datum array for path stats, that will be used as stavalues.
959+
*
960+
* The first element is a path prefix.
959961
*/
960962
static Datum *
961963
jsonAnalyzeBuildPathStatsArray(JsonPathAnlStats **paths, int npaths, int *nvals,
962-
const char *prefix, int prefixlen)
964+
const char *prefix, int prefixlen)
963965
{
964966
Datum *values = palloc(sizeof(Datum) * (npaths + 1));
965967
JsonbValue *jbvprefix = palloc(sizeof(JsonbValue));
@@ -981,14 +983,14 @@ jsonAnalyzeBuildPathStatsArray(JsonPathAnlStats **paths, int npaths, int *nvals,
981983

982984
/*
983985
* jsonAnalyzeMakeStats
984-
* ???
986+
* Build stavalues jsonb array for the root path prefix.
985987
*/
986988
static Datum *
987989
jsonAnalyzeMakeStats(JsonAnalyzeContext *ctx, JsonPathAnlStats **paths,
988990
int npaths, int *numvalues)
989991
{
990-
Datum *values;
991-
MemoryContext oldcxt = MemoryContextSwitchTo(ctx->stats->anl_context);
992+
Datum *values;
993+
MemoryContext oldcxt = MemoryContextSwitchTo(ctx->stats->anl_context);
992994

993995
values = jsonAnalyzeBuildPathStatsArray(paths, npaths, numvalues,
994996
JSON_PATH_ROOT, JSON_PATH_ROOT_LEN);
@@ -1000,7 +1002,10 @@ jsonAnalyzeMakeStats(JsonAnalyzeContext *ctx, JsonPathAnlStats **paths,
10001002

10011003
/*
10021004
* jsonAnalyzeBuildSubPathsData
1003-
* ???
1005+
* Build statvalues and stanumbers arrays for the subset of paths starting
1006+
* from a given prefix.
1007+
*
1008+
* pathsDatums[index] should point to the desired path.
10041009
*/
10051010
bool
10061011
jsonAnalyzeBuildSubPathsData(Datum *pathsDatums, int npaths, int index,
@@ -1009,33 +1014,40 @@ jsonAnalyzeBuildSubPathsData(Datum *pathsDatums, int npaths, int index,
10091014
Datum *pvals, Datum *pnums)
10101015
{
10111016
JsonPathAnlStats **pvalues = palloc(sizeof(*pvalues) * npaths);
1012-
Datum *values;
1013-
Datum numbers[1];
1014-
JsonbValue pathkey;
1015-
int nsubpaths = 0;
1016-
int nvalues;
1017-
int i;
1017+
Datum *values;
1018+
Datum numbers[1];
1019+
JsonbValue pathkey;
1020+
int nsubpaths = 0;
1021+
int nvalues;
1022+
int i;
10181023

10191024
JsonValueInitStringWithLen(&pathkey, "path", 4);
10201025

10211026
for (i = index; i < npaths; i++)
10221027
{
1028+
/* Extract path name */
10231029
Jsonb *jb = DatumGetJsonbP(pathsDatums[i]);
10241030
JsonbValue *jbv = findJsonbValueFromContainer(&jb->root, JB_FOBJECT,
10251031
&pathkey);
10261032

1033+
/* Check if path name starts with a given prefix */
10271034
if (!jbv || jbv->type != jbvString ||
10281035
jbv->val.string.len < pathlen ||
10291036
memcmp(jbv->val.string.val, path, pathlen))
10301037
break;
10311038

10321039
pfree(jbv);
10331040

1041+
/* Collect matching path */
10341042
pvalues[nsubpaths] = palloc(sizeof(**pvalues));
10351043
pvalues[nsubpaths]->stats = jb;
10361044

10371045
nsubpaths++;
10381046

1047+
/*
1048+
* The path should go before its subpaths, so if subpaths are not
1049+
* needed the loop is broken after the first matching path.
1050+
*/
10391051
if (!includeSubpaths)
10401052
break;
10411053
}
@@ -1046,6 +1058,7 @@ jsonAnalyzeBuildSubPathsData(Datum *pathsDatums, int npaths, int index,
10461058
return false;
10471059
}
10481060

1061+
/* Construct new array from the selected paths */
10491062
values = jsonAnalyzeBuildPathStatsArray(pvalues, nsubpaths, &nvalues,
10501063
path, pathlen);
10511064
*pvals = PointerGetDatum(construct_array(values, nvalues, JSONBOID, -1,

0 commit comments

Comments
 (0)