Skip to content

Commit f9bf633

Browse files
committed
Clean up get_pgsm_query_id_hash()
The function was unnecessarily convoluted so clean it up by making the parser easier to read, asserting against NULL input and wrapping the comment at the right width. Especially the code for handling whitespace was much more complicated than necessary.
1 parent 86edb37 commit f9bf633

1 file changed

Lines changed: 41 additions & 55 deletions

File tree

src/pg_stat_monitor.c

Lines changed: 41 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2586,11 +2586,10 @@ get_next_wbucket(pgsmSharedState *pgsm)
25862586
}
25872587

25882588
/*
2589-
* This function expects a NORMALIZED query as the input.
2590-
* It iterates over the normalized query skipping comments and
2591-
* multiple spaces. All spaces are converted to ' ' so that we
2592-
* the calculation is independent of the space type whether
2593-
* newline, tab, or any other type. Trailing and leading spaces
2589+
* This function expects a NORMALIZED query as the input. It iterates over the
2590+
* normalized query skipping comments and multiple spaces. All spaces are
2591+
* converted to ' ' so that we the calculation is independent of the space
2592+
* type whether newline, tab, or any other type. Trailing and leading spaces
25942593
* are also removed before calculating the hash.
25952594
*/
25962595
static int64
@@ -2599,82 +2598,69 @@ get_pgsm_query_id_hash(const char *norm_query, int norm_len)
25992598
char *query;
26002599
char *q_iter;
26012600
const char *norm_q_iter = norm_query;
2602-
int64 pgsm_query_id = 0;
2601+
int64 pgsm_query_id;
2602+
2603+
Assert(norm_query != NULL);
26032604

26042605
if (!pgsm_enable_pgsm_query_id)
26052606
return 0;
26062607

26072608
query = palloc(norm_len + 1);
26082609
q_iter = query;
26092610

2610-
while (norm_q_iter && *norm_q_iter && norm_q_iter < norm_query + norm_len)
2611+
while (*norm_q_iter && norm_q_iter < norm_query + norm_len)
26112612
{
2612-
/*
2613-
* Skip multiline comments, + 1 is safe even if we've reach end of
2614-
* string
2615-
*/
26162613
if (*norm_q_iter == '/' && *(norm_q_iter + 1) == '*')
26172614
{
2618-
while (*norm_q_iter && *(norm_q_iter + 1) && (*norm_q_iter != '*' || *(norm_q_iter + 1) != '/'))
2619-
norm_q_iter++;
2620-
2621-
/*
2622-
* Skip the end if the current character is valid. norm_q_iter
2623-
* points to the *, we have to skip 2 characters
2624-
*/
2625-
if (*norm_q_iter)
2626-
norm_q_iter++;
2627-
if (*norm_q_iter)
2628-
norm_q_iter++;
2615+
/* Skip multiline comments */
2616+
norm_q_iter++;
2617+
norm_q_iter++;
26292618

2630-
continue;
2619+
while (*norm_q_iter)
2620+
{
2621+
if (*norm_q_iter == '*' && *(norm_q_iter + 1) == '/')
2622+
{
2623+
norm_q_iter++;
2624+
norm_q_iter++;
2625+
break;
2626+
}
2627+
else
2628+
norm_q_iter++;
2629+
}
26312630
}
2632-
2633-
/*
2634-
* Skip single line comments, + 1 is safe even if we've reach end of
2635-
* string
2636-
*/
2637-
if (*norm_q_iter == '-' && *(norm_q_iter + 1) == '-')
2631+
else if (*norm_q_iter == '-' && *(norm_q_iter + 1) == '-')
26382632
{
2633+
/* Skip single line comments */
2634+
norm_q_iter++;
2635+
norm_q_iter++;
2636+
26392637
while (*norm_q_iter && *norm_q_iter != '\n')
26402638
norm_q_iter++;
26412639
}
2642-
2643-
/* Skip white spaces */
2644-
if (scanner_isspace(*norm_q_iter))
2640+
else if (scanner_isspace(*norm_q_iter))
26452641
{
2646-
while (scanner_isspace(*++norm_q_iter));
2642+
/* Convert white spaces into single space */
2643+
norm_q_iter++;
26472644

2648-
/*
2649-
* Let's replace it with a simple space. -1 is safe as we are
2650-
* making sure we are not at the start of the string.
2651-
*/
2652-
if (q_iter != query && !scanner_isspace(*(q_iter - 1)))
2653-
*q_iter++ = ' ';
2645+
while (scanner_isspace(*norm_q_iter))
2646+
norm_q_iter++;
26542647

2655-
continue;
2648+
/* Get rid of spaces at beginning and end */
2649+
if (q_iter != query && *norm_q_iter)
2650+
*q_iter++ = ' ';
2651+
}
2652+
else
2653+
{
2654+
*q_iter++ = *norm_q_iter++;
26562655
}
2657-
2658-
*q_iter++ = *norm_q_iter++;
26592656
}
26602657

2661-
/* Ensure we have a terminating zero at the end */
26622658
*q_iter = '\0';
26632659

2664-
/* Get rid of trailing spaces */
2665-
while (q_iter > query && *q_iter == '\0')
2666-
{
2667-
q_iter--;
2668-
2669-
/* Continue reducing the string size if space is found. */
2670-
if (scanner_isspace(*q_iter))
2671-
*q_iter = '\0';
2672-
}
2673-
2674-
/* Calculate the hash. */
2675-
pgsm_query_id = pgsm_hash_string(query, strlen(query));
2660+
pgsm_query_id = pgsm_hash_string(query, q_iter - query);
26762661

26772662
pfree(query);
2663+
26782664
return pgsm_query_id;
26792665
}
26802666

0 commit comments

Comments
 (0)