Skip to content

Commit

Permalink
Merge pull request #797 from mattmundell/reuse-iterator
Browse files Browse the repository at this point in the history
Reuse iterator from print_report_port_xml in print_report_xml_start
  • Loading branch information
mattmundell authored Oct 16, 2019
2 parents e4f7acf + 940089a commit 23eeeae
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Changed

### Fixed
- Add NULL check in nvts_feed_version_epoch [#768](https://github.com/greenbone/gvmd/pull/768)
- Improve performance of GET_REPORTS [#797](https://github.com/greenbone/gvmd/pull/797)
- Consider results_trash when deleting users [#800](https://github.com/greenbone/gvmd/pull/800)

### Removed
Expand Down
37 changes: 22 additions & 15 deletions src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -27901,31 +27901,31 @@ print_report_errors_xml (report_t report, FILE *stream)
* @param[in] sort_order Whether to sort ascending or descending.
* @param[in] sort_field Field to sort on.
* @param[out] host_ports Hash table for counting ports per host.
* @param[in,out] results Result iterator. For caller to reuse.
*
* @return 0 on success, -1 error.
*/
static int
print_report_port_xml (report_t report, FILE *out, const get_data_t *get,
int first_result, int max_results,
int sort_order, const char *sort_field,
GHashTable *host_ports)
GHashTable *host_ports, iterator_t *results)
{
iterator_t results;
result_buffer_t *last_item;
GArray *ports = g_array_new (TRUE, FALSE, sizeof (gchar*));

init_result_get_iterator (&results, get, report, NULL, NULL);
init_result_get_iterator (results, get, report, NULL, NULL);

/* Buffer the results, removing duplicates. */

last_item = NULL;
while (next (&results))
while (next (results))
{
const char *port = result_iterator_port (&results);
const char *host = result_iterator_host (&results);
const char *port = result_iterator_port (results);
const char *host = result_iterator_host (results);
double cvss_double;

cvss_double = result_iterator_severity_double (&results);
cvss_double = result_iterator_severity_double (results);

if (last_item
&& strcmp (port, last_item->port) == 0
Expand All @@ -27934,14 +27934,14 @@ print_report_port_xml (report_t report, FILE *out, const get_data_t *get,
{
last_item->severity_double = cvss_double;
g_free (last_item->severity);
last_item->severity = g_strdup (result_iterator_severity (&results));
last_item->severity = g_strdup (result_iterator_severity (results));
}
else
{
const char *cvss;
result_buffer_t *item;

cvss = result_iterator_severity (&results);
cvss = result_iterator_severity (results);
if (cvss == NULL)
{
cvss_double = 0.0;
Expand Down Expand Up @@ -28045,7 +28045,6 @@ print_report_port_xml (report_t report, FILE *out, const get_data_t *get,
g_array_free (ports, TRUE);
}
PRINT (out, "</ports>");
cleanup_iterator (&results);

return 0;
}
Expand Down Expand Up @@ -29240,6 +29239,7 @@ print_report_xml_start (report_t report, report_t delta, task_t task,
char *uuid, *tsk_uuid = NULL, *start_time, *end_time;
int total_result_count, filtered_result_count;
array_t *result_hosts;
int reuse_result_iterator;
iterator_t results, delta_results;
int debugs, holes, infos, logs, warnings, false_positives;
int f_debugs, f_holes, f_infos, f_logs, f_warnings, f_false_positives;
Expand Down Expand Up @@ -29878,10 +29878,12 @@ print_report_xml_start (report_t report, report_t delta, task_t task,
f_host_ports = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free, NULL);

reuse_result_iterator = 0;
if (get->details && (delta == 0))
{
reuse_result_iterator = 1;
if (print_report_port_xml (report, out, get, first_result, max_results,
sort_order, sort_field, f_host_ports))
sort_order, sort_field, f_host_ports, &results))
{
g_free (term);
tz_revert (zone, tz, old_tz_override);
Expand Down Expand Up @@ -29918,11 +29920,16 @@ print_report_xml_start (report_t report, report_t delta, task_t task,
{
int res;
g_free (term);
res = init_result_get_iterator (&results, get, report, NULL, NULL);
if (res)
if (reuse_result_iterator)
iterator_rewind (&results);
else
{
g_hash_table_destroy (f_host_ports);
return -1;
res = init_result_get_iterator (&results, get, report, NULL, NULL);
if (res)
{
g_hash_table_destroy (f_host_ports);
return -1;
}
}
}
else
Expand Down
3 changes: 3 additions & 0 deletions src/sql.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ init_prepared_iterator (iterator_t *, sql_stmt_t *);
void
init_iterator (iterator_t *, const char *, ...);

void
iterator_rewind (iterator_t *iterator);

double
iterator_double (iterator_t *, int);

Expand Down
14 changes: 14 additions & 0 deletions src/sql_pg.c
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,20 @@ iterator_null (iterator_t* iterator, int col)
return PQgetisnull (iterator->stmt->result, 0, col);
}

/**
* @brief Rewind an iterator to the beginning.
*
* This lets the caller iterate over the data again.
*
* @param[in] iterator Iterator.
*/
void
iterator_rewind (iterator_t* iterator)
{
iterator->done = FALSE;
iterator->stmt->current_row = -1;
}


/* Prepared statements. */

Expand Down

0 comments on commit 23eeeae

Please sign in to comment.